Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backwards compatible fixes to bpf() syscall emulation #3870

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

lmb
Copy link
Contributor

@lmb lmb commented Sep 26, 2024

bpf(): do not return errors via errno

The Linux ABI returns all syscall errors via the function return value, not
via errno.

Fixes https://github.com/microsoft/ebpf-for-windows/issues/3749

Allow detecting if bpf() command is not implemented

Use SetLastError to indicate to callers that a bpf() command is not 
implemented. This avoids polluting the bpf() return value with platform
specific error returns while still allowing detection of this important
case.

Add forwards and backwards compatibility to bpf() emulation

On Linux, bpf() accepts a bpf_attr which is larger than what the syscall
expects, as long as the unknown fields are all 0. It also accepts a bpf_attr
which is smaller than what it expects, by assuming that all missing fields
are zero.

This allows forwards and backwards compatilibity between old and new 
versions of both the Linux kernel and user space tooling.

Implement a similar scheme for the bpf() emulation.

Return EPERM from bpf() if user is not privileged

On Linux, bpf() returns EPERM if the user doesn't have CAP_BPF. Return the
same error when the user isn't able to open the device handle.

The Linux ABI returns all syscall errors via the function return value,
not via errno.

Fixes microsoft#3749
Use SetLastError to indicate to callers that a bpf() command is not
implemented. This avoids polluting the bpf() return value with
platform specific error returns while still allowing detection of
this important case.
Copy link
Collaborator

@dthaler dthaler left a comment

Choose a reason for hiding this comment

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

Looks fine other than two style nits

libs/api/bpf_syscall.cpp Show resolved Hide resolved
libs/api/bpf_syscall.cpp Outdated Show resolved Hide resolved
On Linux, bpf() accepts a bpf_attr which is larger than what the
syscall expects, as long as the unknown fields are all 0. It also
accepts a bpf_attr which is smaller than what it expects, by assuming
that all missing fields are zero.

This allows forwards and backwards compatilibity between old and new
versions of both the Linux kernel and user space tooling.

Implement a similar scheme for the bpf() emulation.
On Linux, bpf() returns EPERM if the user doesn't have CAP_BPF. Return
the same error when the user isn't able to open the device handle.
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