Skip to contentSkip to conversation

bpf: Add simple xattr support to bpffs

PATCH Applied Daniel Borkmann 7 participants 15 messages Jun 2 – Jul 31

Subsystems (5)
lore
Share & tools
lore

Daniel Borkmann

author maintainer
#0 lore raw

PATCH v3 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.

For security.* namespace, we use simple_xattr_set_limited() but there was no simple_xattr_add_limited() API yet which was needed in bpf_fs_initxattrs() to avoid underflows in the accounting. The symlink target is freed in bpf_free_inode() rather than in bpf_destroy_inode() so that it is released only after an RCU grace period, as an RCU path walk following the symlink may still dereference inode->i_link in security_inode_follow_link(). Lastly, the bpf_symlink() allocated the symlink target is switched to GFP_KERNEL_ACCOUNT, so the string is charged to the caller's memcg.

Cc Christian Brauner <[email protected]>

---
v2->v3:
- Add simple_xattr_add_limited and use it for bpf_fs_initxattrs (sashiko)
- Use GFP_KERNEL_ACCOUNT for bpf_symlink (sashiko)
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)

 fs/xattr.c            |  33 ++++++
 include/linux/bpf.h   |   3 +
 include/linux/xattr.h |   4 +
 kernel/bpf/inode.c    | 256 ++++++++++++++++++++++++++++++++++++++----
 4 files changed, 277 insertions(+), 19 deletions(-)
fs/xattr.c +33 -0
index af28029239ce..abe5457b6d0a 100644
@@ -1675,6 +1675,39 @@ int simple_xattr_add(struct simple_xattr_cache *cache, struct list_head *xattrs,
 	return 0;
 }
 
+/**
+ * simple_xattr_add_limited - add an xattr object, charging per-inode limits
+ * @cache: anchor for the hash table
+ * @xattrs: the header of the xattr object
+ * @limits: per-inode limit counters
+ * @new_xattr: the xattr object to add
+ *
+ * Like simple_xattr_add(), but also accounts @new_xattr against @limits so
+ * that a later removal or replacement of it through simple_xattr_set_limited()
+ * decrements counters that were actually incremented, rather than underflowing
+ * them. Use this instead of simple_xattr_add() when seeding initial xattrs
+ * that share a namespace with the limited set/remove path.
+ *
+ * Return: On success zero is returned. On failure a negative error code is
+ * returned.
+ */
+int simple_xattr_add_limited(struct simple_xattr_cache *cache,
+			     struct list_head *xattrs,
+			     struct simple_xattr_limits *limits,
+			     struct simple_xattr *new_xattr)
+{
+	int err;
+
+	err = simple_xattr_limits_inc(limits, new_xattr->size);
+	if (err)
+		return err;
+
+	err = simple_xattr_add(cache, xattrs, new_xattr);
+	if (err)
+		simple_xattr_limits_dec(limits, new_xattr->size);
+	return err;
+}
+
 /**
  * simple_xattrs_free - free xattrs
  * @cache: anchor for the hash table
include/linux/bpf.h +3 -0
index b4b703c90ca9..434ba91401c6 100644
@@ -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 {
include/linux/xattr.h +4 -0
index 7aaaf4f8aff5..54ac3cbc133f 100644
@@ -155,6 +155,10 @@ ssize_t simple_xattr_list(struct inode *inode, struct list_head *xattrs,
 			  char *buffer, size_t size);
 int simple_xattr_add(struct simple_xattr_cache *cache, struct list_head *xattrs,
 		     struct simple_xattr *new_xattr);
+int simple_xattr_add_limited(struct simple_xattr_cache *cache,
+			     struct list_head *xattrs,
+			     struct simple_xattr_limits *limits,
+			     struct simple_xattr *new_xattr);
 int xattr_list_one(char **buffer, ssize_t *remaining_size, const char *name);
 
 void simple_xattr_cache_cleanup(struct simple_xattr_cache *cache);
kernel/bpf/inode.c +237 -19

379 lines not on this page · raw message

Signature · 1 line
2.43.0
#1 lore raw
Does simple_xattr_add() accept three arguments here? Looking at the existing simple_xattr_add() signature in include/linux/xattr.h:
Quoted 30 lines
fs/xattr.c +22 -0
index 09ecbaaa1660..a7abb39f0041 100644
@@ -1689,6 +1689,39 @@ static void simple_xattr_ht_free(void *ptr, void *arg)
 	simple_xattr_free(xattr);
 }
 
+/**
+ * simple_xattr_add_limited - add an xattr object, charging per-inode limits
+ * @cache: anchor for the hash table
+ * @xattrs: the header of the xattr object
+ * @limits: per-inode limit counters
+ * @new_xattr: the xattr object to add
+ *
+ * Like simple_xattr_add(), but also accounts @new_xattr against @limits so
+ * that a later removal or replacement of it through simple_xattr_set_limited()
+ * decrements counters that were actually incremented, rather than underflowing
+ * them. Use this instead of simple_xattr_add() when seeding initial xattrs
+ * that share a namespace with the limited set/remove path.
+ *
+ * Return: On success zero is returned. On failure a negative error code is
+ * returned.
+ */
+int simple_xattr_add_limited(struct simple_xattr_cache *cache,
+			     struct list_head *xattrs,
+			     struct simple_xattr_limits *limits,
+			     struct simple_xattr *new_xattr)
+{
+	int err;

fs/xattr.c static void simple_xattr_ht_free(void *ptr, void *arg) · line 1723

Quoted 10 lines
fs/xattr.c +10 -0
+
+	err = simple_xattr_limits_inc(limits, new_xattr->size);
+	if (err)
+		return err;
+
+	err = simple_xattr_add(cache, xattrs, new_xattr);
+	if (err)
+		simple_xattr_limits_dec(limits, new_xattr->size);
+	return err;
+}

