mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2] statmount: retry an overflowed seq buffer before any other check
@ 2026-11-25 17:13 Ding Feng
  0 siblings, 0 replies; only message in thread
From: Ding Feng @ 2026-11-25 17:13 UTC (permalink / raw)
  To: Christian Brauner
  Cc: Alexander Viro, Jan Kara, Ian Kent, Jeff Layton, Kyuwon Cho,
	linux-fsdevel, linux-kernel

statmount_string() checks whether the seq_file overflowed only after
two earlier returns, and both can hide the overflow.

The size check runs first. 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.

The early return for a string that emits nothing runs before both. 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. 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.

Check for the overflow before both, so the string is regenerated in a
larger seq buffer.

Fixes: 68385d77c05b ("statmount: simplify string option retrieval")
Fixes: 75ead69a7173 ("fs: don't let statmount return empty strings")
Reported-by: Kyuwon Cho <kyuwoncho18@gmail.com>
Closes: https://lore.kernel.org/all/179552693473.1490262.10477620788152048784@gmail.com/
Suggested-by: Kyuwon Cho <kyuwoncho18@gmail.com>
Assisted-by: Claude:claude-opus-5-5 Frama-C
Signed-off-by: Ding Feng <dingf3ng@gmail.com>
---

Notes:
    v2: move the check above the "seq->count == start" return as well,
    which also fixes the dropped string Kyuwon found.
    
    v1: https://lore.kernel.org/all/20261120033644.99834-1-dingf3ng@gmail.com/
    
    Tested on v7.4-rc3 (65e4fd942), arm64 tinyconfig in QEMU:
    
      case                                          v7.4-rc3   v1         v2
      STATMOUNT_MNT_ROOT, 3765-byte root of spaces,
        bufsize 8192 or 12800                       EOVERFLOW  ok         ok
        bufsize 4096                                EOVERFLOW  EOVERFLOW  EOVERFLOW
      tmpfs on a 12278-byte path, SB_SOURCE         missing    missing    tmpfs
      tmpfs on a 12276, 12277, 12279 or 12280-byte
        path, SB_SOURCE                             tmpfs      tmpfs      tmpfs
    
    A plain bind mount gives the same results on all three (1024 ok, 512
    EOVERFLOW).
    
    A string that legitimately emits nothing after a full seq buffer now
    costs one retry, and a buffer too small for a string longer than the
    seq buffer costs one or more retries before -EOVERFLOW.

 fs/namespace.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/fs/namespace.c b/fs/namespace.c
index aecd05ac0..3c41fd42c 100644
--- a/fs/namespace.c
+++ b/fs/namespace.c
@@ -5807,6 +5807,10 @@ static int statmount_string(struct kstatmount *s, u64 flag)
 		return -EINVAL;
 	}
 
+	/* signal a retry */
+	if (unlikely(seq_has_overflowed(seq)))
+		return -EAGAIN;
+
 	/*
 	 * If nothing was emitted, return to avoid setting the flag
 	 * and terminating the buffer.
@@ -5818,10 +5822,6 @@ static int statmount_string(struct kstatmount *s, u64 flag)
 	if (kbufsize >= s->bufsize)
 		return -EOVERFLOW;
 
-	/* signal a retry */
-	if (unlikely(seq_has_overflowed(seq)))
-		return -EAGAIN;
-
 	if (ret)
 		return ret;
 
-- 
2.47.3


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-25 17:14 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-25 17:13 [PATCH v2] statmount: retry an overflowed seq buffer before any other check Ding Feng

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®