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

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