Skip to contentSkip to conversation

xattr: account for existing values on replacement

RFC PATCH New Shawn Shao 1 participant 1 message Dec 4

Subsystems (2)
lore
Share & tools
lore

Shawn Shao

author
#0 lore raw

RFC PATCH 1/1 xattr: account for existing values on replacement

simple_xattr_set_limited() charges the full new value and one more xattr before looking up the old value. At the count or size limit this can reject a replacement even when the resulting xattr count and value size remain within the limits.

Look up the existing value under the caller's serialization. Reserve a count only for a new attribute, and reserve only the size increase for a replacement. Release reservations if setting the xattr fails.

The earlier kernfs limit implementation used the same pre-charge ordering, so it is unclear whether these limits constrain final stored state or peak allocation while replacement objects overlap. This RFC proposes final-state accounting and asks maintainers to confirm the intended semantics. The sockfs selftest exercises replacement at both configured limits.

The selftest builds, and QEMU runs of the reproducer confirm that replacements at both limits succeed while new over-limit attributes remain rejected.

Fixes 4fbe9e78bb41 ("xattr: move user limits for xattrs to generic infra")

---
Reproducer and logs (raw files from the same public Gist):
Gist: https://gist.github.com/shaos26/4ba329195592088b956746e37fd15c8a
baseline console: https://gist.githubusercontent.com/shaos26/4ba329195592088b956746e37fd15c8a/raw/9c1c063cba8cb34d4a8d35441f6309136b2da4c4/console-vulnerable.txt
repeat baseline runs: https://gist.githubusercontent.com/shaos26/4ba329195592088b956746e37fd15c8a/raw/a1028b048d6d324bfa111d15d90307128c05d8ab/baseline-console-output.txt
patched console: https://gist.githubusercontent.com/shaos26/4ba329195592088b956746e37fd15c8a/raw/38fa2440b3f44aab3f78abfed4a15002f9372385/console-fixed.txt
kernel config: https://gist.githubusercontent.com/shaos26/4ba329195592088b956746e37fd15c8a/raw/5baa3a59dc612c2e4e456bd215743a771e0c1722/kernel-config.txt
reproducer: https://gist.githubusercontent.com/shaos26/4ba329195592088b956746e37fd15c8a/raw/5015cb28f2f7115bb8ddac71dce19e64a763ccca/poc.c
patch: https://gist.githubusercontent.com/shaos26/4ba329195592088b956746e37fd15c8a/raw/0d27a1b09876c795eabfde5f5503a77208a19983/fix.patch
reproduction instructions: https://gist.githubusercontent.com/shaos26/4ba329195592088b956746e37fd15c8a/raw/627aae274252f5a447233cf695ea67f10dbdbcd4/README.md
build helper: https://gist.githubusercontent.com/shaos26/4ba329195592088b956746e37fd15c8a/raw/5428175835d4db539e4370d66f334d75024d99f4/build-poc.sh
guest init: https://gist.githubusercontent.com/shaos26/4ba329195592088b956746e37fd15c8a/raw/c7ee5cfd3d286a6f6ea21943d371098509200ca8/guest-init.sh
QEMU runner: https://gist.githubusercontent.com/shaos26/4ba329195592088b956746e37fd15c8a/raw/f7945e586bc3d811260a68db5b13d707eba117bd/run-qemu.sh
The reproducer uses an unprivileged process and its own AF_UNIX socket; this is a correctness issue, not a privilege-boundary issue. The helper is shared by sockfs, kernfs, and bpffs, though runtime testing here used sockfs.

 fs/xattr.c                                    | 65 +++++++++++++++----
 .../filesystems/xattr/xattr_sockfs_test.c     | 44 +++++++++++++
 2 files changed, 96 insertions(+), 13 deletions(-)
fs/xattr.c +52 -13
index 0705801a62cc5..0aa6948e399cf 100644
@@ -1511,6 +1511,17 @@ static inline void simple_xattr_limits_dec(struct simple_xattr_limits *limits,
 	atomic_dec(&limits->nr_xattrs);
 }
 
