From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8C899C388F3 for ; Mon, 15 Nov 2021 19:40:27 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 6E8F461AA5 for ; Mon, 15 Nov 2021 19:40:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1347303AbhKOTjm (ORCPT ); Mon, 15 Nov 2021 14:39:42 -0500 Received: from mail.kernel.org ([198.145.29.99]:44634 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S245004AbhKOTSV (ORCPT ); Mon, 15 Nov 2021 14:18:21 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 1567E63439; Mon, 15 Nov 2021 18:26:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1637000805; bh=ZSb7yFP4AkMk9ONqo0xkm2qS0HJLqlAMxCfaa0V4PxA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=I4bzCY2vRBjgQEEzRVzvXrJpiXOq7ot7phcWrDOA94OQcSeaBthT/elVllTenoUFI 6+9rp4Pb3xUzLh7YZjlzjGG1yPrHJnL+arELsd1TMTqgittyDoM9PAiGQKVpUWb7UK P6jjbranlRFDM+hR+I6V0UCqQjuXLqfCgXi29TFI= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Dongliang Mu , Chao Yu , Jaegeuk Kim Subject: [PATCH 5.14 798/849] f2fs: fix UAF in f2fs_available_free_memory Date: Mon, 15 Nov 2021 18:04:41 +0100 Message-Id: <20211115165447.230649140@linuxfoundation.org> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20211115165419.961798833@linuxfoundation.org> References: <20211115165419.961798833@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: stable@vger.kernel.org From: Dongliang Mu commit 5429c9dbc9025f9a166f64e22e3a69c94fd5b29b upstream. if2fs_fill_super -> f2fs_build_segment_manager -> create_discard_cmd_control -> f2fs_start_discard_thread It invokes kthread_run to create a thread and run issue_discard_thread. However, if f2fs_build_node_manager fails, the control flow goes to free_nm and calls f2fs_destroy_node_manager. This function will free sbi->nm_info. However, if issue_discard_thread accesses sbi->nm_info after the deallocation, but before the f2fs_stop_discard_thread, it will cause UAF(Use-after-free). -> f2fs_destroy_segment_manager -> destroy_discard_cmd_control -> f2fs_stop_discard_thread Fix this by stopping discard thread before f2fs_destroy_node_manager. Note that, the commit d6d2b491a82e1 introduces the call of f2fs_available_free_memory into issue_discard_thread. Cc: stable@vger.kernel.org Fixes: d6d2b491a82e ("f2fs: allow to change discard policy based on cached discard cmds") Signed-off-by: Dongliang Mu Reviewed-by: Chao Yu Signed-off-by: Jaegeuk Kim Signed-off-by: Greg Kroah-Hartman --- fs/f2fs/super.c | 2 ++ 1 file changed, 2 insertions(+) --- a/fs/f2fs/super.c +++ b/fs/f2fs/super.c @@ -4271,6 +4271,8 @@ free_node_inode: free_stats: f2fs_destroy_stats(sbi); free_nm: + /* stop discard thread before destroying node manager */ + f2fs_stop_discard_thread(sbi); f2fs_destroy_node_manager(sbi); free_sm: f2fs_destroy_segment_manager(sbi);