Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012-2014 Wind River Systems, Inc. |
| 3 | * |
David B. Kinder | ac74d8b | 2017-01-18 17:01:01 -0800 | [diff] [blame] | 4 | * SPDX-License-Identifier: Apache-2.0 |
Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | /** |
| 8 | * @file |
| 9 | * @brief Compiler stack protection (kernel part) |
| 10 | * |
| 11 | * This module provides functions to support compiler stack protection |
| 12 | * using canaries. This feature is enabled with configuration |
Flavio Ceolin | 0236f7c | 2024-11-25 15:46:26 -0800 | [diff] [blame] | 13 | * CONFIG_STACK_CANARIES=y or CONFIG_STACK_CANARIES_STRONG=y or |
Flavio Ceolin | 3e75c03 | 2024-12-13 08:58:17 -0800 | [diff] [blame] | 14 | * CONFIG_STACK_CANARIES_ALL=y or CONFIG_STACK_CANARIES_EXPLICIT=y. |
Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 15 | * |
| 16 | * When this feature is enabled, the compiler generated code refers to |
| 17 | * function __stack_chk_fail and global variable __stack_chk_guard. |
| 18 | */ |
| 19 | |
Gerard Marull-Paretas | cffefc8 | 2022-05-06 11:04:23 +0200 | [diff] [blame] | 20 | #include <zephyr/toolchain.h> /* compiler specific configurations */ |
Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 21 | |
Gerard Marull-Paretas | cffefc8 | 2022-05-06 11:04:23 +0200 | [diff] [blame] | 22 | #include <zephyr/kernel_structs.h> |
| 23 | #include <zephyr/toolchain.h> |
| 24 | #include <zephyr/linker/sections.h> |
| 25 | #include <zephyr/kernel.h> |
| 26 | #include <zephyr/app_memory/app_memdomain.h> |
Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 27 | |
| 28 | /** |
| 29 | * |
| 30 | * @brief Stack canary error handler |
| 31 | * |
| 32 | * This function is invoked when a stack canary error is detected. |
| 33 | * |
| 34 | * @return Does not return |
| 35 | */ |
Kumar Gala | bc18159 | 2019-10-03 18:32:35 -0500 | [diff] [blame] | 36 | void _StackCheckHandler(void) |
Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 37 | { |
| 38 | /* Stack canary error is a software fatal condition; treat it as such. |
| 39 | */ |
Andrew Boie | 71ce8ce | 2019-07-11 14:18:28 -0700 | [diff] [blame] | 40 | z_except_reason(K_ERR_STACK_CHK_FAIL); |
Enjia Mai | 53ca709 | 2021-01-15 17:09:58 +0800 | [diff] [blame] | 41 | CODE_UNREACHABLE; /* LCOV_EXCL_LINE */ |
Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | /* Global variable */ |
| 45 | |
| 46 | /* |
| 47 | * Symbol referenced by GCC compiler generated code for canary value. |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 48 | * The canary value gets initialized in z_cstart(). |
Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 49 | */ |
Flavio Ceolin | d16c5b9 | 2023-08-01 15:07:57 -0700 | [diff] [blame] | 50 | #ifdef CONFIG_STACK_CANARIES_TLS |
Daniel Flodin | 746c59c | 2024-09-18 14:07:42 +0200 | [diff] [blame] | 51 | Z_THREAD_LOCAL volatile uintptr_t __stack_chk_guard; |
Flavio Ceolin | d16c5b9 | 2023-08-01 15:07:57 -0700 | [diff] [blame] | 52 | #elif CONFIG_USERSPACE |
Lars-Ove Karlsson | 25cdda1 | 2024-06-12 12:51:21 +0200 | [diff] [blame] | 53 | K_APP_DMEM(z_libc_partition) volatile uintptr_t __stack_chk_guard; |
Andrew Boie | 01100ea | 2019-02-21 15:02:22 -0800 | [diff] [blame] | 54 | #else |
Lars-Ove Karlsson | 25cdda1 | 2024-06-12 12:51:21 +0200 | [diff] [blame] | 55 | __noinit volatile uintptr_t __stack_chk_guard; |
Andrew Boie | 01100ea | 2019-02-21 15:02:22 -0800 | [diff] [blame] | 56 | #endif |
Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 57 | |
| 58 | /** |
| 59 | * |
| 60 | * @brief Referenced by GCC compiler generated code |
| 61 | * |
| 62 | * This routine is invoked when a stack canary error is detected, indicating |
| 63 | * a buffer overflow or stack corruption problem. |
| 64 | */ |
| 65 | FUNC_ALIAS(_StackCheckHandler, __stack_chk_fail, void); |