Skip to content

Commit daaf427

Browse files
Alexei Starovoitovdavem330
authored andcommitted
bpf: fix arraymap NULL deref and missing overflow and zero size checks
- fix NULL pointer dereference: kernel/bpf/arraymap.c:41 array_map_alloc() error: potential null dereference 'array'. (kzalloc returns null) kernel/bpf/arraymap.c:41 array_map_alloc() error: we previously assumed 'array' could be null (see line 40) - integer overflow check was missing in arraymap (hashmap checks for overflow via kmalloc_array()) - arraymap can round_up(value_size, 8) to zero. check was missing. - hashmap was missing zero size check as well, since roundup_pow_of_two() can truncate into zero - found a typo in the arraymap comment and unnecessary empty line Fix all of these issues and make both overflow checks explicit U32 in size. Reported-by: kbuild test robot <fengguang.wu@intel.com> Signed-off-by: Alexei Starovoitov <ast@plumgrid.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent fcd4d35 commit daaf427

2 files changed

Lines changed: 16 additions & 6 deletions

File tree

kernel/bpf/arraymap.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ struct bpf_array {
2525
static struct bpf_map *array_map_alloc(union bpf_attr *attr)
2626
{
2727
struct bpf_array *array;
28-
u32 elem_size;
28+
u32 elem_size, array_size;
2929

3030
/* check sanity of attributes */
3131
if (attr->max_entries == 0 || attr->key_size != 4 ||
@@ -34,11 +34,17 @@ static struct bpf_map *array_map_alloc(union bpf_attr *attr)
3434

3535
elem_size = round_up(attr->value_size, 8);
3636

37+
/* check round_up into zero and u32 overflow */
38+
if (elem_size == 0 ||
39+
attr->max_entries > (U32_MAX - sizeof(*array)) / elem_size)
40+
return ERR_PTR(-ENOMEM);
41+
42+
array_size = sizeof(*array) + attr->max_entries * elem_size;
43+
3744
/* allocate all map elements and zero-initialize them */
38-
array = kzalloc(sizeof(*array) + attr->max_entries * elem_size,
39-
GFP_USER | __GFP_NOWARN);
45+
array = kzalloc(array_size, GFP_USER | __GFP_NOWARN);
4046
if (!array) {
41-
array = vzalloc(array->map.max_entries * array->elem_size);
47+
array = vzalloc(array_size);
4248
if (!array)
4349
return ERR_PTR(-ENOMEM);
4450
}
@@ -51,7 +57,6 @@ static struct bpf_map *array_map_alloc(union bpf_attr *attr)
5157
array->elem_size = elem_size;
5258

5359
return &array->map;
54-
5560
}
5661

5762
/* Called from syscall or from eBPF program */
@@ -101,7 +106,7 @@ static int array_map_update_elem(struct bpf_map *map, void *key, void *value,
101106
return -E2BIG;
102107

103108
if (map_flags == BPF_NOEXIST)
104-
/* all elemenets already exist */
109+
/* all elements already exist */
105110
return -EEXIST;
106111

107112
memcpy(array->value + array->elem_size * index, value, array->elem_size);

kernel/bpf/hashtab.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ static struct bpf_map *htab_map_alloc(union bpf_attr *attr)
6565
goto free_htab;
6666

6767
err = -ENOMEM;
68+
/* prevent zero size kmalloc and check for u32 overflow */
69+
if (htab->n_buckets == 0 ||
70+
htab->n_buckets > U32_MAX / sizeof(struct hlist_head))
71+
goto free_htab;
72+
6873
htab->buckets = kmalloc_array(htab->n_buckets, sizeof(struct hlist_head),
6974
GFP_USER | __GFP_NOWARN);
7075

0 commit comments

Comments
 (0)