Skip to content

Commit c467dbc

Browse files
chentao-kernelmehmetb0
authored andcommitted
bpf: Check percpu map value size first
BugLink: https://bugs.launchpad.net/bugs/2097301 [ Upstream commit 1d24478 ] Percpu map is often used, but the map value size limit often ignored, like issue: iovisor/bcc#2519. Actually, percpu map value size is bound by PCPU_MIN_UNIT_SIZE, so we can check the value size whether it exceeds PCPU_MIN_UNIT_SIZE first, like percpu map of local_storage. Maybe the error message seems clearer compared with "cannot allocate memory". Signed-off-by: Jinke Han <jinkehan@didiglobal.com> Signed-off-by: Tao Chen <chen.dylane@gmail.com> Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Acked-by: Jiri Olsa <jolsa@kernel.org> Acked-by: Andrii Nakryiko <andrii@kernel.org> Link: https://lore.kernel.org/bpf/20240910144111.1464912-2-chen.dylane@gmail.com Signed-off-by: Sasha Levin <sashal@kernel.org> Signed-off-by: Manuel Diewald <manuel.diewald@canonical.com> Signed-off-by: Koichiro Den <koichiro.den@canonical.com>
1 parent 4dfbea7 commit c467dbc

File tree

2 files changed

+6
-0
lines changed

2 files changed

+6
-0
lines changed

kernel/bpf/arraymap.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ int array_map_alloc_check(union bpf_attr *attr)
7373
/* avoid overflow on round_up(map->value_size) */
7474
if (attr->value_size > INT_MAX)
7575
return -E2BIG;
76+
/* percpu map value size is bound by PCPU_MIN_UNIT_SIZE */
77+
if (percpu && round_up(attr->value_size, 8) > PCPU_MIN_UNIT_SIZE)
78+
return -E2BIG;
7679

7780
return 0;
7881
}

kernel/bpf/hashtab.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,9 @@ static int htab_map_alloc_check(union bpf_attr *attr)
459459
* kmalloc-able later in htab_map_update_elem()
460460
*/
461461
return -E2BIG;
462+
/* percpu map value size is bound by PCPU_MIN_UNIT_SIZE */
463+
if (percpu && round_up(attr->value_size, 8) > PCPU_MIN_UNIT_SIZE)
464+
return -E2BIG;
462465

463466
return 0;
464467
}

0 commit comments

Comments
 (0)