Skip to contentSkip to conversation

Support freplace prog from user namespace

PATCH Superseded Mykyta Yatsenko 2 participants 7 messages Mar 11, 2025 – Mar 12, 2025

Subsystems (5)
Kselftest BPF BPF selftests BPF core +1 more subsystems

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v5 cover Support freplace prog from user namespace

From: Mykyta Yatsenko <[email protected]>

Freplace programs can't be loaded from user namespace, as bpf_program__set_attach_target() requires searching for target prog BTF, which is locked under CAP_SYS_ADMIN.
This patch set enables this use case by:
1. Relaxing capable check in bpf's BPF_BTF_GET_FD_BY_ID, check for CAP_BPF instead of CAP_SYS_ADMIN, support BPF token in attr argument.
2. Pass BPF token around libbpf from bpf_program__set_attach_target() to bpf syscall where capable check is.
3. Validate positive/negative scenarios in selftests

This patch set is enabled by the recent libbpf change[1], that introduced bpf_object__prepare() API. Calling bpf_object__prepare() for freplace program before bpf_program__set_attach_target() initializes BPF token, which is then passed to bpf syscall by libbpf.

[1] https://lore.kernel.org/all/[email protected]/

Mykyta Yatsenko (4):
bpf: BPF token support for BPF_BTF_GET_FD_BY_ID
bpf: return prog btf_id without capable check
libbpf: pass BPF token from find_prog_btf_id to BPF_BTF_GET_FD_BY_ID selftests/bpf: test freplace from user namespace

 include/uapi/linux/bpf.h                      |  1 +
 kernel/bpf/syscall.c                          | 24 ++++-
 tools/include/uapi/linux/bpf.h                |  1 +
 tools/lib/bpf/bpf.c                           |  3 +-
 tools/lib/bpf/bpf.h                           |  3 +-
 tools/lib/bpf/btf.c                           | 15 ++-
 tools/lib/bpf/libbpf.c                        | 10 +-
 tools/lib/bpf/libbpf_internal.h               |  1 +
 .../bpf/prog_tests/libbpf_get_fd_by_id_opts.c |  3 +-
 .../testing/selftests/bpf/prog_tests/token.c  | 97 ++++++++++++++++++-
 .../selftests/bpf/progs/priv_freplace_prog.c  | 13 +++
 tools/testing/selftests/bpf/progs/priv_prog.c |  6 +-
 12 files changed, 158 insertions(+), 19 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/progs/priv_freplace_prog.c
Signature · 1 line
2.48.1
#1 lore raw

PATCH v5 1/4 bpf: BPF token support for BPF_BTF_GET_FD_BY_ID

From: Mykyta Yatsenko <[email protected]>

Currently BPF_BTF_GET_FD_BY_ID requires CAP_SYS_ADMIN, which does not allow running it from user namespace. This creates a problem when freplace program running from user namespace needs to query target program BTF.
This patch relaxes capable check from CAP_SYS_ADMIN to CAP_BPF and adds support for BPF token that can be passed in attributes to syscall.

---
 include/uapi/linux/bpf.h                      |  1 +
 kernel/bpf/syscall.c                          | 20 +++++++++++++++++--
 tools/include/uapi/linux/bpf.h                |  1 +
 .../bpf/prog_tests/libbpf_get_fd_by_id_opts.c |  3 +--
 4 files changed, 21 insertions(+), 4 deletions(-)
include/uapi/linux/bpf.h +1 -0
index bb37897c0393..73c23daacabf 100644
@@ -1652,6 +1652,7 @@ union bpf_attr {
 		};
 		__u32		next_id;
 		__u32		open_flags;
+		__s32		token_fd;
 	};
 
 	struct { /* anonymous struct used by BPF_OBJ_GET_INFO_BY_FD */
kernel/bpf/syscall.c +18 -2
index 57a438706215..188f7296cf9f 100644
@@ -5137,15 +5137,31 @@ static int bpf_btf_load(const union bpf_attr *attr, bpfptr_t uattr, __u32 uattr_
 	return btf_new_fd(attr, uattr, uattr_size);
 }
 
-#define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id
+#define BPF_BTF_GET_FD_BY_ID_LAST_FIELD token_fd
 
 static int bpf_btf_get_fd_by_id(const union bpf_attr *attr)
 {
+	struct bpf_token *token = NULL;
+
 	if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID))
 		return -EINVAL;
 
-	if (!capable(CAP_SYS_ADMIN))
+	if (attr->open_flags & BPF_F_TOKEN_FD) {
+		token = bpf_token_get_from_fd(attr->token_fd);
+		if (IS_ERR(token))
+			return PTR_ERR(token);
+		if (!bpf_token_allow_cmd(token, BPF_BTF_GET_FD_BY_ID)) {
+			bpf_token_put(token);
+			token = NULL;
+		}
+	}
+
+	if (!bpf_token_capable(token, CAP_SYS_ADMIN)) {
+		bpf_token_put(token);
 		return -EPERM;
+	}
+
+	bpf_token_put(token);
 
 	return btf_get_fd_by_id(attr->btf_id);
 }
tools/include/uapi/linux/bpf.h +1 -0
index bb37897c0393..73c23daacabf 100644
@@ -1652,6 +1652,7 @@ union bpf_attr {
 		};
 		__u32		next_id;
 		__u32		open_flags;
+		__s32		token_fd;
 	};
 
 	struct { /* anonymous struct used by BPF_OBJ_GET_INFO_BY_FD */
tools/testing/selftests/bpf/prog_tests/libbpf_get_fd_by_id_opts.c +1 -2
index a3f238f51d05..976ff38a6d43 100644
@@ -75,9 +75,8 @@ void test_libbpf_get_fd_by_id_opts(void)
 	if (!ASSERT_EQ(ret, -EINVAL, "bpf_link_get_fd_by_id_opts"))
 		goto close_prog;
 
-	/* BTF get fd with opts set should not work (no kernel support). */
 	ret = bpf_btf_get_fd_by_id_opts(0, &fd_opts_rdonly);
-	ASSERT_EQ(ret, -EINVAL, "bpf_btf_get_fd_by_id_opts");
+	ASSERT_EQ(ret, -ENOENT, "bpf_btf_get_fd_by_id_opts");
 
 close_prog:
 	if (fd >= 0)
Signature · 1 line
2.48.1
#2 lore raw

Re: [PATCH bpf-next v5 1/4] bpf: BPF token support for BPF_BTF_GET_FD_BY_ID

Quoted 37 lines · Mykyta Yatsenko

From: Mykyta Yatsenko <[email protected]>

Currently BPF_BTF_GET_FD_BY_ID requires CAP_SYS_ADMIN, which does not allow running it from user namespace. This creates a problem when freplace program running from user namespace needs to query target program BTF.
This patch relaxes capable check from CAP_SYS_ADMIN to CAP_BPF and adds support for BPF token that can be passed in attributes to syscall.

---
 include/uapi/linux/bpf.h                      |  1 +
 kernel/bpf/syscall.c                          | 20 +++++++++++++++++--
 tools/include/uapi/linux/bpf.h                |  1 +
 .../bpf/prog_tests/libbpf_get_fd_by_id_opts.c |  3 +--
 4 files changed, 21 insertions(+), 4 deletions(-)
include/uapi/linux/bpf.h +1 -0
index bb37897c0393..73c23daacabf 100644
@@ -1652,6 +1652,7 @@ union bpf_attr {
                };
                __u32           next_id;
                __u32           open_flags;
+               __s32           token_fd;
        };
 
        struct { /* anonymous struct used by BPF_OBJ_GET_INFO_BY_FD */
kernel/bpf/syscall.c +0 -0
index 57a438706215..188f7296cf9f 100644
@@ -5137,15 +5137,31 @@ static int bpf_btf_load(const union bpf_attr *attr, bpfptr_t uattr, __u32 uattr_
        return btf_new_fd(attr, uattr, uattr_size);
 }

kernel/bpf/syscall.c static int bpf_btf_load(const union bpf_attr *attr, bpfptr_t uattr, __u32 uattr_ · line 5147

Quoted 10 lines
kernel/bpf/syscall.c +3 -1
 
-#define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id
+#define BPF_BTF_GET_FD_BY_ID_LAST_FIELD token_fd
 
 static int bpf_btf_get_fd_by_id(const union bpf_attr *attr)
 {
+       struct bpf_token *token = NULL;
+
        if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID))
                return -EINVAL;

as I mentioned in another email, we used to implicitly reject any open_flags set because of BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id, but now that we do support some flags in open_flags, we need to validate that there are no other flags. So you need something like:

if (attr->open_flags & ~BPF_F_TOKEN_FD)

    return -EINVAL;

pw-bot: cr

Quoted 51 lines
kernel/bpf/syscall.c +15 -1
-       if (!capable(CAP_SYS_ADMIN))
+       if (attr->open_flags & BPF_F_TOKEN_FD) {
+               token = bpf_token_get_from_fd(attr->token_fd);
+               if (IS_ERR(token))
+                       return PTR_ERR(token);
+               if (!bpf_token_allow_cmd(token, BPF_BTF_GET_FD_BY_ID)) {
+                       bpf_token_put(token);
+                       token = NULL;
+               }
+       }
+
+       if (!bpf_token_capable(token, CAP_SYS_ADMIN)) {
+               bpf_token_put(token);
                return -EPERM;
+       }
+
+       bpf_token_put(token);
 
        return btf_get_fd_by_id(attr->btf_id);
 }