+static inline int simple_xattr_limits_inc_size(struct simple_xattr_limits *limits,
+					       size_t size)
+{
+	if (atomic_add_return(size, &limits->xattr_size) <=
+	    SIMPLE_XATTR_MAX_SIZE)
+		return 0;
+
+	atomic_sub(size, &limits->xattr_size);
+	return -ENOSPC;
+}
+
 static inline int simple_xattr_limits_inc(struct simple_xattr_limits *limits,
 					  size_t size)
 {
@@ -1519,11 +1530,11 @@ static inline int simple_xattr_limits_inc(struct simple_xattr_limits *limits,
 		return -ENOSPC;
 	}
 
-	if (atomic_add_return(size, &limits->xattr_size) <= SIMPLE_XATTR_MAX_SIZE)
-		return 0;
-
-	simple_xattr_limits_dec(limits, size);
-	return -ENOSPC;
+	if (simple_xattr_limits_inc_size(limits, size)) {
+		atomic_dec(&limits->nr_xattrs);
+		return -ENOSPC;
+	}
+	return 0;
 }
 
 /**
@@ -1537,8 +1548,9 @@ static inline int simple_xattr_limits_inc(struct simple_xattr_limits *limits,
  * @flags: XATTR_CREATE, XATTR_REPLACE, or 0
  *
  * Like simple_xattr_set(), but enforces per-inode count and total value size
- * limits for user.* xattrs. Uses speculative pre-increment of the atomic
- * counters to avoid races without requiring external locks.
+ * limits for user.* xattrs. New attributes are charged before insertion;
+ * replacements reserve only any increase in value size. As with
+ * simple_xattr_set(), callers must serialize writes to @xattrs.
  *
  * Return: On success zero is returned. On failure a negative error code is
  * returned.
@@ -1549,22 +1561,49 @@ int simple_xattr_set_limited(struct simple_xattr_cache *cache, struct list_head
 			     size_t size, int flags)
 {
 	struct simple_xattr *old_xattr;
-	int ret;
+	size_t reserved_size = 0;
+	bool reserved_count = false;
+	int old_size, ret;
 
 	if (value) {
-		ret = simple_xattr_limits_inc(limits, size);
-		if (ret)
-			return ret;
+		old_size = simple_xattr_get(cache, xattrs, name, NULL, 0);
+		if (old_size >= 0) {
+			if (flags & XATTR_CREATE)
+				return -EEXIST;
+
+			if (size > old_size) {
+				reserved_size = size - old_size;
+				ret = simple_xattr_limits_inc_size(limits, reserved_size);
+				if (ret)
+					return ret;
+			}
+		} else {
+			if (old_size != -ENODATA)
+				return old_size;
+			if (flags & XATTR_REPLACE)
+				return -ENODATA;
+
+			ret = simple_xattr_limits_inc(limits, size);
+			if (ret)
+				return ret;
+			reserved_size = size;
+			reserved_count = true;
+		}
 	}
 
 	old_xattr = simple_xattr_set(cache, xattrs, name, value, size, flags);
 	if (IS_ERR(old_xattr)) {
-		if (value)
+		if (reserved_count)
 			simple_xattr_limits_dec(limits, size);
+		else if (reserved_size)
+			atomic_sub(reserved_size, &limits->xattr_size);
 		return PTR_ERR(old_xattr);
 	}
 	if (old_xattr) {
-		simple_xattr_limits_dec(limits, old_xattr->size);
+		if (!value)
+			simple_xattr_limits_dec(limits, old_xattr->size);
+		else if (size < old_xattr->size)
+			atomic_sub(old_xattr->size - size, &limits->xattr_size);
 		simple_xattr_free_rcu(old_xattr);
 	}
 	return 0;
tools/testing/selftests/filesystems/xattr/xattr_sockfs_test.c +44 -0

62 lines not on this page · raw message

Signature · 1 line
2.34.1