Skip to content

Commit 5c6f258

Browse files
gregkhborkmann
authored andcommitted
bpf: Explicitly memset some bpf info structures declared on the stack
Trying to initialize a structure with "= {};" will not always clean out all padding locations in a structure. So be explicit and call memset to initialize everything for a number of bpf information structures that are then copied from userspace, sometimes from smaller memory locations than the size of the structure. Reported-by: Daniel Borkmann <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Acked-by: Yonghong Song <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 8096f22 commit 5c6f258

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

kernel/bpf/btf.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4564,7 +4564,7 @@ int btf_get_info_by_fd(const struct btf *btf,
45644564
union bpf_attr __user *uattr)
45654565
{
45664566
struct bpf_btf_info __user *uinfo;
4567-
struct bpf_btf_info info = {};
4567+
struct bpf_btf_info info;
45684568
u32 info_copy, btf_copy;
45694569
void __user *ubtf;
45704570
u32 uinfo_len;
@@ -4573,6 +4573,7 @@ int btf_get_info_by_fd(const struct btf *btf,
45734573
uinfo_len = attr->info.info_len;
45744574

45754575
info_copy = min_t(u32, uinfo_len, sizeof(info));
4576+
memset(&info, 0, sizeof(info));
45764577
if (copy_from_user(&info, uinfo, info_copy))
45774578
return -EFAULT;
45784579

kernel/bpf/syscall.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2795,7 +2795,7 @@ static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
27952795
union bpf_attr __user *uattr)
27962796
{
27972797
struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
2798-
struct bpf_prog_info info = {};
2798+
struct bpf_prog_info info;
27992799
u32 info_len = attr->info.info_len;
28002800
struct bpf_prog_stats stats;
28012801
char __user *uinsns;
@@ -2807,6 +2807,7 @@ static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
28072807
return err;
28082808
info_len = min_t(u32, sizeof(info), info_len);
28092809

2810+
memset(&info, 0, sizeof(info));
28102811
if (copy_from_user(&info, uinfo, info_len))
28112812
return -EFAULT;
28122813

@@ -3070,7 +3071,7 @@ static int bpf_map_get_info_by_fd(struct bpf_map *map,
30703071
union bpf_attr __user *uattr)
30713072
{
30723073
struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3073-
struct bpf_map_info info = {};
3074+
struct bpf_map_info info;
30743075
u32 info_len = attr->info.info_len;
30753076
int err;
30763077

@@ -3079,6 +3080,7 @@ static int bpf_map_get_info_by_fd(struct bpf_map *map,
30793080
return err;
30803081
info_len = min_t(u32, sizeof(info), info_len);
30813082

3083+
memset(&info, 0, sizeof(info));
30823084
info.type = map->map_type;
30833085
info.id = map->id;
30843086
info.key_size = map->key_size;

0 commit comments

Comments
 (0)