Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REBASE && FF] Add Stack Cookie Support for IA32, ARM, and AARCH64 #714

Merged
merged 7 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 0 additions & 26 deletions .pytool/CISettings.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,30 +34,20 @@ def __init__(self):
self.ActualTargets = []
self.ActualArchitectures = []
self.ActualToolChainTag = ""
self.UseBuiltInBaseTools = None
self.ActualScopes = None

# ####################################################################################### #
# Extra CmdLine configuration #
# ####################################################################################### #

def AddCommandLineOptions(self, parserObj):
group = parserObj.add_mutually_exclusive_group()
group.add_argument("-force_piptools", "--fpt", dest="force_piptools", action="store_true", default=False, help="Force the system to use pip tools")
group.add_argument("-no_piptools", "--npt", dest="no_piptools", action="store_true", default=False, help="Force the system to not use pip tools")

try:
codeql_helpers.add_command_line_option(parserObj)
except NameError:
pass

def RetrieveCommandLineOptions(self, args):
super().RetrieveCommandLineOptions(args)
if args.force_piptools:
self.UseBuiltInBaseTools = True
if args.no_piptools:
self.UseBuiltInBaseTools = False

try:
self.codeql = codeql_helpers.is_codeql_enabled_on_command_line(args)
except NameError:
Expand Down Expand Up @@ -158,22 +148,6 @@ def GetActiveScopes(self):

is_linux = GetHostInfo().os.upper() == "LINUX"

if self.UseBuiltInBaseTools is None:
is_linux = GetHostInfo().os.upper() == "LINUX"
# try and import the pip module for basetools
try:
import edk2basetools
self.UseBuiltInBaseTools = True
except ImportError:
self.UseBuiltInBaseTools = False
pass

if self.UseBuiltInBaseTools == True:
scopes += ('pipbuild-unix',) if is_linux else ('pipbuild-win',)
logging.warning("Using Pip Tools based BaseTools")
else:
logging.warning("Falling back to using in-tree BaseTools")

if is_linux and self.ActualToolChainTag.upper().startswith("GCC"):
if "AARCH64" in self.ActualArchitectures:
scopes += ("gcc_aarch64_linux",)
Expand Down
21 changes: 11 additions & 10 deletions BaseTools/Conf/tools_def.template
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@
# 2.43 - Enable stack cookies to VS2019 and VS20222 X64 builds via /GS flag
# 2.44 - Add Rust build support
# 2.45 - Enable stack cookies to GCC X64 builds via -fstack-protector flag
# 2.46 - Enable stack cookies for IA32, ARM, and AARCH64 builds for GCC and MSVC
#
#!VERSION=2.45
#!VERSION=2.46

IDENTIFIER = Default TOOL_CHAIN_CONF

Expand Down Expand Up @@ -1740,9 +1741,9 @@ NOOPT_VS2017_AARCH64_DLINK_FLAGS = /NOLOGO /NODEFAULTLIB /IGNORE:4001 /OPT:REF
*_VS2019_IA32_PP_PATH = DEF(VS2019_BIN_IA32)\cl.exe
*_VS2019_IA32_ASM_PATH = DEF(VS2019_BIN_IA32)\ml.exe

DEBUG_VS2019_IA32_CC_FLAGS = /nologo /arch:IA32 /c /WX /GS- /W4 /Gs32768 /D UNICODE /O1b2 /GL /FIAutoGen.h /EHs-c- /GR- /GF /Gy /Z7 /Gw
RELEASE_VS2019_IA32_CC_FLAGS = /nologo /arch:IA32 /c /WX /GS- /W4 /Gs32768 /D UNICODE /O1b2 /GL /FIAutoGen.h /EHs-c- /GR- /GF /Gw
NOOPT_VS2019_IA32_CC_FLAGS = /nologo /arch:IA32 /c /WX /GS- /W4 /Gs32768 /D UNICODE /FIAutoGen.h /EHs-c- /GR- /GF /Gy /Z7 /Od
DEBUG_VS2019_IA32_CC_FLAGS = /nologo /arch:IA32 /c /WX /GS /W4 /Gs32768 /D UNICODE /O1b2 /GL /FIAutoGen.h /EHs-c- /GR- /GF /Gy /Z7 /Gw
RELEASE_VS2019_IA32_CC_FLAGS = /nologo /arch:IA32 /c /WX /GS /W4 /Gs32768 /D UNICODE /O1b2 /GL /FIAutoGen.h /EHs-c- /GR- /GF /Gw
NOOPT_VS2019_IA32_CC_FLAGS = /nologo /arch:IA32 /c /WX /GS /W4 /Gs32768 /D UNICODE /FIAutoGen.h /EHs-c- /GR- /GF /Gy /Z7 /Od

