Skip to content

Commit fa4e5af

Browse files
liu-song-6Alexei Starovoitov
authored andcommitted
bpf: Move bpf_get_file_xattr to fs/bpf_fs_kfuncs.c
We are putting all fs kfuncs in fs/bpf_fs_kfuncs.c. Move existing bpf_get_file_xattr to it. Signed-off-by: Song Liu <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 6e083ab commit fa4e5af

File tree

2 files changed

+38
-68
lines changed

2 files changed

+38
-68
lines changed

fs/bpf_fs_kfuncs.c

Lines changed: 38 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,50 @@ __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_file_xattr - get xattr of a file
98+
* @file: file 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 *file* 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_file_xattr(struct file *file, 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 dentry *dentry;
113+
u32 value_len;
114+
void *value;
115+
int ret;
116+
117+
if (strncmp(name__str, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
118+
return -EPERM;
119+
120+
value_len = __bpf_dynptr_size(value_ptr);
121+
value = __bpf_dynptr_data_rw(value_ptr, value_len);
122+
if (!value)
123+
return -EINVAL;
124+
125+
dentry = file_dentry(file);
126+
ret = inode_permission(&nop_mnt_idmap, dentry->d_inode, MAY_READ);
127+
if (ret)
128+
return ret;
129+
return __vfs_getxattr(dentry, dentry->d_inode, name__str, value, value_len);
130+
}
131+
95132
__bpf_kfunc_end_defs();
96133

97134
BTF_KFUNCS_START(bpf_fs_kfunc_set_ids)
98135
BTF_ID_FLAGS(func, bpf_get_task_exe_file,
99136
KF_ACQUIRE | KF_TRUSTED_ARGS | KF_RET_NULL)
100137
BTF_ID_FLAGS(func, bpf_put_file, KF_RELEASE)
101138
BTF_ID_FLAGS(func, bpf_path_d_path, KF_TRUSTED_ARGS)
139+
BTF_ID_FLAGS(func, bpf_get_file_xattr, KF_SLEEPABLE | KF_TRUSTED_ARGS)
102140
BTF_KFUNCS_END(bpf_fs_kfunc_set_ids)
103141

104142
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
{

0 commit comments

Comments
 (0)