Skip to content

Commit f63ea3e

Browse files
author
Alexei Starovoitov
committed
Merge branch 'add-bpf_get_dentry_xattr'
Song Liu says: ==================== Add bpf_get_dentry_xattr Add a kfunc to read xattr from dentry. Also add selftest for the new kfunc. Changes v3 => v4: 1. Fix selftest build. V3: https://lore.kernel.org/bpf/[email protected]/T/#u Changes v2 => v3: 1. Move the kfuncs to fs/bpf_fs_kfuncs.c. 2. Fix selftests build error on s390. (Alexei) v2: https://lore.kernel.org/bpf/[email protected]/T/#u Changes v1 => v2: 1. Remove 3 kfuncs that are ready yet. v1: https://lore.kernel.org/linux-fsdevel/[email protected]/T/#u ==================== Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
2 parents 6e083ab + 8681156 commit f63ea3e

File tree

5 files changed

+112
-75
lines changed

5 files changed

+112
-75
lines changed

fs/bpf_fs_kfuncs.c

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <linux/fs.h>
99
#include <linux/file.h>
1010
#include <linux/mm.h>
11+
#include <linux/xattr.h>
1112

1213
__bpf_kfunc_start_defs();
1314

@@ -92,13 +93,74 @@ __bpf_kfunc int bpf_path_d_path(struct path *path, char *buf, size_t buf__sz)
9293
return len;
9394
}
9495

