Received: by 2002:a05:6358:489b:b0:bb:da1:e618 with SMTP id x27csp6281126rwn; Tue, 13 Sep 2022 01:35:45 -0700 (PDT) X-Google-Smtp-Source: AA6agR6WW62TMUqe/XAWWCOMF4xkz0+pTVMKx3SylWfXU3dZV0SXpFGkplNzSHJtoELhybYWPKJQ X-Received: by 2002:a17:907:2bd8:b0:770:77f2:b7af with SMTP id gv24-20020a1709072bd800b0077077f2b7afmr11800238ejc.545.1663058145535; Tue, 13 Sep 2022 01:35:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663058145; cv=none; d=google.com; s=arc-20160816; b=Cv8kkazYcLs5q3PindiC3+WllV85MhWh5/S71oZYu74j80shpUoBluhvmB7M3kY5U0 fzamqpXjMlGmYpjMl191M/DeAteg1dMghu4m0ndqQtN2mxgRT1wKUcov7pPHG/x6ohfI PAhGRY1rCyNgd4nuXAgiNlLTD1TeLs9vGd7lL74hhfEYhHuYYP/gkj2RfNQlCXXLDwkF wnPIN8Zq74Lgc41PREd00bXN2dichRl2PMFyAiIsi1+IA1yPsilu7ykG437BHjPeKTne gKIwJOBNDAM9F77er3iLnxyy3ClEptfCHbAVCjyC8UFAVkD8/uWPP/FHDlVSw7+HCVqA j2vg== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=y44CpGCdnIWmGhQpnzVyKVN6TVeD/FhPI6sDgDRDfJg=; b=lgm6qpt+tfHTPu3NRrIPPCcDCLf1b4eAKCcvqy859Fq4c2XIl2t5l5rJoQjpLMrePM wmntucImJcQBCaUN46BB4YyJEQ0IrKbMcZO3Nbd7ILyr+8mgWgjvb3oB3L5lPmO7CS4B UKezg2H5pYMIsk2TAvqAHF4nWnz5EPaOnDipHuUe+aI3DiuxvoTGMtqWM83QVJNvSLDP dwQlI6uk7EeVyM9plnusmc8FMYsG33latah7lcmsO/532R3gyhvezyX9foahV6+wfdln boqdq8YHXs2AC8r0GYMGxMUet9NN+Yn5AYpEumtcmnWX9NIUHNncXNXpq3Or5zofJM7Q i11A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=k1awtXj5; 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=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id fi16-20020a056402551000b00447c736ddaasi7946049edb.611.2022.09.13.01.35.12; Tue, 13 Sep 2022 01:35:45 -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=@kernel.org header.s=k20201202 header.b=k1awtXj5; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231228AbiIMHs3 (ORCPT + 99 others); Tue, 13 Sep 2022 03:48:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56224 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231259AbiIMHsY (ORCPT ); Tue, 13 Sep 2022 03:48:24 -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 3A3A75A3CA for ; Tue, 13 Sep 2022 00:48:20 -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 D5529B80E19 for ; Tue, 13 Sep 2022 07:48:18 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 21FE0C433C1; Tue, 13 Sep 2022 07:48:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1663055297; bh=Of/zRCi4LPBlW8V1x2NpTuzZS4rJePo9idcJOEnYoAI=; h=From:To:Cc:Subject:Date:From; b=k1awtXj5VWUA7HRjnaeNKOernHCW4kLVx01QqRuCW220YsLBxeBVmzEnV4DPx54Fv uroA8Q1achGI2lJHwbSR4RgXa2DJ0RX+Gi0A5TvCXjIBmHC7ybBE5JGfU8HSgjFfWZ Jmosnzg/L7YlkwtqLF8Hw/t8+UarGTHrg2NwN8aNkxheKx0J3g3MBtUjCsKGYMf1o1 NPt3TVLWoF8T1DkmN802WNmIREaU1r8xt+Fg8RurBbSYJ4FWUrvR9aOw9cg+t+W6+B FFGJBdvHVrGqPRJgBkqfPseERRxVv12qWob9k53huWnW4BqIJOthsw5DVXL2FvJ1Hh krdAL5vLXXd9A== From: Chao Yu To: jaegeuk@kernel.org Cc: linux-f2fs-devel@lists.sourceforge.net, linux-kernel@vger.kernel.org, Chao Yu Subject: [PATCH v3] f2fs: fix to detect corrupted meta ino Date: Tue, 13 Sep 2022 15:48:12 +0800 Message-Id: <20220913074812.2300528-1-chao@kernel.org> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 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 It is possible that ino of dirent or orphan inode is corrupted in a fuzzed image, occasionally, if corrupted ino is equal to meta ino: meta_ino, node_ino or compress_ino, caller of f2fs_iget() from below call paths will get meta inode directly, it's not allowed, let's add sanity check to detect such cases. case #1 - recover_dentry - __f2fs_find_entry - f2fs_iget_retry case #2 - recover_orphan_inode - f2fs_iget_retry Signed-off-by: Chao Yu --- v3: - update commit title/message - change logic inside f2fs_iget() rather than its caller fs/f2fs/inode.c | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/fs/f2fs/inode.c b/fs/f2fs/inode.c index cde0a3dc80c3..1baac6056733 100644 --- a/fs/f2fs/inode.c +++ b/fs/f2fs/inode.c @@ -487,6 +487,12 @@ static int do_read_inode(struct inode *inode) return 0; } +static bool is_meta_ino(struct f2fs_sb_info *sbi, unsigned int ino) +{ + return ino == F2FS_NODE_INO(sbi) || ino == F2FS_META_INO(sbi) || + ino == F2FS_COMPRESS_INO(sbi); +} + struct inode *f2fs_iget(struct super_block *sb, unsigned long ino) { struct f2fs_sb_info *sbi = F2FS_SB(sb); @@ -497,17 +503,22 @@ struct inode *f2fs_iget(struct super_block *sb, unsigned long ino) if (!inode) return ERR_PTR(-ENOMEM); + if (is_meta_ino(sbi, ino)) { + if (!(inode->i_state & I_NEW)) { + f2fs_err(sbi, "detect corrupted inode no:%lu, run fsck to repair", ino); + set_sbi_flag(sbi, SBI_NEED_FSCK); + ret = -EFSCORRUPTED; + trace_f2fs_iget_exit(inode, ret); + iput(inode); + return ERR_PTR(ret); + } + goto make_now; + } + if (!(inode->i_state & I_NEW)) { trace_f2fs_iget(inode); return inode; } - if (ino == F2FS_NODE_INO(sbi) || ino == F2FS_META_INO(sbi)) - goto make_now; - -#ifdef CONFIG_F2FS_FS_COMPRESSION - if (ino == F2FS_COMPRESS_INO(sbi)) - goto make_now; -#endif ret = do_read_inode(inode); if (ret) -- 2.25.1