Does simple_xattr_add() accept three arguments here? Looking at the existing simple_xattr_add() signature in include/linux/xattr.h:

int simple_xattr_add(struct simple_xattrs *xattrs,

		     struct simple_xattr *new_xattr);

The in-tree function takes only two arguments with a single struct simple_xattrs pointer. However, this new code is calling it with three arguments of different types:

err = simple_xattr_add(cache, xattrs, new_xattr);

where cache is a struct simple_xattr_cache * and xattrs is a struct list_head *. The existing implementation uses a rhashtable internally (struct simple_xattrs contains struct rhashtable ht), not a separate cache and list_head pair. Won't this fail to compile due to the argument count and type mismatches?

Quoted 13 lines
include/linux/bpf.h +1 -0
index b4b703c90ca9..434ba91401c6 100644
@@ -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;

include/linux/bpf.h struct bpf_mount_opts { · line 1924

Quoted 6 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;
 };

Is struct simple_xattr_cache defined anywhere? Looking through include/linux/xattr.h, the VFS simple_xattr infrastructure defines struct simple_xattrs, struct simple_xattr, and struct simple_xattr_limits, but there is no struct simple_xattr_cache type. Embedding an undefined struct by value would be a compile error. Are the VFS xattr headers expected to provide this type?

include/linux/xattr.h ssize_t simple_xattr_list(struct inode *inode, struct simple_xattrs *xattrs, · line 161

Quoted 13 lines
include/linux/xattr.h +4 -0
index 8b6601367eae..5ee49199eb22 100644
@@ -154,6 +154,10 @@ ssize_t simple_xattr_list(struct inode *inode, struct simple_xattrs *xattrs,
 			  char *buffer, size_t size);
 int simple_xattr_add(struct simple_xattrs *xattrs,
 		     struct simple_xattr *new_xattr);
+int simple_xattr_add_limited(struct simple_xattr_cache *cache,
+			     struct list_head *xattrs,
+			     struct simple_xattr_limits *limits,
+			     struct simple_xattr *new_xattr);
 int xattr_list_one(char **buffer, ssize_t *remaining_size, const char *name);

Following up on the previous point - this declaration references struct simple_xattr_cache in its first parameter, but that type is not defined in this header or anywhere else in the tree. Won't this create an incomplete type that causes compilation failures when the function implementation attempts to dereference the cache pointer?

Looking at other simple_xattr functions like simple_xattr_get/set/list, they all take struct simple_xattrs * as their first parameter rather than a separate cache pointer and list_head. Was there a separate patch series that introduces the cache-based API that this depends on?

Quoted 4 lines
kernel/bpf/inode.c +0 -0
index 25c06a011825..c3f79b5a2f8c 100644

[ ... ]

Quoted 3 lines
kernel/bpf/inode.c +0 -0
@@ -762,22 +822,147 @@ static int bpf_show_options(struct seq_file *m, struct dentry *root)
 	return 0;
 }

