Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp648307pxv; Thu, 15 Jul 2021 12:25:17 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyq/1ZdXhMO5V0arTSr1XFL9agpwFxlQZk4nyhVwYoyZ5JfAqeez/8QEHcvO/rFeMegS+Y7 X-Received: by 2002:a05:6402:d4c:: with SMTP id ec12mr9369010edb.246.1626377117362; Thu, 15 Jul 2021 12:25:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626377117; cv=none; d=google.com; s=arc-20160816; b=sikQZ0RCm39pNzVOtY8ufHQ9tB8BGcnuP8vP5gFG8xo6T/fbFZz8xmCRnQzvC9xEzE wwceqvy6Q3zxCT8kGl9AdGj3KVPauiRWIiQHOjenjTqHGfh3SVXidofxZG+44eHpxI9t GXkj/WbO/JHsF8NZzipOBgDOOzEqZoO9xBiUtEeipzVPrGmUorUztWjUxtvt8M0t5RO+ 3nrVIZ/ee5bgvvIRUeFFP9gB1cfd339DGe3gXU9IBF4AFApe35MvgsW1QcssP2UeT0AX PVYHlUN+R4ZJeiZQS/VBeBmRotzpTVU1M8P0PY92TchWijJm2m09g/p3osPUG0TBxds/ jpdQ== 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=szFktoTPC0hcTug4YlGI4r0P8eNsFcneUJuoE5H1j0k=; b=YODOtduu3rGYYzybP1QVtE4gfVq0G23ps8p2UjZkSvMjghVcNTnY4L5iguypMtVEK+ B0yWvEMBi1XiqJtZuIkWDIvm8FXGCWAU5Vhpmll9qy5uc0HRLSA02SxW0JaqycGzyTVC uN48yMEz1YNCRnRHuMADMRAzQr3nBMOMlN3xYSuK0kd/QUupgA8SdEqVVa4zBFtE1X1P 3BcQRLWwAFzo7b7a5Hp1I9zpusm2DxFxsis3eoulpW5SAQ9I4nNesbXPkeDIde3kkzXO PU+ZkGcxVnPw7EaxVXVBpk5k4r4KtTQP+w4U9ei3YGNw8Qb97d8TWh97avqLx34orID5 iirQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=bbmqnlQm; 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 j23si10395976eje.501.2021.07.15.12.24.54; Thu, 15 Jul 2021 12:25:17 -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=bbmqnlQm; 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 S245610AbhGOTYT (ORCPT + 99 others); Thu, 15 Jul 2021 15:24:19 -0400 Received: from mail.kernel.org ([198.145.29.99]:45648 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242520AbhGOTHZ (ORCPT ); Thu, 15 Jul 2021 15:07:25 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 9A61B613D6; Thu, 15 Jul 2021 19:03:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1626375804; bh=FMOqtWgbbmv0cWFZ5fXm/IuPiM7DSxYPeD36z7/IRaY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=bbmqnlQmdJUQ5DiRUG7ilyFz+hD+D9RYntv2SW5jNaaMy5mL/jj/KhhK+UqfNILoP Nt17fpkviv08ODveScOPI0BaArLEtawgDEklMPbIDx8+sOPEHS5eFIgjw5IgZoFlPC tYDkrwSQKmp6Ne16HPpWmCFmmslonmcVfSOT6/hg= 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.12 242/242] f2fs: fix to avoid racing on fsync_entry_slab by multi filesystem instances Date: Thu, 15 Jul 2021 20:40:04 +0200 Message-Id: <20210715182635.467745499@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210715182551.731989182@linuxfoundation.org> References: <20210715182551.731989182@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 @@ -3560,6 +3560,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 @@ -787,13 +787,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); @@ -866,8 +859,6 @@ skip: } } - kmem_cache_destroy(fsync_entry_slab); -out: #ifdef CONFIG_QUOTA /* Turn quotas off */ if (quota_enabled) @@ -877,3 +868,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 @@ -4187,9 +4187,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; @@ -4238,6 +4241,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: @@ -4263,6 +4268,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();