Skip to contentSkip to conversation

KASAN: invalid-free in fat_fill_super

PATCH Applied syzbot 2 participants 2 messages Jul 11, 2018 – Jul 12, 2018 VFAT/FAT/msdos filesystem

lore
Share & tools
lore

syzbot

author syzbot
#0 lore raw

Hello,

syzbot found the following crash on:

HEAD commit:    98be45067040 Add linux-next specific files for 20180711
git tree:       linux-next
console output: https://syzkaller.appspot.com/x/log.txt?x=120b1ed0400000
kernel config:  https://syzkaller.appspot.com/x/.config?x=3f3b3673fec35d01
dashboard link: https://syzkaller.appspot.com/bug?extid=90b8e10515ae88228a92
compiler:       gcc (GCC) 8.0.1 20180413 (experimental)

Unfortunately, I don't have any reproducer for this crash yet.

IMPORTANT: if you fix the bug, please add the following tag to the commit:

Reported-by [email protected]

RDX: 00007fd0b961ead0 RSI: 0000000020000080 RDI: 00007fd0b961eaf0
RBP: 0000000020000080 R08: 00007fd0b961eb30 R09: 00007fd0b961ead0
R10: 0000000000000000 R11: 0000000000000206 R12: 0000000000000015
R13: 0000000000000000 R14: 00000000004d3fd0 R15: 000000000000004a
==================================================================

BUG: KASAN: double-free or invalid-free in fat_fill_super+0xc84/0x4400 fs/fat/inode.c:1882

CPU: 1 PID: 31888 Comm: syz-executor6 Not tainted 4.18.0-rc4-next-20180711+ #4
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/01/2011
Call Trace:
 __dump_stack lib/dump_stack.c:77 [inline]
 dump_stack+0x1c9/0x2b4 lib/dump_stack.c:113
 print_address_description+0x6c/0x20b mm/kasan/report.c:256
 kasan_report_invalid_free+0x64/0xa0 mm/kasan/report.c:336
 __kasan_slab_free+0x150/0x170 mm/kasan/kasan.c:501
 kasan_slab_free+0xe/0x10 mm/kasan/kasan.c:528
 __cache_free mm/slab.c:3498 [inline]
 kfree+0xd9/0x260 mm/slab.c:3813
 fat_fill_super+0xc84/0x4400 fs/fat/inode.c:1882
 vfat_fill_super+0x31/0x40 fs/fat/namei_vfat.c:1049
 mount_bdev+0x314/0x3e0 fs/super.c:1344
 vfat_mount+0x3c/0x50 fs/fat/namei_vfat.c:1056
 legacy_get_tree+0x118/0x440 fs/fs_context.c:659
 vfs_get_tree+0x1cb/0x5c0 fs/super.c:1743
 do_new_mount fs/namespace.c:2567 [inline]
 do_mount+0x6c1/0x1fb0 fs/namespace.c:2889
 ksys_mount+0x12d/0x140 fs/namespace.c:3105
 __do_sys_mount fs/namespace.c:3119 [inline]
 __se_sys_mount fs/namespace.c:3116 [inline]
 __x64_sys_mount+0xbe/0x150 fs/namespace.c:3116
 do_syscall_64+0x1b9/0x820 arch/x86/entry/common.c:290
 entry_SYSCALL_64_after_hwframe+0x49/0xbe
RIP: 0033:0x45885a
Code: b8 a6 00 00 00 0f 05 48 3d 01 f0 ff ff 0f 83 dd 8f fb ff c3 66 2e 0f 1f 84 00 00 00 00 00 66 90 49 89 ca b8 a5 00 00 00 0f 05 <48> 3d 01 f0 ff ff 0f 83 ba 8f fb ff c3 66 0f 1f 84 00 00 00 00 00
RSP: 002b:00007fd0b961ea88 EFLAGS: 00000206 ORIG_RAX: 00000000000000a5
RAX: ffffffffffffffda RBX: 00007fd0b961eb30 RCX: 000000000045885a
RDX: 00007fd0b961ead0 RSI: 0000000020000080 RDI: 00007fd0b961eaf0
RBP: 0000000020000080 R08: 00007fd0b961eb30 R09: 00007fd0b961ead0
R10: 0000000000000000 R11: 0000000000000206 R12: 0000000000000015
R13: 0000000000000000 R14: 00000000004d3fd0 R15: 000000000000004a

