[bug]: Known TLV records in lnwire decode with the wrong length and shift the stream
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 74/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- go
- Domain
- networking
Research direction
Start at tlv/stream.go:238-250, where the stream hands a record to its decoder without bounding the reader to length or checking bytes consumed, then read the three offending decoders: lnwire/msat.go decodeMilliSatoshis, lnwire/node_announcement_2.go rgbDecoder, and lnwire/channel_update_2.go booleanDecoder. Decide whether to enforce the invariant centrally in the stream decoder or per-decoder (the fixed-size decoders already self-check), and check tlv/primitive.go DBigSize and tlv/record.go MakeBigSizeRecord. Done means all four hex inputs in the issue make lnwire.ReadMessage fail with a parse error while existing lnwire and tlv tests still pass — verify with the Go repro program in the issue body.
Written by the indexing model from the issue text.
Description
Description
The lnwire TLV stream decoder calls each known record's decoder with the
declared length but does not check that the decoder consumed exactly
length bytes. Three record decoders consume a different number of bytes than
the record declares:
MilliSatoshi(lnwire/msat.go:76-92,decodeMilliSatoshis) calls
tlv.DBigSize, which reads one BigSize value and ignoresl.
tlv/record.go:276-293(MakeBigSizeRecord) behaves the same for
BigSizeTrecords. This affectschannel_update_2types 12 and 14,
dyn_committypes 2 and 4, anddyn_proposetypes 2 and 4.Color(lnwire/node_announcement_2.go:346-352,rgbDecoder) always reads
3 bytes regardless ofl.TrueBoolean(lnwire/channel_update_2.go:457-466,booleanDecoder)
returns success forl == 0 || l == 1and never reads the reader.
tlv/stream.go:238-250 continues at the reader's actual position after the
decoder returns, so any byte the decoder did not consume is reparsed as the
next record's type. A message that BOLT 1 requires the receiver to fail to
parse instead decodes as a different message.
The fixed-size primitive decoders check the declared length themselves.
Existing length-validation tests cover those decoders and cannot reach the
three decoders above. The MilliSatoshi and BigSizeT decoders read
variable-length values from lnwire/msat.go and the tlv primitive and
ignore the record length, and the Color and TrueBoolean decoders live in
the two gossip-v2 message files, so length validation remains incomplete for
all four messages listed below.
Steps to reproduce
Each input below is a complete message in hex.
010d0102010203 node_announcement_2, Color length 2
010d01040102030421abababababababababababababababababababababababababababababababab
node_announcement_2, Color length 4, then NodeID record
010f0c03010d00 channel_update_2, MilliSatoshi length 3
010f08010a020001 channel_update_2, TrueBoolean length 1, then CLTV record
package main
import (
"bytes"
"encoding/hex"
"fmt"
"github.com/lightningnetwork/lnd/lnwire"
)
func main() {
inputs := []string{
"010d0102010203",
"010f0c03010d00",
"010f08010a020001",
}
for _, in := range inputs {
b, _ := hex.DecodeString(in)
msg, err := lnwire.ReadMessage(bytes.NewReader(b), 0)
fmt.Printf("%s err=%v msg=%T\n", in, err, msg)
}
}
Observed:
010d0102010203 err=<nil> msg=*lnwire.NodeAnnouncement2
010f0c03010d00 err=<nil> msg=*lnwire.ChannelUpdate2
010f08010a020001 err=<nil> msg=*lnwire.ChannelUpdate2
All three malformed messages decode without error. In the first input the
Color record declares length 2, the decoder consumes 3 bytes, and the stream
still ends clean. In the second and third inputs the decoder consumes fewer
bytes than declared, and the leftover bytes are parsed as further TLV records
(type 4 NodeID with length 0x21, type 10 CLTV with length 2).
Expected behavior
BOLT 1 (01-messaging.md, Type-Length-Value Format, receiving node rules):
if
typeis known:
- MUST decode the next
lengthbytes using the known encoding fortype.- if
lengthis not exactly equal to that required for the known encoding
fortype:
- MUST fail to parse the
tlv_stream.
A conforming reader fails the parse for all four inputs. The current reader
accepts them and changes the parsed message.
Root cause
tlv/stream.go:238-250 hands the record to its decoder but neither bounds the
reader to length nor verifies that the decoder consumed exactly that many
bytes. Three decoders do not consume length bytes:
lnwire/msat.go:76-92(decodeMilliSatoshis) relies on
tlv.DBigSize(tlv/primitive.go:356-376), which reads one varint and
ignoresl;tlv/record.go:276-293(MakeBigSizeRecord) is the same for
BigSizeT.lnwire/node_announcement_2.go:346-352(rgbDecoder) reads R, G, B with
ReadElementsand ignoresl.lnwire/channel_update_2.go:457-466(booleanDecoder) returns success for
l == 0 || l == 1and never reads the reader.
The fixed-size decoders check l themselves, so the invariant depends on each
decoder implementation rather than on the stream decoder.
- 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
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from lightningnetwork/lnd
-
funding: failure logs print `<nil>` error and zero channel IDPossibly taken @litbot-9000 claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 90/100
lightningnetwork/lnd#11676 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 Half a day Newbie friendliness 85/100
lightningnetwork/lnd#11674 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
lightningnetwork/lnd#11672 ·
Maintainers usually reply within 1 day
-
test flake
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
lightningnetwork/lnd#11663 · 1 comment ·
Maintainers usually reply within 1 day
-
[flake]: lnwallet/test/bitcoind.TestLightningWalletBitcoindRPCPolling/spend_unconfirmed_outputsOpentest flake
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
lightningnetwork/lnd#11642 · 1 comment ·
Maintainers usually reply within 1 day
All issues in lightningnetwork/lnd
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
containers/gvisor-tap-vsock#796 ·
Maintainers usually reply within 2 days
-
external needs:triage
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
celestiaorg/celestia-node#5448 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 79/100
Maintainers usually reply within 1 day
-
[Bug]: Azure SAS signing uses local time instead of UTC, shifting validity window on non-UTC hostsPossibly taken @huanghaoyuanhhy claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
zilliztech/milvus-backup#1418 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
projectdiscovery/nuclei#8423 ·
Maintainers usually reply within 1 day