hc
2023-12-06 08f87f769b595151be1afeff53e144f543faa614
kernel/arch/x86/include/asm/stackprotector.h
....@@ -13,7 +13,7 @@
1313 * On x86_64, %gs is shared by percpu area and stack canary. All
1414 * percpu symbols are zero based and %gs points to the base of percpu
1515 * area. The first occupant of the percpu area is always
16
- * irq_stack_union which contains stack_canary at offset 40. Userland
16
+ * fixed_percpu_data which contains stack_canary at offset 40. Userland
1717 * %gs is always saved and restored on kernel entry and exit using
1818 * swapgs, so stack protector doesn't add any complexity there.
1919 *
....@@ -65,28 +65,43 @@
6565 */
6666 static __always_inline void boot_init_stack_canary(void)
6767 {
68
- u64 canary;
68
+ u64 canary = 0;
6969 u64 tsc;
7070
7171 #ifdef CONFIG_X86_64
72
- BUILD_BUG_ON(offsetof(union irq_stack_union, stack_canary) != 40);
72
+ BUILD_BUG_ON(offsetof(struct fixed_percpu_data, stack_canary) != 40);
7373 #endif
7474 /*
7575 * We both use the random pool and the current TSC as a source
7676 * of randomness. The TSC only matters for very early init,
7777 * there it already has some randomness on most systems. Later
7878 * on during the bootup the random pool has true entropy too.
79
+ * For preempt-rt we need to weaken the randomness a bit, as
80
+ * we can't call into the random generator from atomic context
81
+ * due to locking constraints. We just leave canary
82
+ * uninitialized and use the TSC based randomness on top of it.
7983 */
84
+#ifndef CONFIG_PREEMPT_RT
8085 get_random_bytes(&canary, sizeof(canary));
86
+#endif
8187 tsc = rdtsc();
8288 canary += tsc + (tsc << 32UL);
8389 canary &= CANARY_MASK;
8490
8591 current->stack_canary = canary;
8692 #ifdef CONFIG_X86_64
87
- this_cpu_write(irq_stack_union.stack_canary, canary);
93
+ this_cpu_write(fixed_percpu_data.stack_canary, canary);
8894 #else
8995 this_cpu_write(stack_canary.canary, canary);
96
+#endif
97
+}
98
+
99
+static inline void cpu_init_stack_canary(int cpu, struct task_struct *idle)
100
+{
101
+#ifdef CONFIG_X86_64
102
+ per_cpu(fixed_percpu_data.stack_canary, cpu) = idle->stack_canary;
103
+#else
104
+ per_cpu(stack_canary.canary, cpu) = idle->stack_canary;
90105 #endif
91106 }
92107
....@@ -119,6 +134,9 @@
119134 static inline void setup_stack_canary_segment(int cpu)
120135 { }
121136
137
+static inline void cpu_init_stack_canary(int cpu, struct task_struct *idle)
138
+{ }
139
+
122140 static inline void load_stack_canary_segment(void)
123141 {
124142 #ifdef CONFIG_X86_32