Skip to content

bpf: allow cgroup progs to export custom errnos to userspace

YiFei Zhu bpf 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 Latest3 patches 15 repliesIn review

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/3bpf: Make BPF_PROG_RUN_ARRAY return -errno instead of allow booleaninclude/linux/bpf.h kernel/bpf/cgroup.c security/device_cgroup.c+25 -341 reply
2/3bpf: Add cgroup helper bpf_export_errno to get/set exported errno valueinclude/linux/bpf.h include/uapi/linux/bpf.h kernel/bpf/cgroup.c +1 more+63 -1213 replies
3/3selftests/bpf: Test bpf_export_errno behavior with cgroup/sockopttools/testing/selftests/bpf/prog_tests/cgroup_export_errno.c tools/testing/selftests/bpf/progs/cgroup_export_errno_getsockopt.c tools/testing/selftests/bpf/progs/cgroup_export_errno_setsockopt.c+569 -01 reply

Reviews trailers per patch

All 3 patches have a Reviewed-by or Acked-by.

PatchStanislav FomichevSong Liu
1/3R-b A-b
2/3R-b A-b
3/3R-b A-b

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v1 thread

Cover letter [PATCH bpf-next 0/3] bpf: allow cgroup progs to export custom errnos to userspace

From: YiFei Zhu <[email protected]>

Right now, most cgroup hooks are best used for permission checks. They can only reject a syscall with -EPERM, so a cause of a rejection, if the rejected by eBPF cgroup hooks, is ambiguous to userspace. Additionally, if the syscalls are implemented in eBPF, all permission checks and the implementation has to happen within the same filter, as programs executed later in the series of progs are unaware of the return values return by the previous progs.

This patch series adds a helper, bpf_export_errno, that allows hooks to get/set the errno exported by eBPF to userspace. Invoking the helper with a positive errno will set the exported errno, and invoking the helper with zero will return the previously set errno. This means that an errno, once set, can be overridden but cannot be unset. This also allows later progs to retrieve errnos set by previous progs.

For legacy programs that rejects a syscall without setting the exported errno, for backwards compatibility, if a prog rejects without itself or a prior prog setting errno, the errno is set by the kernel to -EPERM.

Tests have been added in this series to test the behavior of the helper with cgroup setsockopt getsockopt hooks.

Patch 1 changes the API of macros to prepare for the next patch and
should be a no-op.
Patch 2 implements the helper and the tracking of errno. Patch 3 tests the behaviors of the helper.

YiFei Zhu (3):
bpf: Make BPF_PROG_RUN_ARRAY return -errno instead of allow boolean
bpf: Add cgroup helper bpf_export_errno to get/set exported errno
value
selftests/bpf: Test bpf_export_errno behavior with cgroup/sockopt

 include/linux/bpf.h                           |  27 +-
 include/uapi/linux/bpf.h                      |  14 +
 kernel/bpf/cgroup.c                           |  65 ++-
 security/device_cgroup.c                      |   2 +-
 tools/include/uapi/linux/bpf.h                |  14 +
 .../bpf/prog_tests/cgroup_export_errno.c      | 472 ++++++++++++++++++
 .../progs/cgroup_export_errno_getsockopt.c    |  45 ++
 .../progs/cgroup_export_errno_setsockopt.c    |  52 ++
 8 files changed, 651 insertions(+), 40 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/cgroup_export_errno.c
 create mode 100644 tools/testing/selftests/bpf/progs/cgroup_export_errno_getsockopt.c
 create mode 100644 tools/testing/selftests/bpf/progs/cgroup_export_errno_setsockopt.c

--
2.33.0