summaryrefslogtreecommitdiff
path: root/MdePkg/Library/BaseStackCheckLib/BaseStackCheckGcc.c
blob: ecf77f07392cf69c756b59416813b5b38c92ece6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/** @file
 Base Stack Check library for GCC/clang.

 Use -fstack-protector-all compiler flag to make the compiler insert the
 __stack_chk_guard "canary" value into the stack and check the value prior
 to exiting the function. If the "canary" is overwritten __stack_chk_fail()
 is called. This is GCC specific code.

 Copyright (c) 2012, Apple Inc. All rights reserved.<BR>
 This program and the accompanying materials
 are licensed and made available under the terms and conditions of the BSD License
 which accompanies this distribution.  The full text of the license may be found at
 http://opensource.org/licenses/bsd-license.php.

 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

**/

#include <Base.h>
#include <Library/BaseLib.h>
#include <Library/DebugLib.h>
#include <Library/PcdLib.h>

/// "canary" value that is inserted by the compiler into the stack frame.
VOID *__stack_chk_guard = (VOID*)FixedPcdGet64 (PcdBaseStackCanary);

// If ASLR was enabled we could use
//void (*__stack_chk_guard)(void) = __stack_chk_fail;

/**
 Error path for compiler generated stack "canary" value check code. If the
 stack canary has been overwritten this function gets called on exit of the
 function.
**/
VOID
__stack_chk_fail (
 VOID
 )
{
  UINT8 DebugPropertyMask;

  DEBUG ((DEBUG_ERROR, "STACK FAULT: Buffer Overflow in function %a.\n", __builtin_return_address(0)));

  //
  // Generate a Breakpoint, DeadLoop, or NOP based on PCD settings even if
  // BaseDebugLibNull is in use.
  //
  DebugPropertyMask = PcdGet8 (PcdDebugPropertyMask);
  if ((DebugPropertyMask & DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED) != 0) {
    CpuBreakpoint ();
  } else if ((DebugPropertyMask & DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED) != 0) {
   CpuDeadLoop ();
  }
}