Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp665139pxv; Thu, 15 Jul 2021 12:53:54 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyIROf2hiKAQcWgnA7gzKmGe/n+CfyxJJd0c5nuNikfcEyvI77kWK3B/WXcmBtNuzd8yV5U X-Received: by 2002:a50:9f8e:: with SMTP id c14mr9403012edf.283.1626378833976; Thu, 15 Jul 2021 12:53:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626378833; cv=none; d=google.com; s=arc-20160816; b=q5GV4Kx+B8tAqiYVVtm9Cpn6uzzx0GGAYManE29nHdvQw5udiD/OYaeruKHW/xe3YY J9uIXIXR4o0buOXbcOWVmM1metuzb/XElxJufwgAO+LhG44wauOC9yBHYhb/OH71/oFE wOfZiElfFjBhNFrE4mtSy3APYejxGXEByPp+H/AH0/PlxhL7XwkLCbsCDfJO4ikCBAAa 8IqtEAGnAwWRLhMIo4pb7hmUo0axQtHlEgG7PyMyP91cvBK/HKswBP15+prTnW0Yi7+j sIw+9Gli5VpKJniN7xy3oTICI1T3ohC9uzdDWsNwrIy66BfFTuYK/ohMuY1fi0knbqws Digw== 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=0vCWMnP4ZleR76cxPwezy4hasmegymCd7G8ZwqX+j7A=; b=fuSRp/+cgIT/numdqtBoAAjOwZq4F98PxQwPGHXNfAP0KcUrIrC+eQ2tluTAUM9qMA 30MoHjCHO4pYj+cPblcN/oPXce5kuGiC5FdobAYxI9ywHMo32QyBDsrFMLiW+46aEblJ IJDNPRTpL6DTx7eiGnCXUlVqbjU6MmxNFY8zi6Vt86jlHAtWgkhRwqnZQqkGFe0+FTNh VbmwvNpcfWXTOb/ybaUcRDOifClsX8e9Ag6HZchBq/AIiYvo/UNQU0FKNgwjHho8QSuR 1LRRLaus0h9IhGWzTzsDW/Brh0gqKLCHxVOHYC9x7Xj4dnQc5fxU0XOR7cQG1aCKrEtS DAHg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=V4FzL2QP; 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 y20si8092807edv.174.2021.07.15.12.53.31; Thu, 15 Jul 2021 12:53:53 -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=V4FzL2QP; 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 S1345623AbhGOTu1 (ORCPT + 99 others); Thu, 15 Jul 2021 15:50:27 -0400 Received: from mail.kernel.org ([198.145.29.99]:58674 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244994AbhGOTTH (ORCPT ); Thu, 15 Jul 2021 15:19:07 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 841DA6127C; Thu, 15 Jul 2021 19:13:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1626376424; bh=FpFr9ycDBi/6VKXtoDCgB/CgKfcVxQkrwcZ8TP1CwTQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=V4FzL2QPO/ZYNZlwoY/WLL/DSycatlEUDExrEjooN8p0M6M2V1EchZ4jzWP8LtyKz 9iij65tIev4F7sMTITS929y5Fcj6f5//myFLVyzUQgQtYVEOA7NZBPR3cZEhbsc5mn 2FDkS8sWuaAGeQabCGctUXcfOjYq382RyVs1Hh0s= 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.13 266/266] f2fs: fix to avoid racing on fsync_entry_slab by multi filesystem instances Date: Thu, 15 Jul 2021 20:40:21 +0200 Message-Id: <20210715182653.678075001@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210715182613.933608881@linuxfoundation.org> References: <20210715182613.933608881@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 @@ -3566,6 +3566,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 @@ -788,13 +788,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); @@ -867,8 +860,6 @@ skip: } } - kmem_cache_destroy(fsync_entry_slab); -out: #ifdef CONFIG_QUOTA /* Turn quotas off */ if (quota_enabled) @@ -878,3 +869,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 @@ -4227,9 +4227,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; @@ -4278,6 +4281,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: @@ -4303,6 +4308,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();