union bpf_attr member info.info is a __user *ptr to either: struct bpf_btf_info struct bpf_prog_info struct bpf_map_info struct bpf_link_info
These info structs are passed in from userspace and used to store information returned from calls to BPF_OBJ_GET_INFO_BY_FD.
While inbound conversion for all other members of union bpf_attr is handled upfront, this same approach is not possible here for two main reasons:
1. Due to the extra complexity of a further userspace struct that is one of 4 types. These types themselves can be a union/multiplex of even more additional options and types. It is not straight forward or possible in many cases to determine which elements of the union are active upfront, and each has different native/compat64 layouts/offsets.
2. Due to the nature of this subcommand, the main purpose is to copy information back out to userspace, there is more work to do to convert the struct back out to compat64 compatible offsets compared to other subcommands.
Instead of an upfront conversion, convert where each is used at the end site both in/out.
Signed-off-by: Zachary Leaf zachary.leaf@arm.com --- include/linux/bpf_compat.h | 42 ++++++++++ kernel/bpf/syscall.c | 154 ++++++++++++++++++++++++++++++++++--- 2 files changed, 186 insertions(+), 10 deletions(-)
diff --git a/include/linux/bpf_compat.h b/include/linux/bpf_compat.h index 6882322cefc2..34aa707b021e 100644 --- a/include/linux/bpf_compat.h +++ b/include/linux/bpf_compat.h @@ -287,6 +287,48 @@ union compat_bpf_attr {
} __attribute__((aligned(8)));
+struct compat_bpf_prog_info { + __u32 type; + __u32 id; + __u8 tag[BPF_TAG_SIZE]; + __u32 jited_prog_len; + __u32 xlated_prog_len; + __aligned_u64 jited_prog_insns; + __aligned_u64 xlated_prog_insns; + __u64 load_time; /* ns since boottime */ + __u32 created_by_uid; + __u32 nr_map_ids; + __aligned_u64 map_ids; + char name[BPF_OBJ_NAME_LEN]; + __u32 ifindex; + __u32 gpl_compatible:1; + __u32:31; /* alignment pad */ + __u64 netns_dev; + __u64 netns_ino; + __u32 nr_jited_ksyms; + __u32 nr_jited_func_lens; + __aligned_u64 jited_ksyms; + __aligned_u64 jited_func_lens; + __u32 btf_id; + __u32 func_info_rec_size; + __aligned_u64 func_info; + __u32 nr_func_info; + __u32 nr_line_info; + __aligned_u64 line_info; + __aligned_u64 jited_line_info; + __u32 nr_jited_line_info; + __u32 line_info_rec_size; + __u32 jited_line_info_rec_size; + __u32 nr_prog_tags; + __aligned_u64 prog_tags; + __u64 run_time_ns; + __u64 run_cnt; + __u64 recursion_misses; + __u32 verified_insns; + __u32 attach_btf_obj_id; + __u32 attach_btf_id; +} __attribute__((aligned(8))); + struct compat_bpf_btf_info { __aligned_u64 btf; __u32 btf_size; diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index d06700136e1f..929b515c56ae 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -3891,28 +3891,152 @@ static int set_info_rec_size(struct bpf_prog_info *info) return 0; }
+#ifdef CONFIG_COMPAT64 +void convert_compat_prog_info_in(struct bpf_prog_info *dest, + struct compat_bpf_prog_info *cinfo) +{ + dest->type = cinfo->type; + dest->id = cinfo->id; + strncpy((char *)dest->tag, (char *)cinfo->tag, BPF_TAG_SIZE); + dest->jited_prog_len = cinfo->jited_prog_len; + dest->xlated_prog_len = cinfo->xlated_prog_len; + dest->jited_prog_insns = cinfo->jited_prog_insns; + dest->xlated_prog_insns = cinfo->xlated_prog_insns; + dest->load_time = cinfo->load_time; + dest->created_by_uid = cinfo->created_by_uid; + dest->nr_map_ids = cinfo->nr_map_ids; + dest->map_ids = cinfo->map_ids; + strncpy((char *)dest->name, (char *)cinfo->name, BPF_OBJ_NAME_LEN); + dest->ifindex = cinfo->ifindex; + dest->gpl_compatible = cinfo->gpl_compatible; + dest->netns_dev = cinfo->netns_dev; + dest->netns_ino = cinfo->netns_ino; + dest->nr_jited_ksyms = cinfo->nr_jited_ksyms; + dest->nr_jited_func_lens = cinfo->nr_jited_func_lens; + dest->jited_ksyms = cinfo->jited_ksyms; + dest->jited_func_lens = cinfo->jited_func_lens; + dest->btf_id = cinfo->btf_id; + dest->func_info_rec_size = cinfo->func_info_rec_size; + dest->func_info = cinfo->func_info; + dest->nr_func_info = cinfo->nr_func_info; + dest->nr_line_info = cinfo->nr_line_info; + dest->line_info = cinfo->line_info; + dest->jited_line_info = cinfo->jited_line_info; + dest->nr_jited_line_info = cinfo->nr_jited_line_info; + dest->line_info_rec_size = cinfo->line_info_rec_size; + dest->jited_line_info_rec_size = cinfo->jited_line_info_rec_size; + dest->nr_prog_tags = cinfo->nr_prog_tags; + dest->prog_tags = cinfo->prog_tags; + dest->run_time_ns = cinfo->run_time_ns; + dest->run_cnt = cinfo->run_cnt; + dest->recursion_misses = cinfo->recursion_misses; + dest->verified_insns = cinfo->verified_insns; + dest->attach_btf_obj_id = cinfo->attach_btf_obj_id; + dest->attach_btf_id = cinfo->attach_btf_id; +} + +void convert_compat_prog_info_out(struct compat_bpf_prog_info *dest, + struct bpf_prog_info *info) +{ + dest->type = info->type; + dest->id = info->id; + strncpy((char *)dest->tag, (char *)info->tag, BPF_TAG_SIZE); + dest->jited_prog_len = info->jited_prog_len; + dest->xlated_prog_len = info->xlated_prog_len; + dest->jited_prog_insns = info->jited_prog_insns; + dest->xlated_prog_insns = info->xlated_prog_insns; + dest->load_time = info->load_time; + dest->created_by_uid = info->created_by_uid; + dest->nr_map_ids = info->nr_map_ids; + dest->map_ids = info->map_ids; + strncpy((char *)dest->name, (char *)info->name, BPF_OBJ_NAME_LEN); + dest->ifindex = info->ifindex; + dest->gpl_compatible = info->gpl_compatible; + dest->netns_dev = info->netns_dev; + dest->netns_ino = info->netns_ino; + dest->nr_jited_ksyms = info->nr_jited_ksyms; + dest->nr_jited_func_lens = info->nr_jited_func_lens; + dest->jited_ksyms = info->jited_ksyms; + dest->jited_func_lens = info->jited_func_lens; + dest->btf_id = info->btf_id; + dest->func_info_rec_size = info->func_info_rec_size; + dest->func_info = info->func_info; + dest->nr_func_info = info->nr_func_info; + dest->nr_line_info = info->nr_line_info; + dest->line_info = info->line_info; + dest->jited_line_info = info->jited_line_info; + dest->nr_jited_line_info = info->nr_jited_line_info; + dest->line_info_rec_size = info->line_info_rec_size; + dest->jited_line_info_rec_size = info->jited_line_info_rec_size; + dest->nr_prog_tags = info->nr_prog_tags; + dest->prog_tags = info->prog_tags; + dest->run_time_ns = info->run_time_ns; + dest->run_cnt = info->run_cnt; + dest->recursion_misses = info->recursion_misses; + dest->verified_insns = info->verified_insns; + dest->attach_btf_obj_id = info->attach_btf_obj_id; + dest->attach_btf_id = info->attach_btf_id; +} + +#endif /* CONFIG_COMPAT64 */ + static int bpf_prog_get_info_by_fd(struct file *file, struct bpf_prog *prog, const union bpf_attr *attr, union bpf_attr __user *uattr) { - struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info); + struct bpf_prog_info __user *uinfo; struct btf *attach_btf = bpf_prog_get_target_btf(prog); struct bpf_prog_info info; - u32 info_len = attr->info.info_len; + u32 info_len; struct bpf_prog_kstats stats; char __user *uinsns; u32 ulen; int err; +#ifdef CONFIG_COMPAT64 + struct compat_bpf_prog_info __user *cuinfo; + struct compat_bpf_prog_info cinfo; + union compat_bpf_attr __user *cuattr = + (union compat_bpf_attr __user *)uattr; + u32 cuinfo_len; + /* + * for compat64, uattr has already been converted and copied into + * attr - however attr->info.info is a ptr to uapi struct bpf_prog_info, + * which also needs converting + * + * convert bpf_prog_info coming in, and later reverse this conversion + * when writing back out to userspace + */ + if (in_compat_syscall()) { + cuinfo = (struct compat_bpf_prog_info __user *)attr->info.info; + cuinfo_len = attr->info.info_len;
- err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len); - if (err) - return err; - info_len = min_t(u32, sizeof(info), info_len); + err = bpf_check_uarg_tail_zero(USER_BPFPTR(cuinfo), + sizeof(cinfo), cuinfo_len); + if (err) + return err; + cuinfo_len = min_t(u32, sizeof(cinfo), cuinfo_len);
- memset(&info, 0, sizeof(info)); - if (copy_from_user(&info, uinfo, info_len)) - return -EFAULT; + if (copy_from_user_with_ptr(&cinfo, cuinfo, cuinfo_len)) + return -EFAULT; + memset(&info, 0, sizeof(info)); + convert_compat_prog_info_in(&info, &cinfo); + } else +#endif + { + uinfo = u64_to_user_ptr(attr->info.info); + info_len = attr->info.info_len; + + err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), + sizeof(info), info_len); + if (err) + return err; + info_len = min_t(u32, sizeof(info), info_len); + + memset(&info, 0, sizeof(info)); + if (copy_from_user_with_ptr(&info, uinfo, info_len)) + return -EFAULT; + }
info.type = prog->type; info.id = prog->aux->id; @@ -4173,7 +4297,17 @@ static int bpf_prog_get_info_by_fd(struct file *file, }
done: - if (copy_to_user(uinfo, &info, info_len) || +#ifdef CONFIG_COMPAT64 + if (in_compat_syscall()) { + convert_compat_prog_info_out(&cinfo, &info); + if (copy_to_user_with_ptr(cuinfo, &cinfo, cuinfo_len) || + put_user(cuinfo_len, &cuattr->info.info_len)) + return -EFAULT; + + return 0; + } +#endif + if (copy_to_user_with_ptr(uinfo, &info, info_len) || put_user(info_len, &uattr->info.info_len)) return -EFAULT;