Allocated by task 31888:
 save_stack+0x43/0xd0 mm/kasan/kasan.c:448
 set_track mm/kasan/kasan.c:460 [inline]
 kasan_kmalloc+0xc4/0xe0 mm/kasan/kasan.c:553
 __do_kmalloc mm/slab.c:3718 [inline]
 __kmalloc+0x14e/0x760 mm/slab.c:3727
 kmalloc include/linux/slab.h:518 [inline]
 match_strdup+0x5e/0xa0 lib/parser.c:331
 parse_options+0xc7d/0x1a10 fs/fat/inode.c:1291
 fat_fill_super+0x2b3/0x4400 fs/fat/inode.c:1631
 vfat_fill_super+0x31/0x40 fs/fat/namei_vfat.c:1049
 mount_bdev+0x314/0x3e0 fs/super.c:1344
 vfat_mount+0x3c/0x50 fs/fat/namei_vfat.c:1056
 legacy_get_tree+0x118/0x440 fs/fs_context.c:659
 vfs_get_tree+0x1cb/0x5c0 fs/super.c:1743
 do_new_mount fs/namespace.c:2567 [inline]
 do_mount+0x6c1/0x1fb0 fs/namespace.c:2889
 ksys_mount+0x12d/0x140 fs/namespace.c:3105
 __do_sys_mount fs/namespace.c:3119 [inline]
 __se_sys_mount fs/namespace.c:3116 [inline]
 __x64_sys_mount+0xbe/0x150 fs/namespace.c:3116
 do_syscall_64+0x1b9/0x820 arch/x86/entry/common.c:290
 entry_SYSCALL_64_after_hwframe+0x49/0xbe

Freed by task 31888:
 save_stack+0x43/0xd0 mm/kasan/kasan.c:448
 set_track mm/kasan/kasan.c:460 [inline]
 __kasan_slab_free+0x11a/0x170 mm/kasan/kasan.c:521
 kasan_slab_free+0xe/0x10 mm/kasan/kasan.c:528
 __cache_free mm/slab.c:3498 [inline]
 kfree+0xd9/0x260 mm/slab.c:3813
 parse_options+0xc70/0x1a10 fs/fat/inode.c:1290
 fat_fill_super+0x2b3/0x4400 fs/fat/inode.c:1631
 vfat_fill_super+0x31/0x40 fs/fat/namei_vfat.c:1049
 mount_bdev+0x314/0x3e0 fs/super.c:1344
 vfat_mount+0x3c/0x50 fs/fat/namei_vfat.c:1056
 legacy_get_tree+0x118/0x440 fs/fs_context.c:659
 vfs_get_tree+0x1cb/0x5c0 fs/super.c:1743
 do_new_mount fs/namespace.c:2567 [inline]
 do_mount+0x6c1/0x1fb0 fs/namespace.c:2889
 ksys_mount+0x12d/0x140 fs/namespace.c:3105
 __do_sys_mount fs/namespace.c:3119 [inline]
 __se_sys_mount fs/namespace.c:3116 [inline]
 __x64_sys_mount+0xbe/0x150 fs/namespace.c:3116
 do_syscall_64+0x1b9/0x820 arch/x86/entry/common.c:290
 entry_SYSCALL_64_after_hwframe+0x49/0xbe

The buggy address belongs to the object at ffff880190d60c80
 which belongs to the cache kmalloc-32 of size 32
The buggy address is located 0 bytes inside of
 32-byte region [ffff880190d60c80, ffff880190d60ca0)
The buggy address belongs to the page:
page:ffffea0006435800 count:1 mapcount:0 mapping:ffff8801da8001c0 index:0xffff880190d60fc1
flags: 0x2fffc0000000100(slab)
raw: 02fffc0000000100 ffffea00073fe648 ffffea00060af588 ffff8801da8001c0
raw: ffff880190d60fc1 ffff880190d60000 000000010000000d 0000000000000000
page dumped because: kasan: bad access detected

