Skip to contentSkip to conversation

neighbour: Fix __randomize_layout crash in struct neighbour

PATCH Applied Gustavo A. R. Silva 5 participants 6 messages Nov 25, 2023 – Nov 28, 2023

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 neighbour: Fix __randomize_layout crash in struct neighbour

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")
---
 include/net/neighbour.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/net/neighbour.h +1 -1
index 07022bb0d44d..0d28172193fa 100644
@@ -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 {
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Gustavo A. R. Silva

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.

Quoted 6 lines

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")

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

Signature · 1 line
Kees Cook
#2 lore raw
Quoted 14 lines · Kees Cook
Quoted 13 lines · Gustavo A. R. Silva

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.

Quoted 6 lines
Quoted 3 lines
Fixes 1ee60356c2dc ("gcc-plugins: randstruct: Only warn about true flexible arrays")

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.

Quoted 3 lines

Thanks!
--
Gustavo

Tested-by on v1 1/1
#3 lore raw
In reply to Gustavo A. R. Silva

Hi,

Quoted 25 lines · Gustavo A. R. Silva

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")
---
 include/net/neighbour.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/net/neighbour.h +0 -0
index 07022bb0d44d..0d28172193fa 100644

include/net/neighbour.h struct neighbour { · line 168

Quoted 8 lines
include/net/neighbour.h +1 -1
@@ -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!

#5 lore raw
In reply to Gustavo A. R. Silva
This patch was applied to netdev/net.git (main) by Paolo Abeni <[email protected]>:

Hello:

This patch was applied to netdev/net.git (main)
by Paolo Abeni <[email protected]>:

Quoted 11 lines · On Sat, 25 Nov 2023 15:33:58 -0600 you 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.

[...]

Here is the summary with links:
- neighbour: Fix __randomize_layout crash in struct neighbour

    https://git.kernel.org/netdev/net/c/45b3fae4675d

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
#4 lore raw
In reply to Joey Gouly
Quoted 13 lines
Quoted 13 lines
include/net/neighbour.h +1 -1
index 07022bb0d44d..0d28172193fa 100644
@@ -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;
 
Quoted 3 lines

include/net/neighbour.h struct neighbour { · line 168

Quoted 1 line
include/net/neighbour.h +0 -0
  struct neigh_ops {

Fixes the crash for me!

Awesome. :)

--
Gustavo