public inbox for bpf@vger.kernel.org
 help / color / mirror / Atom feed
From: Guillaume GONNET <ggonnet.linux@gmail.com>
To: daniel@iogearbox.net
Cc: ast@kernel.org, bpf@vger.kernel.org, ggonnet.linux@gmail.com,
	john.fastabend@gmail.com, martin.lau@linux.dev
Subject: [PATCH] bpf: fix TCX/netkit detach permissions when prog FD isn't given
Date: Mon, 26 Jan 2026 18:34:31 +0100	[thread overview]
Message-ID: <20260126173431.19825-1-ggonnet.linux@gmail.com> (raw)
In-Reply-To: <d269a9b8946d21a9f93724baa6ed06e9873e8c13890902ff09e201a89c727804@mail.kernel.org>

This commit fixes a security issue where BPF_PROG_DETACH on TCX or netkit
devices could be executed by any user when no program FD was provided,
bypassing permission checks. The fix adds a capability check for
CAP_NET_ADMIN or CAP_SYS_ADMIN in this case.

Fixes: e420bed02507 ("bpf: Add fd-based tcx multi-prog infra with link support")
Signed-off-by: Guillaume GONNET <ggonnet.linux@gmail.com>
---
 include/linux/bpf.h       |  5 +++++
 include/linux/bpf_mprog.h | 10 ++++++++++
 kernel/bpf/syscall.c      |  7 ++-----
 3 files changed, 17 insertions(+), 5 deletions(-)

diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index 4427c6e98331..5f59d1f173a2 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -2742,6 +2742,11 @@ static inline bool bpf_bypass_spec_v4(const struct bpf_token *token)
 		bpf_token_capable(token, CAP_PERFMON);
 }
 
+static inline bool bpf_net_capable(void)
+{
+	return capable(CAP_NET_ADMIN) || capable(CAP_SYS_ADMIN);
+}
+
 int bpf_map_new_fd(struct bpf_map *map, int flags);
 int bpf_prog_new_fd(struct bpf_prog *prog);
 
diff --git a/include/linux/bpf_mprog.h b/include/linux/bpf_mprog.h
index 929225f7b095..0b9f4caeeb0a 100644
--- a/include/linux/bpf_mprog.h
+++ b/include/linux/bpf_mprog.h
@@ -340,4 +340,14 @@ static inline bool bpf_mprog_supported(enum bpf_prog_type type)
 		return false;
 	}
 }
+
+static inline bool bpf_mprog_detach_empty(enum bpf_prog_type type)
+{
+	switch (type) {
+	case BPF_PROG_TYPE_SCHED_CLS:
+		return bpf_net_capable();
+	default:
+		return false;
+	}
+}
 #endif /* __BPF_MPROG_H */
diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index b9184545c3fd..5f59dd47a5b1 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -1363,11 +1363,6 @@ static int map_check_btf(struct bpf_map *map, struct bpf_token *token,
 	return ret;
 }
 
-static bool bpf_net_capable(void)
-{
-	return capable(CAP_NET_ADMIN) || capable(CAP_SYS_ADMIN);
-}
-
 #define BPF_MAP_CREATE_LAST_FIELD excl_prog_hash_size
 /* called via syscall */
 static int map_create(union bpf_attr *attr, bpfptr_t uattr)
@@ -4579,6 +4574,8 @@ static int bpf_prog_detach(const union bpf_attr *attr)
 			prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
 			if (IS_ERR(prog))
 				return PTR_ERR(prog);
+		} else if (!bpf_mprog_detach_empty(ptype)) {
+			return -EPERM;
 		}
 	} else if (is_cgroup_prog_type(ptype, 0, false)) {
 		if (attr->attach_flags || attr->relative_fd)
-- 
2.34.1


  reply	other threads:[~2026-01-26 17:34 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-24 21:43 [PATCH] bpf: fix TCX/netkit detach permissions when prog FD isn't given Guillaume GONNET
2026-01-24 22:06 ` bot+bpf-ci
2026-01-26 13:24 ` Daniel Borkmann
2026-01-26 17:03   ` Guillaume GONNET
2026-01-26 17:07   ` [PATCH bpf] " Guillaume GONNET
2026-01-26 17:23     ` bot+bpf-ci
2026-01-26 17:34       ` Guillaume GONNET [this message]
2026-01-26 21:40         ` [PATCH] " patchwork-bot+netdevbpf
2026-01-27  1:56         ` kernel test robot
     [not found]           ` <CAADnVQJNvx34irz6JYbmZvmaYU0AGRDcu8znsKjA_di798RisA@mail.gmail.com>
2026-01-27  2:29             ` Alexei Starovoitov
2026-01-27  8:26               ` [PATCH bpf v4] bpf: Fix tcx/netkit detach permissions when prog fd " Guillaume Gonnet
2026-01-27 13:10                 ` Daniel Borkmann
2026-01-27 16:02                   ` [PATCH bpf v5] " Guillaume Gonnet
2026-01-28  2:50                     ` patchwork-bot+netdevbpf
2026-01-27  3:12         ` [PATCH] bpf: fix TCX/netkit detach permissions when prog FD " kernel test robot
2026-01-26 17:48       ` [PATCH bpf] " Daniel Borkmann

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=20260126173431.19825-1-ggonnet.linux@gmail.com \
    --to=ggonnet.linux@gmail.com \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=john.fastabend@gmail.com \
    --cc=martin.lau@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox