Skip to content

Commit eae8d82

Browse files
adam900710kdave
authored andcommitted
btrfs: Fix wild memory access in compression level parser
[BUG] Kernel panic when mounting with "-o compress" mount option. KASAN will report like: ------ ================================================================== BUG: KASAN: wild-memory-access in strncmp+0x31/0xc0 Read of size 1 at addr d86735fce994f800 by task mount/662 ... Call Trace: dump_stack+0xe3/0x175 kasan_report+0x163/0x370 __asan_load1+0x47/0x50 strncmp+0x31/0xc0 btrfs_compress_str2level+0x20/0x70 [btrfs] btrfs_parse_options+0xff4/0x1870 [btrfs] open_ctree+0x2679/0x49f0 [btrfs] btrfs_mount+0x1b7f/0x1d30 [btrfs] mount_fs+0x49/0x190 vfs_kern_mount.part.29+0xba/0x280 vfs_kern_mount+0x13/0x20 btrfs_mount+0x31e/0x1d30 [btrfs] mount_fs+0x49/0x190 vfs_kern_mount.part.29+0xba/0x280 do_mount+0xaad/0x1a00 SyS_mount+0x98/0xe0 entry_SYSCALL_64_fastpath+0x1f/0xbe ------ [Cause] For 'compress' and 'compress_force' options, its token doesn't expect any parameter so its args[0] contains uninitialized data. Accessing args[0] will cause above wild memory access. [Fix] For Opt_compress and Opt_compress_force, set compression level to the default. Signed-off-by: Qu Wenruo <[email protected]> Reviewed-by: David Sterba <[email protected]> [ set the default in advance ] Signed-off-by: David Sterba <[email protected]>
1 parent b77000e commit eae8d82

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

fs/btrfs/compression.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1529,5 +1529,5 @@ unsigned int btrfs_compress_str2level(const char *str)
15291529
if (str[4] == ':' && '1' <= str[5] && str[5] <= '9' && str[6] == 0)
15301530
return str[5] - '0';
15311531

1532-
return 0;
1532+
return BTRFS_ZLIB_DEFAULT_LEVEL;
15331533
}

fs/btrfs/compression.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
/* Maximum size of data before compression */
3535
#define BTRFS_MAX_UNCOMPRESSED (SZ_128K)
3636

37+
#define BTRFS_ZLIB_DEFAULT_LEVEL 3
38+
3739
struct compressed_bio {
3840
/* number of bios pending for this compressed extent */
3941
refcount_t pending_bios;

fs/btrfs/super.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -507,9 +507,18 @@ int btrfs_parse_options(struct btrfs_fs_info *info, char *options,
507507
token == Opt_compress_force ||
508508
strncmp(args[0].from, "zlib", 4) == 0) {
509509
compress_type = "zlib";
510+
510511
info->compress_type = BTRFS_COMPRESS_ZLIB;
511-
info->compress_level =
512-
btrfs_compress_str2level(args[0].from);
512+
info->compress_level = BTRFS_ZLIB_DEFAULT_LEVEL;
513+
/*
514+
* args[0] contains uninitialized data since
515+
* for these tokens we don't expect any
516+
* parameter.
517+
*/
518+
if (token != Opt_compress &&
519+
token != Opt_compress_force)
520+
info->compress_level =
521+
btrfs_compress_str2level(args[0].from);
513522
btrfs_set_opt(info->mount_opt, COMPRESS);
514523
btrfs_clear_opt(info->mount_opt, NODATACOW);
515524
btrfs_clear_opt(info->mount_opt, NODATASUM);

0 commit comments

Comments
 (0)