task_struct: Only use anon struct under randstruct plugin
PATCH 1/1 task_struct: Only use anon struct under randstruct plugin
The original intent for always adding the anonymous struct in task_struct was to make sure we had compiler coverage. However, this caused pathological padding of 40 bytes at the start of task_struct. Instead, move the anonymous struct to being only used when struct layout randomization is enabled.
--- include/linux/compiler-clang.h | 3 --- include/linux/compiler-gcc.h | 12 +++--------- 2 files changed, 3 insertions(+), 12 deletions(-)
include/linux/compiler-clang.h +0 -3
@@ -17,9 +17,6 @@*/ #define __UNIQUE_ID (prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __COUNTER__) -#define randomized_struct_fields_start struct { -#define randomized_struct_fields_end }; -/* all clang versions usable with the kernel support KASAN ABI version 5 */ #define KASAN_ABI_VERSION 5
include/linux/compiler-gcc.h +3 -9
@@ -242,6 +242,9 @@#if defined(RANDSTRUCT_PLUGIN ) && !defined(__CHECKER__)#define __randomize_layout __attribute__((randomize_layout))#define __no_randomize_layout __attribute__((no_randomize_layout)) +/* This anon struct can add padding, so only enable it under randstruct. */ +#define randomized_struct_fields_start struct { +#define randomized_struct_fields_end } __randomize_layout;#endif #endif /* GCC_VERSION >= 40500 */
@@ -256,15 +259,6 @@*/ #define __visible __attribute__((externally_visible)) -/* -* RANDSTRUCT_PLUGIN wants to use an anonymous struct, but it is only -* possible since GCC 4.6. To provide as much build testing coverage -* as possible, this is used for all GCC 4.6+ builds, and not just on -* RANDSTRUCT_PLUGIN builds. -*/ -#define randomized_struct_fields_start struct { -#define randomized_struct_fields_end } __randomize_layout; -#endif /* GCC_VERSION >= 40600 */
Signature · 6 lines
2.7.4 -- Kees Cook Pixel Security
Quoted 3 lines · On Tue, 27 Mar 2018 14:36:09 -0700 Kees Cook <[email protected]> wrote:
The original intent for always adding the anonymous struct in
task_structwas to make sure we had compiler coverage. However, this caused pathological padding of 40 bytes at the start oftask_struct.
Why? What caused this padding? It happens in all configs?
Quoted 3 lines
Instead,
move the anonymous struct to being only used when struct layout randomization is enabled.
So the mysterious 40 byte bloat is still present in this case?
Quoted 3 lines
Reported-by Peter Zijlstra <[email protected]>Fixes 29e48ce87f1e ("task_struct: Allow randomized")
Why cc:stable?
Quoted 2 lines · Andrew Morton
Why? What caused this padding? It happens in all configs?
I assume what happens is that the anonymous struct ends up containing fields that are cacheline-aligned, and then the whole anonymous struct is cacheline-aligned.
Which is all kinds of stupid, since the anonymous struct itself does not exist outside of the outer struct. So it would be entirely sufficient to just make the outer struct cacheline aligned (like it used to be), but not align the inner anonymous one - just the fields in it.
But there may be "reasons" why the inner anonymous one needs to be aligned. Maybe it's some standards requirement, or maybe it's just an internal gcc implementation detail.
Regardless, it's a bit sad. It also means that when randomization is on, that unnecessary padding will be there.
I wonder if there is some acceptable trick to avoid it. Maybe the anonymous struct can be marked as not needing alignment, even if the fields inside of it would still need to be aligned wrt the outer struct.
Quoted 5 lines
Quoted 3 lines
Instead,
move the anonymous struct to being only used when struct layout randomization is enabled.So the mysterious 40 byte bloat is still present in this case?
Almost certainly. And the struct randomization will possibly add much *more* padding elsewhere, since at least some of our structures are laid out to try to avoid padding, and then the randomization might be breaking that.
Linus
Quoted 8 lines · Linus Torvalds
Quoted 2 lines · Andrew Morton
Why? What caused this padding? It happens in all configs?
I assume what happens is that the anonymous struct ends up containing fields that are cacheline-aligned, and then the whole anonymous struct is cacheline-aligned.
Yes, structures inherit the alignment requirements of their constituent members.
Quoted 9 lines
Which is all kinds of stupid, since the anonymous struct itself does not exist outside of the outer struct. So it would be entirely sufficient to just make the outer struct cacheline aligned (like it used to be), but not align the inner anonymous one - just the fields in it.
But there may be "reasons" why the inner anonymous one needs to be aligned. Maybe it's some standards requirement, or maybe it's just an internal gcc implementation detail.
Last time I read the standard there wasn't a distinction between anonymous and regular structures for this. So in that regards a strict reading of the standard would mandate this behaviour.
Quoted 2 lines
Regardless, it's a bit sad. It also means that when randomization is on, that unnecessary padding will be there.
The other complaint is that the anonymous structure makes the pahole output (which is what I showed) unnecessarily ugly.
Quoted 6 lines · Andrew Morton
Quoted 3 lines · On Tue, 27 Mar 2018 14:36:09 -0700 Kees Cook <[email protected]> wrote:
Reported-by Peter Zijlstra <[email protected]>Fixes 29e48ce87f1e ("task_struct: Allow randomized")Why cc:stable?
Since the padding existed in all configs, it's kind of an ugly wart and should likely be fixed up for 4.14 and 4.15 -stable.
Quoted 1 line
So the mysterious 40 byte bloat is still present in this case?
Given how insane[1] task_struct can end up under randstruct, these 40 bytes aren't too bad. I've added fixing this to the randstruct to-do list, but I don't view it as high priority.
-Kees
[1] https://git.kernel.org/linus/ffa47aa678cfaa9b88e8a26cfb115b4768325121
Signature · 2 lines
Kees Cook Pixel Security
Quoted 11 lines · On Tue, 27 Mar 2018 17:30:47 -0700 Kees Cook <[email protected]> wrote:
Quoted 6 lines · Andrew Morton
Quoted 3 lines · On Tue, 27 Mar 2018 14:36:09 -0700 Kees Cook <[email protected]> wrote:
Reported-by Peter Zijlstra <[email protected]>Fixes 29e48ce87f1e ("task_struct: Allow randomized")Why cc:stable?
Since the padding existed in all configs, it's kind of an ugly wart and should likely be fixed up for 4.14 and 4.15 -stable.
That didn't tell us much :( Documentation/process/stable-kernel-rules.rst doesn't mention "ugly wart".
I think what you're hearing here is that this patch needs a better changelog, please. Not an uncommon failing, sigh. A better explanation of the origins of this padding and a better explanation of the reasons for backporting the fix.