Received: by 2002:a05:6358:5282:b0:b5:90e7:25cb with SMTP id g2csp3468257rwa; Tue, 23 Aug 2022 05:22:59 -0700 (PDT) X-Google-Smtp-Source: AA6agR6B4Qh8c+UTlsQrs3GfVyZzJnqfpIRvDrOF7TFAX89+0UNjYDusFZ1l9+2GhTQ39TDN7Bqw X-Received: by 2002:a65:460c:0:b0:429:f162:78e1 with SMTP id v12-20020a65460c000000b00429f16278e1mr20741624pgq.7.1661257378741; Tue, 23 Aug 2022 05:22:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661257378; cv=none; d=google.com; s=arc-20160816; b=hcfAzBgB7Y25WObNcPyXIjc/NK0XM7pR5FHQXrR2FYIwGBGWjXzHHwE5rAySFr2Dq7 pz/zXH+nPl/xX5LtHkO1C1jxuYSO3+JyK0eA6ltRmA4Zz3h+XAsLxOufEbF+WSY/0Hvc v8gUHNw0TqJ5bHlB9AphzKT09+tFfVzVPkhTuW3uO/V8089CL3K6T5pE9oiEBmNUeuIK NWDRe/bdbFKuj2wElBQJ4rBHnZPPaYWBRm8Cx6C/jjz1WoYwR8fX9fZf8IR2YKe5hKuv wH+0si01+xHegM9Js1qgs92bHTNACcY4r/K0xonyDPKd0HAiuSdh0s0N3asxCOfoZ1d7 Ei2w== 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=E2afZOPDN4/wo52xZrdgD8QKCd4l0je7uw4fnCujuL8=; b=rWBsKnKIn/PhfpieNmgiGZHxWwXB8OfFMoHOugl2done4JSrWfwh8qyEHBT00E1VOT 1Kz0vyERnPrVy/X/SU9YFnRdqCh2u4kSF1ys36xX7txpVrOn8JxfylwzIL/9A3UB7c3f 8YyRSgnTAdyhPJvPGmvhSN9SyaMW9MdyuI8kCD++NTIb5BbBrbXlllJspqJy+YpmB66O IQ4TZkzcXYOQ3w95Ha/m2FVy9oYpDeBr93x6HmKR0ylwOqJAQ2cJqfhqAIKvZYSlgtSN 50I9iRLsCH6v3Xd1wrATclUjtKgcOnjDkEovh2wHm7fvu7DIw51jdYTnvNblMgIzjDKj lraA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Ho3Wk9nd; 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 i6-20020a170902cf0600b0016786f2d1d8si1666345plg.475.2022.08.23.05.22.47; Tue, 23 Aug 2022 05:22:58 -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=Ho3Wk9nd; 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 S1354347AbiHWKhN (ORCPT + 99 others); Tue, 23 Aug 2022 06:37:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42616 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1354899AbiHWKWX (ORCPT ); Tue, 23 Aug 2022 06:22:23 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 106C513F2D; Tue, 23 Aug 2022 02:03:38 -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 CB69CB81C53; Tue, 23 Aug 2022 09:03:36 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3F10EC433C1; Tue, 23 Aug 2022 09:03:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1661245415; bh=2VWKfxtQ5ExyRo+X1gZyywgtIu3OQS7nZSsQWwD9TLw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Ho3Wk9ndJF+OqHrntSJxRqe3+dbxODMK+Py8dzndkQJQVTKBkTnx7p0ZaB2T+fk7i kwk1cwBJxqnBT42SIyNgnmEr/7iRiIwFfGZTCHAol89P3jAACmIuUCPM/uvot3yrY8 IZxZBZeXE95PBoc01tSF52XfvcvxlMI48rE3941A= 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.19 041/287] ext2: Add more validity checks for inode counts Date: Tue, 23 Aug 2022 10:23:30 +0200 Message-Id: <20220823080101.649109026@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220823080100.268827165@linuxfoundation.org> References: <20220823080100.268827165@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 ad9fd08f66ba..44a1f356aca2 100644 --- a/fs/ext2/super.c +++ b/fs/ext2/super.c @@ -1088,9 +1088,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; } @@ -1100,6 +1101,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