[PATCH] nilfs2: fix missing error check for sb_set_blocksize call

From: Ryusuke Konishi
Date: Wed Nov 29 2023 - 09:15:55 EST


When mounting a filesystem image with a block size larger than the
page size, nilfs2 repeatedly outputs long error messages with stack
traces to the kernel log, such as the following:

getblk(): invalid block size 8192 requested
logical block size: 512
...
Call Trace:
dump_stack_lvl+0x92/0xd4
dump_stack+0xd/0x10
bdev_getblk+0x33a/0x354
__breadahead+0x11/0x80
nilfs_search_super_root+0xe2/0x704 [nilfs2]
load_nilfs+0x72/0x504 [nilfs2]
nilfs_mount+0x30f/0x518 [nilfs2]
legacy_get_tree+0x1b/0x40
vfs_get_tree+0x18/0xc4
path_mount+0x786/0xa88
__ia32_sys_mount+0x147/0x1a8
__do_fast_syscall_32+0x56/0xc8
do_fast_syscall_32+0x29/0x58
do_SYSENTER_32+0x15/0x18
entry_SYSENTER_32+0x98/0xf1
...

This overloads the system logger. And to make matters worse, it
sometimes crashes the kernel with a memory access violation.

This is because the return value of the sb_set_blocksize() call, which
should be checked for errors, is not checked.

The latter issue is due to out-of-buffer memory being accessed based
on a large block size that caused sb_set_blocksize() to fail for
buffers read with the initial minimum block size that remained
unupdated in the super_block structure.

Since nilfs2 mkfs tool does not accept block sizes larger than the
system page size, this has been overlooked. However, it is possible
to create this situation by intentionally modifying the tool or by
passing a filesystem image created on a system with a large page size
to a system with a smaller page size and mounting it.

Fix this issue by inserting the expected error handling for the call
to sb_set_blocksize().

Signed-off-by: Ryusuke Konishi <konishi.ryusuke@xxxxxxxxx>
Tested-by: Ryusuke Konishi <konishi.ryusuke@xxxxxxxxx>
Cc: stable@xxxxxxxxxxxxxxx
---
Andrew, please apply this as a bugfix. This fixes a memory access
violation and flood of log output when trying to mount a filesystem
image with a block size larger than the page size.

fs/nilfs2/the_nilfs.c | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/fs/nilfs2/the_nilfs.c b/fs/nilfs2/the_nilfs.c
index 0f0667957c81..71400496ed36 100644
--- a/fs/nilfs2/the_nilfs.c
+++ b/fs/nilfs2/the_nilfs.c
@@ -716,7 +716,11 @@ int init_nilfs(struct the_nilfs *nilfs, struct super_block *sb, char *data)
goto failed_sbh;
}
nilfs_release_super_block(nilfs);
- sb_set_blocksize(sb, blocksize);
+ if (!sb_set_blocksize(sb, blocksize)) {
+ nilfs_err(sb, "bad blocksize %d", blocksize);
+ err = -EINVAL;
+ goto out;
+ }

err = nilfs_load_super_block(nilfs, sb, blocksize, &sbp);
if (err)
--
2.34.1