Skip to content

Commit f40cc03

Browse files
bpf: Fix oob access in cgroup local storage
JIRA: https://issues.redhat.com/browse/RHEL-78204 commit abad3d0 Author: Daniel Borkmann <[email protected]> Date: Thu Jul 31 01:47:33 2025 +0200 bpf: Fix oob access in cgroup local storage Lonial reported that an out-of-bounds access in cgroup local storage can be crafted via tail calls. Given two programs each utilizing a cgroup local storage with a different value size, and one program doing a tail call into the other. The verifier will validate each of the indivial programs just fine. However, in the runtime context the bpf_cg_run_ctx holds an bpf_prog_array_item which contains the BPF program as well as any cgroup local storage flavor the program uses. Helpers such as bpf_get_local_storage() pick this up from the runtime context: ctx = container_of(current->bpf_ctx, struct bpf_cg_run_ctx, run_ctx); storage = ctx->prog_item->cgroup_storage[stype]; if (stype == BPF_CGROUP_STORAGE_SHARED) ptr = &READ_ONCE(storage->buf)->data[0]; else ptr = this_cpu_ptr(storage->percpu_buf); For the second program which was called from the originally attached one, this means bpf_get_local_storage() will pick up the former program's map, not its own. With mismatching sizes, this can result in an unintended out-of-bounds access. To fix this issue, we need to extend bpf_map_owner with an array of storage_cookie[] to match on i) the exact maps from the original program if the second program was using bpf_get_local_storage(), or ii) allow the tail call combination if the second program was not using any of the cgroup local storage maps. Fixes: 7d9c342 ("bpf: Make cgroup storages shared between programs on the same cgroup") Reported-by: Lonial Con <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]> Signed-off-by: Jerome Marchand <[email protected]>
1 parent 48d9766 commit f40cc03

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

include/linux/bpf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ struct bpf_map_owner {
283283
enum bpf_prog_type type;
284284
bool jited;
285285
bool xdp_has_frags;
286+
u64 storage_cookie[MAX_BPF_CGROUP_STORAGE_TYPE];
286287
const struct btf_type *attach_func_proto;
287288
};
288289

kernel/bpf/core.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2383,7 +2383,9 @@ static bool __bpf_prog_map_compatible(struct bpf_map *map,
23832383
{
23842384
enum bpf_prog_type prog_type = resolve_prog_type(fp);
23852385
struct bpf_prog_aux *aux = fp->aux;
2386+
enum bpf_cgroup_storage_type i;
23862387
bool ret = false;
2388+
u64 cookie;
23872389

23882390
if (fp->kprobe_override)
23892391
return ret;
@@ -2398,11 +2400,24 @@ static bool __bpf_prog_map_compatible(struct bpf_map *map,
23982400
map->owner->jited = fp->jited;
23992401
map->owner->xdp_has_frags = aux->xdp_has_frags;
24002402
map->owner->attach_func_proto = aux->attach_func_proto;
2403+
for_each_cgroup_storage_type(i) {
2404+
map->owner->storage_cookie[i] =
2405+
aux->cgroup_storage[i] ?
2406+
aux->cgroup_storage[i]->cookie : 0;
2407+
}
24012408
ret = true;
24022409
} else {
24032410
ret = map->owner->type == prog_type &&
24042411
map->owner->jited == fp->jited &&
24052412
map->owner->xdp_has_frags == aux->xdp_has_frags;
2413+
for_each_cgroup_storage_type(i) {
2414+
if (!ret)
2415+
break;
2416+
cookie = aux->cgroup_storage[i] ?
2417+
aux->cgroup_storage[i]->cookie : 0;
2418+
ret = map->owner->storage_cookie[i] == cookie ||
2419+
!cookie;
2420+
}
24062421
if (ret &&
24072422
map->owner->attach_func_proto != aux->attach_func_proto) {
24082423
switch (prog_type) {

0 commit comments

Comments
 (0)