Skip to content

Commit f2d0ffe

Browse files
anakryikoAlexei Starovoitov
authored andcommitted
selftests/bpf: utilize string values for delegate_xxx mount options
Use both hex-based and string-based way to specify delegate mount options for BPF FS. Acked-by: John Fastabend <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent c5707b2 commit f2d0ffe

File tree

1 file changed

+32
-20
lines changed
  • tools/testing/selftests/bpf/prog_tests

1 file changed

+32
-20
lines changed

tools/testing/selftests/bpf/prog_tests/token.c

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,22 @@ static int restore_priv_caps(__u64 old_caps)
6666
return cap_enable_effective(old_caps, NULL);
6767
}
6868

69-
static int set_delegate_mask(int fs_fd, const char *key, __u64 mask)
69+
static int set_delegate_mask(int fs_fd, const char *key, __u64 mask, const char *mask_str)
7070
{
7171
char buf[32];
7272
int err;
7373

74-
snprintf(buf, sizeof(buf), "0x%llx", (unsigned long long)mask);
74+
if (!mask_str) {
75+
if (mask == ~0ULL) {
76+
mask_str = "any";
77+
} else {
78+
snprintf(buf, sizeof(buf), "0x%llx", (unsigned long long)mask);
79+
mask_str = buf;
80+
}
81+
}
82+
7583
err = sys_fsconfig(fs_fd, FSCONFIG_SET_STRING, key,
76-
mask == ~0ULL ? "any" : buf, 0);
84+
mask_str, 0);
7785
if (err < 0)
7886
err = -errno;
7987
return err;
@@ -86,6 +94,10 @@ struct bpffs_opts {
8694
__u64 maps;
8795
__u64 progs;
8896
__u64 attachs;
97+
const char *cmds_str;
98+
const char *maps_str;
99+
const char *progs_str;
100+
const char *attachs_str;
89101
};
90102

91103
static int create_bpffs_fd(void)
@@ -104,16 +116,16 @@ static int materialize_bpffs_fd(int fs_fd, struct bpffs_opts *opts)
104116
int mnt_fd, err;
105117

106118
/* set up token delegation mount options */
107-
err = set_delegate_mask(fs_fd, "delegate_cmds", opts->cmds);
119+
err = set_delegate_mask(fs_fd, "delegate_cmds", opts->cmds, opts->cmds_str);
108120
if (!ASSERT_OK(err, "fs_cfg_cmds"))
109121
return err;
110-
err = set_delegate_mask(fs_fd, "delegate_maps", opts->maps);
122+
err = set_delegate_mask(fs_fd, "delegate_maps", opts->maps, opts->maps_str);
111123
if (!ASSERT_OK(err, "fs_cfg_maps"))
112124
return err;
113-
err = set_delegate_mask(fs_fd, "delegate_progs", opts->progs);
125+
err = set_delegate_mask(fs_fd, "delegate_progs", opts->progs, opts->progs_str);
114126
if (!ASSERT_OK(err, "fs_cfg_progs"))
115127
return err;
116-
err = set_delegate_mask(fs_fd, "delegate_attachs", opts->attachs);
128+
err = set_delegate_mask(fs_fd, "delegate_attachs", opts->attachs, opts->attachs_str);
117129
if (!ASSERT_OK(err, "fs_cfg_attachs"))
118130
return err;
119131

@@ -295,13 +307,13 @@ static void child(int sock_fd, struct bpffs_opts *opts, child_callback_fn callba
295307
}
296308

297309
/* ensure unprivileged child cannot set delegation options */
298-
err = set_delegate_mask(fs_fd, "delegate_cmds", 0x1);
310+
err = set_delegate_mask(fs_fd, "delegate_cmds", 0x1, NULL);
299311
ASSERT_EQ(err, -EPERM, "delegate_cmd_eperm");
300-
err = set_delegate_mask(fs_fd, "delegate_maps", 0x1);
312+
err = set_delegate_mask(fs_fd, "delegate_maps", 0x1, NULL);
301313
ASSERT_EQ(err, -EPERM, "delegate_maps_eperm");
302-
err = set_delegate_mask(fs_fd, "delegate_progs", 0x1);
314+
err = set_delegate_mask(fs_fd, "delegate_progs", 0x1, NULL);
303315
ASSERT_EQ(err, -EPERM, "delegate_progs_eperm");
304-
err = set_delegate_mask(fs_fd, "delegate_attachs", 0x1);
316+
err = set_delegate_mask(fs_fd, "delegate_attachs", 0x1, NULL);
305317
ASSERT_EQ(err, -EPERM, "delegate_attachs_eperm");
306318

307319
/* pass BPF FS context object to parent */
@@ -325,22 +337,22 @@ static void child(int sock_fd, struct bpffs_opts *opts, child_callback_fn callba
325337
}
326338

327339
/* ensure unprivileged child cannot reconfigure to set delegation options */
328-
err = set_delegate_mask(fs_fd, "delegate_cmds", ~0ULL);
340+
err = set_delegate_mask(fs_fd, "delegate_cmds", 0, "any");
329341
if (!ASSERT_EQ(err, -EPERM, "delegate_cmd_eperm_reconfig")) {
330342
err = -EINVAL;
331343
goto cleanup;
332344
}
333-
err = set_delegate_mask(fs_fd, "delegate_maps", ~0ULL);
345+
err = set_delegate_mask(fs_fd, "delegate_maps", 0, "any");
334346
if (!ASSERT_EQ(err, -EPERM, "delegate_maps_eperm_reconfig")) {
335347
err = -EINVAL;
336348
goto cleanup;
337349
}
338-
err = set_delegate_mask(fs_fd, "delegate_progs", ~0ULL);
350+
err = set_delegate_mask(fs_fd, "delegate_progs", 0, "any");
339351
if (!ASSERT_EQ(err, -EPERM, "delegate_progs_eperm_reconfig")) {
340352
err = -EINVAL;
341353
goto cleanup;
342354
}
343-
err = set_delegate_mask(fs_fd, "delegate_attachs", ~0ULL);
355+
err = set_delegate_mask(fs_fd, "delegate_attachs", 0, "any");
344356
if (!ASSERT_EQ(err, -EPERM, "delegate_attachs_eperm_reconfig")) {
345357
err = -EINVAL;
346358
goto cleanup;
@@ -933,8 +945,8 @@ void test_token(void)
933945
{
934946
if (test__start_subtest("map_token")) {
935947
struct bpffs_opts opts = {
936-
.cmds = 1ULL << BPF_MAP_CREATE,
937-
.maps = 1ULL << BPF_MAP_TYPE_STACK,
948+
.cmds_str = "map_create",
949+
.maps_str = "stack",
938950
};
939951

940952
subtest_userns(&opts, userns_map_create);
@@ -948,9 +960,9 @@ void test_token(void)
948960
}
949961
if (test__start_subtest("prog_token")) {
950962
struct bpffs_opts opts = {
951-
.cmds = 1ULL << BPF_PROG_LOAD,
952-
.progs = 1ULL << BPF_PROG_TYPE_XDP,
953-
.attachs = 1ULL << BPF_XDP,
963+
.cmds_str = "PROG_LOAD",
964+
.progs_str = "XDP",
965+
.attachs_str = "xdp",
954966
};
955967

956968
subtest_userns(&opts, userns_prog_load);

0 commit comments

Comments
 (0)