Skip to content

Commit bb6f270

Browse files
committed
nilfs2: reject devices with insufficient block count
The current sanity check for nilfs2 geometry information lacks checks for the number of segments stored in superblocks, so even for device images that have been destructively truncated or have an unusually high number of segments, the mount operation may succeed. This causes out-of-bounds block I/O on file system block reads or log writes to the segments, the latter in particular causing "a_ops->writepages" to repeatedly fail, resulting in sync_inodes_sb() to hang. Fix this issue by checking the number of segments stored in the superblock and avoiding mounting devices that can cause out-of-bounds accesses. To eliminate the possibility of overflow when calculating the number of blocks required for the device from the number of segments, this also adds a helper function to calculate the upper bound on the number of segments and inserts a check using it. Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Ryusuke Konishi <[email protected]> Reported-by: [email protected] Link: https://syzkaller.appspot.com/bug?extid=7d50f1e54a12ba3aeae2 Tested-by: Ryusuke Konishi <[email protected]> Cc: <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Ryusuke Konishi <[email protected]>
1 parent 0317d83 commit bb6f270

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

fs/nilfs2/the_nilfs.c

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,18 @@ unsigned long nilfs_nrsvsegs(struct the_nilfs *nilfs, unsigned long nsegs)
397397
100));
398398
}
399399

400+
/**
401+
* nilfs_max_segment_count - calculate the maximum number of segments
402+
* @nilfs: nilfs object
403+
*/
404+
static u64 nilfs_max_segment_count(struct the_nilfs *nilfs)
405+
{
406+
u64 max_count = U64_MAX;
407+
408+
do_div(max_count, nilfs->ns_blocks_per_segment);
409+
return min_t(u64, max_count, ULONG_MAX);
410+
}
411+
400412
void nilfs_set_nsegments(struct the_nilfs *nilfs, unsigned long nsegs)
401413
{
402414
nilfs->ns_nsegments = nsegs;
@@ -406,6 +418,8 @@ void nilfs_set_nsegments(struct the_nilfs *nilfs, unsigned long nsegs)
406418
static int nilfs_store_disk_layout(struct the_nilfs *nilfs,
407419
struct nilfs_super_block *sbp)
408420
{
421+
u64 nsegments, nblocks;
422+
409423
if (le32_to_cpu(sbp->s_rev_level) < NILFS_MIN_SUPP_REV) {
410424
nilfs_err(nilfs->ns_sb,
411425
"unsupported revision (superblock rev.=%d.%d, current rev.=%d.%d). Please check the version of mkfs.nilfs(2).",
@@ -449,7 +463,34 @@ static int nilfs_store_disk_layout(struct the_nilfs *nilfs,
449463
return -EINVAL;
450464
}
451465

452-
nilfs_set_nsegments(nilfs, le64_to_cpu(sbp->s_nsegments));
466+
nsegments = le64_to_cpu(sbp->s_nsegments);
467+
if (nsegments > nilfs_max_segment_count(nilfs)) {
468+
nilfs_err(nilfs->ns_sb,
469+
"segment count %llu exceeds upper limit (%llu segments)",
470+
(unsigned long long)nsegments,
471+
(unsigned long long)nilfs_max_segment_count(nilfs));
472+
return -EINVAL;
473+
}
474+
475+
nblocks = sb_bdev_nr_blocks(nilfs->ns_sb);
476+
if (nblocks) {
477+
u64 min_block_count = nsegments * nilfs->ns_blocks_per_segment;
478+
/*
479+
* To avoid failing to mount early device images without a
480+
* second superblock, exclude that block count from the
481+
* "min_block_count" calculation.
482+
*/
483+
484+
if (nblocks < min_block_count) {
485+
nilfs_err(nilfs->ns_sb,
486+
"total number of segment blocks %llu exceeds device size (%llu blocks)",
487+
(unsigned long long)min_block_count,
488+
(unsigned long long)nblocks);
489+
return -EINVAL;
490+
}
491+
}
492+
493+
nilfs_set_nsegments(nilfs, nsegments);
453494
nilfs->ns_crc_seed = le32_to_cpu(sbp->s_crc_seed);
454495
return 0;
455496
}

0 commit comments

Comments
 (0)