DEBUG_VS2019_IA32_ASM_FLAGS = /nologo /c /WX /W3 /Cx /coff /Zd /Zi
RELEASE_VS2019_IA32_ASM_FLAGS = /nologo /c /WX /W3 /Cx /coff /Zd
Expand Down Expand Up @@ -1899,9 +1900,9 @@ NOOPT_VS2019_AARCH64_DLINK_FLAGS = /NOLOGO /NODEFAULTLIB /IGNORE:4001 /OPT:REF
*_VS2022_IA32_ASM_PATH = DEF(VS2022_BIN_IA32)\ml.exe

*_VS2022_IA32_MAKE_FLAGS = /nologo
DEBUG_VS2022_IA32_CC_FLAGS = /nologo /arch:IA32 /c /WX /GS- /W4 /Gs32768 /D UNICODE /O1b2 /GL /FIAutoGen.h /EHs-c- /GR- /GF /Gy /Z7 /Gw
RELEASE_VS2022_IA32_CC_FLAGS = /nologo /arch:IA32 /c /WX /GS- /W4 /Gs32768 /D UNICODE /O1b2 /GL /FIAutoGen.h /EHs-c- /GR- /GF /Gw
NOOPT_VS2022_IA32_CC_FLAGS = /nologo /arch:IA32 /c /WX /GS- /W4 /Gs32768 /D UNICODE /FIAutoGen.h /EHs-c- /GR- /GF /Gy /Z7 /Od
DEBUG_VS2022_IA32_CC_FLAGS = /nologo /arch:IA32 /c /WX /GS /W4 /Gs32768 /D UNICODE /O1b2 /GL /FIAutoGen.h /EHs-c- /GR- /GF /Gy /Z7 /Gw
RELEASE_VS2022_IA32_CC_FLAGS = /nologo /arch:IA32 /c /WX /GS /W4 /Gs32768 /D UNICODE /O1b2 /GL /FIAutoGen.h /EHs-c- /GR- /GF /Gw
NOOPT_VS2022_IA32_CC_FLAGS = /nologo /arch:IA32 /c /WX /GS /W4 /Gs32768 /D UNICODE /FIAutoGen.h /EHs-c- /GR- /GF /Gy /Z7 /Od

