[PATCH RESEND v3 bpf-next 09/14] selftests/bpf: add BPF token-enabled BPF_BTF_LOAD selftest
Andrii Nakryiko
andrii at kernel.org
Thu Jun 29 05:18:27 UTC 2023
Add a simple test validating that BTF loading can be done from
unprivileged process through delegated BPF token.
Signed-off-by: Andrii Nakryiko <andrii at kernel.org>
---
.../testing/selftests/bpf/prog_tests/token.c | 60 +++++++++++++++++++
1 file changed, 60 insertions(+)
diff --git a/tools/testing/selftests/bpf/prog_tests/token.c b/tools/testing/selftests/bpf/prog_tests/token.c
index 0f832f9178a2..113cd4786a70 100644
--- a/tools/testing/selftests/bpf/prog_tests/token.c
+++ b/tools/testing/selftests/bpf/prog_tests/token.c
@@ -142,10 +142,70 @@ static void subtest_map_token(void)
ASSERT_OK(restore_priv_caps(old_caps), "restore_caps");
}
+static void subtest_btf_token(void)
+{
+ LIBBPF_OPTS(bpf_token_create_opts, token_opts);
+ LIBBPF_OPTS(bpf_btf_load_opts, btf_opts);
+ int token_fd = 0, btf_fd = 0, err;
+ const void *raw_btf_data;
+ struct btf *btf = NULL;
+ __u32 raw_btf_size;
+ __u64 old_caps = 0;
+
+ /* create BPF token allowing BPF_BTF_LOAD command */
+ token_opts.allowed_cmds = 1ULL << BPF_BTF_LOAD;
+ err = bpf_token_create(-EBADF, TOKEN_PATH, &token_opts);
+ if (!ASSERT_OK(err, "token_create"))
+ return;
+
+ /* drop privileges to test token_fd passing */
+ if (!ASSERT_OK(drop_priv_caps(&old_caps), "drop_caps"))
+ goto cleanup;
+
+ token_fd = bpf_obj_get(TOKEN_PATH);
+ if (!ASSERT_GT(token_fd, 0, "token_get"))
+ goto cleanup;
+
+ btf = btf__new_empty();
+ if (!ASSERT_OK_PTR(btf, "empty_btf"))
+ goto cleanup;
+
+ ASSERT_GT(btf__add_int(btf, "int", 4, 0), 0, "int_type");
+
+ raw_btf_data = btf__raw_data(btf, &raw_btf_size);
+ if (!ASSERT_OK_PTR(raw_btf_data, "raw_btf_data"))
+ goto cleanup;
+
+ /* validate we can successfully load new BTF with token */
+ btf_opts.token_fd = token_fd;
+ btf_fd = bpf_btf_load(raw_btf_data, raw_btf_size, &btf_opts);
+ if (!ASSERT_GT(btf_fd, 0, "btf_fd"))
+ goto cleanup;
+ close(btf_fd);
+
+ /* now validate that we *cannot* load BTF without token */
+ btf_opts.token_fd = 0;
+ btf_fd = bpf_btf_load(raw_btf_data, raw_btf_size, &btf_opts);
+ if (!ASSERT_EQ(btf_fd, -EPERM, "btf_fd_eperm"))
+ goto cleanup;
+
+cleanup:
+ btf__free(btf);
+ if (btf_fd > 0)
+ close(btf_fd);
+ if (token_fd)
+ close(token_fd);
+ unlink(TOKEN_PATH);
+ if (old_caps)
+ ASSERT_OK(restore_priv_caps(old_caps), "restore_caps");
+}
+
void test_token(void)
{
if (test__start_subtest("token_create"))
subtest_token_create();
if (test__start_subtest("map_token"))
subtest_map_token();
+ if (test__start_subtest("btf_token"))
+ subtest_btf_token();
}
--
2.34.1
More information about the Linux-security-module-archive
mailing list