Skip to content

Commit 8681156

Browse files
liu-song-6Alexei Starovoitov
authored and
Alexei Starovoitov
committed
selftests/bpf: Add tests for bpf_get_dentry_xattr
Add test for bpf_get_dentry_xattr on hook security_inode_getxattr. Verify that the kfunc can read the xattr. Also test failing getxattr from user space by returning non-zero from the LSM bpf program. Acked-by: Christian Brauner <[email protected]> Signed-off-by: Song Liu <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent ac13a42 commit 8681156

File tree

3 files changed

+50
-7
lines changed

3 files changed

+50
-7
lines changed

tools/testing/selftests/bpf/bpf_kfuncs.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ extern int bpf_dynptr_clone(const struct bpf_dynptr *ptr, struct bpf_dynptr *clo
4545

4646
/* Description
4747
* Modify the address of a AF_UNIX sockaddr.
48-
* Returns__bpf_kfunc
48+
* Returns
4949
* -EINVAL if the address size is too big or, 0 if the sockaddr was successfully modified.
5050
*/
5151
extern int bpf_sock_addr_set_sun_path(struct bpf_sock_addr_kern *sa_kern,
@@ -78,4 +78,13 @@ extern int bpf_verify_pkcs7_signature(struct bpf_dynptr *data_ptr,
7878

7979
extern bool bpf_session_is_return(void) __ksym __weak;
8080
extern __u64 *bpf_session_cookie(void) __ksym __weak;
81+
82+
struct dentry;
83+
/* Description
84+
* Returns xattr of a dentry
85+
* Returns
86+
* Error code
87+
*/
88+
extern int bpf_get_dentry_xattr(struct dentry *dentry, const char *name,
89+
struct bpf_dynptr *value_ptr) __ksym __weak;
8190
#endif

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ static void test_xattr(void)
1616
{
1717
struct test_get_xattr *skel = NULL;
1818
int fd = -1, err;
19+
int v[32];
1920

2021
fd = open(testfile, O_CREAT | O_RDONLY, 0644);
2122
if (!ASSERT_GE(fd, 0, "create_file"))
@@ -50,7 +51,13 @@ static void test_xattr(void)
5051
if (!ASSERT_GE(fd, 0, "open_file"))
5152
goto out;
5253

53-
ASSERT_EQ(skel->bss->found_xattr, 1, "found_xattr");
54+
ASSERT_EQ(skel->bss->found_xattr_from_file, 1, "found_xattr_from_file");
55+
56+
/* 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");
5461

5562
out:
5663
close(fd);

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

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,20 @@
22
/* Copyright (c) 2023 Meta Platforms, Inc. and affiliates. */
33

44
#include "vmlinux.h"
5+
#include <errno.h>
56
#include <bpf/bpf_helpers.h>
67
#include <bpf/bpf_tracing.h>
78
#include "bpf_kfuncs.h"
89

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

1112
__u32 monitored_pid;
12-
__u32 found_xattr;
13+
__u32 found_xattr_from_file;
14+
__u32 found_xattr_from_dentry;
1315

1416
static const char expected_value[] = "hello";
15-
char value[32];
17+
char value1[32];
18+
char value2[32];
1619

1720
SEC("lsm.s/file_open")
1821
int BPF_PROG(test_file_open, struct file *f)
@@ -25,13 +28,37 @@ int BPF_PROG(test_file_open, struct file *f)
2528
if (pid != monitored_pid)
2629
return 0;
2730

28-
bpf_dynptr_from_mem(value, sizeof(value), 0, &value_ptr);
31+
bpf_dynptr_from_mem(value1, sizeof(value1), 0, &value_ptr);
2932

3033
ret = bpf_get_file_xattr(f, "user.kfuncs", &value_ptr);
3134
if (ret != sizeof(expected_value))
3235
return 0;
33-
if (bpf_strncmp(value, ret, expected_value))
36+
if (bpf_strncmp(value1, ret, expected_value))
3437
return 0;
35-
found_xattr = 1;
38+
found_xattr_from_file = 1;
3639
return 0;
3740
}
41+
42+
SEC("lsm.s/inode_getxattr")
43+
int BPF_PROG(test_inode_getxattr, struct dentry *dentry, char *name)
44+
{
45+
struct bpf_dynptr value_ptr;
46+
__u32 pid;
47+
int ret;
48+
49+
pid = bpf_get_current_pid_tgid() >> 32;
50+
if (pid != monitored_pid)
51+
return 0;
52+
53+
bpf_dynptr_from_mem(value2, sizeof(value2), 0, &value_ptr);
54+
55+
ret = bpf_get_dentry_xattr(dentry, "user.kfuncs", &value_ptr);
56+
if (ret != sizeof(expected_value))
57+
return 0;
58+
if (bpf_strncmp(value2, ret, expected_value))
59+
return 0;
60+
found_xattr_from_dentry = 1;
61+
62+
/* return non-zero to fail getxattr from user space */
63+
return -EINVAL;
64+
}

0 commit comments

Comments
 (0)