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 |
| 13 | * CONFIG_STACK_CANARIES=y. |
| 14 | * |
| 15 | * When this feature is enabled, the compiler generated code refers to |
| 16 | * function __stack_chk_fail and global variable __stack_chk_guard. |
| 17 | */ |
| 18 | |
| 19 | #include <toolchain.h> /* compiler specific configurations */ |
| 20 | |
Benjamin Walsh | f6ca7de | 2016-11-08 10:36:50 -0500 | [diff] [blame] | 21 | #include <kernel_structs.h> |
Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 22 | #include <toolchain.h> |
Anas Nashif | 397d29d | 2017-06-17 11:30:47 -0400 | [diff] [blame] | 23 | #include <linker/sections.h> |
Andrew Boie | cdb94d6 | 2017-04-18 15:22:05 -0700 | [diff] [blame] | 24 | #include <kernel.h> |
Andrew Boie | 01100ea | 2019-02-21 15:02:22 -0800 | [diff] [blame] | 25 | #include <app_memory/app_memdomain.h> |
Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 26 | |
| 27 | /** |
| 28 | * |
| 29 | * @brief Stack canary error handler |
| 30 | * |
| 31 | * This function is invoked when a stack canary error is detected. |
| 32 | * |
| 33 | * @return Does not return |
| 34 | */ |
Kumar Gala | bc18159 | 2019-10-03 18:32:35 -0500 | [diff] [blame] | 35 | void _StackCheckHandler(void) |
Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 36 | { |
| 37 | /* Stack canary error is a software fatal condition; treat it as such. |
| 38 | */ |
Andrew Boie | 71ce8ce | 2019-07-11 14:18:28 -0700 | [diff] [blame] | 39 | z_except_reason(K_ERR_STACK_CHK_FAIL); |
Enjia Mai | 53ca709 | 2021-01-15 17:09:58 +0800 | [diff] [blame] | 40 | CODE_UNREACHABLE; /* LCOV_EXCL_LINE */ |
Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | /* Global variable */ |
| 44 | |
| 45 | /* |
| 46 | * Symbol referenced by GCC compiler generated code for canary value. |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 47 | * The canary value gets initialized in z_cstart(). |
Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 48 | */ |
Andrew Boie | 4ce652e | 2019-02-22 16:08:44 -0800 | [diff] [blame] | 49 | #ifdef CONFIG_USERSPACE |
Andrew Boie | 01100ea | 2019-02-21 15:02:22 -0800 | [diff] [blame] | 50 | K_APP_DMEM(z_libc_partition) uintptr_t __stack_chk_guard; |
| 51 | #else |
| 52 | __noinit uintptr_t __stack_chk_guard; |
| 53 | #endif |
Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 54 | |
| 55 | /** |
| 56 | * |
| 57 | * @brief Referenced by GCC compiler generated code |
| 58 | * |
| 59 | * This routine is invoked when a stack canary error is detected, indicating |
| 60 | * a buffer overflow or stack corruption problem. |
| 61 | */ |
| 62 | FUNC_ALIAS(_StackCheckHandler, __stack_chk_fail, void); |