Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp3694855imu; Mon, 28 Jan 2019 09:10:59 -0800 (PST) X-Google-Smtp-Source: ALg8bN5OEW9mXLzJDvM3mrWl/bM3nOrlehMutGMtcsZL0b3u+uGntekdmQqVX9xBBoVqe3+3M8ss X-Received: by 2002:a63:c503:: with SMTP id f3mr19983279pgd.431.1548695459392; Mon, 28 Jan 2019 09:10:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1548695459; cv=none; d=google.com; s=arc-20160816; b=nFlBj9iriEeMx5O7Gj4IhO7TIAlyCe6ehBOZADlMviSfSBh0IX7DPZlhXwkRdo3Ig3 SKxCp1p5FMUHMBYWE9e1GDwPRsccrXYMeSrNFVMxJH+eNi8LU6TjBrtc/8Nx98CHZmrP RhAYEWsNmNpN1OwvYa7ylh6RPky4MvSKxX1QjyzJKZRXhuQuqdj8PfeCep0EuViI420Z DbbDZvI8967bXwx6i9tcmLV5hD/K4FbF+zzdB8AKH7SIzpNMi98AF9MNXbD6248t44Qj aYjbUiRMMZnsihkQC6heqCxEpY9tzOSxzx1tym9aykLpUmiU5u6SWFV4z1wFW62bp/ar eeeQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=hnL3KCbTuAGT3y71TxjkN4Iracd1hVpwNGvAB2aZt8w=; b=G/v88DwhfLJGQ9u/8f81ih5a2QTEu5aNEbayt3+bt1JO+CG+rUF11xniAYYMPtPfql RVeeQFPxQgxha44NGx7OrxtwEm8qxc9TKcgRoeP7RQVJaBuTMGVExIWfy24hP6xiV/8K zmrtJ+oGVZXju0UEbyLqoMLkO4hsf7C566qItFQngGiPuLjRPY4q5zScEJjKvRmQUkcS YPEBxUbrzxeH7mSgg0iXW+M6gk4bKG5WbfSubgATCq2D9/bJGx9wcSb/e9YBGf7Emobf srBoka5wVkplO0D9zvvOeOyXZvc/NjD1a217ED1cgy87ozwjFLWmj6fQIlk1maD/rxuW d0QQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=i0ETusAH; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id e68si33364296pfb.101.2019.01.28.09.10.44; Mon, 28 Jan 2019 09:10:59 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=i0ETusAH; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727959AbfA1RKf (ORCPT + 99 others); Mon, 28 Jan 2019 12:10:35 -0500 Received: from mail.kernel.org ([198.145.29.99]:36052 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732757AbfA1QKS (ORCPT ); Mon, 28 Jan 2019 11:10:18 -0500 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id D850421852; Mon, 28 Jan 2019 16:10:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1548691817; bh=3Iubp7GXKRgKQsPBPU3sK0hPRU7tYTDT7ou4wQD1BsA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=i0ETusAHCsnnRgfrjBYjr1dBmRepZYZ4B+UpgT2GaEfNKSbpznSdbu4qdXg6rT/m8 rcOFZVssQ0okLIAXhkYyzzbmXeQHWfRn+fvC1pBlBMaxAgCPtOM68BjmfOrSgk+VWB RdwgmouKfnJu0qVdFIyYLitPBzxLfWqFBY0ZbE6o= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Sahitya Tummala , Jaegeuk Kim , Sasha Levin , linux-f2fs-devel@lists.sourceforge.net Subject: [PATCH AUTOSEL 4.19 227/258] f2fs: fix use-after-free issue when accessing sbi->stat_info Date: Mon, 28 Jan 2019 10:58:53 -0500 Message-Id: <20190128155924.51521-227-sashal@kernel.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20190128155924.51521-1-sashal@kernel.org> References: <20190128155924.51521-1-sashal@kernel.org> MIME-Version: 1.0 X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Sahitya Tummala [ Upstream commit 60aa4d5536ab7fe32433ca1173bd9d6633851f27 ] iput() on sbi->node_inode can update sbi->stat_info in the below context, if the f2fs_write_checkpoint() has failed with error. f2fs_balance_fs_bg+0x1ac/0x1ec f2fs_write_node_pages+0x4c/0x260 do_writepages+0x80/0xbc __writeback_single_inode+0xdc/0x4ac writeback_single_inode+0x9c/0x144 write_inode_now+0xc4/0xec iput+0x194/0x22c f2fs_put_super+0x11c/0x1e8 generic_shutdown_super+0x70/0xf4 kill_block_super+0x2c/0x5c kill_f2fs_super+0x44/0x50 deactivate_locked_super+0x60/0x8c deactivate_super+0x68/0x74 cleanup_mnt+0x40/0x78 Fix this by moving f2fs_destroy_stats() further below iput() in both f2fs_put_super() and f2fs_fill_super() paths. Signed-off-by: Sahitya Tummala Reviewed-by: Chao Yu Signed-off-by: Jaegeuk Kim Signed-off-by: Sasha Levin --- fs/f2fs/super.c | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/fs/f2fs/super.c b/fs/f2fs/super.c index 338138b34993..c9639ef0e8d5 100644 --- a/fs/f2fs/super.c +++ b/fs/f2fs/super.c @@ -1039,9 +1039,6 @@ static void f2fs_put_super(struct super_block *sb) f2fs_write_checkpoint(sbi, &cpc); } - /* f2fs_write_checkpoint can update stat informaion */ - f2fs_destroy_stats(sbi); - /* * normally superblock is clean, so we need to release this. * In addition, EIO will skip do checkpoint, we need this as well. @@ -1061,6 +1058,12 @@ static void f2fs_put_super(struct super_block *sb) iput(sbi->node_inode); iput(sbi->meta_inode); + /* + * iput() can update stat information, if f2fs_write_checkpoint() + * above failed with error. + */ + f2fs_destroy_stats(sbi); + /* destroy f2fs internal modules */ f2fs_destroy_node_manager(sbi); f2fs_destroy_segment_manager(sbi); @@ -2980,30 +2983,30 @@ static int f2fs_fill_super(struct super_block *sb, void *data, int silent) f2fs_build_gc_manager(sbi); + err = f2fs_build_stats(sbi); + if (err) + goto free_nm; + /* get an inode for node space */ sbi->node_inode = f2fs_iget(sb, F2FS_NODE_INO(sbi)); if (IS_ERR(sbi->node_inode)) { f2fs_msg(sb, KERN_ERR, "Failed to read node inode"); err = PTR_ERR(sbi->node_inode); - goto free_nm; + goto free_stats; } - err = f2fs_build_stats(sbi); - if (err) - goto free_node_inode; - /* read root inode and dentry */ root = f2fs_iget(sb, F2FS_ROOT_INO(sbi)); if (IS_ERR(root)) { f2fs_msg(sb, KERN_ERR, "Failed to read root inode"); err = PTR_ERR(root); - goto free_stats; + goto free_node_inode; } if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size || !root->i_nlink) { iput(root); err = -EINVAL; - goto free_stats; + goto free_node_inode; } sb->s_root = d_make_root(root); /* allocate root dentry */ @@ -3121,12 +3124,12 @@ static int f2fs_fill_super(struct super_block *sb, void *data, int silent) free_root_inode: dput(sb->s_root); sb->s_root = NULL; -free_stats: - f2fs_destroy_stats(sbi); free_node_inode: f2fs_release_ino_entry(sbi, true); truncate_inode_pages_final(NODE_MAPPING(sbi)); iput(sbi->node_inode); +free_stats: + f2fs_destroy_stats(sbi); free_nm: f2fs_destroy_node_manager(sbi); free_sm: -- 2.19.1