Skip to content

Commit 19d18fd

Browse files
Tao ChenAlexei Starovoitov
authored andcommitted
bpf: Add struct bpf_token_info
The 'commit 35f96de ("bpf: Introduce BPF token object")' added BPF token as a new kind of BPF kernel object. And BPF_OBJ_GET_INFO_BY_FD already used to get BPF object info, so we can also get token info with this cmd. One usage scenario, when program runs failed with token, because of the permission failure, we can report what BPF token is allowing with this API for debugging. Acked-by: Andrii Nakryiko <[email protected]> Signed-off-by: Tao Chen <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 8080500 commit 19d18fd

File tree

5 files changed

+69
-1
lines changed

5 files changed

+69
-1
lines changed

include/linux/bpf.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2354,6 +2354,7 @@ extern const struct super_operations bpf_super_ops;
23542354
extern const struct file_operations bpf_map_fops;
23552355
extern const struct file_operations bpf_prog_fops;
23562356
extern const struct file_operations bpf_iter_fops;
2357+
extern const struct file_operations bpf_token_fops;
23572358

23582359
#define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
23592360
extern const struct bpf_prog_ops _name ## _prog_ops; \
@@ -2551,6 +2552,9 @@ void bpf_token_inc(struct bpf_token *token);
25512552
void bpf_token_put(struct bpf_token *token);
25522553
int bpf_token_create(union bpf_attr *attr);
25532554
struct bpf_token *bpf_token_get_from_fd(u32 ufd);
2555+
int bpf_token_get_info_by_fd(struct bpf_token *token,
2556+
const union bpf_attr *attr,
2557+
union bpf_attr __user *uattr);
25542558

25552559
bool bpf_token_allow_cmd(const struct bpf_token *token, enum bpf_cmd cmd);
25562560
bool bpf_token_allow_map_type(const struct bpf_token *token, enum bpf_map_type type);
@@ -2949,6 +2953,13 @@ static inline struct bpf_token *bpf_token_get_from_fd(u32 ufd)
29492953
return ERR_PTR(-EOPNOTSUPP);
29502954
}
29512955

2956+
static inline int bpf_token_get_info_by_fd(struct bpf_token *token,
2957+
const union bpf_attr *attr,
2958+
union bpf_attr __user *uattr)
2959+
{
2960+
return -EOPNOTSUPP;
2961+
}
2962+
29522963
static inline void __dev_flush(struct list_head *flush_list)
29532964
{
29542965
}

include/uapi/linux/bpf.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,7 @@ union bpf_iter_link_info {
450450
* * **struct bpf_map_info**
451451
* * **struct bpf_btf_info**
452452
* * **struct bpf_link_info**
453+
* * **struct bpf_token_info**
453454
*
454455
* Return
455456
* Returns zero on success. On error, -1 is returned and *errno*
@@ -6803,6 +6804,13 @@ struct bpf_link_info {
68036804
};
68046805
} __attribute__((aligned(8)));
68056806

6807+
struct bpf_token_info {
6808+
__u64 allowed_cmds;
6809+
__u64 allowed_maps;
6810+
__u64 allowed_progs;
6811+
__u64 allowed_attachs;
6812+
} __attribute__((aligned(8)));
6813+
68066814
/* User bpf_sock_addr struct to access socket fields and sockaddr struct passed
68076815
* by user and intended to be used by socket (e.g. to bind to, depends on
68086816
* attach type).

kernel/bpf/syscall.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5239,6 +5239,21 @@ static int bpf_link_get_info_by_fd(struct file *file,
52395239
}
52405240

52415241

5242+
static int token_get_info_by_fd(struct file *file,
5243+
struct bpf_token *token,
5244+
const union bpf_attr *attr,
5245+
union bpf_attr __user *uattr)
5246+
{
5247+
struct bpf_token_info __user *uinfo = u64_to_user_ptr(attr->info.info);
5248+
u32 info_len = attr->info.info_len;
5249+
int err;
5250+
5251+
err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(*uinfo), info_len);
5252+
if (err)
5253+
return err;
5254+
return bpf_token_get_info_by_fd(token, attr, uattr);
5255+
}
5256+
52425257
#define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
52435258

52445259
static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
@@ -5262,6 +5277,9 @@ static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
52625277
else if (fd_file(f)->f_op == &bpf_link_fops || fd_file(f)->f_op == &bpf_link_fops_poll)
52635278
return bpf_link_get_info_by_fd(fd_file(f), fd_file(f)->private_data,
52645279
attr, uattr);
5280+
else if (fd_file(f)->f_op == &bpf_token_fops)
5281+
return token_get_info_by_fd(fd_file(f), fd_file(f)->private_data,
5282+
attr, uattr);
52655283
return -EINVAL;
52665284
}
52675285

kernel/bpf/token.c

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ static void bpf_token_show_fdinfo(struct seq_file *m, struct file *filp)
103103

104104
static const struct inode_operations bpf_token_iops = { };
105105

106-
static const struct file_operations bpf_token_fops = {
106+
const struct file_operations bpf_token_fops = {
107107
.release = bpf_token_release,
108108
.show_fdinfo = bpf_token_show_fdinfo,
109109
};
@@ -210,6 +210,29 @@ int bpf_token_create(union bpf_attr *attr)
210210
return err;
211211
}
212212

213+
int bpf_token_get_info_by_fd(struct bpf_token *token,
214+
const union bpf_attr *attr,
215+
union bpf_attr __user *uattr)
216+
{
217+
struct bpf_token_info __user *uinfo = u64_to_user_ptr(attr->info.info);
218+
struct bpf_token_info info;
219+
u32 info_len = attr->info.info_len;
220+
221+
info_len = min_t(u32, info_len, sizeof(info));
222+
memset(&info, 0, sizeof(info));
223+
224+
info.allowed_cmds = token->allowed_cmds;
225+
info.allowed_maps = token->allowed_maps;
226+
info.allowed_progs = token->allowed_progs;
227+
info.allowed_attachs = token->allowed_attachs;
228+
229+
if (copy_to_user(uinfo, &info, info_len) ||
230+
put_user(info_len, &uattr->info.info_len))
231+
return -EFAULT;
232+
233+
return 0;
234+
}
235+
213236
struct bpf_token *bpf_token_get_from_fd(u32 ufd)
214237
{
215238
CLASS(fd, f)(ufd);

tools/include/uapi/linux/bpf.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,7 @@ union bpf_iter_link_info {
450450
* * **struct bpf_map_info**
451451
* * **struct bpf_btf_info**
452452
* * **struct bpf_link_info**
453+
* * **struct bpf_token_info**
453454
*
454455
* Return
455456
* Returns zero on success. On error, -1 is returned and *errno*
@@ -6803,6 +6804,13 @@ struct bpf_link_info {
68036804
};
68046805
} __attribute__((aligned(8)));
68056806

6807+
struct bpf_token_info {
6808+
__u64 allowed_cmds;
6809+
__u64 allowed_maps;
6810+
__u64 allowed_progs;
6811+
__u64 allowed_attachs;
6812+
} __attribute__((aligned(8)));
6813+
68066814
/* User bpf_sock_addr struct to access socket fields and sockaddr struct passed
68076815
* by user and intended to be used by socket (e.g. to bind to, depends on
68086816
* attach type).

0 commit comments

Comments
 (0)