BPF token
Subsystems (14)
Andrii Nakryiko
maintainerv3 cover BPF token
This patch set introduces new BPF object, BPF token, which allows to delegate a subset of BPF functionality from privileged system-wide daemon (e.g., systemd or any other container manager) to a *trusted* unprivileged application. Trust is the key here. This functionality is not about allowing unconditional unprivileged BPF usage. Establishing trust, though, is completely up to the discretion of respective privileged application that would create a BPF token, as different production setups can and do achieve it through a combination of different means (signing, LSM, code reviews, etc), and it's undesirable and infeasible for kernel to enforce any particular way of validating trustworthiness of particular process.
The main motivation for BPF token is a desire to enable containerized BPF applications to be used together with user namespaces. This is currently impossible, as CAP_BPF, required for BPF subsystem usage, cannot be namespaced or sandboxed, as a general rule. E.g., tracing BPF programs, thanks to BPF helpers like bpf_probe_read_kernel() and bpf_probe_read_user() can safely read arbitrary memory, and it's impossible to ensure that they only read memory of processes belonging to any given namespace. This means that it's impossible to have namespace-aware CAP_BPF capability, and as such another mechanism to allow safe usage of BPF functionality is necessary. BPF token and delegation of it to a trusted unprivileged applications is such mechanism. Kernel makes no assumption about what "trusted" constitutes in any particular case, and it's up to specific privileged applications and their surrounding infrastructure to decide that. What kernel provides is a set of APIs to create and tune BPF token, and pass it around to privileged BPF commands that are creating new BPF objects like BPF programs, BPF maps, etc.
Previous attempt at addressing this very same problem ([0]) attempted to utilize authoritative LSM approach, but was conclusively rejected by upstream LSM maintainers. BPF token concept is not changing anything about LSM approach, but can be combined with LSM hooks for very fine-grained security policy. Some ideas about making BPF token more convenient to use with LSM (in particular custom BPF LSM programs) was briefly described in recent LSF/MM/BPF 2023 presentation ([1]). E.g., an ability to specify user-provided data (context), which in combination with BPF LSM would allow implementing a very dynamic and fine-granular custom security policies on top of BPF token. In the interest of minimizing API surface area discussions this is going to be added in follow up patches, as it's not essential to the fundamental concept of delegatable BPF token.
It should be noted that BPF token is conceptually quite similar to the idea of /dev/bpf device file, proposed by Song a while ago ([2]). The biggest difference is the idea of using virtual anon_inode file to hold BPF token and allowing multiple independent instances of them, each with its own set of restrictions. BPF pinning solves the problem of exposing such BPF token through file system (BPF FS, in this case) for cases where transferring FDs over Unix domain sockets is not convenient. And also, crucially, BPF token approach is not using any special stateful task-scoped flags. Instead, bpf() syscall accepts token_fd parameters explicitly for each relevant BPF command. This addresses main concerns brought up during the /dev/bpf discussion, and fits better with overall BPF subsystem design.
This patch set adds a basic minimum of functionality to make BPF token useful and to discuss API and functionality. Currently only low-level libbpf APIs support passing BPF token around, allowing to test kernel functionality, but for the most part is not sufficient for real-world applications, which typically use high-level libbpf APIs based on struct bpf_object type. This was done with the intent to limit the size of patch set and concentrate on mostly kernel-side changes. All the necessary plumbing for libbpf will be sent as a separate follow up patch set kernel support makes it upstream.
Another part that should happen once kernel-side BPF token is established, is a set of conventions between applications (e.g., systemd), tools (e.g., bpftool), and libraries (e.g., libbpf) about sharing BPF tokens through BPF FS at well-defined locations to allow applications take advantage of this in automatic fashion without explicit code changes on BPF application's side. But I'd like to postpone this discussion to after BPF token concept lands.
Once important distinctions from v2 that should be noted is a chance in the semantics of a newly added BPF_TOKEN_CREATE command. Previously, BPF_TOKEN_CREATE would create BPF token kernel object and return its FD to user-space, allowing to (optionally) pin it in BPF FS using BPF_OBJ_PIN command. This v3 version changes this slightly: BPF_TOKEN_CREATE combines BPF token object creation *and* pinning in BPF FS. Such change ensures that BPF token is always associated with a specific instance of BPF FS and cannot "escape" it by application re-pinning it somewhere else using another BPF_OBJ_PIN call. Now, BPF token can only be pinned once during its creation, better containing it inside intended container (under assumption BPF FS is set up in such a way as to not be shared with other containers on the system).
[0] https://lore.kernel.org/bpf/[email protected]/
[1] http://vger.kernel.org/bpfconf2023_material/Trusted_unprivileged_BPF_LSFMM2023.pdf
[2] https://lore.kernel.org/bpf/[email protected]/
v2->v3:
- make BPF_TOKEN_CREATE pin created BPF token in BPF FS, and disallow
BPF_OBJ_PIN for BPF token;
v1->v2:
- fix build failures on Kconfig with CONFIG_BPF_SYSCALL unset;
- drop BPF_F_TOKEN_UNKNOWN_* flags and simplify UAPI (Stanislav).
Andrii Nakryiko (14):
bpf: introduce BPF token object
libbpf: add bpf_token_create() API
selftests/bpf: add BPF_TOKEN_CREATE test
bpf: add BPF token support to BPF_MAP_CREATE command
libbpf: add BPF token support to bpf_map_create() API selftests/bpf: add BPF token-enabled test for BPF_MAP_CREATE command
bpf: add BPF token support to BPF_BTF_LOAD command
libbpf: add BPF token support to bpf_btf_load() API selftests/bpf: add BPF token-enabled BPF_BTF_LOAD selftest
bpf: add BPF token support to BPF_PROG_LOAD command
bpf: take into account BPF token when fetching helper protos
bpf: consistenly use BPF token throughout BPF verifier logic
libbpf: add BPF token support to bpf_prog_load() API selftests/bpf: add BPF token-enabled BPF_PROG_LOAD tests
drivers/media/rc/bpf-lirc.c | 2 +- include/linux/bpf.h | 79 ++++- include/linux/filter.h | 2 +- include/uapi/linux/bpf.h | 53 ++++ kernel/bpf/Makefile | 2 +- kernel/bpf/arraymap.c | 2 +- kernel/bpf/cgroup.c | 6 +- kernel/bpf/core.c | 3 +- kernel/bpf/helpers.c | 6 +- kernel/bpf/inode.c | 46 ++- kernel/bpf/syscall.c | 183 +++++++++--- kernel/bpf/token.c | 201 +++++++++++++ kernel/bpf/verifier.c | 13 +- kernel/trace/bpf_trace.c | 2 +- net/core/filter.c | 36 +-- net/ipv4/bpf_tcp_ca.c | 2 +- net/netfilter/nf_bpf_link.c | 2 +- tools/include/uapi/linux/bpf.h | 53 ++++ tools/lib/bpf/bpf.c | 35 ++- tools/lib/bpf/bpf.h | 45 ++- tools/lib/bpf/libbpf.map | 1 + .../selftests/bpf/prog_tests/libbpf_probes.c | 4 + .../selftests/bpf/prog_tests/libbpf_str.c | 6 + .../testing/selftests/bpf/prog_tests/token.c | 277 ++++++++++++++++++ 24 files changed, 957 insertions(+), 104 deletions(-) create mode 100644 kernel/bpf/token.c create mode 100644 tools/testing/selftests/bpf/prog_tests/token.c
Signature · 1 line
2.34.1
Andrii Nakryiko
maintainerPATCH v3 1/14 bpf: introduce BPF token object
Add new kind of BPF kernel object, BPF token. BPF token is meant to to allow delegating privileged BPF functionality, like loading a BPF program or creating a BPF map, from privileged process to a *trusted* unprivileged process, all while have a good amount of control over which privileged operations could be performed using provided BPF token.
This patch adds new BPF_TOKEN_CREATE command to bpf() syscall, which allows to create a new BPF token object along with a set of allowed commands that such BPF token allows to unprivileged applications. Currently only BPF_TOKEN_CREATE command itself can be delegated, but other patches gradually add ability to delegate BPF_MAP_CREATE, BPF_BTF_LOAD, and BPF_PROG_LOAD commands.
The above means that new BPF tokens can be created using existing BPF token, if original privileged creator allowed BPF_TOKEN_CREATE command. New derived BPF token cannot be more powerful than the original BPF token.
Importantly, BPF token is automatically pinned at the specified location inside an instance of BPF FS and cannot be repinned using BPF_OBJ_PIN command, unlike BPF prog/map/btf/link. This provides more control over unintended sharing of BPF tokens through pinning it in another BPF FS instances.
--- include/linux/bpf.h | 47 ++++++++++ include/uapi/linux/bpf.h | 38 ++++++++ kernel/bpf/Makefile | 2 +- kernel/bpf/inode.c | 46 +++++++-- kernel/bpf/syscall.c | 17 ++++ kernel/bpf/token.c | 167 +++++++++++++++++++++++++++++++++ tools/include/uapi/linux/bpf.h | 38 ++++++++ 7 files changed, 344 insertions(+), 11 deletions(-) create mode 100644 kernel/bpf/token.c
include/linux/bpf.h +47 -0
@@ -51,6 +51,7 @@ struct module;struct bpf_func_state ;struct ftrace_ops ;struct cgroup ; +struct bpf_token ;extern struct idr btf_idr;extern spinlock_t btf_idr_lock;
@@ -1533,6 +1534,12 @@ struct bpf_link_primer {u32 id; }; +struct bpf_token { +struct work_struct work; +atomic64_t refcnt; +u64 allowed_cmds; +}; +struct bpf_struct_ops_value ;struct btf_member ;
@@ -1916,6 +1923,11 @@ bpf_prog_run_array_sleepable(const struct bpf_prog_array __rcu *array_rcu,return ret; } +static inline bool bpf_token_capable (const struct bpf_token *token,int cap) +{ +return token ||capable (cap) || (cap !=CAP_SYS_ADMIN &&capable (CAP_SYS_ADMIN )); +} +#ifdef CONFIG_BPF_SYSCALL DECLARE_PER_CPU (int, bpf_prog_active);extern struct mutex bpf_stats_enabled_mutex;
@@ -2077,8 +2089,25 @@ struct file *bpf_link_new_file(struct bpf_link *link, int *reserved_fd);struct bpf_link *bpf_link_get_from_fd (u32 ufd);struct bpf_link *bpf_link_get_curr_or_next (u32 *id); +void bpf_token_inc (struct bpf_token *token); +void bpf_token_put (struct bpf_token *token); +int bpf_token_create (union bpf_attr *attr); +int bpf_token_new_fd (struct bpf_token *token); +struct bpf_token *bpf_token_get_from_fd (u32 ufd); + +bool bpf_token_allow_cmd (const struct bpf_token *token,enum bpf_cmd cmd); + +enum bpf_type { +BPF_TYPE_UNSPEC =0 , +BPF_TYPE_PROG , +BPF_TYPE_MAP , +BPF_TYPE_LINK , +BPF_TYPE_TOKEN , +}; +int bpf_obj_pin_user (u32 ufd,int path_fd,const char __user *pathname);int bpf_obj_get_user (int path_fd,const char __user *pathname,int flags); +int bpf_obj_pin_any (int path_fd,const char __user *pathname,void *raw,enum bpf_type type);#define BPF_ITER_FUNC_PREFIX "bpf_iter_" #define DEFINE_BPF_ITER_FUNC(target, args...) \
@@ -2436,6 +2465,24 @@ static inline int bpf_obj_get_user(const char __user *pathname, int flags)return -EOPNOTSUPP ; } +static inline void bpf_token_inc (struct bpf_token *token) +{ +} + +static inline void bpf_token_put (struct bpf_token *token) +{ +} + +static inline int bpf_token_new_fd (struct bpf_token *token) +{ +return -EOPNOTSUPP ; +} + +static inline struct bpf_token *bpf_token_get_from_fd (u32 ufd) +{ +return ERR_PTR (-EOPNOTSUPP ); +} +static inline void __dev_flush (void ) { }
include/uapi/linux/bpf.h +38 -0
@@ -846,6 +846,24 @@ union bpf_iter_link_info {* Returns zero on success. On error, -1 is returned and *errno* * is set appropriately. * +* BPF_TOKEN_CREATE +* Description +* Create BPF token with embedded information about what +* BPF-related functionality it allows. This BPF token can be +* passed as an extra parameter to various bpf() syscall commands +* to grant BPF subsystem functionality to unprivileged processes. +* BPF token is automatically pinned at specified location in BPF +* FS. It can be retrieved (to get FD passed to bpf() syscall) +* using BPF_OBJ_GET command. It's not allowed to re-pin BPF +* token using BPF_OBJ_PIN command. Such restrictions ensure BPF +* token stays associated with originally intended BPF FS +* instance and cannot be intentionally or unintentionally pinned +* somewhere else. +* +* Return +* Returns zero on success. On error, -1 is returned and *errno* +* is set appropriately. +* * NOTES * eBPF objects (maps and programs) can be shared between processes. *
@@ -900,6 +918,7 @@ enum bpf_cmd {BPF_ITER_CREATE ,BPF_LINK_DETACH ,BPF_PROG_BIND_MAP , + BPF_TOKEN_CREATE, }; enum bpf_map_type {
@@ -1621,6 +1640,25 @@ union bpf_attr {__u32 flags;/* extra flags */ } prog_bind_map; +struct {/* struct used by BPF_TOKEN_CREATE command */ +/* optional, BPF token FD granting operation */ +__u32 token_fd; +__u32 token_flags; +__u32 pin_flags; +/* pin_{path_fd,pathname} specify location in BPF FS instance +* to pin BPF token at; +* path_fd + pathname have the same semantics as openat() syscall +*/ +__u32 pin_path_fd; +__u64 pin_pathname; +/* a bit set of allowed bpf() syscall commands, +* e.g., (1ULL << BPF_TOKEN_CREATE) | (1ULL << BPF_PROG_LOAD) +* will allow creating derived BPF tokens and loading new BPF +* programs +*/ +__u64 allowed_cmds; + } token_create; + } __attribute__((aligned(8)));/* The description below is an attempt at providing documentation to eBPF
kernel/bpf/Makefile +1 -1
@@ -6,7 +6,7 @@ cflags-nogcse-$(CONFIG_X86)$(CONFIG_CC_IS_GCC) := -fno-gcse endif CFLAGS_core.o += $(call cc-disable-warning, override-init) $(cflags-nogcse-yy) -obj-$(CONFIG_BPF_SYSCALL) += syscall.o verifier.o inode.o helpers.o tnum.o log.o +obj-$(CONFIG_BPF_SYSCALL) += syscall.o verifier.o inode.o helpers.o tnum.o log.o token.o obj-$(CONFIG_BPF_SYSCALL) += bpf_iter.o map_iter.o task_iter.o prog_iter.o link_iter.o obj-$(CONFIG_BPF_SYSCALL) += hashtab.o arraymap.o percpu_freelist.o bpf_lru_list.o lpm_trie.o map_in_map.o bloom_filter.o obj-$(CONFIG_BPF_SYSCALL) += local_storage.o queue_stack_maps.o ringbuf.o
kernel/bpf/inode.c +36 -10
112 lines not on this page · raw message
kernel/bpf/syscall.c +17 -0
@@ -5081,6 +5081,20 @@ static int bpf_prog_bind_map(union bpf_attr *attr)return ret; } +#define BPF_TOKEN_CREATE_LAST_FIELD token_create.allowed_cmds + +static int token_create (union bpf_attr *attr) +{ +if (CHECK_ATTR (BPF_TOKEN_CREATE )) +return -EINVAL ; + +/* no flags are supported yet */ +if (attr->token_create.token_flags || attr->token_create.pin_flags) +return -EINVAL ; + +return bpf_token_create (attr); +} +static int __sys_bpf (int cmd,bpfptr_t uattr,unsigned int size) {union bpf_attr attr;
@@ -5214,6 +5228,9 @@ static int __sys_bpf(int cmd, bpfptr_t uattr, unsigned int size)case BPF_PROG_BIND_MAP : err =bpf_prog_bind_map (&attr);break ; +case BPF_TOKEN_CREATE : + err =token_create (&attr); +break ;default : err = -EINVAL ;break ;
kernel/bpf/token.c new file +167 -0
167 lines not on this page · raw message
tools/include/uapi/linux/bpf.h +38 -0
@@ -846,6 +846,24 @@ union bpf_iter_link_info {* Returns zero on success. On error, -1 is returned and *errno* * is set appropriately. * +* BPF_TOKEN_CREATE +* Description +* Create BPF token with embedded information about what +* BPF-related functionality it allows. This BPF token can be +* passed as an extra parameter to various bpf() syscall commands +* to grant BPF subsystem functionality to unprivileged processes. +* BPF token is automatically pinned at specified location in BPF +* FS. It can be retrieved (to get FD passed to bpf() syscall) +* using BPF_OBJ_GET command. It's not allowed to re-pin BPF +* token using BPF_OBJ_PIN command. Such restrictions ensure BPF +* token stays associated with originally intended BPF FS +* instance and cannot be intentionally or unintentionally pinned +* somewhere else. +* +* Return +* Returns zero on success. On error, -1 is returned and *errno* +* is set appropriately. +* * NOTES * eBPF objects (maps and programs) can be shared between processes. *
@@ -900,6 +918,7 @@ enum bpf_cmd {BPF_ITER_CREATE ,BPF_LINK_DETACH ,BPF_PROG_BIND_MAP , + BPF_TOKEN_CREATE, }; enum bpf_map_type {
@@ -1621,6 +1640,25 @@ union bpf_attr {__u32 flags;/* extra flags */ } prog_bind_map; +struct {/* struct used by BPF_TOKEN_CREATE command */ +/* optional, BPF token FD granting operation */ +__u32 token_fd; +__u32 token_flags; +__u32 pin_flags; +/* pin_{path_fd,pathname} specify location in BPF FS instance +* to pin BPF token at; +* path_fd + pathname have the same semantics as openat() syscall +*/ +__u32 pin_path_fd; +__u64 pin_pathname; +/* a bit set of allowed bpf() syscall commands, +* e.g., (1ULL << BPF_TOKEN_CREATE) | (1ULL << BPF_PROG_LOAD) +* will allow creating derived BPF tokens and loading new BPF +* programs +*/ +__u64 allowed_cmds; + } token_create; + } __attribute__((aligned(8)));/* The description below is an attempt at providing documentation to eBPF
Signature · 1 line
2.34.1
Andrii Nakryiko
maintainerPATCH v3 2/14 libbpf: add bpf_token_create() API
In reply to Andrii NakryikoAdd low-level wrapper API for BPF_TOKEN_CREATE command in bpf() syscall.
--- tools/lib/bpf/bpf.c | 21 +++++++++++++++++++++ tools/lib/bpf/bpf.h | 32 ++++++++++++++++++++++++++++++++ tools/lib/bpf/libbpf.map | 1 + 3 files changed, 54 insertions(+)
tools/lib/bpf/bpf.c +21 -0
@@ -1201,3 +1201,24 @@ int bpf_prog_bind_map(int prog_fd, int map_fd, ret =sys_bpf (BPF_PROG_BIND_MAP , &attr, attr_sz);return libbpf_err_errno (ret); } + +int bpf_token_create (int pin_path_fd,const char *pin_pathname,struct bpf_token_create_opts *opts) +{ + const size_t attr_sz = offsetofend(union bpf_attr, token_create); +union bpf_attr attr; +int ret; + +if (!OPTS_VALID (opts, bpf_token_create_opts)) +return libbpf_err (-EINVAL ); + +memset (&attr,0 , attr_sz); + attr.token_create.pin_path_fd = pin_path_fd; + attr.token_create.pin_pathname =ptr_to_u64 (pin_pathname); + attr.token_create.token_fd =OPTS_GET (opts, token_fd,0 ); + attr.token_create.token_flags =OPTS_GET (opts, token_flags,0 ); + attr.token_create.pin_flags =OPTS_GET (opts, pin_flags,0 ); + attr.token_create.allowed_cmds =OPTS_GET (opts, allowed_cmds,0 ); + + ret =sys_bpf (BPF_TOKEN_CREATE , &attr, attr_sz); +return libbpf_err_errno (ret); +}
tools/lib/bpf/bpf.h +32 -0
@@ -551,6 +551,38 @@ struct bpf_test_run_opts { LIBBPF_API int bpf_prog_test_run_opts(int prog_fd,struct bpf_test_run_opts *opts); +struct bpf_token_create_opts { +size_t sz;/* size of this struct for forward/backward compatibility */ +__u32 token_fd; +__u32 token_flags; +__u32 pin_flags; +__u64 allowed_cmds; + size_t :0; +}; +#define bpf_token_create_opts__last_field allowed_cmds + +/** +* @brief **bpf_token_create()** creates a new instance of BPF token, pinning +* it at the specified location in BPF FS. +* +* BPF token created and pinned with this API can be subsequently opened using +* bpf_obj_get() API to obtain FD that can be passed to bpf() syscall for +* commands like BPF_PROG_LOAD, BPF_MAP_CREATE, etc. +* +* @param pin_path_fd O_PATH FD (see man 2 openat() for semantics) specifying, +* in combination with *pin_pathname*, target location in BPF FS at which to +* create and pin BPF token. +* @param pin_pathname absolute or relative path specifying, in combination +* with *pin_path_fd*, specifying in combination with *pin_path_fd*, target +* location in BPF FS at which to create and pin BPF token. +* @param opts optional BPF token creation options, can be NULL +* +* @return 0, on success; negative error code, otherwise (errno is also set to +* the error code) +*/ +LIBBPF_API int bpf_token_create(int pin_path_fd, const char *pin_pathname, +struct bpf_token_create_opts *opts); + #ifdef __cplusplus }/* extern "C" */ #endif
tools/lib/bpf/libbpf.map +1 -0
@@ -395,4 +395,5 @@ LIBBPF_1.2.0 { LIBBPF_1.3.0 { global: bpf_obj_pin_opts; + bpf_token_create; } LIBBPF_1.2.0;
Signature · 1 line
2.34.1
Andrii Nakryiko
maintainerPATCH v3 3/14 selftests/bpf: add BPF_TOKEN_CREATE test
In reply to Andrii NakryikoAdd a subtest validating BPF_TOKEN_CREATE command, pinning/getting BPF token in/from BPF FS, and creating derived BPF tokens using token_fd parameter.
--- .../testing/selftests/bpf/prog_tests/token.c | 96 +++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/token.c
tools/testing/selftests/bpf/prog_tests/token.c new file +96 -0
@@ -0,0 +1,96 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2023 Meta Platforms, Inc. and affiliates. */ +#include "linux/bpf.h" +#include <test_progs.h> +#include <bpf/btf.h> +#include "cap_helpers.h" + +static int drop_priv_caps (__u64 *old_caps) +{ +return cap_disable_effective ((1ULL <<CAP_BPF ) | + (1ULL <<CAP_PERFMON ) | + (1ULL <<CAP_NET_ADMIN ) | + (1ULL <<CAP_SYS_ADMIN ), old_caps); +} + +static int restore_priv_caps (__u64 old_caps) +{ +return cap_enable_effective (old_caps,NULL ); +} + +#define BPFFS_PATH "/sys/fs/bpf" +#define TOKEN_PATH BPFFS_PATH "/test_token" + +static void subtest_token_create (void ) +{ +LIBBPF_OPTS (bpf_token_create_opts, opts); +int token_fd =0 , limited_token_fd =0 , err; +__u64 old_caps =0 ; + +/* check that any current and future cmd can be specified */ + opts.allowed_cmds = ~0ULL ; + err =bpf_token_create (-EBADF ,TOKEN_PATH , &opts); +if (!ASSERT_OK (err,"token_create_future_proof" )) +return ; +unlink (TOKEN_PATH ); + +/* create BPF token which allows creating derived BPF tokens */ + opts.allowed_cmds =1ULL <<BPF_TOKEN_CREATE ; + err =bpf_token_create (-EBADF ,TOKEN_PATH , &opts); +if (!ASSERT_OK (err,"token_create" )) +return ; + + token_fd =bpf_obj_get (TOKEN_PATH ); +if (!ASSERT_GT (token_fd,0 ,"token_get" )) + goto cleanup; +unlink (TOKEN_PATH ); + +/* validate pinning and getting works as expected */ + err =bpf_obj_pin (token_fd,TOKEN_PATH ); +if (!ASSERT_ERR (err,"token_pin_unexpected_success" )) + goto cleanup; + + +/* drop privileges to test token_fd passing */ +if (!ASSERT_OK (drop_priv_caps (&old_caps),"drop_caps" )) + goto cleanup; + +/* unprivileged BPF_TOKEN_CREATE should fail */ + err =bpf_token_create (-EBADF ,TOKEN_PATH ,NULL ); +if (!ASSERT_ERR (err,"token_create_unpriv_fail" )) + goto cleanup; + +/* unprivileged BPF_TOKEN_CREATE using granted BPF token succeeds */ + opts.allowed_cmds =0 ;/* ask for BPF token which doesn't allow new tokens */ + opts.token_fd = token_fd; + err =bpf_token_create (-EBADF ,TOKEN_PATH , &opts); +if (!ASSERT_OK (limited_token_fd,"token_create_limited" )) + goto cleanup; + + limited_token_fd =bpf_obj_get (TOKEN_PATH ); +if (!ASSERT_GT (limited_token_fd,0 ,"token_get_limited" )) + goto cleanup; +unlink (TOKEN_PATH ); + +/* creating yet another token using "limited" BPF token should fail */ + opts.allowed_cmds =0 ; + opts.token_fd = limited_token_fd; + err =bpf_token_create (-EBADF ,TOKEN_PATH , &opts); +if (!ASSERT_ERR (err,"token_create_from_lim_fail" )) + goto cleanup; + +cleanup: +if (token_fd) +close (token_fd); +if (limited_token_fd) +close (limited_token_fd); +unlink (TOKEN_PATH ); +if (old_caps) +ASSERT_OK (restore_priv_caps (old_caps),"restore_caps" ); +} + +void test_token (void ) +{ +if (test__start_subtest ("token_create" )) +subtest_token_create (); +}
Signature · 1 line
2.34.1
Andrii Nakryiko
maintainerPATCH v3 4/14 bpf: add BPF token support to BPF_MAP_CREATE command
In reply to Andrii NakryikoAllow providing token_fd for BPF_MAP_CREATE command to allow controlled BPF map creation from unprivileged process through delegated BPF token.
Further, add a filter of allowed BPF map types to BPF token, specified at BPF token creation time. This, in combination with allowed_cmds allows to create a narrowly-focused BPF token (controlled by privileged agent) with a restrictive set of BPF maps that application can attempt to create.
--- include/linux/bpf.h | 3 + include/uapi/linux/bpf.h | 6 ++ kernel/bpf/syscall.c | 56 +++++++++++++++---- kernel/bpf/token.c | 13 +++++ tools/include/uapi/linux/bpf.h | 6 ++ .../selftests/bpf/prog_tests/libbpf_probes.c | 2 + .../selftests/bpf/prog_tests/libbpf_str.c | 3 + 7 files changed, 77 insertions(+), 12 deletions(-)
include/linux/bpf.h +3 -0
@@ -251,6 +251,7 @@ struct bpf_map {u32 btf_value_type_id;u32 btf_vmlinux_value_type_id;struct btf *btf; +struct bpf_token *token;#ifdef CONFIG_MEMCG_KMEM struct obj_cgroup *objcg;#endif
@@ -1538,6 +1539,7 @@ struct bpf_token {struct work_struct work;atomic64_t refcnt;u64 allowed_cmds; +u64 allowed_map_types; };struct bpf_struct_ops_value ;
@@ -2096,6 +2098,7 @@ int bpf_token_new_fd(struct bpf_token *token);struct bpf_token *bpf_token_get_from_fd (u32 ufd);bool bpf_token_allow_cmd (const struct bpf_token *token,enum bpf_cmd cmd); +bool bpf_token_allow_map_type (const struct bpf_token *token,enum bpf_map_type type);enum bpf_type {BPF_TYPE_UNSPEC =0 ,
include/uapi/linux/bpf.h +6 -0
@@ -962,6 +962,7 @@ enum bpf_map_type {BPF_MAP_TYPE_BLOOM_FILTER ,BPF_MAP_TYPE_USER_RINGBUF ,BPF_MAP_TYPE_CGRP_STORAGE , + __MAX_BPF_MAP_TYPE };/* Note that tracing related programs such as
@@ -1367,6 +1368,7 @@ union bpf_attr {* to using 5 hash functions). */ __u64 map_extra; +__u32 map_token_fd; };struct {/* anonymous struct used by BPF_MAP_*_ELEM commands */
@@ -1657,6 +1659,10 @@ union bpf_attr {* programs */ __u64 allowed_cmds; +/* similarly to allowed_cmds, a bit set of BPF map types that +* are allowed to be created by requested BPF token; +*/ +__u64 allowed_map_types; } token_create; } __attribute__((aligned(8)));
kernel/bpf/syscall.c +44 -12
131 lines not on this page · raw message
kernel/bpf/token.c +13 -0
@@ -110,6 +110,10 @@ int bpf_token_create(union bpf_attr *attr)/* requested cmds should be a subset of associated token's set */ if (token && !is_bit_subset_of (attr->token_create.allowed_cmds, token->allowed_cmds)) goto out; +/* requested map types should be a subset of associated token's set */ +if (token && !is_bit_subset_of (attr->token_create.allowed_map_types, + token->allowed_map_types)) + goto out; new_token =bpf_token_alloc ();if (!new_token) {
@@ -118,6 +122,7 @@ int bpf_token_create(union bpf_attr *attr) } new_token->allowed_cmds = attr->token_create.allowed_cmds; + new_token->allowed_map_types = attr->token_create.allowed_map_types; ret = bpf_obj_pin_any(attr->token_create.pin_path_fd, u64_to_user_ptr(attr->token_create.pin_pathname),
@@ -165,3 +170,11 @@ bool bpf_token_allow_cmd(const struct bpf_token *token, enum bpf_cmd cmd)return token->allowed_cmds & (1ULL << cmd); } + +bool bpf_token_allow_map_type (const struct bpf_token *token,enum bpf_map_type type) +{ +if (!token || type >= __MAX_BPF_MAP_TYPE) +return false; + +return token->allowed_map_types & (1ULL << type); +}
tools/include/uapi/linux/bpf.h +6 -0
@@ -962,6 +962,7 @@ enum bpf_map_type {BPF_MAP_TYPE_BLOOM_FILTER ,BPF_MAP_TYPE_USER_RINGBUF ,BPF_MAP_TYPE_CGRP_STORAGE , + __MAX_BPF_MAP_TYPE };/* Note that tracing related programs such as
@@ -1367,6 +1368,7 @@ union bpf_attr {* to using 5 hash functions). */ __u64 map_extra; +__u32 map_token_fd; };struct {/* anonymous struct used by BPF_MAP_*_ELEM commands */
@@ -1657,6 +1659,10 @@ union bpf_attr {* programs */ __u64 allowed_cmds; +/* similarly to allowed_cmds, a bit set of BPF map types that +* are allowed to be created by requested BPF token; +*/ +__u64 allowed_map_types; } token_create; } __attribute__((aligned(8)));
tools/testing/selftests/bpf/prog_tests/libbpf_probes.c +2 -0
@@ -68,6 +68,8 @@ void test_libbpf_probe_map_types(void)if (map_type ==BPF_MAP_TYPE_UNSPEC )continue ; +if (strcmp (map_type_name,"__MAX_BPF_MAP_TYPE" ) ==0 ) +continue ;if (!test__start_subtest (map_type_name))continue ;
tools/testing/selftests/bpf/prog_tests/libbpf_str.c +3 -0
@@ -132,6 +132,9 @@ static void test_libbpf_bpf_map_type_str(void)const char *map_type_str;char buf[256 ]; +if (map_type == __MAX_BPF_MAP_TYPE) +continue ; + map_type_name =btf__str_by_offset (btf, e->name_off); map_type_str =libbpf_bpf_map_type_str (map_type);ASSERT_OK_PTR (map_type_str, map_type_name);
Signature · 1 line
2.34.1
Andrii Nakryiko
maintainerPATCH v3 5/14 libbpf: add BPF token support to bpf_map_create() API
In reply to Andrii NakryikoAdd ability to provide token_fd for BPF_MAP_CREATE command through bpf_map_create() API.
Also wire through token_create.allowed_map_types param for BPF_TOKEN_CREATE command.
--- tools/lib/bpf/bpf.c | 5 ++++- tools/lib/bpf/bpf.h | 7 +++++-- 2 files changed, 9 insertions(+), 3 deletions(-)
tools/lib/bpf/bpf.c +4 -1
@@ -169,7 +169,7 @@ int bpf_map_create(enum bpf_map_type map_type,__u32 max_entries, const struct bpf_map_create_opts *opts) { - const size_t attr_sz = offsetofend(union bpf_attr, map_extra); + const size_t attr_sz = offsetofend(union bpf_attr, map_token_fd);union bpf_attr attr;int fd;
@@ -198,6 +198,8 @@ int bpf_map_create(enum bpf_map_type map_type, attr.numa_node =OPTS_GET (opts, numa_node,0 ); attr.map_ifindex =OPTS_GET (opts, map_ifindex,0 ); + attr.map_token_fd =OPTS_GET (opts, token_fd,0 ); + fd =sys_bpf_fd (BPF_MAP_CREATE , &attr, attr_sz);return libbpf_err_errno (fd); }
@@ -1218,6 +1220,7 @@ int bpf_token_create(int pin_path_fd, const char *pin_pathname, struct bpf_token attr.token_create.token_flags =OPTS_GET (opts, token_flags,0 ); attr.token_create.pin_flags =OPTS_GET (opts, pin_flags,0 ); attr.token_create.allowed_cmds =OPTS_GET (opts, allowed_cmds,0 ); + attr.token_create.allowed_map_types =OPTS_GET (opts, allowed_map_types,0 ); ret =sys_bpf (BPF_TOKEN_CREATE , &attr, attr_sz);return libbpf_err_errno (ret);
tools/lib/bpf/bpf.h +5 -2
@@ -51,8 +51,10 @@ struct bpf_map_create_opts {__u32 numa_node;__u32 map_ifindex; + +__u32 token_fd; }; -#define bpf_map_create_opts__last_field map_ifindex +#define bpf_map_create_opts__last_field token_fd LIBBPF_API int bpf_map_create(enum bpf_map_type map_type, const char *map_name,
@@ -557,9 +559,10 @@ struct bpf_token_create_opts {__u32 token_flags;__u32 pin_flags;__u64 allowed_cmds; +__u64 allowed_map_types; size_t :0; }; -#define bpf_token_create_opts__last_field allowed_cmds +#define bpf_token_create_opts__last_field allowed_map_types/** * @brief **bpf_token_create()** creates a new instance of BPF token, pinning
Signature · 1 line
2.34.1
Andrii Nakryiko
maintainerPATCH v3 6/14 selftests/bpf: add BPF token-enabled test for BPF_MAP_CREATE command
In reply to Andrii NakryikoAdd test for creating BPF token with support for BPF_MAP_CREATE delegation. And validate that its allowed_map_types filter works as expected and allows to create privileged BPF maps through delegated token, as long as they are allowed by privileged creator of a token.
--- .../testing/selftests/bpf/prog_tests/token.c | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+)
tools/testing/selftests/bpf/prog_tests/token.c +55 -0
@@ -89,8 +89,63 @@ static void subtest_token_create(void)ASSERT_OK (restore_priv_caps (old_caps),"restore_caps" ); } +static void subtest_map_token (void ) +{ +LIBBPF_OPTS (bpf_token_create_opts, token_opts); +LIBBPF_OPTS (bpf_map_create_opts, map_opts); +int token_fd =0 , map_fd =0 , err; +__u64 old_caps =0 ; + +/* check that it's ok to allow any map type */ + token_opts.allowed_map_types = ~0ULL ;/* any current and future map types is allowed */ + err =bpf_token_create (-EBADF ,TOKEN_PATH , &token_opts); +if (!ASSERT_OK (err,"token_create_future_proof" )) +return ; +unlink (TOKEN_PATH ); + +/* create BPF token allowing STACK, but not QUEUE map */ + token_opts.allowed_cmds =1ULL <<BPF_MAP_CREATE ; + token_opts.allowed_map_types =1ULL <<BPF_MAP_TYPE_STACK ;/* but not QUEUE */ + err =bpf_token_create (-EBADF ,TOKEN_PATH , &token_opts); +if (!ASSERT_OK (err,"token_create" )) +return ; + +/* drop privileges to test token_fd passing */ +if (!ASSERT_OK (drop_priv_caps (&old_caps),"drop_caps" )) + goto cleanup; + + token_fd =bpf_obj_get (TOKEN_PATH ); +if (!ASSERT_GT (token_fd,0 ,"token_get" )) + goto cleanup; + +/* BPF_MAP_TYPE_STACK is privileged, but with given token_fd should succeed */ + map_opts.token_fd = token_fd; + map_fd =bpf_map_create (BPF_MAP_TYPE_STACK ,"token_stack" ,0 ,8 ,1 , &map_opts); +if (!ASSERT_GT (map_fd,0 ,"stack_map_fd" )) + goto cleanup; +close (map_fd); + map_fd =0 ; + +/* BPF_MAP_TYPE_QUEUE is privileged, and token doesn't allow it, so should fail */ + map_opts.token_fd = token_fd; + map_fd =bpf_map_create (BPF_MAP_TYPE_QUEUE ,"token_queue" ,0 ,8 ,1 , &map_opts); +if (!ASSERT_EQ (map_fd, -EPERM ,"queue_map_fd" )) + goto cleanup; + +cleanup: +if (map_fd >0 ) +close (map_fd); +if (token_fd) +close (token_fd); +unlink (TOKEN_PATH ); +if (old_caps) +ASSERT_OK (restore_priv_caps (old_caps),"restore_caps" ); +} +void test_token (void ) {if (test__start_subtest ("token_create" ))subtest_token_create (); +if (test__start_subtest ("map_token" )) +subtest_map_token (); }
Signature · 1 line
2.34.1
Andrii Nakryiko
maintainerPATCH v3 7/14 bpf: add BPF token support to BPF_BTF_LOAD command
In reply to Andrii NakryikoAccept BPF token FD in BPF_BTF_LOAD command to allow BTF data loading through delegated BPF token. BTF loading is a pretty straightforward operation, so as long as BPF token is created with allow_cmds granting BPF_BTF_LOAD command, kernel proceeds to parsing BTF data and creating BTF object.
--- include/uapi/linux/bpf.h | 1 + kernel/bpf/syscall.c | 20 ++++++++++++++++++-- tools/include/uapi/linux/bpf.h | 1 + 3 files changed, 20 insertions(+), 2 deletions(-)
include/uapi/linux/bpf.h +1 -0
@@ -1535,6 +1535,7 @@ union bpf_attr {* truncated), or smaller (if log buffer wasn't filled completely). */ __u32 btf_log_true_size; +__u32 btf_token_fd; };struct {
kernel/bpf/syscall.c +18 -2
@@ -4475,15 +4475,31 @@ static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,return err; } -#define BPF_BTF_LOAD_LAST_FIELD btf_log_true_size +#define BPF_BTF_LOAD_LAST_FIELD btf_token_fdstatic int bpf_btf_load (const union bpf_attr *attr,bpfptr_t uattr,__u32 uattr_size) { +struct bpf_token *token =NULL ; +if (CHECK_ATTR (BPF_BTF_LOAD ))return -EINVAL ; -if (!bpf_capable ()) +if (attr->btf_token_fd) { + token =bpf_token_get_from_fd (attr->btf_token_fd); +if (IS_ERR (token)) +return PTR_ERR (token); +if (!bpf_token_allow_cmd (token,BPF_BTF_LOAD )) { +bpf_token_put (token); + token =NULL ; + } + } + +if (!bpf_token_capable (token,CAP_BPF )) { +bpf_token_put (token);return -EPERM ; + } + +bpf_token_put (token);return btf_new_fd (attr, uattr, uattr_size); }
tools/include/uapi/linux/bpf.h +1 -0
@@ -1535,6 +1535,7 @@ union bpf_attr {* truncated), or smaller (if log buffer wasn't filled completely). */ __u32 btf_log_true_size; +__u32 btf_token_fd; };struct {
Signature · 1 line
2.34.1
Andrii Nakryiko
maintainerPATCH v3 8/14 libbpf: add BPF token support to bpf_btf_load() API
In reply to Andrii NakryikoAllow user to specify token_fd for bpf_btf_load() API that wraps kernel's BPF_BTF_LOAD command. This allows loading BTF from unprivileged process as long as it has BPF token allowing BPF_BTF_LOAD command, which can be created and delegated by privileged process.
--- tools/lib/bpf/bpf.c | 4 +++- tools/lib/bpf/bpf.h | 3 ++- 2 files changed, 5 insertions(+), 2 deletions(-)
tools/lib/bpf/bpf.c +3 -1
@@ -1098,7 +1098,7 @@ int bpf_raw_tracepoint_open(const char *name, int prog_fd)int bpf_btf_load (const void *btf_data,size_t btf_size,struct bpf_btf_load_opts *opts) { - const size_t attr_sz = offsetofend(union bpf_attr, btf_log_true_size); + const size_t attr_sz = offsetofend(union bpf_attr, btf_token_fd);union bpf_attr attr;char *log_buf;size_t log_size;
@@ -1123,6 +1123,8 @@ int bpf_btf_load(const void *btf_data, size_t btf_size, struct bpf_btf_load_opts attr.btf =ptr_to_u64 (btf_data); attr.btf_size = btf_size; + attr.btf_token_fd =OPTS_GET (opts, token_fd,0 ); +/* log_level == 0 and log_buf != NULL means "try loading without * log_buf, but retry with log_buf and log_level=1 on error", which is * consistent across low-level and high-level BTF and program loading
tools/lib/bpf/bpf.h +2 -1
@@ -132,9 +132,10 @@ struct bpf_btf_load_opts {* If kernel doesn't support this feature, log_size is left unchanged. */ __u32 log_true_size; +__u32 token_fd; size_t :0; }; -#define bpf_btf_load_opts__last_field log_true_size +#define bpf_btf_load_opts__last_field token_fd LIBBPF_API int bpf_btf_load(const void *btf_data, size_t btf_size,struct bpf_btf_load_opts *opts);
Signature · 1 line
2.34.1
Andrii Nakryiko
maintainerPATCH v3 9/14 selftests/bpf: add BPF token-enabled BPF_BTF_LOAD selftest
In reply to Andrii NakryikoAdd a simple test validating that BTF loading can be done from unprivileged process through delegated BPF token.
--- .../testing/selftests/bpf/prog_tests/token.c | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+)
tools/testing/selftests/bpf/prog_tests/token.c +60 -0
@@ -142,10 +142,70 @@ static void subtest_map_token(void)ASSERT_OK (restore_priv_caps (old_caps),"restore_caps" ); } +static void subtest_btf_token (void ) +{ +LIBBPF_OPTS (bpf_token_create_opts, token_opts); +LIBBPF_OPTS (bpf_btf_load_opts, btf_opts); +int token_fd =0 , btf_fd =0 , err; +const void *raw_btf_data; +struct btf *btf =NULL ; +__u32 raw_btf_size; +__u64 old_caps =0 ; + +/* create BPF token allowing BPF_BTF_LOAD command */ + token_opts.allowed_cmds =1ULL <<BPF_BTF_LOAD ; + err =bpf_token_create (-EBADF ,TOKEN_PATH , &token_opts); +if (!ASSERT_OK (err,"token_create" )) +return ; + +/* drop privileges to test token_fd passing */ +if (!ASSERT_OK (drop_priv_caps (&old_caps),"drop_caps" )) + goto cleanup; + + token_fd =bpf_obj_get (TOKEN_PATH ); +if (!ASSERT_GT (token_fd,0 ,"token_get" )) + goto cleanup; + + btf =btf__new_empty (); +if (!ASSERT_OK_PTR (btf,"empty_btf" )) + goto cleanup; + +ASSERT_GT (btf__add_int (btf,"int" ,4 ,0 ),0 ,"int_type" ); + + raw_btf_data =btf__raw_data (btf, &raw_btf_size); +if (!ASSERT_OK_PTR (raw_btf_data,"raw_btf_data" )) + goto cleanup; + +/* validate we can successfully load new BTF with token */ + btf_opts.token_fd = token_fd; + btf_fd =bpf_btf_load (raw_btf_data, raw_btf_size, &btf_opts); +if (!ASSERT_GT (btf_fd,0 ,"btf_fd" )) + goto cleanup; +close (btf_fd); + +/* now validate that we *cannot* load BTF without token */ + btf_opts.token_fd =0 ; + btf_fd =bpf_btf_load (raw_btf_data, raw_btf_size, &btf_opts); +if (!ASSERT_EQ (btf_fd, -EPERM ,"btf_fd_eperm" )) + goto cleanup; + +cleanup: +btf__free (btf); +if (btf_fd >0 ) +close (btf_fd); +if (token_fd) +close (token_fd); +unlink (TOKEN_PATH ); +if (old_caps) +ASSERT_OK (restore_priv_caps (old_caps),"restore_caps" ); +} +void test_token (void ) {if (test__start_subtest ("token_create" ))subtest_token_create ();if (test__start_subtest ("map_token" ))subtest_map_token (); +if (test__start_subtest ("btf_token" )) +subtest_btf_token (); }
Signature · 1 line
2.34.1
Andrii Nakryiko
maintainerPATCH v3 10/14 bpf: add BPF token support to BPF_PROG_LOAD command
In reply to Andrii NakryikoAdd basic support of BPF token to BPF_PROG_LOAD. Extend BPF token to allow specifying BPF_PROG_LOAD as an allowed command, and also allow to specify bit sets of program type and attach type combination that would be allowed to be loaded by requested BPF token.
--- include/linux/bpf.h | 6 ++ include/uapi/linux/bpf.h | 8 ++ kernel/bpf/core.c | 1 + kernel/bpf/syscall.c | 89 +++++++++++++------ kernel/bpf/token.c | 21 +++++ tools/include/uapi/linux/bpf.h | 8 ++ .../selftests/bpf/prog_tests/libbpf_probes.c | 2 + .../selftests/bpf/prog_tests/libbpf_str.c | 3 + 8 files changed, 113 insertions(+), 25 deletions(-)
include/linux/bpf.h +6 -0
@@ -1411,6 +1411,7 @@ struct bpf_prog_aux {#ifdef CONFIG_SECURITY void *security;#endif +struct bpf_token *token;struct bpf_prog_offload *offload;struct btf *btf;struct bpf_func_info *func_info;
@@ -1540,6 +1541,8 @@ struct bpf_token {atomic64_t refcnt;u64 allowed_cmds;u64 allowed_map_types; +u64 allowed_prog_types; +u64 allowed_attach_types; };struct bpf_struct_ops_value ;
@@ -2099,6 +2102,9 @@ struct bpf_token *bpf_token_get_from_fd(u32 ufd);bool bpf_token_allow_cmd (const struct bpf_token *token,enum bpf_cmd cmd);bool bpf_token_allow_map_type (const struct bpf_token *token,enum bpf_map_type type); +bool bpf_token_allow_prog_type (const struct bpf_token *token, +enum bpf_prog_type prog_type, +enum bpf_attach_type attach_type);enum bpf_type {BPF_TYPE_UNSPEC =0 ,
include/uapi/linux/bpf.h +8 -0
@@ -1007,6 +1007,7 @@ enum bpf_prog_type {BPF_PROG_TYPE_SK_LOOKUP ,BPF_PROG_TYPE_SYSCALL ,/* a program that can execute syscalls */ BPF_PROG_TYPE_NETFILTER , + __MAX_BPF_PROG_TYPE }; enum bpf_attach_type {
@@ -1438,6 +1439,7 @@ union bpf_attr {* truncated), or smaller (if log buffer wasn't filled completely). */ __u32 log_true_size; +__u32 prog_token_fd; };struct {/* anonymous struct used by BPF_OBJ_* commands */
@@ -1664,6 +1666,12 @@ union bpf_attr {* are allowed to be created by requested BPF token; */ __u64 allowed_map_types; +/* similarly to allowed_map_types, bit sets of BPF program +* types and BPF program attach types that are allowed to be +* loaded by requested BPF token +*/ +__u64 allowed_prog_types; +__u64 allowed_attach_types; } token_create; } __attribute__((aligned(8)));
kernel/bpf/core.c +1 -0
@@ -2599,6 +2599,7 @@ void bpf_prog_free(struct bpf_prog *fp)if (aux->dst_prog)bpf_prog_put (aux->dst_prog); +bpf_token_put (aux->token);INIT_WORK (&aux->work, bpf_prog_free_deferred);schedule_work (&aux->work); }
kernel/bpf/syscall.c +64 -25
175 lines not on this page · raw message
kernel/bpf/token.c +21 -0
@@ -114,6 +114,14 @@ int bpf_token_create(union bpf_attr *attr)if (token && !is_bit_subset_of (attr->token_create.allowed_map_types, token->allowed_map_types)) goto out; +/* requested prog types should be a subset of associated token's set */ +if (token && !is_bit_subset_of (attr->token_create.allowed_prog_types, + token->allowed_prog_types)) + goto out; +/* requested attach types should be a subset of associated token's set */ +if (token && !is_bit_subset_of (attr->token_create.allowed_attach_types, + token->allowed_attach_types)) + goto out; new_token =bpf_token_alloc ();if (!new_token) {
@@ -123,6 +131,8 @@ int bpf_token_create(union bpf_attr *attr) new_token->allowed_cmds = attr->token_create.allowed_cmds; new_token->allowed_map_types = attr->token_create.allowed_map_types; + new_token->allowed_prog_types = attr->token_create.allowed_prog_types; + new_token->allowed_attach_types = attr->token_create.allowed_attach_types; ret = bpf_obj_pin_any(attr->token_create.pin_path_fd, u64_to_user_ptr(attr->token_create.pin_pathname),
@@ -178,3 +188,14 @@ bool bpf_token_allow_map_type(const struct bpf_token *token, enum bpf_map_type treturn token->allowed_map_types & (1ULL << type); } + +bool bpf_token_allow_prog_type (const struct bpf_token *token, +enum bpf_prog_type prog_type, +enum bpf_attach_type attach_type) +{ +if (!token || prog_type >= __MAX_BPF_PROG_TYPE || attach_type >= __MAX_BPF_ATTACH_TYPE) +return false; + +return (token->allowed_prog_types & (1ULL << prog_type)) && + (token->allowed_attach_types & (1ULL << attach_type)); +}
tools/include/uapi/linux/bpf.h +8 -0
@@ -1007,6 +1007,7 @@ enum bpf_prog_type {BPF_PROG_TYPE_SK_LOOKUP ,BPF_PROG_TYPE_SYSCALL ,/* a program that can execute syscalls */ BPF_PROG_TYPE_NETFILTER , + __MAX_BPF_PROG_TYPE }; enum bpf_attach_type {
@@ -1438,6 +1439,7 @@ union bpf_attr {* truncated), or smaller (if log buffer wasn't filled completely). */ __u32 log_true_size; +__u32 prog_token_fd; };struct {/* anonymous struct used by BPF_OBJ_* commands */
@@ -1664,6 +1666,12 @@ union bpf_attr {* are allowed to be created by requested BPF token; */ __u64 allowed_map_types; +/* similarly to allowed_map_types, bit sets of BPF program +* types and BPF program attach types that are allowed to be +* loaded by requested BPF token +*/ +__u64 allowed_prog_types; +__u64 allowed_attach_types; } token_create; } __attribute__((aligned(8)));
tools/testing/selftests/bpf/prog_tests/libbpf_probes.c +2 -0
@@ -30,6 +30,8 @@ void test_libbpf_probe_prog_types(void)if (prog_type ==BPF_PROG_TYPE_UNSPEC )continue ; +if (strcmp (prog_type_name,"__MAX_BPF_PROG_TYPE" ) ==0 ) +continue ;if (!test__start_subtest (prog_type_name))continue ;
tools/testing/selftests/bpf/prog_tests/libbpf_str.c +3 -0
@@ -185,6 +185,9 @@ static void test_libbpf_bpf_prog_type_str(void)const char *prog_type_str;char buf[256 ]; +if (prog_type == __MAX_BPF_PROG_TYPE) +continue ; + prog_type_name =btf__str_by_offset (btf, e->name_off); prog_type_str =libbpf_bpf_prog_type_str (prog_type);ASSERT_OK_PTR (prog_type_str, prog_type_name);
Signature · 1 line
2.34.1
Andrii Nakryiko
maintainerPATCH v3 11/14 bpf: take into account BPF token when fetching helper protos
In reply to Andrii NakryikoInstead of performing unconditional system-wide bpf_capable() and perfmon_capable() calls inside bpf_base_func_proto() function (and other similar ones) to determine eligibility of a given BPF helper for a given program, use previously recorded BPF token during BPF_PROG_LOAD command handling to inform the decision.
--- drivers/media/rc/bpf-lirc.c | 2 +- include/linux/bpf.h | 5 +++-- kernel/bpf/cgroup.c | 6 +++--- kernel/bpf/helpers.c | 6 +++--- kernel/bpf/syscall.c | 5 +++-- kernel/trace/bpf_trace.c | 2 +- net/core/filter.c | 32 ++++++++++++++++---------------- net/ipv4/bpf_tcp_ca.c | 2 +- net/netfilter/nf_bpf_link.c | 2 +- 9 files changed, 32 insertions(+), 30 deletions(-)
drivers/media/rc/bpf-lirc.c +1 -1
@@ -110,7 +110,7 @@ lirc_mode2_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)case BPF_FUNC_get_prandom_u32:return &bpf_get_prandom_u32_proto;case BPF_FUNC_trace_printk: -if (perfmon_capable ()) +if (bpf_token_capable (prog->aux->token,CAP_PERFMON ))return bpf_get_trace_printk_proto (); fallthrough;default :
include/linux/bpf.h +3 -2
@@ -2358,7 +2358,8 @@ int btf_check_type_match(struct bpf_verifier_log *log, const struct bpf_prog *prstruct bpf_prog *bpf_prog_by_id (u32 id);struct bpf_link *bpf_link_by_id (u32 id); -const struct bpf_func_proto *bpf_base_func_proto (enum bpf_func_id func_id); +const struct bpf_func_proto *bpf_base_func_proto (enum bpf_func_id func_id, +const struct bpf_prog *prog);void bpf_task_storage_free (struct task_struct *task);void bpf_cgrp_storage_free (struct cgroup *cgroup);bool bpf_prog_has_kfunc_call (const struct bpf_prog *prog);
@@ -2615,7 +2616,7 @@ static inline int btf_struct_access(struct bpf_verifier_log *log, }static inline const struct bpf_func_proto * -bpf_base_func_proto (enum bpf_func_id func_id) +bpf_base_func_proto (enum bpf_func_id func_id,const struct bpf_prog *prog) {return NULL ; }
kernel/bpf/cgroup.c +3 -3
@@ -1615,7 +1615,7 @@ cgroup_dev_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)case BPF_FUNC_perf_event_output:return &bpf_event_output_data_proto;default : -return bpf_base_func_proto (func_id); +return bpf_base_func_proto (func_id, prog); } }
@@ -2173,7 +2173,7 @@ sysctl_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)case BPF_FUNC_perf_event_output:return &bpf_event_output_data_proto;default : -return bpf_base_func_proto (func_id); +return bpf_base_func_proto (func_id, prog); } }
@@ -2330,7 +2330,7 @@ cg_sockopt_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)case BPF_FUNC_perf_event_output:return &bpf_event_output_data_proto;default : -return bpf_base_func_proto (func_id); +return bpf_base_func_proto (func_id, prog); } }
kernel/bpf/helpers.c +3 -3
@@ -1663,7 +1663,7 @@ const struct bpf_func_proto bpf_probe_read_kernel_str_proto __weak;const struct bpf_func_proto bpf_task_pt_regs_proto__weak ;const struct bpf_func_proto * -bpf_base_func_proto (enum bpf_func_id func_id) +bpf_base_func_proto (enum bpf_func_id func_id,const struct bpf_prog *prog) {switch (func_id) {case BPF_FUNC_map_lookup_elem:
@@ -1714,7 +1714,7 @@ bpf_base_func_proto(enum bpf_func_id func_id)break ; } -if (!bpf_capable ()) +if (!bpf_token_capable (prog->aux->token,CAP_BPF ))return NULL ;switch (func_id) {
@@ -1772,7 +1772,7 @@ bpf_base_func_proto(enum bpf_func_id func_id)break ; } -if (!perfmon_capable ()) +if (!bpf_token_capable (prog->aux->token,CAP_PERFMON ))return NULL ;switch (func_id) {
kernel/bpf/syscall.c +3 -2
@@ -5424,7 +5424,7 @@ static const struct bpf_func_proto bpf_sys_bpf_proto = {const struct bpf_func_proto *__weak tracing_prog_func_proto (enum bpf_func_id func_id,const struct bpf_prog *prog) { -return bpf_base_func_proto (func_id); +return bpf_base_func_proto (func_id, prog); }BPF_CALL_1 (bpf_sys_close, u32, fd)
@@ -5474,7 +5474,8 @@ syscall_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) {switch (func_id) {case BPF_FUNC_sys_bpf: -return !perfmon_capable () ?NULL : &bpf_sys_bpf_proto; +return !bpf_token_capable (prog->aux->token,CAP_PERFMON ) + ?NULL : &bpf_sys_bpf_proto;case BPF_FUNC_btf_find_by_name_kind:return &bpf_btf_find_by_name_kind_proto;case BPF_FUNC_sys_close:
kernel/trace/bpf_trace.c +1 -1
@@ -1511,7 +1511,7 @@ bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)case BPF_FUNC_trace_vprintk:return bpf_get_trace_vprintk_proto ();default : -return bpf_base_func_proto (func_id); +return bpf_base_func_proto (func_id, prog); } }
net/core/filter.c +16 -16
@@ -83,7 +83,7 @@#include <net/netfilter/nf_conntrack_bpf.h> static const struct bpf_func_proto * -bpf_sk_base_func_proto (enum bpf_func_id func_id); +bpf_sk_base_func_proto (enum bpf_func_id func_id,const struct bpf_prog *prog);int copy_bpf_fprog_from_user (struct sock_fprog *dst,sockptr_t src,int len) {
@@ -7739,7 +7739,7 @@ sock_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)case BPF_FUNC_ktime_get_coarse_ns:return &bpf_ktime_get_coarse_ns_proto;default : -return bpf_base_func_proto (func_id); +return bpf_base_func_proto (func_id, prog); } }
@@ -7822,7 +7822,7 @@ sock_addr_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)return NULL ; }default : -return bpf_sk_base_func_proto (func_id); +return bpf_sk_base_func_proto (func_id, prog); } }
@@ -7841,7 +7841,7 @@ sk_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)case BPF_FUNC_perf_event_output:return &bpf_skb_event_output_proto;default : -return bpf_sk_base_func_proto (func_id); +return bpf_sk_base_func_proto (func_id, prog); } }
@@ -8028,7 +8028,7 @@ tc_cls_act_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)#endif #endif default : -return bpf_sk_base_func_proto (func_id); +return bpf_sk_base_func_proto (func_id, prog); } }
@@ -8087,7 +8087,7 @@ xdp_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)#endif #endif default : -return bpf_sk_base_func_proto (func_id); +return bpf_sk_base_func_proto (func_id, prog); }#if IS_MODULE (CONFIG_NF_CONNTRACK ) &&IS_ENABLED (CONFIG_DEBUG_INFO_BTF_MODULES )
@@ -8148,7 +8148,7 @@ sock_ops_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)return &bpf_tcp_sock_proto;#endif /* CONFIG_INET */ default : -return bpf_sk_base_func_proto (func_id); +return bpf_sk_base_func_proto (func_id, prog); } }
@@ -8190,7 +8190,7 @@ sk_msg_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)return &bpf_get_cgroup_classid_curr_proto;#endif default : -return bpf_sk_base_func_proto (func_id); +return bpf_sk_base_func_proto (func_id, prog); } }
@@ -8234,7 +8234,7 @@ sk_skb_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)return &bpf_skc_lookup_tcp_proto;#endif default : -return bpf_sk_base_func_proto (func_id); +return bpf_sk_base_func_proto (func_id, prog); } }
@@ -8245,7 +8245,7 @@ flow_dissector_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)case BPF_FUNC_skb_load_bytes:return &bpf_flow_dissector_load_bytes_proto;default : -return bpf_sk_base_func_proto (func_id); +return bpf_sk_base_func_proto (func_id, prog); } }
@@ -8272,7 +8272,7 @@ lwt_out_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)case BPF_FUNC_skb_under_cgroup:return &bpf_skb_under_cgroup_proto;default : -return bpf_sk_base_func_proto (func_id); +return bpf_sk_base_func_proto (func_id, prog); } }
@@ -11103,7 +11103,7 @@ sk_reuseport_func_proto(enum bpf_func_id func_id,case BPF_FUNC_ktime_get_coarse_ns:return &bpf_ktime_get_coarse_ns_proto;default : -return bpf_base_func_proto (func_id); +return bpf_base_func_proto (func_id, prog); } }
@@ -11285,7 +11285,7 @@ sk_lookup_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)case BPF_FUNC_sk_release:return &bpf_sk_release_proto;default : -return bpf_sk_base_func_proto (func_id); +return bpf_sk_base_func_proto (func_id, prog); } }
@@ -11619,7 +11619,7 @@ const struct bpf_func_proto bpf_sock_from_file_proto = { };static const struct bpf_func_proto * -bpf_sk_base_func_proto (enum bpf_func_id func_id) +bpf_sk_base_func_proto (enum bpf_func_id func_id,const struct bpf_prog *prog) {const struct bpf_func_proto *func;
@@ -11648,10 +11648,10 @@ bpf_sk_base_func_proto(enum bpf_func_id func_id)case BPF_FUNC_ktime_get_coarse_ns:return &bpf_ktime_get_coarse_ns_proto;default : -return bpf_base_func_proto (func_id); +return bpf_base_func_proto (func_id, prog); } -if (!perfmon_capable ()) +if (!bpf_token_capable (prog->aux->token,CAP_PERFMON ))return NULL ;return func;
net/ipv4/bpf_tcp_ca.c +1 -1
@@ -193,7 +193,7 @@ bpf_tcp_ca_get_func_proto(enum bpf_func_id func_id,case BPF_FUNC_ktime_get_coarse_ns:return &bpf_ktime_get_coarse_ns_proto;default : -return bpf_base_func_proto (func_id); +return bpf_base_func_proto (func_id, prog); } }
net/netfilter/nf_bpf_link.c +1 -1
@@ -219,7 +219,7 @@ static bool nf_is_valid_access(int off, int size, enum bpf_access_type type,static const struct bpf_func_proto *bpf_nf_func_proto (enum bpf_func_id func_id,const struct bpf_prog *prog) { -return bpf_base_func_proto (func_id); +return bpf_base_func_proto (func_id, prog); }const struct bpf_verifier_ops netfilter_verifier_ops = {
Signature · 1 line
2.34.1
Andrii Nakryiko
maintainerPATCH v3 12/14 bpf: consistenly use BPF token throughout BPF verifier logic
In reply to Andrii NakryikoRemove remaining direct queries to perfmon_capable() and bpf_capable() in BPF verifier logic and instead use BPF token (if available) to make decisions about privileges.
--- include/linux/bpf.h | 18 ++++++++++-------- include/linux/filter.h | 2 +- kernel/bpf/arraymap.c | 2 +- kernel/bpf/core.c | 2 +- kernel/bpf/verifier.c | 13 ++++++------- net/core/filter.c | 4 ++-- 6 files changed, 21 insertions(+), 20 deletions(-)
include/linux/bpf.h +10 -8
@@ -2059,24 +2059,26 @@ bpf_map_alloc_percpu(const struct bpf_map *map, size_t size, size_t align,extern int sysctl_unprivileged_bpf_disabled; -static inline bool bpf_allow_ptr_leaks (void ) +bool bpf_token_capable (const struct bpf_token *token,int cap); + +static inline bool bpf_allow_ptr_leaks (const struct bpf_token *token) { -return perfmon_capable (); +return bpf_token_capable (token,CAP_PERFMON ); } -static inline bool bpf_allow_uninit_stack (void ) +static inline bool bpf_allow_uninit_stack (const struct bpf_token *token) { -return perfmon_capable (); +return bpf_token_capable (token,CAP_PERFMON ); } -static inline bool bpf_bypass_spec_v1 (void ) +static inline bool bpf_bypass_spec_v1 (const struct bpf_token *token) { -return perfmon_capable (); +return bpf_token_capable (token,CAP_PERFMON ); } -static inline bool bpf_bypass_spec_v4 (void ) +static inline bool bpf_bypass_spec_v4 (const struct bpf_token *token) { -return perfmon_capable (); +return bpf_token_capable (token,CAP_PERFMON ); }int bpf_map_new_fd (struct bpf_map *map,int flags);
include/linux/filter.h +1 -1
@@ -1109,7 +1109,7 @@ static inline bool bpf_jit_blinding_enabled(struct bpf_prog *prog)return false;if (!bpf_jit_harden)return false; -if (bpf_jit_harden ==1 &&bpf_capable ()) +if (bpf_jit_harden ==1 &&bpf_token_capable (prog->aux->token,CAP_BPF ))return false;return true;
kernel/bpf/arraymap.c +1 -1
@@ -82,7 +82,7 @@ static struct bpf_map *array_map_alloc(union bpf_attr *attr)bool percpu = attr->map_type ==BPF_MAP_TYPE_PERCPU_ARRAY ;int numa_node =bpf_map_attr_numa_node (attr);u32 elem_size, index_mask, max_entries; -bool bypass_spec_v1 =bpf_bypass_spec_v1 (); +bool bypass_spec_v1 =bpf_bypass_spec_v1 (NULL );u64 array_size, mask64;struct bpf_array *array;
kernel/bpf/core.c +1 -1
@@ -661,7 +661,7 @@ static bool bpf_prog_kallsyms_candidate(const struct bpf_prog *fp)void bpf_prog_kallsyms_add (struct bpf_prog *fp) {if (!bpf_prog_kallsyms_candidate (fp) || - !bpf_capable ()) + !bpf_token_capable (fp->aux->token,CAP_BPF ))return ;bpf_prog_ksym_set_addr (fp);
kernel/bpf/verifier.c +6 -7
@@ -19397,7 +19397,12 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr, __u3 env->prog = *prog; env->ops = bpf_verifier_ops[env->prog->type]; env->fd_array =make_bpfptr (attr->fd_array, uattr.is_kernel); - is_priv =bpf_capable (); + + env->allow_ptr_leaks =bpf_allow_ptr_leaks (env->prog->aux->token); + env->allow_uninit_stack =bpf_allow_uninit_stack (env->prog->aux->token); + env->bypass_spec_v1 =bpf_bypass_spec_v1 (env->prog->aux->token); + env->bypass_spec_v4 =bpf_bypass_spec_v4 (env->prog->aux->token); + env->bpf_capable = is_priv =bpf_token_capable (env->prog->aux->token,CAP_BPF );bpf_get_btf_vmlinux ();
@@ -19429,12 +19434,6 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr, __u3if (attr->prog_flags &BPF_F_ANY_ALIGNMENT ) env->strict_alignment = false; - env->allow_ptr_leaks =bpf_allow_ptr_leaks (); - env->allow_uninit_stack =bpf_allow_uninit_stack (); - env->bypass_spec_v1 =bpf_bypass_spec_v1 (); - env->bypass_spec_v4 =bpf_bypass_spec_v4 (); - env->bpf_capable =bpf_capable (); -if (is_priv) env->test_state_freq = attr->prog_flags &BPF_F_TEST_STATE_FREQ ;
net/core/filter.c +2 -2
@@ -8447,7 +8447,7 @@ static bool cg_skb_is_valid_access(int off, int size,return false; case bpf_ctx_range(struct __sk_buff, data): case bpf_ctx_range(struct __sk_buff, data_end): -if (!bpf_capable ()) +if (!bpf_token_capable (prog->aux->token,CAP_BPF ))return false;break ; }
@@ -8459,7 +8459,7 @@ static bool cg_skb_is_valid_access(int off, int size, case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):break ; case bpf_ctx_range(struct __sk_buff, tstamp): -if (!bpf_capable ()) +if (!bpf_token_capable (prog->aux->token,CAP_BPF ))return false;break ;default :
Signature · 1 line
2.34.1
Andrii Nakryiko
maintainerPATCH v3 13/14 libbpf: add BPF token support to bpf_prog_load() API
In reply to Andrii NakryikoWire through token_fd into bpf_prog_load(). Also make sure to pass allowed_{prog,attach}_types to kernel in bpf_token_create().
--- tools/lib/bpf/bpf.c | 5 ++++- tools/lib/bpf/bpf.h | 7 +++++-- 2 files changed, 9 insertions(+), 3 deletions(-)
tools/lib/bpf/bpf.c +4 -1
@@ -234,7 +234,7 @@ int bpf_prog_load(enum bpf_prog_type prog_type, const struct bpf_insn *insns, size_t insn_cnt, struct bpf_prog_load_opts *opts) { - const size_t attr_sz = offsetofend(union bpf_attr, log_true_size); + const size_t attr_sz = offsetofend(union bpf_attr, prog_token_fd);void *finfo =NULL , *linfo =NULL ;const char *func_info, *line_info;__u32 log_size, log_level, attach_prog_fd, attach_btf_obj_fd;
@@ -263,6 +263,7 @@ int bpf_prog_load(enum bpf_prog_type prog_type, attr.prog_flags =OPTS_GET (opts, prog_flags,0 ); attr.prog_ifindex =OPTS_GET (opts, prog_ifindex,0 ); attr.kern_version =OPTS_GET (opts, kern_version,0 ); + attr.prog_token_fd =OPTS_GET (opts, token_fd,0 );if (prog_name &&kernel_supports (NULL ,FEAT_PROG_NAME ))libbpf_strlcpy (attr.prog_name, prog_name,sizeof (attr.prog_name));
@@ -1223,6 +1224,8 @@ int bpf_token_create(int pin_path_fd, const char *pin_pathname, struct bpf_token attr.token_create.pin_flags =OPTS_GET (opts, pin_flags,0 ); attr.token_create.allowed_cmds =OPTS_GET (opts, allowed_cmds,0 ); attr.token_create.allowed_map_types =OPTS_GET (opts, allowed_map_types,0 ); + attr.token_create.allowed_prog_types =OPTS_GET (opts, allowed_prog_types,0 ); + attr.token_create.allowed_attach_types =OPTS_GET (opts, allowed_attach_types,0 ); ret =sys_bpf (BPF_TOKEN_CREATE , &attr, attr_sz);return libbpf_err_errno (ret);
tools/lib/bpf/bpf.h +5 -2
@@ -104,9 +104,10 @@ struct bpf_prog_load_opts {* If kernel doesn't support this feature, log_size is left unchanged. */ __u32 log_true_size; +__u32 token_fd; size_t :0; }; -#define bpf_prog_load_opts__last_field log_true_size +#define bpf_prog_load_opts__last_field token_fd LIBBPF_API int bpf_prog_load(enum bpf_prog_type prog_type, const char *prog_name, const char *license,
@@ -561,9 +562,11 @@ struct bpf_token_create_opts {__u32 pin_flags;__u64 allowed_cmds;__u64 allowed_map_types; +__u64 allowed_prog_types; +__u64 allowed_attach_types; size_t :0; }; -#define bpf_token_create_opts__last_field allowed_map_types +#define bpf_token_create_opts__last_field allowed_attach_types/** * @brief **bpf_token_create()** creates a new instance of BPF token, pinning
Signature · 1 line
2.34.1
Andrii Nakryiko
maintainerPATCH v3 14/14 selftests/bpf: add BPF token-enabled BPF_PROG_LOAD tests
In reply to Andrii NakryikoAdd a test validating that BPF token can be used to load privileged BPF program using privileged BPF helpers through delegated BPF token created by privileged process.
--- .../testing/selftests/bpf/prog_tests/token.c | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+)
tools/testing/selftests/bpf/prog_tests/token.c +66 -0
@@ -4,6 +4,7 @@#include <test_progs.h> #include <bpf/btf.h> #include "cap_helpers.h" +#include <linux/filter.h> static int drop_priv_caps (__u64 *old_caps) {
@@ -200,6 +201,69 @@ static void subtest_btf_token(void)ASSERT_OK (restore_priv_caps (old_caps),"restore_caps" ); } +static void subtest_prog_token (void ) +{ +LIBBPF_OPTS (bpf_token_create_opts, token_opts); +LIBBPF_OPTS (bpf_prog_load_opts, prog_opts); +int token_fd =0 , prog_fd =0 , err; +__u64 old_caps =0 ; +struct bpf_insn insns[] = { +/* bpf_jiffies64() requires CAP_BPF */ +BPF_RAW_INSN (BPF_JMP |BPF_CALL ,0 ,0 ,0 , BPF_FUNC_jiffies64), +/* bpf_get_current_task() requires CAP_PERFMON */ +BPF_RAW_INSN (BPF_JMP |BPF_CALL ,0 ,0 ,0 , BPF_FUNC_get_current_task), +/* r0 = 0; exit; */ +BPF_MOV64_IMM (BPF_REG_0 ,0 ), +BPF_EXIT_INSN (), + }; +size_t insn_cnt =ARRAY_SIZE (insns); + +/* create BPF token allowing BPF_PROG_LOAD command */ + token_opts.allowed_cmds =1ULL <<BPF_PROG_LOAD ; + token_opts.allowed_prog_types =1ULL <<BPF_PROG_TYPE_XDP ; + token_opts.allowed_attach_types =1ULL <<BPF_XDP ; + err =bpf_token_create (-EBADF ,TOKEN_PATH , &token_opts); +if (!ASSERT_OK (err,"token_create" )) +return ; + +/* drop privileges to test token_fd passing */ +if (!ASSERT_OK (drop_priv_caps (&old_caps),"drop_caps" )) + goto cleanup; + + token_fd =bpf_obj_get (TOKEN_PATH ); +if (!ASSERT_GT (token_fd,0 ,"token_get" )) + goto cleanup; + +/* validate we can successfully load BPF program with token; this +* being XDP program (CAP_NET_ADMIN) using bpf_jiffies64() (CAP_BPF) +* and bpf_get_current_task() (CAP_PERFMON) helpers validates we have +* BPF token wired properly in a bunch of places in the kernel +*/ + prog_opts.token_fd = token_fd; + prog_opts.expected_attach_type =BPF_XDP ; + prog_fd =bpf_prog_load (BPF_PROG_TYPE_XDP ,"token_prog" ,"GPL" , + insns, insn_cnt, &prog_opts); +if (!ASSERT_GT (prog_fd,0 ,"prog_fd" )) + goto cleanup; +close (prog_fd); + +/* now validate that we *cannot* load BPF program without token */ + prog_opts.token_fd =0 ; + prog_fd =bpf_prog_load (BPF_PROG_TYPE_XDP ,"token_prog" ,"GPL" , + insns, insn_cnt, &prog_opts); +if (!ASSERT_EQ (prog_fd, -EPERM ,"prog_fd_eperm" )) + goto cleanup; + +cleanup: +if (prog_fd >0 ) +close (prog_fd); +if (token_fd) +close (token_fd); +unlink (TOKEN_PATH ); +if (old_caps) +ASSERT_OK (restore_priv_caps (old_caps),"restore_caps" ); +} +void test_token (void ) {if (test__start_subtest ("token_create" ))
@@ -208,4 +272,6 @@ void test_token(void)subtest_map_token ();if (test__start_subtest ("btf_token" ))subtest_btf_token (); +if (test__start_subtest ("prog_token" )) +subtest_prog_token (); }
Signature · 1 line
2.34.1