[ ... ]

kernel/bpf/inode.c static int bpf_show_options(struct seq_file *m, struct dentry *root)

Quoted 13 lines

static void bpf_destroy_inode(struct inode *inode) {

kernel/bpf/inode.c +3 -3
+	struct bpf_mount_opts *opts = inode->i_sb->s_fs_info;
+	struct bpf_fs_inode *bi = BPF_FS_I(inode);
 	enum bpf_type type;
 
-	if (S_ISLNK(inode->i_mode))
-		kfree(inode->i_link);
 	if (!bpf_inode_type(inode, &type))
 		bpf_any_put(inode->i_private, type);
-	free_inode_nonrcu(inode);
+	simple_xattrs_free(&opts->xa_cache, &bi->xattrs, NULL);
 }

Does simple_xattrs_free() take three arguments? Looking at the in-tree implementation, simple_xattrs_free takes two parameters:

void simple_xattrs_free(struct simple_xattrs *xattrs, size_t *freed_space)

But this call is passing three arguments: a cache pointer, a list_head pointer, and NULL. This appears to be the same API mismatch - won't this fail to compile?

Similarly, throughout the bpffs xattr handlers (bpf_fs_xattr_get, bpf_fs_xattr_set, bpf_fs_listxattr, bpf_fs_initxattrs), the code is calling simple_xattr functions with an extra leading &opts->xa_cache parameter:

simple_xattr_get(&opts->xa_cache, &bi->xattrs, name, value, size) simple_xattr_set(&opts->xa_cache, &bi->xattrs, name, ...) simple_xattr_set_limited(&opts->xa_cache, &bi->xattrs, &bi->xlimits, ...) simple_xattr_list(inode, &BPF_FS_I(inode)->xattrs, buf, size) simple_xattr_add_limited(&opts->xa_cache, &bi->xattrs, &bi->xlimits, ...)

where bi->xattrs is a struct list_head. However, the existing VFS functions expect struct simple_xattrs * as their first parameter (a structure containing an rhashtable, not a list_head). For comparison, fs/kernfs/inode.c uses simple_xattr_set_limited like:

simple_xattr_set_limited(xattrs, &attrs->xattr_limits, ...)

with just the struct simple_xattrs pointer. Are all these calls expecting a different API than what currently exists in fs/xattr.c?

[ ... ]

kernel/bpf/inode.c static void bpf_kill_super(struct super_block *sb) · line 1271

Quoted 7 lines
kernel/bpf/inode.c +1 -0
@@ -1068,6 +1266,7 @@ static void bpf_kill_super(struct super_block *sb)
 	struct bpf_mount_opts *opts = sb->s_fs_info;
 
 	kill_anon_super(sb);
+	simple_xattr_cache_cleanup(&opts->xa_cache);
 	kfree(opts);
 }

Is simple_xattr_cache_cleanup() defined? A tree-wide search doesn't show any definition of this function - only this call site. Won't this cause an undefined reference at link time?

All of these issues appear related: the patch adds a cache-based simple_xattr API (struct simple_xattr_cache, simple_xattr_add_limited with cache parameter, simple_xattr_cache_cleanup) and calls it throughout the bpffs xattr handlers, but the VFS side (fs/xattr.c and include/linux/xattr.h) only partially implements this API. The struct simple_xattr_cache type is never defined, simple_xattr_add_limited's implementation calls the two-argument simple_xattr_add with three arguments, and simple_xattr_cache_cleanup is never defined. Does this depend on a separate VFS xattr rework that hasn't been included?

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/26835946342

