Linux Hardening
 help / color / mirror / Atom feed
From: Kees Cook <kees@kernel.org>
To: "Gustavo A. R. Silva" <gustavo@embeddedor.com>,
	Joey Gouly <joey.gouly@arm.com>,
	Kees Cook <keescook@chromium.org>,
	linux-hardening@vger.kernel.org, netdev@vger.kernel.org
Cc: "David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@google.com>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	linux-kernel@vger.kernel.org,
	"Gustavo A. R. Silva" <gustavoars@kernel.org>
Subject: Re: [BUG] Boot crash on v6.7-rc2
Date: Sat, 25 Nov 2023 10:31:56 -0800	[thread overview]
Message-ID: <05D345C3-BAFB-4658-8F78-4BC674A764DB@kernel.org> (raw)
In-Reply-To: <d7294586-04a4-49f7-8f5f-2dd66c8b4cde@embeddedor.com>



On November 25, 2023 9:54:28 AM PST, "Gustavo A. R. Silva" <gustavo@embeddedor.com> wrote:
>
>
>On 11/24/23 09:28, Gustavo A. R. Silva wrote:
>> 
>> 
>> On 11/24/23 04:24, Joey Gouly wrote:
>>> Hi all,
>>> 
>>> I just hit a boot crash on v6.7-rc2 (arm64, FVP model):
>> 
>> [..]
>> 
>>> Checking `struct neighbour`:
>>> 
>>>     struct neighbour {
>>>         struct neighbour __rcu    *next;
>>>         struct neigh_table    *tbl;
>>>     .. fields ..
>>>         u8            primary_key[0];
>>>     } __randomize_layout;
>>> 
>>> Due to the `__randomize_layout`, `primary_key` field is being placed before `tbl` (actually it's the same address since it's a 0 length array). That means the memcpy() corrupts the tbl pointer.
>>> 
>>> I think I just got unlucky with my CONFIG_RANDSTRUCT seed (I can provide it if needed), it doesn't look as if it's a new issue.
>> 
>> It seems the issue is caused by this change that was recently added to -rc2:
>> 
>> commit 1ee60356c2dc ("gcc-plugins: randstruct: Only warn about true flexible arrays")
>> 
>> Previously, one-element and zero-length arrays were treated as true flexible arrays
>> (however, they are "fake" flex arrays), and __randomize_layout would leave them
>> untouched at the end of the struct; the same for proper C99 flex-array members. 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 other two types
>> of arrays will be randomized.
>
>mmh... it seems that commit 1ee60356c2dc only prevents one-element arrays from being
>treated as flex arrays, while the code should still keep zero-length arrays untouched:
>
>        if (typesize == NULL_TREE && TYPE_DOMAIN(fieldtype) != NULL_TREE &&
>            TYPE_MAX_VALUE(TYPE_DOMAIN(fieldtype)) == NULL_TREE)
>                return true;
>
>-       if (typesize != NULL_TREE &&
>-           (TREE_CONSTANT(typesize) && (!tree_to_uhwi(typesize) ||
>-            tree_to_uhwi(typesize) == tree_to_uhwi(elemsize))))
>-               return true;
>-

This should be both the 0 and 1 checks. I think the original fix is correct: switch to a true flex array.

>
>Sorry about the confusion.
>
>> 
>>> 
>>> I couldn't reproduce directly on v6.6 (the offsets for `tbl` and `primary_key` didn't overlap).
>>> However I tried changing the zero-length-array to a flexible one:
>>> 
>>>     +    DECLARE_FLEX_ARRAY(u8, primary_key);
>>>     +    u8        primary_key[0];

Was this line supposed to be "-"?

>>> 
>>> Then the field offsets ended up overlapping, and I also got the same crash on v6.6.
>> 
>> The right approach is to transform the zero-length array into a C99 flex-array member,
>> like this:
>> 
>> diff --git a/include/net/neighbour.h b/include/net/neighbour.h
>> index 07022bb0d44d..0d28172193fa 100644
>> --- a/include/net/neighbour.h
>> +++ b/include/net/neighbour.h
>> @@ -162,7 +162,7 @@ struct neighbour {
>>          struct rcu_head         rcu;
>>          struct net_device       *dev;
>>          netdevice_tracker       dev_tracker;
>> -       u8                      primary_key[0];
>> +       u8                      primary_key[];
>>   } __randomize_layout;
>> 
>>   struct neigh_ops {
>
>In any case, I think we still should convert [0] to [ ].

I would expect the above to fix the problem. If it doesn't I'll need to take a closer look at the plugin...

-Kees

-- 
Kees Cook

  reply	other threads:[~2023-11-25 18:32 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-24 10:24 [BUG] Boot crash on v6.7-rc2 Joey Gouly
2023-11-24 15:28 ` Gustavo A. R. Silva
2023-11-24 15:51   ` Gustavo A. R. Silva
2023-11-25 17:54   ` Gustavo A. R. Silva
2023-11-25 18:31     ` Kees Cook [this message]
2023-11-25 18:41       ` Gustavo A. R. Silva
2023-11-26  1:14 ` Bagas Sanjaya

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=05D345C3-BAFB-4658-8F78-4BC674A764DB@kernel.org \
    --to=kees@kernel.org \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=gustavo@embeddedor.com \
    --cc=gustavoars@kernel.org \
    --cc=joey.gouly@arm.com \
    --cc=keescook@chromium.org \
    --cc=kuba@kernel.org \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox