blob: 30da82d4a49e41bafba1f8b1ce204747b346b59a [file] [log] [blame]
Kumar Galad12d8af2016-10-05 12:01:54 -05001/*
2 * Copyright (c) 2012-2014 Wind River Systems, Inc.
3 *
David B. Kinderac74d8b2017-01-18 17:01:01 -08004 * SPDX-License-Identifier: Apache-2.0
Kumar Galad12d8af2016-10-05 12:01:54 -05005 */
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 Ceolin0236f7c2024-11-25 15:46:26 -080013 * CONFIG_STACK_CANARIES=y or CONFIG_STACK_CANARIES_STRONG=y or
Flavio Ceolin3e75c032024-12-13 08:58:17 -080014 * CONFIG_STACK_CANARIES_ALL=y or CONFIG_STACK_CANARIES_EXPLICIT=y.
Kumar Galad12d8af2016-10-05 12:01:54 -050015 *
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-Paretascffefc82022-05-06 11:04:23 +020020#include <zephyr/toolchain.h> /* compiler specific configurations */
Kumar Galad12d8af2016-10-05 12:01:54 -050021
Gerard Marull-Paretascffefc82022-05-06 11:04:23 +020022#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 Galad12d8af2016-10-05 12:01:54 -050027
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 Galabc181592019-10-03 18:32:35 -050036void _StackCheckHandler(void)
Kumar Galad12d8af2016-10-05 12:01:54 -050037{
38 /* Stack canary error is a software fatal condition; treat it as such.
39 */
Andrew Boie71ce8ce2019-07-11 14:18:28 -070040 z_except_reason(K_ERR_STACK_CHK_FAIL);
Enjia Mai53ca7092021-01-15 17:09:58 +080041 CODE_UNREACHABLE; /* LCOV_EXCL_LINE */
Kumar Galad12d8af2016-10-05 12:01:54 -050042}
43
44/* Global variable */
45
46/*
47 * Symbol referenced by GCC compiler generated code for canary value.
Patrik Flykt4344e272019-03-08 14:19:05 -070048 * The canary value gets initialized in z_cstart().
Kumar Galad12d8af2016-10-05 12:01:54 -050049 */
Flavio Ceolind16c5b92023-08-01 15:07:57 -070050#ifdef CONFIG_STACK_CANARIES_TLS
Daniel Flodin746c59c2024-09-18 14:07:42 +020051Z_THREAD_LOCAL volatile uintptr_t __stack_chk_guard;
Flavio Ceolind16c5b92023-08-01 15:07:57 -070052#elif CONFIG_USERSPACE
Lars-Ove Karlsson25cdda12024-06-12 12:51:21 +020053K_APP_DMEM(z_libc_partition) volatile uintptr_t __stack_chk_guard;
Andrew Boie01100ea2019-02-21 15:02:22 -080054#else
Lars-Ove Karlsson25cdda12024-06-12 12:51:21 +020055__noinit volatile uintptr_t __stack_chk_guard;
Andrew Boie01100ea2019-02-21 15:02:22 -080056#endif
Kumar Galad12d8af2016-10-05 12:01:54 -050057
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 */
65FUNC_ALIAS(_StackCheckHandler, __stack_chk_fail, void);