Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f3238a1

Browse files
liu-song-6Kernel Patches Daemon
authored andcommittedJun 23, 2025
bpf: Introduce bpf_cgroup_read_xattr to read xattr of cgroup's node
BPF programs, such as LSM and sched_ext, would benefit from tags on cgroups. One common practice to apply such tags is to set xattrs on cgroupfs folders. Introduce kfunc bpf_cgroup_read_xattr, which allows reading cgroup's xattr. Note that, we already have bpf_get_[file|dentry]_xattr. However, these two APIs are not ideal for reading cgroupfs xattrs, because: 1) These two APIs only works in sleepable contexts; 2) There is no kfunc that matches current cgroup to cgroupfs dentry. bpf_cgroup_read_xattr is generic and can be useful for many program types. It is also safe, because it requires trusted or rcu protected argument (KF_RCU). Therefore, we make it available to all program types. Signed-off-by: Song Liu <song@kernel.org> Acked-by: Tejun Heo <tj@kernel.org>
1 parent 0ce98ba commit f3238a1

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed
 

‎fs/bpf_fs_kfuncs.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <linux/fs.h>
1010
#include <linux/fsnotify.h>
1111
#include <linux/file.h>
12+
#include <linux/kernfs.h>
1213
#include <linux/mm.h>
1314
#include <linux/xattr.h>
1415

@@ -322,6 +323,39 @@ __bpf_kfunc int bpf_remove_dentry_xattr(struct dentry *dentry, const char *name_
322323
return ret;
323324
}
324325

326+
#ifdef CONFIG_CGROUPS
327+
/**
328+
* bpf_cgroup_read_xattr - read xattr of a cgroup's node in cgroupfs
329+
* @cgroup: cgroup to get xattr from
330+
* @name__str: name of the xattr
331+
* @value_p: output buffer of the xattr value
332+
*
333+
* Get xattr *name__str* of *cgroup* and store the output in *value_ptr*.
334+
*
335+
* For security reasons, only *name__str* with prefix "user." is allowed.
336+
*
337+
* Return: length of the xattr value on success, a negative value on error.
338+
*/
339+
__bpf_kfunc int bpf_cgroup_read_xattr(struct cgroup *cgroup, const char *name__str,
340+
struct bpf_dynptr *value_p)
341+
{
342+
struct bpf_dynptr_kern *value_ptr = (struct bpf_dynptr_kern *)value_p;
343+
u32 value_len;
344+
void *value;
345+
346+
/* Only allow reading "user.*" xattrs */
347+
if (strncmp(name__str, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
348+
return -EPERM;
349+
350+
value_len = __bpf_dynptr_size(value_ptr);
351+
value = __bpf_dynptr_data_rw(value_ptr, value_len);
352+
if (!value)
353+
return -EINVAL;
354+
355+
return kernfs_xattr_get(cgroup->kn, name__str, value, value_len);
356+
}
357+
#endif /* CONFIG_CGROUPS */
358+
325359
__bpf_kfunc_end_defs();
326360

327361
BTF_KFUNCS_START(bpf_fs_kfunc_set_ids)

‎kernel/bpf/helpers.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3397,6 +3397,9 @@ BTF_ID_FLAGS(func, bpf_iter_dmabuf_next, KF_ITER_NEXT | KF_RET_NULL | KF_SLEEPAB
33973397
BTF_ID_FLAGS(func, bpf_iter_dmabuf_destroy, KF_ITER_DESTROY | KF_SLEEPABLE)
33983398
#endif
33993399
BTF_ID_FLAGS(func, __bpf_trap)
3400+
#ifdef CONFIG_CGROUPS
3401+
BTF_ID_FLAGS(func, bpf_cgroup_read_xattr, KF_RCU)
3402+
#endif
34003403
BTF_KFUNCS_END(common_btf_ids)
34013404

34023405
static const struct btf_kfunc_id_set common_kfunc_set = {

0 commit comments

Comments
 (0)
Please sign in to comment.