Blame SOURCES/0035-exfat-fix-memory-leak-in-exfat_parse_param.patch

Kmods SIG 9e3ffb
From f341a7d8dcc4e3d01544d7bc145633f062ef6249 Mon Sep 17 00:00:00 2001
Kmods SIG 9e3ffb
From: Al Viro <viro@zeniv.linux.org.uk>
Kmods SIG 9e3ffb
Date: Wed, 3 Jun 2020 09:48:36 +0900
Kmods SIG 9e3ffb
Subject: [Backport f341a7d8dcc4] exfat: fix memory leak in exfat_parse_param()
Kmods SIG 9e3ffb
Kmods SIG 9e3ffb
butt3rflyh4ck reported memory leak found by syzkaller.
Kmods SIG 9e3ffb
Kmods SIG 9e3ffb
A param->string held by exfat_mount_options.
Kmods SIG 9e3ffb
Kmods SIG 9e3ffb
BUG: memory leak
Kmods SIG 9e3ffb
Kmods SIG 9e3ffb
unreferenced object 0xffff88801972e090 (size 8):
Kmods SIG 9e3ffb
  comm "syz-executor.2", pid 16298, jiffies 4295172466 (age 14.060s)
Kmods SIG 9e3ffb
  hex dump (first 8 bytes):
Kmods SIG 9e3ffb
    6b 6f 69 38 2d 75 00 00                          koi8-u..
Kmods SIG 9e3ffb
  backtrace:
Kmods SIG 9e3ffb
    [<000000005bfe35d6>] kstrdup+0x36/0x70 mm/util.c:60
Kmods SIG 9e3ffb
    [<0000000018ed3277>] exfat_parse_param+0x160/0x5e0
Kmods SIG 9e3ffb
src/super.c:276
Kmods SIG 9e3ffb
    [<000000007680462b>] vfs_parse_fs_param+0x2b4/0x610
Kmods SIG 9e3ffb
fs/fs_context.c:147
Kmods SIG 9e3ffb
    [<0000000097c027f2>] vfs_parse_fs_string+0xe6/0x150
Kmods SIG 9e3ffb
fs/fs_context.c:191
Kmods SIG 9e3ffb
    [<00000000371bf78f>] generic_parse_monolithic+0x16f/0x1f0
Kmods SIG 9e3ffb
fs/fs_context.c:231
Kmods SIG 9e3ffb
    [<000000005ce5eb1b>] do_new_mount fs/namespace.c:2812 [inline]
Kmods SIG 9e3ffb
    [<000000005ce5eb1b>] do_mount+0x12bb/0x1b30 fs/namespace.c:3141
Kmods SIG 9e3ffb
    [<00000000b642040c>] __do_sys_mount fs/namespace.c:3350 [inline]
Kmods SIG 9e3ffb
    [<00000000b642040c>] __se_sys_mount fs/namespace.c:3327 [inline]
Kmods SIG 9e3ffb
    [<00000000b642040c>] __x64_sys_mount+0x18f/0x230 fs/namespace.c:3327
Kmods SIG 9e3ffb
    [<000000003b024e98>] do_syscall_64+0xf6/0x7d0
Kmods SIG 9e3ffb
arch/x86/entry/common.c:295
Kmods SIG 9e3ffb
    [<00000000ce2b698c>] entry_SYSCALL_64_after_hwframe+0x49/0xb3
Kmods SIG 9e3ffb
Kmods SIG 9e3ffb
exfat_free() should call exfat_free_iocharset(), to prevent a leak
Kmods SIG 9e3ffb
in case we fail after parsing iocharset= but before calling
Kmods SIG 9e3ffb
get_tree_bdev().
Kmods SIG 9e3ffb
Kmods SIG 9e3ffb
Additionally, there's no point copying param->string in
Kmods SIG 9e3ffb
exfat_parse_param() - just steal it, leaving NULL in param->string.
Kmods SIG 9e3ffb
That's independent from the leak or fix thereof - it's simply
Kmods SIG 9e3ffb
avoiding an extra copy.
Kmods SIG 9e3ffb
Kmods SIG 9e3ffb
Fixes: 719c1e182916 ("exfat: add super block operations")
Kmods SIG 9e3ffb
Cc: stable@vger.kernel.org # v5.7
Kmods SIG 9e3ffb
Reported-by: butt3rflyh4ck <butterflyhuangxx@gmail.com>
Kmods SIG 9e3ffb
Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
Kmods SIG 9e3ffb
Signed-off-by: Namjae Jeon <namjae.jeon@samsung.com>
Kmods SIG 9e3ffb
---
Kmods SIG 9e3ffb
 src/super.c | 12 ++++++++----
Kmods SIG 9e3ffb
 1 file changed, 8 insertions(+), 4 deletions(-)
Kmods SIG 9e3ffb
Kmods SIG 9e3ffb
diff --git a/src/super.c b/src/super.c
Kmods SIG 9e3ffb
index 405717e4e3eac6b3233e1e57b0bcc70f56f1c42f..e650e65536f848c544b731f273b5f8839298e850 100644
Kmods SIG 9e3ffb
--- a/src/super.c
Kmods SIG 9e3ffb
+++ b/src/super.c
Kmods SIG 9e3ffb
@@ -273,9 +273,8 @@ static int exfat_parse_param(struct fs_context *fc, struct fs_parameter *param)
Kmods SIG 9e3ffb
 		break;
Kmods SIG 9e3ffb
 	case Opt_charset:
Kmods SIG 9e3ffb
 		exfat_free_iocharset(sbi);
Kmods SIG 9e3ffb
-		opts->iocharset = kstrdup(param->string, GFP_KERNEL);
Kmods SIG 9e3ffb
-		if (!opts->iocharset)
Kmods SIG 9e3ffb
-			return -ENOMEM;
Kmods SIG 9e3ffb
+		opts->iocharset = param->string;
Kmods SIG 9e3ffb
+		param->string = NULL;
Kmods SIG 9e3ffb
 		break;
Kmods SIG 9e3ffb
 	case Opt_errors:
Kmods SIG 9e3ffb
 		opts->errors = result.uint_32;
Kmods SIG 9e3ffb
@@ -686,7 +685,12 @@ static int exfat_get_tree(struct fs_context *fc)
Kmods SIG 9e3ffb
 
Kmods SIG 9e3ffb
 static void exfat_free(struct fs_context *fc)
Kmods SIG 9e3ffb
 {
Kmods SIG 9e3ffb
-	kfree(fc->s_fs_info);
Kmods SIG 9e3ffb
+	struct exfat_sb_info *sbi = fc->s_fs_info;
Kmods SIG 9e3ffb
+
Kmods SIG 9e3ffb
+	if (sbi) {
Kmods SIG 9e3ffb
+		exfat_free_iocharset(sbi);
Kmods SIG 9e3ffb
+		kfree(sbi);
Kmods SIG 9e3ffb
+	}
Kmods SIG 9e3ffb
 }
Kmods SIG 9e3ffb
 
Kmods SIG 9e3ffb
 static const struct fs_context_operations exfat_context_ops = {
Kmods SIG 9e3ffb
-- 
Kmods SIG 9e3ffb
2.31.1
Kmods SIG 9e3ffb