xref: /arm-trusted-firmware/lib/stack_protector/stack_protector.c (revision 91f16700b400a8c0651d24a598fc48ee2997a0d7)
1*91f16700Schasinglulu /*
2*91f16700Schasinglulu  * Copyright (c) 2017, Arm Limited and Contributors. All rights reserved.
3*91f16700Schasinglulu  *
4*91f16700Schasinglulu  * SPDX-License-Identifier: BSD-3-Clause
5*91f16700Schasinglulu  */
6*91f16700Schasinglulu 
7*91f16700Schasinglulu #include <stdint.h>
8*91f16700Schasinglulu 
9*91f16700Schasinglulu #include <common/debug.h>
10*91f16700Schasinglulu #include <plat/common/platform.h>
11*91f16700Schasinglulu 
12*91f16700Schasinglulu /*
13*91f16700Schasinglulu  * Canary value used by the compiler runtime checks to detect stack corruption.
14*91f16700Schasinglulu  *
15*91f16700Schasinglulu  * Force the canary to be in .data to allow predictable memory layout relatively
16*91f16700Schasinglulu  * to the stacks.
17*91f16700Schasinglulu  */
18*91f16700Schasinglulu u_register_t  __attribute__((section(".data.stack_protector_canary")))
19*91f16700Schasinglulu 	__stack_chk_guard = (u_register_t) 3288484550995823360ULL;
20*91f16700Schasinglulu 
21*91f16700Schasinglulu /*
22*91f16700Schasinglulu  * Function called when the stack's canary check fails, which means the stack
23*91f16700Schasinglulu  * was corrupted. It must not return.
24*91f16700Schasinglulu  */
25*91f16700Schasinglulu void __dead2 __stack_chk_fail(void)
26*91f16700Schasinglulu {
27*91f16700Schasinglulu #if DEBUG
28*91f16700Schasinglulu 	ERROR("Stack corruption detected\n");
29*91f16700Schasinglulu #endif
30*91f16700Schasinglulu 	panic();
31*91f16700Schasinglulu }
32*91f16700Schasinglulu 
33