[PATCH v4 3/9] ocfs2: reject invalid short external xattr values
From: Siddh Raman Pant
Date: Sat Dec 05 2026 - 05:15:11 EST
OCFS2 stores values up to OCFS2_XATTR_INLINE_SIZE locally. If a
short value is marked external, removal expects a value root, but
namevalue_size_xe() uses the value length to calculate its storage
size. The mismatch can hit BUG_ON() in ocfs2_xa_fill_value_buf().
Reject a nonzero external value at or below the inline size limit in
ocfs2_validate_xattr_entry(). A zero-length external value can be
journaled by ocfs2_xattr_bucket_value_truncate() during inode
deletion, after all its clusters have been removed. Allow that entry
and use its local flag to account for the root during compaction and
removal.
Sashiko pointed out that reusing an empty external entry that still
claims clusters can hit BUG_ON(!orig_value_size) if resizing fails.
Once the value root is in bounds, check that an empty external value
has no clusters before allowing readers or writers to use it.
Fixes: 3fc12afa0cea ("ocfs2: Provide ocfs2_xa_fill_value_buf() for external value processing")
Signed-off-by: Siddh Raman Pant <siddh.raman.pant@xxxxxxxxxx>
---
fs/ocfs2/xattr.c | 52 +++++++++++++++++++++++++++++++++++++++++++++---
1 file changed, 49 insertions(+), 3 deletions(-)
diff --git a/fs/ocfs2/xattr.c b/fs/ocfs2/xattr.c
index 2d32cba2a36f..769e01e61a45 100644
--- a/fs/ocfs2/xattr.c
+++ b/fs/ocfs2/xattr.c
@@ -26,6 +26,7 @@
#include <linux/module.h>
#include <linux/string.h>
#include <linux/security.h>
+#include <linux/unaligned.h>
#include <cluster/masklog.h>
@@ -228,13 +229,21 @@ static int namevalue_size_xi(struct ocfs2_xattr_info *xi)
return namevalue_size(xi->xi_name_len, xi->xi_value_len);
}
+static int ocfs2_xattr_value_size(struct ocfs2_xattr_entry *xe)
+{
+ if (!ocfs2_xattr_is_local(xe))
+ return OCFS2_XATTR_ROOT_SIZE;
+
+ return OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
+}
+
static int namevalue_size_xe(struct ocfs2_xattr_entry *xe)
{
u64 value_len = le64_to_cpu(xe->xe_value_size);
BUG_ON((value_len > OCFS2_XATTR_INLINE_SIZE) &&
ocfs2_xattr_is_local(xe));
- return namevalue_size(xe->xe_name_len, value_len);
+ return OCFS2_XATTR_SIZE(xe->xe_name_len) + ocfs2_xattr_value_size(xe);
}
static int ocfs2_validate_xattr_entry(struct super_block *sb, u64 blkno,
@@ -254,9 +263,36 @@ static int ocfs2_validate_xattr_entry(struct super_block *sb, u64 blkno,
(unsigned long long)blkno,
(unsigned long long)value_len);
+ /* Inode deletion can leave a truncated external value in a bucket. */
+ if (!ocfs2_xattr_is_local(xe) && value_len &&
+ value_len <= OCFS2_XATTR_INLINE_SIZE)
+ return ocfs2_error(sb,
+ "Invalid external xattr in block %llu: value size %llu\n",
+ (unsigned long long)blkno,
+ (unsigned long long)value_len);
+
return 0;
}
+static int ocfs2_validate_empty_xattr_root(struct super_block *sb, u64 blkno,
+ struct ocfs2_xattr_entry *xe,
+ const void *value)
+{
+ size_t off;
+ u32 clusters;
+
+ if (ocfs2_xattr_is_local(xe) || xe->xe_value_size)
+ return 0;
+
+ off = offsetof(struct ocfs2_xattr_value_root, xr_clusters);
+ clusters = get_unaligned_le32(value + off);
+ if (clusters)
+ return ocfs2_error(sb,
+ "Empty external xattr in block %llu has %u clusters\n",
+ (unsigned long long)blkno, clusters);
+
+ return 0;
+}
static int ocfs2_xattr_bucket_get_name_value(struct super_block *sb,
struct ocfs2_xattr_header *xh,
@@ -1153,6 +1189,11 @@ static int ocfs2_validate_xattr_entries_flat(struct super_block *sb, u64 blkno,
"Invalid xattr in block %llu: entry %d value root is out of bounds\n",
(unsigned long long)blkno, i);
}
+
+ ret = ocfs2_validate_empty_xattr_root(sb, blkno, xe,
+ (void *)xh + value_offset);
+ if (ret)
+ return ret;
}
return 0;
@@ -1275,6 +1316,7 @@ static int ocfs2_validate_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
size_t block_offset = name_offset % nv_limit;
size_t value_offset;
u64 value_len = le64_to_cpu(xe->xe_value_size);
+ const void *value;
ret = ocfs2_validate_xattr_entry(sb, blkno, xe);
if (ret)
@@ -1308,6 +1350,12 @@ static int ocfs2_validate_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
"Invalid xattr bucket %llu: entry %d value root is out of bounds\n",
(unsigned long long)blkno, i);
}
+
+ value = bucket_block(bucket, block_off) + value_offset;
+ ret = ocfs2_validate_empty_xattr_root(sb, blkno + block_off,
+ xe, value);
+ if (ret)
+ return ret;
}
return 0;
@@ -1917,8 +1965,6 @@ static void ocfs2_xa_fill_value_buf(struct ocfs2_xa_loc *loc,
/* Value bufs are for value trees */
BUG_ON(ocfs2_xattr_is_local(loc->xl_entry));
- BUG_ON(namevalue_size_xe(loc->xl_entry) !=
- (name_size + OCFS2_XATTR_ROOT_SIZE));
loc->xl_ops->xlo_fill_value_buf(loc, vb);
vb->vb_xv =
--
2.53.0