Skip to content

Commit 498c1a4

Browse files
liu-song-6Kernel Patches Daemon
authored andcommitted
selftests/bpf: Test kfuncs that set and remove xattr from BPF programs
Two sets of tests are added to exercise the not _locked and _locked version of the kfuncs. For both tests, user space accesses xattr security.bpf.foo on a testfile. The BPF program is triggered by user space access (on LSM hook inode_[set|get]_xattr) and sets or removes xattr security.bpf.bar. Then user space then validates that xattr security.bpf.bar is set or removed as expected. Note that, in both tests, the BPF programs use the not _locked kfuncs. The verifier picks the proper kfuncs based on the calling context. Signed-off-by: Song Liu <song@kernel.org>
1 parent 45937ce commit 498c1a4

File tree

3 files changed

+263
-0
lines changed

3 files changed

+263
-0
lines changed

tools/testing/selftests/bpf/bpf_kfuncs.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,9 @@ struct dentry;
8787
*/
8888
extern int bpf_get_dentry_xattr(struct dentry *dentry, const char *name,
8989
struct bpf_dynptr *value_ptr) __ksym __weak;
90+
91+
extern int bpf_set_dentry_xattr(struct dentry *dentry, const char *name__str,
92+
const struct bpf_dynptr *value_p, int flags) __ksym __weak;
93+
extern int bpf_remove_dentry_xattr(struct dentry *dentry, const char *name__str) __ksym __weak;
94+
9095
#endif

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

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <unistd.h>
99
#include <test_progs.h>
1010
#include "test_get_xattr.skel.h"
11+
#include "test_set_remove_xattr.skel.h"
1112
#include "test_fsverity.skel.h"
1213

1314
static const char testfile[] = "/tmp/test_progs_fs_kfuncs";
@@ -72,6 +73,127 @@ static void test_get_xattr(const char *name, const char *value, bool allow_acces
7273
remove(testfile);
7374
}
7475

