1 | /** @file
|
---|
2 | Base Stack Check library for GCC/clang.
|
---|
3 |
|
---|
4 | Use -fstack-protector-all compiler flag to make the compiler insert the
|
---|
5 | __stack_chk_guard "canary" value into the stack and check the value prior
|
---|
6 | to exiting the function. If the "canary" is overwritten __stack_chk_fail()
|
---|
7 | is called. This is GCC specific code.
|
---|
8 |
|
---|
9 | Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
|
---|
10 | Copyright (c) 2012, Apple Inc. All rights reserved.<BR>
|
---|
11 | SPDX-License-Identifier: BSD-2-Clause-Patent
|
---|
12 |
|
---|
13 | **/
|
---|
14 |
|
---|
15 | #include <Base.h>
|
---|
16 | #include <Library/BaseLib.h>
|
---|
17 | #include <Library/DebugLib.h>
|
---|
18 | #include <Library/PcdLib.h>
|
---|
19 |
|
---|
20 | /// "canary" value that is inserted by the compiler into the stack frame.
|
---|
21 | VOID *__stack_chk_guard = (VOID *)0x0AFF;
|
---|
22 |
|
---|
23 | // If ASLR was enabled we could use
|
---|
24 | // void (*__stack_chk_guard)(void) = __stack_chk_fail;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | Error path for compiler generated stack "canary" value check code. If the
|
---|
28 | stack canary has been overwritten this function gets called on exit of the
|
---|
29 | function.
|
---|
30 | **/
|
---|
31 | VOID
|
---|
32 | __stack_chk_fail (
|
---|
33 | VOID
|
---|
34 | )
|
---|
35 | {
|
---|
36 | UINT8 DebugPropertyMask;
|
---|
37 |
|
---|
38 | DEBUG ((DEBUG_ERROR, "STACK FAULT: Buffer Overflow at 0x%p.\n", RETURN_ADDRESS (0)));
|
---|
39 |
|
---|
40 | //
|
---|
41 | // Generate a Breakpoint, DeadLoop, or NOP based on PCD settings even if
|
---|
42 | // BaseDebugLibNull is in use.
|
---|
43 | //
|
---|
44 | DebugPropertyMask = PcdGet8 (PcdDebugPropertyMask);
|
---|
45 | if ((DebugPropertyMask & DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED) != 0) {
|
---|
46 | CpuBreakpoint ();
|
---|
47 | } else if ((DebugPropertyMask & DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED) != 0) {
|
---|
48 | CpuDeadLoop ();
|
---|
49 | }
|
---|
50 | }
|
---|