Received: by 2002:ac0:946b:0:0:0:0:0 with SMTP id j40csp2657734imj; Mon, 11 Feb 2019 06:35:12 -0800 (PST) X-Google-Smtp-Source: AHgI3IZYJbDDOUgMXsr7TDyEa4AxP2FVLwaNuk81Deck9+LG+89z+nuxJz/LP3BaLjXnM7548U9G X-Received: by 2002:a65:534b:: with SMTP id w11mr33861734pgr.125.1549895712813; Mon, 11 Feb 2019 06:35:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1549895712; cv=none; d=google.com; s=arc-20160816; b=qwgPqm+Bk7VZ0nVvAvI4HgqMjvif8f+/Z5/9+vtEsGa1RBUbk2b1hm+eSJ4tjZXFTd 276BXCfslM8eXZ3UY0VGikoB91XJ5IdUkJrATVkxHvzvuPXbUyVfShEm9BKR0bbFRoBT SkBTvbnWhw0X/9dfVN09cW0I7wbT1duhFy7KF2OrDBSECHFEAfNww2lu74wkNPWbURfi 5iddHcs2xU+BBAEA1GGEOiDPdhpsK5nMbdEZNWpHwXE/m0suHo/oNFjujmZtdoBHcGd2 UHJgSPyOwPIB+JrU0xOcwu0Po2RI8M/4sMR8xlCRk/zcIQX78UeIlQuIR+NvaskGsClU CQfA== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=6e5fuzMMG/YaLTx+Xh8s/3ACLDdvbRcSTLULs8NY2Vk=; b=LcbJ4Gcw+2QFgyVIlJOZNUHRLi4krWfNGnApIjmRlYIHWe+kOzDNQ9h2o9MQiWSKcu GWQxAFoFaGHvDxfMM9N8jqcmBcbQ+bTxvfHSTQHmza4eWG8IW8WK+GHyHw6ow7dWfXyv /y3sooaYD23OlMrZJ3qIbyYJ8D4XAayUafVaE7DtiJnn+OgtbnaKZVcbKoBWDxwbLFS6 IU3Ryqmrih2AyZpYr2Qjl2l2gXTQqPdmDbLA6NlqYdpzJfASPjwfBy5nP9rk2mYNIt4R 3anmbYMPlm8uROYd1rr2a0vq5dRZJFbXv9UNnpJLMs7EGVNpp872dJap0thbUg8XpZSH 3m6g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=FNrhCUQx; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b5si2225639pgl.254.2019.02.11.06.34.56; Mon, 11 Feb 2019 06:35:12 -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=FNrhCUQx; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730092AbfBKOdT (ORCPT + 99 others); Mon, 11 Feb 2019 09:33:19 -0500 Received: from mail.kernel.org ([198.145.29.99]:40934 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730611AbfBKOdQ (ORCPT ); Mon, 11 Feb 2019 09:33:16 -0500 Received: from localhost (5356596B.cm-6-7b.dynamic.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 543EC218A3; Mon, 11 Feb 2019 14:33:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1549895594; bh=0Hbv8y3jXssBHkC2LFIKj7kdQF8Ty88VXIN7SUvIA0I=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=FNrhCUQxO9tZEEhvxpLclmA+jAyoLcQa6sInTxR1Hm2c+WIC+gA3sdCckkbRCby+R M+t6y84Ld3Vfd+Eb5r93yKHtcNNTp35Drzrl0Zv6M8sqjI9xyh6rrFuHFQPNwj7W82 781dR1qbl+qLtuW9hqRRngGlIvgJp2yRYhd/C4Rg= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Sahitya Tummala , Chao Yu , Jaegeuk Kim , Sasha Levin Subject: [PATCH 4.20 258/352] f2fs: fix use-after-free issue when accessing sbi->stat_info Date: Mon, 11 Feb 2019 15:18:05 +0100 Message-Id: <20190211141903.494985465@linuxfoundation.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190211141846.543045703@linuxfoundation.org> References: <20190211141846.543045703@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review X-Patchwork-Hint: ignore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.20-stable review patch. If anyone has any objections, please let me know. ------------------ [ 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 855a622fb052..a239472f405a 100644 --- a/fs/f2fs/super.c +++ b/fs/f2fs/super.c @@ -1058,9 +1058,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. @@ -1080,6 +1077,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); @@ -3256,30 +3259,30 @@ try_onemore: 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 */ @@ -3403,12 +3406,12 @@ free_meta: 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