Received: by 2002:a05:6358:5282:b0:b5:90e7:25cb with SMTP id g2csp3412018rwa; Tue, 23 Aug 2022 04:28:10 -0700 (PDT) X-Google-Smtp-Source: AA6agR6fR9W/mAftKnKeHKi5TZy/YluVoz4AL1glSNg9BW+gwaTLvn1uqIdhcl5i15uqQiwy9aUi X-Received: by 2002:a05:6402:c45:b0:442:c549:8e6b with SMTP id cs5-20020a0564020c4500b00442c5498e6bmr3286925edb.123.1661254090709; Tue, 23 Aug 2022 04:28:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661254090; cv=none; d=google.com; s=arc-20160816; b=kY/saxdaQ9faTDC+zAbZRpawc06lp5cCFOg0JfK+tQ3gKDFDIKD0rQBNOj+uXvt8qg yxWMW2L3LaoGpPW6WH/UElwppS9Pjd4oHCBGa1lb0EvgUwmkcngsYEFdgY+ye8xLs/dk luC1ZBOIUHqoKqHx2pfUChOQ6hYo2Yp1Lgs+yc9lgSAMXJQl8i4lpk0DSbqZ0UvuLFLF rNbS3AeShNzuH7UyrqAdS08isXKQ5c5zuDf2yBe252n2Pd+hYTPiP0U3R1QXxnscQayb NFppApo4u16SA+dLBB6XFhnb/XlOBQ9zI8E5cUTGgAG9NZaieAidAzLoXFxJf9yxvEz+ Hpfg== 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=yLrkFOzOBZR0gSW1reLzHHTXwJzmwZ/PK9aDTF+6iV4=; b=pcVu8Yy5Myj+FetRQ+klMOpuleC2kLHpGHy52jK7uowGZnOZHxHw0p9PXTLXB4kkLz y9U60eAkOuW9Av4bfz1CiHRS4HFo0sr2zrdEf+BYWUodKKs1UPVQ0xzYMlsU9S0vErl1 gQCiq9pwAp31toIywHZnuPLzt3wRUIwMOd5Dv6KGLWwb1yS+FvqmY721TwYP2NjPGXqz 9+Bd4imEoMlYrX4SJ8SaeosII1ZtCW6ZMzJFZgxbS4zrklLzjp1OyBn/ilQaOvANG+up hWmIciFM9OhePJbU9fD3jocO1rF6ab2tAJdKRmSXsr8RcgAvva06MEBA8H4S6Pg0PsnN v3vw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=e4MX2CwD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id qw35-20020a1709066a2300b0073da4a0f01csi823996ejc.743.2022.08.23.04.27.45; Tue, 23 Aug 2022 04:28:10 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=e4MX2CwD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S239704AbiHWJ0u (ORCPT + 99 others); Tue, 23 Aug 2022 05:26:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42514 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1348812AbiHWJXR (ORCPT ); Tue, 23 Aug 2022 05:23:17 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E18C48F954; Tue, 23 Aug 2022 01:35:40 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 69562B8105C; Tue, 23 Aug 2022 08:35:40 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B8002C433D6; Tue, 23 Aug 2022 08:35:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1661243739; bh=VNBLwqQwMaxRtdYunQRItVfVIZ3NgdC7IdwywpT5kvM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=e4MX2CwDKHzbLbrlJ8XCvgCvom/wP+m0W4i3Kg4M77BkI0YZNX8Tru6VsE5R89ghA iEawymA3PuTVdy4neaM8fKuUH33CNmcwN/4vdRFq3px8m7I82gGqzk9OdgVSa9NDuZ 0eheI+dvHNZLcrZ1Px+tGUPJvLBAQ8BlUI0DYuNQ= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Ye Bin , Chao Yu , Jaegeuk Kim , Sasha Levin Subject: [PATCH 5.19 363/365] f2fs: fix null-ptr-deref in f2fs_get_dnode_of_data Date: Tue, 23 Aug 2022 10:04:24 +0200 Message-Id: <20220823080133.493253720@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220823080118.128342613@linuxfoundation.org> References: <20220823080118.128342613@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Ye Bin [ Upstream commit 4a2c5b7994960fac29cf8a3f4e62855bae1b27d4 ] There is issue as follows when test f2fs atomic write: F2FS-fs (loop0): Can't find valid F2FS filesystem in 2th superblock F2FS-fs (loop0): invalid crc_offset: 0 F2FS-fs (loop0): f2fs_check_nid_range: out-of-range nid=1, run fsck to fix. F2FS-fs (loop0): f2fs_check_nid_range: out-of-range nid=2, run fsck to fix. ================================================================== BUG: KASAN: null-ptr-deref in f2fs_get_dnode_of_data+0xac/0x16d0 Read of size 8 at addr 0000000000000028 by task rep/1990 CPU: 4 PID: 1990 Comm: rep Not tainted 5.19.0-rc6-next-20220715 #266 Call Trace: dump_stack_lvl+0x6e/0x91 print_report.cold+0x49a/0x6bb kasan_report+0xa8/0x130 f2fs_get_dnode_of_data+0xac/0x16d0 f2fs_do_write_data_page+0x2a5/0x1030 move_data_page+0x3c5/0xdf0 do_garbage_collect+0x2015/0x36c0 f2fs_gc+0x554/0x1d30 f2fs_balance_fs+0x7f5/0xda0 f2fs_write_single_data_page+0xb66/0xdc0 f2fs_write_cache_pages+0x716/0x1420 f2fs_write_data_pages+0x84f/0x9a0 do_writepages+0x130/0x3a0 filemap_fdatawrite_wbc+0x87/0xa0 file_write_and_wait_range+0x157/0x1c0 f2fs_do_sync_file+0x206/0x12d0 f2fs_sync_file+0x99/0xc0 vfs_fsync_range+0x75/0x140 f2fs_file_write_iter+0xd7b/0x1850 vfs_write+0x645/0x780 ksys_write+0xf1/0x1e0 do_syscall_64+0x3b/0x90 entry_SYSCALL_64_after_hwframe+0x63/0xcd As 3db1de0e582c commit changed atomic write way which new a cow_inode for atomic write file, and also mark cow_inode as FI_ATOMIC_FILE. When f2fs_do_write_data_page write cow_inode will use cow_inode's cow_inode which is NULL. Then will trigger null-ptr-deref. To solve above issue, introduce FI_COW_FILE flag for COW inode. Fiexes: 3db1de0e582c("f2fs: change the current atomic write way") Signed-off-by: Ye Bin Reviewed-by: Chao Yu Signed-off-by: Jaegeuk Kim Signed-off-by: Sasha Levin --- fs/f2fs/f2fs.h | 6 ++++++ fs/f2fs/file.c | 2 +- fs/f2fs/segment.c | 4 ++-- 3 files changed, 9 insertions(+), 3 deletions(-) --- a/fs/f2fs/f2fs.h +++ b/fs/f2fs/f2fs.h @@ -757,6 +757,7 @@ enum { FI_ENABLE_COMPRESS, /* enable compression in "user" compression mode */ FI_COMPRESS_RELEASED, /* compressed blocks were released */ FI_ALIGNED_WRITE, /* enable aligned write */ + FI_COW_FILE, /* indicate COW file */ FI_MAX, /* max flag, never be used */ }; @@ -3208,6 +3209,11 @@ static inline bool f2fs_is_atomic_file(s return is_inode_flag_set(inode, FI_ATOMIC_FILE); } +static inline bool f2fs_is_cow_file(struct inode *inode) +{ + return is_inode_flag_set(inode, FI_COW_FILE); +} + static inline bool f2fs_is_first_block_written(struct inode *inode) { return is_inode_flag_set(inode, FI_FIRST_BLOCK_WRITTEN); --- a/fs/f2fs/file.c +++ b/fs/f2fs/file.c @@ -2061,7 +2061,7 @@ static int f2fs_ioc_start_atomic_write(s spin_unlock(&sbi->inode_lock[ATOMIC_FILE]); set_inode_flag(inode, FI_ATOMIC_FILE); - set_inode_flag(fi->cow_inode, FI_ATOMIC_FILE); + set_inode_flag(fi->cow_inode, FI_COW_FILE); clear_inode_flag(fi->cow_inode, FI_INLINE_DATA); f2fs_up_write(&fi->i_gc_rwsem[WRITE]); --- a/fs/f2fs/segment.c +++ b/fs/f2fs/segment.c @@ -193,7 +193,7 @@ void f2fs_abort_atomic_write(struct inod if (f2fs_is_atomic_file(inode)) { if (clean) truncate_inode_pages_final(inode->i_mapping); - clear_inode_flag(fi->cow_inode, FI_ATOMIC_FILE); + clear_inode_flag(fi->cow_inode, FI_COW_FILE); iput(fi->cow_inode); fi->cow_inode = NULL; clear_inode_flag(inode, FI_ATOMIC_FILE); @@ -3166,7 +3166,7 @@ static int __get_segment_type_6(struct f return CURSEG_COLD_DATA; if (file_is_hot(inode) || is_inode_flag_set(inode, FI_HOT_DATA) || - f2fs_is_atomic_file(inode)) + f2fs_is_cow_file(inode)) return CURSEG_HOT_DATA; return f2fs_rw_hint_to_seg_type(inode->i_write_hint); } else {