From fa0feec3f79aa128434dda050455566e9a16bf61 Mon Sep 17 00:00:00 2001 From: Daniel McIlvaney Date: Wed, 21 Aug 2019 16:58:41 -0700 Subject: [PATCH 1/7] Initial commit --- .../defaultvars/default_variable_example.bin | 1 + .../AuthVars/defaultvars/default_vars.json | 10 ++ .../AuthVars/defaultvars/default_vars.py | 149 ++++++++++++++++++ .../AuthVars/defaultvars/defaultvars.h | 51 ++++++ TAs/optee_ta/AuthVars/defaultvars/sub.mk | 12 ++ TAs/optee_ta/AuthVars/sub.mk | 1 + 6 files changed, 224 insertions(+) create mode 100644 TAs/optee_ta/AuthVars/defaultvars/default_variable_example.bin create mode 100644 TAs/optee_ta/AuthVars/defaultvars/default_vars.json create mode 100644 TAs/optee_ta/AuthVars/defaultvars/default_vars.py create mode 100644 TAs/optee_ta/AuthVars/defaultvars/defaultvars.h create mode 100644 TAs/optee_ta/AuthVars/defaultvars/sub.mk diff --git a/TAs/optee_ta/AuthVars/defaultvars/default_variable_example.bin b/TAs/optee_ta/AuthVars/defaultvars/default_variable_example.bin new file mode 100644 index 0000000..02e40b0 --- /dev/null +++ b/TAs/optee_ta/AuthVars/defaultvars/default_variable_example.bin @@ -0,0 +1 @@ +Default Example Binary diff --git a/TAs/optee_ta/AuthVars/defaultvars/default_vars.json b/TAs/optee_ta/AuthVars/defaultvars/default_vars.json new file mode 100644 index 0000000..17158c6 --- /dev/null +++ b/TAs/optee_ta/AuthVars/defaultvars/default_vars.json @@ -0,0 +1,10 @@ +{ + "variables": [ + { + "name": "Default Variable Exampleé", + "bin_path": "./default_variable_example.bin", + "guid": "{0xe4b297c1, 0x507d, 0x407f, {0xbe,0x4a,0xe9,0x25,0x68,0x69,0x25,0x14}}", + "attributes": "EFI_VARIABLE_APPEND_WRITE | EFI_VARIABLE_RUNTIME_ACCESS" + } + ] +} \ No newline at end of file diff --git a/TAs/optee_ta/AuthVars/defaultvars/default_vars.py b/TAs/optee_ta/AuthVars/defaultvars/default_vars.py new file mode 100644 index 0000000..ed69062 --- /dev/null +++ b/TAs/optee_ta/AuthVars/defaultvars/default_vars.py @@ -0,0 +1,149 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +import os, sys, json, codecs + +# Variables are encoded as: +# BYTE _name[] = L"ECS\x002d2\x20COMPATIBLE\x20STRING"; +# BYTE _bin[] = { 0x0, 0x1, 0x2, ... }; +# GUID _guid = {0x12345678,0x9abc,0xdef0,{0x12,0x34,0x56,0x78,0x9A,0xBC,0xDE,0xF0}}; +# ATTRIBUTES _attributes = ; +# +# The function add_vars() will add each variable in the order it was found in the json by calling +# the add_default_var() function from default_var_utils.c + + +VALID_TYPES = ['EFI_VARIABLE_NON_VOLATILE', + 'EFI_VARIABLE_BOOTSERVICE_ACCESS', + 'EFI_VARIABLE_RUNTIME_ACCESS', + 'EFI_VARIABLE_HARDWARE_ERROR_RECORD', + 'EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS', + 'EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS', + 'EFI_VARIABLE_APPEND_WRITE'] + +#define EFI_IMAGE_SECURITY_DATABASE_GUID \ +WELL_KNOWN_GUIDS = [ + ('EFI_IMAGE_SECURITY_DATABASE_GUID','{ 0xd719b2cb, 0x3d3a, 0x4596, { 0xa3, 0xbc, 0xda, 0xd0, 0xe, 0x67, 0x65, 0x6f } }'), + ('EFI_GLOBAL_VARIABLE','{ 0x8BE4DF61, 0x93CA, 0x11d2, { 0xAA, 0x0D, 0x00, 0xE0, 0x98, 0x03, 0x2B, 0x8C } }')] + +def convert_to_wchar(char): + if ord(char) >= 0xd800 and ord(char) <= 0xdfff: + raise ValueError("Invalid ECS2 encoding (UTF-16 surrogate) for character '%c'" % char) + if ord(char) > 0xffff: + raise ValueError("Invalid ECS2 encoding for character '%c', to large for ECS2" % char) + if ((ord(char) in range(ord('a'), ord('z'))) or + (ord(char) in range(ord('A'), ord('Z'))) or + (ord(char) in range(ord('0'), ord('9')))): + return char + else: + # Generates something of the form: <" "\x0123" "> + return (r'" "\x' + ('%04x' % ord(char)) + '" "' ) + +# We could compile unicode strings here, but it is easier to convert to hex representation +# instead of handing escaping everything correctly. Also verify we are not encoding any +# 4 byte characters which ECS2 can't handle. +def format_wchar_string(string): + return 'L"' + ''.join(convert_to_wchar(c) for c in string) + '"' + +def format_byte_array(bytes): + line_length = 16 + return '{\n\t%s\n}' % (''.join(['0x%02x, ' % byte + ('\n\t' if ((i+1) % line_length == 0) else '') for i, byte in enumerate(bytes)])) + +# Generate tupples of the form: +# (name, , guid, attributes) +def parse_json(json_file): + json_data = json_file.read() + json_data = json.loads(str(json_data)) + vars = list() + for variable in json_data['variables']: + variable_name = variable['name'] + variable_bin_path = variable['bin_path'] + variable_guild = variable['guid'] + variable_attributes = variable['attributes'] + + with open(variable_bin_path, 'rb') as bin_file: + vars.append((format_wchar_string(variable_name), + format_byte_array(bin_file.read()), + variable_guild, + variable_attributes)) + return vars + +def write_header(f): + f.write('/* Automatically generated file created by default_vars.py */\n') + f.write('/* Add hard-coded variables to the image by editing default_vars.json */\n') + f.write('#include \n') + f.write('#include \n') + + +# CHAR16 var__name[] = { 0x0, 0x1, 0x2, ... }; +# BYTE var__bin[] = { 0x0, 0x1, 0x2, ... }; +# GUID var__guid = {0x12345678,0x9abc,0xdef0,{0x12,0x34,0x56,0x78,0x9A,0xBC,0xDE,0xF0}}; +# ATTRIBUTES var__attributes = ; +def write_variables(f, variables): + for i,var in enumerate(variables): + f.write(('CHAR16 var_%d_name[] = ' % i) + var[0] + ";\n") + f.write(('BYTE var_%d_bin[] = ' % i) + var[1] + ";\n") + f.write(('GUID var_%d_guid = ' % i) + var[2] + ";\n") + f.write(('ATTRIBUTES var_%d_attributes = ' % i) + var[3] + ";\n") + f.write('\n') + +def format_setvar_single_call(var_num): + var_name = 'var_%d_name' % var_num + var_name_size = 'sizeof(%s)' % var_name + var_bin = 'var_%d_bin' % var_num + var_bin_size = 'sizeof(%s)' % var_bin + var_guid = 'var_%d_guid' % var_num + var_attributes = 'var_%d_attributes' % var_num + return 'SetDefaultVariable(%s, %s, %s, %s, %s, %s)' % ( + var_name, var_name_size, var_bin, var_bin_size, + var_guid, var_attributes) + +def format_setvar_calls(variables): + return ''.join([ +''' + res = %s; + if (res != TEE_SUCCESS) + return res; +''' % format_setvar_single_call(i) for i,_ in enumerate(variables) + ]) + +def write_end(f, variables): + f.write(''' +TEE_Result InitializeDefaultVariables( VOID ) { + TEE_Result res; +%s + return TEE_SUCCESS; +} +''' % format_setvar_calls(variables)) + + + +num_args = len(sys.argv) +if num_args != 3: +# raise ValueError("Expecting 2 arguments (in.json, out.c)") + json_file_in = 'foo' + c_file_out = 'bar.c' + json_file_in_path = os.path.abspath(os.path.join(os.getcwd(), json_file_in)) + c_file_out_path = os.path.abspath(os.path.join(os.getcwd(), c_file_out)) +else: + json_file_in = sys.argv[1] + json_file_in_path = os.path.abspath(os.path.join(os.getcwd(), json_file_in)) + c_file_out = sys.argv[2] + c_file_out_path = os.path.abspath(os.path.join(os.getcwd(), c_file_out)) + +print("Checking for variable file in", json_file_in_path) +if os.path.isfile(json_file_in_path): + print("Parsing variables from %s" % json_file_in_path) +else: + raise FileNotFoundError("Could not find input " + str(json_file_in_path)) + +if os.path.isfile(c_file_out_path): + os.remove(c_file_out_path) + +with codecs.open(json_file_in_path, encoding='utf-8', mode='r') as json_file: + variables = parse_json(json_file) + +with open(c_file_out_path, 'w') as c_file: + write_header(c_file) + write_variables(c_file, variables) + write_end(c_file, variables) diff --git a/TAs/optee_ta/AuthVars/defaultvars/defaultvars.h b/TAs/optee_ta/AuthVars/defaultvars/defaultvars.h new file mode 100644 index 0000000..2b504ec --- /dev/null +++ b/TAs/optee_ta/AuthVars/defaultvars/defaultvars.h @@ -0,0 +1,51 @@ +/* The copyright in this software is being made available under the BSD License, + * included below. This software may be subject to other third party and + * contributor rights, including patent rights, and no such rights are granted + * under this license. + * + * Copyright (c) Microsoft Corporation + * + * All rights reserved. + * + * BSD License + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this list + * of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ""AS IS"" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once +#include +#include +#include +#include "ntdefs.h" +#include "uefidefs.h" +#include "defaultbins.h" + +TEE_Result +InitializeDefaultVariables( + VOID +); + +TEE_Result +SetDefaultVariable( + CHAR16 *Name, UINT32 NameSize, BYTE *Bin, UINT32 BinSize, + GUID Guid, ATTRIBUTES Attributes +); \ No newline at end of file diff --git a/TAs/optee_ta/AuthVars/defaultvars/sub.mk b/TAs/optee_ta/AuthVars/defaultvars/sub.mk new file mode 100644 index 0000000..fc25ab7 --- /dev/null +++ b/TAs/optee_ta/AuthVars/defaultvars/sub.mk @@ -0,0 +1,12 @@ +DEFAULT_VARS_PATHS := $(shell find $(sub-dir) -name '*.bin') +DEFAULT_VARS_JSON ?= $(sub-dir)/default_vars.json +#DEFAULT_VAR_PATHS = $(foreach file,$(DEFAULT_VAR_BINS),$(sub-dir)/$(file)) + +srcs-y += default_vars.c +global-incdirs-y += ./ + +gensrcs-y += default_vars +produce-default_vars = default_vars_encoding.c +depends-default_vars = $(DEFAULT_VARS_PATHS) $(sub-dir)/default_vars.py $(DEFAULT_VARS_JSON) +recipe-default_vars = python3 $(sub-dir)default_vars.py $(DEFAULT_VARS_JSON) $(sub-dir-out)/default_vars_encoding.c +cleanfiles += $(sub-dir-out)/default_vars_encoding.c \ No newline at end of file diff --git a/TAs/optee_ta/AuthVars/sub.mk b/TAs/optee_ta/AuthVars/sub.mk index aa39869..4d713c5 100644 --- a/TAs/optee_ta/AuthVars/sub.mk +++ b/TAs/optee_ta/AuthVars/sub.mk @@ -51,6 +51,7 @@ clean: clean_lib_symlinks cflags-y += $(AUTHVAR_FLAGS) $(SSL_FLAGS) $(INCLUDE_OVERWRITES) subdirs-y += lib +subdirs-y += defaultvars global-incdirs-y += src/include From f98c4d9cae71b1702f1540c448462a4e1d783e2a Mon Sep 17 00:00:00 2001 From: Daniel McIlvaney Date: Thu, 22 Aug 2019 16:17:44 -0700 Subject: [PATCH 2/7] Improve ECS2 unicode handling, add documentation --- .../AuthVars/defaultvars/default_vars.py | 149 --------------- .../AuthVars/defaultvars/defaultvars.c | 94 ++++++++++ .../AuthVars/defaultvars/defaultvars.h | 11 +- .../AuthVars/defaultvars/defaultvars.py | 174 ++++++++++++++++++ ...ult_vars.json => defaultvars_example.json} | 6 +- .../defaultvars_secureboot_example.json | 22 +++ ...fault_variable_example.bin => example.bin} | 0 .../AuthVars/defaultvars/example_KEK.bin | Bin 0 -> 6748 bytes .../AuthVars/defaultvars/example_PK.bin | Bin 0 -> 5189 bytes .../AuthVars/defaultvars/example_db.bin | Bin 0 -> 8903 bytes TAs/optee_ta/AuthVars/defaultvars/sub.mk | 13 +- TAs/optee_ta/AuthVars/src/varmgmt.c | 11 ++ TAs/optee_ta/AuthVars/sub.mk | 2 +- 13 files changed, 318 insertions(+), 164 deletions(-) delete mode 100644 TAs/optee_ta/AuthVars/defaultvars/default_vars.py create mode 100644 TAs/optee_ta/AuthVars/defaultvars/defaultvars.c create mode 100644 TAs/optee_ta/AuthVars/defaultvars/defaultvars.py rename TAs/optee_ta/AuthVars/defaultvars/{default_vars.json => defaultvars_example.json} (51%) create mode 100644 TAs/optee_ta/AuthVars/defaultvars/defaultvars_secureboot_example.json rename TAs/optee_ta/AuthVars/defaultvars/{default_variable_example.bin => example.bin} (100%) create mode 100644 TAs/optee_ta/AuthVars/defaultvars/example_KEK.bin create mode 100644 TAs/optee_ta/AuthVars/defaultvars/example_PK.bin create mode 100644 TAs/optee_ta/AuthVars/defaultvars/example_db.bin diff --git a/TAs/optee_ta/AuthVars/defaultvars/default_vars.py b/TAs/optee_ta/AuthVars/defaultvars/default_vars.py deleted file mode 100644 index ed69062..0000000 --- a/TAs/optee_ta/AuthVars/defaultvars/default_vars.py +++ /dev/null @@ -1,149 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -import os, sys, json, codecs - -# Variables are encoded as: -# BYTE _name[] = L"ECS\x002d2\x20COMPATIBLE\x20STRING"; -# BYTE _bin[] = { 0x0, 0x1, 0x2, ... }; -# GUID _guid = {0x12345678,0x9abc,0xdef0,{0x12,0x34,0x56,0x78,0x9A,0xBC,0xDE,0xF0}}; -# ATTRIBUTES _attributes = ; -# -# The function add_vars() will add each variable in the order it was found in the json by calling -# the add_default_var() function from default_var_utils.c - - -VALID_TYPES = ['EFI_VARIABLE_NON_VOLATILE', - 'EFI_VARIABLE_BOOTSERVICE_ACCESS', - 'EFI_VARIABLE_RUNTIME_ACCESS', - 'EFI_VARIABLE_HARDWARE_ERROR_RECORD', - 'EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS', - 'EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS', - 'EFI_VARIABLE_APPEND_WRITE'] - -#define EFI_IMAGE_SECURITY_DATABASE_GUID \ -WELL_KNOWN_GUIDS = [ - ('EFI_IMAGE_SECURITY_DATABASE_GUID','{ 0xd719b2cb, 0x3d3a, 0x4596, { 0xa3, 0xbc, 0xda, 0xd0, 0xe, 0x67, 0x65, 0x6f } }'), - ('EFI_GLOBAL_VARIABLE','{ 0x8BE4DF61, 0x93CA, 0x11d2, { 0xAA, 0x0D, 0x00, 0xE0, 0x98, 0x03, 0x2B, 0x8C } }')] - -def convert_to_wchar(char): - if ord(char) >= 0xd800 and ord(char) <= 0xdfff: - raise ValueError("Invalid ECS2 encoding (UTF-16 surrogate) for character '%c'" % char) - if ord(char) > 0xffff: - raise ValueError("Invalid ECS2 encoding for character '%c', to large for ECS2" % char) - if ((ord(char) in range(ord('a'), ord('z'))) or - (ord(char) in range(ord('A'), ord('Z'))) or - (ord(char) in range(ord('0'), ord('9')))): - return char - else: - # Generates something of the form: <" "\x0123" "> - return (r'" "\x' + ('%04x' % ord(char)) + '" "' ) - -# We could compile unicode strings here, but it is easier to convert to hex representation -# instead of handing escaping everything correctly. Also verify we are not encoding any -# 4 byte characters which ECS2 can't handle. -def format_wchar_string(string): - return 'L"' + ''.join(convert_to_wchar(c) for c in string) + '"' - -def format_byte_array(bytes): - line_length = 16 - return '{\n\t%s\n}' % (''.join(['0x%02x, ' % byte + ('\n\t' if ((i+1) % line_length == 0) else '') for i, byte in enumerate(bytes)])) - -# Generate tupples of the form: -# (name, , guid, attributes) -def parse_json(json_file): - json_data = json_file.read() - json_data = json.loads(str(json_data)) - vars = list() - for variable in json_data['variables']: - variable_name = variable['name'] - variable_bin_path = variable['bin_path'] - variable_guild = variable['guid'] - variable_attributes = variable['attributes'] - - with open(variable_bin_path, 'rb') as bin_file: - vars.append((format_wchar_string(variable_name), - format_byte_array(bin_file.read()), - variable_guild, - variable_attributes)) - return vars - -def write_header(f): - f.write('/* Automatically generated file created by default_vars.py */\n') - f.write('/* Add hard-coded variables to the image by editing default_vars.json */\n') - f.write('#include \n') - f.write('#include \n') - - -# CHAR16 var__name[] = { 0x0, 0x1, 0x2, ... }; -# BYTE var__bin[] = { 0x0, 0x1, 0x2, ... }; -# GUID var__guid = {0x12345678,0x9abc,0xdef0,{0x12,0x34,0x56,0x78,0x9A,0xBC,0xDE,0xF0}}; -# ATTRIBUTES var__attributes = ; -def write_variables(f, variables): - for i,var in enumerate(variables): - f.write(('CHAR16 var_%d_name[] = ' % i) + var[0] + ";\n") - f.write(('BYTE var_%d_bin[] = ' % i) + var[1] + ";\n") - f.write(('GUID var_%d_guid = ' % i) + var[2] + ";\n") - f.write(('ATTRIBUTES var_%d_attributes = ' % i) + var[3] + ";\n") - f.write('\n') - -def format_setvar_single_call(var_num): - var_name = 'var_%d_name' % var_num - var_name_size = 'sizeof(%s)' % var_name - var_bin = 'var_%d_bin' % var_num - var_bin_size = 'sizeof(%s)' % var_bin - var_guid = 'var_%d_guid' % var_num - var_attributes = 'var_%d_attributes' % var_num - return 'SetDefaultVariable(%s, %s, %s, %s, %s, %s)' % ( - var_name, var_name_size, var_bin, var_bin_size, - var_guid, var_attributes) - -def format_setvar_calls(variables): - return ''.join([ -''' - res = %s; - if (res != TEE_SUCCESS) - return res; -''' % format_setvar_single_call(i) for i,_ in enumerate(variables) - ]) - -def write_end(f, variables): - f.write(''' -TEE_Result InitializeDefaultVariables( VOID ) { - TEE_Result res; -%s - return TEE_SUCCESS; -} -''' % format_setvar_calls(variables)) - - - -num_args = len(sys.argv) -if num_args != 3: -# raise ValueError("Expecting 2 arguments (in.json, out.c)") - json_file_in = 'foo' - c_file_out = 'bar.c' - json_file_in_path = os.path.abspath(os.path.join(os.getcwd(), json_file_in)) - c_file_out_path = os.path.abspath(os.path.join(os.getcwd(), c_file_out)) -else: - json_file_in = sys.argv[1] - json_file_in_path = os.path.abspath(os.path.join(os.getcwd(), json_file_in)) - c_file_out = sys.argv[2] - c_file_out_path = os.path.abspath(os.path.join(os.getcwd(), c_file_out)) - -print("Checking for variable file in", json_file_in_path) -if os.path.isfile(json_file_in_path): - print("Parsing variables from %s" % json_file_in_path) -else: - raise FileNotFoundError("Could not find input " + str(json_file_in_path)) - -if os.path.isfile(c_file_out_path): - os.remove(c_file_out_path) - -with codecs.open(json_file_in_path, encoding='utf-8', mode='r') as json_file: - variables = parse_json(json_file) - -with open(c_file_out_path, 'w') as c_file: - write_header(c_file) - write_variables(c_file, variables) - write_end(c_file, variables) diff --git a/TAs/optee_ta/AuthVars/defaultvars/defaultvars.c b/TAs/optee_ta/AuthVars/defaultvars/defaultvars.c new file mode 100644 index 0000000..272d215 --- /dev/null +++ b/TAs/optee_ta/AuthVars/defaultvars/defaultvars.c @@ -0,0 +1,94 @@ +/* The copyright in this software is being made available under the BSD License, + * included below. This software may be subject to other third party and + * contributor rights, including patent rights, and no such rights are granted + * under this license. + * + * Copyright (c) Microsoft Corporation + * + * All rights reserved. + * + * BSD License + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this list + * of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ""AS IS"" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + + +#include + +TEE_Result +SetDefaultVariable( + WCHAR *Name, UINT32 NameSize, BYTE *Bin, UINT32 BinSize, + GUID Guid, ATTRIBUTES Attributes +) +/*++ + + Routine Description: + + Populates and uses a set variable paramteter structure for the + pre-compiled default variables. Called from the autogenerated + file defaultvars_encoding.c (see defaultvars.py). + + Arguments: + + Name - Null terminated WCHAR string with the variable name + + NameSize - Length of the name + + Bin - Pointer to the binary contents of the variable + + BinSize - Size of the binary + + Guid - Variable GUID + + Attributes - Attributes to set the variable with + + Returns: + + TEE_Result + +--*/ +{ + TEE_Result res; + UINT32 totalSize = sizeof(VARIABLE_SET_PARAM) + NameSize + BinSize; + PVARIABLE_SET_PARAM setParam = TEE_Malloc(totalSize, TEE_MALLOC_FILL_ZERO); + + if(!setParam) { + return TEE_ERROR_OUT_OF_MEMORY; + } + + setParam->Size = sizeof(VARIABLE_SET_PARAM); + setParam->NameSize = NameSize; + setParam->VendorGuid = Guid; + setParam->Attributes = Attributes; + setParam->DataSize = BinSize; + setParam->OffsetName = 0; + setParam->OffsetData = NameSize; + + memcpy(&setParam->Payload[0], Name, NameSize); + memcpy(&setParam->Payload[NameSize], Bin, BinSize); + + res = SetVariable(totalSize, setParam); + + TEE_Free(setParam); + + return res; +} \ No newline at end of file diff --git a/TAs/optee_ta/AuthVars/defaultvars/defaultvars.h b/TAs/optee_ta/AuthVars/defaultvars/defaultvars.h index 2b504ec..2ea8ef3 100644 --- a/TAs/optee_ta/AuthVars/defaultvars/defaultvars.h +++ b/TAs/optee_ta/AuthVars/defaultvars/defaultvars.h @@ -35,10 +35,13 @@ #include #include #include -#include "ntdefs.h" -#include "uefidefs.h" -#include "defaultbins.h" +#include +#include +#include +/* + * Defined in an autogenerated file, see defaultvars.py. + */ TEE_Result InitializeDefaultVariables( VOID @@ -46,6 +49,6 @@ InitializeDefaultVariables( TEE_Result SetDefaultVariable( - CHAR16 *Name, UINT32 NameSize, BYTE *Bin, UINT32 BinSize, + WCHAR *Name, UINT32 NameSize, BYTE *Bin, UINT32 BinSize, GUID Guid, ATTRIBUTES Attributes ); \ No newline at end of file diff --git a/TAs/optee_ta/AuthVars/defaultvars/defaultvars.py b/TAs/optee_ta/AuthVars/defaultvars/defaultvars.py new file mode 100644 index 0000000..3212290 --- /dev/null +++ b/TAs/optee_ta/AuthVars/defaultvars/defaultvars.py @@ -0,0 +1,174 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. +from __future__ import print_function + +import os, sys, json, codecs + +# Reads a utf-8 encoded json file containing default variables, then generates +# a .c file which is responsible for placing those variables into memory when +# the TA first runs (ie has no stored NV state available). +# ex: python defaultvars.py input.json output.c + +# The json file must be utf-8 encoded. +# -- The name field may contain valid unicode characters so long as they can be encoded +# with ECS2 (a subset of utf-16 which does not allow 4 byte merged characters). +# -- The binary path is relative to the TA root (AuthvVars/), or +# absolute (/path/to/file.json). +# -- The guid can be either valid C GUID struct syntax or one of the well known +# GUIDs from uefidefs.h +# -- The attributes are also selected from uefidefs.h. +# +# Example JSON to add a one-time volatile variable: +# { +# "variables": [ +# { +# "name": "ECS2 Variable Name", +# "bin_path": "/path/to/variable.bin", +# "guid": "{0xe4b297c1, 0x507d, 0x407f, {0xbe,0x4a,0xe9,0x25,0x68,0x69,0x25,0x14}}", +# < OR > +# "guid": "", +# "attributes": "EFI_VARIABLE_APPEND_WRITE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS" +# } +# ] +# } + +def write_header(f, input_file_name): + f.write( +''' +/* Automatically generated file created by defaultvars.py */ +/* Add hard-coded variables to the image by editing %s */ + +#include + +''' % input_file_name) + +# Convert characters to a valid ECS2 encoding. Leave a subset of basic ASCII +# characters alone, but convert anything else to the form \x1234. Each +# character of the form \x1234 is enclosed in quotes: ie "foo / bar" becomes +# L"foo " "\x002f" " bar". The compiler will automatically concatenate the +# strings. Throws an error on any valid utf-16 characters which cannot be +# represented by ECS2. +def convert_to_ecs2_wchar(char): + if ord(char) >= 0xd800 and ord(char) <= 0xdfff: + raise ValueError("Invalid ECS2 encoding (UTF-16 surrogate) for character '%c'" % char) + if ord(char) > 0xffff: + raise ValueError("Invalid ECS2 encoding for character '%c', to large for ECS2" % char) + if ((ord(char) in range(ord('a'), ord('z'))) or + (ord(char) in range(ord('A'), ord('Z'))) or + (ord(char) in range(ord('0'), ord('9'))) or + (ord(char) == ord(' '))): + # Leave very basic characters alone (a-z,A-Z,0-9,' ') + return char + else: + # Generates something of the form: " "\x0123" " for everything else + return (r'" "\x' + ('%04x' % ord(char)) + '" "' ) + +# We could compile unicode strings here, but it is easier to convert to hex +# representation instead of handing escaping everything correctly. Also +# verify we are not encoding any 4 byte characters which ECS2 can't handle. +def format_wchar_string(string): + return 'L"' + ''.join(convert_to_ecs2_wchar(c) for c in string) + '"' + +# Formats data into a valid C array (ie { 0x0, 0x1, 0x2, ...}) +def format_byte_array(bytes): + line_length = 16 + return '{\n\t%s\n}' % (''.join([ + '0x%02x, ' % byte + ('\n\t' if ((i+1) % line_length == 0) else '') + for i, byte in enumerate(bytearray(bytes)) + ])) + +# Generate tupples of the form: +# (name, , guid, attributes) +# from a file containing json data. +def parse_json(json_file): + json_data = json_file.read() + json_data = json.loads(json_data, encoding='utf-8') + vars = list() + for variable in json_data['variables']: + variable_name = variable['name'] + variable_bin_path = variable['bin_path'] + variable_guild = variable['guid'] + variable_attributes = variable['attributes'] + + with open(variable_bin_path, 'rb') as bin_file: + vars.append((format_wchar_string(variable_name), + format_byte_array(bin_file.read()), + variable_guild, + variable_attributes)) + return vars + +# Generates a code chunk for each variable of the form: +# WCHAR var__name[] = L"Abcd" "\x1234" "5678"; +# GUID var__guid = {0x12345678,0x9abc,0xdef0,{0x12,0x34,0x56,0x78,0x9A,0xBC,0xDE,0xF0}}; +# +# GUID var__guid = +# ATTRIBUTES var__attributes = "ATTR1 | ATTR2 | ... "; +# BYTE var__bin[] = { 0x0, 0x1, 0x2, ... }; +def write_variables(f, variables): + for i,var in enumerate(variables): + f.write(('WCHAR var_%d_name[] = ' % i) + var[0] + ";\n") + f.write(('GUID var_%d_guid = ' % i) + var[2] + ";\n") + f.write(('ATTRIBUTES var_%d_attributes = {' % i) + var[3] + "};\n") + f.write(('BYTE var_%d_bin[] = ' % i) + var[1] + ";\n") + f.write('\n') + +# Calls SetDefaultVariable() with variables created with write_variables() for +# a given variable number +def format_setvar_single_call(var_num): + var_name = 'var_%d_name' % var_num + var_name_size = 'sizeof(%s)' % var_name + var_bin = 'var_%d_bin' % var_num + var_bin_size = 'sizeof(%s)' % var_bin + var_guid = 'var_%d_guid' % var_num + var_attributes = 'var_%d_attributes' % var_num + return 'SetDefaultVariable(%s, %s, %s, %s, %s, %s)' % ( + var_name, var_name_size, var_bin, var_bin_size, + var_guid, var_attributes) + +# Fills in the body of InitializeDefaultVariables() with calls to set +# each variable from the json file in the order they are found in the file. +def format_setvar_calls(variables): + return ''.join([ +''' + res = %s; + if (res != TEE_SUCCESS) + return res; +''' % format_setvar_single_call(i) for i,_ in enumerate(variables) + ]) + +# Defines the function InitializeDefaultVariables(), then fills it with a +# a set call for each default variable. +def write_end(f, variables): + f.write(''' +TEE_Result InitializeDefaultVariables( VOID ) { + TEE_Result res; +%s + DMSG("Done setting %d default variables"); + return TEE_SUCCESS; +} +''' % (format_setvar_calls(variables), len(variables)) ) + +num_args = len(sys.argv) +if num_args != 3: + raise ValueError("Expecting 2 arguments (in.json, out.c)") +else: + json_file_in = sys.argv[1] + json_file_in_path = os.path.abspath(json_file_in) + c_file_out = sys.argv[2] + c_file_out_path = os.path.abspath(c_file_out) + +print("defaultvars.py: Checking for variable file at", json_file_in_path) +if not os.path.isfile(json_file_in_path): + raise FileNotFoundError("Could not find input " + str(json_file_in_path)) + +if os.path.isfile(c_file_out_path): + print("defaultvars.py: Clearing output file at", c_file_out_path) + os.remove(c_file_out_path) + +with codecs.open(json_file_in_path, encoding='utf-8', mode='r') as json_file: + variables = parse_json(json_file) + +with open(c_file_out_path, 'w') as c_file: + write_header(c_file, json_file_in_path) + write_variables(c_file, variables) + write_end(c_file, variables) diff --git a/TAs/optee_ta/AuthVars/defaultvars/default_vars.json b/TAs/optee_ta/AuthVars/defaultvars/defaultvars_example.json similarity index 51% rename from TAs/optee_ta/AuthVars/defaultvars/default_vars.json rename to TAs/optee_ta/AuthVars/defaultvars/defaultvars_example.json index 17158c6..cd5f272 100644 --- a/TAs/optee_ta/AuthVars/defaultvars/default_vars.json +++ b/TAs/optee_ta/AuthVars/defaultvars/defaultvars_example.json @@ -1,10 +1,10 @@ { "variables": [ { - "name": "Default Variable Exampleé", - "bin_path": "./default_variable_example.bin", + "name": "Default Volatile Variable Example / (Supports ECS-2 characters ✍)", + "bin_path": "defaultvars/example.bin", "guid": "{0xe4b297c1, 0x507d, 0x407f, {0xbe,0x4a,0xe9,0x25,0x68,0x69,0x25,0x14}}", - "attributes": "EFI_VARIABLE_APPEND_WRITE | EFI_VARIABLE_RUNTIME_ACCESS" + "attributes": "EFI_VARIABLE_APPEND_WRITE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS" } ] } \ No newline at end of file diff --git a/TAs/optee_ta/AuthVars/defaultvars/defaultvars_secureboot_example.json b/TAs/optee_ta/AuthVars/defaultvars/defaultvars_secureboot_example.json new file mode 100644 index 0000000..45445d9 --- /dev/null +++ b/TAs/optee_ta/AuthVars/defaultvars/defaultvars_secureboot_example.json @@ -0,0 +1,22 @@ +{ + "variables": [ + { + "name": "db", + "bin_path": "defaultvars/example_db.bin", + "guid": "EFI_IMAGE_SECURITY_DATABASE_GUID", + "attributes": "EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS" + }, + { + "name": "KEK", + "bin_path": "defaultvars/example_KEK.bin", + "guid": "EFI_GLOBAL_VARIABLE", + "attributes": "EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS" + }, + { + "name": "PK", + "bin_path": "defaultvars/example_PK.bin", + "guid": "EFI_GLOBAL_VARIABLE", + "attributes": "EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS" + } + ] +} \ No newline at end of file diff --git a/TAs/optee_ta/AuthVars/defaultvars/default_variable_example.bin b/TAs/optee_ta/AuthVars/defaultvars/example.bin similarity index 100% rename from TAs/optee_ta/AuthVars/defaultvars/default_variable_example.bin rename to TAs/optee_ta/AuthVars/defaultvars/example.bin diff --git a/TAs/optee_ta/AuthVars/defaultvars/example_KEK.bin b/TAs/optee_ta/AuthVars/defaultvars/example_KEK.bin new file mode 100644 index 0000000000000000000000000000000000000000..16706ad7ab5d6f568cb51bec88f467070e67d53b GIT binary patch literal 6748 zcmchbcT^NfyT+Me$Z5zKhMb{?BuNC3AV?IDoU$=L?9ec7K-== zfP&BiC@^LgCKv<;Lj%qF?^1;)r70vd#j>iXuU>Zl?S2pex;rZz1~6e`>OnBcsWlbV zVOj_`8>F?Tqnj&COFA;dsE83Yk%%N*SOk5DNs37t0rYTM zfcn=npg*7CZ0Tv|hH!zY+4=w|5bGbuL{AwL1tP&f*W-gxAP`2@Yc|#AvW(@A*$dp} z3g3o>wwSr7*M|+S)<_B5gc{i^NzF_f!4i%|nm_ndCI^}blaA&l>{vTL%&^p8e|BG1 ztTfs@j9gn1pCASdUn2Ov;^(jo?k@&qylOeX$yp&?siOPviNc>KpL~Ht7<0Qx zIOr+Cx6cAvB$HLf8&`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x8>(+m9V9 z7~1v_HJ)L_YwjVX3tzCGYRSv_%nE4YJ8^lr>TD)C`jwL$hG7jVwwK1J0~IrIp_rF>{nGUUcHLv!Hg)c2|6aEn3D(@>wa=KD3Rg ztv|SSN33c|6ZyKr0+aQ2S)32!NSZt|WvH%u)ijY9iGPJCqCVL|`kT*sP8EY`4pm*) z+g195>SPW#iOl)9ACW%KIvj~>a~WV*$wwb3$}mPdzqe9Ak#|M2=&MsKzME9My`v%i KQ8>8e`F{bgmn&8P literal 0 HcmV?d00001 diff --git a/TAs/optee_ta/AuthVars/defaultvars/example_PK.bin b/TAs/optee_ta/AuthVars/defaultvars/example_PK.bin new file mode 100644 index 0000000000000000000000000000000000000000..85f63546d04b89dc5aa02e8dec47896a20c8f260 GIT binary patch literal 5189 zcmeI0hf@^E0*BdMa$K_HU2@dfC1;SFc7X#Vs{|1w!;%$P$yq@%5+$RcB*_>MB#WRT zQNofDBp&IZ-mAKIcdzdLfa{v72{qk4)%}~V=bJeqFeRfT8Qy=(3f1`$c#AUU-4pHU zn;Tj&Wn#A_P3%hmEY$!Q1VT^&6oe$#A~Zqv5PUF*01qG|BoU0zjM%3D5rOisl&Sz0 zp9R2zA$brm2n>dXSP$G{h)T^;Np4Nz)YM!4Uj4WGL1gFEIT0{`oe*M-520m5tLee? zJ>Bduw%(3zt}uNSWf&4bNC51N6bK0ffRqFf076XEoRLCQ8i5o&-^8WGrOg2r1QWpc zvkmBvHk_|}-*EGEf$7-$16UB}@B2J|WC#{Sg?E0P6pRId@bYT8v>z(4J&EHkcAG7E z9U0MX?V{HdIkH?YBccU0cTks^o-&6eABwiU^{+__u|!gTC`{h6b-t5x9mSpQryyPx zZ;^5#744}3{sdh}4Y=)#>@MikWqIGmB#9IDmbi$z_k5L*i$eX7auq*xVi--XdFfH z5qbRO<3N}G*t;^nANTXQhG4-UJQGMS!33h268_LQYK&|ik_ox7Uskbf2k*XUoo2!7 z7iFM+LEV|fd9$)CaA=*YlVsQU5T;~_k4-9)E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x<95`b&Y${#clvNW*&@8B z_jO{6VU|XfEA8B>*gI7Dw*&ni#tmb+ubz<`{Pj+C>X{_&1>Vla-OeSwz@7E@Q>Ls6 ze@etBgMUayLIB}3e)%5UU4Uk`#CQVyf64eaEB+5M-dLcon!_b_N%DSLNgS-x@`CLvelrDcb}7{@!s-%!woRklh|a&yg#{g zAZmj)9++QRqxOSoCn}?To{;PQLZ|LVZQJzwJu!e)H;XPNshf69Y|u+rHb#r(ljHs?<^Y2xoeHKW%T} zSDTUfq^daxgnrum!osiRBav&k70hrQs$C`tHwTs2S2F&Hesh9vyPxn_J=a5UCxAlW z(aV=m@633jjgsp0eKV#n11*K!*a$Dxg>c%#0^iwI`9%%x;?s!6%66zlw9Tvh?2jPx zG?(GTu~4q-;}t$?MTvvFh~VpDWnrBfpZy2)x}vy?@7mJjrfn?|bVSRP7KsY_Atu>(si~ zduu>TMi2$b#KmG|vvk6qc3a$FFPvOrp`DizxBW##VcrvL{s}BXvs=MICUh@UhLFK) z?D18`+2fQB&R3G1`Eo8I$yG7bjG27u;9LY2bp2e=tj>iKkoYHx_}8BKdrkeFcAhIB zJ{E+RarMK_OPcM;3~7UdD>V@82KBqiNGdd`O>KhGs?(kS~bX^HxVKMqPIr4geHxRXE=LA==40YA4H97l{!Cka1&jb z^pk%9zfg1`xatzA2m6fwT>_-)IDlfMjGu1+yQYZPhmMBPc=_K*#Me5zq`YV*^CX2Q zgaxdG@#Pc2Tyj4w-TN1e(*MjQ7nm|7WSRn)aa(I#RM#*p;tFrLG zK_>iS8{e4sm3|8YY939ZTD>r2S%s+(YO>v7Iv_f8nPwGqlI*6KHiFfY9MY;b)Tn%( uZ`Sc>KBYuEvaW*OQs}Fv4*t)70GH19#(yaPO8vi5|F6{lEA{_J>i+{K4AFA{ literal 0 HcmV?d00001 diff --git a/TAs/optee_ta/AuthVars/defaultvars/example_db.bin b/TAs/optee_ta/AuthVars/defaultvars/example_db.bin new file mode 100644 index 0000000000000000000000000000000000000000..3b74d60627da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pn{$4mWSJeBthHu03})_ z$Ge5HB+wT78e+{Q!kUo%K23Atql{b@*=6WS7KW)xA8N0<>@^nCZXm6u=C$hdo+HbG z_6e^C#Qok=)d4lnF$L*sqf3JIgNiPK+?2UN15S~5N3*BiR*0#bS3mj2!aTXorGZIZ z9$}0?J<*P8`}`o0Ht+kLpbDx?0om+GEuH%4-#Vcyo!3s|WS7dWYq2MBB<%59b=$pK`N +#ifdef CFG_AUTHVARS_DEFAULT_VARS +#include +#endif + // // Auth Var in-memory storage layout @@ -207,6 +211,13 @@ AuthVarInitStorage( if (status == TEE_ERROR_ITEM_NOT_FOUND) { DMSG("No stored variables found"); +#ifdef CFG_AUTHVARS_DEFAULT_VARS + status = InitializeDefaultVariables(); + if (status != TEE_SUCCESS) { + EMSG("Failed to set default variables"); + goto Cleanup; + } +#endif status = TEE_SUCCESS; } goto Cleanup; diff --git a/TAs/optee_ta/AuthVars/sub.mk b/TAs/optee_ta/AuthVars/sub.mk index 4d713c5..c846f68 100644 --- a/TAs/optee_ta/AuthVars/sub.mk +++ b/TAs/optee_ta/AuthVars/sub.mk @@ -51,7 +51,7 @@ clean: clean_lib_symlinks cflags-y += $(AUTHVAR_FLAGS) $(SSL_FLAGS) $(INCLUDE_OVERWRITES) subdirs-y += lib -subdirs-y += defaultvars +subdirs-$(CFG_AUTHVARS_DEFAULT_VARS) += defaultvars global-incdirs-y += src/include From ccb37620971ecf683a9833a2e273270c8570668f Mon Sep 17 00:00:00 2001 From: Daniel McIlvaney Date: Fri, 23 Aug 2019 17:56:13 -0700 Subject: [PATCH 3/7] Add documentation, small cleanup --- TAs/optee_ta/AuthVars/README.md | 4 ++ TAs/optee_ta/AuthVars/defaultvars/README.md | 63 +++++++++++++++++++ .../defaultvars_secureboot_example.json | 6 +- TAs/optee_ta/AuthVars/defaultvars/sub.mk | 11 +++- 4 files changed, 78 insertions(+), 6 deletions(-) create mode 100644 TAs/optee_ta/AuthVars/defaultvars/README.md diff --git a/TAs/optee_ta/AuthVars/README.md b/TAs/optee_ta/AuthVars/README.md index 250db49..87e07e0 100644 --- a/TAs/optee_ta/AuthVars/README.md +++ b/TAs/optee_ta/AuthVars/README.md @@ -24,6 +24,10 @@ The storage is currently encrypted by OP-TEE using a TA Storage Key (TSK) encryp The UEFI spec describes an authenticated variable store for use with Secure Boot. The TA supports variable reads and writes up to 16KB in size (this is a limitation of the rich OS side driver). This should allow for a reasonable set of Secure Boot keys to be stored. +### Default Variables + +The AuthVar TA can encode a set of default variables at compile time, and place them into NV memory when it first runs. This is useful for forcing at default set of UEFI Secure Boot variables to be active during first boot, and as a protection against RPMB wipes. This can be turned on by setting `CFG_AUTHVARS_DEFAULT_VARS=y`. See [defaultvars/README.md](./defaultvars/README.md) for details on configuring this behavior. + ## Debugging AuthVars ### Clearing Storage diff --git a/TAs/optee_ta/AuthVars/defaultvars/README.md b/TAs/optee_ta/AuthVars/defaultvars/README.md new file mode 100644 index 0000000..ecb5b4b --- /dev/null +++ b/TAs/optee_ta/AuthVars/defaultvars/README.md @@ -0,0 +1,63 @@ +# Default Variables + +Setting `CFG_AUTHVARS_DEFAULT_VARS=y` will cause the AuthVars TA to ensure a set of default variables are present during first boot. + +If the TA detects that there is no pre-existing non-volatile storage it will set the default variables as if it received a call from UEFI. The name, GUID, attributes, and binary data of the variables are defined in a JSON file, passed via `CFG_DEFAULT_VARS_JSON=/path/to/vars.json`. + +## JSON File +The JSON file pointed to by `CFG_DEFAULT_VARS_JSON=/path/to/vars.json` defines which variables will be added. + +* **`name:`** ECS-2 encodable string. Since the JSON file is saved as UTF-8 the unicode characters can be placed directly in the string. +* **`bin_path:`** Path to the binary contents of the variable. If the variable is authenticated this binary should include the authentication headers. **If** the path is not absolute, it is taken relative to the AuthVars TA root directory (`MSRSec/TAs/optee_ta/AuthVars`). +* **`guid:`** The variable GUID, must be valid C syntax. May also be one of the well known GUIDs `EFI_IMAGE_SECURITY_DATABASE_GUID` or `EFI_GLOBAL_VARIABLE`. +* **`attributes:`** The attributes the variable should be saved with (from `uefidefs.h`). Must be valid C syntax. + +The variables will be saved into the AuthVar TA in the order they are found in the JSON file. + +```json +{ + "variables": [ + { + "name": "Default Volatile Variable Example / (Supports ECS-2 characters ✍)", + "bin_path": "defaultvars/example.bin", + "guid": "{0xe4b297c1, 0x507d, 0x407f, {0xbe,0x4a,0xe9,0x25,0x68,0x69,0x25,0x14}}", + "attributes": "EFI_VARIABLE_APPEND_WRITE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS" + } + ] +} +``` + +### Text Encoding of Variable Names +The JSON file **must be `UTF-8` encoded**, as the python script explicitly decodes it as UTF-8. The JSON file may contain *MOST*, **but not all** unicode characters. UEFI uses ECS-2 encoding for its strings, a subset of UTF-16. ECS-2 does not allow 4-byte characters to be stored, so no UTF-16 surrogate pairs. Valid ECS-2 values are: `[0x0000, 0xd800] ∪ [0xdfff, 0xffff]`. The python script used to compile the variables will check the validity of the encoding for each character. + + +## Compilation +At compilation time `defaultvars.py` runs, taking the JSON file found at `CFG_DEFAULT_VARS_JSON`, and creating the file `$(sub-dir-out)/defaultvars_encoding.c`. This C file encodes each variable in the JSON file as a set of C variables: +```c +WCHAR var_0_name[] = L"Abcd" "\x1234" "5678"; +GUID var_0_guid = {0x12345678,0x9abc,0xdef0,{0x12,0x34,0x56,0x78,0x9A,0xBC,0xDE,0xF0}}; +ATTRIBUTES var_0_attributes = "EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS"; +BYTE var_0_bin[] = { 0x0, 0x1, 0x2, 0x3, ... }; + +WCHAR var_2_name[] = L"XYZ" "\x1234" "5678"; +... +``` + +The generated C file also contains the definition for `InitializeDefaultVariables()`, which is defined in `defaultvars.h`, and is called during initialization of the TA in `AuthVarInitStorage()` if and only if there is no pre-existing non-volatile storage pressent (assuming `CFG_DEFAULT_VARS_JSON=y`). + +```c +TEE_Result InitializeDefaultVariables( VOID ) { + TEE_Result res; + + res = SetDefaultVariable(var_0_name, sizeof(var_0_name), var_0_bin, sizeof(var_0_bin), var_0_guid, var_0_attributes); + if (res != TEE_SUCCESS) + return res; + + res = SetDefaultVariable(var_1_name, sizeof(var_1_name), var_1_bin, sizeof(var_1_bin), var_1_guid, var_1_attributes); + if (res != TEE_SUCCESS) + return res; + + DMSG("Done setting 2 default variables"); + return TEE_SUCCESS; +} +``` diff --git a/TAs/optee_ta/AuthVars/defaultvars/defaultvars_secureboot_example.json b/TAs/optee_ta/AuthVars/defaultvars/defaultvars_secureboot_example.json index 45445d9..a172370 100644 --- a/TAs/optee_ta/AuthVars/defaultvars/defaultvars_secureboot_example.json +++ b/TAs/optee_ta/AuthVars/defaultvars/defaultvars_secureboot_example.json @@ -4,19 +4,19 @@ "name": "db", "bin_path": "defaultvars/example_db.bin", "guid": "EFI_IMAGE_SECURITY_DATABASE_GUID", - "attributes": "EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS" + "attributes": "EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS | EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS" }, { "name": "KEK", "bin_path": "defaultvars/example_KEK.bin", "guid": "EFI_GLOBAL_VARIABLE", - "attributes": "EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS" + "attributes": "EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS | EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS" }, { "name": "PK", "bin_path": "defaultvars/example_PK.bin", "guid": "EFI_GLOBAL_VARIABLE", - "attributes": "EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS" + "attributes": "EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS | EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS" } ] } \ No newline at end of file diff --git a/TAs/optee_ta/AuthVars/defaultvars/sub.mk b/TAs/optee_ta/AuthVars/defaultvars/sub.mk index 6313153..8ffdf4f 100644 --- a/TAs/optee_ta/AuthVars/defaultvars/sub.mk +++ b/TAs/optee_ta/AuthVars/defaultvars/sub.mk @@ -1,11 +1,16 @@ +CFG_DEFAULT_VARS_JSON ?= $(sub-dir)/defaultvars_example.json + DEFAULT_VARS_PATHS := $(shell find $(sub-dir) -name '*.bin') -DEFAULT_VARS_JSON ?= $(sub-dir)/defaultvars_example.json srcs-y += defaultvars.c global-incdirs-y += ./ +# Make can have trouble tracking updates to binary files outside the tree. +# It is very fast to rebuild the encoding, so just do that every time. +.PHONY: always_rebuild_defaultvars + gensrcs-y += default_vars produce-default_vars = defaultvars_encoding.c -depends-default_vars = $(DEFAULT_VARS_PATHS) $(sub-dir)/defaultvars.py $(DEFAULT_VARS_JSON) -recipe-default_vars = python3 $(sub-dir)/defaultvars.py $(DEFAULT_VARS_JSON) $(sub-dir-out)/defaultvars_encoding.c +depends-default_vars = always_rebuild_defaultvars +recipe-default_vars = python $(sub-dir)/defaultvars.py $(CFG_DEFAULT_VARS_JSON) $(sub-dir-out)/defaultvars_encoding.c cleanfiles += $(sub-dir-out)/defaultvars_encoding.c \ No newline at end of file From 9d5c2e0a190ebf13fad9528eb673f9f0846fea21 Mon Sep 17 00:00:00 2001 From: Daniel McIlvaney Date: Fri, 23 Aug 2019 18:06:22 -0700 Subject: [PATCH 4/7] Add description of each example JSON --- TAs/optee_ta/AuthVars/defaultvars/README.md | 9 ++++++++- .../AuthVars/defaultvars/defaultvars_example.json | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/TAs/optee_ta/AuthVars/defaultvars/README.md b/TAs/optee_ta/AuthVars/defaultvars/README.md index ecb5b4b..a92990a 100644 --- a/TAs/optee_ta/AuthVars/defaultvars/README.md +++ b/TAs/optee_ta/AuthVars/defaultvars/README.md @@ -21,12 +21,19 @@ The variables will be saved into the AuthVar TA in the order they are found in t "name": "Default Volatile Variable Example / (Supports ECS-2 characters ✍)", "bin_path": "defaultvars/example.bin", "guid": "{0xe4b297c1, 0x507d, 0x407f, {0xbe,0x4a,0xe9,0x25,0x68,0x69,0x25,0x14}}", - "attributes": "EFI_VARIABLE_APPEND_WRITE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS" + "attributes": "EFI_VARIABLE_APPEND_WRITE | EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS" } ] } ``` +### Examples +Two examples are included, along with binaries. + +The first example, `defaultvars_example.json`, is included by default if `CFG_DEFAULT_VARS_JSON` is left unset. It adds a single variable (as shown above). + +The second example, `defaultvars_secureboot_example.json` can be used by setting `CFG_DEFAULT_VARS_JSON=defaultvars/defaultvars_secureboot_example.json`. This example automatically enables secure boot using **NON-SECURE** keys from the [TurnkeySecurity](https://github.com/ms-iot/security/tree/master/TurnkeySecurity) repository. They are included for example purposes only. A production image must use newly generated signing keys. + ### Text Encoding of Variable Names The JSON file **must be `UTF-8` encoded**, as the python script explicitly decodes it as UTF-8. The JSON file may contain *MOST*, **but not all** unicode characters. UEFI uses ECS-2 encoding for its strings, a subset of UTF-16. ECS-2 does not allow 4-byte characters to be stored, so no UTF-16 surrogate pairs. Valid ECS-2 values are: `[0x0000, 0xd800] ∪ [0xdfff, 0xffff]`. The python script used to compile the variables will check the validity of the encoding for each character. diff --git a/TAs/optee_ta/AuthVars/defaultvars/defaultvars_example.json b/TAs/optee_ta/AuthVars/defaultvars/defaultvars_example.json index cd5f272..30fe0bc 100644 --- a/TAs/optee_ta/AuthVars/defaultvars/defaultvars_example.json +++ b/TAs/optee_ta/AuthVars/defaultvars/defaultvars_example.json @@ -4,7 +4,7 @@ "name": "Default Volatile Variable Example / (Supports ECS-2 characters ✍)", "bin_path": "defaultvars/example.bin", "guid": "{0xe4b297c1, 0x507d, 0x407f, {0xbe,0x4a,0xe9,0x25,0x68,0x69,0x25,0x14}}", - "attributes": "EFI_VARIABLE_APPEND_WRITE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS" + "attributes": "EFI_VARIABLE_APPEND_WRITE | EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS" } ] } \ No newline at end of file From 10cddd312ced12012bd1df1a8acbd15be05446a3 Mon Sep 17 00:00:00 2001 From: Daniel McIlvaney Date: Fri, 23 Aug 2019 18:12:00 -0700 Subject: [PATCH 5/7] Add TODO message. --- TAs/optee_ta/AuthVars/src/varmgmt.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/TAs/optee_ta/AuthVars/src/varmgmt.c b/TAs/optee_ta/AuthVars/src/varmgmt.c index e9f4525..30e23b8 100644 --- a/TAs/optee_ta/AuthVars/src/varmgmt.c +++ b/TAs/optee_ta/AuthVars/src/varmgmt.c @@ -215,6 +215,10 @@ AuthVarInitStorage( status = InitializeDefaultVariables(); if (status != TEE_SUCCESS) { EMSG("Failed to set default variables"); + // TODO: We should set up all the variables which were just + // created, or add variable by variable checking to make sure + // we don't miss any variables in the case one fails, or the + // TA is interuppted during its first boot. goto Cleanup; } #endif From 0dbeaa77f1c0d670c174fcd91c71fe3f7c862b4e Mon Sep 17 00:00:00 2001 From: Daniel McIlvaney Date: Thu, 5 Sep 2019 14:03:36 -0700 Subject: [PATCH 6/7] Update TAs/optee_ta/AuthVars/README.md --- TAs/optee_ta/AuthVars/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TAs/optee_ta/AuthVars/README.md b/TAs/optee_ta/AuthVars/README.md index 87e07e0..54a8323 100644 --- a/TAs/optee_ta/AuthVars/README.md +++ b/TAs/optee_ta/AuthVars/README.md @@ -26,7 +26,7 @@ The UEFI spec describes an authenticated variable store for use with Secure Boot ### Default Variables -The AuthVar TA can encode a set of default variables at compile time, and place them into NV memory when it first runs. This is useful for forcing at default set of UEFI Secure Boot variables to be active during first boot, and as a protection against RPMB wipes. This can be turned on by setting `CFG_AUTHVARS_DEFAULT_VARS=y`. See [defaultvars/README.md](./defaultvars/README.md) for details on configuring this behavior. +The AuthVar TA can encode a set of default variables at compile time, and place them into NV memory when it first runs. This is useful for forcing a default set of UEFI Secure Boot variables to be active during first boot, and as a protection against RPMB wipes. This can be turned on by setting `CFG_AUTHVARS_DEFAULT_VARS=y`. See [defaultvars/README.md](./defaultvars/README.md) for details on configuring this behavior. ## Debugging AuthVars From dc0aa4db880a542518a0605a2267df48ba284891 Mon Sep 17 00:00:00 2001 From: Daniel McIlvaney Date: Mon, 16 Sep 2019 14:40:41 -0700 Subject: [PATCH 7/7] Expand TODO comment --- TAs/optee_ta/AuthVars/src/varmgmt.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/TAs/optee_ta/AuthVars/src/varmgmt.c b/TAs/optee_ta/AuthVars/src/varmgmt.c index 30e23b8..f56dd55 100644 --- a/TAs/optee_ta/AuthVars/src/varmgmt.c +++ b/TAs/optee_ta/AuthVars/src/varmgmt.c @@ -215,10 +215,20 @@ AuthVarInitStorage( status = InitializeDefaultVariables(); if (status != TEE_SUCCESS) { EMSG("Failed to set default variables"); - // TODO: We should set up all the variables which were just - // created, or add variable by variable checking to make sure - // we don't miss any variables in the case one fails, or the - // TA is interuppted during its first boot. + // TODO: Make this operation atomic. + + // Ex: Setting default variables {A, B, C, db, kek, pk}. + // If {A, B, C} are set correctly, but setting db fails, the TA + // we be in a bad state. The TA will be 'initialized' since it + // has at least one persistent object available (A, B, and C), + // and will make no further attempts to set db, kek, pk. + // + // This step must be made atomic either by removing the variables, + // or setting a NV flag when done. Setting a flag is probably + // preferable since it will handle both graceful failures and + // premature power cycles. + + // WipeMemory(); goto Cleanup; } #endif