--- /dev/null
+/** @file\r
+ Library provides a hook called when a stack cookie check fails.\r
+\r
+ Copyright (c) Microsoft Corporation.\r
+ SPDX-License-Identifier: BSD-2-Clause-Patent\r
+**/\r
+\r
+#ifndef STACK_COOKIE_FAILURE_HOOK_LIB_H_\r
+#define STACK_COOKIE_FAILURE_HOOK_LIB_H_\r
+\r
+#include <Uefi.h>\r
+\r
+/**\r
+ This function gets called when a compiler generated stack cookie fails. This allows a platform to hook this\r
+ call and perform any required actions/telemetry at that time.\r
+\r
+ @param FailureAddress The address of the function that failed the stack cookie check.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+StackCheckFailureHook (\r
+ VOID *FailureAddress\r
+ );\r
+\r
+#endif\r
--- /dev/null
+/** @file\r
+ Library provides a hook called when a stack cookie check fails.\r
+\r
+ Copyright (c) Microsoft Corporation.\r
+ SPDX-License-Identifier: BSD-2-Clause-Patent\r
+**/\r
+\r
+#include <Base.h>\r
+#include <Uefi.h>\r
+\r
+/**\r
+ This function gets called when a compiler generated stack cookie fails. This allows a platform to hook this\r
+ call and perform any required actions/telemetry at that time.\r
+\r
+ @param FailureAddress The address of the function that failed the stack cookie check.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+StackCheckFailureHook (\r
+ VOID *FailureAddress\r
+ )\r
+{\r
+ return;\r
+}\r
--- /dev/null
+## @file\r
+# Library provides a hook called when a stack cookie check fails.\r
+#\r
+# Copyright (c) Microsoft Corporation.\r
+# SPDX-License-Identifier: BSD-2-Clause-Patent\r
+##\r
+\r
+[Defines]\r
+ INF_VERSION = 1.29\r
+ BASE_NAME = StackCheckFailureHookLibNull\r
+ FILE_GUID = 9ca2587c-d1f2-451a-989a-d49a9a0a613e\r
+ MODULE_TYPE = BASE\r
+ VERSION_STRING = 1.0\r
+ LIBRARY_CLASS = StackCheckFailureHookLib\r
+\r
+[Sources]\r
+ StackCheckFailureHook.c\r
+\r
+[Packages]\r
+ MdePkg/MdePkg.dec\r
--- /dev/null
+;------------------------------------------------------------------------------\r
+; IA32/StackCheckFunctionsMsvc.nasm\r
+;\r
+; Copyright (c) Microsoft Corporation.\r
+; SPDX-License-Identifier: BSD-2-Clause-Patent\r
+;------------------------------------------------------------------------------\r
+\r
+ DEFAULT REL\r
+ SECTION .text\r
+\r
+global ASM_PFX(__report_rangecheckfailure)\r
+ASM_PFX(__report_rangecheckfailure):\r
+ ret\r
+\r
+global ASM_PFX(__GSHandlerCheck)\r
+ASM_PFX(__GSHandlerCheck):\r
+ ret\r
+\r
+global @__security_check_cookie@4\r
+@__security_check_cookie@4:\r
+ ret\r
--- /dev/null
+/** @file\r
+ This file is empty to allow host applications\r
+ to use the MSVC C runtime lib that provides\r
+ stack cookie definitions without breaking the\r
+ build.\r
+\r
+ Copyright (c) Microsoft Corporation.\r
+ SPDX-License-Identifier: BSD-2-Clause-Patent\r
+**/\r
+\r
+#include <Uefi.h>\r
+\r
+extern VOID *__security_cookie;\r
--- /dev/null
+## @file\r
+# Null library instance for StackCheckLib which can be included\r
+# when a build needs to include stack check functions but does\r
+# not want to generate stack check failures.\r
+#\r
+# Copyright (c) Microsoft Corporation.\r
+# SPDX-License-Identifier: BSD-2-Clause-Patent\r
+##\r
+\r
+[Defines]\r
+ INF_VERSION = 1.29\r
+ BASE_NAME = StackCheckLibNull\r
+ FILE_GUID = f6ef2763-ca3b-4c6f-a931-2a48de3ce352\r
+ MODULE_TYPE = BASE\r
+ VERSION_STRING = 1.0\r
+ LIBRARY_CLASS = StackCheckLib\r
+\r
+[Sources]\r
+ StackCheckLibNullGcc.c | GCC\r
+ StackCheckLibNullMsvc.c | MSFT\r
+\r
+[Sources.IA32]\r
+ IA32/StackCheckFunctionsMsvc.nasm | MSFT\r
+\r
+[Sources.X64]\r
+ X64/StackCheckFunctionsMsvc.nasm | MSFT\r
+\r
+[Packages]\r
+ MdePkg/MdePkg.dec\r
+\r
+[BuildOptions]\r
+ # We cannot build the MSVC version with /GL (whole program optimization) because we run into linker error\r
+ # LNK1237, which is a failure to link against a symbol from a library compiled with /GL. The whole program\r
+ # optimization tries to do away with references to this symbol. The solution is to not compile the stack\r
+ # check libs with /GL\r
+ MSFT:*_*_*_CC_FLAGS = /GL-\r
+\r
+ # We cannot build the GCC version with LTO (link time optimization) because we run into linker errors where\r
+ # the stack cookie variable has been optimized away, as it looks to GCC like the variable is not used, because\r
+ # the compiler inserts the usage.\r
+ GCC:*_*_*_CC_FLAGS = -fno-lto\r
--- /dev/null
+/** @file\r
+ Defines the stack cookie variable for GCC and Clang compilers.\r
+\r
+ Copyright (c) Microsoft Corporation.\r
+ SPDX-License-Identifier: BSD-2-Clause-Patent\r
+**/\r
+\r
+#include <Uefi.h>\r
+\r
+VOID *__stack_chk_guard = (VOID *)(UINTN)0x0;\r
+\r
+/**\r
+ This function gets called when a gcc/clang generated stack cookie fails. This implementation does nothing when\r
+ a stack cookie failure occurs.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+__stack_chk_fail (\r
+ VOID\r
+ )\r
+{\r
+}\r
--- /dev/null
+## @file\r
+# Null library instance for StackCheckLib which can be included\r
+# when a build needs to include stack check functions but does\r
+# not want to generate stack check failures. This instance is used\r
+# for HOST_APPLICATIONS specifically, as MSVC host applications link\r
+# to the C runtime lib that contains the stack cookie definitions, so\r
+# must link to a completely null version of this lib, whereas GCC host\r
+# host applications do not link to a C runtime lib that contains the stack\r
+# cookie definitions, so we must link against our version.\r
+#\r
+# Copyright (c) Microsoft Corporation.\r
+# SPDX-License-Identifier: BSD-2-Clause-Patent\r
+##\r
+\r
+[Defines]\r
+ INF_VERSION = 1.29\r
+ BASE_NAME = StackCheckLibNullHostApplication\r
+ FILE_GUID = 7EBE7BD1-0D92-4609-89AA-6EA3815CB844\r
+ MODULE_TYPE = HOST_APPLICATION\r
+ VERSION_STRING = 1.0\r
+ LIBRARY_CLASS = StackCheckLib|HOST_APPLICATION\r
+\r
+[Sources]\r
+ StackCheckLibHostApplicationMsvc.c | MSFT\r
+ StackCheckLibNullGcc.c | GCC\r
+\r
+[Packages]\r
+ MdePkg/MdePkg.dec\r
+\r
+[BuildOptions]\r
+ # We cannot build the GCC version with LTO (link time optimization) because we run into linker errors where\r
+ # the stack cookie variable has been optimized away, as it looks to GCC like the variable is not used, because\r
+ # the compiler inserts the usage. We do not worry about the MSVC version here as it is a no-op.\r
+ GCC:*_*_*_CC_FLAGS = -fno-lto\r
--- /dev/null
+/** @file\r
+ Defines the stack cookie variable for GCC, Clang and MSVC compilers.\r
+\r
+ Copyright (c) Microsoft Corporation.\r
+ SPDX-License-Identifier: BSD-2-Clause-Patent\r
+**/\r
+\r
+#include <Uefi.h>\r
+\r
+VOID *__security_cookie = (VOID *)(UINTN)0x0;\r
--- /dev/null
+;------------------------------------------------------------------------------\r
+; X64/StackCheckFunctionsMsvc.nasm\r
+;\r
+; Copyright (c) Microsoft Corporation.\r
+; SPDX-License-Identifier: BSD-2-Clause-Patent\r
+;------------------------------------------------------------------------------\r
+\r
+ DEFAULT REL\r
+ SECTION .text\r
+\r
+global ASM_PFX(__report_rangecheckfailure)\r
+ASM_PFX(__report_rangecheckfailure):\r
+ ret\r
+\r
+global ASM_PFX(__GSHandlerCheck)\r
+ASM_PFX(__GSHandlerCheck):\r
+ ret\r
+\r
+global ASM_PFX(__security_check_cookie)\r
+ASM_PFX(__security_check_cookie):\r
+ ret\r
# definitions for the intrinsic functions.\r
#\r
NULL|MdePkg/Library/CompilerIntrinsicsLib/CompilerIntrinsicsLib.inf\r
+\r
+# Stack Cookies cannot be generically applied to SEC modules because they may not define _ModuleEntryPoint and when we\r
+# link a library in, we have to be able to define the entry point. SEC modules that do define _ModuleEntryPoint can\r
+# apply a library class override to get StackCheckLibNull.inf\r
+[LibraryClasses.common.PEI_CORE, LibraryClasses.common.PEIM, LibraryClasses.common.DXE_CORE, LibraryClasses.common.SMM_CORE, LibraryClasses.common.MM_CORE_STANDALONE, LibraryClasses.common.DXE_DRIVER, LibraryClasses.common.DXE_RUNTIME_DRIVER, LibraryClasses.common.DXE_SMM_DRIVER, LibraryClasses.common.MM_STANDALONE, LibraryClasses.common.UEFI_DRIVER, LibraryClasses.common.UEFI_APPLICATION]\r
+ NULL|MdePkg/Library/StackCheckLibNull/StackCheckLibNull.inf\r
"7007", "_EFI_SPI_NOR_FLASH_PROTOCOL",\r
"7007", "_EFI_SPI_HC_PROTOCOL",\r
"8002", "aligned (",\r
- "4002", "_ReturnAddress"\r
+ "4002", "_ReturnAddress",\r
+ "8005", "__security_cookie",\r
+ "8006", "__stack_chk_fail"\r
],\r
## Both file path and directory path are accepted.\r
"IgnoreFiles": [\r
#\r
TraceHubDebugSysTLib|Include/Library/TraceHubDebugSysTLib.h\r
\r
+ ## @libraryclass Provides a hook called when a stack cookie check fails.\r
+ #\r
+ StackCheckFailureHookLib|Include/Library/StackCheckFailureHookLib.h\r
+\r
[LibraryClasses.IA32, LibraryClasses.X64, LibraryClasses.AARCH64]\r
## @libraryclass Provides services to generate random number.\r
#\r
MdePkg/Library/JedecJep106Lib/JedecJep106Lib.inf\r
MdePkg/Library/BaseFdtLib/BaseFdtLib.inf\r
\r
+ MdePkg/Library/StackCheckFailureHookLibNull/StackCheckFailureHookLibNull.inf\r
+ MdePkg/Library/StackCheckLibNull/StackCheckLibNull.inf\r
+\r
[Components.IA32, Components.X64, Components.ARM, Components.AARCH64]\r
#\r
# Add UEFI Target Based Unit Tests\r
MdePkg/Test/Mock/Library/GoogleTest/MockPeiServicesLib/MockPeiServicesLib.inf\r
MdePkg/Test/Mock/Library/GoogleTest/MockHobLib/MockHobLib.inf\r
MdePkg/Test/Mock/Library/GoogleTest/MockFdtLib/MockFdtLib.inf\r
+\r
+ MdePkg/Library/StackCheckLibNull/StackCheckLibNullHostApplication.inf\r