Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp2894880rwb; Mon, 15 Aug 2022 13:30:30 -0700 (PDT) X-Google-Smtp-Source: AA6agR5N3iftfkIYVTnWm+opTeWGJ/1dlcBYEv0C4YMpi+2jwKW2CHjgMXqBDaWnuCWwn3Qlqa5Q X-Received: by 2002:a05:6402:328a:b0:43d:f295:f14c with SMTP id f10-20020a056402328a00b0043df295f14cmr16181699eda.299.1660595430527; Mon, 15 Aug 2022 13:30:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660595430; cv=none; d=google.com; s=arc-20160816; b=O6AbL3/XKsKtWX99Kz+7egUxahPhv3OqCySN+W/2QFGVcF6I47JU6G4TCZ3RfkbQPy cDklPN73/qgsnRm5DFkEy6//RoGX8UV0l6SbZQiUKnA9R6NfPeeWDgFvAcuAqqAmcLXU 8jzowEL4qompdH8lGn6Gz46PlbYUiypFc4E9sGzLpVq35TtKCVXSesXjsrjark+7n5qK KDIuaIgFgpxcyL/MLu/hk4sFj/Ttn2zH+F5cn0VcjeuROiNOPkxi5SeXF8TNZMjtVh23 PS2UhKORbIoPSP+MfdEAAvunqnz3Ic65NVC96KEsHxc+XY9idB5qBDBd0xPT1CWThRZy 8H5Q== 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=Liv49+2IEZORTSXdxfhfuOouR7cjMV6QJMfZgu49OVg=; b=KI9vTA+/CbVC3cUoLAMCQsMZUN9xMCnGNQ85ojfuLtC+SQ3a17RlxTcjXXDQACXhzB /lgBQImwygLE+/+HfgGy/4UKaod2LJdDRSZ+CeBgP/ZhNdMdkmCP8NYbff61vx8iA7Xx UZV7GXPodmkmFNI0jTurrkA6Ew9nW6bpPDJVj6tkXqV6us7cJ60tK0MChIonWGb1nFTw EVZ1I8MwD4m19JLjP8w5w4x3EVYNwoDodfCVY5bvllg4r2vj10FAZMTQ7Tu+dGO+YKHO CZWdj2UhK9zQEEjhx+aUnrBjNVV7uXiIVhGr3HUICEDDkz5VYDOpmwB2KgDkmhc1RtAn SgJQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=tSiP4VNK; 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 m7-20020a1709060d8700b00730a11988dcsi7187163eji.562.2022.08.15.13.29.57; Mon, 15 Aug 2022 13:30:30 -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=tSiP4VNK; 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 S1347370AbiHOU1Z (ORCPT + 99 others); Mon, 15 Aug 2022 16:27:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47858 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347082AbiHOUV5 (ORCPT ); Mon, 15 Aug 2022 16:21:57 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EC7A298D3A; Mon, 15 Aug 2022 12:01:08 -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 dfw.source.kernel.org (Postfix) with ESMTPS id 0E68B61230; Mon, 15 Aug 2022 19:01:08 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1689AC433D6; Mon, 15 Aug 2022 19:01:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1660590067; bh=Ll3VrOpxsVmeadqheby9L6HRHAFePhaWMYtkUoqpQtY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=tSiP4VNKNnVW2y8TnavONPG1nQjNeI+AeKQM8mNUPkPDeH4EPYPURm08eP9rTP5AH i2cosss8il3yW3TYaYwA7rVZOHIgAzcdfb78ifC3F8HB7GeH/wW6zzFf5ahcAB5PqQ uIohsAwwWoRV+abtzwE97RDvNsgN6s3okldhMXRg= 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 5.18 0141/1095] ext2: Add more validity checks for inode counts Date: Mon, 15 Aug 2022 19:52:20 +0200 Message-Id: <20220815180435.414430515@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220815180429.240518113@linuxfoundation.org> References: <20220815180429.240518113@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 f6a19f6d9f6d..cdffa2a041af 100644 --- a/fs/ext2/super.c +++ b/fs/ext2/super.c @@ -1059,9 +1059,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; } @@ -1071,6 +1072,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_array(db_count, -- 2.35.1