Skip to content

Commit 52a22c8

Browse files
liu-song-6Kernel Patches Daemon
authored andcommitted
selftests/bpf: Extend test fs_kfuncs to cover security.bpf. xattr names
Extend test_progs fs_kfuncs to cover different xattr names. Specifically: xattr name "user.kfuncs" and "security.bpf.xxx" can be read from BPF program with kfuncs bpf_get_[file|dentry]_xattr(); while "security.bpf" and "security.selinux" cannot be read. Signed-off-by: Song Liu <song@kernel.org>
1 parent 5c1482c commit 52a22c8

File tree

2 files changed

+51
-14
lines changed

2 files changed

+51
-14
lines changed

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

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
static const char testfile[] = "/tmp/test_progs_fs_kfuncs";
1414

15-
static void test_xattr(void)
15+
static void test_get_xattr(const char *name, const char *value, bool allow_access)
1616
{
1717
struct test_get_xattr *skel = NULL;
1818
int fd = -1, err;
@@ -25,7 +25,7 @@ static void test_xattr(void)
2525
close(fd);
2626
fd = -1;
2727

28-
err = setxattr(testfile, "user.kfuncs", "hello", sizeof("hello"), 0);
28+
err = setxattr(testfile, name, value, strlen(value) + 1, 0);
2929
if (err && errno == EOPNOTSUPP) {
3030
printf("%s:SKIP:local fs doesn't support xattr (%d)\n"
3131
"To run this test, make sure /tmp filesystem supports xattr.\n",
@@ -48,16 +48,23 @@ static void test_xattr(void)
4848
goto out;
4949

5050
fd = open(testfile, O_RDONLY, 0644);
51+
5152
if (!ASSERT_GE(fd, 0, "open_file"))
5253
goto out;
5354

54-
ASSERT_EQ(skel->bss->found_xattr_from_file, 1, "found_xattr_from_file");
55-
5655
/* Trigger security_inode_getxattr */
57-
err = getxattr(testfile, "user.kfuncs", v, sizeof(v));
58-
ASSERT_EQ(err, -1, "getxattr_return");
59-
ASSERT_EQ(errno, EINVAL, "getxattr_errno");
60-
ASSERT_EQ(skel->bss->found_xattr_from_dentry, 1, "found_xattr_from_dentry");
56+
err = getxattr(testfile, name, v, sizeof(v));
57+
58+
if (allow_access) {
59+
ASSERT_EQ(err, -1, "getxattr_return");
60+
ASSERT_EQ(errno, EINVAL, "getxattr_errno");
61+
ASSERT_EQ(skel->bss->found_xattr_from_file, 1, "found_xattr_from_file");
62+
ASSERT_EQ(skel->bss->found_xattr_from_dentry, 1, "found_xattr_from_dentry");
63+
} else {
64+
ASSERT_EQ(err, strlen(value) + 1, "getxattr_return");
65+
ASSERT_EQ(skel->bss->found_xattr_from_file, 0, "found_xattr_from_file");
66+
ASSERT_EQ(skel->bss->found_xattr_from_dentry, 0, "found_xattr_from_dentry");
67+
}
6168

6269
out:
6370
close(fd);
@@ -141,8 +148,18 @@ static void test_fsverity(void)
141148

142149
void test_fs_kfuncs(void)
143150
{
144-
if (test__start_subtest("xattr"))
145-
test_xattr();
151+
/* Matches xattr_names in progs/test_get_xattr.c */
152+
if (test__start_subtest("user_xattr"))
153+
test_get_xattr("user.kfuncs", "hello", true);
154+
155+
if (test__start_subtest("security_bpf_xattr"))
156+
test_get_xattr("security.bpf.xxx", "hello", true);
157+
158+
if (test__start_subtest("security_bpf_xattr_error"))
159+
test_get_xattr("security.bpf", "hello", false);
160+
161+
if (test__start_subtest("security_selinux_xattr_error"))
162+
test_get_xattr("security.selinux", "hello", false);
146163

147164
if (test__start_subtest("fsverity"))
148165
test_fsverity();

tools/testing/selftests/bpf/progs/test_get_xattr.c

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <bpf/bpf_helpers.h>
77
#include <bpf/bpf_tracing.h>
88
#include "bpf_kfuncs.h"
9+
#include "bpf_misc.h"
910

1011
char _license[] SEC("license") = "GPL";
1112

@@ -17,20 +18,35 @@ static const char expected_value[] = "hello";
1718
char value1[32];
1819
char value2[32];
1920

21+
/* Matches caller of test_get_xattr() in prog_tests/fs_kfuncs.c */
22+
static const char * const xattr_names[] = {
23+
/* The following work. */
24+
"user.kfuncs",
25+
"security.bpf.xxx",
26+
27+
/* The following do not work. */
28+
"security.bpf",
29+
"security.selinux"
30+
};
31+
2032
SEC("lsm.s/file_open")
2133
int BPF_PROG(test_file_open, struct file *f)
2234
{
2335
struct bpf_dynptr value_ptr;
2436
__u32 pid;
25-
int ret;
37+
int ret, i;
2638

2739
pid = bpf_get_current_pid_tgid() >> 32;
2840
if (pid != monitored_pid)
2941
return 0;
3042

3143
bpf_dynptr_from_mem(value1, sizeof(value1), 0, &value_ptr);
3244

33-
ret = bpf_get_file_xattr(f, "user.kfuncs", &value_ptr);
45+
for (i = 0; i < ARRAY_SIZE(xattr_names); i++) {
46+
ret = bpf_get_file_xattr(f, xattr_names[i], &value_ptr);
47+
if (ret == sizeof(expected_value))
48+
break;
49+
}
3450
if (ret != sizeof(expected_value))
3551
return 0;
3652
if (bpf_strncmp(value1, ret, expected_value))
@@ -44,15 +60,19 @@ int BPF_PROG(test_inode_getxattr, struct dentry *dentry, char *name)
4460
{
4561
struct bpf_dynptr value_ptr;
4662
__u32 pid;
47-
int ret;
63+
int ret, i;
4864

4965
pid = bpf_get_current_pid_tgid() >> 32;
5066
if (pid != monitored_pid)
5167
return 0;
5268

5369
bpf_dynptr_from_mem(value2, sizeof(value2), 0, &value_ptr);
5470

55-
ret = bpf_get_dentry_xattr(dentry, "user.kfuncs", &value_ptr);
71+
for (i = 0; i < ARRAY_SIZE(xattr_names); i++) {
72+
ret = bpf_get_dentry_xattr(dentry, xattr_names[i], &value_ptr);
73+
if (ret == sizeof(expected_value))
74+
break;
75+
}
5676
if (ret != sizeof(expected_value))
5777
return 0;
5878
if (bpf_strncmp(value2, ret, expected_value))

0 commit comments

Comments
 (0)