author maintainer
#2 lore raw
Quoted 44 lines · On 6/2/26 8:07 PM, [email protected] wrote:
Quoted 30 lines
fs/xattr.c +22 -0
index 09ecbaaa1660..a7abb39f0041 100644
@@ -1689,6 +1689,39 @@ static void simple_xattr_ht_free(void *ptr, void *arg)
  	simple_xattr_free(xattr);
  }
 
+/**
+ * simple_xattr_add_limited - add an xattr object, charging per-inode limits
+ * @cache: anchor for the hash table
+ * @xattrs: the header of the xattr object
+ * @limits: per-inode limit counters
+ * @new_xattr: the xattr object to add
+ *
+ * Like simple_xattr_add(), but also accounts @new_xattr against @limits so
+ * that a later removal or replacement of it through simple_xattr_set_limited()
+ * decrements counters that were actually incremented, rather than underflowing
+ * them. Use this instead of simple_xattr_add() when seeding initial xattrs
+ * that share a namespace with the limited set/remove path.
+ *
+ * Return: On success zero is returned. On failure a negative error code is
+ * returned.
+ */
+int simple_xattr_add_limited(struct simple_xattr_cache *cache,
+			     struct list_head *xattrs,
+			     struct simple_xattr_limits *limits,
+			     struct simple_xattr *new_xattr)
+{
+	int err;

fs/xattr.c static void simple_xattr_ht_free(void *ptr, void *arg) · line 1723

Quoted 10 lines
fs/xattr.c +10 -0
+
+	err = simple_xattr_limits_inc(limits, new_xattr->size);
+	if (err)
+		return err;
+
+	err = simple_xattr_add(cache, xattrs, new_xattr);
+	if (err)
+		simple_xattr_limits_dec(limits, new_xattr->size);
+	return err;
+}

Does simple_xattr_add() accept three arguments here? Looking at the existing simple_xattr_add() signature in include/linux/xattr.h:

Quoted 2 lines

int simple_xattr_add(struct simple_xattrs *xattrs,

		     struct simple_xattr *new_xattr);

(this is on top of https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git/log/?h=vfs-7.2.xattr)

maintainer Applied
#3 lore raw
In reply to Daniel Borkmann
Quoted 9 lines · Daniel Borkmann

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.

[...]

Applied to the vfs-7.2.xattr branch of the vfs/vfs.git tree. Patches in the vfs-7.2.xattr branch should appear in linux-next soon.

Please report any outstanding bugs that were missed during review in a new review to the original patch series allowing us to drop it.

It's encouraged to provide Acked-bys and Reviewed-bys even though the patch has now been applied. If possible patch trailers will be updated.

Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. If in doubt, please check the listed branch.

tree:   https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git
branch: vfs-7.2.xattr

[1/1] bpf: Add simple xattr support to bpffs

      https://git.kernel.org/vfs/vfs/c/a146500e1144
#4 lore raw
Quoted 20 lines · Christian Brauner
Quoted 9 lines · Daniel Borkmann

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.

[...]

Applied to the vfs-7.2.xattr branch of the vfs/vfs.git tree. Patches in the vfs-7.2.xattr branch should appear in linux-next soon.

Please report any outstanding bugs that were missed during review in a new review to the original patch series allowing us to drop it.

It's encouraged to provide Acked-bys and Reviewed-bys even though the patch has now been applied. If possible patch trailers will be updated.

Quoted 8 lines

Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. If in doubt, please check the listed branch.

tree:   https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git
branch: vfs-7.2.xattr

[1/1] bpf: Add simple xattr support to bpffs

      https://git.kernel.org/vfs/vfs/c/a146500e1144

Hey Christian / Daniel,

I'm getting a regression with this patch when using genfs for bpffs. It seems like calling selinux_inode_init_security() with !SBLABEL_MNT will leave the isec->initialized before doing the EOPNOTSUPP exit.

The genfs path is later skipped because inode_doinit_with_dentry() sees the inode initialized and I get denials in userspace.

