Given a fd representing a bpfdump target, user can retrieve the target_proto name which represents the bpf program prototype. Given a fd representing a file dumper, user can retrieve the bpf_prog id associated with that dumper. Signed-off-by: Yonghong Song <yhs@xxxxxx> --- include/linux/bpf.h | 2 + include/uapi/linux/bpf.h | 11 +++++- kernel/bpf/dump.c | 72 ++++++++++++++++++++++++++++++++++ kernel/bpf/syscall.c | 2 +- tools/include/uapi/linux/bpf.h | 11 +++++- 5 files changed, 95 insertions(+), 3 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 401e5bf921a2..a1ae8509e735 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -1138,6 +1138,8 @@ int bpf_prog_dump_create(struct bpf_prog *prog); struct bpf_prog *bpf_dump_get_prog(struct seq_file *seq, u32 priv_data_size, u64 *session_id, u64 *seq_num, bool is_last); int bpf_dump_run_prog(struct bpf_prog *prog, void *ctx); +int bpf_dump_query(const union bpf_attr *attr, union bpf_attr __user *uattr); + int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value); int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value); int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value, diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 75f3657d526c..856e3f8a63b8 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -533,7 +533,10 @@ union bpf_attr { }; struct { /* anonymous struct used by BPF_OBJ_GET_INFO_BY_FD */ - __u32 bpf_fd; + union { + __u32 bpf_fd; + __u32 dump_query_fd; + }; __u32 info_len; __aligned_u64 info; } info; @@ -3618,6 +3621,12 @@ struct bpf_btf_info { __u32 id; } __attribute__((aligned(8))); +struct bpf_dump_info { + __aligned_u64 prog_ctx_type_name; + __u32 type_name_buf_len; + __u32 prog_id; +} __attribute__((aligned(8))); + /* User bpf_sock_addr struct to access socket fields and sockaddr struct passed * by user and intended to be used by socket (e.g. to bind to, depends on * attach attach type). diff --git a/kernel/bpf/dump.c b/kernel/bpf/dump.c index 789b35772a81..643591bf5aea 100644 --- a/kernel/bpf/dump.c +++ b/kernel/bpf/dump.c @@ -93,6 +93,78 @@ static void *get_extra_priv_dptr(void *old_ptr, u32 old_size) return old_ptr + roundup(old_size, 8); } +int bpf_dump_query(const union bpf_attr *attr, union bpf_attr __user *uattr) +{ + struct bpf_dump_info __user *ubpf_dinfo; + struct bpfdump_target_info *tinfo; + struct dumper_inode_info *i_info; + struct bpf_dump_info bpf_dinfo; + const char *prog_ctx_type_name; + void * __user tname_buf; + u32 tname_len, info_len; + struct file *filp; + struct fd qfd; + int err = 0; + + qfd = fdget(attr->info.dump_query_fd); + filp = qfd.file; + if (!filp) + return -EBADF; + + if (filp->f_op != &bpf_dumper_ops && + filp->f_inode->i_op != &bpfdump_dir_iops) { + err = -EINVAL; + goto done; + } + + info_len = attr->info.info_len; + ubpf_dinfo = u64_to_user_ptr(attr->info.info); + err = bpf_check_uarg_tail_zero(ubpf_dinfo, sizeof(bpf_dinfo), + info_len); + if (err) + goto done; + info_len = min_t(u32, sizeof(bpf_dinfo), info_len); + + memset(&bpf_dinfo, 0, sizeof(bpf_dinfo)); + if (copy_from_user(&bpf_dinfo, ubpf_dinfo, info_len)) { + err = -EFAULT; + goto done; + } + + /* copy prog_id for dumpers */ + if (filp->f_op == &bpf_dumper_ops) { + i_info = filp->f_inode->i_private; + bpf_dinfo.prog_id = i_info->prog->aux->id; + tinfo = i_info->tinfo; + } else { + tinfo = filp->f_inode->i_private; + } + + prog_ctx_type_name = tinfo->prog_ctx_type_name; + + tname_len = strlen(prog_ctx_type_name) + 1; + if (bpf_dinfo.type_name_buf_len < tname_len) { + err = -ENOSPC; + goto done; + } + + /* copy prog_ctx_type_name */ + tname_buf = u64_to_user_ptr(bpf_dinfo.prog_ctx_type_name); + if (copy_to_user(tname_buf, prog_ctx_type_name, tname_len)) { + err = -EFAULT; + goto done; + } + + /* copy potentially updated bpf_dinfo and info_len */ + if (copy_to_user(ubpf_dinfo, &bpf_dinfo, info_len) || + put_user(info_len, &uattr->info.info_len)) + return -EFAULT; + +done: + fdput(qfd); + return err; +} + #ifdef CONFIG_PROC_FS static void dumper_show_fdinfo(struct seq_file *m, struct file *filp) { diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index e6a4514435c4..1cde78e53a17 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -3358,7 +3358,7 @@ static int bpf_obj_get_info_by_fd(const union bpf_attr *attr, else if (f.file->f_op == &btf_fops) err = bpf_btf_get_info_by_fd(f.file->private_data, attr, uattr); else - err = -EINVAL; + err = bpf_dump_query(attr, uattr); fdput(f); return err; diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 75f3657d526c..856e3f8a63b8 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -533,7 +533,10 @@ union bpf_attr { }; struct { /* anonymous struct used by BPF_OBJ_GET_INFO_BY_FD */ - __u32 bpf_fd; + union { + __u32 bpf_fd; + __u32 dump_query_fd; + }; __u32 info_len; __aligned_u64 info; } info; @@ -3618,6 +3621,12 @@ struct bpf_btf_info { __u32 id; } __attribute__((aligned(8))); +struct bpf_dump_info { + __aligned_u64 prog_ctx_type_name; + __u32 type_name_buf_len; + __u32 prog_id; +} __attribute__((aligned(8))); + /* User bpf_sock_addr struct to access socket fields and sockaddr struct passed * by user and intended to be used by socket (e.g. to bind to, depends on * attach attach type). -- 2.24.1