From aee4582fb5144e1dd493041a8d1fc0c08146a622 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Fri, 4 Sep 2026 18:14:16 +0200 Subject: x86_64/cpu: isolate kernel and IST1 stack Previously, the kernel stack was allocated inside the .kernel_bss section, along other uninitialized data. While this works, it assumes that the section will always be laid out with the kernel stack at the top. If that is not the case, the allocated guard page before .kernel_bss would not trigger a #PF since out-of-bounds (overflow) access would quietly overwrite other data in the section, thus potentially corrupting global kernel data. At the same time, there was no separate section (and guard page) for the future #DF exception stack (IST1 in our case). Thus we allocate a section, and a guard page, while we are already modifying the linker script. --- arch/x86_64/scripts/kernel.ld | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) (limited to 'arch/x86_64/scripts/kernel.ld') diff --git a/arch/x86_64/scripts/kernel.ld b/arch/x86_64/scripts/kernel.ld index e6ae3425..8cd6c1c3 100644 --- a/arch/x86_64/scripts/kernel.ld +++ b/arch/x86_64/scripts/kernel.ld @@ -98,9 +98,21 @@ SECTIONS . += 4K; + .kernel_stack ALIGN(4K) : AT (ADDR (.kernel_stack) - TEACHOS_VMA) + { + *(.stack) + } :kernel_data + + . += 4K; + + .kernel_ist1_stack ALIGN(4K) : AT (ADDR (.kernel_ist1_stack) - TEACHOS_VMA) + { + *(.ist1_stack) + } :kernel_data + .kernel_bss ALIGN(4K) : AT (ADDR (.kernel_bss) - TEACHOS_VMA) { - *(.stack .bss*) + *(.bss*) } :kernel_data .kernel_text ALIGN(4K) : AT(ADDR (.kernel_text) - TEACHOS_VMA) -- cgit v1.2.3 From 3eb72bb6bc7446df3e34bd990cb84a0b8178b9cd Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Sat, 5 Sep 2026 11:18:38 +0200 Subject: x86_64/cpu: improve stack overflow detection --- arch/x86_64/scripts/kernel.ld | 2 ++ 1 file changed, 2 insertions(+) (limited to 'arch/x86_64/scripts/kernel.ld') diff --git a/arch/x86_64/scripts/kernel.ld b/arch/x86_64/scripts/kernel.ld index 8cd6c1c3..f96152d7 100644 --- a/arch/x86_64/scripts/kernel.ld +++ b/arch/x86_64/scripts/kernel.ld @@ -100,6 +100,7 @@ SECTIONS .kernel_stack ALIGN(4K) : AT (ADDR (.kernel_stack) - TEACHOS_VMA) { + PROVIDE_HIDDEN(__kernel_stack_bottom = .); *(.stack) } :kernel_data @@ -107,6 +108,7 @@ SECTIONS .kernel_ist1_stack ALIGN(4K) : AT (ADDR (.kernel_ist1_stack) - TEACHOS_VMA) { + PROVIDE_HIDDEN(__ist1_stack_bottom = .); *(.ist1_stack) } :kernel_data -- cgit v1.2.3 From c0b2173d6b6c4587d1df3e8ea992ee19c9ff4418 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Sat, 5 Sep 2026 11:27:44 +0200 Subject: x86_64: stop loading uninitialized data --- arch/x86_64/scripts/kernel.ld | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'arch/x86_64/scripts/kernel.ld') diff --git a/arch/x86_64/scripts/kernel.ld b/arch/x86_64/scripts/kernel.ld index f96152d7..3b2fd898 100644 --- a/arch/x86_64/scripts/kernel.ld +++ b/arch/x86_64/scripts/kernel.ld @@ -52,7 +52,7 @@ SECTIONS PROVIDE_HIDDEN(__init_array_end = .); } :boot_data - .boot_bss ALIGN(4K) : + .boot_bss ALIGN(4K) (NOLOAD) : { KEEP(*(.boot_stack .boot_bss*)) } :boot_data @@ -98,7 +98,7 @@ SECTIONS . += 4K; - .kernel_stack ALIGN(4K) : AT (ADDR (.kernel_stack) - TEACHOS_VMA) + .kernel_stack ALIGN(4K) (NOLOAD) : AT (ADDR (.kernel_stack) - TEACHOS_VMA) { PROVIDE_HIDDEN(__kernel_stack_bottom = .); *(.stack) @@ -106,13 +106,13 @@ SECTIONS . += 4K; - .kernel_ist1_stack ALIGN(4K) : AT (ADDR (.kernel_ist1_stack) - TEACHOS_VMA) + .kernel_ist1_stack ALIGN(4K) (NOLOAD) : AT (ADDR (.kernel_ist1_stack) - TEACHOS_VMA) { PROVIDE_HIDDEN(__ist1_stack_bottom = .); *(.ist1_stack) } :kernel_data - .kernel_bss ALIGN(4K) : AT (ADDR (.kernel_bss) - TEACHOS_VMA) + .kernel_bss ALIGN(4K) (NOLOAD) : AT (ADDR (.kernel_bss) - TEACHOS_VMA) { *(.bss*) } :kernel_data @@ -138,7 +138,7 @@ SECTIONS . += 4K; - .user_bss ALIGN(4K) : AT(ADDR (.user_bss) - TEACHOS_VMA) + .user_bss ALIGN(4K) (NOLOAD) : AT(ADDR (.user_bss) - TEACHOS_VMA) { KEEP(*(.user_bss*)) } :user_data -- cgit v1.2.3