I don't know if this would be correct but doing the check for SBLABEL_MNT before marking the isec as initialized worked for me.

Any thoughts? Should I send a formal patch?

Carlos Llamas

---

security/selinux/hooks.c +4 -4
index 0f704380a8c8..9cb1724d2bbc 100644
@@ -2980,6 +2980,10 @@ static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
 	if (rc)
 		return rc;
 
+	if (!selinux_initialized() ||
+	    !(sbsec->flags & SBLABEL_MNT))
+		return -EOPNOTSUPP;
+
 	/* Possibly defer initialization to selinux_complete_init. */
 	if (sbsec->flags & SE_SBINITIALIZED) {
 		struct inode_security_struct *isec = selinux_inode(inode);
@@ -2988,10 +2992,6 @@ static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
 		isec->initialized = LABEL_INITIALIZED;
 	}
 
-	if (!selinux_initialized() ||
-	    !(sbsec->flags & SBLABEL_MNT))
-		return -EOPNOTSUPP;
-
 	xattr = lsm_get_xattr_slot(xattrs, xattr_count);
 	if (xattr) {
 		rc = security_sid_to_context_force(newsid,
author maintainer
#5 lore raw
Quoted 36 lines · Carlos Llamas
Quoted 20 lines · Christian Brauner
Quoted 9 lines · Daniel Borkmann

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.

[...]

Applied to the vfs-7.2.xattr branch of the vfs/vfs.git tree. Patches in the vfs-7.2.xattr branch should appear in linux-next soon.

Please report any outstanding bugs that were missed during review in a new review to the original patch series allowing us to drop it.

It's encouraged to provide Acked-bys and Reviewed-bys even though the patch has now been applied. If possible patch trailers will be updated.

Quoted 8 lines

Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. If in doubt, please check the listed branch.

tree:   https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git
branch: vfs-7.2.xattr

[1/1] bpf: Add simple xattr support to bpffs

       https://git.kernel.org/vfs/vfs/c/a146500e1144

Hey Christian / Daniel,

I'm getting a regression with this patch when using genfs for bpffs. It seems like calling selinux_inode_init_security() with !SBLABEL_MNT will leave the isec->initialized before doing the EOPNOTSUPP exit.

Quoted 7 lines

The genfs path is later skipped because inode_doinit_with_dentry() sees the inode initialized and I get denials in userspace.

I don't know if this would be correct but doing the check for SBLABEL_MNT before marking the isec as initialized worked for me.

Any thoughts? Should I send a formal patch?

Yeap, sorry for the delay; fwiw looks reasonable.

Quoted 29 lines

Carlos Llamas

---

security/selinux/hooks.c +4 -4
index 0f704380a8c8..9cb1724d2bbc 100644
@@ -2980,6 +2980,10 @@ static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
  	if (rc)
  		return rc;
 
+	if (!selinux_initialized() ||
+	    !(sbsec->flags & SBLABEL_MNT))
+		return -EOPNOTSUPP;
+
  	/* Possibly defer initialization to selinux_complete_init. */
  	if (sbsec->flags & SE_SBINITIALIZED) {
  		struct inode_security_struct *isec = selinux_inode(inode);
@@ -2988,10 +2992,6 @@ static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
  		isec->initialized = LABEL_INITIALIZED;
  	}
 
-	if (!selinux_initialized() ||
-	    !(sbsec->flags & SBLABEL_MNT))
-		return -EOPNOTSUPP;
-
  	xattr = lsm_get_xattr_slot(xattrs, xattr_count);
  	if (xattr) {
  		rc = security_sid_to_context_force(newsid,
#6 lore raw

PATCH 1/1 selinux: bpf: check SBLABEL_MNT before isec init

selinux_inode_init_security() marks the isec as initialized before checking if mount labeling is supported (SBLABEL_MNT). This was fine until commit 9722955b5430 ("bpf: Add simple xattr support to bpffs"), where genfscon bpffs mounts fail the SBLABEL_MNT check as expected (no xattrs) and yet leave the isec->initialized. This breaks subsequent calls to inode_doinit_with_dentry().

Do the SBLABEL_MNT check before the inode security is initialized.

Fixes 9722955b5430 ("bpf: Add simple xattr support to bpffs")
---
 security/selinux/hooks.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
security/selinux/hooks.c +4 -4
index 8d6945edae7a..09a12eb8652c 100644
@@ -2980,6 +2980,10 @@ static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
 	if (rc)
 		return rc;
 
+	if (!selinux_initialized() ||
+	    !(sbsec->flags & SBLABEL_MNT))
+		return -EOPNOTSUPP;
+
 	/* Possibly defer initialization to selinux_complete_init. */
 	if (sbsec->flags & SE_SBINITIALIZED) {
 		struct inode_security_struct *isec = selinux_inode(inode);
@@ -2988,10 +2992,6 @@ static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
 		isec->initialized = LABEL_INITIALIZED;
 	}
 
-	if (!selinux_initialized() ||
-	    !(sbsec->flags & SBLABEL_MNT))
-		return -EOPNOTSUPP;
-
 	xattr = lsm_get_xattr_slot(xattrs, xattr_count);
 	if (xattr) {
 		rc = security_sid_to_context_force(newsid,
Signature · 1 line
2.55.0.508.g3f0d502094-goog
#7 lore raw

Re: [PATCH] selinux: bpf: check SBLABEL_MNT before isec init

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 7 lines

selinux_inode_init_security() marks the isec as initialized before checking if mount labeling is supported (SBLABEL_MNT). This was fine until commit 9722955b5430 ("bpf: Add simple xattr support to bpffs"), where genfscon bpffs mounts fail the SBLABEL_MNT check as expected (no xattrs) and yet leave the isec->initialized. This breaks subsequent calls to inode_doinit_with_dentry().

Quoted 6 lines

Do the SBLABEL_MNT check before the inode security is initialized.

Fixes 9722955b5430 ("bpf: Add simple xattr support to bpffs")

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer Acked-by on v1 1/1
#8 lore raw

Re: [PATCH] selinux: bpf: check SBLABEL_MNT before isec init

In reply to Carlos Llamas
Quoted 8 lines · Carlos Llamas

selinux_inode_init_security() marks the isec as initialized before checking if mount labeling is supported (SBLABEL_MNT). This was fine until commit 9722955b5430 ("bpf: Add simple xattr support to bpffs"), where genfscon bpffs mounts fail the SBLABEL_MNT check as expected (no xattrs) and yet leave the isec->initialized. This breaks subsequent calls to inode_doinit_with_dentry().

Quoted 6 lines

Do the SBLABEL_MNT check before the inode security is initialized.

Fixes 9722955b5430 ("bpf: Add simple xattr support to bpffs")
maintainer
#9 lore raw

Re: [PATCH] selinux: bpf: check SBLABEL_MNT before isec init

In reply to Carlos Llamas
Quoted 24 lines · On Jul 30, 2026 Carlos Llamas <[email protected]> wrote:

selinux_inode_init_security() marks the isec as initialized before checking if mount labeling is supported (SBLABEL_MNT). This was fine until commit 9722955b5430 ("bpf: Add simple xattr support to bpffs"), where genfscon bpffs mounts fail the SBLABEL_MNT check as expected (no xattrs) and yet leave the isec->initialized. This breaks subsequent calls to inode_doinit_with_dentry().

Do the SBLABEL_MNT check before the inode security is initialized.

---
 security/selinux/hooks.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
security/selinux/hooks.c +0 -0
index 8d6945edae7a..09a12eb8652c 100644

security/selinux/hooks.c static int selinux_inode_init_security(struct inode *inode, struct inode *dir, · line 2985

Quoted 6 lines
security/selinux/hooks.c +3 -0
@@ -2980,6 +2980,10 @@ static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
 	if (rc)
 		return rc;
 
+	if (!selinux_initialized() ||
+	    !(sbsec->flags & SBLABEL_MNT))
+		return -EOPNOTSUPP;

If we're moving this check, we should probably just move it to right after we assign 'sbsec' at the top of the function. The calls to inode_mode_to_security() and selinux_determine_inode_label() aren't doing anything useful in either the !selinux_initialized() or !SBLABEL_MNT cases so let's avoid the unnecessary work.

Quoted 16 lines
security/selinux/hooks.c +0 -4
 	/* Possibly defer initialization to selinux_complete_init. */
 	if (sbsec->flags & SE_SBINITIALIZED) {
 		struct inode_security_struct *isec = selinux_inode(inode);
@@ -2988,10 +2992,6 @@ static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
 		isec->initialized = LABEL_INITIALIZED;
 	}
 
-	if (!selinux_initialized() ||
-	    !(sbsec->flags & SBLABEL_MNT))
-		return -EOPNOTSUPP;
-
 	xattr = lsm_get_xattr_slot(xattrs, xattr_count);
 	if (xattr) {
 		rc = security_sid_to_context_force(newsid,
Signature · 1 line
2.55.0.508.g3f0d502094-goog

--
paul-moore.com

#10 lore raw

Re: [PATCH] selinux: bpf: check SBLABEL_MNT before isec init

Quoted 28 lines · Paul Moore
Quoted 27 lines · On Jul 30, 2026 Carlos Llamas <[email protected]> wrote:

selinux_inode_init_security() marks the isec as initialized before checking if mount labeling is supported (SBLABEL_MNT). This was fine until commit 9722955b5430 ("bpf: Add simple xattr support to bpffs"), where genfscon bpffs mounts fail the SBLABEL_MNT check as expected (no xattrs) and yet leave the isec->initialized. This breaks subsequent calls to inode_doinit_with_dentry().

Do the SBLABEL_MNT check before the inode security is initialized.

---
 security/selinux/hooks.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
security/selinux/hooks.c +0 -0
index 8d6945edae7a..09a12eb8652c 100644
@@ -2980,6 +2980,10 @@ static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
 	if (rc)
 		return rc;
 
Quoted 9 lines

security/selinux/hooks.c static int selinux_inode_init_security(struct inode *inode, struct inode *dir, · line 2985

Quoted 3 lines
security/selinux/hooks.c +3 -0
+	if (!selinux_initialized() ||
+	    !(sbsec->flags & SBLABEL_MNT))
+		return -EOPNOTSUPP;

If we're moving this check, we should probably just move it to right after we assign 'sbsec' at the top of the function. The calls to inode_mode_to_security() and selinux_determine_inode_label() aren't doing anything useful in either the !selinux_initialized() or !SBLABEL_MNT cases so let's avoid the unnecessary work.

Ha! That is a really good point. Let me move the check further up then.

#11 lore raw

PATCH v2 1/1 selinux: bpf: check SBLABEL_MNT before isec init

In reply to Paul Moore

selinux_inode_init_security() marks the isec as initialized before checking if mount labeling is supported (SBLABEL_MNT). This was fine until commit 9722955b5430 ("bpf: Add simple xattr support to bpffs"), where genfscon bpffs mounts fail the SBLABEL_MNT check as expected (no xattrs) and yet leave the isec->initialized. This breaks subsequent calls to inode_doinit_with_dentry().

Do the SBLABEL_MNT check before the inode security is initialized.

---
v2:
- Move the check further at the top per Paul's feedback.
- Collect Stephen's Ack.

v1:
https://lore.kernel.org/all/[email protected]/

 security/selinux/hooks.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
security/selinux/hooks.c +4 -4
index 8d6945edae7a..18dd28b2bb13 100644
@@ -2974,6 +2974,10 @@ static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
 
 	sbsec = selinux_superblock(dir->i_sb);
 
+	if (!selinux_initialized() ||
+	    !(sbsec->flags & SBLABEL_MNT))
+		return -EOPNOTSUPP;
+
 	newsid = crsec->create_sid;
 	newsclass = inode_mode_to_security_class(inode->i_mode);
 	rc = selinux_determine_inode_label(crsec, dir, qstr, newsclass, &newsid);
@@ -2988,10 +2992,6 @@ static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
 		isec->initialized = LABEL_INITIALIZED;
 	}
 
-	if (!selinux_initialized() ||
-	    !(sbsec->flags & SBLABEL_MNT))
-		return -EOPNOTSUPP;
-
 	xattr = lsm_get_xattr_slot(xattrs, xattr_count);
 	if (xattr) {
 		rc = security_sid_to_context_force(newsid,
Signature · 1 line
2.55.0.508.g3f0d502094-goog
#12 lore raw

Re: [PATCH v2] selinux: bpf: check SBLABEL_MNT before isec init

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 7 lines

selinux_inode_init_security() marks the isec as initialized before checking if mount labeling is supported (SBLABEL_MNT). This was fine until commit 9722955b5430 ("bpf: Add simple xattr support to bpffs"), where genfscon bpffs mounts fail the SBLABEL_MNT check as expected (no xattrs) and yet leave the isec->initialized. This breaks subsequent calls to inode_doinit_with_dentry().

Quoted 7 lines

Do the SBLABEL_MNT check before the inode security is initialized.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer Acked-by on v2 1/1
#13 lore raw

Re: [PATCH v2] selinux: bpf: check SBLABEL_MNT before isec init

In reply to Carlos Llamas
Quoted 8 lines · Carlos Llamas

selinux_inode_init_security() marks the isec as initialized before checking if mount labeling is supported (SBLABEL_MNT). This was fine until commit 9722955b5430 ("bpf: Add simple xattr support to bpffs"), where genfscon bpffs mounts fail the SBLABEL_MNT check as expected (no xattrs) and yet leave the isec->initialized. This breaks subsequent calls to inode_doinit_with_dentry().

Quoted 7 lines

Do the SBLABEL_MNT check before the inode security is initialized.

Just re-affirming my Ack since the patch did change slightly.

Quoted 40 lines

---
v2:
- Move the check further at the top per Paul's feedback.
- Collect Stephen's Ack.

v1:
https://lore.kernel.org/all/[email protected]/

 security/selinux/hooks.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
security/selinux/hooks.c +4 -5
index 8d6945edae7a..18dd28b2bb13 100644
@@ -2974,6 +2974,10 @@ static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
 
        sbsec = selinux_superblock(dir->i_sb);
 
+       if (!selinux_initialized() ||
+           !(sbsec->flags & SBLABEL_MNT))
+               return -EOPNOTSUPP;
+
        newsid = crsec->create_sid;
        newsclass = inode_mode_to_security_class(inode->i_mode);
        rc = selinux_determine_inode_label(crsec, dir, qstr, newsclass, &newsid);
@@ -2988,10 +2992,6 @@ static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
                isec->initialized = LABEL_INITIALIZED;
        }
 
-       if (!selinux_initialized() ||
-           !(sbsec->flags & SBLABEL_MNT))
-               return -EOPNOTSUPP;
-
        xattr = lsm_get_xattr_slot(xattrs, xattr_count);
        if (xattr) {
                rc = security_sid_to_context_force(newsid,
--

2.55.0.508.g3f0d502094-goog

maintainer Applied
#14 lore raw

Re: [PATCH v2] selinux: bpf: check SBLABEL_MNT before isec init

In reply to Carlos Llamas
Quoted 20 lines · On Jul 30, 2026 Carlos Llamas <[email protected]> wrote:

selinux_inode_init_security() marks the isec as initialized before checking if mount labeling is supported (SBLABEL_MNT). This was fine until commit 9722955b5430 ("bpf: Add simple xattr support to bpffs"), where genfscon bpffs mounts fail the SBLABEL_MNT check as expected (no xattrs) and yet leave the isec->initialized. This breaks subsequent calls to inode_doinit_with_dentry().

Do the SBLABEL_MNT check before the inode security is initialized.

---
v2:
- Move the check further at the top per Paul's feedback.
- Collect Stephen's Ack.

Quoted 5 lines

v1:
https://lore.kernel.org/all/[email protected]/

 security/selinux/hooks.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

Merged into selinux/stable-7.2, thanks!

--
paul-moore.com