76+
/* xattr value we will set to security.bpf.foo */
77+
static const char value_foo[] = "hello";
78+
79+
static void read_and_validate_foo(struct test_set_remove_xattr *skel)
80+
{
81+
char value_out[32];
82+
int err;
83+
84+
err = getxattr(testfile, skel->rodata->xattr_foo, value_out, sizeof(value_out));
85+
ASSERT_EQ(err, sizeof(value_foo), "getxattr size foo");
86+
ASSERT_EQ(strncmp(value_out, value_foo, sizeof(value_foo)), 0, "strncmp value_foo");
87+
}
88+
89+
static void set_foo(struct test_set_remove_xattr *skel)
90+
{
91+
ASSERT_OK(setxattr(testfile, skel->rodata->xattr_foo, value_foo, strlen(value_foo) + 1, 0),
92+
"setxattr foo");
93+
}
94+
95+
static void validate_bar_match(struct test_set_remove_xattr *skel)
96+
{
97+
char value_out[32];
98+
int err;
99+
100+
err = getxattr(testfile, skel->rodata->xattr_bar, value_out, sizeof(value_out));
101+
ASSERT_EQ(err, sizeof(skel->data->value_bar), "getxattr size bar");
102+
ASSERT_EQ(strncmp(value_out, skel->data->value_bar, sizeof(skel->data->value_bar)), 0,
103+
"strncmp value_bar");
104+
}
105+
106+
static void validate_bar_removed(struct test_set_remove_xattr *skel)
107+
{
108+
char value_out[32];
109+
int err;
110+
111+
err = getxattr(testfile, skel->rodata->xattr_bar, value_out, sizeof(value_out));
112+
ASSERT_LT(err, 0, "getxattr size bar should fail");
113+
}
114+
115+
static void test_set_remove_xattr(void)
116+
{
117+
struct test_set_remove_xattr *skel = NULL;
118+
int fd = -1, err;
119+
120+
fd = open(testfile, O_CREAT | O_RDONLY, 0644);
121+
if (!ASSERT_GE(fd, 0, "create_file"))
122+
return;
123+
124+
close(fd);
125+
fd = -1;
126+
127+
skel = test_set_remove_xattr__open_and_load();
128+
if (!ASSERT_OK_PTR(skel, "test_set_remove_xattr__open_and_load"))
129+
return;
130+
131+
/* Set security.bpf.foo to "hello" */
132+
err = setxattr(testfile, skel->rodata->xattr_foo, value_foo, strlen(value_foo) + 1, 0);
133+
if (err && errno == EOPNOTSUPP) {
134+
printf("%s:SKIP:local fs doesn't support xattr (%d)\n"
135+
"To run this test, make sure /tmp filesystem supports xattr.\n",
136+
__func__, errno);
137+
test__skip();
138+
goto out;
139+
}
140+
141+
if (!ASSERT_OK(err, "setxattr"))
142+
goto out;
143+
144+
skel->bss->monitored_pid = getpid();
145+
err = test_set_remove_xattr__attach(skel);
146+
if (!ASSERT_OK(err, "test_set_remove_xattr__attach"))
147+
goto out;
148+
149+
/* First, test not _locked version of the kfuncs with getxattr. */
150+
151+
/* Read security.bpf.foo and trigger test_inode_getxattr. This
152+
* bpf program will set security.bpf.bar to "world".
153+
*/
154+
read_and_validate_foo(skel);
155+
validate_bar_match(skel);
156+
157+
/* Read security.bpf.foo and trigger test_inode_getxattr again.
158+
* This will remove xattr security.bpf.bar.
159+
*/
160+
read_and_validate_foo(skel);
161+
validate_bar_removed(skel);
162+
163+
ASSERT_TRUE(skel->bss->set_security_bpf_bar_success, "set_security_bpf_bar_success");
164+
ASSERT_TRUE(skel->bss->remove_security_bpf_bar_success, "remove_security_bpf_bar_success");
165+
ASSERT_TRUE(skel->bss->set_security_selinux_fail, "set_security_selinux_fail");
166+
ASSERT_TRUE(skel->bss->remove_security_selinux_fail, "remove_security_selinux_fail");
167+
168+
/* Second, test _locked version of the kfuncs, with setxattr */
169+
170+
/* Set security.bpf.foo and trigger test_inode_setxattr. This
171+
* bpf program will set security.bpf.bar to "world".
172+
*/
173+
set_foo(skel);
174+
validate_bar_match(skel);
175+
176+
/* Set security.bpf.foo and trigger test_inode_setxattr again.
177+
* This will remove xattr security.bpf.bar.
178+
*/
179+
set_foo(skel);
180+
validate_bar_removed(skel);
181+
182+
ASSERT_TRUE(skel->bss->locked_set_security_bpf_bar_success,
183+
"locked_set_security_bpf_bar_success");
184+
ASSERT_TRUE(skel->bss->locked_remove_security_bpf_bar_success,
185+
"locked_remove_security_bpf_bar_success");
186+
ASSERT_TRUE(skel->bss->locked_set_security_selinux_fail,
187+
"locked_set_security_selinux_fail");
188+
ASSERT_TRUE(skel->bss->locked_remove_security_selinux_fail,
189+
"locked_remove_security_selinux_fail");
190+
191+
out:
192+
close(fd);
193+
test_set_remove_xattr__destroy(skel);
194+
remove(testfile);
195+
}
196+
75197
#ifndef SHA256_DIGEST_SIZE
76198
#define SHA256_DIGEST_SIZE 32
77199
#endif
@@ -161,6 +283,9 @@ void test_fs_kfuncs(void)
161283
if (test__start_subtest("security_selinux_xattr_error"))
162284
test_get_xattr("security.selinux", "hello", false);
163285

