Skip to content

Commit b8a2e3f

Browse files
htejunAlexei Starovoitov
authored andcommitted
cgroup: Make current_cgns_cgroup_dfl() safe to call after exit_task_namespace()
The commit 332ea1f ("bpf: Add bpf_cgroup_from_id() kfunc") added bpf_cgroup_from_id() which calls current_cgns_cgroup_dfl() through cgroup_get_from_id(). However, BPF programs may be attached to a point where current->nsproxy has already been cleared to NULL by exit_task_namespace() and calling bpf_cgroup_from_id() would cause an oops. Just return the system-wide root if nsproxy has been cleared. This allows all cgroups to be looked up after the task passed through exit_task_namespace(), which semantically makes sense. Given that the only way to get this behavior is through BPF programs, it seems safe but let's see what others think. Fixes: 332ea1f ("bpf: Add bpf_cgroup_from_id() kfunc") Signed-off-by: Tejun Heo <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 3c2611b commit b8a2e3f

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

kernel/cgroup/cgroup.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1465,8 +1465,18 @@ static struct cgroup *current_cgns_cgroup_dfl(void)
14651465
{
14661466
struct css_set *cset;
14671467

1468-
cset = current->nsproxy->cgroup_ns->root_cset;
1469-
return __cset_cgroup_from_root(cset, &cgrp_dfl_root);
1468+
if (current->nsproxy) {
1469+
cset = current->nsproxy->cgroup_ns->root_cset;
1470+
return __cset_cgroup_from_root(cset, &cgrp_dfl_root);
1471+
} else {
1472+
/*
1473+
* NOTE: This function may be called from bpf_cgroup_from_id()
1474+
* on a task which has already passed exit_task_namespaces() and
1475+
* nsproxy == NULL. Fall back to cgrp_dfl_root which will make all
1476+
* cgroups visible for lookups.
1477+
*/
1478+
return &cgrp_dfl_root.cgrp;
1479+
}
14701480
}
14711481

14721482
/* look up cgroup associated with given css_set on the specified hierarchy */

0 commit comments

Comments
 (0)