Skip to content

examples/c: reject unknown sockfilter interfaces - #377

Merged
danielocfb merged 1 commit into
libbpf:masterfrom
sairex53:fix/sockfilter-invalid-interface
Oct 20, 2026
Merged

danielocfb merged 1 commit into
libbpf:masterfrom
sairex53:fix/sockfilter-invalid-interface

Conversation

@sairex53

Copy link
Copy Markdown
Contributor

sockfilter -i <missing-interface> passes the zero returned by if_nametoindex() to bind(). For an AF_PACKET socket, index zero means all interfaces, so a misspelled interface silently broadens capture.

Reject a failed lookup and close the socket before returning.

Validation on Fedora 44 aarch64:

  • Built sockfilter with Clang 22 and the installed bpftool 7.6.0 (make -C examples/c CC=clang BPFTOOL=/usr/bin/bpftool sockfilter). The bundled bpftool build hits the existing Build fails with -Werror=discarded-qualifiers in libbpf.c #373.
  • Called the actual open_raw_sock() implementation with real AF_PACKET sockets in an isolated user/network namespace. Before this change, a nonexistent interface succeeded with ifindex 0; after it, the lookup fails and repeated failures leave the descriptor count unchanged. The existing lo interface still binds correctly.
  • Repeated the check with stdin closed, so the raw socket receives fd 0.

Privileged BPF load/attach was not tested; passwordless sudo is unavailable in this environment.

if_nametoindex() returns zero when the requested interface does not exist.
Passing that index to bind() on an AF_PACKET socket selects all interfaces
instead of reporting an error.

Reject a failed lookup and close the socket before returning.

Signed-off-by: Eduard Zateev <hackerowskiy@gmail.com>
@sairex53
sairex53 marked this pull request as ready for review October 18, 2026 06:20

@danielocfb danielocfb left a comment

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.

Seems fine to me, thanks

@danielocfb
danielocfb merged commit 3a24f87 into libbpf:master Oct 20, 2026
14 checks passed
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