Received: by 2002:a05:7412:419a:b0:f3:1519:9f41 with SMTP id i26csp2025357rdh; Sat, 25 Nov 2023 10:32:24 -0800 (PST) X-Google-Smtp-Source: AGHT+IGWB1dx4DsTNWlS5ysvmI33uPf2n3rCpibwwy6QaTsdlROB7RATuZzjCEvx6kdg5kvKpbAC X-Received: by 2002:a05:6a20:8f0f:b0:188:2b6:316b with SMTP id b15-20020a056a208f0f00b0018802b6316bmr10069536pzk.38.1700937143651; Sat, 25 Nov 2023 10:32:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700937143; cv=none; d=google.com; s=arc-20160816; b=NkNZogUpFyFH5oJWWVZcROTgvjwUW504y7q1bbTb4W+tyxYMVULQzrDuPfLdnysISk vbJmi08L+D6qyRZxx1AL/fbv7T4Uw6IXZB4WBk7oGgoo3ET7QYCeT0s5TlyuXppxe85/ nZz73oPMbCV3q+eqAZ7Fi53BRUG5Az7B671+JW7ZjK+BFfVk4gOcCWyq9cCf4AjnNkfh d8Zv4RUc/07TJLyY3sOAuYbI3Y4Ost5Zqmsn9R5ROYBpEDSB9nh4ElOGof7IZBfn6Wfz But/dr1FfHlAucuSNhzLEbYS3fWqA0M75AI4urIQRh/OaguInoGNr5WurgT5lsBhNxvu na4Q== 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 :message-id:references:in-reply-to:user-agent:subject:cc:to:from :date:dkim-signature; bh=vXBuGJO1TY5tDauKjEfGJiTh9RLFj2prAc2eY69C6Bs=; fh=JGjqpKkFCvQ+1M4Q4n0RF5BBZx1rnDl+YXGVOaGIMfg=; b=cUtITzKAWVlJAiRqx7UaFxHcXeCK+A46O0WtQGi4wgMKKbPoLh3H4dQGJ5qALWwa6L RjhBDbnDP+Zrxw5sPICLI8ni+wMiiRPa5fkC4qAwpZ0fnojwyYrXnz0ezqaUdKhVr1oD hyuWlQ00ZyRq0Djmhvnr4v+sMtB3Wcvhi1rmn01ISfGfsMy1sKs4Ksy7lCJmu5EVGxuY Vd7Cv3qi7WZQt5utlZGBMkskIb3sER+Q2uFabhbybM1fuIknOOKs/VTA8Mqb9YoP2Huh XbFKWTMyGbDGIJ2R7Jq4tVl6J/XZuJUjD3MU3V6iJHHVrjEPlZhcF1YQO0pNBsB/qHz7 48HQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=YofFKFHm; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id q5-20020a056a00088500b0068e4c3f3ac2si6464273pfj.245.2023.11.25.10.32.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 25 Nov 2023 10:32:23 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=YofFKFHm; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 4BE0F803DB07; Sat, 25 Nov 2023 10:32:21 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231697AbjKYScA (ORCPT + 99 others); Sat, 25 Nov 2023 13:32:00 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56814 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229746AbjKYSb6 (ORCPT ); Sat, 25 Nov 2023 13:31:58 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A728B127 for ; Sat, 25 Nov 2023 10:32:04 -0800 (PST) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8DDEAC433C8; Sat, 25 Nov 2023 18:32:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700937124; bh=QELuRHVQ8QvisUNbMPPKmMoHXkay24CtnhYonlpSLP0=; h=Date:From:To:CC:Subject:In-Reply-To:References:From; b=YofFKFHmpJ/tyt1h7P2riHKi3FBCJkChIECWBNF4my74A8npLFWoj4QQs8iFAw95D 5qSc32zakdyAo5d+johcJcp8LWT/x8qOoWetDUzRhjgOkIx6ZU8BAguLERFd7aZsdg pQNzdsaJINbJuqIuNraH90cST6iBBRCFkVjfzLkkT99gw9bQ1Fow5ZE+kMScMQnzbr V8I7OfuHLChiKoVjtyY4DFwQ9XG08vQrIDL5R8QQEcgMd/FTarWRwPQ5l3LCXUpNcc lY5wfIbF2bhNtlSeraugQkynPhuAjPPU5yoz+yVQEndX0enjYMaIvITF/JNYi2ylRM pk9sU9S5rdjag== Date: Sat, 25 Nov 2023 10:31:56 -0800 From: Kees Cook To: "Gustavo A. R. Silva" , Joey Gouly , Kees Cook , linux-hardening@vger.kernel.org, netdev@vger.kernel.org CC: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , linux-kernel@vger.kernel.org, "Gustavo A. R. Silva" Subject: Re: [BUG] Boot crash on v6.7-rc2 User-Agent: K-9 Mail for Android In-Reply-To: References: <20231124102458.GB1503258@e124191.cambridge.arm.com> <7086f60f-9f74-4118-a10c-d98b9c6cc8eb@embeddedor.com> Message-ID: <05D345C3-BAFB-4658-8F78-4BC674A764DB@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-1.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Sat, 25 Nov 2023 10:32:21 -0800 (PST) On November 25, 2023 9:54:28 AM PST, "Gustavo A=2E R=2E Silva" wrote: > > >On 11/24/23 09:28, Gustavo A=2E R=2E Silva wrote: >>=20 >>=20 >> On 11/24/23 04:24, Joey Gouly wrote: >>> Hi all, >>>=20 >>> I just hit a boot crash on v6=2E7-rc2 (arm64, FVP model): >>=20 >> [=2E=2E] >>=20 >>> Checking `struct neighbour`: >>>=20 >>> =C2=A0=C2=A0=C2=A0=C2=A0struct neighbour { >>> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 struct neighbour __rcu=C2= =A0=C2=A0=C2=A0 *next; >>> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 struct neigh_table=C2=A0=C2= =A0=C2=A0 *tbl; >>> =C2=A0=C2=A0=C2=A0=C2=A0=2E=2E fields =2E=2E >>> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 u8=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 primary_key[0]; >>> =C2=A0=C2=A0=C2=A0=C2=A0} __randomize_layout; >>>=20 >>> Due to the `__randomize_layout`, `primary_key` field is being placed b= efore `tbl` (actually it's the same address since it's a 0 length array)=2E= That means the memcpy() corrupts the tbl pointer=2E >>>=20 >>> I think I just got unlucky with my CONFIG_RANDSTRUCT seed (I can provi= de it if needed), it doesn't look as if it's a new issue=2E >>=20 >> It seems the issue is caused by this change that was recently added to = -rc2: >>=20 >> commit 1ee60356c2dc ("gcc-plugins: randstruct: Only warn about true fle= xible arrays") >>=20 >> Previously, one-element and zero-length arrays were treated as true fle= xible arrays >> (however, they are "fake" flex arrays), and __randomize_layout would le= ave them >> untouched at the end of the struct; the same for proper C99 flex-array = members=2E But >> after the commit above, that's no longer the case: Only C99 flex-array = members will >> behave correctly (remaining untouched at end of the struct), and the ot= her two types >> of arrays will be randomized=2E > >mmh=2E=2E=2E it seems that commit 1ee60356c2dc only prevents one-element = arrays from being >treated as flex arrays, while the code should still keep zero-length arra= ys untouched: > > if (typesize =3D=3D NULL_TREE && TYPE_DOMAIN(fieldtype) !=3D NULL= _TREE && > TYPE_MAX_VALUE(TYPE_DOMAIN(fieldtype)) =3D=3D NULL_TREE) > return true; > >- if (typesize !=3D NULL_TREE && >- (TREE_CONSTANT(typesize) && (!tree_to_uhwi(typesize) || >- tree_to_uhwi(typesize) =3D=3D tree_to_uhwi(elemsize)))) >- return true; >- This should be both the 0 and 1 checks=2E I think the original fix is corr= ect: switch to a true flex array=2E > >Sorry about the confusion=2E > >>=20 >>>=20 >>> I couldn't reproduce directly on v6=2E6 (the offsets for `tbl` and `pr= imary_key` didn't overlap)=2E >>> However I tried changing the zero-length-array to a flexible one: >>>=20 >>> =C2=A0=C2=A0=C2=A0=C2=A0+=C2=A0=C2=A0=C2=A0 DECLARE_FLEX_ARRAY(u8, pri= mary_key); >>> =C2=A0=C2=A0=C2=A0=C2=A0+=C2=A0=C2=A0=C2=A0 u8=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0 primary_key[0]; Was this line supposed to be "-"? >>>=20 >>> Then the field offsets ended up overlapping, and I also got the same c= rash on v6=2E6=2E >>=20 >> The right approach is to transform the zero-length array into a C99 fle= x-array member, >> like this: >>=20 >> diff --git a/include/net/neighbour=2Eh b/include/net/neighbour=2Eh >> index 07022bb0d44d=2E=2E0d28172193fa 100644 >> --- a/include/net/neighbour=2Eh >> +++ b/include/net/neighbour=2Eh >> @@ -162,7 +162,7 @@ struct neighbour { >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 struct rcu_head=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 rcu; >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 struct net_device=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0 *dev; >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 netdevice_tracker=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0 dev_tracker; >> -=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 u8=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0 primary_key[0]; >> +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 u8=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0 primary_key[]; >> =C2=A0} __randomize_layout; >>=20 >> =C2=A0struct neigh_ops { > >In any case, I think we still should convert [0] to [ ]=2E I would expect the above to fix the problem=2E If it doesn't I'll need to = take a closer look at the plugin=2E=2E=2E -Kees --=20 Kees Cook