Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp777707rwb; Mon, 26 Sep 2022 05:52:38 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6BztIZyK5DB8C9bTNKCfhLdzGAq7viRMYg5lnNqdWikNiK2sLhrjLOS65VZ1ejvra4KFbr X-Received: by 2002:a50:fe8c:0:b0:454:31ad:b99a with SMTP id d12-20020a50fe8c000000b0045431adb99amr22162549edt.165.1664196758381; Mon, 26 Sep 2022 05:52:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664196758; cv=none; d=google.com; s=arc-20160816; b=a3+RdDyEyOewW8YorUWzUJesHk7AXpPmT3kNaaXUti7XbQtCOi2rWc+KiioH1POjn3 qhgZdoGiiigO/NUA9tbJT7+IzaMDTsW1SeiZfiDm3iJEuQFKMm6rKe4euvMMtjcgqhxr CRc5/ds5GnFBX2CWoS9BzMaqp3FGSIzshyoFzPZK4/GsnVRfTb7Ax3Qx6Y5efK/CQMe5 uANR8jkuKQuw/MaYsKnY2cvuCuA3GgZB63X2+NS7YcGd9jkz5a7Om+YhQR+0z8YdT//u nXH165SGM29E5m9r7V6tDnn7yeEqYWVft/P0v9JnEnpLD7TF54qGpLV3hhlbrAzOoRRL R3Qw== 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=M7hnvrFTfjyoYivZLqY+Le/jSRuA+Zu4xBo5rE/DMSE=; b=PSFY8PJ6LqgmEU4u6ck2GTo2UeOLiFKjsc/SD3LY5jPO7Aiwtn3/QYyFzARXNPawlV GnnJZyt9PQIHo4GN6nqYBggJOlUYlZqx0y8R8qX9atwntGnCqshABPQZunxvKHk8YDvS 5hZu8l0K258/HlJLu1jBt73EI80HUsqf2O/gJapMPLniRNqFDi0yl60FUzs6ZJqPVlle WBdLvSQtG7ynA2nZcrZmgKRRlbOMU67/j+X2V1XWFyShmXh+5y/LquywMVuV4+mcjapO 52ebcgkVxepQomCtxmLO23LQz/JqN2HAfK8D6pBxB5yZpw4W0OMETx3GqRjt7q83Bnql /X/w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=ZuqXPSbB; 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 fw38-20020a170907502600b00781ebe14ffesi12989082ejc.816.2022.09.26.05.52.12; Mon, 26 Sep 2022 05:52:38 -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=ZuqXPSbB; 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 S237104AbiIZLBh (ORCPT + 99 others); Mon, 26 Sep 2022 07:01:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53884 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237292AbiIZK7T (ORCPT ); Mon, 26 Sep 2022 06:59:19 -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 E3BE6B27; Mon, 26 Sep 2022 03:31:04 -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 A7C83B80924; Mon, 26 Sep 2022 10:31:01 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E6A9CC433C1; Mon, 26 Sep 2022 10:30:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1664188260; bh=TyxACUXGIm6aGe2LuaTJwT1xJzwCVvZ6mrSZAVlEq6k=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ZuqXPSbBmoFXRoY3edL+XwA0VEiEAeqFT6eq3uo0jv8VwGdW3JlD5GmPj+QCbn2K/ 22HhSxmdsvrvHXMF2AzCTLAC+SI07ICNlTMamvEp3qKTcvBgB7iSJuVuHFHuDXbHYn y634ULD0lx3PBunAl4WThd6V3O9NOnmWl56JWIOc= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Dave Chinner , Christoph Hellwig , "Darrick J. Wong" , Dave Chinner , Amir Goldstein Subject: [PATCH 5.10 062/141] xfs: validate inode fork size against fork format Date: Mon, 26 Sep 2022 12:11:28 +0200 Message-Id: <20220926100756.694423580@linuxfoundation.org> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20220926100754.639112000@linuxfoundation.org> References: <20220926100754.639112000@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.2 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 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: Dave Chinner commit 1eb70f54c445fcbb25817841e774adb3d912f3e8 upstream. [backport for 5.10.y] xfs_repair catches fork size/format mismatches, but the in-kernel verifier doesn't, leading to null pointer failures when attempting to perform operations on the fork. This can occur in the xfs_dir_is_empty() where the in-memory fork format does not match the size and so the fork data pointer is accessed incorrectly. Note: this causes new failures in xfs/348 which is testing mode vs ftype mismatches. We now detect a regular file that has been changed to a directory or symlink mode as being corrupt because the data fork is for a symlink or directory should be in local form when there are only 3 bytes of data in the data fork. Hence the inode verify for the regular file now fires w/ -EFSCORRUPTED because the inode fork format does not match the format the corrupted mode says it should be in. Signed-off-by: Dave Chinner Reviewed-by: Christoph Hellwig Reviewed-by: Darrick J. Wong Signed-off-by: Dave Chinner Signed-off-by: Amir Goldstein Acked-by: Darrick J. Wong Signed-off-by: Greg Kroah-Hartman --- fs/xfs/libxfs/xfs_inode_buf.c | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) --- a/fs/xfs/libxfs/xfs_inode_buf.c +++ b/fs/xfs/libxfs/xfs_inode_buf.c @@ -358,19 +358,36 @@ xfs_dinode_verify_fork( int whichfork) { uint32_t di_nextents = XFS_DFORK_NEXTENTS(dip, whichfork); + mode_t mode = be16_to_cpu(dip->di_mode); + uint32_t fork_size = XFS_DFORK_SIZE(dip, mp, whichfork); + uint32_t fork_format = XFS_DFORK_FORMAT(dip, whichfork); - switch (XFS_DFORK_FORMAT(dip, whichfork)) { + /* + * For fork types that can contain local data, check that the fork + * format matches the size of local data contained within the fork. + * + * For all types, check that when the size says the should be in extent + * or btree format, the inode isn't claiming it is in local format. + */ + if (whichfork == XFS_DATA_FORK) { + if (S_ISDIR(mode) || S_ISLNK(mode)) { + if (be64_to_cpu(dip->di_size) <= fork_size && + fork_format != XFS_DINODE_FMT_LOCAL) + return __this_address; + } + + if (be64_to_cpu(dip->di_size) > fork_size && + fork_format == XFS_DINODE_FMT_LOCAL) + return __this_address; + } + + switch (fork_format) { case XFS_DINODE_FMT_LOCAL: /* - * no local regular files yet + * No local regular files yet. */ - if (whichfork == XFS_DATA_FORK) { - if (S_ISREG(be16_to_cpu(dip->di_mode))) - return __this_address; - if (be64_to_cpu(dip->di_size) > - XFS_DFORK_SIZE(dip, mp, whichfork)) - return __this_address; - } + if (S_ISREG(mode) && whichfork == XFS_DATA_FORK) + return __this_address; if (di_nextents) return __this_address; break;