Memory state around the buggy address:
 ffff880190d60b80: fb fb fb fb fc fc fc fc fb fb fb fb fc fc fc fc
 ffff880190d60c00: fb fb fb fb fc fc fc fc fb fb fb fb fc fc fc fc
Quoted 1 line

ffff880190d60c80: fb fb fb fb fc fc fc fc fb fb fb fb fc fc fc fc

                   ^

ffff880190d60d00: fb fb fb fb fc fc fc fc fb fb fb fb fc fc fc fc
ffff880190d60d80: fb fb fb fb fc fc fc fc fb fb fb fb fc fc fc fc
==================================================================

---
This bug is generated by a bot. It may contain errors. See https://goo.gl/tpsmEJ for more information about syzbot. syzbot engineers can be reached at [email protected].

syzbot will keep track of this bug report. See:
https://goo.gl/tpsmEJ#bug-status-tracking for how to communicate with syzbot.

maintainer
#1 lore raw

PATCH 1/1 fat: Fix memory allocation failure handling of match_strdup()

In parse_options(), if match_strdup() failed, parse_options() leaves opts->iocharset in unexpected state (i.e. still pointing the freed string). And this can be the cause of double free.

To fix, this initialize opts->iocharset always when freeing.

---

 fs/fat/inode.c |   20 +++++++++++++-------
 1 file changed, 13 insertions(+), 7 deletions(-)
fs/fat/inode.c renamed from fs/fat/inode.c~fat-fix-kmalloc-failure +13 -7
diff -puN fs/fat/inode.c~fat-fix-kmalloc-failure fs/fat/inode.c
@@ -703,13 +703,21 @@ static void fat_set_state(struct super_b
 	brelse(bh);
 }
 
+static void fat_reset_iocharset(struct fat_mount_options *opts)
+{
+	if (opts->iocharset != fat_default_iocharset) {
+		/* Note: opts->iocharset can be NULL here */
+		kfree(opts->iocharset);
+		opts->iocharset = fat_default_iocharset;
+	}
+}
+
 static void delayed_free(struct rcu_head *p)
 {
 	struct msdos_sb_info *sbi = container_of(p, struct msdos_sb_info, rcu);
 	unload_nls(sbi->nls_disk);
 	unload_nls(sbi->nls_io);
-	if (sbi->options.iocharset != fat_default_iocharset)
-		kfree(sbi->options.iocharset);
+	fat_reset_iocharset(&sbi->options);
 	kfree(sbi);
 }
 
@@ -1124,7 +1132,7 @@ static int parse_options(struct super_bl
 	opts->fs_fmask = opts->fs_dmask = current_umask();
 	opts->allow_utime = -1;
 	opts->codepage = fat_default_codepage;
-	opts->iocharset = fat_default_iocharset;
+	fat_reset_iocharset(opts);
 	if (is_vfat) {
 		opts->shortname = VFAT_SFN_DISPLAY_WINNT|VFAT_SFN_CREATE_WIN95;
 		opts->rodir = 0;
@@ -1281,8 +1289,7 @@ static int parse_options(struct super_bl
 
 		/* vfat specific */
 		case Opt_charset:
-			if (opts->iocharset != fat_default_iocharset)
-				kfree(opts->iocharset);
+			fat_reset_iocharset(opts);
 			iocharset = match_strdup(&args[0]);
 			if (!iocharset)
 				return -ENOMEM;
@@ -1873,8 +1880,7 @@ out_fail:
 		iput(fat_inode);
 	unload_nls(sbi->nls_io);
 	unload_nls(sbi->nls_disk);
-	if (sbi->options.iocharset != fat_default_iocharset)
-		kfree(sbi->options.iocharset);
+	fat_reset_iocharset(&sbi->options);
 	sb->s_fs_info = NULL;
 	kfree(sbi);
 	return error;

_

Signature · 1 line
OGAWA Hirofumi <[email protected]>