Skip to contentSkip to conversation

sched: fix build with GCC_PLUGIN_RANDSTRUCT

PATCH Superseded Mike Rapoport 2 participants 4 messages Jun 20, 2020 – Jun 28, 2020 Scheduler

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sched: fix build with GCC_PLUGIN_RANDSTRUCT

From: Mike Rapoport <[email protected]>

Since the commit a148866489fb ("sched: Replace rq::wake_list") task_struct and CSD_TYPE_TTWU objects can be on the same queue and this requires that have "layout similar enough".

This assumption is broken when CONFIG_GCC_PLUGIN_RANDSTRUCT is enabled:

CHK include/generated/compile.h
CC kernel/smp.o
In file included from arch/x86/include/asm/atomic.h:5,

                 from include/linux/atomic.h:7,
                 from include/linux/llist.h:51,
                 from include/linux/irq_work.h:5,
                 from kernel/smp.c:10:

kernel/smp.c: In function ‘smp_init’:
include/linux/compiler.h:392:38: error: call to ‘__compiletime_assert_157’ declared with attribute error: BUILD_BUG_ON failed: offsetof(struct task_struct, wake_entry_type) - offsetof(struct task_struct, wake_entry) != offsetof(struct __call_single_data, flags) - offsetof(struct __call_single_data, llist)
392 | _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)
| ^
include/linux/compiler.h:373:4: note: in definition of macro ‘__compiletime_assert’

  373 |    prefix ## suffix();    \
      |    ^~~~~~
include/linux/compiler.h:392:2: note: in expansion of macro ‘_compiletime_assert’
  392 |  _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)
      |  ^~~~~~~~~~~~~~~~~~~
include/linux/build_bug.h:39:37: note: in expansion of macro ‘compiletime_assert’
   39 | #define BUILD_BUG_ON_MSG(cond, msg) compiletime_assert(!(cond), msg)
      |                                     ^~~~~~~~~~~~~~~~~~
