Skip to content

Commit abad3d0

Browse files
borkmannAlexei Starovoitov
authored andcommitted
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]>
1 parent 9621e60 commit abad3d0

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
@@ -2378,7 +2378,9 @@ static bool __bpf_prog_map_compatible(struct bpf_map *map,
23782378
{
23792379
enum bpf_prog_type prog_type = resolve_prog_type(fp);
23802380
struct bpf_prog_aux *aux = fp->aux;
2381+
enum bpf_cgroup_storage_type i;
23812382
bool ret = false;
2383+
u64 cookie;
23822384

23832385
if (fp->kprobe_override)
23842386
return ret;
@@ -2393,11 +2395,24 @@ static bool __bpf_prog_map_compatible(struct bpf_map *map,
23932395
map->owner->jited = fp->jited;
23942396
map->owner->xdp_has_frags = aux->xdp_has_frags;
23952397
map->owner->attach_func_proto = aux->attach_func_proto;
2398+
for_each_cgroup_storage_type(i) {
2399+
map->owner->storage_cookie[i] =
2400+
aux->cgroup_storage[i] ?
2401+
aux->cgroup_storage[i]->cookie : 0;
2402+
}
23962403
ret = true;
23972404
} else {
23982405
ret = map->owner->type == prog_type &&
23992406
map->owner->jited == fp->jited &&
24002407
map->owner->xdp_has_frags == aux->xdp_has_frags;
2408+
for_each_cgroup_storage_type(i) {
2409+
if (!ret)
2410+
break;
2411+
cookie = aux->cgroup_storage[i] ?
2412+
aux->cgroup_storage[i]->cookie : 0;
2413+
ret = map->owner->storage_cookie[i] == cookie ||
2414+
!cookie;
2415+
}
24012416
if (ret &&
24022417
map->owner->attach_func_proto != aux->attach_func_proto) {
24032418
switch (prog_type) {

0 commit comments

Comments
 (0)