DEBUG_VS2022_IA32_ASM_FLAGS = /nologo /c /WX /W3 /Cx /coff /Zd /Zi
RELEASE_VS2022_IA32_ASM_FLAGS = /nologo /c /WX /W3 /Cx /coff /Zd
Expand Down Expand Up @@ -2033,7 +2034,7 @@ NOOPT_*_*_OBJCOPY_ADDDEBUGFLAG = --add-gnu-debuglink=$(DEBUG_DIR)/$(MODULE_N
*_*_*_DTCPP_PATH = DEF(DTCPP_BIN)
*_*_*_DTC_PATH = DEF(DTC_BIN)

DEFINE GCC_ALL_CC_FLAGS = -g -Os -fshort-wchar -fno-builtin -fno-strict-aliasing -Wall -Werror -Wno-array-bounds -include AutoGen.h -fno-common
DEFINE GCC_ALL_CC_FLAGS = -g -Os -fshort-wchar -fno-builtin -fno-strict-aliasing -Wall -Werror -Wno-array-bounds -include AutoGen.h -fno-common -fstack-protector -mstack-protector-guard=global
DEFINE GCC_IA32_CC_FLAGS = DEF(GCC_ALL_CC_FLAGS) -m32 -malign-double -freorder-blocks -freorder-blocks-and-partition -O2 -mno-stack-arg-probe
DEFINE GCC_X64_CC_FLAGS = DEF(GCC_ALL_CC_FLAGS) -mno-red-zone -Wno-address -mno-stack-arg-probe
DEFINE GCC_ARM_CC_FLAGS = DEF(GCC_ALL_CC_FLAGS) -mlittle-endian -mabi=aapcs -fno-short-enums -funsigned-char -ffunction-sections -fdata-sections -fomit-frame-pointer -Wno-address -mthumb -fno-pic -fno-pie
Expand Down Expand Up @@ -2074,8 +2075,8 @@ DEFINE GCC_DEPS_FLAGS = -MMD -MF [email protected]

DEFINE GCC48_ALL_CC_FLAGS = DEF(GCC_ALL_CC_FLAGS) -ffunction-sections -fdata-sections -DSTRING_ARRAY_NAME=$(BASE_NAME)Strings
DEFINE GCC48_IA32_X64_DLINK_COMMON = -nostdlib -Wl,-n,-q,--gc-sections -z common-page-size=0x20
DEFINE GCC48_IA32_CC_FLAGS = DEF(GCC48_ALL_CC_FLAGS) -m32 -march=i586 -malign-double -fno-stack-protector -D EFI32 -fno-asynchronous-unwind-tables -Wno-address -fno-omit-frame-pointer
DEFINE GCC48_X64_CC_FLAGS = DEF(GCC48_ALL_CC_FLAGS) -m64 -fstack-protector -mstack-protector-guard=global "-DEFIAPI=__attribute__((ms_abi))" -maccumulate-outgoing-args -mno-red-zone -Wno-address -mcmodel=small -fpie -fno-asynchronous-unwind-tables -Wno-address -fno-omit-frame-pointer
DEFINE GCC48_IA32_CC_FLAGS = DEF(GCC48_ALL_CC_FLAGS) -m32 -march=i586 -malign-double -D EFI32 -fno-asynchronous-unwind-tables -Wno-address -fno-omit-frame-pointer
DEFINE GCC48_X64_CC_FLAGS = DEF(GCC48_ALL_CC_FLAGS) -m64 "-DEFIAPI=__attribute__((ms_abi))" -maccumulate-outgoing-args -mno-red-zone -Wno-address -mcmodel=small -fpie -fno-asynchronous-unwind-tables -Wno-address -fno-omit-frame-pointer
DEFINE GCC48_IA32_X64_ASLDLINK_FLAGS = DEF(GCC48_IA32_X64_DLINK_COMMON) -Wl,--entry,ReferenceAcpiTable -u ReferenceAcpiTable
DEFINE GCC48_IA32_X64_DLINK_FLAGS = DEF(GCC48_IA32_X64_DLINK_COMMON) -Wl,--entry,$(IMAGE_ENTRY_POINT) -u $(IMAGE_ENTRY_POINT) -Wl,-Map,$(DEST_DIR_DEBUG)/$(BASE_NAME).map,--whole-archive
DEFINE GCC48_IA32_DLINK2_FLAGS = -Wl,--defsym=PECOFF_HEADER_SIZE=0x220 DEF(GCC_DLINK2_FLAGS_COMMON)
Expand Down
25 changes: 23 additions & 2 deletions BaseTools/Source/Python/AutoGen/GenC.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
from .GenPcdDb import CreatePcdDatabaseCode
from .IdfClassObject import *

import secrets # MU_CHANGE: Add Stack Cookie Support

## PCD type string
gItemTypeStringDatabase = {
TAB_PCDS_FEATURE_FLAG : TAB_PCDS_FIXED_AT_BUILD,
Expand Down Expand Up @@ -698,10 +700,12 @@
}

## Library Constructor and Destructor Templates
# MU_CHANGE [BEGIN]: Add StackCookieSupport marker for stack cookie support.
gLibraryString = {
SUP_MODULE_BASE : TemplateString("""
${BEGIN}${FunctionPrototype}${END}

${StackCookieSupport}
VOID
EFIAPI
ProcessLibrary${Type}List (
Expand All @@ -712,10 +716,10 @@
${FunctionCall}${END}
}
"""),
# MU_CHANGE [BEGIN]: Add StackCookieSupport marker for stack cookie support.
'PEI' : TemplateString("""
${BEGIN}${FunctionPrototype}${END}

${StackCookieSupport}
VOID
EFIAPI
ProcessLibrary${Type}List (
Expand Down Expand Up @@ -743,10 +747,10 @@
${FunctionCall}${END}
}
"""),
# MU_CHANGE [END]: Add StackCookieSupport marker for stack cookie support.
'MM' : TemplateString("""
${BEGIN}${FunctionPrototype}${END}

${StackCookieSupport}
VOID
EFIAPI
ProcessLibrary${Type}List (
Expand All @@ -759,6 +763,7 @@
}
"""),
}
# MU_CHANGE [END]: Add StackCookieSupport marker for stack cookie support.

gBasicHeaderFile = "Base.h"

Expand Down Expand Up @@ -2042,6 +2047,22 @@ def CreateFooterCode(Info, AutoGenC, AutoGenH):
def CreateCode(Info, AutoGenC, AutoGenH, StringH, UniGenCFlag, UniGenBinBuffer, StringIdf, IdfGenCFlag, IdfGenBinBuffer):
CreateHeaderCode(Info, AutoGenC, AutoGenH)

# MU_CHANGE [START]: Add Stack Cookie Support
if Info.ModuleType != SUP_MODULE_HOST_APPLICATION:
TaylorBeebe marked this conversation as resolved.
Show resolved Hide resolved
if Info.Arch not in ['X64', 'IA32', 'ARM', 'AARCH64']:
EdkLogger.error("build", AUTOGEN_ERROR, "Unsupported Arch %s" % Info.Arch, ExtraData="[%s]" % str(Info))
else:
Bitwidth = 64 if Info.Arch == 'X64' or Info.Arch == 'AARCH64' else 32

CookieValue = secrets.randbelow(0xFFFFFFFFFFFFFFFF if Bitwidth == 64 else 0xFFFFFFFF)

AutoGenH.Append((
'#define STACK_COOKIE_VALUE 0x%XULL\n' % CookieValue
if Bitwidth == 64 else
'#define STACK_COOKIE_VALUE 0x%X\n' % CookieValue
))
# MU_CHANGE [END]: Add Stack Cookie Support

CreateGuidDefinitionCode(Info, AutoGenC, AutoGenH)
CreateProtocolDefinitionCode(Info, AutoGenC, AutoGenH)
CreatePpiDefinitionCode(Info, AutoGenC, AutoGenH)
Expand Down
4 changes: 4 additions & 0 deletions BaseTools/Source/Python/Workspace/WorkspaceCommon.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ def GetModuleLibInstances(Module, Platform, BuildDatabase, Arch, Target, Toolcha
while len(LibraryConsumerList) > 0:
M = LibraryConsumerList.pop()
for LibraryClassName in M.LibraryClasses:
# MU_CHANGE [START]: Fix NULL LibraryClass Inclusion Issue
if LibraryClassName.startswith("NULL") and bool(M.LibraryClass):
makubacki marked this conversation as resolved.
Show resolved Hide resolved
continue
# MU_CHANGE [END]: Fix NULL LibraryClass Inclusion Issue
if LibraryClassName not in LibraryInstance:
# override library instance for this module
LibraryPath = Platform.Modules[str(Module)].LibraryClasses.get(LibraryClassName,Platform.LibraryClasses[LibraryClassName, ModuleType])
Expand Down
4 changes: 3 additions & 1 deletion CryptoPkg/CryptoPkg.dsc
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
TlsLib|CryptoPkg/Library/TlsLibNull/TlsLibNull.inf
HashApiLib|CryptoPkg/Library/BaseHashApiLib/BaseHashApiLib.inf
RngLib|MdePkg/Library/BaseRngLibNull/BaseRngLibNull.inf
NULL|MdePkg/Library/StackCheckLibNull/StackCheckLibNull.inf # MU_CHANGE: /GS and -fstack-protector support


##MSCHANGE Begin
FltUsedLib|MdePkg/Library/FltUsedLib/FltUsedLib.inf
Expand Down Expand Up @@ -95,7 +97,7 @@
NULL|MdePkg/Library/CompilerIntrinsicsLib/ArmCompilerIntrinsicsLib.inf

# Add support for stack protector
NULL|MdePkg/Library/BaseStackCheckLib/BaseStackCheckLib.inf
# NULL|MdePkg/Library/BaseStackCheckLib/BaseStackCheckLib.inf # MU_CHANGE: Use Project Mu StackCheckLib

[LibraryClasses.common.PEIM]
PeimEntryPoint|MdePkg/Library/PeimEntryPoint/PeimEntryPoint.inf
Expand Down
4 changes: 3 additions & 1 deletion MdeModulePkg/MdeModulePkg.dsc
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@

PanicLib|MdePkg/Library/BasePanicLibNull/BasePanicLibNull.inf # MU_CHANGE

NULL|MdePkg/Library/StackCheckLibNull/StackCheckLibNull.inf # MU_CHANGE: /GS and -fstack-protector support

# MU_CHANGE START Include MemoryProtectionHobLib
[LibraryClasses.common.DXE_DRIVER, LibraryClasses.common.DXE_CORE, LibraryClasses.common.UEFI_APPLICATION]
DxeMemoryProtectionHobLib|MdeModulePkg/Library/MemoryProtectionHobLib/DxeMemoryProtectionHobLib.inf
Expand Down Expand Up @@ -235,7 +237,7 @@
# Since software stack checking may be heuristically enabled by the compiler
# include BaseStackCheckLib unconditionally.
#
NULL|MdePkg/Library/BaseStackCheckLib/BaseStackCheckLib.inf
# NULL|MdePkg/Library/BaseStackCheckLib/BaseStackCheckLib.inf # MU_CHANGE: Use Project Mu StackCheckLib

[LibraryClasses.EBC, LibraryClasses.RISCV64, LibraryClasses.LOONGARCH64]
LockBoxLib|MdeModulePkg/Library/LockBoxNullLib/LockBoxNullLib.inf
Expand Down
20 changes: 20 additions & 0 deletions MdePkg/Include/Library/StackCheckFailureHookLib.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/** @file
Library provides a hook called when a stack cookie check fails.

Copyright (c) Microsoft Corporation. All rights reserved.
SPDX-License-Identifier: BSD-2-Clause-Patent
**/

#ifndef STACK_COOKIE_FAILURE_HOOK_LIB_H_
#define STACK_COOKIE_FAILURE_HOOK_LIB_H_

#include <Uefi.h>

NO_STACK_COOKIE
VOID
EFIAPI
StackCheckFailureHook (
VOID *FailureAddress
);

#endif
19 changes: 0 additions & 19 deletions MdePkg/Include/Library/StackCheckFailureLib.h

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/** @file
Library provides a hook called when a stack cookie check fails.

Copyright (c) Microsoft Corporation. All rights reserved.
SPDX-License-Identifier: BSD-2-Clause-Patent
**/

#include <Base.h>
#include <Uefi.h>

/**
Initialize the security cookie.
**/
NO_STACK_COOKIE
VOID
EFIAPI
StackCheckFailureHook (
VOID *FailureAddress
)
{
return;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
## @file
# Library provides a hook called when a stack cookie check fails.
#
# Copyright (c) Microsoft Corporation. All rights reserved.
# SPDX-License-Identifier: BSD-2-Clause-Patent
##

[Defines]
INF_VERSION = 0x00010005
BASE_NAME = StackCheckFailureHookLibNull
FILE_GUID = 9ca2587c-d1f2-451a-989a-d49a9a0a613e
MODULE_TYPE = BASE
VERSION_STRING = 1.0
LIBRARY_CLASS = StackCheckFailureHookLib

[Sources]
StackCheckFailureHook.c

[Packages]
MdePkg/MdePkg.dec
19 changes: 0 additions & 19 deletions MdePkg/Library/StackCheckFailureLibNull/StackCheckFailureLibNull.c

This file was deleted.

This file was deleted.

21 changes: 21 additions & 0 deletions MdePkg/Library/StackCheckLib/AArch64/StackCookieInterrupt.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//------------------------------------------------------------------------------
// AArch64/StackCookieInterrupt.S
//
// Copyright (c) Microsoft Corporation. All rights reserved.
// SPDX-License-Identifier: BSD-2-Clause-Patent
//------------------------------------------------------------------------------

.text

//------------------------------------------------------------------------------
// Calls an interrupt using the vector specified by PcdStackCookieExceptionVector
//
// VOID
// TriggerStackCookieInterrupt (
// VOID
// );
//------------------------------------------------------------------------------
.global ASM_PFX(TriggerStackCookieInterrupt)
ASM_PFX(TriggerStackCookieInterrupt):
smc FixedPcdGet8 (PcdStackCookieExceptionVector)
ret
Loading
Loading