[bug]: Known TLV records in lnwire decode with the wrong length and shift the stream

Open
#11,295 1 comment 0 reactions 0 assignees View on GitHub

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 ignores l.
    tlv/record.go:276-293 (MakeBigSizeRecord) behaves the same for
    BigSizeT records. This affects channel_update_2 types 12 and 14,
    dyn_commit types 2 and 4, and dyn_propose types 2 and 4.
  • Color (lnwire/node_announcement_2.go:346-352, rgbDecoder) always reads
    3 bytes regardless of l.
  • TrueBoolean (lnwire/channel_update_2.go:457-466, booleanDecoder)
    returns success for l == 0 || l == 1 and 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 type is known:

  • MUST decode the next length bytes using the known encoding for type.
  • if length is not exactly equal to that required for the known encoding
    for type:
    • 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
    ignores l; tlv/record.go:276-293 (MakeBigSizeRecord) is the same for
    BigSizeT.
  • lnwire/node_announcement_2.go:346-352 (rgbDecoder) reads R, G, B with
    ReadElements and ignores l.
  • lnwire/channel_update_2.go:457-466 (booleanDecoder) returns success for
    l == 0 || l == 1 and 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

  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.