Skip to contentSkip to conversation

start_kernel: Add __no_stack_protector function attribute

PATCH Applied Artem Dinaburg 2 participants 2 messages Sep 24 – Sep 25 Stable branch

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 start_kernel: Add __no_stack_protector function attribute

From: "[email protected]" <[email protected]>

[ Upstream commit 514ca14ed5444b911de59ed3381dfd195d99fe4b ]

Back during the discussion of
commit a9a3ed1eff36 ("x86: Fix early boot crash on gcc-10, third try") we discussed the need for a function attribute to control the omission of stack protectors on a per-function basis; at the time Clang had support for no_stack_protector but GCC did not. This was fixed in gcc-11. Now that the function attribute is available, let's start using it.

Callers of boot_init_stack_canary need to use this function attribute unless they're compiled with -fno-stack-protector, otherwise the canary stored in the stack slot of the caller will differ upon the call to boot_init_stack_canary. This will lead to a call to __stack_chk_fail() then panic.

[ Backport to 6.1.y: retain this tree's start_kernel() signature, which
predates its upstream __noreturn annotation, and add __no_stack_protector. ]

Assisted-by LLM

---
Hi Greg, Sasha, and kernel and powerpc maintainers,

I am continuing CVE backports still missing from 6.1.y.
This fix is inherited by v6.6 and every later mainline release, but 6.1.y still has the affected code. The target-specific adjustment is described in the bracketed note above.

Could you please queue it for 6.1.y?

Thanks,
Artem Dinaburg

CVE: CVE-2023-53491

Build validation: The touched powerpc allmodconfig objects built with CONFIG_WERROR=y, and the patched functions had no stack_chk_fail reference.

AI assistance: An LLM helped find, adapt, and validate this backport; I reviewed the patch and test output.

 arch/powerpc/kernel/smp.c           |  1 +
 include/linux/compiler_attributes.h | 12 ++++++++++++
 init/main.c                         |  3 ++-
 3 files changed, 15 insertions(+), 1 deletion(-)
arch/powerpc/kernel/smp.c +1 -0
index 980f2bc832d98d..fa17fdcc55a3de 100644
@@ -1608,6 +1608,7 @@ static void add_cpu_to_masks(int cpu)
 }
 
 /* Activate a secondary processor. */
+__no_stack_protector
 void start_secondary(void *unused)
 {
 	unsigned int cpu = raw_smp_processor_id();
include/linux/compiler_attributes.h +12 -0
index ab1fe28162b5e8..b9778e13b0a085 100644
@@ -277,6 +277,18 @@
  */
 #define __noreturn                      __attribute__((__noreturn__))
 
+/*
+ * Optional: only supported since GCC >= 11.1, clang >= 7.0.
+ *
+ *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-no_005fstack_005fprotector-function-attribute
+ *   clang: https://clang.llvm.org/docs/AttributeReference.html#no-stack-protector-safebuffers
+ */
+#if __has_attribute(__no_stack_protector__)
+# define __no_stack_protector		__attribute__((__no_stack_protector__))
+#else
+# define __no_stack_protector
+#endif
+
 /*
  * Optional: not supported by gcc.
  * Optional: not supported by icc.
init/main.c +2 -1
index 50b84f1f9ff38a..6acfd5a78bd7a5 100644
@@ -908,7 +908,8 @@ static void __init print_unknown_bootoptions(void)
 	memblock_free(unknown_options, len);
 }
 
-asmlinkage __visible void __init __no_sanitize_address start_kernel(void)
+asmlinkage __visible __init __no_sanitize_address __no_stack_protector
+void start_kernel(void)
 {
 	char *command_line;
 	char *after_dashes;
Signature · 1 line
2.39.5
maintainer Applied
#1 lore raw
Quoted 3 lines

[ Backport to 6.1.y: retain this tree's start_kernel() signature, which
predates its upstream __noreturn annotation, and add __no_stack_protector. ]

Queued for 6.1, thanks.

Signature · 2 lines
Thanks,
Sasha