bpf: Do not limit bpf_cgroup_from_id to current's namespace
authorKumar Kartikeya Dwivedi <memxor@gmail.com>
Mon, 15 Sep 2025 03:26:17 +0000 (03:26 +0000)
committerAlexei Starovoitov <ast@kernel.org>
Mon, 15 Sep 2025 17:53:15 +0000 (10:53 -0700)
commit2c895133950646f45e5cf3900b168c952c8dbee8
tree8257e385bb2ff09dba39bea639f1e5b3e22d57a1
parenta9d4e9f0e871352a48a82da11a50df7196fe567a
bpf: Do not limit bpf_cgroup_from_id to current's namespace

The bpf_cgroup_from_id kfunc relies on cgroup_get_from_id to obtain the
cgroup corresponding to a given cgroup ID. This helper can be called in
a lot of contexts where the current thread can be random. A recent
example was its use in sched_ext's ops.tick(), to obtain the root cgroup
pointer. Since the current task can be whatever random user space task
preempted by the timer tick, this makes the behavior of the helper
unreliable.

Refactor out __cgroup_get_from_id as the non-namespace aware version of
cgroup_get_from_id, and change bpf_cgroup_from_id to make use of it.

There is no compatibility breakage here, since changing the namespace
against which the lookup is being done to the root cgroup namespace only
permits a wider set of lookups to succeed now. The cgroup IDs across
namespaces are globally unique, and thus don't need to be retranslated.

Reported-by: Dan Schatzberg <dschatzberg@meta.com>
Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
Acked-by: Tejun Heo <tj@kernel.org>
Link: https://lore.kernel.org/r/20250915032618.1551762-2-memxor@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
include/linux/cgroup.h
kernel/bpf/helpers.c
kernel/cgroup/cgroup.c