From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795995044; b=cevg2SLLILkqOy1r/CsvvjWW13ZaRCdezrjSwbPoqSMhVjWl6UXdOJtsocTQb1RTb3ZW CTpfltVHFKNG9pDVhQ7B/tP2X8HXAqNnWHkdiQtBO5FhD9X/6ZFT3VNqnG9JW7tkBAooF UWWmm+tySXdf1ueX5ZsN3Hitq+Hh9bTPQL9tGXlt4TEe/aGhsoI9sVo8xK78+wppgchVy Fb+k8MZuMWPXxjJ/nT9K6k7qa8pGKKkABAdnq1w6Fe46PF9m5nXuu7i3a4uBVd80RYrQP XijvhxIEYgJs2axWxrJDP/7iN9rvKlFIK9WeDtnsqQe+fchts33W/hob7243D0XOQYQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795995044; h=Received:DKIM-Signature:Date:From:To:Cc:Subject:Message-ID: References:MIME-Version:Content-Type:Content-Disposition:In-Reply-To; bh=uiers59JkZI0anHUdzVI3p9H6XV4zR9+mTyBqJORX08=; b=1q4Z1n7Y/X7YYN7q7n5nvxjVO1n++EpIrU3hy/k0xbsxIbjLcRwzeOEbz0VQlg+31ETP C4wHbAQWW7jm6VMQCXPcyo8JqdY6qVR5vvSqdL+/ILCTvfTX5FEZepNa2vny+cwtndgIS W/T4Uf4M1AKrV1N1izJCdeP8TO4JdKlf+uzS22CC2y773lwbQ0BKzqVFuIaBj5myNMnnV 1Pw43/0pfHZt6WhLTO1xpZk156gdCQQrAJiMOkR6U9OYUCgNx5G4NTjRalsv79XCEGBYI YVlLN/m5w52OD67S2mCwi+9kQwpi7MIK4gqamkG1UOCFmGe/SyiSNt+DZ2k95U5GRxQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=AWTInRGf; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 140B5606A31C; Sun, 29 Nov 2026 23:30:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=AWTInRGf Received: by smtp.kernel.org (Postfix) with ESMTPSA id 73D221F000C1; Sun, 29 Nov 2026 23:30:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795995043; bh=uiers59JkZI0anHUdzVI3p9H6XV4zR9+mTyBqJORX08=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=AWTInRGfgNUbMZ9R6WiaAkg77jSJX/SffnmIyH4YGrtTDiRI4MHzBwM5wn5cibf94 iksNCAvUlKnvgggRiD1DpPz7FzqWNWMG7ibuK/CuzkPUshpkpu70d/cjIzyoQ5czyi cF7dJkHw5RMCENKEAqKCbZe/R5rDxdWqupoCJoNFptJLjD106XlOIcEYrPEkpAEssz Xhmf+QkemFRGyKGGgAvtaarVe3RbomghLQxgy90ip0RTEA+rv6/gYgkO6ryFzcjImj YO+y+/RGLcyw3iHcxP/6VW4L5sW9YLXCgzWnqTPrPVnPpnDb2Yp2oPVwZStLYOjbjH ar8RuQE/LZXjw== Date: Sun, 29 Nov 2026 15:30:42 -0800 From: Eric Biggers To: Jake Joyner Cc: Alasdair Kergon , Mike Snitzer , Mikulas Patocka , Benjamin Marzinski , dm-devel@lists.linux.dev, linux-kernel@vger.kernel.org, syzbot+3f8f5b65af8de862dcec@syzkaller.appspotmail.com, stable@vger.kernel.org Subject: Re: [PATCH v2] dm-verity: fix shift-out-of-bounds and overflow in hash level sizes Message-ID: <20261129233042.GD2118@sol> References: <20261123210544.196717-1-jake@jakejoyner.com> X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20261123210544.196717-1-jake@jakejoyner.com> On Mon, Nov 23, 2026 at 01:05:44PM -0800, Jake Joyner wrote: > verity_ctr() computes the number of hash blocks at each level of the > hash tree as > > s = (data_blocks + (1 << n) - 1) >> n > > where n = (i + 1) * hash_per_block_bits, i.e. data_blocks / 2^n rounded > up. This goes wrong in two ways. > > First, the loop that counts the levels only stops once > hash_per_block_bits * levels is 64 or more, so n can be 64 or larger for > the top level. For example, with 1024-byte hash blocks and sha512, > hash_per_block_bits is 4, and a large enough number of data blocks gives > 16 levels and n = 64. Shifting a 64-bit value by 64 or more is undefined > behaviour, and UBSAN reports: > > UBSAN: shift-out-of-bounds in drivers/md/dm-verity-target.c:1598:38 > shift exponent 64 is too large for 64-bit type 'long long unsigned int' > > Second, the addition can overflow. With 512-byte data blocks any 64-bit > number of data blocks is accepted, and for data_blocks close to 2^64 the > sum wraps around, so s is far too small (0 for data_blocks = 2^64 - 1 > and 1 <= n < 64). The hash area then appears to need almost no space, > and such a table is accepted on a small hash device. > > data_blocks is non-zero here (levels is 0 otherwise), so compute the > rounded-up quotient as ((data_blocks - 1) >> n) + 1, which cannot > overflow, and use 1 directly when n >= 64, where the quotient is > always 1. > > Tested on v7.4-rc4 in QEMU: the syzbot reproducer no longer triggers the > UBSAN report, a table with 2^64 - 1 data blocks of 512 bytes is now > rejected with "Hash device is too small", and a small valid table still > loads. > > Fixes: b1bf2de07271 ("dm verity: fix inability to use a few specific devices sizes") > Reported-by: syzbot+3f8f5b65af8de862dcec@syzkaller.appspotmail.com > Closes: https://syzkaller.appspot.com/bug?extid=3f8f5b65af8de862dcec > Cc: stable@vger.kernel.org > Signed-off-by: Jake Joyner > --- > Changes in v2: > - Compute s as ((data_blocks - 1) >> n) + 1 so the addition cannot > overflow, as pointed out by the Sashiko review of v1. > - Describe and test the overflow case; add Cc: stable. > v1: https://lore.kernel.org/r/20261123203413.145298-1-jake@jakejoyner.com > > The v1 review also noted that verity_prefetch_io() can underflow > hash_block_end - hash_block_start when v->hash_end is wrongly small. > That path is only reachable once verity_ctr() has accepted a table whose > level sizes do not match the hash device, which is exactly what the > shift and overflow above allowed; with the sizes computed correctly such > a table is rejected at construction, so the prefetch path is no longer > reached. I did not add a separate change for it. > > The code change is mine. An AI assistant (Claude) helped me reproduce > the syzbot report, write a table-loading test, review the change and > word this description. > > drivers/md/dm-verity-target.c | 7 +++++-- > 1 file changed, 5 insertions(+), 2 deletions(-) > > diff --git a/drivers/md/dm-verity-target.c b/drivers/md/dm-verity-target.c > index 306ed1f6c118..6b6b9116fc28 100644 > --- a/drivers/md/dm-verity-target.c > +++ b/drivers/md/dm-verity-target.c > @@ -1593,10 +1593,13 @@ static int verity_ctr(struct dm_target *ti, unsigned int argc, char **argv) > hash_position = v->hash_start; > for (i = v->levels - 1; i >= 0; i--) { > sector_t s; > + unsigned int shift = (i + 1) * v->hash_per_block_bits; > > v->hash_level_block[i] = hash_position; > - s = (v->data_blocks + ((sector_t)1 << ((i + 1) * v->hash_per_block_bits)) - 1) > - >> ((i + 1) * v->hash_per_block_bits); > + if (shift >= 64) > + s = 1; > + else > + s = ((v->data_blocks - 1) >> shift) + 1; > if (hash_position + s < hash_position) { > ti->error = "Hash device offset overflow"; > r = -E2BIG; The commit message is awfully verbose, but this looks correct. It would be a bit simpler to write it as the following, though: s = (verity_position_at_level(v, v->data_blocks - 1, i) >> v->hash_per_block_bits) + 1; - Eric