Skip to content

btf: raw BTF parser panics when HdrLen exceeds available header bytes #2022

Closed

Description

@arunsingh

Malformed raw BTF input can panic the parser through btf.LoadSpecFromReader.

The panic is in parseBTFHeader. A malformed BTF header can set HdrLen larger than the bytes available after the fixed header. parseBTFHeader computes the optional header-trailer length and then slices buf[:remainder] before checking whether that many bytes are actually present.

Expected behavior:

Malformed raw BTF should return a parse error and a nil spec.

Actual behavior:

The parser panics with a slice bounds violation.

This is separate from #2019: that issue is about string-table offsets equal to the string table length; this one is about the raw BTF header length being trusted before the buffer length check runs.

reproducing the bug

I reproduced this on upstream main at commit edd6d12.

A minimal regression test using the public API:

package btf_test

import (
	"bytes"
	"testing"

	"github.com/cilium/ebpf/btf"
)

func TestHdrLenPanicRepro(t *testing.T) {
	input := []byte("\x9f\xeb\x01\x0000000000000000000000")
	_, err := btf.LoadSpecFromReader(bytes.NewReader(input))
	if err == nil {
		t.Fatal("expected error")
	}
}

Run:

go test ./btf -run TestHdrLenPanicRepro -count=1

Observed failure:

panic: runtime error: slice bounds out of range [:808464408] with capacity 488
github.com/cilium/ebpf/btf.parseBTFHeader
btf/btf_types.go:123

I also hit this through the existing fuzz target:

go test ./btf -run '^$' -fuzz=FuzzSpec -fuzztime=20s

The relevant code path is:

btf.LoadSpecFromReader
  -> loadRawSpec
  -> parseBTFHeader

parseBTFHeader checks that HdrLen is not shorter than the fixed header, but it does not check that the optional header trailer is present before slicing:

remainder := int64(header.HdrLen) - int64(binary.Size(&header))
...
for _, b := range buf[:remainder] {

The later loadRawSpec HdrLen bounds check is too late because the panic has already occurred.

Version information

Reproduced on upstream main at edd6d12.

go version go1.26.3 linux/amd64
Linux amd64

Suggested patch

Validate that the computed trailer length is available before slicing:

if remainder > int64(len(buf)) {
	return nil, nil, errors.New("header length is out of bounds")
}

Then add a regression test or fuzz corpus entry for the minimized input.

Activity

  1. dylandreimerink commented on May 28, 2026

    @dylandreimerink
    Member

    I found this independently while running the fuzzer as well (great minds think alike). It has already been fixed in c8c6a42

  2. arunsingh commented on May 28, 2026

    @arunsingh
    ContributorAuthor

    I found this independently while running the fuzzer as well (great minds think alike). It has already been fixed in c8c6a42

    Thanks @dylandreimerink for your efforts and addressing this, I appreciate your coordination.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions