[PATCH bpf-next v4 2/3] bpf: use copy_struct_from_user() in bpf_prog_get_info_by_fd()
From: Christian Brauner
Date: Wed Oct 16 2019 - 07:19:25 EST
In v5.4-rc2 we added a new helper (cf. [1]) copy_struct_from_user().
This helper is intended for all codepaths that copy structs from
userspace that are versioned by size. bpf_prog_get_info_by_fd() does
exactly what copy_struct_from_user() is doing.
Note that copy_struct_from_user() is calling min() already. So
technically, the min_t() call could go. But the info_len is used further
below so leave it.
[1]: f5a1a536fa14 ("lib: introduce copy_struct_from_user() helper")
Cc: Alexei Starovoitov <ast@xxxxxxxxxx>
Cc: Daniel Borkmann <daniel@xxxxxxxxxxxxx>
Cc: Aleksa Sarai <cyphar@xxxxxxxxxx>
Cc: bpf@xxxxxxxxxxxxxxx
Signed-off-by: Christian Brauner <christian.brauner@xxxxxxxxxx>
---
/* v1 */
Link: https://lore.kernel.org/r/20191009160907.10981-3-christian.brauner@xxxxxxxxxx
/* v2 */
Link: https://lore.kernel.org/r/20191016004138.24845-3-christian.brauner@xxxxxxxxxx
- Alexei Starovoitov <ast@xxxxxxxxxx>:
- remove unneeded initialization
/* v3 */
Link: https://lore.kernel.org/r/20191016034432.4418-3-christian.brauner@xxxxxxxxxx
unchanged
/* v4 */
- Alexei Starovoitov <ast@xxxxxxxxxx>:
- move misplaced min after copy_struct_from_user()
---
kernel/bpf/syscall.c | 7 ++-----
1 file changed, 2 insertions(+), 5 deletions(-)
diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index 8d112bc069c0..d554ca7671b6 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -2304,21 +2304,18 @@ static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
union bpf_attr __user *uattr)
{
struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
- struct bpf_prog_info info = {};
+ struct bpf_prog_info info;
u32 info_len = attr->info.info_len;
struct bpf_prog_stats stats;
char __user *uinsns;
u32 ulen;
int err;
- err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
+ err = copy_struct_from_user(&info, sizeof(info), uinfo, info_len);
if (err)
return err;
info_len = min_t(u32, sizeof(info), info_len);
- if (copy_from_user(&info, uinfo, info_len))
- return -EFAULT;
-
info.type = prog->type;
info.id = prog->aux->id;
info.load_time = prog->aux->load_time;
--
2.23.0