Skip to contentSkip to conversation

ocfs2: retain all security xattrs during inode creation

PATCH Applied Joseph Qi 1 participant 2 messages Sep 17 1 message not in the archive OCFS2

lore
Share & tools
lore

Joseph Qi

author maintainer
#0 lore raw

PATCH v2 1/1 ocfs2: retain all security xattrs during inode creation

When creating a new inode, ocfs2_init_security_get() stores the security xattr returned by the LSM framework in ocfs2_security_xattr_info so that it can be written later within the same transaction. But ocfs2_initxattrs() only ever looks at the first element of the xattr array, and it keeps the name as a bare pointer into memory owned by the security layer instead of copying it. The value, by contrast, is already duplicated with kmemdup().

With stacked LSMs, security_inode_init_security() provides one xattr per LSM, so all labels but the first are silently dropped and never written to disk, leaving newly created files without the security xattrs the other LSMs rely on.

The borrowed name is not an outright bug with the LSMs in tree
today: security_inode_init_security() frees only ->value once the callback returns, and documents ->name as the attribute name suffix, so the pointer stays valid for as long as ocfs2 uses it. It is fragile though, as an LSM that allocates the name together with the value would turn it into a use-after-free, so the rework below copies the names as well.

Fix this by storing copies of all the security xattrs: allocate an array in ocfs2_initxattrs(), copy the names and values, account for all of them in the credit calculations, and write each one in ocfs2_init_security_set().

While at it, switch the allocations from GFP_KERNEL to GFP_NOFS. The callback runs in the inode creation path with the parent directory locked, and the security layer allocates the very same xattr array with GFP_NOFS, so direct reclaim must not be allowed to recurse into the filesystem from here.

Fixes de3004c874e7 ("ocfs2: Switch to security_inode_init_security()")

---
v1->v2: reserve the cluster an xattr bucket needs in case of block size 512B, just like ocfs2_calc_xattr_init()

 fs/ocfs2/namei.c |   6 +--
 fs/ocfs2/xattr.c | 133 +++++++++++++++++++++++++++++++++++++----------
 fs/ocfs2/xattr.h |  12 +++--
 3 files changed, 116 insertions(+), 35 deletions(-)
