]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
bpf/selftests: Add selftests for token info
authorTao Chen <chen.dylane@linux.dev>
Wed, 16 Jul 2025 13:46:54 +0000 (21:46 +0800)
committerAlexei Starovoitov <ast@kernel.org>
Thu, 17 Jul 2025 01:38:05 +0000 (18:38 -0700)
A previous change added bpf_token_info to get token info with
bpf_get_obj_info_by_fd, this patch adds a new test for token info.

 #461/12  token/bpf_token_info:OK

Acked-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Tao Chen <chen.dylane@linux.dev>
Link: https://lore.kernel.org/r/20250716134654.1162635-2-chen.dylane@linux.dev
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
tools/testing/selftests/bpf/prog_tests/token.c

index cfc032b910c40c3b9a66c1315556b64fe1af5606..b81dde283052e5c81b9f11665b76065db0a888a9 100644 (file)
@@ -1047,6 +1047,41 @@ err_out:
 
 #define bit(n) (1ULL << (n))
 
+static int userns_bpf_token_info(int mnt_fd, struct token_lsm *lsm_skel)
+{
+       int err, token_fd = -1;
+       struct bpf_token_info info;
+       u32 len = sizeof(struct bpf_token_info);
+
+       /* create BPF token from BPF FS mount */
+       token_fd = bpf_token_create(mnt_fd, NULL);
+       if (!ASSERT_GT(token_fd, 0, "token_create")) {
+               err = -EINVAL;
+               goto cleanup;
+       }
+
+       memset(&info, 0, len);
+       err = bpf_obj_get_info_by_fd(token_fd, &info, &len);
+       if (!ASSERT_ERR(err, "bpf_obj_get_token_info"))
+               goto cleanup;
+       if (!ASSERT_EQ(info.allowed_cmds, bit(BPF_MAP_CREATE), "token_info_cmds_map_create")) {
+               err = -EINVAL;
+               goto cleanup;
+       }
+       if (!ASSERT_EQ(info.allowed_progs, bit(BPF_PROG_TYPE_XDP), "token_info_progs_xdp")) {
+               err = -EINVAL;
+               goto cleanup;
+       }
+
+       /* The BPF_PROG_TYPE_EXT is not set in token */
+       if (ASSERT_EQ(info.allowed_progs, bit(BPF_PROG_TYPE_EXT), "token_info_progs_ext"))
+               err = -EINVAL;
+
+cleanup:
+       zclose(token_fd);
+       return err;
+}
+
 void test_token(void)
 {
        if (test__start_subtest("map_token")) {
@@ -1150,4 +1185,13 @@ void test_token(void)
 
                subtest_userns(&opts, userns_obj_priv_implicit_token_envvar);
        }
+       if (test__start_subtest("bpf_token_info")) {
+               struct bpffs_opts opts = {
+                       .cmds = bit(BPF_MAP_CREATE),
+                       .progs = bit(BPF_PROG_TYPE_XDP),
+                       .attachs = ~0ULL,
+               };
+
+               subtest_userns(&opts, userns_bpf_token_info);
+       }
 }