Received: by 2002:a05:6358:5282:b0:b5:90e7:25cb with SMTP id g2csp3328349rwa; Tue, 23 Aug 2022 02:57:45 -0700 (PDT) X-Google-Smtp-Source: AA6agR45IFgaE1G13YqYOk59NgMnHa2VSTwm8R+bxv24rwm6UrOpT5hCtSMS4c4t1aDBnvhGcrnJ X-Received: by 2002:a05:6402:26c6:b0:446:b4aa:5d00 with SMTP id x6-20020a05640226c600b00446b4aa5d00mr3030415edd.63.1661248665018; Tue, 23 Aug 2022 02:57:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661248665; cv=none; d=google.com; s=arc-20160816; b=yf6bPtXEBFNEpRcQAPbXjmlk5RQDuuBCZX+mtd7LYhO6rUqIanmkiGHAR6Ou/+Mf3t YW5wm72dL7R6ec1QYByeAy6+h6KhNPipvy+WIH8pXDcdAQYAVTcK0xLF0PUDEXoLnroj VSJ0p1R5qvrlDis5IDZRx/yte8bOzuJ3KW/sn/U4z61oczLfP6HgoONsq81OJSmviSZN 9z9QJMpZOCZ8lmyCY7SfqCdGkkg8aOWRVfcm8082h7MWb0HQ0mt+kCsrmpSXD3e2VNYj pQXJ/ATQqEaw8lTJm6VDt/Xbds0dR8yFMg+7f3rSWOsCUhIxCvkCRLXURS0zAL9W523Z NLqg== 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=AThmsXy/XWc5cKLtHUFcL7+zaseMBWryLprz28uC6Iw=; b=Vok5uDVatLJpen3gOkUN7Ey7li4Gw0F+9r3ZOSQqckYQ0XzHhrvVJvMWF4ABsqA+41 Zr9/u9AGOQDZpjaxW6nohbfDp5bprUs1oAbfQIQljDTZwVR8I9sBFXuZGk8gXfCVO6MX jHz2dnWDBFOyBFoViwIbSmXGKEy6gBuAQsO2JTgz1MDWZzZZ9nndJnFJ/VExQOF/4Ngg zezxwHUycynS9fq/t3ju3b7iW1Y3pv5RZlamVN8+iQxq2IXYSJwskTp+L2xNgMSwwrcY 2BH3Ks14ZcROFR1GqYT4To3vTTMx+whpu6CM9B9QGh/DfQNnB+L8xlBxUkV0nJUoGXwm aUag== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=tXwh8GvF; 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 hv8-20020a17090760c800b0073132e7e79bsi6474891ejc.784.2022.08.23.02.57.19; Tue, 23 Aug 2022 02:57: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=@linuxfoundation.org header.s=korg header.b=tXwh8GvF; 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 S1351636AbiHWJiy (ORCPT + 99 others); Tue, 23 Aug 2022 05:38:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53150 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1351594AbiHWJh7 (ORCPT ); Tue, 23 Aug 2022 05:37:59 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1BC295A158; Tue, 23 Aug 2022 01:40:48 -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 0030FB81C6A; Tue, 23 Aug 2022 08:39:54 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 50021C4314C; Tue, 23 Aug 2022 08:39:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1661243992; bh=bKOMXVn5Qf7CFuGv6kc/zrvEaFQfUTDMxbBHWEk4aqI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=tXwh8GvFoWbkZxeeWct7H1lxFaT+mLpjhbuuRXWSdAJcohumDgORef7/CSQ8ik6WI 2kG6iLpdginPggKKUQw5/s9uBHBKem+dCIbV6ZpVbpGkxQsrIKlOOPYnb9hoUY3dGi ZtQk0425EnmGWUALWA82Nm2uMuUMFOISwcEHjYkc= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, syzbot+d273f7d7f58afd93be48@syzkaller.appspotmail.com, Jan Kara , Sasha Levin Subject: [PATCH 4.14 041/229] ext2: Add more validity checks for inode counts Date: Tue, 23 Aug 2022 10:23:22 +0200 Message-Id: <20220823080055.066458693@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220823080053.202747790@linuxfoundation.org> References: <20220823080053.202747790@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: Jan Kara [ Upstream commit fa78f336937240d1bc598db817d638086060e7e9 ] Add checks verifying number of inodes stored in the superblock matches the number computed from number of inodes per group. Also verify we have at least one block worth of inodes per group. This prevents crashes on corrupted filesystems. Reported-by: syzbot+d273f7d7f58afd93be48@syzkaller.appspotmail.com Signed-off-by: Jan Kara Signed-off-by: Sasha Levin --- fs/ext2/super.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/fs/ext2/super.c b/fs/ext2/super.c index f3d55f1c0ce4..5f7079b65426 100644 --- a/fs/ext2/super.c +++ b/fs/ext2/super.c @@ -1072,9 +1072,10 @@ static int ext2_fill_super(struct super_block *sb, void *data, int silent) sbi->s_frags_per_group); goto failed_mount; } - if (sbi->s_inodes_per_group > sb->s_blocksize * 8) { + if (sbi->s_inodes_per_group < sbi->s_inodes_per_block || + sbi->s_inodes_per_group > sb->s_blocksize * 8) { ext2_msg(sb, KERN_ERR, - "error: #inodes per group too big: %lu", + "error: invalid #inodes per group: %lu", sbi->s_inodes_per_group); goto failed_mount; } @@ -1084,6 +1085,13 @@ static int ext2_fill_super(struct super_block *sb, void *data, int silent) sbi->s_groups_count = ((le32_to_cpu(es->s_blocks_count) - le32_to_cpu(es->s_first_data_block) - 1) / EXT2_BLOCKS_PER_GROUP(sb)) + 1; + if ((u64)sbi->s_groups_count * sbi->s_inodes_per_group != + le32_to_cpu(es->s_inodes_count)) { + ext2_msg(sb, KERN_ERR, "error: invalid #inodes: %u vs computed %llu", + le32_to_cpu(es->s_inodes_count), + (u64)sbi->s_groups_count * sbi->s_inodes_per_group); + goto failed_mount; + } db_count = (sbi->s_groups_count + EXT2_DESC_PER_BLOCK(sb) - 1) / EXT2_DESC_PER_BLOCK(sb); sbi->s_group_desc = kmalloc (db_count * sizeof (struct buffer_head *), GFP_KERNEL); -- 2.35.1