Re: [PATCH] net: randomize layout of struct net_device
From: Andrew Lunn <andrew@lunn.ch>
Date: 2025-06-02 14:46:25
Also in:
linux-kernel-mentees, lkml
On Mon, Jun 02, 2025 at 07:29:32PM +0530, Pranav Tyagi wrote:
quoted hunk ↗ jump to hunk
Add __randomize_layout to struct net_device to support structure layout randomization if CONFIG_RANDSTRUCT is enabled else the macro expands to do nothing. This enhances kernel protection by making it harder to predict the memory layout of this structure. Link: https://github.com/KSPP/linux/issues/188 Signed-off-by: Pranav Tyagi <redacted> --- include/linux/netdevice.h | 4 ++++ 1 file changed, 4 insertions(+)diff --git a/include/linux/netdevice.h b/include/linux/netdevice.h index 7ea022750e4e..0caff664ef3a 100644 --- a/include/linux/netdevice.h +++ b/include/linux/netdevice.h@@ -2077,7 +2077,11 @@ enum netdev_reg_state { * moves out. */ +#ifdef CONFIG_RANDSTRUCT +struct __randomize_layout net_device { +#else struct net_device { +#endif /* Cacheline organization can be found documented in * Documentation/networking/net_cachelines/net_device.rst. * Please update the document when adding new fields.
A dumb question i hope. As you can see from this comment, some time and effort has been put into the order of members in this structure so that those which are accessed on the TX fast path are in the same cache line, and those on the RX fast path are in the same cache line, and RX and TX fast paths are in different cache lines, etc. Does CONFIG_RANDSTRUCT understand this? It is safe to move members around within a cache line. And it is safe to move whole cache lines around. But it would be bad if the randomisation moved members between cache lines, mixing up RX and TX fast path members, or spreading fast path members over more cache lines, etc. Is there documentation somewhere about what __randomize_layout actually does? Given you are posting to a networking mailing list, you should not assume the developers here are deep into how the compiler works, and want to include a link to documentation, so we can see this is actually safe to do. Andrew