bpf: Add simple xattr support to bpffs
Daniel Borkmann
maintainerPATCH v2 1/1 bpf: Add simple xattr support to bpffs
Add support for extended attributes on bpffs inodes so that user space and BPF LSM programs can attach metadata, for example, a content hash or a security label - to a pinned object or directory. BPF LSM or user space tooling can then uniformly look at this (e.g. security.bpf.*) in similar way to other fs'es. The store is in-memory and non-persistent:
it lives only for the lifetime of the mount, like everything else in bpffs. The modelling is similar to tmpfs.
bpffs serves the trusted.* and security.* namespaces; user.* is left unsupported. As bpffs is FS_USERNS_MOUNT, security.* is reachable by the unprivileged mounter in a user namespace, and thus we are using the simple_xattr_set_limited infra there (trusted.* needs global CAP_SYS_ADMIN).
bpf_fill_super() is open-coded instead of using simple_fill_super(), because the root inode must now be allocated through bpf_fs_alloc_inode() i.e. carry the bpf_fs_inode wrapper and come from the right cache - which requires s_op (and s_xattr) to be installed before the first inode is created. While at it, also harden s_iflags with SB_I_NOEXEC and SB_I_NODEV.
bpf_fs_listxattr() is only reachable through the filesystem via i_op->listxattr, so the BPF token inode is left untouched. Name-based fsetxattr()/fgetxattr() on a token fd still work since the get/set handlers are installed at the superblock.
---
v1->v2:
- Rebase against vfs-7.2.xattr for routing via vfs given it contains simple xattr rework (Christian, Alexei)
- Use simple_xattr_set_limited for security.* namespace (sashiko)
- Dropped user.* namespace (sashiko)
include/linux/bpf.h | 3 + kernel/bpf/inode.c | 248 +++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 234 insertions(+), 17 deletions(-)
include/linux/bpf.h +3 -0
@@ -31,6 +31,7 @@#include <linux/static_call.h> #include <linux/memcontrol.h> #include <linux/cfi.h> +#include <linux/xattr.h> #include <asm/rqspinlock.h> struct bpf_verifier_env ;
@@ -1918,6 +1919,8 @@ struct bpf_mount_opts {u64 delegate_maps;u64 delegate_progs;u64 delegate_attachs; + +struct simple_xattr_cache xa_cache; };struct bpf_token {
kernel/bpf/inode.c +231 -17
373 lines not on this page · raw message
Signature · 1 line
2.43.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
New issues:
- [Critical] The patch relies on an out-of-tree or obsolete version of the VFS simple_xattr API, leading to hard compilation failures and potential memory corruption.
- [High] The per-inode xattr quota limits can be underflowed by removing initial security xattrs, allowing unprivileged users to bypass the SIMPLE_XATTR_MAX_SIZE and SIMPLE_XATTR_MAX_NR limits.
Pre-existing issues:
- [High] bpf_symlink allocates symlink target strings without memcg accounting, allowing an unprivileged user to bypass memory limits and exhaust kernel memory. --
commit 5eef577d514964df263d306b47c480c2c453a101
Author: Daniel Borkmann <[email protected]>
bpf: Add simple xattr support to bpffs
This commit adds extended attribute support to bpffs inodes. It allows user space and BPF LSM programs to attach metadata like security labels to pinned objects or directories. The trusted and security namespaces are supported using in-memory simple_xattr infrastructure.
Quoted 3 lines
include/linux/bpf.h +0
-0
[ ... ]
include/linux/bpf.h struct bpf_mount_opts { · line 1924
Quoted 7 lines
include/linux/bpf.h +2
-0@@ -1918,6 +1919,8 @@ struct bpf_mount_opts {u64 delegate_maps;u64 delegate_progs;u64 delegate_attachs; + +struct simple_xattr_cache xa_cache; };
[Severity: Critical]
Does this rely on an out-of-tree version of the VFS simple_xattr API? The struct simple_xattr_cache does not exist in the mainline tree, which causes a compilation failure here.
Quoted 3 lines
kernel/bpf/inode.c +0
-0
[ ... ]
kernel/bpf/inode.c enum bpf_type { · line 40
Quoted 5 lines
kernel/bpf/inode.c +5
-0+struct bpf_fs_inode { +struct list_head xattrs; +struct simple_xattr_limits xlimits; +struct inode vfs_inode; +};
[Severity: Critical]
Could this cause memory corruption due to a struct layout mismatch? The mainline simple_xattr API requires struct simple_xattrs (which embeds an rhashtable) rather than a list_head.
[ ... ]
kernel/bpf/inode.c bpf_lookup(struct inode *dir, struct dentry *dentry, unsigned flags) · line 434
Quoted 9 lines
static int bpf_symlink(struct
mnt_idmap*idmap, struct inode *dir,kernel/bpf/inode.c +3
-1struct dentry *dentry, const char *target) { - char *link = kstrdup(target, GFP_USER | __GFP_NOWARN); struct inode *inode; +char *link; +int ret; + link =kstrdup (target,GFP_USER | __GFP_NOWARN);
[Severity: High]
This isn't a bug introduced by this patch, but does this code bypass memory cgroup accounting?
The string allocation in bpf_symlink() lacks __GFP_ACCOUNT. Because bpffs is an in-memory filesystem with no maximum limits, an unprivileged user inside a container with write access could repeatedly create symlinks with large targets and exhaust host kernel memory without triggering their memory limits.
[ ... ]
Quoted 5 lines
kernel/bpf/inode.c +5
-0+static int bpf_fs_xattr_set (const struct xattr_handler *handler, +struct mnt_idmap *idmap,struct dentry *unused, +struct inode *inode,const char *name, +const void *value,size_t size,int flags) +{
[ ... ]
kernel/bpf/inode.c static int bpf_show_options(struct seq_file *m, struct dentry *root) · line 885
Quoted 4 lines
kernel/bpf/inode.c +4
-0+if (handler->flags ==BPF_FS_XATTR_SECURITY ) { + err =simple_xattr_set_limited (&opts->xa_cache, &bi->xattrs, + &bi->xlimits, name, value, size, + flags);
[Severity: Critical]
Is the argument count correct for simple_xattr_set_limited()? The current upstream API expects 6 arguments, but this code passes 7 (including the obsolete xa_cache).
[Severity: High]
Can this lead to an atomic underflow of the xattr quota limits?
Since bpffs is FS_USERNS_MOUNT, an unprivileged user in a user namespace can modify security.* xattrs. If they remove or replace an initially seeded xattr, simple_xattr_set_limited() will decrement the limits. Because those limits were never incremented during initialization, this causes an underflow. The counters would wrap to negative values, effectively disabling the SIMPLE_XATTR_MAX_SIZE limit and allowing unbounded memory allocation.
[ ... ]
Quoted 3 lines
kernel/bpf/inode.c +3
-0+static int bpf_fs_initxattrs (struct inode *inode, +const struct xattr *xattr_array,void *fs_info) +{
[ ... ]
Quoted 2 lines
kernel/bpf/inode.c +2
-0+for (xattr = xattr_array; xattr->name !=NULL ; xattr++) { +CLASS (simple_xattr, new_xattr)(xattr->value, xattr->value_len);
[ ... ]
kernel/bpf/inode.c static int bpf_show_options(struct seq_file *m, struct dentry *root) · line 944
Quoted 2 lines
kernel/bpf/inode.c +2
-0+if (simple_xattr_add (&opts->xa_cache, &bi->xattrs, new_xattr)) +break ;
[Severity: High]
Does this code bypass the quota limits increment?
Initial LSM security xattrs added directly via simple_xattr_add() in bpf_fs_initxattrs() do not increment the quota counters in bi->xlimits. This sets up the underflow condition mentioned in bpf_fs_xattr_set() when an unprivileged user later replaces or removes these xattrs.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Daniel Borkmann
maintainerQuoted 4 lines · On 5/30/26 12:52 AM, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
New issues:
- [Critical] The patch relies on an out-of-tree or obsolete version of the VFSsimple_xattrAPI, leading to hard compilation failures and potential memory corruption.
nope, vfs-7.2.xattr branch of vfs.git
Quoted 1 line
- [High] The per-inode xattr quota limits can be underflowed by removing initial security xattrs, allowing unprivileged users to bypass the
SIMPLE_XATTR_MAX_SIZEandSIMPLE_XATTR_MAX_NRlimits.
I'll take a look.
Quoted 2 lines
Pre-existing issues:
- [High]bpf_symlinkallocates symlink target strings without memcg accounting, allowing an unprivileged user to bypass memory limits and exhaust kernel memory.
Independent, but I'll also look into this one.