The BPF_*_GET_NEXT_ID commands are currently restricted to system-wide CAP_SYS_ADMIN, but iterating the objects themselves is not actually an administrator-only operation, so this seems overly restrictive. In particular, being able to walk the BTF IDs is necessary for libbpf to attach to functions in modules. So the CAP_SYS_ADMIN restriction prevents some BPF applications from working with permissions delegated by a bpf_token. Relax the permission check in bpf_obj_get_next_id() to token_capable(CAP_BPF). There is no change to the operations that turn the IDs into file descriptors (BPF_*_GET_FD_BY_ID commands). Signed-off-by: Toke Høiland-Jørgensen --- include/uapi/linux/bpf.h | 5 ++++- kernel/bpf/syscall.c | 33 ++++++++++++++++++++++++++------- tools/include/uapi/linux/bpf.h | 5 ++++- 3 files changed, 34 insertions(+), 9 deletions(-) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 62de0919db72..cdc313f9ab14 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -1786,7 +1786,10 @@ union bpf_attr { }; __u32 next_id; __u32 open_flags; - __s32 fd_by_id_token_fd; + union { + __s32 fd_by_id_token_fd; + __s32 get_next_id_token_fd; + }; }; struct { /* anonymous struct used by BPF_OBJ_GET_INFO_BY_FD */ diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index ec8e78792911..1440c10ea0cc 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -4872,21 +4872,40 @@ static int bpf_prog_test_run(const union bpf_attr *attr, return ret; } -#define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id +#define BPF_OBJ_GET_NEXT_ID_LAST_FIELD get_next_id_token_fd -static int bpf_obj_get_next_id(const union bpf_attr *attr, +static int bpf_obj_get_next_id(enum bpf_cmd cmd, + const union bpf_attr *attr, union bpf_attr __user *uattr, struct idr *idr, spinlock_t *lock) { + struct bpf_token *token = NULL; u32 next_id = attr->start_id; int err = 0; if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX) return -EINVAL; - if (!capable(CAP_SYS_ADMIN)) + if (attr->open_flags & ~BPF_F_TOKEN_FD) + return -EINVAL; + + if (attr->open_flags & BPF_F_TOKEN_FD) { + token = bpf_token_get_from_fd(attr->fd_by_id_token_fd); + if (IS_ERR(token)) + return PTR_ERR(token); + if (!bpf_token_allow_cmd(token, cmd)) { + bpf_token_put(token); + token = NULL; + } + } + + if (!bpf_token_capable(token, CAP_BPF)) { + bpf_token_put(token); return -EPERM; + } + + bpf_token_put(token); next_id++; spin_lock_bh(lock); @@ -6523,15 +6542,15 @@ static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uattr, unsigned int size, err = bpf_prog_test_run(&attr, uattr.user); break; case BPF_PROG_GET_NEXT_ID: - err = bpf_obj_get_next_id(&attr, uattr.user, + err = bpf_obj_get_next_id(cmd, &attr, uattr.user, &prog_idr, &prog_idr_lock); break; case BPF_MAP_GET_NEXT_ID: - err = bpf_obj_get_next_id(&attr, uattr.user, + err = bpf_obj_get_next_id(cmd, &attr, uattr.user, &map_idr, &map_idr_lock); break; case BPF_BTF_GET_NEXT_ID: - err = bpf_obj_get_next_id(&attr, uattr.user, + err = bpf_obj_get_next_id(cmd, &attr, uattr.user, &btf_idr, &btf_idr_lock); break; case BPF_PROG_GET_FD_BY_ID: @@ -6586,7 +6605,7 @@ static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uattr, unsigned int size, err = bpf_link_get_fd_by_id(&attr); break; case BPF_LINK_GET_NEXT_ID: - err = bpf_obj_get_next_id(&attr, uattr.user, + err = bpf_obj_get_next_id(cmd, &attr, uattr.user, &link_idr, &link_idr_lock); break; case BPF_ENABLE_STATS: diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 62de0919db72..cdc313f9ab14 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -1786,7 +1786,10 @@ union bpf_attr { }; __u32 next_id; __u32 open_flags; - __s32 fd_by_id_token_fd; + union { + __s32 fd_by_id_token_fd; + __s32 get_next_id_token_fd; + }; }; struct { /* anonymous struct used by BPF_OBJ_GET_INFO_BY_FD */ -- 2.56.1