xref: /rk3399_ARM-atf/lib/stack_protector/stack_protector.c (revision 51faada71a219a8b94cd8d8e423f0f22e9da4d8f)
1*51faada7SDouglas Raillard /*
2*51faada7SDouglas Raillard  * Copyright (c) 2017, ARM Limited and Contributors. All rights reserved.
3*51faada7SDouglas Raillard  *
4*51faada7SDouglas Raillard  * Redistribution and use in source and binary forms, with or without
5*51faada7SDouglas Raillard  * modification, are permitted provided that the following conditions are met:
6*51faada7SDouglas Raillard  *
7*51faada7SDouglas Raillard  * Redistributions of source code must retain the above copyright notice, this
8*51faada7SDouglas Raillard  * list of conditions and the following disclaimer.
9*51faada7SDouglas Raillard  *
10*51faada7SDouglas Raillard  * Redistributions in binary form must reproduce the above copyright notice,
11*51faada7SDouglas Raillard  * this list of conditions and the following disclaimer in the documentation
12*51faada7SDouglas Raillard  * and/or other materials provided with the distribution.
13*51faada7SDouglas Raillard  *
14*51faada7SDouglas Raillard  * Neither the name of ARM nor the names of its contributors may be used
15*51faada7SDouglas Raillard  * to endorse or promote products derived from this software without specific
16*51faada7SDouglas Raillard  * prior written permission.
17*51faada7SDouglas Raillard  *
18*51faada7SDouglas Raillard  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19*51faada7SDouglas Raillard  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20*51faada7SDouglas Raillard  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21*51faada7SDouglas Raillard  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22*51faada7SDouglas Raillard  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23*51faada7SDouglas Raillard  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24*51faada7SDouglas Raillard  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25*51faada7SDouglas Raillard  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26*51faada7SDouglas Raillard  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27*51faada7SDouglas Raillard  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28*51faada7SDouglas Raillard  * POSSIBILITY OF SUCH DAMAGE.
29*51faada7SDouglas Raillard  */
30*51faada7SDouglas Raillard #include <debug.h>
31*51faada7SDouglas Raillard #include <platform.h>
32*51faada7SDouglas Raillard #include <stdint.h>
33*51faada7SDouglas Raillard 
34*51faada7SDouglas Raillard /*
35*51faada7SDouglas Raillard  * Canary value used by the compiler runtime checks to detect stack corruption.
36*51faada7SDouglas Raillard  *
37*51faada7SDouglas Raillard  * Force the canary to be in .data to allow predictable memory layout relatively
38*51faada7SDouglas Raillard  * to the stacks.
39*51faada7SDouglas Raillard  */
40*51faada7SDouglas Raillard u_register_t  __attribute__((section(".data.stack_protector_canary")))
41*51faada7SDouglas Raillard 	__stack_chk_guard = (u_register_t) 3288484550995823360ULL;
42*51faada7SDouglas Raillard 
43*51faada7SDouglas Raillard /*
44*51faada7SDouglas Raillard  * Function called when the stack's canary check fails, which means the stack
45*51faada7SDouglas Raillard  * was corrupted. It must not return.
46*51faada7SDouglas Raillard  */
47*51faada7SDouglas Raillard void __dead2 __stack_chk_fail(void)
48*51faada7SDouglas Raillard {
49*51faada7SDouglas Raillard #if DEBUG
50*51faada7SDouglas Raillard 	ERROR("Stack corruption detected\n");
51*51faada7SDouglas Raillard #endif
52*51faada7SDouglas Raillard 	panic();
53*51faada7SDouglas Raillard }
54*51faada7SDouglas Raillard 
55