[PATCH] nilfs2: validate segment summary sizes before recovery
From: Kyuwon Cho
Date: Thu Dec 03 2026 - 11:41:11 EST
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.
Fixes: 8b94025c00f9 ("nilfs2: refactor recovery logic routines")
Signed-off-by: Kyuwon Cho <kyuwon.cho@xxxxxxxx>
---
The issue was found by an LLM-guided test generator that we are
developing as a research prototype.
The reproducer mounts a crafted NILFS2 image whose segment summary header
size extends beyond the filesystem block. Mounting the image requires root
in the initial namespace.
The decoded trace is:
kernel BUG at fs/nilfs2/recovery.c:268
nilfs_read_summary_info (fs/nilfs2/recovery.c:268)
nilfs_salvage_orphan_logs (fs/nilfs2/recovery.c:355
fs/nilfs2/recovery.c:693
fs/nilfs2/recovery.c:832)
load_nilfs (fs/nilfs2/the_nilfs.c:352)
nilfs_get_tree (fs/nilfs2/super.c:1069 fs/nilfs2/super.c:1222)
vfs_get_tree (fs/super.c:1881)
fc_mount (fs/namespace.c:1282)
With this patch, the reproducer completes without the
nilfs_read_summary_info() BUG on fe6ff8d5d675. The patch also builds on
the current NILFS tree at 0a99f20855ea.
fs/nilfs2/recovery.c | 12 +++++++++++-
1 file changed, 11 insertions(+), 1 deletion(-)
diff --git a/fs/nilfs2/recovery.c b/fs/nilfs2/recovery.c
index c384325a57d0..b09a2e512e16 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.
+ * * %NIFLS_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,15 @@ 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;
+ 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),