Skip to content

Commit 0317d83

Browse files
committed
nilfs2: fix possible out-of-bounds segment allocation in resize ioctl
Syzbot reports that in its stress test for resize ioctl, the log writing function nilfs_segctor_do_construct hits a WARN_ON in nilfs_segctor_truncate_segments(). It turned out that there is a problem with the current implementation of the resize ioctl, which changes the writable range on the device (the range of allocatable segments) at the end of the resize process. This order is necessary for file system expansion to avoid corrupting the superblock at trailing edge. However, in the case of a file system shrink, if log writes occur after truncating out-of-bounds trailing segments and before the resize is complete, segments may be allocated from the truncated space. The userspace resize tool was fine as it limits the range of allocatable segments before performing the resize, but it can run into this issue if the resize ioctl is called alone. Fix this issue by changing nilfs_sufile_resize() to update the range of allocatable segments immediately after successful truncation of segment space in case of file system shrink. Link: https://lkml.kernel.org/r/[email protected] Fixes: 4e33f9eab07e ("nilfs2: implement resize ioctl") Signed-off-by: Ryusuke Konishi <[email protected]> Reported-by: [email protected] Closes: https://lkml.kernel.org/r/[email protected] 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 2c59410 commit 0317d83

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

fs/nilfs2/sufile.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -772,6 +772,15 @@ int nilfs_sufile_resize(struct inode *sufile, __u64 newnsegs)
772772
goto out_header;
773773

774774
sui->ncleansegs -= nsegs - newnsegs;
775+
776+
/*
777+
* If the sufile is successfully truncated, immediately adjust
778+
* the segment allocation space while locking the semaphore
779+
* "mi_sem" so that nilfs_sufile_alloc() never allocates
780+
* segments in the truncated space.
781+
*/
782+
sui->allocmax = newnsegs - 1;
783+
sui->allocmin = 0;
775784
}
776785

777786
kaddr = kmap_atomic(header_bh->b_page);

0 commit comments

Comments
 (0)