Skip to content

Commit 41688d1

Browse files
borkmanngregkh
authored andcommitted
bpf: Fix oob access in cgroup local storage
[ Upstream commit abad3d0 ] 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 <kongln9170@gmail.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Link: https://lore.kernel.org/r/20250730234733.530041-4-daniel@iogearbox.net Signed-off-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent f1f241e commit 41688d1

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
@@ -279,6 +279,7 @@ struct bpf_map_owner {
279279
enum bpf_prog_type type;
280280
bool jited;
281281
bool xdp_has_frags;
282+
u64 storage_cookie[MAX_BPF_CGROUP_STORAGE_TYPE];
282283
const struct btf_type *attach_func_proto;
283284
};
284285

kernel/bpf/core.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2311,7 +2311,9 @@ static bool __bpf_prog_map_compatible(struct bpf_map *map,
23112311
{
23122312
enum bpf_prog_type prog_type = resolve_prog_type(fp);
23132313
struct bpf_prog_aux *aux = fp->aux;
2314+
enum bpf_cgroup_storage_type i;
23142315
bool ret = false;
2316+
u64 cookie;
23152317

23162318
if (fp->kprobe_override)
23172319
return ret;
@@ -2326,11 +2328,24 @@ static bool __bpf_prog_map_compatible(struct bpf_map *map,
23262328
map->owner->jited = fp->jited;
23272329
map->owner->xdp_has_frags = aux->xdp_has_frags;
23282330
map->owner->attach_func_proto = aux->attach_func_proto;
2331+
for_each_cgroup_storage_type(i) {
2332+
map->owner->storage_cookie[i] =
2333+
aux->cgroup_storage[i] ?
2334+
aux->cgroup_storage[i]->cookie : 0;
2335+
}
23292336
ret = true;
23302337
} else {
23312338
ret = map->owner->type == prog_type &&
23322339
map->owner->jited == fp->jited &&
23332340
map->owner->xdp_has_frags == aux->xdp_has_frags;
2341+
for_each_cgroup_storage_type(i) {
2342+
if (!ret)
2343+
break;
2344+
cookie = aux->cgroup_storage[i] ?
2345+
aux->cgroup_storage[i]->cookie : 0;
2346+
ret = map->owner->storage_cookie[i] == cookie ||
2347+
!cookie;
2348+
}
23342349
if (ret &&
23352350
map->owner->attach_func_proto != aux->attach_func_proto) {
23362351
switch (prog_type) {

0 commit comments

Comments
 (0)