fs/ocfs2/namei.c +2 -4
index 58c6061ed983..ef03f90e7265 100644
@@ -248,7 +248,6 @@ static int ocfs2_mknod(struct mnt_idmap *idmap,
 	int want_meta = 0;
 	int xattr_credits = 0;
 	struct ocfs2_security_xattr_info si = {
-		.name = NULL,
 		.enable = 1,
 	};
 	int did_quota_inode = 0;
@@ -475,7 +474,7 @@ static int ocfs2_mknod(struct mnt_idmap *idmap,
 
 	brelse(new_fe_bh);
 	brelse(parent_fe_bh);
-	kfree(si.value);
+	ocfs2_free_security_xattrs(&si);
 
 	ocfs2_acl_init_release(&acl_state);
 
@@ -1831,7 +1830,6 @@ static int ocfs2_symlink(struct mnt_idmap *idmap,
 	int want_clusters = 0;
 	int xattr_credits = 0;
 	struct ocfs2_security_xattr_info si = {
-		.name = NULL,
 		.enable = 1,
 	};
 	int did_quota = 0, did_quota_inode = 0;
@@ -2063,7 +2061,7 @@ static int ocfs2_symlink(struct mnt_idmap *idmap,
 
 	brelse(new_fe_bh);
 	brelse(parent_fe_bh);
-	kfree(si.value);
+	ocfs2_free_security_xattrs(&si);
 	ocfs2_free_dir_lookup_result(&lookup);
 	if (inode_ac)
 		ocfs2_free_alloc_context(inode_ac);
fs/ocfs2/xattr.c +105 -28

193 lines not on this page · raw message

fs/ocfs2/xattr.h +9 -3
index 887cc1a18b1a..1dd08f979e3c 100644
@@ -20,13 +20,18 @@ enum ocfs2_xattr_type {
 	OCFS2_XATTR_MAX
 };
 
-struct ocfs2_security_xattr_info {
-	int enable;
-	const char *name;
+struct ocfs2_security_xattr {
+	char *name;
 	void *value;
 	size_t value_len;
 };
 
+struct ocfs2_security_xattr_info {
+	int enable;
+	struct ocfs2_security_xattr *xattrs;
+	int count;
+};
+
 extern const struct xattr_handler ocfs2_xattr_user_handler;
 extern const struct xattr_handler ocfs2_xattr_trusted_handler;
 extern const struct xattr_handler ocfs2_xattr_security_handler;
@@ -49,6 +54,7 @@ int ocfs2_xattr_remove(struct inode *, struct buffer_head *);
 int ocfs2_init_security_get(struct inode *, struct inode *,
 			    const struct qstr *,
 			    struct ocfs2_security_xattr_info *);
+void ocfs2_free_security_xattrs(struct ocfs2_security_xattr_info *);
 int ocfs2_init_security_set(handle_t *, struct inode *,
 			    struct buffer_head *,
 			    struct ocfs2_security_xattr_info *,
Signature · 1 line
2.39.3

A message that is not in the archive (sent off-list or not yet received): [email protected]

author maintainer
#2 lore raw
Quoted 61 lines · On 9/17/26 9:58 AM, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] ocfs2: BUG_ON() in xattr index creation due to unallocated bucket --

commit cd302f8c577646bddd492b0e34b4efe956759c91
Author: Joseph Qi <[email protected]>

ocfs2: retain all security xattrs during inode creation

This commit modifies OCFS2 to store all security xattrs returned by the LSM framework when creating a new inode, instead of just the first one. It copies the names and values, calculates the necessary credits, and writes them all within the same transaction.

Quoted 4 lines
fs/ocfs2/xattr.c +0 -0
index a2d6c3d0f2e8d..25fb23cc00d2d 100644

[ ... ]

Quoted 8 lines
fs/ocfs2/xattr.c +0 -4
@@ -663,14 +668,27 @@ int ocfs2_calc_security_init(struct inode *dir,
 		*xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
 	}
 
-	/* reserve clusters for xattr value which will be set in B tree*/
-	if (si->value_len > OCFS2_XATTR_INLINE_SIZE) {
-		int new_clusters = ocfs2_clusters_for_bytes(dir->i_sb,
-							    si->value_len);

fs/ocfs2/xattr.c int ocfs2_calc_security_init(struct inode *dir, · line 680

Quoted 10 lines
fs/ocfs2/xattr.c +10 -0
+	/*
+	 * when blocksize = 512, the security xattrs may not fit in the
+	 * single block reserved above and a bucket has to be allocated
+	 * for them, so reserve the cluster it needs as well.
+	 */
+	if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE &&
+	    s_size > OCFS2_XATTR_FREE_IN_BLOCK(dir)) {
+		*want_clusters += 1;
+		*xattr_credits += ocfs2_blocks_per_xattr_bucket(dir->i_sb);
+	}

[Severity: High]
Is it possible for the total size of stacked LSM xattrs to exceed the capacity of larger block sizes as well?

With stacked LSMs, if the combined size of the security xattrs exceeds the available space in a single xattr block (even for block sizes > 512 bytes), this condition will fail to reserve the necessary cluster and credits.

Consequently, when ocfs2_init_security_set() loops over the xattrs to write them, it may run out of space in the block and fall back to creating an index block.

Since ocfs2_xattr_set_handle() also assumes buckets are only needed for 512-byte blocks:

fs/ocfs2/xattr.c:ocfs2_xattr_set_handle() {
    ...
	if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE) {
		xbs.bucket = ocfs2_xattr_bucket_new(inode);
		if (!xbs.bucket) {
    ...
}
Quoted 7 lines

This will result in xbs.bucket being NULL. When the index block conversion is forced due to ENOSPC, it triggers a BUG_ON(!xs->bucket) inside ocfs2_xattr_create_index_block(), causing a kernel panic.

Does this same limitation also affect the identical block size check in ocfs2_calc_xattr_init() when calculating credits for combined xattr sizes?

It seems a false positive. The BUG_ON() looks not reachable on this path at any block size. So I'd rather leave it as of now.

Thanks,
Joseph