Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp635542pxv; Thu, 15 Jul 2021 12:05:41 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxKLoS39rg6lbelfV2wM3QBtwrZ4LgEFz/G7ok4IU/h0ChwBspi3cVccwQdtwcRWLTlNVWT X-Received: by 2002:a05:6e02:2190:: with SMTP id j16mr3935625ila.144.1626375940931; Thu, 15 Jul 2021 12:05:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626375940; cv=none; d=google.com; s=arc-20160816; b=Eqr+67dE1rQdLtEGKgLQtuY8i02TNZ32Iv6ur1UMPTMA38jwvOL6hM356GQGFVQSaa 2EoCWBr9+paRYMaVlUsrK2eA0KZ1oXw4x7SVgOh8Neb23e/aYJ5qwYc0feC94Oy5uWR+ 2wKAA7lFYQwkR/e4YOXK2MKGBiYRHle/00Q1eKEIrYP1dUawDiM8daD2oRCGAzUfxFgn +ykPYQO5klPYw2CEanm0AQ/nd6pyQLH5OqnukUlJb1WarsmXZn2AEDxTkJkAsvKK9MaY DLe9iR7iWDsEqLFGyF4WyQMVVWKm+UE/j5/t0kTN5yx9GWe0WB7LKtlnDyyG2BYyDtJ+ xarw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=N/qtDt7HEUc4AhQ897KAjNOD1IyFtobWR6UfSFAr+00=; b=jybu0wuyqUo1KPB1hZjcP4NC9NE7oJItRCtvs7Icmyj4GhyH7DVZ1MXNEi+VAfkhjP DoLpABL3d4Gtc5nm4a+Bq+s0kN6+TtcpOlqkYQtIgufGIIJd3bOC+jfbrEjEhrCOsX2f gnBOdKjOdHmwa8milW1vRDwKMwzYO3b7IFRS2FbwxKEqxRFl39H4Ve2EyjKo4uAojE9M Rmd4zhg8mCUM3Fi7WRZN5IXbY0XQjZd7ZGb/g0FDpbx8sqqIZkq1KVVx27FRoPC06ede GFlo41osrXhbrpvwrcBV3qjAjP3KaLLidn+cVbxLciEdyz2LxGO5O5xY+Fmphf8Dtkqh 8mfA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=gRrL9uAa; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id h13si7599556iop.97.2021.07.15.12.05.28; Thu, 15 Jul 2021 12:05:40 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=gRrL9uAa; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242453AbhGOTG5 (ORCPT + 99 others); Thu, 15 Jul 2021 15:06:57 -0400 Received: from mail.kernel.org ([198.145.29.99]:33468 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241018AbhGOS4o (ORCPT ); Thu, 15 Jul 2021 14:56:44 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 396A8613D1; Thu, 15 Jul 2021 18:53:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1626375230; bh=uTfMtEl8lNqdnF0HOgRYsgNEB8zxn1gq3kZz34x9N7c=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=gRrL9uAa1MaI7EVCkgC5nUHBZipo9AB77uNi5tITrHoBlGqOJbSpOAC0+h5DO9ybk 78X3R3+BB6H/UcMVcvQkEWAL+jXX/ovIRcNZQC26KHm0l+49EmhHSLuRcFi3wc0s9S CXzjm4jyolLZichM9Wj99d3Ib4qL6A6wB9KOijUo= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, syzbot+9d90dad32dd9727ed084@syzkaller.appspotmail.com, Chao Yu , Jaegeuk Kim Subject: [PATCH 5.10 215/215] f2fs: fix to avoid racing on fsync_entry_slab by multi filesystem instances Date: Thu, 15 Jul 2021 20:39:47 +0200 Message-Id: <20210715182637.438443933@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210715182558.381078833@linuxfoundation.org> References: <20210715182558.381078833@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Chao Yu commit cad83c968c2ebe97905f900326988ed37146c347 upstream. As syzbot reported, there is an use-after-free issue during f2fs recovery: Use-after-free write at 0xffff88823bc16040 (in kfence-#10): kmem_cache_destroy+0x1f/0x120 mm/slab_common.c:486 f2fs_recover_fsync_data+0x75b0/0x8380 fs/f2fs/recovery.c:869 f2fs_fill_super+0x9393/0xa420 fs/f2fs/super.c:3945 mount_bdev+0x26c/0x3a0 fs/super.c:1367 legacy_get_tree+0xea/0x180 fs/fs_context.c:592 vfs_get_tree+0x86/0x270 fs/super.c:1497 do_new_mount fs/namespace.c:2905 [inline] path_mount+0x196f/0x2be0 fs/namespace.c:3235 do_mount fs/namespace.c:3248 [inline] __do_sys_mount fs/namespace.c:3456 [inline] __se_sys_mount+0x2f9/0x3b0 fs/namespace.c:3433 do_syscall_64+0x3f/0xb0 arch/x86/entry/common.c:47 entry_SYSCALL_64_after_hwframe+0x44/0xae The root cause is multi f2fs filesystem instances can race on accessing global fsync_entry_slab pointer, result in use-after-free issue of slab cache, fixes to init/destroy this slab cache only once during module init/destroy procedure to avoid this issue. Reported-by: syzbot+9d90dad32dd9727ed084@syzkaller.appspotmail.com Signed-off-by: Chao Yu Signed-off-by: Jaegeuk Kim Signed-off-by: Greg Kroah-Hartman --- fs/f2fs/f2fs.h | 2 ++ fs/f2fs/recovery.c | 23 ++++++++++++++--------- fs/f2fs/super.c | 8 +++++++- 3 files changed, 23 insertions(+), 10 deletions(-) --- a/fs/f2fs/f2fs.h +++ b/fs/f2fs/f2fs.h @@ -3462,6 +3462,8 @@ void f2fs_destroy_garbage_collection_cac */ int f2fs_recover_fsync_data(struct f2fs_sb_info *sbi, bool check_only); bool f2fs_space_for_roll_forward(struct f2fs_sb_info *sbi); +int __init f2fs_create_recovery_cache(void); +void f2fs_destroy_recovery_cache(void); /* * debug.c --- a/fs/f2fs/recovery.c +++ b/fs/f2fs/recovery.c @@ -777,13 +777,6 @@ int f2fs_recover_fsync_data(struct f2fs_ quota_enabled = f2fs_enable_quota_files(sbi, s_flags & SB_RDONLY); #endif - fsync_entry_slab = f2fs_kmem_cache_create("f2fs_fsync_inode_entry", - sizeof(struct fsync_inode_entry)); - if (!fsync_entry_slab) { - err = -ENOMEM; - goto out; - } - INIT_LIST_HEAD(&inode_list); INIT_LIST_HEAD(&tmp_inode_list); INIT_LIST_HEAD(&dir_list); @@ -856,8 +849,6 @@ skip: } } - kmem_cache_destroy(fsync_entry_slab); -out: #ifdef CONFIG_QUOTA /* Turn quotas off */ if (quota_enabled) @@ -867,3 +858,17 @@ out: return ret ? ret: err; } + +int __init f2fs_create_recovery_cache(void) +{ + fsync_entry_slab = f2fs_kmem_cache_create("f2fs_fsync_inode_entry", + sizeof(struct fsync_inode_entry)); + if (!fsync_entry_slab) + return -ENOMEM; + return 0; +} + +void f2fs_destroy_recovery_cache(void) +{ + kmem_cache_destroy(fsync_entry_slab); +} --- a/fs/f2fs/super.c +++ b/fs/f2fs/super.c @@ -4027,9 +4027,12 @@ static int __init init_f2fs_fs(void) err = f2fs_create_checkpoint_caches(); if (err) goto free_segment_manager_caches; - err = f2fs_create_extent_cache(); + err = f2fs_create_recovery_cache(); if (err) goto free_checkpoint_caches; + err = f2fs_create_extent_cache(); + if (err) + goto free_recovery_cache; err = f2fs_create_garbage_collection_cache(); if (err) goto free_extent_cache; @@ -4078,6 +4081,8 @@ free_garbage_collection_cache: f2fs_destroy_garbage_collection_cache(); free_extent_cache: f2fs_destroy_extent_cache(); +free_recovery_cache: + f2fs_destroy_recovery_cache(); free_checkpoint_caches: f2fs_destroy_checkpoint_caches(); free_segment_manager_caches: @@ -4103,6 +4108,7 @@ static void __exit exit_f2fs_fs(void) f2fs_exit_sysfs(); f2fs_destroy_garbage_collection_cache(); f2fs_destroy_extent_cache(); + f2fs_destroy_recovery_cache(); f2fs_destroy_checkpoint_caches(); f2fs_destroy_segment_manager_caches(); f2fs_destroy_node_manager_caches();