Skip to content

support expected_attach_type bpf_attr field#91

Merged
fuhry merged 1 commit into
dropbox:masterfrom
fuhry:add-expected-attach-type
Jun 30, 2026
Merged

support expected_attach_type bpf_attr field#91
fuhry merged 1 commit into
dropbox:masterfrom
fuhry:add-expected-attach-type

Conversation

@fuhry

@fuhry fuhry commented Jun 23, 2026

Copy link
Copy Markdown
Member

The expected_attach_type struct field was added quite a while ago but was not enforced on tailcalls until torvalds/linux@4540aed (CVE-2025-40123). On hosts with this patch applied, bpf_map_update_elem fails with -EINVAL when attempting to update a prog array map entry to a program with a non-matching expected_attach_type.

Add expected_attach_type arg to cgo API and pass via bpf_attr. Due to bpf_check_uarg_tail_zero, automatically retry if program load fails with non-zero expected attach type.

libbpf equivalent: bpf_program__set_expected_attach_type

@fuhry fuhry requested a review from ravenblackx June 23, 2026 20:30
@fuhry fuhry force-pushed the add-expected-attach-type branch from 60a4f11 to 2d1f9c6 Compare June 23, 2026 20:58
Comment thread ebpf.go Outdated
Comment thread program_base.go
type AttachType int

const (
AttachTypeCgroupInetIngress AttachType = iota

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any way to automatically validate that these are in sync with the values in the kernel header file?

This "just having them straight-up hardcoded here" model seems super risky with an off-by-one error making all subsequent enum values incorrect.

(Though also only the first one is actually used, so alternative question, why are these even here?)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This "just having them straight-up hardcoded here" model seems super risky with an off-by-one error making all subsequent enum values incorrect.

A lot of binary compatibility would break if the enum in bpf.h was to have any members deleted or inserted before the end, so I'm not particularly concerned about this happening. For this PR, I'll add a "keep in sync with" comment. I'll open a separate PR to //go:generate the AttachType and ProgramType enums in Go from the system's bpf.h file.

(Though also only the first one is actually used, so alternative question, why are these even here?)

AttachTypeXdp is referenced at program_xdp.go:82.

The [expected_attach_type](https://elixir.bootlin.com/linux/v6.12.94/source/include/uapi/linux/bpf.h#L1548) struct field was added quite a while ago but was not enforced on tailcalls until torvalds/linux@4540aed (CVE-2025-40123). On hosts with this patch applied, `bpf_map_update_elem` fails with `-EINVAL` when attempting to update a prog array map entry to a program with a non-matching `expected_attach_type`.

Add expected_attach_type arg to cgo API and pass via `bpf_attr`. Due to [bpf_check_uarg_tail_zero](https://github.com/torvalds/linux/blob/f31c00c377ccf07c85442712f7c940a855cb3371/kernel/bpf/syscall.c#L83-L113), automatically retry if program load fails with non-zero expected attach type.

libbpf equivalent: [bpf_program__set_expected_attach_type](https://docs.ebpf.io/ebpf-library/libbpf/userspace/bpf_program__set_expected_attach_type/)
@fuhry fuhry force-pushed the add-expected-attach-type branch from 2d1f9c6 to 3935b1e Compare June 30, 2026 16:46
@fuhry fuhry merged commit 6362619 into dropbox:master Jun 30, 2026
2 of 5 checks passed
@fuhry fuhry deleted the add-expected-attach-type branch June 30, 2026 20:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants