From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtpout-03.galae.net (smtpout-03.galae.net [185.246.85.4]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CB0353254BB for ; Tue, 25 Aug 2026 13:42:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.246.85.4 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787665375; cv=none; b=P7HekqARC8EnYzl6SQNrx8Wh3rhjpkNvYg8FHpMIFiKfRhB9woqKYWDbbaWWnu9T7WjBCOo+LrXNZ3riH4dB9hu1lWOw9UE+qnNdrVXozGhi018BwXtc0f00samL7Qio2W7WlfSVRj5tl2slVThDiligjoGrLKEgOxWzwxo3ZJI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787665375; c=relaxed/simple; bh=PAP3yWg4vGgFhAvbs3WdmbW2vN9xoZNmh1/sQ2pb6as=; h=From:To:Cc:Subject:In-Reply-To:References:Date:Message-ID: MIME-Version:Content-Type; b=Da/aUKFnTXdgFRTq5+lqnjdSd8DAH2Nw2GS8slI01PIy8RgvBfhsAt69wj3tRVTKTdU3w2XfRnBTNRZlGEqsJ/cbmeIQuvOA1U6ugWzj2Abllnq8Wsx9E0ALn+1FjPIgFpWLpZaZuKFedIUKjDG0lz2v8Eo8gXsEwIHVz9Eg2N8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b=dGCzR00m; arc=none smtp.client-ip=185.246.85.4 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bootlin.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b="dGCzR00m" Received: from smtpout-01.galae.net (smtpout-01.galae.net [212.83.139.233]) by smtpout-03.galae.net (Postfix) with ESMTPS id 0FAB04E4138B; Tue, 25 Aug 2026 13:42:50 +0000 (UTC) Received: from mail.galae.net (mail.galae.net [212.83.136.155]) by smtpout-01.galae.net (Postfix) with ESMTPS id D7634604C4; Tue, 25 Aug 2026 13:42:49 +0000 (UTC) Received: from [127.0.0.1] (localhost [127.0.0.1]) by localhost (Mailerdaemon) with ESMTPSA id 4864F11C7A0D5; Tue, 25 Aug 2026 15:42:36 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=dkim; t=1787665365; h=from:subject:date:message-id:to:cc:mime-version:content-type: content-transfer-encoding:in-reply-to:references; bh=IyGOw4U4ix5P0wWcI0DA7kOT20fFy32UpdeDywIjYwA=; b=dGCzR00m9UIpEy5m7VlUEVwJdxrJnrQ+CrWjVndRowk/uUUXXg3pBKqXZNXccj48IH0YvJ dzbZDs9RE2dOBwLcSqs725Trr6vV9p/KeIVJM4nxFvqJMHMnGsmE/9hkDw3rCYqwXBZOOr Gq4ngQkcPJXqQXqqbWoRnbeuxo9EzXmHZXLivj6y+t5Bw9U0nldC++JSJl7Bdt8XYbxTR7 cxvYC5HCYi5ale2ogffV1bwiWhLD0wRsM+NgRzcH8yUB+LqyTtqHgkPrCOtXYA5XfnkA1U jLa1ed52EwcN/+vjtXW5mCskKLIZVqKEAgYMO7Shf6TmN8SPS2CTzdMsjGrfFw== From: Miquel Raynal To: Griffin Kroah-Hartman Cc: Richard Weinberger , Vignesh Raghavendra , linux-mtd@lists.infradead.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] mtd: parsers: afs: add size check to v2 partition In-Reply-To: <20260824-mtd_break_looper-v1-1-e46d7f02faab@kroah.com> (Griffin Kroah-Hartman's message of "Mon, 24 Aug 2026 15:50:27 +0200") References: <20260824-mtd_break_looper-v1-1-e46d7f02faab@kroah.com> User-Agent: mu4e 1.12.12; emacs 30.2 Date: Tue, 25 Aug 2026 15:42:35 +0200 Message-ID: <87cxv6gvuc.fsf@bootlin.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Last-TLS-Session-Version: TLSv1.3 Hello Griffin, Thanks for the patch! On 24/08/2026 at 15:50:27 +02, Griffin Kroah-Hartman wr= ote: > Add a size check to the loop in afs_parse_v2_partition(), avoiding > walking out of the imginfo[] array bounds if a malicious packet fakes a "packet" refers to network wording, whereas here, in the MTD world, we would rather talk about a malicious image. > large region count. > > Assisted-by: gkh_clanker_t1000 > Signed-off-by: Griffin Kroah-Hartman I guess such a fix would require a Fixes tag, as well as Cc'ing the stable@vger.kernel.org alias. > --- > drivers/mtd/parsers/afs.c | 12 +++++++++--- > 1 file changed, 9 insertions(+), 3 deletions(-) > > diff --git a/drivers/mtd/parsers/afs.c b/drivers/mtd/parsers/afs.c > index 26116694c821..2c6f8768312c 100644 > --- a/drivers/mtd/parsers/afs.c > +++ b/drivers/mtd/parsers/afs.c > @@ -287,12 +287,18 @@ static int afs_parse_v2_partition(struct mtd_info *= mtd, > block_start, block_end); >=20=20 > for (i =3D 0; i < region_count; i++) { > - u32 region_load_addr =3D imginfo[pad + 3 + i*4]; > - u32 region_size =3D imginfo[pad + 4 + i*4]; > - u32 region_offset =3D imginfo[pad + 5 + i*4]; > + u32 region_load_addr; > + u32 region_size; > + u32 region_offset; > u32 region_start; > u32 region_end; >=20=20 > + if (pad + 5 + i*4 >=3D ARRAY_SIZE(imginfo)) > + break; > + region_load_addr =3D imginfo[pad + 3 + i*4]; > + region_size =3D imginfo[pad + 4 + i*4]; > + region_offset =3D imginfo[pad + 5 + i*4]; > + Looking at the code, I see that just above the loop there is an actual check bailing out early in case the region count is overly big. In practice: - pad can only be 0, 1 or 2 - imginfo array size is 36 The check is: if (region_count > (ARRAY_SIZE(imginfo) - pad - 3) / 4) return -EINVAL; And the loop goes at most through: pad + 5 + (region_count - 1) * 4 So the only possibilities are: pad | max(region_count) | max(index) ----+-------------------+----------- 0 | 8 | 33 1 | 8 | 34 2 | 7 | 31 It seems like none of those situations could actually lead to a reachable/exploitable bug. So while I understand the wish for a more defensive hardening, the check you add seems redundant. I would therefore suggest to just keep the driver as-is. Thanks, Miqu=C3=A8l