BPF token support in libbpf's BPF object
Subsystems (4)
Andrii Nakryiko
maintainerv2 cover BPF token support in libbpf's BPF object
Add fuller support for BPF token in high-level BPF object APIs. This is the most frequently used way to work with BPF using libbpf, so supporting BPF token there is critical.
Patch #1 is improving kernel-side BPF_TOKEN_CREATE behavior by rejecting to create "empty" BPF token with no delegation. This seems like saner behavior which also makes libbpf's caching better overall. If we ever want to create BPF token with no delegate_xxx options set on BPF FS, we can use a new flag to enable that.
Patches #2-#5 refactor libbpf internals, mostly feature detection code, to prepare it from BPF token FD.
Patch #6 adds options to pass BPF token into BPF object open options. It also adds implicit BPF token creation logic to BPF object load step, even without any explicit involvement of the user. If the environment is setup properly, BPF token will be created transparently and used implicitly. This allows for all existing application to gain BPF token support by just linking with latest version of libbpf library. No source code modifications are required. All that under assumption that privileged container management agent properly set up default BPF FS instance at /sys/bpf/fs to allow BPF token creation.
Patches #7-#8 adds more selftests, validating BPF object APIs work as expected under unprivileged user namespaced conditions in the presence of BPF token.
v1->v2:
- remove minor code redundancies (Eduard, John);
- add acks and rebase.
Andrii Nakryiko (8):
bpf: fail BPF_TOKEN_CREATE if no delegation option was set on BPF FS
libbpf: split feature detectors definitions from cached results
libbpf: further decouple feature checking logic from bpf_object
libbpf: move feature detection code into its own file
libbpf: wire up token_fd into feature probing logic
libbpf: wire up BPF token support at BPF object level selftests/bpf: add BPF object loading tests with explicit token
passing
selftests/bpf: add tests for BPF object load with implicit token
kernel/bpf/token.c | 10 +- tools/lib/bpf/Build | 2 +- tools/lib/bpf/bpf.c | 9 +- tools/lib/bpf/btf.c | 7 +- tools/lib/bpf/elf.c | 2 - tools/lib/bpf/features.c | 478 +++++++++++++++ tools/lib/bpf/libbpf.c | 567 ++++-------------- tools/lib/bpf/libbpf.h | 28 +- tools/lib/bpf/libbpf_internal.h | 36 +- tools/lib/bpf/libbpf_probes.c | 8 +- tools/lib/bpf/str_error.h | 3 + .../testing/selftests/bpf/prog_tests/token.c | 235 ++++++++ tools/testing/selftests/bpf/progs/priv_map.c | 13 + tools/testing/selftests/bpf/progs/priv_prog.c | 13 + 14 files changed, 938 insertions(+), 473 deletions(-) create mode 100644 tools/lib/bpf/features.c create mode 100644 tools/testing/selftests/bpf/progs/priv_map.c create mode 100644 tools/testing/selftests/bpf/progs/priv_prog.c
Signature · 1 line
2.34.1
Andrii Nakryiko
maintainerPATCH v2 1/8 bpf: fail BPF_TOKEN_CREATE if no delegation option was set on BPF FS
It's quite confusing in practice when it's possible to successfully create a BPF token from BPF FS that didn't have any of delegate_xxx mount options set up. While it's not wrong, it's actually more meaningful to reject BPF_TOKEN_CREATE with specific error code (-ENOENT) to let user-space know that no token delegation is setup up.
So, instead of creating empty BPF token that will be always ignored because it doesn't have any of the allow_xxx bits set, reject it with -ENOENT. If we ever need empty BPF token to be possible, we can support that with extra flag passed into BPF_TOKEN_CREATE.
--- kernel/bpf/token.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)
kernel/bpf/token.c +9 -1
@@ -152,6 +152,15 @@ int bpf_token_create(union bpf_attr *attr) goto out_path; } + mnt_opts = path.dentry->d_sb->s_fs_info; +if (mnt_opts->delegate_cmds ==0 && + mnt_opts->delegate_maps ==0 && + mnt_opts->delegate_progs ==0 && + mnt_opts->delegate_attachs ==0 ) { + err = -ENOENT ;/* no BPF token delegation is set up */ + goto out_path; + } + mode =S_IFREG | ((S_IRUSR |S_IWUSR ) & ~current_umask ()); inode =bpf_get_inode (path.mnt->mnt_sb,NULL , mode);if (IS_ERR (inode)) {
@@ -181,7 +190,6 @@ int bpf_token_create(union bpf_attr *attr)/* remember bpffs owning userns for future ns_capable() checks */ token->userns =get_user_ns (userns); - mnt_opts = path.dentry->d_sb->s_fs_info; token->allowed_cmds = mnt_opts->delegate_cmds; token->allowed_maps = mnt_opts->delegate_maps; token->allowed_progs = mnt_opts->delegate_progs;
Signature · 1 line
2.34.1
Andrii Nakryiko
maintainerPATCH v2 2/8 libbpf: split feature detectors definitions from cached results
In reply to Andrii NakryikoSplit a list of supported feature detectors with their corresponding callbacks from actual cached supported/missing values. This will allow to have more flexible per-token or per-object feature detectors in subsequent refactorings.
--- tools/lib/bpf/libbpf.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-)
tools/lib/bpf/libbpf.c +12 -6
@@ -4999,12 +4999,17 @@ enum kern_feature_result { FEAT_MISSING = 2, }; +struct kern_feature_cache { +enum kern_feature_result res[__FEAT_CNT]; +}; +typedef int (*feature_probe_fn )(void ); +static struct kern_feature_cache feature_cache; +static struct kern_feature_desc {const char *desc;feature_probe_fn probe; -enum kern_feature_result res; } feature_probes[__FEAT_CNT] = { [FEAT_PROG_NAME ] = {"BPF program name" , probe_kern_prog_name,
@@ -5072,6 +5077,7 @@ static struct kern_feature_desc {bool kernel_supports (const struct bpf_object *obj,enum kern_feature_id feat_id) {struct kern_feature_desc *feat = &feature_probes[feat_id]; +struct kern_feature_cache *cache = &feature_cache;int ret;if (obj && obj->gen_loader)
@@ -5080,19 +5086,19 @@ bool kernel_supports(const struct bpf_object *obj, enum kern_feature_id feat_id)*/ return true; -if (READ_ONCE (feat->res) ==FEAT_UNKNOWN ) { +if (READ_ONCE (cache->res[feat_id]) ==FEAT_UNKNOWN ) { ret = feat->probe ();if (ret >0 ) { -WRITE_ONCE (feat->res,FEAT_SUPPORTED ); +WRITE_ONCE (cache->res[feat_id],FEAT_SUPPORTED ); }else if (ret ==0 ) { -WRITE_ONCE (feat->res,FEAT_MISSING ); +WRITE_ONCE (cache->res[feat_id],FEAT_MISSING ); }else {pr_warn ("Detection of kernel %s support failed: %d\n" , feat->desc, ret); -WRITE_ONCE (feat->res,FEAT_MISSING ); +WRITE_ONCE (cache->res[feat_id],FEAT_MISSING ); } } -return READ_ONCE (feat->res) ==FEAT_SUPPORTED ; +return READ_ONCE (cache->res[feat_id]) ==FEAT_SUPPORTED ; }static bool map_is_reuse_compat (const struct bpf_map *map,int map_fd)
Signature · 1 line
2.34.1
Andrii Nakryiko
maintainerPATCH v2 3/8 libbpf: further decouple feature checking logic from bpf_object
In reply to Andrii NakryikoAdd feat_supported() helper that accepts feature cache instead of bpf_object. This allows low-level code in bpf.c to not know or care about higher-level concept of bpf_object, yet it will be able to utilize custom feature checking in cases where BPF token might influence the outcome.
--- tools/lib/bpf/bpf.c | 6 +++--- tools/lib/bpf/libbpf.c | 22 +++++++++++++++------- tools/lib/bpf/libbpf_internal.h | 5 ++++- 3 files changed, 22 insertions(+), 11 deletions(-)
tools/lib/bpf/bpf.c +3 -3
@@ -146,7 +146,7 @@ int bump_rlimit_memlock(void)struct rlimit rlim;/* if kernel supports memcg-based accounting, skip bumping RLIMIT_MEMLOCK */ -if (memlock_bumped ||kernel_supports (NULL ,FEAT_MEMCG_ACCOUNT )) +if (memlock_bumped ||feat_supported (NULL ,FEAT_MEMCG_ACCOUNT ))return 0 ; memlock_bumped = true;
@@ -181,7 +181,7 @@ int bpf_map_create(enum bpf_map_type map_type,return libbpf_err (-EINVAL ); attr.map_type = map_type; -if (map_name &&kernel_supports (NULL ,FEAT_PROG_NAME )) +if (map_name &&feat_supported (NULL ,FEAT_PROG_NAME ))libbpf_strlcpy (attr.map_name, map_name,sizeof (attr.map_name)); attr.key_size = key_size; attr.value_size = value_size;
@@ -265,7 +265,7 @@ int bpf_prog_load(enum bpf_prog_type prog_type, 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 )) +if (prog_name &&feat_supported (NULL ,FEAT_PROG_NAME ))libbpf_strlcpy (attr.prog_name, prog_name,sizeof (attr.prog_name)); attr.license =ptr_to_u64 (license);
tools/lib/bpf/libbpf.c +15 -7
@@ -5074,17 +5074,14 @@ static struct kern_feature_desc { }, }; -bool kernel_supports (const struct bpf_object *obj,enum kern_feature_id feat_id) +bool feat_supported (struct kern_feature_cache *cache,enum kern_feature_id feat_id) {struct kern_feature_desc *feat = &feature_probes[feat_id]; -struct kern_feature_cache *cache = &feature_cache;int ret; -if (obj && obj->gen_loader) -/* To generate loader program assume the latest kernel -* to avoid doing extra prog_load, map_create syscalls. -*/ -return true; +/* assume global feature cache, unless custom one is provided */ +if (!cache) + cache = &feature_cache;if (READ_ONCE (cache->res[feat_id]) ==FEAT_UNKNOWN ) { ret = feat->probe ();
@@ -5101,6 +5098,17 @@ bool kernel_supports(const struct bpf_object *obj, enum kern_feature_id feat_id)return READ_ONCE (cache->res[feat_id]) ==FEAT_SUPPORTED ; } +bool kernel_supports (const struct bpf_object *obj,enum kern_feature_id feat_id) +{ +if (obj && obj->gen_loader) +/* To generate loader program assume the latest kernel +* to avoid doing extra prog_load, map_create syscalls. +*/ +return true; + +return feat_supported (NULL , feat_id); +} +static bool map_is_reuse_compat (const struct bpf_map *map,int map_fd) {struct bpf_map_info map_info;
tools/lib/bpf/libbpf_internal.h +4 -1
@@ -360,8 +360,11 @@ enum kern_feature_id { __FEAT_CNT, }; -int probe_memcg_account (void ); +struct kern_feature_cache ; +bool feat_supported (struct kern_feature_cache *cache,enum kern_feature_id feat_id);bool kernel_supports (const struct bpf_object *obj,enum kern_feature_id feat_id); + +int probe_memcg_account (void );int bump_rlimit_memlock (void );int parse_cpu_mask_str (const char *s,bool **mask,int *mask_sz);
Signature · 1 line
2.34.1
Andrii Nakryiko
maintainerPATCH v2 4/8 libbpf: move feature detection code into its own file
In reply to Andrii NakryikoIt's quite a lot of well isolated code, so it seems like a good candidate to move it out of libbpf.c to reduce its size.
--- tools/lib/bpf/Build | 2 +- tools/lib/bpf/elf.c | 2 - tools/lib/bpf/features.c | 473 ++++++++++++++++++++++++++++++++ tools/lib/bpf/libbpf.c | 463 +------------------------------ tools/lib/bpf/libbpf_internal.h | 2 + tools/lib/bpf/str_error.h | 3 + 6 files changed, 480 insertions(+), 465 deletions(-) create mode 100644 tools/lib/bpf/features.c
tools/lib/bpf/Build +1 -1
@@ -1,4 +1,4 @@ libbpf-y := libbpf.o bpf.o nlattr.o btf.o libbpf_errno.o str_error.o \ netlink.o bpf_prog_linfo.o libbpf_probes.o hashmap.o \ btf_dump.o ringbuf.o strset.o linker.o gen_loader.o relo_core.o \ - usdt.o zip.o elf.o + usdt.o zip.o elf.o features.o
tools/lib/bpf/elf.c +0 -2
@@ -11,8 +11,6 @@#include "libbpf_internal.h" #include "str_error.h" -#define STRERR_BUFSIZE 128 -/* A SHT_GNU_versym section holds 16-bit words. This bit is set if * the symbol is hidden and can only be seen when referenced using an * explicit version number. This is a GNU extension.
tools/lib/bpf/features.c new file +473 -0
473 lines not on this page · raw message
tools/lib/bpf/libbpf.c +1 -462
475 lines not on this page · raw message
tools/lib/bpf/libbpf_internal.h +2 -0
@@ -602,4 +602,6 @@ int elf_resolve_syms_offsets(const char *binary_path, int cnt,int elf_resolve_pattern_offsets (const char *binary_path,const char *pattern,unsigned long **poffsets,size_t *pcnt); +int probe_kern_syscall_wrapper (void ); +#endif /* __LIBBPF_LIBBPF_INTERNAL_H */
tools/lib/bpf/str_error.h +3 -0
@@ -2,5 +2,8 @@#ifndef __LIBBPF_STR_ERROR_H#define __LIBBPF_STR_ERROR_H +#define STRERR_BUFSIZE 128 +char *libbpf_strerror_r (int err,char *dst,int len); +#endif /* __LIBBPF_STR_ERROR_H */
Signature · 1 line
2.34.1
Andrii Nakryiko
maintainerPATCH v2 5/8 libbpf: wire up token_fd into feature probing logic
In reply to Andrii NakryikoAdjust feature probing callbacks to take into account optional token_fd. In unprivileged contexts, some feature detectors would fail to detect kernel support just because BPF program, BPF map, or BTF object can't be loaded due to privileged nature of those operations. So when BPF object is loaded with BPF token, this token should be used for feature probing.
This patch is setting support for this scenario, but we don't yet pass non-zero token FD. This will be added in the next patch.
We also switched BPF cookie detector from using kprobe program to tracepoint one, as tracepoint is somewhat less dangerous BPF program type and has higher likelihood of being allowed through BPF token in the future. This change has no effect on detection behavior.
--- tools/lib/bpf/bpf.c | 3 +- tools/lib/bpf/features.c | 101 +++++++++++++++++--------------- tools/lib/bpf/libbpf.c | 2 +- tools/lib/bpf/libbpf_internal.h | 20 +++++-- tools/lib/bpf/libbpf_probes.c | 8 ++- 5 files changed, 76 insertions(+), 58 deletions(-)
tools/lib/bpf/bpf.c +2 -1
@@ -103,7 +103,7 @@ int sys_bpf_prog_load(union bpf_attr *attr, unsigned int size, int attempts)* [0] https://lore.kernel.org/bpf/[email protected]/ * [1] d05512618056 ("bpf: Add bpf_ktime_get_coarse_ns helper") */ -int probe_memcg_account (void ) +int probe_memcg_account (int token_fd) { const size_t attr_sz = offsetofend(union bpf_attr, attach_btf_obj_fd);struct bpf_insn insns[] = {
@@ -120,6 +120,7 @@ int probe_memcg_account(void) attr.insns =ptr_to_u64 (insns); attr.insn_cnt = insn_cnt; attr.license =ptr_to_u64 ("GPL" ); + attr.prog_token_fd = token_fd; prog_fd =sys_bpf_fd (BPF_PROG_LOAD , &attr, attr_sz);if (prog_fd >=0 ) {
tools/lib/bpf/features.c +53 -48
296 lines not on this page · raw message
tools/lib/bpf/libbpf.c +1 -1
@@ -10165,7 +10165,7 @@ static const char *arch_specific_syscall_pfx(void)#endif } -int probe_kern_syscall_wrapper (void ) +int probe_kern_syscall_wrapper (int token_fd) {char syscall_name[64 ];const char *ksys_pfx;
tools/lib/bpf/libbpf_internal.h +15 -5
@@ -360,17 +360,29 @@ enum kern_feature_id { __FEAT_CNT, }; -struct kern_feature_cache ; +enum kern_feature_result { +FEAT_UNKNOWN =0 , +FEAT_SUPPORTED =1 , +FEAT_MISSING =2 , +}; + +struct kern_feature_cache { +enum kern_feature_result res[__FEAT_CNT]; +int token_fd; +}; +bool feat_supported (struct kern_feature_cache *cache,enum kern_feature_id feat_id);bool kernel_supports (const struct bpf_object *obj,enum kern_feature_id feat_id); -int probe_memcg_account (void ); +int probe_kern_syscall_wrapper (int token_fd); +int probe_memcg_account (int token_fd);int bump_rlimit_memlock (void );int parse_cpu_mask_str (const char *s,bool **mask,int *mask_sz);int parse_cpu_mask_file (const char *fcpu,bool **mask,int *mask_sz);int libbpf__load_raw_btf (const char *raw_types,size_t types_len, -const char *str_sec,size_t str_len); +const char *str_sec,size_t str_len, +int token_fd);int btf_load_into_kernel (struct btf *btf,char *log_buf,size_t log_sz,__u32 log_level);struct btf *btf_get_from_fd (int btf_fd,struct btf *base_btf);
@@ -602,6 +614,4 @@ int elf_resolve_syms_offsets(const char *binary_path, int cnt,int elf_resolve_pattern_offsets (const char *binary_path,const char *pattern,unsigned long **poffsets,size_t *pcnt); -int probe_kern_syscall_wrapper (void ); -#endif /* __LIBBPF_LIBBPF_INTERNAL_H */
tools/lib/bpf/libbpf_probes.c +5 -3
@@ -219,7 +219,8 @@ int libbpf_probe_bpf_prog_type(enum bpf_prog_type prog_type, const void *opts) }int libbpf__load_raw_btf (const char *raw_types,size_t types_len, -const char *str_sec,size_t str_len) +const char *str_sec,size_t str_len, +int token_fd) {struct btf_header hdr = { .magic =BTF_MAGIC ,
@@ -229,6 +230,7 @@ int libbpf__load_raw_btf(const char *raw_types, size_t types_len, .str_off = types_len, .str_len = str_len, }; + LIBBPF_OPTS(bpf_btf_load_opts, opts, .token_fd = token_fd);int btf_fd, btf_len;__u8 *raw_btf;
@@ -241,7 +243,7 @@ int libbpf__load_raw_btf(const char *raw_types, size_t types_len,memcpy (raw_btf + hdr.hdr_len, raw_types, hdr.type_len);memcpy (raw_btf + hdr.hdr_len + hdr.type_len, str_sec, hdr.str_len); - btf_fd =bpf_btf_load (raw_btf, btf_len,NULL ); + btf_fd =bpf_btf_load (raw_btf, btf_len, &opts);free (raw_btf);return btf_fd;
@@ -271,7 +273,7 @@ static int load_local_storage_btf(void) };return libbpf__load_raw_btf ((char *)types,sizeof (types), - strs,sizeof (strs)); + strs,sizeof (strs),0 ); }static int probe_map_create (enum bpf_map_type map_type)
Signature · 1 line
2.34.1
Re: [PATCH v2 bpf-next 5/8] libbpf: wire up token_fd into feature probing logic
tools/lib/bpf/features.c static int probe_kern_btf_enum64(void) · line 385
Quoted 13 lines · Andrii Nakryiko
tools/lib/bpf/features.c +1
-11-enum kern_feature_result { -FEAT_UNKNOWN =0 , -FEAT_SUPPORTED =1 , -FEAT_MISSING =2 , -}; - -typedef int (*feature_probe_fn )(void ); - -struct kern_feature_cache { -enum kern_feature_result res[__FEAT_CNT]; -}; +typedef int (*feature_probe_fn )(int /* token_fd */ );
Should have been in the previous patch?
Re: [PATCH v2 bpf-next 5/8] libbpf: wire up token_fd into feature probing logic
Quoted 17 lines · Alexei Starovoitov
tools/lib/bpf/features.c static int probe_kern_btf_enum64(void) · line 385
Quoted 13 lines · Andrii Nakryiko
tools/lib/bpf/features.c +1
-11-enum kern_feature_result { -FEAT_UNKNOWN =0 , -FEAT_SUPPORTED =1 , -FEAT_MISSING =2 , -}; - -typedef int (*feature_probe_fn )(void ); - -struct kern_feature_cache { -enum kern_feature_result res[__FEAT_CNT]; -}; +typedef int (*feature_probe_fn )(int /* token_fd */ );Should have been in the previous patch?
This is the first patch that needs to instantiate struct kern_feature_cache outside of features.c (now also in struct bpf_object), so I only started needing this change in this patch. But yep, I can probably place it in a final place in the previous patch and make it less distracting from the main change. Will adjust in the next revision.
Andrii Nakryiko
maintainerPATCH v2 6/8 libbpf: wire up BPF token support at BPF object level
In reply to Andrii NakryikoAdd BPF token support to BPF object-level functionality.
BPF token is supported by BPF object logic either as an explicitly provided BPF token from outside (through BPF FS path or explicit BPF token FD), or implicitly (unless prevented through bpf_object_open_opts).
Implicit mode is assumed to be the most common one for user namespaced unprivileged workloads. The assumption is that privileged container manager sets up default BPF FS mount point at /sys/fs/bpf with BPF token delegation options (delegate_{cmds,maps,progs,attachs} mount options). BPF object during loading will attempt to create BPF token from /sys/fs/bpf location, and pass it for all relevant operations (currently, map creation, BTF load, and program load).
In this implicit mode, if BPF token creation fails due to whatever reason (BPF FS is not mounted, or kernel doesn't support BPF token, etc), this is not considered an error. BPF object loading sequence will proceed with no BPF token.
In explicit BPF token mode, user provides explicitly either custom BPF FS mount point path or creates BPF token on their own and just passes token FD directly. In such case, BPF object will either dup() token FD (to not require caller to hold onto it for entire duration of BPF object lifetime) or will attempt to create BPF token from provided BPF FS location. If BPF token creation fails, that is considered a critical error and BPF object load fails with an error.
Libbpf provides a way to disable implicit BPF token creation, if it causes any troubles (BPF token is designed to be completely optional and shouldn't cause any problems even if provided, but in the world of BPF LSM, custom security logic can be installed that might change outcome dependin on the presence of BPF token). To disable libbpf's default BPF token creation behavior user should provide either invalid BPF token FD (negative), or empty bpf_token_path option.
BPF token presence can influence libbpf's feature probing, so if BPF object has associated BPF token, feature probing is instructed to use BPF object-specific feature detection cache and token FD.
--- tools/lib/bpf/btf.c | 7 +- tools/lib/bpf/libbpf.c | 118 ++++++++++++++++++++++++++++++-- tools/lib/bpf/libbpf.h | 28 +++++++- tools/lib/bpf/libbpf_internal.h | 17 ++++- 4 files changed, 158 insertions(+), 12 deletions(-)
tools/lib/bpf/btf.c +5 -2
@@ -1317,7 +1317,9 @@ struct btf *btf__parse_split(const char *path, struct btf *base_btf)static void *btf_get_raw_data (const struct btf *btf,__u32 *size,bool swap_endian); -int btf_load_into_kernel (struct btf *btf,char *log_buf,size_t log_sz,__u32 log_level) +int btf_load_into_kernel (struct btf *btf, +char *log_buf,size_t log_sz,__u32 log_level, +int token_fd) {LIBBPF_OPTS (bpf_btf_load_opts, opts);__u32 buf_sz =0 , raw_size;
@@ -1367,6 +1369,7 @@ int btf_load_into_kernel(struct btf *btf, char *log_buf, size_t log_sz, __u32 lo opts.log_level = log_level; } + opts.token_fd = token_fd; btf->fd =bpf_btf_load (raw_data, raw_size, &opts);if (btf->fd <0 ) {/* time to turn on verbose mode and try again */
@@ -1394,7 +1397,7 @@ int btf_load_into_kernel(struct btf *btf, char *log_buf, size_t log_sz, __u32 loint btf__load_into_kernel (struct btf *btf) { -return btf_load_into_kernel (btf,NULL ,0 ,0 ); +return btf_load_into_kernel (btf,NULL ,0 ,0 ,0 ); }int btf__fd (const struct btf *btf)
tools/lib/bpf/libbpf.c +111 -7
211 lines not on this page · raw message
tools/lib/bpf/libbpf.h +27 -1
@@ -177,10 +177,36 @@ struct bpf_object_open_opts {* logs through its print callback. */ __u32 kernel_log_level; +/* FD of a BPF token instantiated by user through bpf_token_create() +* API. BPF object will keep dup()'ed FD internally, so passed token +* FD can be closed after BPF object/skeleton open step. +* +* Setting bpf_token_fd to negative value disables libbpf's automatic +* attempt to create BPF token from default BPF FS mount point +* (/sys/fs/bpf), in case this default behavior is undesirable. +* +* bpf_token_path and bpf_token_fd are mutually exclusive and only one +* of those options should be set. +*/ +int bpf_token_fd; +/* Path to BPF FS mount point to derive BPF token from. +* +* Created BPF token will be used for all bpf() syscall operations +* that accept BPF token (e.g., map creation, BTF and program loads, +* etc) automatically within instantiated BPF object. +* +* Setting bpf_token_path option to empty string disables libbpf's +* automatic attempt to create BPF token from default BPF FS mount +* point (/sys/fs/bpf), in case this default behavior is undesirable. +* +* bpf_token_path and bpf_token_fd are mutually exclusive and only one +* of those options should be set. +*/ +const char *bpf_token_path; size_t :0; }; -#define bpf_object_open_opts__last_field kernel_log_level +#define bpf_object_open_opts__last_field bpf_token_path/** * @brief **bpf_object__open()** creates a bpf_object by opening
tools/lib/bpf/libbpf_internal.h +15 -2
@@ -383,7 +383,9 @@ int parse_cpu_mask_file(const char *fcpu, bool **mask, int *mask_sz);int libbpf__load_raw_btf (const char *raw_types,size_t types_len,const char *str_sec,size_t str_len,int token_fd); -int btf_load_into_kernel (struct btf *btf,char *log_buf,size_t log_sz,__u32 log_level); +int btf_load_into_kernel (struct btf *btf, +char *log_buf,size_t log_sz,__u32 log_level, +int token_fd);struct btf *btf_get_from_fd (int btf_fd,struct btf *base_btf); void btf_get_kernel_prefix_kind(enum bpf_attach_type attach_type,
@@ -547,6 +549,17 @@ static inline bool is_ldimm64_insn(struct bpf_insn *insn)return insn->code == (BPF_LD |BPF_IMM |BPF_DW ); } +/* Unconditionally dup FD, ensuring it doesn't use [0, 2] range. +* Original FD is not closed or altered in any other way. +* Preserves original FD value, if it's invalid (negative). +*/ +static inline int dup_good_fd (int fd) +{ +if (fd <0 ) +return fd; +return fcntl (fd,F_DUPFD_CLOEXEC ,3 ); +} +/* if fd is stdin, stdout, or stderr, dup to a fd greater than 2 * Takes ownership of the fd passed in, and closes it if calling * fcntl(fd, F_DUPFD_CLOEXEC, 3).
@@ -558,7 +571,7 @@ static inline int ensure_good_fd(int fd)if (fd <0 )return fd;if (fd <3 ) { - fd =fcntl (fd,F_DUPFD_CLOEXEC ,3 ); + fd =dup_good_fd (fd); saved_errno = errno;close (old_fd); errno = saved_errno;
Signature · 1 line
2.34.1
Re: [PATCH v2 bpf-next 6/8] libbpf: wire up BPF token support at BPF object level
Quoted 28 lines · Andrii Nakryiko
Add BPF token support to BPF object-level functionality.
BPF token is supported by BPF object logic either as an explicitly provided BPF token from outside (through BPF FS path or explicit BPF token FD), or implicitly (unless prevented through
bpf_object_open_opts).Implicit mode is assumed to be the most common one for user namespaced unprivileged workloads. The assumption is that privileged container manager sets up default BPF FS mount point at
/sys/fs/bpfwith BPF token delegation options (delegate_{cmds,maps,progs,attachs} mount options). BPF object during loading will attempt to create BPF token from/sys/fs/bpflocation, and pass it for all relevant operations (currently, map creation, BTF load, and program load).In this implicit mode, if BPF token creation fails due to whatever reason (BPF FS is not mounted, or kernel doesn't support BPF token, etc), this is not considered an error. BPF object loading sequence will proceed with no BPF token.
In explicit BPF token mode, user provides explicitly either custom BPF FS mount point path or creates BPF token on their own and just passes token FD directly. In such case, BPF object will either
dup()token FD (to not require caller to hold onto it for entire duration of BPF object lifetime) or will attempt to create BPF token from provided BPF FS location. If BPF token creation fails, that is considered a critical error and BPF object load fails with an error.
Quoted 7 lines
Libbpf provides a way to disable implicit BPF token creation, if it causes any troubles (BPF token is designed to be completely optional and shouldn't cause any problems even if provided, but in the world of BPF LSM, custom security logic can be installed that might change outcome dependin on the presence of BPF token). To disable libbpf's default BPF token creation behavior user should provide either invalid BPF token FD (negative), or empty
bpf_token_pathoption.
Have you considered an external (to the application) way to provide bpffs path and enable/disable implicit mode?
Since libbpf is trying to provide the seamless use of token (just recompiling with new libbpf and admin setup) it seems the admin should be able to pick the location as well. envvar would be one way.
Re: [PATCH v2 bpf-next 6/8] libbpf: wire up BPF token support at BPF object level
Quoted 38 lines · Alexei Starovoitov
Quoted 28 lines · Andrii Nakryiko
Add BPF token support to BPF object-level functionality.
BPF token is supported by BPF object logic either as an explicitly provided BPF token from outside (through BPF FS path or explicit BPF token FD), or implicitly (unless prevented through
bpf_object_open_opts).Implicit mode is assumed to be the most common one for user namespaced unprivileged workloads. The assumption is that privileged container manager sets up default BPF FS mount point at
/sys/fs/bpfwith BPF token delegation options (delegate_{cmds,maps,progs,attachs} mount options). BPF object during loading will attempt to create BPF token from/sys/fs/bpflocation, and pass it for all relevant operations (currently, map creation, BTF load, and program load).In this implicit mode, if BPF token creation fails due to whatever reason (BPF FS is not mounted, or kernel doesn't support BPF token, etc), this is not considered an error. BPF object loading sequence will proceed with no BPF token.
In explicit BPF token mode, user provides explicitly either custom BPF FS mount point path or creates BPF token on their own and just passes token FD directly. In such case, BPF object will either
dup()token FD (to not require caller to hold onto it for entire duration of BPF object lifetime) or will attempt to create BPF token from provided BPF FS location. If BPF token creation fails, that is considered a critical error and BPF object load fails with an error.Quoted 7 lines
Libbpf provides a way to disable implicit BPF token creation, if it causes any troubles (BPF token is designed to be completely optional and shouldn't cause any problems even if provided, but in the world of BPF LSM, custom security logic can be installed that might change outcome dependin on the presence of BPF token). To disable libbpf's default BPF token creation behavior user should provide either invalid BPF token FD (negative), or empty
bpf_token_pathoption.
Quoted 5 lines
Have you considered an external (to the application) way to provide bpffs path and enable/disable implicit mode?
Since libbpf is trying to provide the seamless use of token (just recompiling with new libbpf and admin setup) it seems the admin should be able to pick the location as well. envvar would be one way.
I haven't considered this yet, but I see the point. I can define a LIBBPF_BPF_TOKEN_PATH envvar which will be used if the user didn't explicitly provide a path or token fd through bpf_object_open_opts. Enable/disable semantics will be the same as bpf_object_open_opts->bpf_token_path: unset - default value, empty - disable, otherwise it will point to BPF FS location.
I'll add it as a separate patch in the next revision.
Andrii Nakryiko
maintainerPATCH v2 7/8 selftests/bpf: add BPF object loading tests with explicit token passing
In reply to Andrii NakryikoAdd a few tests that attempt to load BPF object containing privileged map, program, and the one requiring mandatory BTF uploading into the kernel (to validate token FD propagation to BPF_BTF_LOAD command).
--- .../testing/selftests/bpf/prog_tests/token.c | 159 ++++++++++++++++++ tools/testing/selftests/bpf/progs/priv_map.c | 13 ++ tools/testing/selftests/bpf/progs/priv_prog.c | 13 ++ 3 files changed, 185 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/priv_map.c create mode 100644 tools/testing/selftests/bpf/progs/priv_prog.c
tools/testing/selftests/bpf/prog_tests/token.c +159 -0
@@ -14,6 +14,9 @@#include <sys/socket.h> #include <sys/syscall.h> #include <sys/un.h> +#include "priv_map.skel.h" +#include "priv_prog.skel.h" +#include "dummy_st_ops_success.skel.h" static inline int sys_mount(const char *dev_name, const char *dir_name, const char *type, unsigned long flags,
@@ -643,6 +646,123 @@ static int userns_prog_load(int mnt_fd)return err; } +static int userns_obj_priv_map (int mnt_fd) +{ +LIBBPF_OPTS (bpf_object_open_opts, opts); +char buf[256 ]; +struct priv_map *skel; +int err, token_fd; + + skel =priv_map__open_and_load (); +if (!ASSERT_ERR_PTR (skel,"obj_tokenless_load" )) { +priv_map__destroy (skel); +return -EINVAL ; + } + +/* use bpf_token_path to provide BPF FS path */ +snprintf (buf,sizeof (buf),"/proc/self/fd/%d" , mnt_fd); + opts.bpf_token_path = buf; + skel =priv_map__open_opts (&opts); +if (!ASSERT_OK_PTR (skel,"obj_token_path_open" )) +return -EINVAL ; + + err =priv_map__load (skel); +priv_map__destroy (skel); +if (!ASSERT_OK (err,"obj_token_path_load" )) +return -EINVAL ; + +/* create token and pass it through bpf_token_fd */ + token_fd =bpf_token_create (mnt_fd,NULL ); +if (!ASSERT_GT (token_fd,0 ,"create_token" )) +return -EINVAL ; + + opts.bpf_token_path =NULL ; + opts.bpf_token_fd = token_fd; + skel =priv_map__open_opts (&opts); +if (!ASSERT_OK_PTR (skel,"obj_token_fd_open" )) +return -EINVAL ; + +/* we can close our token FD, bpf_object owns dup()'ed FD now */ +close (token_fd); + + err =priv_map__load (skel); +priv_map__destroy (skel); +if (!ASSERT_OK (err,"obj_token_fd_load" )) +return -EINVAL ; + +return 0 ; +} + +static int userns_obj_priv_prog (int mnt_fd) +{ +LIBBPF_OPTS (bpf_object_open_opts, opts); +char buf[256 ]; +struct priv_prog *skel; +int err; + + skel =priv_prog__open_and_load (); +if (!ASSERT_ERR_PTR (skel,"obj_tokenless_load" )) { +priv_prog__destroy (skel); +return -EINVAL ; + } + +/* use bpf_token_path to provide BPF FS path */ +snprintf (buf,sizeof (buf),"/proc/self/fd/%d" , mnt_fd); + opts.bpf_token_path = buf; + skel =priv_prog__open_opts (&opts); +if (!ASSERT_OK_PTR (skel,"obj_token_path_open" )) +return -EINVAL ; + + err =priv_prog__load (skel); +priv_prog__destroy (skel); +if (!ASSERT_OK (err,"obj_token_path_load" )) +return -EINVAL ; + +return 0 ; +} + +/* this test is called with BPF FS that doesn't delegate BPF_BTF_LOAD command, +* which should cause struct_ops application to fail, as BTF won't be uploaded +* into the kernel, even if STRUCT_OPS programs themselves are allowed +*/ +static int validate_struct_ops_load (int mnt_fd,bool expect_success) +{ +LIBBPF_OPTS (bpf_object_open_opts, opts); +char buf[256 ]; +struct dummy_st_ops_success *skel; +int err; + +snprintf (buf,sizeof (buf),"/proc/self/fd/%d" , mnt_fd); + opts.bpf_token_path = buf; + skel =dummy_st_ops_success__open_opts (&opts); +if (!ASSERT_OK_PTR (skel,"obj_token_path_open" )) +return -EINVAL ; + + err =dummy_st_ops_success__load (skel); +dummy_st_ops_success__destroy (skel); +if (expect_success) { +if (!ASSERT_OK (err,"obj_token_path_load" )) +return -EINVAL ; + }else /* expect failure */ { +if (!ASSERT_ERR (err,"obj_token_path_load" )) +return -EINVAL ; + } + +return 0 ; +} + +static int userns_obj_priv_btf_fail (int mnt_fd) +{ +return validate_struct_ops_load (mnt_fd, false/* should fail */ ); +} + +static int userns_obj_priv_btf_success (int mnt_fd) +{ +return validate_struct_ops_load (mnt_fd, true/* should succeed */ ); +} + +#define bit (n) (1ULL << (n)) +void test_token (void ) {if (test__start_subtest ("map_token" )) {
@@ -669,4 +789,43 @@ void test_token(void)subtest_userns (&opts, userns_prog_load); } +if (test__start_subtest ("obj_priv_map" )) { +struct bpffs_opts opts = { + .cmds =bit (BPF_MAP_CREATE ), + .maps =bit (BPF_MAP_TYPE_QUEUE ), + }; + +subtest_userns (&opts, userns_obj_priv_map); + } +if (test__start_subtest ("obj_priv_prog" )) { +struct bpffs_opts opts = { + .cmds =bit (BPF_PROG_LOAD ), + .progs =bit (BPF_PROG_TYPE_KPROBE ), + .attachs = ~0ULL , + }; + +subtest_userns (&opts, userns_obj_priv_prog); + } +if (test__start_subtest ("obj_priv_btf_fail" )) { +struct bpffs_opts opts = { +/* disallow BTF loading */ + .cmds =bit (BPF_MAP_CREATE ) |bit (BPF_PROG_LOAD ), + .maps =bit (BPF_MAP_TYPE_STRUCT_OPS ), + .progs =bit (BPF_PROG_TYPE_STRUCT_OPS ), + .attachs = ~0ULL , + }; + +subtest_userns (&opts, userns_obj_priv_btf_fail); + } +if (test__start_subtest ("obj_priv_btf_success" )) { +struct bpffs_opts opts = { +/* allow BTF loading */ + .cmds =bit (BPF_BTF_LOAD ) |bit (BPF_MAP_CREATE ) |bit (BPF_PROG_LOAD ), + .maps =bit (BPF_MAP_TYPE_STRUCT_OPS ), + .progs =bit (BPF_PROG_TYPE_STRUCT_OPS ), + .attachs = ~0ULL , + }; + +subtest_userns (&opts, userns_obj_priv_btf_success); + } }
tools/testing/selftests/bpf/progs/priv_map.c new file +13 -0
@@ -0,0 +1,13 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2023 Meta Platforms, Inc. and affiliates. */ + +#include "vmlinux.h" +#include <bpf/bpf_helpers.h> + +char _license[] SEC("license") = "GPL"; + +struct { + __uint(type, BPF_MAP_TYPE_QUEUE); + __uint(max_entries, 1); + __type(value, __u32); +} priv_map SEC(".maps");
tools/testing/selftests/bpf/progs/priv_prog.c new file +13 -0
@@ -0,0 +1,13 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2023 Meta Platforms, Inc. and affiliates. */ + +#include "vmlinux.h" +#include <bpf/bpf_helpers.h> + +char _license[] SEC("license") = "GPL"; + +SEC("kprobe") +int kprobe_prog (void *ctx) +{ +return 1 ; +}
Signature · 1 line
2.34.1
Andrii Nakryiko
maintainerPATCH v2 8/8 selftests/bpf: add tests for BPF object load with implicit token
In reply to Andrii NakryikoAdd a test to validate libbpf's implicit BPF token creation from default BPF FS location (/sys/fs/bpf). Also validate that disabling this implicit BPF token creation works.
--- .../testing/selftests/bpf/prog_tests/token.c | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+)
tools/testing/selftests/bpf/prog_tests/token.c +76 -0
@@ -12,6 +12,7 @@#include <linux/unistd.h> #include <linux/mount.h> #include <sys/socket.h> +#include <sys/stat.h> #include <sys/syscall.h> #include <sys/un.h> #include "priv_map.skel.h"
@@ -45,6 +46,13 @@ static inline int sys_fsmount(int fs_fd, unsigned flags, unsigned ms_flags)return syscall (__NR_fsmount, fs_fd, flags, ms_flags); } +static inline int sys_move_mount (int from_dfd,const char *from_path, +int to_dfd,const char *to_path, +unsigned flags) +{ +return syscall (__NR_move_mount, from_dfd, from_path, to_dfd, to_path, flags); +} +static int drop_priv_caps (__u64 *old_caps) { return cap_disable_effective((1ULL << CAP_BPF) |
@@ -761,6 +769,63 @@ static int userns_obj_priv_btf_success(int mnt_fd)return validate_struct_ops_load (mnt_fd, true/* should succeed */ ); } +static int userns_obj_priv_implicit_token (int mnt_fd) +{ +LIBBPF_OPTS (bpf_object_open_opts, opts); +struct dummy_st_ops_success *skel; +int err; + +/* before we mount BPF FS with token delegation, struct_ops skeleton +* should fail to load +*/ + skel =dummy_st_ops_success__open_and_load (); +if (!ASSERT_ERR_PTR (skel,"obj_tokenless_load" )) { +dummy_st_ops_success__destroy (skel); +return -EINVAL ; + } + +/* mount custom BPF FS over /sys/fs/bpf so that libbpf can create BPF +* token automatically and implicitly +*/ + err =sys_move_mount (mnt_fd,"" ,AT_FDCWD ,"/sys/fs/bpf" ,MOVE_MOUNT_F_EMPTY_PATH ); +if (!ASSERT_OK (err,"move_mount_bpffs" )) +return -EINVAL ; + +/* now the same struct_ops skeleton should succeed thanks to libppf +* creating BPF token from /sys/fs/bpf mount point +*/ + skel =dummy_st_ops_success__open_and_load (); +if (!ASSERT_OK_PTR (skel,"obj_implicit_token_load" )) +return -EINVAL ; + +dummy_st_ops_success__destroy (skel); + +/* now disable implicit token through empty bpf_token_path, should fail */ + opts.bpf_token_path ="" ; + skel =dummy_st_ops_success__open_opts (&opts); +if (!ASSERT_OK_PTR (skel,"obj_empty_token_path_open" )) +return -EINVAL ; + + err =dummy_st_ops_success__load (skel); +dummy_st_ops_success__destroy (skel); +if (!ASSERT_ERR (err,"obj_empty_token_path_load" )) +return -EINVAL ; + +/* now disable implicit token through negative bpf_token_fd, should fail */ + opts.bpf_token_path =NULL ; + opts.bpf_token_fd =-1 ; + skel =dummy_st_ops_success__open_opts (&opts); +if (!ASSERT_OK_PTR (skel,"obj_neg_token_fd_open" )) +return -EINVAL ; + + err =dummy_st_ops_success__load (skel); +dummy_st_ops_success__destroy (skel); +if (!ASSERT_ERR (err,"obj_neg_token_fd_load" )) +return -EINVAL ; + +return 0 ; +} +#define bit (n) (1ULL << (n))void test_token (void )
@@ -828,4 +893,15 @@ void test_token(void)subtest_userns (&opts, userns_obj_priv_btf_success); } +if (test__start_subtest ("obj_priv_implicit_token" )) { +struct bpffs_opts opts = { +/* allow BTF loading */ + .cmds =bit (BPF_BTF_LOAD ) |bit (BPF_MAP_CREATE ) |bit (BPF_PROG_LOAD ), + .maps =bit (BPF_MAP_TYPE_STRUCT_OPS ), + .progs =bit (BPF_PROG_TYPE_STRUCT_OPS ), + .attachs = ~0ULL , + }; + +subtest_userns (&opts, userns_obj_priv_implicit_token); + } }
Signature · 1 line
2.34.1