neighbour: Fix __randomize_layout crash in struct neighbour

Message ID ZWJoRsJGnCPdJ3+2@work
State New
Headers
Series neighbour: Fix __randomize_layout crash in struct neighbour |

Commit Message

Gustavo A. R. Silva Nov. 25, 2023, 9:33 p.m. UTC
  Previously, one-element and zero-length arrays were treated as true
flexible arrays, even though they are actually "fake" flex arrays.
The __randomize_layout would leave them untouched at the end of the
struct, similarly to proper C99 flex-array members.

However, this approach changed with commit 1ee60356c2dc ("gcc-plugins:
randstruct: Only warn about true flexible arrays"). Now, only C99
flexible-array members will remain untouched at the end of the struct,
while one-element and zero-length arrays will be subject to randomization.

Fix a `__randomize_layout` crash in `struct neighbour` by transforming
zero-length array `primary_key` into a proper C99 flexible-array member.

Fixes: 1ee60356c2dc ("gcc-plugins: randstruct: Only warn about true flexible arrays")
Closes: https://lore.kernel.org/linux-hardening/20231124102458.GB1503258@e124191.cambridge.arm.com/
Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
---
 include/net/neighbour.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
  

Comments

Kees Cook Nov. 28, 2023, 12:29 a.m. UTC | #1
On Sat, Nov 25, 2023 at 03:33:58PM -0600, Gustavo A. R. Silva wrote:
> Previously, one-element and zero-length arrays were treated as true
> flexible arrays, even though they are actually "fake" flex arrays.
> The __randomize_layout would leave them untouched at the end of the
> struct, similarly to proper C99 flex-array members.
> 
> However, this approach changed with commit 1ee60356c2dc ("gcc-plugins:
> randstruct: Only warn about true flexible arrays"). Now, only C99
> flexible-array members will remain untouched at the end of the struct,
> while one-element and zero-length arrays will be subject to randomization.
> 
> Fix a `__randomize_layout` crash in `struct neighbour` by transforming
> zero-length array `primary_key` into a proper C99 flexible-array member.
> 
> Fixes: 1ee60356c2dc ("gcc-plugins: randstruct: Only warn about true flexible arrays")
> Closes: https://lore.kernel.org/linux-hardening/20231124102458.GB1503258@e124191.cambridge.arm.com/
> Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>

Yes, please. Do we have any other 0-sized arrays hiding out in the
kernel? We need to get these all cleared...

Reviewed-by: Kees Cook <keescook@chromium.org>
  
Gustavo A. R. Silva Nov. 28, 2023, 1:33 a.m. UTC | #2
On 11/27/23 18:29, Kees Cook wrote:
> On Sat, Nov 25, 2023 at 03:33:58PM -0600, Gustavo A. R. Silva wrote:
>> Previously, one-element and zero-length arrays were treated as true
>> flexible arrays, even though they are actually "fake" flex arrays.
>> The __randomize_layout would leave them untouched at the end of the
>> struct, similarly to proper C99 flex-array members.
>>
>> However, this approach changed with commit 1ee60356c2dc ("gcc-plugins:
>> randstruct: Only warn about true flexible arrays"). Now, only C99
>> flexible-array members will remain untouched at the end of the struct,
>> while one-element and zero-length arrays will be subject to randomization.
>>
>> Fix a `__randomize_layout` crash in `struct neighbour` by transforming
>> zero-length array `primary_key` into a proper C99 flexible-array member.
>>
>> Fixes: 1ee60356c2dc ("gcc-plugins: randstruct: Only warn about true flexible arrays")
>> Closes: https://lore.kernel.org/linux-hardening/20231124102458.GB1503258@e124191.cambridge.arm.com/
>> Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
> 
> Yes, please. Do we have any other 0-sized arrays hiding out in the
> kernel? We need to get these all cleared...

I've found 27 instances of zero-length fake-flex arrays in next-20231127.

I'll send a patch series to transform all of them.

> 
> Reviewed-by: Kees Cook <keescook@chromium.org>
> 

Thanks!
--
Gustavo
  
Joey Gouly Nov. 28, 2023, 11:10 a.m. UTC | #3
Hi,

On Sat, Nov 25, 2023 at 03:33:58PM -0600, Gustavo A. R. Silva wrote:
> Previously, one-element and zero-length arrays were treated as true
> flexible arrays, even though they are actually "fake" flex arrays.
> The __randomize_layout would leave them untouched at the end of the
> struct, similarly to proper C99 flex-array members.
> 
> However, this approach changed with commit 1ee60356c2dc ("gcc-plugins:
> randstruct: Only warn about true flexible arrays"). Now, only C99
> flexible-array members will remain untouched at the end of the struct,
> while one-element and zero-length arrays will be subject to randomization.
> 
> Fix a `__randomize_layout` crash in `struct neighbour` by transforming
> zero-length array `primary_key` into a proper C99 flexible-array member.
> 
> Fixes: 1ee60356c2dc ("gcc-plugins: randstruct: Only warn about true flexible arrays")
> Closes: https://lore.kernel.org/linux-hardening/20231124102458.GB1503258@e124191.cambridge.arm.com/
> Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
> ---
>  include/net/neighbour.h | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> 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 {

Fixes the crash for me!

Tested-by: Joey Gouly <joey.gouly@arm.com>
  
Gustavo A. R. Silva Nov. 28, 2023, 8:12 p.m. UTC | #4
>> 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 {
> 
> Fixes the crash for me!

Awesome. :)

--
Gustavo
  

Patch

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 {