Support freplace prog from user namespace
Subsystems (5)
v4 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 | 25 ++++- tools/include/uapi/linux/bpf.h | 1 + tools/lib/bpf/bpf.c | 3 +- tools/lib/bpf/bpf.h | 4 +- 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, 159 insertions(+), 20 deletions(-) create mode 100644 tools/testing/selftests/bpf/progs/priv_freplace_prog.c
Signature · 1 line
2.48.1
PATCH v4 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 | 21 ++++++++++++++++--- tools/include/uapi/linux/bpf.h | 1 + .../bpf/prog_tests/libbpf_get_fd_by_id_opts.c | 3 +-- 4 files changed, 21 insertions(+), 5 deletions(-)
include/uapi/linux/bpf.h +1 -0
@@ -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 -3
@@ -5137,17 +5137,32 @@ 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_fdstatic 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 )) -return -EPERM ; +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 )) + goto out; + } + +if (!bpf_token_capable (token,CAP_SYS_ADMIN )) + goto out; + +bpf_token_put (token);return btf_get_fd_by_id (attr->btf_id); +out: +bpf_token_put (token); +return -EPERM ; } static int bpf_task_fd_query_copy(const union bpf_attr *attr,
tools/include/uapi/linux/bpf.h +1 -0
@@ -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
@@ -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
Re: [PATCH bpf-next v4 1/4] bpf: BPF token support for BPF_BTF_GET_FD_BY_ID
Quoted 10 lines · Mykyta Yatsenko
From: Mykyta Yatsenko <[email protected]>
Currently
BPF_BTF_GET_FD_BY_IDrequiresCAP_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 fromCAP_SYS_ADMINtoCAP_BPFand adds support for BPF token that can be passed in attributes to syscall.Signed-off-by Mykyta Yatsenko <[email protected]>
Re: [PATCH bpf-next v4 1/4] bpf: BPF token support for BPF_BTF_GET_FD_BY_ID
In reply to Mykyta YatsenkoQuoted 46 lines · Mykyta Yatsenko
From: Mykyta Yatsenko <[email protected]>
Currently
BPF_BTF_GET_FD_BY_IDrequiresCAP_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 fromCAP_SYS_ADMINtoCAP_BPFand adds support for BPF token that can be passed in attributes to syscall.Signed-off-by Mykyta Yatsenko <[email protected]>--- include/uapi/linux/bpf.h | 1 + kernel/bpf/syscall.c | 21 ++++++++++++++++--- tools/include/uapi/linux/bpf.h | 1 + .../bpf/prog_tests/libbpf_get_fd_by_id_opts.c | 3 +-- 4 files changed, 21 insertions(+), 5 deletions(-)include/uapi/linux/bpf.h +1
-0@@ -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 +3
-1@@ -5137,17 +5137,32 @@ 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_fdstatic 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 ))
kernel/bpf/syscall.c static int bpf_btf_load(const union bpf_attr *attr, bpfptr_t uattr, __u32 uattr_ · line 5154
Quoted 10 lines
kernel/bpf/syscall.c +6
-2return -EINVAL ; -if (!capable (CAP_SYS_ADMIN )) -return -EPERM ; +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 )) + goto out;
Look at map_create() and its handling of BPF token. If bpf_token_allow_cmd() returns false, we still perform bpf_token_capable(token, <cap>) check (where token will be NULL, so it's effectively just capable() check). While here you will just return -EPERM *even if the process actually has real CAP_SYS_ADMIN* capability.
Instead, do:
bpf_token_put(token);
token = NULL;
and carry on the rest of the logic
pw-bot: cr
Quoted 31 lines
kernel/bpf/syscall.c +9
-0+ } + +if (!bpf_token_capable (token,CAP_SYS_ADMIN )) + goto out; + +bpf_token_put (token);return btf_get_fd_by_id (attr->btf_id); +out: +bpf_token_put (token); +return -EPERM ; } static int bpf_task_fd_query_copy(const union bpf_attr *attr,tools/include/uapi/linux/bpf.h +1
-0@@ -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 +0
-0
tools/testing/selftests/bpf/prog_tests/libbpf_get_fd_by_id_opts.c void test_libbpf_get_fd_by_id_opts(void) · line 79
Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/libbpf_get_fd_by_id_opts.c +1
-2@@ -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" );
Why would your patch change this behavior? and if it does, should it? This looks fishy.
Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/libbpf_get_fd_by_id_opts.c +0
-1close_prog:if (fd >=0 ) --2.48.1
Yonghong Song
reviewerRe: [PATCH bpf-next v4 1/4] bpf: BPF token support for BPF_BTF_GET_FD_BY_ID
Quoted 64 lines · Andrii Nakryiko
Quoted 45 lines · Mykyta Yatsenko
From: Mykyta Yatsenko <[email protected]>
Currently
BPF_BTF_GET_FD_BY_IDrequiresCAP_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 fromCAP_SYS_ADMINtoCAP_BPFand adds support for BPF token that can be passed in attributes to syscall.Signed-off-by Mykyta Yatsenko <[email protected]>--- include/uapi/linux/bpf.h | 1 + kernel/bpf/syscall.c | 21 ++++++++++++++++--- tools/include/uapi/linux/bpf.h | 1 + .../bpf/prog_tests/libbpf_get_fd_by_id_opts.c | 3 +-- 4 files changed, 21 insertions(+), 5 deletions(-)include/uapi/linux/bpf.h +1
-0@@ -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 +3
-1@@ -5137,17 +5137,32 @@ 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_fdstatic 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 ))kernel/bpf/syscall.c static int bpf_btf_load(const union bpf_attr *attr, bpfptr_t uattr, __u32 uattr_ · line 5154
Quoted 10 lines
kernel/bpf/syscall.c +6
-2return -EINVAL ; -if (!capable (CAP_SYS_ADMIN )) -return -EPERM ; +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 )) + goto out;Look at
map_create()and its handling of BPF token. Ifbpf_token_allow_cmd()returns false, we still performbpf_token_capable(token, <cap>)check (where token will be NULL, so it's effectively justcapable()check). While here you will just return-EPERM*even if the process actually has real CAP_SYS_ADMIN* capability.
Quoted 6 lines
Instead, do:
bpf_token_put(token);
token = NULL;and carry on the rest of the logic
It looks like my earlier suggestion, which leads to this version, is incorrect. Sorry about this. I need to dig out a little more for func cap_capable_helper(). But it is apparent that task cred is used for capability checking.
Quoted 50 lines
pw-bot: cr
Quoted 31 lines
kernel/bpf/syscall.c +9
-0+ } + +if (!bpf_token_capable (token,CAP_SYS_ADMIN )) + goto out; + +bpf_token_put (token);return btf_get_fd_by_id (attr->btf_id); +out: +bpf_token_put (token); +return -EPERM ; } static int bpf_task_fd_query_copy(const union bpf_attr *attr,tools/include/uapi/linux/bpf.h +1
-0@@ -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 +0
-0tools/testing/selftests/bpf/prog_tests/libbpf_get_fd_by_id_opts.c void test_libbpf_get_fd_by_id_opts(void) · line 79
Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/libbpf_get_fd_by_id_opts.c +1
-2@@ -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" );Why would your patch change this behavior? and if it does, should it? This looks fishy.
Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/libbpf_get_fd_by_id_opts.c +0
-1close_prog:if (fd >=0 ) --2.48.1
Re: [PATCH bpf-next v4 1/4] bpf: BPF token support for BPF_BTF_GET_FD_BY_ID
In reply to Andrii NakryikoQuoted 64 lines · Andrii Nakryiko
Quoted 45 lines · Mykyta Yatsenko
From: Mykyta Yatsenko <[email protected]>
Currently
BPF_BTF_GET_FD_BY_IDrequiresCAP_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 fromCAP_SYS_ADMINtoCAP_BPFand adds support for BPF token that can be passed in attributes to syscall.Signed-off-by Mykyta Yatsenko <[email protected]>--- include/uapi/linux/bpf.h | 1 + kernel/bpf/syscall.c | 21 ++++++++++++++++--- tools/include/uapi/linux/bpf.h | 1 + .../bpf/prog_tests/libbpf_get_fd_by_id_opts.c | 3 +-- 4 files changed, 21 insertions(+), 5 deletions(-)include/uapi/linux/bpf.h +1
-0@@ -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 +3
-1@@ -5137,17 +5137,32 @@ 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_fdstatic 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 ))kernel/bpf/syscall.c static int bpf_btf_load(const union bpf_attr *attr, bpfptr_t uattr, __u32 uattr_ · line 5154
Quoted 10 lines
kernel/bpf/syscall.c +6
-2return -EINVAL ; -if (!capable (CAP_SYS_ADMIN )) -return -EPERM ; +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 )) + goto out;Look at
map_create()and its handling of BPF token. Ifbpf_token_allow_cmd()returns false, we still performbpf_token_capable(token, <cap>)check (where token will be NULL, so it's effectively justcapable()check). While here you will just return-EPERM*even if the process actually has real CAP_SYS_ADMIN* capability.
Quoted 6 lines
Instead, do:
bpf_token_put(token);
token = NULL;and carry on the rest of the logic
Got it, thanks.
Quoted 37 lines
pw-bot: cr
Quoted 34 lines
kernel/bpf/syscall.c +9
-0+ } + +if (!bpf_token_capable (token,CAP_SYS_ADMIN )) + goto out; + +bpf_token_put (token);return btf_get_fd_by_id (attr->btf_id); +out: +bpf_token_put (token); +return -EPERM ; } static int bpf_task_fd_query_copy(const union bpf_attr *attr,tools/include/uapi/linux/bpf.h +1
-0@@ -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 +0
-0@@ -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;
Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/libbpf_get_fd_by_id_opts.c void test_libbpf_get_fd_by_id_opts(void) · line 79
Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/libbpf_get_fd_by_id_opts.c +1
-2-/* 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" );Why would your patch change this behavior? and if it does, should it? This looks fishy.
I agree this does not look right, I think the test itself is not ideal. The behavior this test checked for has changed, btf_get_fd_by_id was returning EINVAL from here:
``` if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID)) return -EINVAL; ```
That no longer fails because I added new field (token_fd) to the attr structure.
Function now fails further down the road.//I'm on the fence whether delete this check at all or change to new error code.
Quoted 5 lines
Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/libbpf_get_fd_by_id_opts.c +0
-1close_prog:if (fd >=0 ) --2.48.1
Re: [PATCH bpf-next v4 1/4] bpf: BPF token support for BPF_BTF_GET_FD_BY_ID
Quoted 125 lines · Mykyta Yatsenko
Quoted 64 lines · Andrii Nakryiko
Quoted 45 lines · Mykyta Yatsenko
From: Mykyta Yatsenko <[email protected]>
Currently
BPF_BTF_GET_FD_BY_IDrequiresCAP_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 fromCAP_SYS_ADMINtoCAP_BPFand adds support for BPF token that can be passed in attributes to syscall.Signed-off-by Mykyta Yatsenko <[email protected]>--- include/uapi/linux/bpf.h | 1 + kernel/bpf/syscall.c | 21 ++++++++++++++++--- tools/include/uapi/linux/bpf.h | 1 + .../bpf/prog_tests/libbpf_get_fd_by_id_opts.c | 3 +-- 4 files changed, 21 insertions(+), 5 deletions(-)include/uapi/linux/bpf.h +1
-0@@ -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 +3
-1@@ -5137,17 +5137,32 @@ 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_fdstatic 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 ))kernel/bpf/syscall.c static int bpf_btf_load(const union bpf_attr *attr, bpfptr_t uattr, __u32 uattr_ · line 5154
Quoted 10 lines
kernel/bpf/syscall.c +6
-2return -EINVAL ; -if (!capable (CAP_SYS_ADMIN )) -return -EPERM ; +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 )) + goto out;Look at
map_create()and its handling of BPF token. Ifbpf_token_allow_cmd()returns false, we still performbpf_token_capable(token, <cap>)check (where token will be NULL, so it's effectively justcapable()check). While here you will just return-EPERM*even if the process actually has real CAP_SYS_ADMIN* capability.Quoted 6 lines
Instead, do:
bpf_token_put(token);
token = NULL;and carry on the rest of the logic
Got it, thanks.
Quoted 37 lines
pw-bot: cr
Quoted 34 lines
kernel/bpf/syscall.c +9
-0+ } + +if (!bpf_token_capable (token,CAP_SYS_ADMIN )) + goto out; + +bpf_token_put (token);return btf_get_fd_by_id (attr->btf_id); +out: +bpf_token_put (token); +return -EPERM ; } static int bpf_task_fd_query_copy(const union bpf_attr *attr,tools/include/uapi/linux/bpf.h +1
-0@@ -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 +0
-0@@ -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;Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/libbpf_get_fd_by_id_opts.c void test_libbpf_get_fd_by_id_opts(void) · line 79
Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/libbpf_get_fd_by_id_opts.c +1
-2-/* 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" );Why would your patch change this behavior? and if it does, should it? This looks fishy.
I agree this does not look right, I think the test itself is not ideal. The behavior this test checked for has changed,
btf_get_fd_by_idwas returning EINVAL from here:``` if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID)) return -EINVAL; ```
Quoted 4 lines
That no longer fails because I added new field (
token_fd) to the attr structure.
Function now fails further down the road.//I'm on the fence whether delete this check at all or change to new error code.
Ah, ok, so I did spot a problem then. You need to add validation of valid flags and reject any unknown flag (test provides "unsupported" BPF_F_RDONLY). But let me look at the latest version first, before submitting a new version.
Quoted 6 lines
Quoted 5 lines
Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/libbpf_get_fd_by_id_opts.c +0
-1close_prog:if (fd >=0 ) --2.48.1
PATCH v4 2/4 bpf: return prog btf_id without capable check
In reply to Mykyta YatsenkoFrom: 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
@@ -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
PATCH v4 3/4 libbpf: pass BPF token from find_prog_btf_id to BPF_BTF_GET_FD_BY_ID
In reply to Mykyta YatsenkoFrom: 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 | 4 +++- tools/lib/bpf/btf.c | 15 +++++++++++++-- tools/lib/bpf/libbpf.c | 10 +++++----- tools/lib/bpf/libbpf_internal.h | 1 + 5 files changed, 24 insertions(+), 9 deletions(-)
tools/lib/bpf/bpf.c +2 -1
@@ -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 +3 -1
@@ -487,9 +487,11 @@ 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
@@ -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
@@ -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 *attacpr_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
@@ -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
Re: [PATCH bpf-next v4 3/4] libbpf: pass BPF token from find_prog_btf_id to BPF_BTF_GET_FD_BY_ID
Quoted 45 lines · Mykyta Yatsenko
From: Mykyta Yatsenko <[email protected]>
Pass BPF token from
bpf_program__set_attach_targettoBPF_BTF_GET_FD_BY_IDbpf 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.Signed-off-by Mykyta Yatsenko <[email protected]>Acked-by Yonghong Song <[email protected]>--- tools/lib/bpf/bpf.c | 3 ++- tools/lib/bpf/bpf.h | 4 +++- tools/lib/bpf/btf.c | 15 +++++++++++++-- tools/lib/bpf/libbpf.c | 10 +++++----- tools/lib/bpf/libbpf_internal.h | 1 + 5 files changed, 24 insertions(+), 9 deletions(-)tools/lib/bpf/bpf.c +2
-1@@ -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 +0
-0
tools/lib/bpf/bpf.h LIBBPF_API int bpf_link_get_next_id(__u32 start_id, __u32 *next_id); · line 493
Quoted 8 lines
tools/lib/bpf/bpf.h +2
-1@@ -487,9 +487,11 @@ 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 +
why new empty line? please keep the style consistent
Quoted 4 lines
tools/lib/bpf/bpf.h +1
-0+#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,
[...]
PATCH v4 4/4 selftests/bpf: test freplace from user namespace
In reply to Mykyta YatsenkoFrom: 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
@@ -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
@@ -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
@@ -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