Received: by 2002:a05:6358:f14:b0:e5:3b68:ec04 with SMTP id b20csp487923rwj; Thu, 22 Dec 2022 10:12:56 -0800 (PST) X-Google-Smtp-Source: AMrXdXuhlQwNv8Z7GlDsR8wUEfndyVHHkny324KlhxlGQfVTdxc3m/4lLDJLKIk8ogB7tQ0t1ZJZ X-Received: by 2002:a17:902:da83:b0:189:8425:730d with SMTP id j3-20020a170902da8300b001898425730dmr9110105plx.27.1671732775789; Thu, 22 Dec 2022 10:12:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671732775; cv=none; d=google.com; s=arc-20160816; b=SB6y4UcVbuE24S/+iBHWXpt5mJUtN3iJ+KDG7Oqk7sSr9Q+zXdqkiRR6nRAFcon/OZ cTta67OuyH+n7m3jDerv6SmQp0qTCZ5V68kYvyiEYldxPFwMSEMCoQ3PT+1MdXJUKCdW 1tNuRQxqqI9Ng177w6RnWUMpbu32fx89Q9c0iXS3nDMaMM6oLaa3wXVlZxYrt/gs0kxf 2DNS33FUR46AvXcCtswvnjyjzGaKirPk9vo8s5JHvrHHDqZl/fTis0mclkbcKst4zSiR npzGv2D6Qmmo59ODtz9HpMtC9s0LnkOnwap4K6cqM4gb6uIt7dx6+HERrq3hmOdQI0zg +fNQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=Nv1jUoN+7aOf0QlLvEjPnLq5Kf0NnGIpLcvc0N7GVQY=; b=uJhTu/phJjNQ+TGYDnVZTu+CBSpPDKJfflhZMLeWEacjsGRxDg4kb92+yBaqQNZ7bU aWijiDfy6MH9539hKeKXryOQdTWzHOa0fg2c3/tWBR97KeljIcAA5Q651Gx6I+t9HRrb o64S+fagiPzNScaRc5R1xaStjmsbu4SQ2wSDjk/YcP6oi7Ykizss3IH1LCgjkSQvci/j fLgZc+acvuReRyFmMFtcZWmG3Rx85oEtS6mq6OGEZEla2a/V3+MdquGD2ngHdpvrypXQ 1IvntN4sQAYPGBMaq4dyaDvm5v899TOIDU131L/WFLERx8LpnQVy1bG+3OAzi6YEgl2A qmZQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="O+/8KgAV"; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-ext4-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 iw2-20020a170903044200b001893689660asi1012408plb.209.2022.12.22.10.12.37; Thu, 22 Dec 2022 10:12:55 -0800 (PST) Received-SPF: pass (google.com: domain of linux-ext4-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="O+/8KgAV"; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-ext4-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 S229526AbiLVSJG (ORCPT + 99 others); Thu, 22 Dec 2022 13:09:06 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47742 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229524AbiLVSJE (ORCPT ); Thu, 22 Dec 2022 13:09:04 -0500 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6656817E2A; Thu, 22 Dec 2022 10:09:03 -0800 (PST) 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 sin.source.kernel.org (Postfix) with ESMTPS id 7DF73CE1BBF; Thu, 22 Dec 2022 18:09:01 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 888A8C433EF; Thu, 22 Dec 2022 18:08:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1671732539; bh=NRWCSSJiKT9ebsO+yFv6XlrvJQ8lw4SJc9ho5RGZ8kQ=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=O+/8KgAVud6rfrWzxP+YdfiPHcK/C2uyvMeKfRTVp5GXnfxlTSw2YhTIZ1suPtB+g qa1Vzj6th9MaT9J68NdAap/uY2YFhNb9CDl8F+pbqazkGKHMAvfEN9QGorEiWYuoYo kZ9KpbK36As7+rEwBKrmzRQCVBbcStXbYsyvXzGD3V6P5Ko5M6V5MMlDH/io2lavKm 661bNCMaBttnvQON6e1pMcKeusaf8/flZELGwfrMk/AmoRnUmtRvl7jbqa34tbYgUN ACHf1U0EU019/dPcX+GligpopT/J76AsClKBhd0wc+vLwlyA7Kue72tCQdaVxE+pQ+ BJsffRUwMiLGA== Date: Thu, 22 Dec 2022 10:08:59 -0800 From: "Darrick J. Wong" To: Theodore Ts'o Cc: Jun Nie , adilger.kernel@dilger.ca, linux-ext4@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] ext4: fix underflow in group bitmap calculation Message-ID: References: <20221222020244.1821308-1-jun.nie@linaro.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: 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 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-ext4@vger.kernel.org On Thu, Dec 22, 2022 at 12:41:58PM -0500, Theodore Ts'o wrote: > On Thu, Dec 22, 2022 at 10:02:44AM +0800, Jun Nie wrote: > > There is case that s_first_data_block is not 0 and block nr is smaller than > > s_first_data_block when calculating group bitmap during allocation. This > > underflow make index exceed es->s_groups_count in ext4_get_group_info() > > and trigger the BUG_ON. > > > > Fix it with protection of underflow. > > When was this happening, and why? If blocknr is less than > s_first_data_block, this is either a insufficient input validation, > insufficient validation to detection file system corruption. or some > other kernel bug. > > Looking quickly at the code and the repro, it appears that issue is > that FS_IOC_GETFSMAP is getting passed a stating physical block of 0 > in fmh_keys[0] when on a file system with a blocksize of 1k (in which > case s_first_data_block is 1). It's unclear to me what Question -- on a 1k-block filesystem, are the first 1024 bytes of the device *reserved* by ext4 for whatever bootloader crud goes in there? Or is that space undefined in the filesystem specification? I never did figure that out when I was writing the ondisk specification that's in the kernel, but maybe you remember? > FS_IOC_GETFSMAP should *do* when passed a value which requests that it > provide a mapping for a block which is out of bounds (either too big, > or too small)?. Should it return an error? Should it simply not > return a mapping? The map page for ioctl_getfsmap() doesn't shed any > light on this question. > > Darrick, you designed the interface and wrote most of fs/ext4/fsmap.c. > Can you let us know what is supposed to happen in this case? Many > thanks!! If those first 1024 bytes are defined to be reserved in the ondisk format, then you could return a mapping for those bytes with the owner code set to EXT4_FMR_OWN_UNKNOWN. If, however, the space is undefined, then going off this statement in the manpage: "For example, if the low key (fsmap_head.fmh_keys[0]) is set to (8:0, 36864, 0, 0, 0), the filesystem will only return records for extents starting at or above 36 KiB on disk." I think the 'at or above' clause means that ext4 should not pass back any mapping for the byte range 0-1023 on a 1k-block filesystem. If the low key is set to (8:0, 0, 0, 0, 0) and high key is set to (8:0, 1023, 0, 0, 0) then ext4 shouldn't return any mapping at all, because there's no space usage defined for that region of the disk. If the low key is set to (8:0, 0, 0, 0, 0) and high key is set to all ones, then ext4 can return mappings for the primary superblock at offset 1024. --D > > > Fixes: 72b64b594081ef ("ext4 uninline ext4_get_group_no_and_offset()") > > This makes ***no*** sense; the commit in question is from 2006, which > means that in some jourisdictions it's old enough to drive a car. :-) > Futhermore, all it does is move the function from an inline function > to a C file (in this case, balloc.c). It also long predates > introduction of FS_IOC_GETFSMAP support, which was in 2017. > > I'm guessing you just did a "git blame" and blindly assumed that > whatever commit last touched the C code in question was what > introduced the problem? > > Anyway, please try to understand what is going on instead of doing the > moral equivalent of taking a sledgehammer to the code until the > reproducer stops triggering a BUG. It's not enough to shut up the > reproducer; you should understand what is happening, and why, and then > strive to find the best fix to the problem. Papering over problems in > the end will result in more fragile code, and the goal of syzkaller is > to improve kernel quality. But syzkaller is just a tool and used > wrongly, it can have the opposite effect. > > Regards, > > - Ted