Skip to content

Commit 0de445d

Browse files
mykyta5anakryiko
authored andcommitted
bpf: BPF token support for BPF_BTF_GET_FD_BY_ID
Currently BPF_BTF_GET_FD_BY_ID requires CAP_SYS_ADMIN, which does not allow running it from user namespace. This creates a problem when freplace program running from user namespace needs to query target program BTF. This patch relaxes capable check from CAP_SYS_ADMIN to CAP_BPF and adds support for BPF token that can be passed in attributes to syscall. Signed-off-by: Mykyta Yatsenko <yatsenko@meta.com> Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Link: https://lore.kernel.org/bpf/20250317174039.161275-2-mykyta.yatsenko5@gmail.com
1 parent 812f770 commit 0de445d

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

include/uapi/linux/bpf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1652,6 +1652,7 @@ union bpf_attr {
16521652
};
16531653
__u32 next_id;
16541654
__u32 open_flags;
1655+
__s32 fd_by_id_token_fd;
16551656
};
16561657

16571658
struct { /* anonymous struct used by BPF_OBJ_GET_INFO_BY_FD */

kernel/bpf/syscall.c

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5120,15 +5120,34 @@ static int bpf_btf_load(const union bpf_attr *attr, bpfptr_t uattr, __u32 uattr_
51205120
return btf_new_fd(attr, uattr, uattr_size);
51215121
}
51225122

5123-
#define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id
5123+
#define BPF_BTF_GET_FD_BY_ID_LAST_FIELD fd_by_id_token_fd
51245124

51255125
static int bpf_btf_get_fd_by_id(const union bpf_attr *attr)
51265126
{
5127+
struct bpf_token *token = NULL;
5128+
51275129
if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID))
51285130
return -EINVAL;
51295131

5130-
if (!capable(CAP_SYS_ADMIN))
5132+
if (attr->open_flags & ~BPF_F_TOKEN_FD)
5133+
return -EINVAL;
5134+
5135+
if (attr->open_flags & BPF_F_TOKEN_FD) {
5136+
token = bpf_token_get_from_fd(attr->fd_by_id_token_fd);
5137+
if (IS_ERR(token))
5138+
return PTR_ERR(token);
5139+
if (!bpf_token_allow_cmd(token, BPF_BTF_GET_FD_BY_ID)) {
5140+
bpf_token_put(token);
5141+
token = NULL;
5142+
}
5143+
}
5144+
5145+
if (!bpf_token_capable(token, CAP_SYS_ADMIN)) {
5146+
bpf_token_put(token);
51315147
return -EPERM;
5148+
}
5149+
5150+
bpf_token_put(token);
51325151

51335152
return btf_get_fd_by_id(attr->btf_id);
51345153
}

tools/include/uapi/linux/bpf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1652,6 +1652,7 @@ union bpf_attr {
16521652
};
16531653
__u32 next_id;
16541654
__u32 open_flags;
1655+
__s32 fd_by_id_token_fd;
16551656
};
16561657

16571658
struct { /* anonymous struct used by BPF_OBJ_GET_INFO_BY_FD */

0 commit comments

Comments
 (0)