tools/include/uapi/linux/bpf.h +1 -0
index bb37897c0393..73c23daacabf 100644
@@ -1652,6 +1652,7 @@ union bpf_attr {
                };
                __u32           next_id;
                __u32           open_flags;
+               __s32           token_fd;
        };
 
        struct { /* anonymous struct used by BPF_OBJ_GET_INFO_BY_FD */
tools/testing/selftests/bpf/prog_tests/libbpf_get_fd_by_id_opts.c +1 -3
index a3f238f51d05..976ff38a6d43 100644
@@ -75,9 +75,8 @@ void test_libbpf_get_fd_by_id_opts(void)
        if (!ASSERT_EQ(ret, -EINVAL, "bpf_link_get_fd_by_id_opts"))
                goto close_prog;
 
-       /* BTF get fd with opts set should not work (no kernel support). */
        ret = bpf_btf_get_fd_by_id_opts(0, &fd_opts_rdonly);
-       ASSERT_EQ(ret, -EINVAL, "bpf_btf_get_fd_by_id_opts");
+       ASSERT_EQ(ret, -ENOENT, "bpf_btf_get_fd_by_id_opts");
 
 close_prog:
        if (fd >= 0)
--

2.48.1

#3 lore raw

Re: [PATCH bpf-next v5 1/4] bpf: BPF token support for BPF_BTF_GET_FD_BY_ID

Quoted 21 lines · Andrii Nakryiko
Quoted 18 lines · Mykyta Yatsenko

From: Mykyta Yatsenko <[email protected]>

Currently BPF_BTF_GET_FD_BY_ID requires CAP_SYS_ADMIN, which does not allow running it from user namespace. This creates a problem when freplace program running from user namespace needs to query target program BTF.
This patch relaxes capable check from CAP_SYS_ADMIN to CAP_BPF and adds support for BPF token that can be passed in attributes to syscall.

---
 include/uapi/linux/bpf.h                      |  1 +
 kernel/bpf/syscall.c                          | 20 +++++++++++++++++--
 tools/include/uapi/linux/bpf.h                |  1 +
 .../bpf/prog_tests/libbpf_get_fd_by_id_opts.c |  3 +--
 4 files changed, 21 insertions(+), 4 deletions(-)
Quoted 9 lines

include/uapi/linux/bpf.h union bpf_attr { · line 1655

Quoted 9 lines
include/uapi/linux/bpf.h +1 -0
index bb37897c0393..73c23daacabf 100644
@@ -1652,6 +1652,7 @@ union bpf_attr {
                };
                __u32           next_id;
                __u32           open_flags;
+               __s32           token_fd;

So I almost applied it (I was going to add that flags check myself), but then I realized that attrs->token_fd looks a bit too good to be true... It's an ugly "historical" bpf_attr structure, but we have these weird mixes of named and anon sub-structs, so some fields are way too easily accessed even when dealing with a completely unrelated command.

That is just to say that token_fd here is way too error-prone and generically named, unfortunately. All other token-enabled commands have more specific name (btf_token_fd, prog_token_fd, map_token_fd), so I think we should continue the pattern here.

Let's call it fd_by_id_token_fd (unless someone has a better name)? Let's keep a clean "token_fd" on libbpf API side (in opts), like we did for other APIs.

Quoted 83 lines
Quoted 10 lines
include/uapi/linux/bpf.h +0 -0
        };
 
        struct { /* anonymous struct used by BPF_OBJ_GET_INFO_BY_FD */
kernel/bpf/syscall.c +0 -0
index 57a438706215..188f7296cf9f 100644
@@ -5137,15 +5137,31 @@ static int bpf_btf_load(const union bpf_attr *attr, bpfptr_t uattr, __u32 uattr_
        return btf_new_fd(attr, uattr, uattr_size);
 }

kernel/bpf/syscall.c static int bpf_btf_load(const union bpf_attr *attr, bpfptr_t uattr, __u32 uattr_ · line 5147

Quoted 10 lines
kernel/bpf/syscall.c +3 -1
 
-#define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id
+#define BPF_BTF_GET_FD_BY_ID_LAST_FIELD token_fd
 
 static int bpf_btf_get_fd_by_id(const union bpf_attr *attr)
 {
+       struct bpf_token *token = NULL;
+
        if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID))
                return -EINVAL;

as I mentioned in another email, we used to implicitly reject any open_flags set because of BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id, but now that we do support some flags in open_flags, we need to validate that there are no other flags. So you need something like:

if (attr->open_flags & ~BPF_F_TOKEN_FD)

    return -EINVAL;

pw-bot: cr

Quoted 51 lines
kernel/bpf/syscall.c +15 -1
-       if (!capable(CAP_SYS_ADMIN))
+       if (attr->open_flags & BPF_F_TOKEN_FD) {
+               token = bpf_token_get_from_fd(attr->token_fd);
+               if (IS_ERR(token))
+                       return PTR_ERR(token);
+               if (!bpf_token_allow_cmd(token, BPF_BTF_GET_FD_BY_ID)) {
+                       bpf_token_put(token);
+                       token = NULL;
+               }
+       }
+
+       if (!bpf_token_capable(token, CAP_SYS_ADMIN)) {
+               bpf_token_put(token);
                return -EPERM;
+       }
+
+       bpf_token_put(token);
 
        return btf_get_fd_by_id(attr->btf_id);
 }
tools/include/uapi/linux/bpf.h +1 -0
index bb37897c0393..73c23daacabf 100644
@@ -1652,6 +1652,7 @@ union bpf_attr {
                };
                __u32           next_id;
                __u32           open_flags;
+               __s32           token_fd;
        };
 
        struct { /* anonymous struct used by BPF_OBJ_GET_INFO_BY_FD */
tools/testing/selftests/bpf/prog_tests/libbpf_get_fd_by_id_opts.c +1 -3
index a3f238f51d05..976ff38a6d43 100644
@@ -75,9 +75,8 @@ void test_libbpf_get_fd_by_id_opts(void)
        if (!ASSERT_EQ(ret, -EINVAL, "bpf_link_get_fd_by_id_opts"))
                goto close_prog;
 
-       /* BTF get fd with opts set should not work (no kernel support). */
        ret = bpf_btf_get_fd_by_id_opts(0, &fd_opts_rdonly);
-       ASSERT_EQ(ret, -EINVAL, "bpf_btf_get_fd_by_id_opts");
+       ASSERT_EQ(ret, -ENOENT, "bpf_btf_get_fd_by_id_opts");
 
 close_prog:
        if (fd >= 0)
--

2.48.1

#4 lore raw

PATCH v5 2/4 bpf: return prog btf_id without capable check

In reply to Mykyta Yatsenko

From: Mykyta Yatsenko <[email protected]>

Return prog's btf_id from bpf_prog_get_info_by_fd regardless of capable check. This patch enables scenario, when freplace program, running from user namespace, requires to query target prog's btf.

---
 kernel/bpf/syscall.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/bpf/syscall.c +2 -2
index 188f7296cf9f..c51193ced383 100644
@@ -4749,6 +4749,8 @@ static int bpf_prog_get_info_by_fd(struct file *file,
 	info.recursion_misses = stats.misses;
 
 	info.verified_insns = prog->aux->verified_insns;
+	if (prog->aux->btf)
+		info.btf_id = btf_obj_id(prog->aux->btf);
 
 	if (!bpf_capable()) {
 		info.jited_prog_len = 0;
@@ -4895,8 +4897,6 @@ static int bpf_prog_get_info_by_fd(struct file *file,
 		}
 	}
 
-	if (prog->aux->btf)
-		info.btf_id = btf_obj_id(prog->aux->btf);
 	info.attach_btf_id = prog->aux->attach_btf_id;
 	if (attach_btf)
 		info.attach_btf_obj_id = btf_obj_id(attach_btf);
Signature · 1 line
2.48.1
#5 lore raw

PATCH v5 3/4 libbpf: pass BPF token from find_prog_btf_id to BPF_BTF_GET_FD_BY_ID

In reply to Mykyta Yatsenko

From: Mykyta Yatsenko <[email protected]>

Pass BPF token from bpf_program__set_attach_target to BPF_BTF_GET_FD_BY_ID bpf command.
When freplace program attaches to target program, it needs to look up for BTF of the target, this may require BPF token, if, for example, running from user namespace.

---
 tools/lib/bpf/bpf.c             |  3 ++-
 tools/lib/bpf/bpf.h             |  3 ++-
 tools/lib/bpf/btf.c             | 15 +++++++++++++--
 tools/lib/bpf/libbpf.c          | 10 +++++-----
 tools/lib/bpf/libbpf_internal.h |  1 +
 5 files changed, 23 insertions(+), 9 deletions(-)
tools/lib/bpf/bpf.c +2 -1
index 359f73ead613..783274172e56 100644
@@ -1097,7 +1097,7 @@ int bpf_map_get_fd_by_id(__u32 id)
 int bpf_btf_get_fd_by_id_opts(__u32 id,
 			      const struct bpf_get_fd_by_id_opts *opts)
 {
-	const size_t attr_sz = offsetofend(union bpf_attr, open_flags);
+	const size_t attr_sz = offsetofend(union bpf_attr, token_fd);
 	union bpf_attr attr;
 	int fd;
 
@@ -1107,6 +1107,7 @@ int bpf_btf_get_fd_by_id_opts(__u32 id,
 	memset(&attr, 0, attr_sz);
 	attr.btf_id = id;
 	attr.open_flags = OPTS_GET(opts, open_flags, 0);
+	attr.token_fd = OPTS_GET(opts, token_fd, 0);
 
 	fd = sys_bpf_fd(BPF_BTF_GET_FD_BY_ID, &attr, attr_sz);
 	return libbpf_err_errno(fd);
tools/lib/bpf/bpf.h +2 -1
index 435da95d2058..777627d33d25 100644
@@ -487,9 +487,10 @@ LIBBPF_API int bpf_link_get_next_id(__u32 start_id, __u32 *next_id);
 struct bpf_get_fd_by_id_opts {
 	size_t sz; /* size of this struct for forward/backward compatibility */
 	__u32 open_flags; /* permissions requested for the operation on fd */
+	__u32 token_fd;
 	size_t :0;
 };
-#define bpf_get_fd_by_id_opts__last_field open_flags
+#define bpf_get_fd_by_id_opts__last_field token_fd
 
 LIBBPF_API int bpf_prog_get_fd_by_id(__u32 id);
 LIBBPF_API int bpf_prog_get_fd_by_id_opts(__u32 id,
tools/lib/bpf/btf.c +13 -2
index eea99c766a20..38bc6b14b066 100644
@@ -1619,12 +1619,18 @@ struct btf *btf_get_from_fd(int btf_fd, struct btf *base_btf)
 	return btf;
 }
 
-struct btf *btf__load_from_kernel_by_id_split(__u32 id, struct btf *base_btf)
+struct btf *btf_load_from_kernel(__u32 id, struct btf *base_btf, int token_fd)
 {
 	struct btf *btf;
 	int btf_fd;
+	LIBBPF_OPTS(bpf_get_fd_by_id_opts, opts);
+
+	if (token_fd) {
+		opts.open_flags |= BPF_F_TOKEN_FD;
+		opts.token_fd = token_fd;
+	}
 
-	btf_fd = bpf_btf_get_fd_by_id(id);
+	btf_fd = bpf_btf_get_fd_by_id_opts(id, &opts);
 	if (btf_fd < 0)
 		return libbpf_err_ptr(-errno);
 
@@ -1634,6 +1640,11 @@ struct btf *btf__load_from_kernel_by_id_split(__u32 id, struct btf *base_btf)
 	return libbpf_ptr(btf);
 }
 
+struct btf *btf__load_from_kernel_by_id_split(__u32 id, struct btf *base_btf)
+{
+	return btf_load_from_kernel(id, base_btf, 0);
+}
+
 struct btf *btf__load_from_kernel_by_id(__u32 id)
 {
 	return btf__load_from_kernel_by_id_split(id, NULL);
tools/lib/bpf/libbpf.c +5 -5
index 8e32286854ef..6b85060f07b3 100644
@@ -10024,7 +10024,7 @@ int libbpf_find_vmlinux_btf_id(const char *name,
 	return libbpf_err(err);
 }
 
-static int libbpf_find_prog_btf_id(const char *name, __u32 attach_prog_fd)
+static int libbpf_find_prog_btf_id(const char *name, __u32 attach_prog_fd, int token_fd)
 {
 	struct bpf_prog_info info;
 	__u32 info_len = sizeof(info);
@@ -10044,7 +10044,7 @@ static int libbpf_find_prog_btf_id(const char *name, __u32 attach_prog_fd)
 		pr_warn("The target program doesn't have BTF\n");
 		goto out;
 	}
-	btf = btf__load_from_kernel_by_id(info.btf_id);
+	btf = btf_load_from_kernel(info.btf_id, NULL, token_fd);
 	err = libbpf_get_error(btf);
 	if (err) {
 		pr_warn("Failed to get BTF %d of the program: %s\n", info.btf_id, errstr(err));
@@ -10127,7 +10127,7 @@ static int libbpf_find_attach_btf_id(struct bpf_program *prog, const char *attac
 			pr_warn("prog '%s': attach program FD is not set\n", prog->name);
 			return -EINVAL;
 		}
-		err = libbpf_find_prog_btf_id(attach_name, attach_prog_fd);
+		err = libbpf_find_prog_btf_id(attach_name, attach_prog_fd, prog->obj->token_fd);
 		if (err < 0) {
 			pr_warn("prog '%s': failed to find BPF program (FD %d) BTF ID for '%s': %s\n",
 				prog->name, attach_prog_fd, attach_name, errstr(err));
@@ -12923,7 +12923,7 @@ struct bpf_link *bpf_program__attach_freplace(const struct bpf_program *prog,
 	if (target_fd) {
 		LIBBPF_OPTS(bpf_link_create_opts, target_opts);
 
-		btf_id = libbpf_find_prog_btf_id(attach_func_name, target_fd);
+		btf_id = libbpf_find_prog_btf_id(attach_func_name, target_fd, prog->obj->token_fd);
 		if (btf_id < 0)
 			return libbpf_err_ptr(btf_id);
 
@@ -13744,7 +13744,7 @@ int bpf_program__set_attach_target(struct bpf_program *prog,
 
 	if (attach_prog_fd) {
 		btf_id = libbpf_find_prog_btf_id(attach_func_name,
-						 attach_prog_fd);
+						 attach_prog_fd, prog->obj->token_fd);
 		if (btf_id < 0)
 			return libbpf_err(btf_id);
 	} else {
tools/lib/bpf/libbpf_internal.h +1 -0
index de498e2dd6b0..76669c73dcd1 100644
@@ -409,6 +409,7 @@ int libbpf__load_raw_btf(const char *raw_types, size_t types_len,
 int btf_load_into_kernel(struct btf *btf,
 			 char *log_buf, size_t log_sz, __u32 log_level,
 			 int token_fd);
+struct btf *btf_load_from_kernel(__u32 id, struct btf *base_btf, int token_fd);
 
 struct btf *btf_get_from_fd(int btf_fd, struct btf *base_btf);
 void btf_get_kernel_prefix_kind(enum bpf_attach_type attach_type,
Signature · 1 line
2.48.1
#6 lore raw

PATCH v5 4/4 selftests/bpf: test freplace from user namespace

In reply to Mykyta Yatsenko

From: Mykyta Yatsenko <[email protected]>

Add selftests to verify that it is possible to load freplace program from user namespace if BPF token is initialized by bpf_object__prepare before calling bpf_program__set_attach_target.
Negative test is added as well.

Modified type of the priv_prog to xdp, as kprobe did not work on aarch64 and s390x.

---
 .../testing/selftests/bpf/prog_tests/token.c  | 97 ++++++++++++++++++-
 .../selftests/bpf/progs/priv_freplace_prog.c  | 13 +++
 tools/testing/selftests/bpf/progs/priv_prog.c |  6 +-
 3 files changed, 112 insertions(+), 4 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/progs/priv_freplace_prog.c
tools/testing/selftests/bpf/prog_tests/token.c +96 -1
index c3ab9b6fb069..f9392df23f8a 100644
@@ -19,6 +19,7 @@
 #include "priv_prog.skel.h"
 #include "dummy_st_ops_success.skel.h"
 #include "token_lsm.skel.h"
+#include "priv_freplace_prog.skel.h"
 
 static inline int sys_mount(const char *dev_name, const char *dir_name,
 			    const char *type, unsigned long flags,
@@ -788,6 +789,84 @@ static int userns_obj_priv_prog(int mnt_fd, struct token_lsm *lsm_skel)
 	return 0;
 }
 
+static int userns_obj_priv_freplace_setup(int mnt_fd, struct priv_freplace_prog **fr_skel,
+					  struct priv_prog **skel, int *tgt_fd)
+{
+	LIBBPF_OPTS(bpf_object_open_opts, opts);
+	int err;
+	char buf[256];
+
+	/* use bpf_token_path to provide BPF FS path */
+	snprintf(buf, sizeof(buf), "/proc/self/fd/%d", mnt_fd);
+	opts.bpf_token_path = buf;
+	*skel = priv_prog__open_opts(&opts);
+	if (!ASSERT_OK_PTR(*skel, "priv_prog__open_opts"))
+		return -EINVAL;
+	err = priv_prog__load(*skel);
+	if (!ASSERT_OK(err, "priv_prog__load"))
+		return -EINVAL;
+
+	*fr_skel = priv_freplace_prog__open_opts(&opts);
+	if (!ASSERT_OK_PTR(*skel, "priv_freplace_prog__open_opts"))
+		return -EINVAL;
+
+	*tgt_fd = bpf_program__fd((*skel)->progs.xdp_prog1);
+	return 0;
+}
+
+/* Verify that freplace works from user namespace, because bpf token is loaded
+ * in bpf_object__prepare
+ */
+static int userns_obj_priv_freplace_prog(int mnt_fd, struct token_lsm *lsm_skel)
+{
+	struct priv_freplace_prog *fr_skel = NULL;
+	struct priv_prog *skel = NULL;
+	int err, tgt_fd;
+
+	err = userns_obj_priv_freplace_setup(mnt_fd, &fr_skel, &skel, &tgt_fd);
+	if (!ASSERT_OK(err, "setup"))
+		goto out;
+
+	err = bpf_object__prepare(fr_skel->obj);
+	if (!ASSERT_OK(err, "freplace__prepare"))
+		goto out;
+
+	err = bpf_program__set_attach_target(fr_skel->progs.new_xdp_prog2, tgt_fd, "xdp_prog1");
+	if (!ASSERT_OK(err, "set_attach_target"))
+		goto out;
+
+	err = priv_freplace_prog__load(fr_skel);
+	ASSERT_OK(err, "priv_freplace_prog__load");
+
+out:
+	priv_freplace_prog__destroy(fr_skel);
+	priv_prog__destroy(skel);
+	return err;
+}
+
+/* Verify that replace fails to set attach target from user namespace without bpf token */
+static int userns_obj_priv_freplace_prog_fail(int mnt_fd, struct token_lsm *lsm_skel)
+{
+	struct priv_freplace_prog *fr_skel = NULL;
+	struct priv_prog *skel = NULL;
+	int err, tgt_fd;
+
+	err = userns_obj_priv_freplace_setup(mnt_fd, &fr_skel, &skel, &tgt_fd);
+	if (!ASSERT_OK(err, "setup"))
+		goto out;
+
+	err = bpf_program__set_attach_target(fr_skel->progs.new_xdp_prog2, tgt_fd, "xdp_prog1");
+	if (ASSERT_ERR(err, "attach fails"))
+		err = 0;
+	else
+		err = -EINVAL;
+
+out:
+	priv_freplace_prog__destroy(fr_skel);
+	priv_prog__destroy(skel);
+	return err;
+}
+
 /* this test is called with BPF FS that doesn't delegate BPF_BTF_LOAD command,
  * which should cause struct_ops application to fail, as BTF won't be uploaded
  * into the kernel, even if STRUCT_OPS programs themselves are allowed
@@ -1004,12 +1083,28 @@ void test_token(void)
 	if (test__start_subtest("obj_priv_prog")) {
 		struct bpffs_opts opts = {
 			.cmds = bit(BPF_PROG_LOAD),
-			.progs = bit(BPF_PROG_TYPE_KPROBE),
+			.progs = bit(BPF_PROG_TYPE_XDP),
 			.attachs = ~0ULL,
 		};
 
 		subtest_userns(&opts, userns_obj_priv_prog);
 	}
+	if (test__start_subtest("obj_priv_freplace_prog")) {
+		struct bpffs_opts opts = {
+			.cmds = bit(BPF_BTF_LOAD) | bit(BPF_PROG_LOAD) | bit(BPF_BTF_GET_FD_BY_ID),
+			.progs = bit(BPF_PROG_TYPE_EXT) | bit(BPF_PROG_TYPE_XDP),
+			.attachs = ~0ULL,
+		};
+		subtest_userns(&opts, userns_obj_priv_freplace_prog);
+	}
+	if (test__start_subtest("obj_priv_freplace_prog_fail")) {
+		struct bpffs_opts opts = {
+			.cmds = bit(BPF_BTF_LOAD) | bit(BPF_PROG_LOAD) | bit(BPF_BTF_GET_FD_BY_ID),
+			.progs = bit(BPF_PROG_TYPE_EXT) | bit(BPF_PROG_TYPE_XDP),
+			.attachs = ~0ULL,
+		};
+		subtest_userns(&opts, userns_obj_priv_freplace_prog_fail);
+	}
 	if (test__start_subtest("obj_priv_btf_fail")) {
 		struct bpffs_opts opts = {
 			/* disallow BTF loading */
tools/testing/selftests/bpf/progs/priv_freplace_prog.c new file +13 -0
new file mode 100644
index 000000000000..ccf1b04010ba
@@ -0,0 +1,13 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */
+
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+
+char _license[] SEC("license") = "GPL";
+
+SEC("freplace/xdp_prog1")
+int new_xdp_prog2(struct xdp_md *xd)
+{
+	return XDP_DROP;
+}
tools/testing/selftests/bpf/progs/priv_prog.c +3 -3
index 3c7b2b618c8a..725e29595079 100644
@@ -6,8 +6,8 @@
 
 char _license[] SEC("license") = "GPL";
 
-SEC("kprobe")
-int kprobe_prog(void *ctx)
+SEC("xdp")
+int xdp_prog1(struct xdp_md *xdp)
 {
-	return 1;
+	return XDP_DROP;
 }
Signature · 1 line
2.48.1