286+
if (test__start_subtest("set_remove_xattr"))
287+
test_set_remove_xattr();
288+
164289
if (test__start_subtest("fsverity"))
165290
test_fsverity();
166291
}
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */
3+
4+
#include "vmlinux.h"
5+
#include <errno.h>
6+
#include <bpf/bpf_tracing.h>
7+
#include "bpf_kfuncs.h"
8+
#include "bpf_misc.h"
9+
10+
char _license[] SEC("license") = "GPL";
11+
12+
__u32 monitored_pid;
13+
14+
const char xattr_foo[] = "security.bpf.foo";
15+
const char xattr_bar[] = "security.bpf.bar";
16+
const char xattr_linux[] = "security.selinux";
17+
char value_bar[] = "world";
18+
char read_value[32];
19+
20+
bool set_security_bpf_bar_success;
21+
bool remove_security_bpf_bar_success;
22+
bool set_security_selinux_fail;
23+
bool remove_security_selinux_fail;
24+
25+
char name_buf[32];
26+
27+
static inline bool name_match_foo(const char *name)
28+
{
29+
bpf_probe_read_kernel(name_buf, sizeof(name_buf), name);
30+
31+
return !bpf_strncmp(name_buf, sizeof(xattr_foo), xattr_foo);
32+
}
33+
34+
/* Test bpf_set_dentry_xattr and bpf_remove_dentry_xattr */
35+
SEC("lsm.s/inode_getxattr")
36+
int BPF_PROG(test_inode_getxattr, struct dentry *dentry, char *name)
37+
{
38+
struct bpf_dynptr value_ptr;
39+
__u32 pid;
40+
int ret;
41+
42+
pid = bpf_get_current_pid_tgid() >> 32;
43+
if (pid != monitored_pid)
44+
return 0;
45+
46+
/* Only do the following for security.bpf.foo */
47+
if (!name_match_foo(name))
48+
return 0;
49+
50+
bpf_dynptr_from_mem(read_value, sizeof(read_value), 0, &value_ptr);
51+
52+
/* read security.bpf.bar */
53+
ret = bpf_get_dentry_xattr(dentry, xattr_bar, &value_ptr);
54+
55+
if (ret < 0) {
56+
/* If security.bpf.bar doesn't exist, set it */
57+
bpf_dynptr_from_mem(value_bar, sizeof(value_bar), 0, &value_ptr);
58+
59+
ret = bpf_set_dentry_xattr(dentry, xattr_bar, &value_ptr, 0);
60+
if (!ret)
61+
set_security_bpf_bar_success = true;
62+
ret = bpf_set_dentry_xattr(dentry, xattr_linux, &value_ptr, 0);
63+
if (ret)
64+
set_security_selinux_fail = true;
65+
} else {
66+
/* If security.bpf.bar exists, remove it */
67+
ret = bpf_remove_dentry_xattr(dentry, xattr_bar);
68+
if (!ret)
69+
remove_security_bpf_bar_success = true;
70+
71+
ret = bpf_remove_dentry_xattr(dentry, xattr_linux);
72+
if (ret)
73+
remove_security_selinux_fail = true;
74+
}
75+
76+
return 0;
77+
}
78+
79+
bool locked_set_security_bpf_bar_success;
80+
bool locked_remove_security_bpf_bar_success;
81+
bool locked_set_security_selinux_fail;
82+
bool locked_remove_security_selinux_fail;
83+
84+
/* Test bpf_set_dentry_xattr_locked and bpf_remove_dentry_xattr_locked.
85+
* It not necessary to differentiate the _locked version and the
86+
* not-_locked version in the BPF program. The verifier will fix them up
87+
* properly.
88+
*/
89+
SEC("lsm.s/inode_setxattr")
90+
int BPF_PROG(test_inode_setxattr, struct mnt_idmap *idmap,
91+
struct dentry *dentry, const char *name,
92+
const void *value, size_t size, int flags)
93+
{
94+
struct bpf_dynptr value_ptr;
95+
__u32 pid;
96+
int ret;
97+
98+
pid = bpf_get_current_pid_tgid() >> 32;
99+
if (pid != monitored_pid)
100+
return 0;
101+
102+
/* Only do the following for security.bpf.foo */
103+
if (!name_match_foo(name))
104+
return 0;
105+
106+
bpf_dynptr_from_mem(read_value, sizeof(read_value), 0, &value_ptr);
107+
108+
/* read security.bpf.bar */
109+
ret = bpf_get_dentry_xattr(dentry, xattr_bar, &value_ptr);
110+
111+
if (ret < 0) {
112+
/* If security.bpf.bar doesn't exist, set it */
113+
bpf_dynptr_from_mem(value_bar, sizeof(value_bar), 0, &value_ptr);
114+
115+
ret = bpf_set_dentry_xattr(dentry, xattr_bar, &value_ptr, 0);
116+
if (!ret)
117+
locked_set_security_bpf_bar_success = true;
118+
ret = bpf_set_dentry_xattr(dentry, xattr_linux, &value_ptr, 0);
119+
if (ret)
120+
locked_set_security_selinux_fail = true;
121+
} else {
122+
/* If security.bpf.bar exists, remove it */
123+
ret = bpf_remove_dentry_xattr(dentry, xattr_bar);
124+
if (!ret)
125+
locked_remove_security_bpf_bar_success = true;
126+
127+
ret = bpf_remove_dentry_xattr(dentry, xattr_linux);
128+
if (ret)
129+
locked_remove_security_selinux_fail = true;
130+
}
131+
132+
return 0;
133+
}

0 commit comments

Comments
 (0)