Skip to content

Commit 87ac0d6

Browse files
anakryikoAlexei Starovoitov
authored andcommitted
bpf: fix potential 32-bit overflow when accessing ARRAY map element
If BPF array map is bigger than 4GB, element pointer calculation can overflow because both index and elem_size are u32. Fix this everywhere by forcing 64-bit multiplication. Extract this formula into separate small helper and use it consistently in various places. Speculative-preventing formula utilizing index_mask trick is left as is, but explicit u64 casts are added in both places. Fixes: c85d691 ("bpf: move memory size checks to bpf_map_charge_init()") Signed-off-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent e5e2342 commit 87ac0d6

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

kernel/bpf/arraymap.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,11 @@ static struct bpf_map *array_map_alloc(union bpf_attr *attr)
156156
return &array->map;
157157
}
158158

159+
static void *array_map_elem_ptr(struct bpf_array* array, u32 index)
160+
{
161+
return array->value + (u64)array->elem_size * index;
162+
}
163+
159164
/* Called from syscall or from eBPF program */
160165
static void *array_map_lookup_elem(struct bpf_map *map, void *key)
161166
{
@@ -165,7 +170,7 @@ static void *array_map_lookup_elem(struct bpf_map *map, void *key)
165170
if (unlikely(index >= array->map.max_entries))
166171
return NULL;
167172

168-
return array->value + array->elem_size * (index & array->index_mask);
173+
return array->value + (u64)array->elem_size * (index & array->index_mask);
169174
}
170175

171176
static int array_map_direct_value_addr(const struct bpf_map *map, u64 *imm,
@@ -339,7 +344,7 @@ static int array_map_update_elem(struct bpf_map *map, void *key, void *value,
339344
value, map->value_size);
340345
} else {
341346
val = array->value +
342-
array->elem_size * (index & array->index_mask);
347+
(u64)array->elem_size * (index & array->index_mask);
343348
if (map_flags & BPF_F_LOCK)
344349
copy_map_value_locked(map, val, value, false);
345350
else
@@ -408,8 +413,7 @@ static void array_map_free_timers(struct bpf_map *map)
408413
return;
409414

410415
for (i = 0; i < array->map.max_entries; i++)
411-
bpf_timer_cancel_and_free(array->value + array->elem_size * i +
412-
map->timer_off);
416+
bpf_timer_cancel_and_free(array_map_elem_ptr(array, i) + map->timer_off);
413417
}
414418

415419
/* Called when map->refcnt goes to zero, either from workqueue or from syscall */
@@ -420,7 +424,7 @@ static void array_map_free(struct bpf_map *map)
420424

421425
if (map_value_has_kptrs(map)) {
422426
for (i = 0; i < array->map.max_entries; i++)
423-
bpf_map_free_kptrs(map, array->value + array->elem_size * i);
427+
bpf_map_free_kptrs(map, array_map_elem_ptr(array, i));
424428
bpf_map_free_kptr_off_tab(map);
425429
}
426430

@@ -556,7 +560,7 @@ static void *bpf_array_map_seq_start(struct seq_file *seq, loff_t *pos)
556560
index = info->index & array->index_mask;
557561
if (info->percpu_value_buf)
558562
return array->pptrs[index];
559-
return array->value + array->elem_size * index;
563+
return array_map_elem_ptr(array, index);
560564
}
561565

562566
static void *bpf_array_map_seq_next(struct seq_file *seq, void *v, loff_t *pos)
@@ -575,7 +579,7 @@ static void *bpf_array_map_seq_next(struct seq_file *seq, void *v, loff_t *pos)
575579
index = info->index & array->index_mask;
576580
if (info->percpu_value_buf)
577581
return array->pptrs[index];
578-
return array->value + array->elem_size * index;
582+
return array_map_elem_ptr(array, index);
579583
}
580584

581585
static int __bpf_array_map_seq_show(struct seq_file *seq, void *v)
@@ -690,7 +694,7 @@ static int bpf_for_each_array_elem(struct bpf_map *map, bpf_callback_t callback_
690694
if (is_percpu)
691695
val = this_cpu_ptr(array->pptrs[i]);
692696
else
693-
val = array->value + array->elem_size * i;
697+
val = array_map_elem_ptr(array, i);
694698
num_elems++;
695699
key = i;
696700
ret = callback_fn((u64)(long)map, (u64)(long)&key,

0 commit comments

Comments
 (0)