[bug]: zpay32.Decode wraps an invoice amount that overflows uint64

Open Beginner friendly
#11,415 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

  • #11441 by @Reacher3745 — closed without merging

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
79/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
go
Domain
backend, payments

Research direction

Start with the multiplication sites in zpay32/amountunits.go at lines 30–43 and 99–105, then run the provided TestAmountWrap reproduction with go test ./zpay32 -run TestAmountWrap -v. Check that zpay32.Decode rejects an invoice when converting its amount would overflow uint64; done when the reproduction returns an error instead of a wrapped MilliSat value.

Written by the indexing model from the issue text.

Description

bug needs triage
Pre-Submission Checklist
  • I have searched the existing issues and believe this is a new bug.
  • I am not asking a question about how to use lnd, but reporting a bug (otherwise open a discussion).
LND Version

master at 1a359d4aa1e5, v0.21.4-beta at 390b2d2b7bdc, and v0.20.5-beta at cfa58463aedc

LND Configuration

None. The test below runs with go test in the lnd source tree at each version above.

Backend Version

Not used. The test runs with go test only.

Backend Configuration

None.

OS/Distribution

Fedora Linux 44, x86_64, with Go 1.26.10 for master and Go 1.26.9 for the releases

Bug Details & Steps to Reproduce

zpay32.Decode accepts the invoice below and returns a MilliSat of 90,448,384 msat. Its amount, 184467440738m, is 18,446,744,073,800,000,000 msat, and 90,448,384 msat is that product modulo 2^64. The payment intent that SendPaymentV2 builds for this invoice carries 90,448,384 msat as its amount. This happens on master at 1a359d4aa1e5, v0.21.4-beta and v0.20.5-beta.

Saved as zpay32/amount_wrap_test.go on master, this test decodes the invoice, which is signed with the private key from the BOLT 11 examples:

package zpay32

import (
	"testing"

	"github.com/btcsuite/btcd/chaincfg/v2"
)

func TestAmountWrap(t *testing.T) {
	inv, err := Decode("lnbc184467440738m1pvjluezpp5qqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqypqdq5vdhkven9v5sxyetpdeessp5zyg3zyg3zyg3zyg3zyg3zyg3zyg3zyg3zyg3zyg3zyg3zyg3zygs9qrsgqujcur0j9f47men4nlh79jjnav8799h55ufx3dyhec962ctkpv6u8dz9j36avrfwph0f0l562f05a769wkdkpqe3wkpre66ea5udgfqcpm5v6f3", &chaincfg.MainNetParams)
	if err == nil {
		t.Fatalf("expected error, got MilliSat: %v", *inv.MilliSat)
	}
}

With go test ./zpay32 -run TestAmountWrap -v, the test fails:

=== RUN   TestAmountWrap
    amount_wrap_test.go:12: expected error, got MilliSat: 90448384 mSAT
--- FAIL: TestAmountWrap (0.00s)

zpay32 multiplies the amount by its multiplier in a uint64 without an overflow check. The products are at zpay32/amountunits.go:30-43 for m, u and n, and at zpay32/amountunits.go:99-105 when there is no multiplier.

Expected Behavior

zpay32.Decode should return an error for this invoice, because 18,446,744,073,800,000,000 msat does not fit in a uint64. BOLT 11 says at lines 83-85 that a reader "MUST multiply amount by the multiplier value to derive the amount required for payment".

Debug Information

No response

Environment

No response

Dominant language
Go
Stars
8.2k
Forks
2.3k
Avg merge
1d 11h
Merged PRs (30d)
103

Getting set up

  • Ships a Dockerfile or Docker Compose file
  • Has a pull request template
  • No contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from lightningnetwork/lnd

All issues in lightningnetwork/lnd

Similar issues

More Go issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.