Re: [PATCH v2] nilfs2: validate segment summary sizes before recovery

From: Ryusuke Konishi

Date: Sun Dec 06 2026 - 06:04:27 EST


On Sun, Dec 6, 2026 at 5:04 PM Kyuwon Cho wrote:
>
> Recovery trusts ss_bytes as the initial offset into the segment summary.
> A corrupted value larger than the filesystem block size reaches the
> BUG_ON() in nilfs_read_summary_info(). The ss_sumbytes field can likewise
> place the recovered data past the logical segment.
>
> Validate that the header fits in its first block and that the complete
> summary starts after the header and fits in the declared log. Document why
> the lower header-size bound permits the historical format without ss_cno.
>
> Fixes: 8b94025c00f9 ("nilfs2: refactor recovery logic routines")
> Signed-off-by: Kyuwon Cho <kyuwon.cho@xxxxxxxx>
> ---
> Changes in v2:
> - Fix the NIFLS kernel-doc typo and document the historical ss_cno lower
> bound before validating the segment summary size, as requested by
> Ryusuke Konishi.
>
> v1: https://lore.kernel.org/all/179621427629.2668950.8242567196312880856@xxxxxxxx/
> Review reply: https://lore.kernel.org/all/179643157802.1720721.3384841973123776085@xxxxxxxx/
>
> The issue was found by an LLM-guided test generator that we are
> developing as a research prototype.
>
> A reproducer is available on request.

Acked-by: Ryusuke Konishi <konishi.ryusuke@xxxxxxxxx>

Hi Viacheslav,

Could you please apply this patch directly to the nilfs2 tree?

Thanks,
Ryusuke Konishi

>
> fs/nilfs2/recovery.c | 16 +++++++++++++++-
> 1 file changed, 15 insertions(+), 1 deletion(-)
>
> diff --git a/fs/nilfs2/recovery.c b/fs/nilfs2/recovery.c
> index c384325a57d0..f5e0d86abb0d 100644
> --- a/fs/nilfs2/recovery.c
> +++ b/fs/nilfs2/recovery.c
> @@ -208,7 +208,7 @@ nilfs_read_log_header(struct the_nilfs *nilfs, sector_t start_blocknr,
> * Return: 0 on success, or one of the following internal codes on failure:
> * * %NILFS_SEG_FAIL_MAGIC - Magic number mismatch.
> * * %NILFS_SEG_FAIL_SEQ - Sequence number mismatch.
> - * * %NIFLS_SEG_FAIL_CONSISTENCY - Block count out of range.
> + * * %NILFS_SEG_FAIL_CONSISTENCY - Block count or summary size out of range.
> * * %NILFS_SEG_FAIL_IO - I/O error.
> * * %NILFS_SEG_FAIL_CHECKSUM_FULL - Full log checksum verification failed.
> */
> @@ -217,6 +217,7 @@ static int nilfs_validate_log(struct the_nilfs *nilfs, u64 seg_seq,
> struct nilfs_segment_summary *sum)
> {
> unsigned long nblock;
> + unsigned int header_bytes, summary_bytes;
> u32 crc;
> int ret;
>
> @@ -234,6 +235,19 @@ static int nilfs_validate_log(struct the_nilfs *nilfs, u64 seg_seq,
> /* This limits the number of blocks read in the CRC check */
> goto out;
>
> + /*
> + * Validate header and summary sizes. Allow the header size down to
> + * the offset of the ss_cno field for the earlier format lacking it.
> + */
> + header_bytes = le16_to_cpu(sum->ss_bytes);
> + summary_bytes = le32_to_cpu(sum->ss_sumbytes);
> + if (unlikely(header_bytes < offsetof(struct nilfs_segment_summary,
> + ss_cno) ||
> + header_bytes > nilfs->ns_blocksize ||
> + summary_bytes < header_bytes ||
> + summary_bytes > (u64)nblock << nilfs->ns_blocksize_bits))
> + goto out;
> +
> ret = NILFS_SEG_FAIL_IO;
> if (nilfs_compute_checksum(nilfs, bh_sum, &crc, sizeof(sum->ss_datasum),
> ((u64)nblock << nilfs->ns_blocksize_bits),
> --
> 2.43.0
>