• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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) 2012, Apple Inc. All rights reserved.<BR>
10  This program and the accompanying materials
11  are licensed and made available under the terms and conditions of the BSD License
12  which accompanies this distribution.  The full text of the license may be found at
13  http://opensource.org/licenses/bsd-license.php.
14 
15  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
16  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 
18 **/
19 
20 #include <Base.h>
21 #include <Library/BaseLib.h>
22 #include <Library/DebugLib.h>
23 #include <Library/PcdLib.h>
24 
25 /// "canary" value that is inserted by the compiler into the stack frame.
26 VOID *__stack_chk_guard = (VOID*)0x0AFF;
27 
28 // If ASLR was enabled we could use
29 //void (*__stack_chk_guard)(void) = __stack_chk_fail;
30 
31 /**
32  Error path for compiler generated stack "canary" value check code. If the
33  stack canary has been overwritten this function gets called on exit of the
34  function.
35 **/
36 VOID
__stack_chk_fail(VOID)37 __stack_chk_fail (
38  VOID
39  )
40 {
41   UINT8 DebugPropertyMask;
42 
43   DEBUG ((DEBUG_ERROR, "STACK FAULT: Buffer Overflow in function %a.\n", __builtin_return_address(0)));
44 
45   //
46   // Generate a Breakpoint, DeadLoop, or NOP based on PCD settings even if
47   // BaseDebugLibNull is in use.
48   //
49   DebugPropertyMask = PcdGet8 (PcdDebugPropertyMask);
50   if ((DebugPropertyMask & DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED) != 0) {
51     CpuBreakpoint ();
52   } else if ((DebugPropertyMask & DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED) != 0) {
53    CpuDeadLoop ();
54   }
55 }
56