From cac0955658cb591d4629bf90aaa542a66e25be55 Mon Sep 17 00:00:00 2001 From: Taylor Beebe Date: Fri, 14 Jun 2024 14:07:33 -0700 Subject: BaseTools: Update Stack Cookie Logic This patch updates the GenC logic to generate a random stack cookie value for the stack check libraries. These random values improve security for modules which cannot update the global intrinsics. If the stack cookie value is randomized in the AutoGen.h file each build, the build system will determine the module/library must be rebuilt causing effectively a clean build every time. This also makes binary reproducibility impossible. This patch updates the early build scripts to create 32 and 64-bit JSON files in the build output directory which each contain 100 randomized stack cookie values for each bitwidth. If the JSON files are already present, then they are not recreated which allows them to be stored and moved to other builds for binary reproducibility. Because they are in the build directory, a clean build will cause the values to be regenerated. The logic which creates AutoGen.h will read these JSON files and use a hash of the module GUID (the hash seed is fixed in Basetools) to index into the array of stack cookie values for the module bitwidth. This model is necessary because there isn't thread-consistent data so we cannot use a locking mechanism to ensure only one thread is writing to the stack cookie files at a time. With this model, the build threads only need to read from the files. Signed-off-by: Oliver Smith-Denny --- BaseTools/Source/Python/build/build.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'BaseTools/Source/Python/build') diff --git a/BaseTools/Source/Python/build/build.py b/BaseTools/Source/Python/build/build.py index 51fb1f4..ce1bb87 100755 --- a/BaseTools/Source/Python/build/build.py +++ b/BaseTools/Source/Python/build/build.py @@ -28,6 +28,8 @@ import threading from linecache import getlines from subprocess import Popen,PIPE, STDOUT from collections import OrderedDict, defaultdict +import json +import secrets from AutoGen.PlatformAutoGen import PlatformAutoGen from AutoGen.ModuleAutoGen import ModuleAutoGen @@ -282,6 +284,22 @@ def LaunchCommand(Command, WorkingDir,ModuleAuto = None): iau.CreateDepsTarget() return "%dms" % (int(round((time.time() - BeginTime) * 1000))) +def GenerateStackCookieValues(): + if GlobalData.gBuildDirectory == "": + return + + # Check if the 32 bit values array needs to be created + if not os.path.exists(os.path.join(GlobalData.gBuildDirectory, "StackCookieValues32.json")): + StackCookieValues32 = [secrets.randbelow(0xFFFFFFFF) for _ in range(0, 100)] + with open (os.path.join(GlobalData.gBuildDirectory, "StackCookieValues32.json"), "w") as file: + json.dump(StackCookieValues32, file) + + # Check if the 64 bit values array needs to be created + if not os.path.exists(os.path.join(GlobalData.gBuildDirectory, "StackCookieValues64.json")): + StackCookieValues64 = [secrets.randbelow(0xFFFFFFFFFFFFFFFF) for _ in range(0, 100)] + with open (os.path.join(GlobalData.gBuildDirectory, "StackCookieValues64.json"), "w") as file: + json.dump(StackCookieValues64, file) + ## The smallest unit that can be built in multi-thread build mode # # This is the base class of build unit. The "Obj" parameter must provide @@ -1794,6 +1812,7 @@ class Build(): self.UniFlag, self.Progress ) + GenerateStackCookieValues() self.Fdf = Wa.FdfFile self.LoadFixAddress = Wa.Platform.LoadFixAddress self.BuildReport.AddPlatformReport(Wa) @@ -1897,6 +1916,7 @@ class Build(): self.Progress, self.ModuleFile ) + GenerateStackCookieValues() self.Fdf = Wa.FdfFile self.LoadFixAddress = Wa.Platform.LoadFixAddress Wa.CreateMakeFile(False) @@ -2147,6 +2167,7 @@ class Build(): self.UniFlag, self.Progress ) + GenerateStackCookieValues() self.Fdf = Wa.FdfFile self.LoadFixAddress = Wa.Platform.LoadFixAddress self.BuildReport.AddPlatformReport(Wa) -- cgit v1.1