RFC bpf: Introduce BPF namespace
Get series with b4
b4 am [email protected]Versions newest first
- v1 RFC Latest13 patches 35 repliesIn review
Patches in v1 No applies recorded yet
| # | Subject and files | Size | Replies | Applied |
|---|---|---|---|---|
| 1/13 | fork: New clone3 flag for BPF namespaceinclude/ kernel/ | +4 | 0 replies | |
| 2/13 | proc_ns: Extend the field type in struct proc_ns_operations to longinclude/ | +1 | 0 replies | |
| 3/13 | bpf: Implement bpf namespacefs/ include/ include/ +6 more | +294 | 0 replies | |
| 4/13 | bpf: No need to check if id is 0kernel/ | +0 | 0 replies | |
| 5/13 | bpf: Make bpf objects id have the same alloc and free patternkernel/ kernel/ | +41 | 0 replies | |
| 6/13 | bpf: Helpers to alloc and free object id in bpf namespaceinclude/ kernel/ kernel/ | +125 | 0 replies | |
| 7/13 | bpf: Add bpf helper to get bpf object idinclude/ include/ kernel/ +2 more | +29 | 0 replies | |
| 8/13 | bpf: Alloc and free bpf_map id in bpf namespaceinclude/ kernel/ kernel/ +2 more | +30 | 2 replies | |
| 9/13 | bpf: Alloc and free bpf_prog id in bpf namespaceinclude/ kernel/ kernel/ +1 more | +22 | 0 replies | |
| 10/13 | bpf: Alloc and free bpf_link id in bpf namespaceinclude/ kernel/ kernel/ +1 more | +24 | 0 replies | |
| 11/13 | bpf: Allow iterating bpf objects with CAP_BPF in bpf namespaceinclude/ kernel/ | +13 | 0 replies | |
| 12/13 | bpf: Use bpf_idr_lock array insteadinclude/ kernel/ kernel/ | +28 | 0 replies | |
| 13/13 | selftests/bpf: Add selftest for bpf namespacetools/ tools/ | +78 | 0 replies |
Reviews trailers per patch
No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.
Cover letter [RFC PATCH bpf-next 00/13] bpf: Introduce BPF namespace
Currently only CAP_SYS_ADMIN can iterate BPF object IDs and convert IDs to FDs, that's intended for BPF's security model[1]. Not only does it prevent non-privilidged users from getting other users' bpf program, but also it prevents the user from iterating his own bpf objects.
In container environment, some users want to run bpf programs in their containers. These users can run their bpf programs under CAP_BPF and some other specific CAPs, but they can't inspect their bpf programs in a generic way. For example, the bpftool can't be used as it requires CAP_SYS_ADMIN. That is very inconvenient.
Without CAP_SYS_ADMIN, the only way to get the information of a bpf object which is not created by the process itself is with SCM_RIGHTS, that requires each processes which created bpf object has to implement a unix domain socket to share the fd of a bpf object between different processes, that is really trivial and troublesome.
Hence we need a better mechanism to get bpf object info without CAP_SYS_ADMIN.
BPF namespace is introduced in this patchset with an attempt to remove the CAP_SYS_ADMIN requirement. The user can create bpf map, prog and link in a specific bpf namespace, then these bpf objects will not be visible to the users in a different bpf namespace. But these bpf objects are visible to its parent bpf namespace, so the sys admin can still iterate and inspect them.
BPF namespace is similar to PID namespace, and the bpf objects are similar to tasks, so BPF namespace is very easy to understand. These patchset only implements BPF namespace for bpf map, prog and link. In the future we may extend it to other bpf objects like btf, bpffs and etc. For example, we can allow some of the BTF objects to be used in non-init bpf namespace, then the container user can only trace the processes running in his container, but can't get the information of tasks running in other containers.
A simple example is introduced into selftests/bpf on how to use the bpf namespace.
Putting bpf map, prog and link into bpf namespace is the first step. Let's start with it.
[1]. https://lore.kernel.org/bpf/[email protected]/
Yafang Shao (13):
fork: New clone3 flag for BPF namespace
proc_ns: Extend the field type in struct proc_ns_operations to long
bpf: Implement bpf namespace
bpf: No need to check if id is 0
bpf: Make bpf objects id have the same alloc and free pattern
bpf: Helpers to alloc and free object id in bpf namespace
bpf: Add bpf helper to get bpf object id
bpf: Alloc and free bpf_map id in bpf namespace
bpf: Alloc and free bpf_prog id in bpf namespace
bpf: Alloc and free bpf_link id in bpf namespace
bpf: Allow iterating bpf objects with CAP_BPF in bpf namespace
bpf: Use bpf_idr_lock array instead
selftests/bpf: Add selftest for bpf namespace
fs/proc/namespaces.c | 4 + include/linux/bpf.h | 9 +- include/linux/bpf_namespace.h | 88 ++++++++++ include/linux/nsproxy.h | 4 + include/linux/proc_ns.h | 3 +- include/linux/user_namespace.h | 1 + include/uapi/linux/bpf.h | 7 + include/uapi/linux/sched.h | 1 + kernel/bpf/Makefile | 1 + kernel/bpf/bpf_namespace.c | 283 ++++++++++++++++++++++++++++++ kernel/bpf/offload.c | 16 +- kernel/bpf/syscall.c | 262 ++++++++++----------------- kernel/bpf/task_iter.c | 12 ++ kernel/fork.c | 5 +- kernel/nsproxy.c | 19 +- kernel/trace/bpf_trace.c | 2 + kernel/ucount.c | 1 + tools/bpf/bpftool/skeleton/pid_iter.bpf.c | 13 +- tools/include/uapi/linux/bpf.h | 7 + tools/testing/selftests/bpf/Makefile | 3 +- tools/testing/selftests/bpf/test_bpfns.c | 76 ++++++++ 21 files changed, 637 insertions(+), 180 deletions(-) create mode 100644 include/linux/bpf_namespace.h create mode 100644 kernel/bpf/bpf_namespace.c create mode 100644 tools/testing/selftests/bpf/test_bpfns.c
Signature · 1 line
1.8.3.1