include/linux/build_bug.h:50:2: note: in expansion of macro ‘BUILD_BUG_ON_MSG’
   50 |  BUILD_BUG_ON_MSG(condition, "BUILD_BUG_ON failed: " #condition)
      |  ^~~~~~~~~~~~~~~~
kernel/smp.c:687:2: note: in expansion of macro ‘BUILD_BUG_ON’
  687 |  BUILD_BUG_ON(offsetof(struct task_struct, wake_entry_type) - offsetof(struct task_struct, wake_entry) !=
      |  ^~~~~~~~~~~~

make[2]: *** [scripts/Makefile.build:280: kernel/smp.o] Error 1 make[1]: *** [Makefile:1764: kernel] Error 2
make[1]: *** Waiting for unfinished jobs....
make[1]: Leaving directory '/home/mike/build/kernel'
make: *** [Makefile:185: __sub-make] Error 2

Move 'wake_entry' and 'wake_entry_type' fiels of task_struct out of the randomized fields to keep their layout intact.

---
 include/linux/sched.h | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
include/linux/sched.h +9 -2
index b62e6aaf28f0..c885573669ac 100644
@@ -641,6 +641,15 @@ struct task_struct {
 	/* -1 unrunnable, 0 runnable, >0 stopped: */
 	volatile long			state;
 
+	/*
+	 * The layout of these fields must match the layout CSD_TYPE_TTWU
+	 * so they can be on the same the @call_single_queue
+	 */
+#ifdef CONFIG_SMP
+	struct llist_node		wake_entry;
+	unsigned int			wake_entry_type;
+#endif
+
 	/*
 	 * This begins the randomizable portion of task_struct. Only
 	 * scheduling-critical items should be added above here.
@@ -654,8 +663,6 @@ struct task_struct {
 	unsigned int			ptrace;
 
 #ifdef CONFIG_SMP
-	struct llist_node		wake_entry;
-	unsigned int			wake_entry_type;
 	int				on_cpu;
 #ifdef CONFIG_THREAD_INFO_IN_TASK
 	/* Current CPU: */
Signature · 1 line
2.25.4
#1 lore raw

ping?

Quoted 80 lines · Mike Rapoport

From: Mike Rapoport <[email protected]>

Since the commit a148866489fb ("sched: Replace rq::wake_list") task_struct and CSD_TYPE_TTWU objects can be on the same queue and this requires that have "layout similar enough".

This assumption is broken when CONFIG_GCC_PLUGIN_RANDSTRUCT is enabled:

CHK include/generated/compile.h
CC kernel/smp.o
In file included from arch/x86/include/asm/atomic.h:5,

                 from include/linux/atomic.h:7,
                 from include/linux/llist.h:51,
                 from include/linux/irq_work.h:5,
                 from kernel/smp.c:10:

kernel/smp.c: In function ‘smp_init’:
include/linux/compiler.h:392:38: error: call to ‘__compiletime_assert_157’ declared with attribute error: BUILD_BUG_ON failed: offsetof(struct task_struct, wake_entry_type) - offsetof(struct task_struct, wake_entry) != offsetof(struct __call_single_data, flags) - offsetof(struct __call_single_data, llist)
392 | _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)
| ^
include/linux/compiler.h:373:4: note: in definition of macro ‘__compiletime_assert’

  373 |    prefix ## suffix();    \
      |    ^~~~~~
include/linux/compiler.h:392:2: note: in expansion of macro ‘_compiletime_assert’
  392 |  _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)
      |  ^~~~~~~~~~~~~~~~~~~
include/linux/build_bug.h:39:37: note: in expansion of macro ‘compiletime_assert’
   39 | #define BUILD_BUG_ON_MSG(cond, msg) compiletime_assert(!(cond), msg)
      |                                     ^~~~~~~~~~~~~~~~~~
include/linux/build_bug.h:50:2: note: in expansion of macro ‘BUILD_BUG_ON_MSG’
   50 |  BUILD_BUG_ON_MSG(condition, "BUILD_BUG_ON failed: " #condition)
      |  ^~~~~~~~~~~~~~~~
kernel/smp.c:687:2: note: in expansion of macro ‘BUILD_BUG_ON’
  687 |  BUILD_BUG_ON(offsetof(struct task_struct, wake_entry_type) - offsetof(struct task_struct, wake_entry) !=
      |  ^~~~~~~~~~~~

make[2]: *** [scripts/Makefile.build:280: kernel/smp.o] Error 1 make[1]: *** [Makefile:1764: kernel] Error 2
make[1]: *** Waiting for unfinished jobs....
make[1]: Leaving directory '/home/mike/build/kernel'
make: *** [Makefile:185: __sub-make] Error 2

Move 'wake_entry' and 'wake_entry_type' fiels of task_struct out of the randomized fields to keep their layout intact.

---
 include/linux/sched.h | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
include/linux/sched.h +9 -2
index b62e6aaf28f0..c885573669ac 100644
@@ -641,6 +641,15 @@ struct task_struct {
 	/* -1 unrunnable, 0 runnable, >0 stopped: */
 	volatile long			state;
 
+	/*
+	 * The layout of these fields must match the layout CSD_TYPE_TTWU
+	 * so they can be on the same the @call_single_queue
+	 */
+#ifdef CONFIG_SMP
+	struct llist_node		wake_entry;
+	unsigned int			wake_entry_type;
+#endif
+
 	/*
 	 * This begins the randomizable portion of task_struct. Only
 	 * scheduling-critical items should be added above here.
@@ -654,8 +663,6 @@ struct task_struct {
 	unsigned int			ptrace;
 
 #ifdef CONFIG_SMP
-	struct llist_node		wake_entry;
-	unsigned int			wake_entry_type;
 	int				on_cpu;
 #ifdef CONFIG_THREAD_INFO_IN_TASK
 	/* Current CPU: */
Signature · 1 line
2.25.4
Signature · 2 lines
Sincerely yours,
Mike.
#2 lore raw
In reply to Mike Rapoport
Quoted 7 lines · On Sat, 20 Jun 2020 13:41:36 +0300 Mike Rapoport <[email protected]> wrote:

From: Mike Rapoport <[email protected]>

Since the commit a148866489fb ("sched: Replace rq::wake_list") task_struct and CSD_TYPE_TTWU objects can be on the same queue and this requires that have "layout similar enough".

This assumption is broken when CONFIG_GCC_PLUGIN_RANDSTRUCT is enabled:

You forgot to Cc Kees, who's the one that is probably the most concerned about randomizing structures!

Quoted 27 lines

CHK include/generated/compile.h
CC kernel/smp.o
In file included from arch/x86/include/asm/atomic.h:5,

                 from include/linux/atomic.h:7,
                 from include/linux/llist.h:51,
                 from include/linux/irq_work.h:5,
                 from kernel/smp.c:10:

kernel/smp.c: In function ‘smp_init’:
include/linux/compiler.h:392:38: error: call to ‘__compiletime_assert_157’ declared with attribute error: BUILD_BUG_ON failed: offsetof(struct task_struct, wake_entry_type) - offsetof(struct task_struct, wake_entry) != offsetof(struct __call_single_data, flags) - offsetof(struct __call_single_data, llist)
392 | _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)
| ^
include/linux/compiler.h:373:4: note: in definition of macro ‘__compiletime_assert’

  373 |    prefix ## suffix();    \
      |    ^~~~~~
include/linux/compiler.h:392:2: note: in expansion of macro ‘_compiletime_assert’
  392 |  _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)
      |  ^~~~~~~~~~~~~~~~~~~
include/linux/build_bug.h:39:37: note: in expansion of macro ‘compiletime_assert’
   39 | #define BUILD_BUG_ON_MSG(cond, msg) compiletime_assert(!(cond), msg)
      |                                     ^~~~~~~~~~~~~~~~~~
include/linux/build_bug.h:50:2: note: in expansion of macro ‘BUILD_BUG_ON_MSG’
   50 |  BUILD_BUG_ON_MSG(condition, "BUILD_BUG_ON failed: " #condition)
      |  ^~~~~~~~~~~~~~~~
kernel/smp.c:687:2: note: in expansion of macro ‘BUILD_BUG_ON’
  687 |  BUILD_BUG_ON(offsetof(struct task_struct, wake_entry_type) - offsetof(struct task_struct, wake_entry) !=
      |  ^~~~~~~~~~~~
Quoted 5 lines

make[2]: *** [scripts/Makefile.build:280: kernel/smp.o] Error 1 make[1]: *** [Makefile:1764: kernel] Error 2
make[1]: *** Waiting for unfinished jobs....
make[1]: Leaving directory '/home/mike/build/kernel'
make: *** [Makefile:185: __sub-make] Error 2

Seems the dependency is that the offsets between wake_entry and wake_entry_type doesn't change.

Quoted 31 lines

Move 'wake_entry' and 'wake_entry_type' fiels of task_struct out of the randomized fields to keep their layout intact.

---
 include/linux/sched.h | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
include/linux/sched.h +2 -0
index b62e6aaf28f0..c885573669ac 100644
@@ -641,6 +641,15 @@ struct task_struct {
 	/* -1 unrunnable, 0 runnable, >0 stopped: */
 	volatile long			state;
 
+	/*
+	 * The layout of these fields must match the layout

CSD_TYPE_TTWU

include/linux/sched.h +7 -0
+	 * so they can be on the same the @call_single_queue
+	 */
+#ifdef CONFIG_SMP
+	struct llist_node		wake_entry;
+	unsigned int			wake_entry_type;
+#endif
+
 	/*
 	 * This begins the randomizable portion of task_struct. Only
 	 * scheduling-critical items should be added above here.

include/linux/sched.h struct task_struct { · line 658

Quoted 5 lines
include/linux/sched.h +0 -2
@@ -654,8 +663,6 @@ struct task_struct {
 	unsigned int			ptrace;
 
 #ifdef CONFIG_SMP
-	struct llist_node		wake_entry;
-	unsigned int			wake_entry_type;

What about instead just create an anonymous structure of the two. That way they can still be randomized within the task struct and not be a target of attacks?

	struct {
		struct llist_node	wake_entry;
		unsigned int		wake_entry_type;
	};

Would that work?

-- Steve

Quoted 3 lines
include/linux/sched.h +0 -0
 	int				on_cpu;
 #ifdef CONFIG_THREAD_INFO_IN_TASK
 	/* Current CPU: */
#3 lore raw
Quoted 5 lines · Steven Rostedt
Quoted 2 lines · On Sat, 20 Jun 2020 13:41:36 +0300 Mike Rapoport <[email protected]> wrote:

From: Mike Rapoport <[email protected]>

Quoted 8 lines
Quoted 5 lines

Since the commit a148866489fb ("sched: Replace rq::wake_list") task_struct and CSD_TYPE_TTWU objects can be on the same queue and this requires that have "layout similar enough".

This assumption is broken when CONFIG_GCC_PLUGIN_RANDSTRUCT is enabled:

You forgot to Cc Kees, who's the one that is probably the most concerned about randomizing structures!

I was not concerned about randomizing, I was troubled by failing allyesconfig builds :)

Quoted 14 lines

include/linux/sched.h struct task_struct { · line 658

Quoted 9 lines
include/linux/sched.h +0 -2
 	/*
 	 * This begins the randomizable portion of task_struct. Only
 	 * scheduling-critical items should be added above here.
@@ -654,8 +663,6 @@ struct task_struct {
 	unsigned int			ptrace;
 
 #ifdef CONFIG_SMP
-	struct llist_node		wake_entry;
-	unsigned int			wake_entry_type;

What about instead just create an anonymous structure of the two. That way they can still be randomized within the task struct and not be a target of attacks?

Quoted 6 lines
	struct {
		struct llist_node	wake_entry;
		unsigned int		wake_entry_type;
	};

Would that work?

Yep, thanks, this works.
Will send v2 soon.

Quoted 7 lines

-- Steve

Quoted 3 lines
include/linux/sched.h +0 -0
 	int				on_cpu;
 #ifdef CONFIG_THREAD_INFO_IN_TASK
 	/* Current CPU: */
Signature · 2 lines
Sincerely yours,
Mike.