Skip to contentSkip to conversation

statmount: retry an overflowed string before reporting -EOVERFLOW

PATCH In review Ding Feng 2 participants 3 messages Nov 20 – Nov 25 VFS

lore
Share & tools
lore

Ding Feng

author
#0 lore raw

PATCH 1/1 statmount: retry an overflowed string before reporting -EOVERFLOW

statmount_string() compares the seq_file count with the caller's buffer size before it checks whether the seq_file overflowed. After an overflow, seq->count equals seq->size, so every buffer of up to sizeof(struct statmount) + seq->size bytes gets -EOVERFLOW, and the retry with a larger seq buffer never runs.

The result can still fit in such a buffer. statmount_mnt_root() and statmount_sb_source() write escaped strings and unescape them in place, so the escaped form can overflow the 3 * PATH_MAX seq buffer while the final string is much shorter. A bind mount whose root is a 3765-byte path of spaces needs a 4.3 KiB buffer, but STATMOUNT_MNT_ROOT fails with -EOVERFLOW for every buffer up to 12800 bytes and succeeds from 12801.

Check for the overflow first, so the string is regenerated in a larger seq buffer and the size check sees its final length.

Fixes 68385d77c05b ("statmount: simplify string option retrieval")
Assisted-by Claude:claude-opus-5-5 Frama-C

---

Notes:
Tested on v7.4-rc3 (65e4fd942), arm64 tinyconfig in QEMU. The test bind mounts a tmpfs directory whose path below the tmpfs root is 15 names of 250 spaces (3765 bytes, about 15000 bytes escaped) and calls statmount() with STATMOUNT_MNT_ROOT; the result needs 4279 bytes:

bufsize unpatched patched
1024 EOVERFLOW EOVERFLOW
4096 EOVERFLOW EOVERFLOW
8192 EOVERFLOW ok
12800 EOVERFLOW ok
12801 ok ok

A plain mount gives the same results on both kernels (1024 ok, 512 EOVERFLOW).

With this order, a buffer too small for a string longer than the seq buffer now costs one or more retries before -EOVERFLOW, instead of failing at once.

This came up while proving statmount(2) against its man page with Frama-C/WP.

 fs/namespace.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
fs/namespace.c +4 -4
index aecd05ac0..2c4987942 100644
@@ -5813,15 +5813,15 @@ static int statmount_string(struct kstatmount *s, u64 flag)
 	 */
 	if (seq->count == start)
 		return ret;
+	/* signal a retry */
+	if (unlikely(seq_has_overflowed(seq)))
+		return -EAGAIN;
+
 	if (unlikely(check_add_overflow(sizeof(*sm), seq->count, &kbufsize)))
 		return -EOVERFLOW;
 	if (kbufsize >= s->bufsize)
 		return -EOVERFLOW;
 
-	/* signal a retry */
-	if (unlikely(seq_has_overflowed(seq)))
-		return -EAGAIN;
-
 	if (ret)
 		return ret;
 
Signature · 1 line
2.47.3
#1 lore raw

Hi Ding,

Moving the check one step further up, above the "seq->count == start" return, also fixes a case where a string is dropped without any error:

	/* signal a retry */
	if (unlikely(seq_has_overflowed(seq)))
		return -EAGAIN;

	if (seq->count == start)
		return ret;

The NUL written after a string that ends one byte before the end of the seq buffer leaves seq->count == seq->size. The next string then emits nothing, and the early return drops it with no error and no retry. For example, with a tmpfs mounted on a 12278-byte path, statmount() for STATMOUNT_FS_TYPE | STATMOUNT_MNT_ROOT | STATMOUNT_MNT_POINT | STATMOUNT_SB_SOURCE returns 0 without STATMOUNT_SB_SOURCE, with or without your patch, and returns it with the check moved up.

Could you fold this into a v2?

We found this with an LLM-guided test generator, a research prototype we are developing.

Thanks,
Kyuwon

author
#2 lore raw

Hi Kyuwon,

Thanks, confirmed. On v7.4-rc3 (arm64, QEMU), a tmpfs mounted on a 12278-byte path loses STATMOUNT_SB_SOURCE with and without v1, and gets it back with the check moved up. 12276, 12277, 12279 and 12280 bytes work on all three kernels.

The early return came in with 75ead69a7173 ("fs: don't let statmount return empty strings"), so v2 has a second Fixes tag, along with your Reported-by and Suggested-by:

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

Thanks,
Ding Feng