2016-10-06 01:01:54 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2012-2014 Wind River Systems, Inc.
|
|
|
|
*
|
2017-01-19 09:01:01 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2016-10-06 01:01:54 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* @brief Compiler stack protection (kernel part)
|
|
|
|
*
|
|
|
|
* This module provides functions to support compiler stack protection
|
|
|
|
* using canaries. This feature is enabled with configuration
|
|
|
|
* CONFIG_STACK_CANARIES=y.
|
|
|
|
*
|
|
|
|
* When this feature is enabled, the compiler generated code refers to
|
|
|
|
* function __stack_chk_fail and global variable __stack_chk_guard.
|
|
|
|
*/
|
|
|
|
|
2022-05-06 17:04:23 +08:00
|
|
|
#include <zephyr/toolchain.h> /* compiler specific configurations */
|
2016-10-06 01:01:54 +08:00
|
|
|
|
2022-05-06 17:04:23 +08:00
|
|
|
#include <zephyr/kernel_structs.h>
|
|
|
|
#include <zephyr/toolchain.h>
|
|
|
|
#include <zephyr/linker/sections.h>
|
|
|
|
#include <zephyr/kernel.h>
|
|
|
|
#include <zephyr/app_memory/app_memdomain.h>
|
2016-10-06 01:01:54 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @brief Stack canary error handler
|
|
|
|
*
|
|
|
|
* This function is invoked when a stack canary error is detected.
|
|
|
|
*
|
|
|
|
* @return Does not return
|
|
|
|
*/
|
2019-10-04 07:32:35 +08:00
|
|
|
void _StackCheckHandler(void)
|
2016-10-06 01:01:54 +08:00
|
|
|
{
|
|
|
|
/* Stack canary error is a software fatal condition; treat it as such.
|
|
|
|
*/
|
2019-07-12 05:18:28 +08:00
|
|
|
z_except_reason(K_ERR_STACK_CHK_FAIL);
|
2021-01-15 17:09:58 +08:00
|
|
|
CODE_UNREACHABLE; /* LCOV_EXCL_LINE */
|
2016-10-06 01:01:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Global variable */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Symbol referenced by GCC compiler generated code for canary value.
|
2019-03-09 05:19:05 +08:00
|
|
|
* The canary value gets initialized in z_cstart().
|
2016-10-06 01:01:54 +08:00
|
|
|
*/
|
2023-08-02 06:07:57 +08:00
|
|
|
#ifdef CONFIG_STACK_CANARIES_TLS
|
|
|
|
__thread uintptr_t __stack_chk_guard;
|
|
|
|
#elif CONFIG_USERSPACE
|
2019-02-22 07:02:22 +08:00
|
|
|
K_APP_DMEM(z_libc_partition) uintptr_t __stack_chk_guard;
|
|
|
|
#else
|
|
|
|
__noinit uintptr_t __stack_chk_guard;
|
|
|
|
#endif
|
2016-10-06 01:01:54 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @brief Referenced by GCC compiler generated code
|
|
|
|
*
|
|
|
|
* This routine is invoked when a stack canary error is detected, indicating
|
|
|
|
* a buffer overflow or stack corruption problem.
|
|
|
|
*/
|
|
|
|
FUNC_ALIAS(_StackCheckHandler, __stack_chk_fail, void);
|