Re: [PATCH v2] statmount: retry an overflowed seq buffer before any other check
From: Jan Kara
Date: Fri Nov 27 2026 - 10:27:18 EST
On Thu 26-11-26 01:13:48, Ding Feng wrote:
> 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@xxxxxxxxx>
> Closes: https://lore.kernel.org/all/179552693473.1490262.10477620788152048784@xxxxxxxxx/
> Suggested-by: Kyuwon Cho <kyuwoncho18@xxxxxxxxx>
> Assisted-by: Claude:claude-opus-5-5 Frama-C
> Signed-off-by: Ding Feng <dingf3ng@xxxxxxxxx>
Looks good to me. Feel free to add:
Reviewed-by: Jan Kara <jack@xxxxxxx>
Honza
> ---
>
> 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@xxxxxxxxx/
>
> 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
>
--
Jan Kara <jack@xxxxxxxx>
SUSE Labs, CR