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

Aperta
#11,295 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
74/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
go
Ambito
networking

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
Go
Stelle
8.2k
Fork
2.3k
Merge medio
1g 11h
PR unite (30g)
103

Preparare l'ambiente

  • Include un Dockerfile o un file Docker Compose
  • Ha un modello di pull request
  • Nessuna guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di lightningnetwork/lnd

Tutte le issue di lightningnetwork/lnd

Issue simili

Altre issue su Go

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.