96+
/**
97+
* bpf_get_dentry_xattr - get xattr of a dentry
98+
* @dentry: dentry to get xattr from
99+
* @name__str: name of the xattr
100+
* @value_p: output buffer of the xattr value
101+
*
102+
* Get xattr *name__str* of *dentry* and store the output in *value_ptr*.
103+
*
104+
* For security reasons, only *name__str* with prefix "user." is allowed.
105+
*
106+
* Return: 0 on success, a negative value on error.
107+
*/
108+
__bpf_kfunc int bpf_get_dentry_xattr(struct dentry *dentry, const char *name__str,
109+
struct bpf_dynptr *value_p)
110+
{
111+
struct bpf_dynptr_kern *value_ptr = (struct bpf_dynptr_kern *)value_p;
112+
struct inode *inode = d_inode(dentry);
113+
u32 value_len;
114+
void *value;
115+
int ret;
116+
117+
if (WARN_ON(!inode))
118+
return -EINVAL;
119+
120+
if (strncmp(name__str, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
121+
return -EPERM;
122+
123+
value_len = __bpf_dynptr_size(value_ptr);
124+
value = __bpf_dynptr_data_rw(value_ptr, value_len);
125+
if (!value)
126+
return -EINVAL;
127+
128+
ret = inode_permission(&nop_mnt_idmap, inode, MAY_READ);
129+
if (ret)
130+
return ret;
131+
return __vfs_getxattr(dentry, inode, name__str, value, value_len);
132+
}
133+
134+
/**
135+
* bpf_get_file_xattr - get xattr of a file
136+
* @file: file to get xattr from
137+
* @name__str: name of the xattr
138+
* @value_p: output buffer of the xattr value
139+
*
140+
* Get xattr *name__str* of *file* and store the output in *value_ptr*.
141+
*
142+
* For security reasons, only *name__str* with prefix "user." is allowed.
143+
*
144+
* Return: 0 on success, a negative value on error.
145+
*/
146+
__bpf_kfunc int bpf_get_file_xattr(struct file *file, const char *name__str,
147+
struct bpf_dynptr *value_p)
148+
{
149+
struct dentry *dentry;
150+
151+
dentry = file_dentry(file);
152+
return bpf_get_dentry_xattr(dentry, name__str, value_p);
153+
}
154+
95155
__bpf_kfunc_end_defs();
96156

97157
BTF_KFUNCS_START(bpf_fs_kfunc_set_ids)
98158
BTF_ID_FLAGS(func, bpf_get_task_exe_file,
99159
KF_ACQUIRE | KF_TRUSTED_ARGS | KF_RET_NULL)
100160
BTF_ID_FLAGS(func, bpf_put_file, KF_RELEASE)
101161
BTF_ID_FLAGS(func, bpf_path_d_path, KF_TRUSTED_ARGS)
162+
BTF_ID_FLAGS(func, bpf_get_dentry_xattr, KF_SLEEPABLE | KF_TRUSTED_ARGS)
163+
BTF_ID_FLAGS(func, bpf_get_file_xattr, KF_SLEEPABLE | KF_TRUSTED_ARGS)
102164
BTF_KFUNCS_END(bpf_fs_kfunc_set_ids)
103165

104166
static int bpf_fs_kfuncs_filter(const struct bpf_prog *prog, u32 kfunc_id)

kernel/trace/bpf_trace.c

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
#include <linux/key.h>
2525
#include <linux/verification.h>
2626
#include <linux/namei.h>
27-
#include <linux/fileattr.h>
2827

2928
#include <net/bpf_sk_storage.h>
3029

@@ -1439,73 +1438,6 @@ static int __init bpf_key_sig_kfuncs_init(void)
14391438
late_initcall(bpf_key_sig_kfuncs_init);
14401439
#endif /* CONFIG_KEYS */
14411440

1442-
/* filesystem kfuncs */
1443-
__bpf_kfunc_start_defs();
1444-
1445-
/**
1446-
* bpf_get_file_xattr - get xattr of a file
1447-
* @file: file to get xattr from
1448-
* @name__str: name of the xattr
1449-
* @value_p: output buffer of the xattr value
1450-
*
1451-
* Get xattr *name__str* of *file* and store the output in *value_ptr*.
1452-
*
1453-
* For security reasons, only *name__str* with prefix "user." is allowed.
1454-
*
1455-
* Return: 0 on success, a negative value on error.
1456-
*/
1457-
__bpf_kfunc int bpf_get_file_xattr(struct file *file, const char *name__str,
1458-
struct bpf_dynptr *value_p)
1459-
{
1460-
struct bpf_dynptr_kern *value_ptr = (struct bpf_dynptr_kern *)value_p;
1461-
struct dentry *dentry;
1462-
u32 value_len;
1463-
void *value;
1464-
int ret;
1465-
1466-
if (strncmp(name__str, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
1467-
return -EPERM;
1468-
1469-
value_len = __bpf_dynptr_size(value_ptr);
1470-
value = __bpf_dynptr_data_rw(value_ptr, value_len);
1471-
if (!value)
1472-
return -EINVAL;
1473-
1474-
dentry = file_dentry(file);
1475-
ret = inode_permission(&nop_mnt_idmap, dentry->d_inode, MAY_READ);
1476-
if (ret)
1477-
return ret;
1478-
return __vfs_getxattr(dentry, dentry->d_inode, name__str, value, value_len);
1479-
}
1480-
1481-
__bpf_kfunc_end_defs();
1482-
1483-
BTF_KFUNCS_START(fs_kfunc_set_ids)
1484-
BTF_ID_FLAGS(func, bpf_get_file_xattr, KF_SLEEPABLE | KF_TRUSTED_ARGS)
1485-
BTF_KFUNCS_END(fs_kfunc_set_ids)
1486-
1487-
static int bpf_get_file_xattr_filter(const struct bpf_prog *prog, u32 kfunc_id)
1488-
{
1489-
if (!btf_id_set8_contains(&fs_kfunc_set_ids, kfunc_id))
1490-
return 0;
1491-
1492-
/* Only allow to attach from LSM hooks, to avoid recursion */
1493-
return prog->type != BPF_PROG_TYPE_LSM ? -EACCES : 0;
1494-
}
1495-
1496-
static const struct btf_kfunc_id_set bpf_fs_kfunc_set = {
1497-
.owner = THIS_MODULE,
1498-
.set = &fs_kfunc_set_ids,
1499-
.filter = bpf_get_file_xattr_filter,
1500-
};
1501-
1502-
static int __init bpf_fs_kfuncs_init(void)
1503-
{
1504-
return register_btf_kfunc_id_set(BPF_PROG_TYPE_LSM, &bpf_fs_kfunc_set);
1505-
}
1506-
1507-
late_initcall(bpf_fs_kfuncs_init);
1508-
15091441
static const struct bpf_func_proto *
15101442
bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
15111443
{

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)