87 lines
3.8 KiB
C
87 lines
3.8 KiB
C
/*******************************************************************************
|
|
* DISCLAIMER
|
|
* This software is supplied by Renesas Electronics Corporation and is only
|
|
* intended for use with Renesas products. No other uses are authorized. This
|
|
* software is owned by Renesas Electronics Corporation and is protected under
|
|
* all applicable laws, including copyright laws.
|
|
* THIS SOFTWARE IS PROVIDED "AS IS" AND RENESAS MAKES NO WARRANTIES REGARDING
|
|
* THIS SOFTWARE, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT
|
|
* LIMITED TO WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
|
|
* AND NON-INFRINGEMENT. ALL SUCH WARRANTIES ARE EXPRESSLY DISCLAIMED.
|
|
* TO THE MAXIMUM EXTENT PERMITTED NOT PROHIBITED BY LAW, NEITHER RENESAS
|
|
* ELECTRONICS CORPORATION NOR ANY OF ITS AFFILIATED COMPANIES SHALL BE LIABLE
|
|
* FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR
|
|
* ANY REASON RELATED TO THIS SOFTWARE, EVEN IF RENESAS OR ITS AFFILIATES HAVE
|
|
* BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
|
|
* Renesas reserves the right, without notice, to make changes to this software
|
|
* and to discontinue the availability of this software. By using this software,
|
|
* you agree to the additional terms and conditions found by accessing the
|
|
* following link:
|
|
* http://www.renesas.com/disclaimer
|
|
* Copyright 2023 Renesas Electronics Corporation All rights reserved.
|
|
*******************************************************************************/
|
|
|
|
/*******************************************************************************
|
|
* DESCRIPTION : Stack protect function
|
|
******************************************************************************/
|
|
/******************************************************************************
|
|
* @file stack_protect.c
|
|
* - Version : 0.01
|
|
* @brief Check for Stack Smashing Attacks.
|
|
* .
|
|
*****************************************************************************/
|
|
/******************************************************************************
|
|
* History : DD.MM.YYYY Version Description
|
|
* : 31.01.2023 0.01 First Release
|
|
*****************************************************************************/
|
|
|
|
#include <stdint.h>
|
|
#include <log.h>
|
|
|
|
/* Specify fixed canary value as reference implementation. */
|
|
#define CANARY_VAL (0xB9DFF6A0U)
|
|
|
|
#define STACK_PROTECT_ENABLE (1U)
|
|
|
|
/* Save canary to __stack_chk_guard. */
|
|
extern uintptr_t __stack_chk_guard;
|
|
__attribute__((section(".canary"))) uintptr_t __stack_chk_guard;
|
|
|
|
/* Prototype */
|
|
extern void __ghs_set_stack_chk_guard(void);
|
|
#if (STACK_PROTECT == STACK_PROTECT_ENABLE)
|
|
extern void __stack_chk_fail(void);
|
|
static inline uintptr_t *__ghs_get_stack_chk_guard_address(void);
|
|
#endif
|
|
|
|
void __ghs_set_stack_chk_guard(void)
|
|
{
|
|
#if (STACK_PROTECT == STACK_PROTECT_ENABLE)
|
|
/* Initialize the stack canary before any code that may require a stack canary. */
|
|
/* So don't add valiables larger than 8-bytes to this function. */
|
|
/* Don't call function that uses stack canaries from this function. */
|
|
/* If customize CANARY_VAL to random value, don't allow the function */
|
|
/* to be inlined or this function may require a canary. */
|
|
|
|
*__ghs_get_stack_chk_guard_address() = (uintptr_t)CANARY_VAL;
|
|
|
|
#endif
|
|
}
|
|
/* End of function __ghs_set_stack_chk_guard(void) */
|
|
|
|
#if (STACK_PROTECT == STACK_PROTECT_ENABLE)
|
|
static inline uintptr_t *__ghs_get_stack_chk_guard_address(void)
|
|
{
|
|
/* Don't modify this function. */
|
|
return &__stack_chk_guard;
|
|
}
|
|
/* End of function __ghs_get_stack_chk_guard_address(void) */
|
|
|
|
void __stack_chk_fail(void)
|
|
{
|
|
ERROR("Stack smashing detected\n");
|
|
panic;
|
|
}
|
|
/* End of function __stack_chk_fail(void) */
|
|
#endif /* (STACK_PROTECT == STACK_PROTECT_ENABLE) */
|