mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] bpf: netns: require CAP_NET_ADMIN for link targets
@ 2026-11-02 20:29 Jérémy Jean
  2026-11-02 20:53 ` bot+bpf-ci
  2026-11-02 22:08 ` Alexei Starovoitov
  0 siblings, 2 replies; 4+ messages in thread
From: Jérémy Jean @ 2026-11-02 20:29 UTC (permalink / raw)
  To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	Eduard Zingerman, Kumar Kartikeya Dwivedi
  Cc: bpf, linux-kernel, Jérémy Jean, stable

BPF tokens allow a process to load BPF programs using CAP_NET_ADMIN in
a userns, however, netns_bpf_link_create() does not check for
CAP_NET_ADMIN in the target netns. Consequently, a token can be used
to attach a BPF program to a netns it does not control.

Require CAP_NET_ADMIN in the userns that owns the target netns for
netns BPF link creation.

Fixes: caf8f28e036c ("bpf: Add BPF token support to BPF_PROG_LOAD command")
Cc: stable@vger.kernel.org
Signed-off-by: Jérémy Jean <Jeremy.Jean@oss.cyber.gouv.fr>
---
 kernel/bpf/net_namespace.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/kernel/bpf/net_namespace.c b/kernel/bpf/net_namespace.c
index 81006a242618..0ca29c20ba85 100644
--- a/kernel/bpf/net_namespace.c
+++ b/kernel/bpf/net_namespace.c
@@ -2,6 +2,7 @@
 
 #include <linux/bpf.h>
 #include <linux/bpf-netns.h>
+#include <linux/capability.h>
 #include <linux/filter.h>
 #include <net/net_namespace.h>
 
@@ -488,6 +489,10 @@ int netns_bpf_link_create(const union bpf_attr *attr, struct bpf_prog *prog)
 	net = get_net_ns_by_fd(attr->link_create.target_fd);
 	if (IS_ERR(net))
 		return PTR_ERR(net);
+	if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) {
+		err = -EPERM;
+		goto out_put_net;
+	}
 
 	net_link = kzalloc_obj(*net_link, GFP_USER);
 	if (!net_link) {
-- 
2.47.3


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

end of thread, other threads:[~2026-11-02 22:35 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-02 20:29 [PATCH] bpf: netns: require CAP_NET_ADMIN for link targets Jérémy Jean
2026-11-02 20:53 ` bot+bpf-ci
2026-11-02 22:08 ` Alexei Starovoitov
2026-11-02 22:35   ` Jérémy Jean

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®