From: Shakeel Butt <shakeel.butt@linux.dev>
To: bpf@vger.kernel.org
Cc: "Alexei Starovoitov" <ast@kernel.org>,
"Daniel Borkmann" <daniel@iogearbox.net>,
"Andrii Nakryiko" <andrii@kernel.org>,
"Martin KaFai Lau" <martin.lau@linux.dev>,
"Eduard Zingerman" <eddyz87@gmail.com>,
"Kumar Kartikeya Dwivedi" <memxor@gmail.com>,
"Song Liu" <song@kernel.org>,
"Yonghong Song" <yonghong.song@linux.dev>,
"John Fastabend" <john.fastabend@gmail.com>,
"Jiri Olsa" <jolsa@kernel.org>,
"Emil Tsalapatis" <emil@etsalapatis.com>,
"Ihor Solodrai" <ihor.solodrai@linux.dev>,
"Roman Gushchin" <roman.gushchin@linux.dev>,
"Amery Hung" <ameryhung@gmail.com>, "Tejun Heo" <tj@kernel.org>,
"Johannes Weiner" <hannes@cmpxchg.org>,
"Michal Koutný" <mkoutny@suse.com>,
"Meta kernel team" <kernel-team@meta.com>,
cgroups@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH bpf-next 2/3] bpf, cgroup: Don't attach to a removed cgroup
Date: Thu, 12 Nov 2026 09:27:14 -0800 [thread overview]
Message-ID: <20261112172721.440275-3-shakeel.butt@linux.dev> (raw)
In-Reply-To: <20261112172721.440275-1-shakeel.butt@linux.dev>
Attach gets the cgroup from an fd before it takes cgroup_mutex. If the
cgroup is removed in between and cgroup_bpf_release() runs first, attach
adds a program that never runs:
- With BPF_PROG_ATTACH the program leaks, since nothing can detach it.
- With a link that uses cgroup storage, the storage stays on the freed
cgroup's list, and freeing the map later writes to the freed cgroup.
- A cgroup struct_ops link is attached but never called.
The window is small today because the release work also waits for
cgroup_mutex. The next patch stops that, which makes the race easy to
hit.
Check cgroup_is_dead() under the lock and return -ENOENT, the error the
fd lookup gives for a removed cgroup. rmdir marks the cgroup dead under
cgroup_mutex before it starts the release, so the check is exact.
Reproduced in a VM with debug-only delays. With the next patch applied,
a link attach queued behind a slow mkdir lands on the released cgroup,
and freeing the map then hits a KASAN use-after-free in
cgroup_storage_map_free(). BPF_PROG_ATTACH leaks the program. With
this patch, attach returns -ENOENT.
Fixes: 4bfc0bb2c60e ("bpf: decouple the lifetime of cgroup_bpf from cgroup itself")
Fixes: 369d9dcd8fb8 ("bpf: Add infrastructure to support attaching struct_ops to cgroups")
Signed-off-by: Shakeel Butt <shakeel.butt@linux.dev>
---
kernel/bpf/cgroup.c | 13 +++++++++++++
1 file changed, 13 insertions(+)
diff --git a/kernel/bpf/cgroup.c b/kernel/bpf/cgroup.c
index 9258c2669fa8..334237096759 100644
--- a/kernel/bpf/cgroup.c
+++ b/kernel/bpf/cgroup.c
@@ -988,6 +988,13 @@ static int __cgroup_bpf_attach(struct cgroup *cgrp,
/* replace_prog implies BPF_F_REPLACE, and vice versa */
return -EINVAL;
+ /*
+ * The cgroup may have been removed since the caller looked it up.
+ * Then its bpf state may be freed, and nothing would detach this.
+ */
+ if (cgroup_is_dead(cgrp))
+ return -ENOENT;
+
atype = bpf_cgroup_atype_find(type, new_prog->aux->attach_btf_id);
if (atype < 0)
return -EINVAL;
@@ -3170,6 +3177,12 @@ int cgroup_bpf_struct_ops_attach(struct bpf_map *map, const union bpf_attr *attr
cgroup_lock();
+ /* See the cgroup_is_dead() check in __cgroup_bpf_attach() */
+ if (cgroup_is_dead(cgrp)) {
+ err = -ENOENT;
+ goto unlock;
+ }
+
if (attr->link_create.cgroup.expected_revision &&
attr->link_create.cgroup.expected_revision != cgrp->bpf.revisions[atype]) {
err = -ESTALE;
--
2.53.0-Meta
next prev parent reply other threads:[~2026-11-12 17:28 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-12 17:27 [PATCH bpf-next 0/3] bpf, cgroup: Don't wait for cgroup_mutex to read or free cgroup bpf state Shakeel Butt
2026-11-12 17:27 ` [PATCH bpf-next 1/3] bpf, cgroup: Don't read freed effective arrays in BPF_PROG_QUERY Shakeel Butt
2026-11-12 17:27 ` Shakeel Butt [this message]
2026-11-12 17:27 ` [PATCH bpf-next 3/3] bpf, cgroup: Protect cgroup_bpf state with its own mutex Shakeel Butt
2026-11-12 17:41 ` sashiko-bot
2026-11-12 18:23 ` Shakeel Butt
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261112172721.440275-3-shakeel.butt@linux.dev \
--to=shakeel.butt@linux.dev \
--cc=ameryhung@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=cgroups@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=emil@etsalapatis.com \
--cc=hannes@cmpxchg.org \
--cc=ihor.solodrai@linux.dev \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=kernel-team@meta.com \
--cc=linux-kernel@vger.kernel.org \
--cc=martin.lau@linux.dev \
--cc=memxor@gmail.com \
--cc=mkoutny@suse.com \
--cc=roman.gushchin@linux.dev \
--cc=song@kernel.org \
--cc=tj@kernel.org \
--cc=yonghong.song@linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.