mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] bpf: cgroup: fix null-ptr-deref in __cgroup_bpf_attach
@ 2026-09-02  9:02 Liu Jing
  2026-09-02  9:41 ` Jiayuan Chen
  2026-09-02  9:52 ` bot+bpf-ci
  0 siblings, 2 replies; 4+ messages in thread
From: Liu Jing @ 2026-09-02  9:02 UTC (permalink / raw)
  To: ast
  Cc: daniel, andrii, eddyz87, memxor, martin.lau, song, yonghong.song,
	jolsa, emil, ihor.solodrai, bpf, linux-kernel, Liu Jing

In __cgroup_bpf_attach(), the expression "prog ? : link->link.prog"
dereferences link before validating that link is non-NULL. If both
prog and link are NULL (a contract violation by the caller), this
causes a null pointer dereference.

Add a safe ternary fallback and a NULL check for new_prog before
it is used further.

Signed-off-by: Liu Jing <liujing@cmss.chinamobile.com>
---
 kernel/bpf/cgroup.c | 4 +++-
 1 file changed, 3 insertion(+), 1 deletion(-)

--- a/kernel/bpf/cgroup.c
+++ b/kernel/bpf/cgroup.c
@@ -814,7 +814,7 @@
 	struct bpf_cgroup_storage *storage[MAX_BPF_CGROUP_STORAGE_TYPE] = {};
 	struct bpf_cgroup_storage *new_storage[MAX_BPF_CGROUP_STORAGE_TYPE] = {};
 	struct bpf_cgroup_storage *old_storage[MAX_BPF_CGROUP_STORAGE_TYPE] = {};
-	struct bpf_prog *new_prog = prog ? : link->link.prog;
+	struct bpf_prog *new_prog = prog ? : (link ? link->link.prog : NULL);
 	enum cgroup_bpf_attach_type atype;
 	u32 old_flags, old_pl_flags;
 	struct bpf_prog_list *pl;
@@ -833,6 +833,8 @@
 		return -EINVAL;
 	if (!!replace_prog != !!(flags & BPF_F_REPLACE))
 		/* replace_prog implies BPF_F_REPLACE, and vice versa */
+		return -EINVAL;
+	if (!new_prog)
 		return -EINVAL;
 
 	atype = bpf_cgroup_atype_find(type, new_prog->aux->attach_btf_id);

--
2.43.0



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-09-08 16:00 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-09-02  9:02 [PATCH] bpf: cgroup: fix null-ptr-deref in __cgroup_bpf_attach Liu Jing
2026-09-02  9:41 ` Jiayuan Chen
2026-09-08 16:00   ` Emil Tsalapatis
2026-09-02  9:52 ` bot+bpf-ci

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®