Skip to content

Commit 571683f

Browse files
mattbobrowskiKernel Patches Daemon
authored andcommitted
selftests/bpf: add new negative tests for xattr related BPF kfuncs
Add a set of negative tests to verify the newly enforced constraints applied to xattr related BPF kfuncs. Signed-off-by: Matt Bobrowski <mattbobrowski@google.com>
1 parent 1f0b0f0 commit 571683f

2 files changed

Lines changed: 33 additions & 4 deletions

File tree

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,18 +115,18 @@ static void validate_bar_removed(struct test_set_remove_xattr *skel)
115115
static void test_set_remove_xattr(void)
116116
{
117117
struct test_set_remove_xattr *skel = NULL;
118-
int fd = -1, err;
118+
int fd, err;
119119

120120
fd = open(testfile, O_CREAT | O_RDONLY, 0644);
121121
if (!ASSERT_GE(fd, 0, "create_file"))
122122
return;
123123

124124
close(fd);
125-
fd = -1;
126125

127126
skel = test_set_remove_xattr__open_and_load();
128127
if (!ASSERT_OK_PTR(skel, "test_set_remove_xattr__open_and_load"))
129-
return;
128+
goto out;
129+
130130

131131
/* Set security.bpf.foo to "hello" */
132132
err = setxattr(testfile, skel->rodata->xattr_foo, value_foo, strlen(value_foo) + 1, 0);
@@ -188,8 +188,16 @@ static void test_set_remove_xattr(void)
188188
ASSERT_TRUE(skel->bss->locked_remove_security_selinux_fail,
189189
"locked_remove_security_selinux_fail");
190190

191+
ASSERT_EQ(skel->bss->ret_code_name_empty, -ERANGE,
192+
"ret_code_name_empty");
193+
ASSERT_EQ(skel->bss->ret_code_name_too_long, -ERANGE,
194+
"ret_code_name_too_long");
195+
ASSERT_EQ(skel->bss->ret_code_value_too_large, -E2BIG,
196+
"ret_code_value_too_large");
197+
ASSERT_EQ(skel->bss->ret_code_invalid_flags, -EINVAL,
198+
"ret_code_invalid_flags");
199+
191200
out:
192-
close(fd);
193201
test_set_remove_xattr__destroy(skel);
194202
remove(testfile);
195203
}

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ static const char xattr_selinux[] = "security.selinux";
1717
char value_bar[] = "world";
1818
char read_value[32];
1919

20+
const char xattr_negative[] = "security.bpf.negative";
21+
int ret_code_name_empty;
22+
int ret_code_name_too_long;
23+
int ret_code_value_too_large;
24+
int ret_code_invalid_flags;
25+
const char long_name[] = "security.bpf.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
26+
char large_value[65537];
27+
2028
bool set_security_bpf_bar_success;
2129
bool remove_security_bpf_bar_success;
2230
bool set_security_selinux_fail;
@@ -73,6 +81,19 @@ int BPF_PROG(test_inode_getxattr, struct dentry *dentry, char *name)
7381
remove_security_selinux_fail = true;
7482
}
7583

84+
bpf_dynptr_from_mem(read_value, sizeof(read_value), 0, &value_ptr);
85+
ret_code_name_empty = bpf_get_dentry_xattr(dentry, "", &value_ptr);
86+
ret_code_name_too_long =
87+
bpf_get_dentry_xattr(dentry, long_name, &value_ptr);
88+
89+
bpf_dynptr_from_mem(large_value, sizeof(large_value), 0, &value_ptr);
90+
ret_code_value_too_large =
91+
bpf_set_dentry_xattr(dentry, xattr_negative, &value_ptr, 0);
92+
93+
bpf_dynptr_from_mem(value_bar, sizeof(value_bar), 0, &value_ptr);
94+
ret_code_invalid_flags = bpf_set_dentry_xattr(dentry, xattr_negative,
95+
&value_ptr, 0xFFFF);
96+
7697
return 0;
7798
}
7899

0 commit comments

Comments
 (0)