efi_loader: add secure boot support
v5 cover efi_loader: add secure boot support
# Documentation for UEFI secure boot on U-Boot will be submitted in
# a separate patch in near future.
One of major missing features in current UEFI implementation is "secure boot." The ultimate goal of my attempt is to implement image authentication based on signature and provide UEFI secure boot support which would be fully compliant with UEFI specification, section 32[1].
(The code was originally developed by Patrick Wildt.)
Please note, however, this patch doesn't work on its own; there are a couple of functional dependencies[2] and [3], that I have submitted before. For complete workable patch set, see my repository[4], which also contains experimental timestamp-based revocation suuport.
My "non-volatile" support[5], which is under discussion, is not mandatory and so not included here, but this inevitably implies that, for example, signature database variables, like db and dbx, won't be persistent unless you explicitly run "env save" command.
Anyhow, Linaro is also working on implementing real "secure storage" solution based on TF-A and OP-TEE.
Supported features:
* image authentication based on db and dbx
* supported signature types are EFI_CERT_SHA256_GUID (SHA256 digest for unsigned images) EFI_CERT_X509_GUID (x509 certificate for signed images)
* SecureBoot/SignatureSupport variables
* SetupMode and user mode
* variable authentication based on PK and KEK EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
* basic pytest test cases
Unsupported features: (marked as TODO in most cases in the source code,
and won't be included in this series)
* hash algorithms other than SHA256
* dbt: timestamp(RFC6131)-based certificate revocation
* dbr: OS recovery
* xxxDefault: default values for signature stores
* transition to AuditMode and DeployedMode
* recording rejected images in EFI_IMAGE_EXECUTION_INFO_TABLE
* verification "policy", in particular, check against signature's owner
* private authenticated variables
* variable authentication with EFI_VARIABLE_ENHANCED_AUTHENTICATED_ACCESS
* real secure storage support, including hardware-specific PK (Platform Key) installation
TODO's other than "Unsupported features": (won't be included in this series)
* fail recovery, in particular, in modifying authenticated variables
* support read-only attributes of well-defined global variables in particular, "SignatureSupport"
* Extensive test suite (or more test cases) to confirm compatibility with EDK2
=> I requested EDK SCT community to add tests[6].
Test:
* My pytest, included in this patch set, passed.
* efi_selftest passed. (At least no regression.)
* Travis CI tests have passed.
Known issues:
* efitools is used in pytest, and its version must be v1.5.2 or later. (Solution: You can define EFITOOLS_PATH in defs.py for your own efitools.)
Hints about how to use:
(Please see other documents, or my pytest scripts, for details.)
* You can create your own certificates with openssl.
* You can sign your application with sbsign (on Ubuntu).
* You can create raw data for signature database with efitools, and install/manage authenticated variables with "env -set -e" command or efitools' "UpdateVars.efi" application.
[1] https://uefi.org/sites/default/files/resources/UEFI_Spec_2_8_final.pdf
[2] https://lists.denx.de/pipermail/u-boot/2019-November/390127.html
(import x509/pkcs7 parsers from linux)
[3] https://lists.denx.de/pipermail/u-boot/2020-January/398057.html
(extend rsa_verify() for UEFI secure boot)
[4] http://git.linaro.org/people/takahiro.akashi/u-boot.git/ efi/secboot
[5] https://lists.denx.de/pipermail/u-boot/2019-September/382835.html
(non-volatile variables support)
[6] https://bugzilla.tianocore.org/show_bug.cgi?id=2230
Changes in v5 (Jan 28, 2020)
* rebased to pre-v2020.04-rc1 (fixed some merge conflicts)
* remove already-merged commits (v4's patch#1)
* fix a compile error caused by gcc 9.x (patch#4)
* return SECURITY_VIOLATION instead of ACCESS_DENIED if authentication fails (patch#7)
* use qsort() for section sorting (patch#7)
* add "efidebug test" sub-command (patch#11)
* add efi_start_image(SECURITY_VIOLATION) test (patch#14)
Changes in v4 (Dec 18, 2019)
* adjust EFI_SECURE_BOOT dependencies due to a change of RSA extension patch v5 (patch#2)
* change "imply" to "select" against kconfig dependencies (patch#2)
* otherwise, no functional changes
Changes in v3 (Dec 9, 2019)
* allow for arbitrary number of regions in efi_image_region_add() (patch#3, #5 and #8)
* remove a redundant check in a while loop at efi_sigstore_free() (patch#4)
Changes in v2 (Nov 26, 2019)
* rebased to v2020.01-rc3
* rename IMAGE_DIRECTORY_ENTRY_CERTTABLE to IMAGE_DIRECTORY_ENTRY_SECURITY (patch#1,#9)
* add comments (patch#1)
* drop v1's patch#2 as it is no longer necessary
* drop v1's patch#3 as other "SECURE_BOOT" architectures have renamed this option and no longer use it
* add structure descriptions (patch#3)
* rework hash calculation code in efi_signature_verify() and remove an odd constant, WinIndrectSha256 (patch#3)
* move travis.yml changes to a separate patch (patch#12, #16)
* yield_fixture() -> fixture() (patch#12)
* call console.restart_uboot() at every test case (13,#14)
* add patch#15; enable UEFI-related configurations by default on sandbox
* add patch#16; modify Travis CI environment to run UEFI secure boot test
Changes in v1 (Nov 13, 2019)
* rebased to v2020.01-rc
* remove already-merged patches
* re-work the patch set for easier reviews, including
- move a config definition patch forward (patch#4)
- refactor/rename verification functions (patch#5/#10)
- split signature database parser as a separate patch (patch#6)
- split secure state transition code as a separate patch (patch#8)
- move most part of init_secure_boot() into init_variables() (patch#8)
- split test environment setup from test patches (patch#14)
* add function descriptions (patch#5-#11)
* make sure the section list is sorted in ascending order in hash calculation of PE image (patch#10)
* add a new "-at" (authenticated access) option to "env -e" (patch#13)
* list required host packages, in particular udisks2, in pytest (patch#14)
* modify conftest.py to run under python3 (patch#14)
* use a partition on a disk instead of a whole disk without partition table (patch#14)
* reduce dependency on efitools, yet relying on its host tools (patch#14)
* modify pytests to catch up wth latest changes of "env -e" syntax (patch#15,#16)
RFC (Sept 18, 2019)
AKASHI Takahiro (16):
efi_loader: add CONFIG_EFI_SECURE_BOOT config option
efi_loader: add signature verification functions
efi_loader: add signature database parser
efi_loader: variable: support variable authentication
efi_loader: variable: add secure boot state transition
efi_loader: variable: add VendorKeys variable
efi_loader: image_loader: support image authentication
efi_loader: set up secure boot
cmd: env: use appropriate guid for authenticated UEFI variable
cmd: env: add "-at" option to "env set -e" command
cmd: efidebug: add "test bootmgr" sub-command efi_loader, pytest: set up secure boot environment efi_loader, pytest: add UEFI secure boot tests (authenticated
variables)
efi_loader, pytest: add UEFI secure boot tests (image)
sandbox: add extra configurations for UEFI and related tests
travis: add packages for UEFI secure boot test
.travis.yml | 11 +- cmd/efidebug.c | 78 +- cmd/nvedit.c | 5 +- cmd/nvedit_efi.c | 23 +- configs/sandbox64_defconfig | 3 + configs/sandbox_defconfig | 3 + include/efi_api.h | 87 ++ include/efi_loader.h | 91 +- lib/efi_loader/Kconfig | 18 + lib/efi_loader/Makefile | 1 + lib/efi_loader/efi_boottime.c | 10 +- lib/efi_loader/efi_image_loader.c | 460 ++++++++- lib/efi_loader/efi_setup.c | 38 + lib/efi_loader/efi_signature.c | 809 +++++++++++++++ lib/efi_loader/efi_variable.c | 951 ++++++++++++++++-- test/py/README.md | 8 + test/py/tests/test_efi_secboot/conftest.py | 151 +++ test/py/tests/test_efi_secboot/defs.py | 21 + .../py/tests/test_efi_secboot/test_authvar.py | 282 ++++++ test/py/tests/test_efi_secboot/test_signed.py | 117 +++ .../tests/test_efi_secboot/test_unsigned.py | 121 +++ 21 files changed, 3157 insertions(+), 131 deletions(-) create mode 100644 lib/efi_loader/efi_signature.c create mode 100644 test/py/tests/test_efi_secboot/conftest.py create mode 100644 test/py/tests/test_efi_secboot/defs.py create mode 100644 test/py/tests/test_efi_secboot/test_authvar.py create mode 100644 test/py/tests/test_efi_secboot/test_signed.py create mode 100644 test/py/tests/test_efi_secboot/test_unsigned.py
Signature · 1 line
2.24.0
PATCH v5 1/16 efi_loader: add CONFIG_EFI_SECURE_BOOT config option
Under this configuration, UEFI secure boot support will be added in later patches.
--- lib/efi_loader/Kconfig | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)
lib/efi_loader/Kconfig +18 -0
@@ -130,4 +130,22 @@ config EFI_RNG_PROTOCOL "Support for EFI_RNG_PROTOCOL implementation. Uses the rng device on the platform" +config EFI_SECURE_BOOT + bool "Enable EFI secure boot support" + depends on EFI_LOADER + select SHA256 + select RSA + select RSA_VERIFY_WITH_PKEY + select IMAGE_SIGN_INFO + select ASYMMETRIC_KEY_TYPE + select ASYMMETRIC_PUBLIC_KEY_SUBTYPE + select X509_CERTIFICATE_PARSER + select PKCS7_MESSAGE_PARSER + default n + help + Select this option to enable EFI secure boot support. + Once SecureBoot mode is enforced, any EFI binary can run only if + it is signed with a trusted key. To do that, you need to install, + at least, PK, KEK and db. + endif
Signature · 1 line
2.24.0
PATCH v5 1/16 efi_loader: add CONFIG_EFI_SECURE_BOOT config option
Quoted 4 lines · AKASHI Takahiro
Under this configuration, UEFI secure boot support will be added in later patches.
Signed-off-by AKASHI Takahiro <[email protected]>
This patch should be after all the patches that are necessary for secure boot, i.e. after patch 09/16. I can take care of that.
Best regards
Heinrich
Quoted 32 lines
--- lib/efi_loader/Kconfig | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)lib/efi_loader/Kconfig +18
-0@@ -130,4 +130,22 @@ config EFI_RNG_PROTOCOL "Support for EFI_RNG_PROTOCOL implementation. Uses the rng device on the platform" +config EFI_SECURE_BOOT + bool "Enable EFI secure boot support" + depends on EFI_LOADER + select SHA256 + select RSA + select RSA_VERIFY_WITH_PKEY + select IMAGE_SIGN_INFO + select ASYMMETRIC_KEY_TYPE + select ASYMMETRIC_PUBLIC_KEY_SUBTYPE + select X509_CERTIFICATE_PARSER + select PKCS7_MESSAGE_PARSER + default n + help + Select this option to enable EFI secure boot support. + Once SecureBoot mode is enforced, any EFI binary can run only if + it is signed with a trusted key. To do that, you need to install, + at least, PK, KEK and db. + endif
PATCH v5 1/16 efi_loader: add CONFIG_EFI_SECURE_BOOT config option
Quoted 8 lines · Heinrich Schuchardt
Quoted 4 lines · AKASHI Takahiro
Under this configuration, UEFI secure boot support will be added in later patches.
Signed-off-by AKASHI Takahiro <[email protected]>This patch should be after all the patches that are necessary for secure boot, i.e. after patch 09/16. I can take care of that.
I disagree.
Doing so will constrain bisect ability to some extent because any code under EFI_SECURE_BOOT will never have a chance to be compiled until this patch is applied.
Then bisect result could be inaccurate.
Thanks,
-Takahiro Akashi
Quoted 37 lines
Best regards
Heinrich
Quoted 32 lines
--- lib/efi_loader/Kconfig | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)lib/efi_loader/Kconfig +18
-0@@ -130,4 +130,22 @@ config EFI_RNG_PROTOCOL "Support for EFI_RNG_PROTOCOL implementation. Uses the rng device on the platform" +config EFI_SECURE_BOOT + bool "Enable EFI secure boot support" + depends on EFI_LOADER + select SHA256 + select RSA + select RSA_VERIFY_WITH_PKEY + select IMAGE_SIGN_INFO + select ASYMMETRIC_KEY_TYPE + select ASYMMETRIC_PUBLIC_KEY_SUBTYPE + select X509_CERTIFICATE_PARSER + select PKCS7_MESSAGE_PARSER + default n + help + Select this option to enable EFI secure boot support. + Once SecureBoot mode is enforced, any EFI binary can run only if + it is signed with a trusted key. To do that, you need to install, + at least, PK, KEK and db. + endif
PATCH v5 2/16 efi_loader: add signature verification functions
In reply to AKASHI TakahiroIn this commit, implemented are a couple of helper functions which will be used to materialize variable authentication as well as image authentication in later patches.
--- include/efi_api.h | 87 +++++ include/efi_loader.h | 72 ++++ lib/efi_loader/Makefile | 1 + lib/efi_loader/efi_signature.c | 583 +++++++++++++++++++++++++++++++++ 4 files changed, 743 insertions(+) create mode 100644 lib/efi_loader/efi_signature.c
include/efi_api.h +87 -0
@@ -18,6 +18,7 @@#include <efi.h> #include <charset.h> +#include <pe.h> #ifdef CONFIG_EFI_LOADER #include <asm/setjmp.h>
@@ -307,6 +308,10 @@ struct efi_runtime_services {EFI_GUID (0x8be4df61 ,0x93ca ,0x11d2 ,0xaa ,0x0d , \ 0x00, 0xe0, 0x98, 0x03, 0x2b, 0x8c) +#define EFI_IMAGE_SECURITY_DATABASE_GUID \ + EFI_GUID(0xd719b2cb, 0x3d3a, 0x4596, 0xa3, 0xbc, \ + 0xda, 0xd0, 0x0e, 0x67, 0x65, 0x6f) +#define EFI_FDT_GUID \ EFI_GUID(0xb1b621d5, 0xf19c, 0x41a5, \ 0x83, 0x0b, 0xd9, 0x15, 0x2c, 0x69, 0xaa, 0xe0)
@@ -1616,4 +1621,86 @@ struct efi_unicode_collation_protocol {#define LOAD_OPTION_CATEGORY_BOOT 0x00000000#define LOAD_OPTION_CATEGORY_APP 0x00000100 +/* Certificate types in signature database */ +#define EFI_CERT_SHA256_GUID \ + EFI_GUID(0xc1c41626, 0x504c, 0x4092, 0xac, 0xa9, \ + 0x41, 0xf9, 0x36, 0x93, 0x43, 0x28) +#define EFI_CERT_RSA2048_GUID \ + EFI_GUID(0x3c5766e8, 0x269c, 0x4e34, 0xaa, 0x14, \ + 0xed, 0x77, 0x6e, 0x85, 0xb3, 0xb6) +#define EFI_CERT_X509_GUID \ + EFI_GUID(0xa5c059a1, 0x94e4, 0x4aa7, 0x87, 0xb5, \ + 0xab, 0x15, 0x5c, 0x2b, 0xf0, 0x72) +#define EFI_CERT_X509_SHA256_GUID \ + EFI_GUID(0x3bd2a492, 0x96c0, 0x4079, 0xb4, 0x20, \ + 0xfc, 0xf9, 0x8e, 0xf1, 0x03, 0xed) +#define EFI_CERT_TYPE_PKCS7_GUID \ + EFI_GUID(0x4aafd29d, 0x68df, 0x49ee, 0x8a, 0xa9, \ + 0x34, 0x7d, 0x37, 0x56, 0x65, 0xa7) + +/** +* win_certificate_uefi_guid - A certificate that encapsulates +* a GUID-specific signature +* +* @hdr: Windows certificate header +* @cert_type: Certificate type +* @cert_data: Certificate data +*/ +struct win_certificate_uefi_guid { +WIN_CERTIFICATE hdr; +efi_guid_t cert_type; +u8 cert_data[]; +} __attribute__((__packed__ )); + +/** +* efi_variable_authentication_2 - A time-based authentication method +* descriptor +* +* This structure describes an authentication information for +* a variable with EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS +* and should be included as part of a variable's value. +* Only EFI_CERT_TYPE_PKCS7_GUID is accepted. +* +* @time_stamp: Descriptor's time stamp +* @auth_info: Authentication info +*/ +struct efi_variable_authentication_2 { +struct efi_time time_stamp; +struct win_certificate_uefi_guid auth_info; +} __attribute__((__packed__ )); + +/** +* efi_signature_data - A format of signature +* +* This structure describes a single signature in signature database. +* +* @signature_owner: Signature owner +* @signature_data: Signature data +*/ +struct efi_signature_data { +efi_guid_t signature_owner; +u8 signature_data[]; +} __attribute__((__packed__ )); + +/** +* efi_signature_list - A format of signature database +* +* This structure describes a list of signatures with the same type. +* An authenticated variable's value is a concatenation of one or more +* efi_signature_list's. +* +* @signature_type: Signature type +* @signature_list_size: Size of signature list +* @signature_header_size: Size of signature header +* @signature_size: Size of signature +*/ +struct efi_signature_list { +efi_guid_t signature_type; +u32 signature_list_size; +u32 signature_header_size; +u32 signature_size; +/* u8 signature_header[signature_header_size]; */ +/* struct efi_signature_data signatures[...][signature_size]; */ +} __attribute__((__packed__ )); +#endif
include/efi_loader.h +72 -0
90 lines not on this page · raw message
lib/efi_loader/Makefile +1 -0
@@ -43,3 +43,4 @@ obj-$(CONFIG_NET) += efi_net.o obj-$(CONFIG_GENERATE_ACPI_TABLE) += efi_acpi.o obj-$(CONFIG_GENERATE_SMBIOS_TABLE) += efi_smbios.o obj-$(CONFIG_EFI_RNG_PROTOCOL) += efi_rng.o +obj-y += efi_signature.o
lib/efi_loader/efi_signature.c new file +583 -0
583 lines not on this page · raw message
Signature · 1 line
2.24.0
PATCH v5 3/16 efi_loader: add signature database parser
In reply to AKASHI Takahiroefi_signature_parse_sigdb() is a helper function will be used to parse signature database variable and instantiate a signature store structure in later patches.
--- include/efi_loader.h | 3 + lib/efi_loader/efi_signature.c | 226 +++++++++++++++++++++++++++++++++ 2 files changed, 229 insertions(+)
include/efi_loader.h +3 -0
@@ -735,6 +735,9 @@ bool efi_signature_verify_with_sigdb(struct efi_image_regions *regs,efi_status_t efi_image_region_add (struct efi_image_regions *regs,const void *start,const void *end,int nocheck); + +void efi_sigstore_free (struct efi_signature_store *sigstore); +struct efi_signature_store *efi_sigstore_parse_sigdb (u16 *name);#endif /* CONFIG_EFI_SECURE_BOOT */ #else /* CONFIG_IS_ENABLED(EFI_LOADER) */
lib/efi_loader/efi_signature.c +226 -0
230 lines not on this page · raw message
Signature · 1 line
2.24.0
PATCH v5 4/16 efi_loader: variable: support variable authentication
In reply to AKASHI TakahiroWith this commit, EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS is supported for authenticated variables and the system secure state will transfer between setup mode and user mode as UEFI specification section 32.3 describes.
Internally, authentication data is stored as part of authenticated variable's value. It is nothing but a pkcs7 message (but we need some wrapper, see efi_variable_parse_signature()) and will be validated by efi_variable_authenticate(), hence efi_signature_verify_with_db().
Associated time value will be encoded in "{...,time=...}" along with other UEFI variable's attributes.
--- include/efi_loader.h | 3 + lib/efi_loader/efi_variable.c | 665 ++++++++++++++++++++++++++++------ 2 files changed, 564 insertions(+), 104 deletions(-)
include/efi_loader.h +3 -0
@@ -184,6 +184,7 @@ extern const efi_guid_t efi_guid_image_security_database;extern const efi_guid_t efi_guid_sha256;extern const efi_guid_t efi_guid_cert_x509;extern const efi_guid_t efi_guid_cert_x509_sha256; +extern const efi_guid_t efi_guid_cert_type_pkcs7;/* GUID of RNG protocol */ extern const efi_guid_t efi_guid_rng_protocol;
@@ -738,6 +739,8 @@ efi_status_t efi_image_region_add(struct efi_image_regions *regs,void efi_sigstore_free (struct efi_signature_store *sigstore);struct efi_signature_store *efi_sigstore_parse_sigdb (u16 *name); + +bool efi_secure_boot_enabled (void );#endif /* CONFIG_EFI_SECURE_BOOT */ #else /* CONFIG_IS_ENABLED(EFI_LOADER) */
lib/efi_loader/efi_variable.c +561 -104
832 lines not on this page · raw message
Signature · 1 line
2.24.0
PATCH v5 4/16 efi_loader: variable: support variable authentication
Quoted 806 lines · AKASHI Takahiro
With this commit,
EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESSis supported for authenticated variables and the system secure state will transfer between setup mode and user mode as UEFI specification section 32.3 describes.Internally, authentication data is stored as part of authenticated variable's value. It is nothing but a pkcs7 message (but we need some wrapper, see
efi_variable_parse_signature()) and will be validated byefi_variable_authenticate(), henceefi_signature_verify_with_db().Associated time value will be encoded in "{...,time=...}" along with other UEFI variable's attributes.
Signed-off-by AKASHI Takahiro <[email protected]>--- include/efi_loader.h | 3 + lib/efi_loader/efi_variable.c | 665 ++++++++++++++++++++++++++++------ 2 files changed, 564 insertions(+), 104 deletions(-)include/efi_loader.h +3
-0@@ -184,6 +184,7 @@ extern const efi_guid_t efi_guid_image_security_database;extern const efi_guid_t efi_guid_sha256;extern const efi_guid_t efi_guid_cert_x509;extern const efi_guid_t efi_guid_cert_x509_sha256; +extern const efi_guid_t efi_guid_cert_type_pkcs7;/* GUID of RNG protocol */ extern const efi_guid_t efi_guid_rng_protocol;@@ -738,6 +739,8 @@ efi_status_t efi_image_region_add(struct efi_image_regions *regs,void efi_sigstore_free (struct efi_signature_store *sigstore);struct efi_signature_store *efi_sigstore_parse_sigdb (u16 *name); + +bool efi_secure_boot_enabled (void );#endif /* CONFIG_EFI_SECURE_BOOT */ #else /* CONFIG_IS_ENABLED(EFI_LOADER) */ lib/efi_loader/efi_variable.c +482
-104@@ -10,8 +10,14 @@#include <env_internal.h> #include <hexdump.h> #include <malloc.h> +#include <rtc.h> #include <search.h> +#include <linux/compat.h> #include <u-boot/crc.h> +#include "../lib/crypto/pkcs7_parser.h" + +const efi_guid_t efi_guid_cert_type_pkcs7 =EFI_CERT_TYPE_PKCS7_GUID ; +static bool efi_secure_boot;#define READ_ONLY BIT(31)@@ -108,7 +114,7 @@ static const char *prefix(const char *str, const char *prefix)* @attrp: pointer to UEFI attributes * Return: pointer to remainder of U-Boot variable value */ -static const char *parse_attr (const char *str,u32 *attrp) +static const char *parse_attr (const char *str,u32 *attrp,u64 *timep) {u32 attr =0 ;char sep ='{' ;@@ -131,6 +137,12 @@ static const char *parse_attr(const char *str, u32 *attrp) attr |=EFI_VARIABLE_BOOTSERVICE_ACCESS ; }else if ((s =prefix (str,"run" ))) { attr |=EFI_VARIABLE_RUNTIME_ACCESS ; + }else if ((s =prefix (str,"time=" ))) { + attr |=EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS ; +hex2bin ((u8 *)timep, s,sizeof (*timep)); + s +=sizeof (*timep) *2 ; + }else if (*str =='}' ) { +break ; }else {printf ("invalid attribute: %s\n" , str);break ;@@ -148,48 +160,291 @@ static const char *parse_attr(const char *str, u32 *attrp) }/** -* efi_get_variable() - retrieve value of a UEFI variable +* efi_secure_boot_enabled - return if secure boot is enabled or not * -* This function implements the GetVariable runtime service. +* Return: true if enabled, false if disabled +*/ +bool efi_secure_boot_enabled (void ) +{ +return efi_secure_boot; +} + +#ifdef CONFIG_EFI_SECURE_BOOT +static u8 pkcs7_hdr[] = { +/* SEQUENCE */ +0x30 ,0x82 ,0x05 ,0xc7 , +/* OID: pkcs7-signedData */ +0x06 ,0x09 ,0x2a ,0x86 ,0x48 ,0x86 ,0xf7 ,0x0d ,0x01 ,0x07 ,0x02 , +/* Context Structured? */ +0xa0 ,0x82 ,0x05 ,0xb8 , +}; + +/** +* efi_variable_parse_signature - parse a signature in variable +* @buf: Pointer to variable's value +* @buflen: Length of @buf * -* See the Unified Extensible Firmware Interface (UEFI) specification for -* details. +* Parse a signature embedded in variable's value and instantiate +* a pkcs7_message structure. Since pkcs7_parse_message() accepts only +* pkcs7's signedData, some header needed be prepended for correctly +* parsing authentication data, particularly for variable's. * -* @variable_name: name of the variable -* @vendor: vendor GUID -* @attributes: attributes of the variable -* @data_size: size of the buffer to which the variable value is copied -* @data: buffer to which the variable value is copied -* Return: status code +* Return: Pointer to pkcs7_message structure on success, NULL on error */ -efi_status_t EFIAPI efi_get_variable(u16 *variable_name, -const efi_guid_t *vendor,u32 *attributes, -efi_uintn_t *data_size,void *data) +static struct pkcs7_message *efi_variable_parse_signature (const void *buf, +size_t buflen) +{ +u8 *ebuf; +size_t ebuflen, len; +struct pkcs7_message *msg; + +/* +* This is the best assumption to check if the binary is +* already in a form of pkcs7's signedData. +*/ +if (buflen >sizeof (pkcs7_hdr) && + !memcmp (&((u8 *)buf)[4 ], &pkcs7_hdr[4 ],11 )) { + msg =pkcs7_parse_message (buf, buflen); + goto out; + } + +/* +* Otherwise, we should add a dummy prefix sequence for pkcs7 +* message parser to be able to process. +* NOTE: EDK2 also uses similar hack in WrapPkcs7Data() +* in CryptoPkg/Library/BaseCryptLib/Pk/CryptPkcs7VerifyCommon.c +* TODO: +* The header should be composed in a more refined manner. +*/ +debug ("Makeshift prefix added to authentication data\n" ); + ebuflen =sizeof (pkcs7_hdr) + buflen; +if (ebuflen <=0x7f ) { +debug ("Data is too short\n" ); +return NULL ; + } + + ebuf =malloc (ebuflen); +if (!ebuf) { +debug ("Out of memory\n" ); +return NULL ; + } + +memcpy (ebuf, pkcs7_hdr,sizeof (pkcs7_hdr)); +memcpy (ebuf +sizeof (pkcs7_hdr), buf, buflen); + len = ebuflen -4 ; + ebuf[2 ] = (len >>8 ) &0xff ; + ebuf[3 ] = len &0xff ; + len = ebuflen -0x13 ; + ebuf[0x11 ] = (len >>8 ) &0xff ; + ebuf[0x12 ] = len &0xff ; + + msg =pkcs7_parse_message (ebuf, ebuflen); + +free (ebuf); + +out: +if (IS_ERR (msg)) +return NULL ; + +return msg; +} + +/** +* efi_variable_authenticate - authenticate a variable +* @variable: Variable name in u16 +* @vendor: Guid of variable +* @data_size: Size of @data +* @data: Pointer to variable's value +* @given_attr: Attributes to be given at SetVariable() +* @env_attr: Attributes that an existing variable holds +* @time: signed time that an existing variable holds +* +* Called by efi_set_variable() to verify that the input is correct. +* Will replace the given data pointer with another that points to +* the actual data to store in the internal memory. +* On success, @data and @data_size will be replaced with variable's +* actual data, excluding authentication data, and its size, and variable's +* attributes and signed time will also be returned in @env_attr and @time, +* respectively. +* +* Return: EFI_SUCCESS on success, status code (negative) on error +*/ +static efi_status_t efi_variable_authenticate (u16 *variable, +const efi_guid_t *vendor, +efi_uintn_t *data_size, +const void **data,u32 given_attr, +u32 *env_attr,u64 *time) +{ +const struct efi_variable_authentication_2 *auth; +struct efi_signature_store *truststore, *truststore2; +struct pkcs7_message *var_sig; +struct efi_image_regions *regs; +struct efi_time timestamp; +struct rtc_time tm; +u64 new_time; +efi_status_t ret; + + var_sig =NULL ; + truststore =NULL ; + truststore2 =NULL ; + regs =NULL ; + ret =EFI_SECURITY_VIOLATION ; + +if (*data_size <sizeof (struct efi_variable_authentication_2 )) + goto err; + +/* authentication data */ + auth = *data; +if (*data_size < (sizeof (auth->time_stamp) + + auth->auth_info.hdr.dwLength)) + goto err; + +if (guidcmp (&auth->auth_info.cert_type, &efi_guid_cert_type_pkcs7)) + goto err; + + *data +=sizeof (auth->time_stamp) + auth->auth_info.hdr.dwLength; + *data_size -= (sizeof (auth->time_stamp) + + auth->auth_info.hdr.dwLength); + +memcpy (×tamp, &auth->time_stamp,sizeof (timestamp)); +memset (&tm,0 ,sizeof (tm)); + tm.tm_year = timestamp.year; + tm.tm_mon = timestamp.month; + tm.tm_mday = timestamp.day; + tm.tm_hour = timestamp.hour; + tm.tm_min = timestamp.minute; + tm.tm_sec = timestamp.second; + new_time =rtc_mktime (&tm); + +if (!efi_secure_boot_enabled ()) { +/* finished checking */ + *time = new_time; +return EFI_SUCCESS ; + } + +if (new_time <= *time) + goto err; + +/* data to be digested */ + regs =calloc (sizeof (*regs) +sizeof (struct image_region ) *5 ,1 ); +if (!regs) + goto err; + regs->max =5 ; +efi_image_region_add (regs, (uint8_t *)variable, + (uint8_t *)variable + +u16_strlen (variable) *sizeof (u16),1 ); +efi_image_region_add (regs, (uint8_t *)vendor, + (uint8_t *)vendor +sizeof (*vendor),1 ); +efi_image_region_add (regs, (uint8_t *)&given_attr, + (uint8_t *)&given_attr +sizeof (given_attr),1 ); +efi_image_region_add (regs, (uint8_t *)×tamp, + (uint8_t *)×tamp +sizeof (timestamp),1 ); +efi_image_region_add (regs, (uint8_t *)*data, + (uint8_t *)*data + *data_size,1 ); + +/* variable's signature list */ +if (auth->auth_info.hdr.dwLength <sizeof (auth->auth_info)) + goto err; + var_sig =efi_variable_parse_signature (auth->auth_info.cert_data, + auth->auth_info.hdr.dwLength + -sizeof (auth->auth_info)); +if (IS_ERR (var_sig)) { +debug ("Parsing variable's signature failed\n" ); + var_sig =NULL ; + goto err; + } + +/* signature database used for authentication */ +if (u16_strcmp (variable,L"PK" ) ==0 || +u16_strcmp (variable,L"KEK" ) ==0 ) { +/* with PK */ + truststore =efi_sigstore_parse_sigdb (L"PK" ); +if (!truststore) + goto err; + }else if (u16_strcmp (variable,L"db" ) ==0 || +u16_strcmp (variable,L"dbx" ) ==0 ) { +/* with PK and KEK */ + truststore =efi_sigstore_parse_sigdb (L"KEK" ); + truststore2 =efi_sigstore_parse_sigdb (L"PK" ); + +if (!truststore) { +if (!truststore2) + goto err; + + truststore = truststore2; + truststore2 =NULL ; + } + }else { +/* TODO: support private authenticated variables */ + goto err; + } + +/* verify signature */ +if (efi_signature_verify_with_sigdb (regs, var_sig, truststore,NULL )) { +debug ("Verified\n" ); + }else { +if (truststore2 && +efi_signature_verify_with_sigdb (regs, var_sig, + truststore2,NULL )) { +debug ("Verified\n" ); + }else { +debug ("Verifying variable's signature failed\n" ); + goto err; + } + } + +/* finished checking */ + *time =rtc_mktime (&tm); + ret =EFI_SUCCESS ; + +err: +efi_sigstore_free (truststore); +efi_sigstore_free (truststore2); +pkcs7_free_message (var_sig); +free (regs); + +return ret; +} +#else +static efi_status_t efi_variable_authenticate (u16 *variable, +const efi_guid_t *vendor, +efi_uintn_t *data_size, +const void **data,u32 given_attr, +u32 *env_attr,u64 *time) +{ +return EFI_SUCCESS ; +} +#endif /* CONFIG_EFI_SECURE_BOOT */ + +static +efi_status_t EFIAPI efi_get_variable_common(u16 *variable_name, +const efi_guid_t *vendor, +u32 *attributes, +efi_uintn_t *data_size,void *data, +bool is_non_volatile) {char *native_name;efi_status_t ret;unsigned long in_size; -const char *val, *s; +const char *val =NULL , *s; +u64 time =0 ;u32 attr; -EFI_ENTRY ("\"%ls\" %pUl %p %p %p" , variable_name, vendor, attributes, - data_size, data); -if (!variable_name || !vendor || !data_size)return EFI_EXIT (EFI_INVALID_PARAMETER ); ret =efi_to_native (&native_name, variable_name, vendor);if (ret) -return EFI_EXIT (ret); +return ret;EFI_PRINT ("get '%s'\n" , native_name); val =env_get (native_name);free (native_name);if (!val) -return EFI_EXIT (EFI_NOT_FOUND ); +return EFI_NOT_FOUND ; - val =parse_attr (val, &attr); + val =parse_attr (val, &attr, &time); in_size = *data_size;@@ -198,7 +453,7 @@ efi_status_t EFIAPI efi_get_variable(u16 *variable_name,/* number of hexadecimal digits must be even */ if (len &1 ) -return EFI_EXIT (EFI_DEVICE_ERROR ); +return EFI_DEVICE_ERROR ;/* two characters per byte: */ len /=2 ;@@ -209,11 +464,13 @@ efi_status_t EFIAPI efi_get_variable(u16 *variable_name, goto out; } -if (!data) -return EFI_EXIT (EFI_INVALID_PARAMETER ); +if (!data) { +debug ("Variable with no data shouldn't exist.\n" ); +return EFI_INVALID_PARAMETER ; + }if (hex2bin (data, s, len)) -return EFI_EXIT (EFI_DEVICE_ERROR ); +return EFI_DEVICE_ERROR ;EFI_PRINT ("got value: \"%s\"\n" , s); }else if ((s =prefix (val,"(utf8)" ))) {@@ -226,8 +483,10 @@ efi_status_t EFIAPI efi_get_variable(u16 *variable_name, goto out; } -if (!data) -return EFI_EXIT (EFI_INVALID_PARAMETER ); +if (!data) { +debug ("Variable with no data shouldn't exist.\n" ); +return EFI_INVALID_PARAMETER ; + }memcpy (data, s, len); ((char *)data)[len] ='\0' ;@@ -235,13 +494,67 @@ efi_status_t EFIAPI efi_get_variable(u16 *variable_name,EFI_PRINT ("got value: \"%s\"\n" , (char *)data); }else {EFI_PRINT ("invalid value: '%s'\n" , val); -return EFI_EXIT (EFI_DEVICE_ERROR ); +return EFI_DEVICE_ERROR ; } out:if (attributes) *attributes = attr &EFI_VARIABLE_MASK ; +return ret; +} + +static +efi_status_t EFIAPI efi_get_volatile_variable(u16 *variable_name, +const efi_guid_t *vendor, +u32 *attributes, +efi_uintn_t *data_size, +void *data) +{ +return efi_get_variable_common (variable_name, vendor, attributes, + data_size, data, false); +} + +efi_status_t EFIAPI efi_get_nonvolatile_variable(u16 *variable_name, +const efi_guid_t *vendor, +u32 *attributes, +efi_uintn_t *data_size, +void *data) +{ +return efi_get_variable_common (variable_name, vendor, attributes, + data_size, data, true); +} + +/** +* efi_efi_get_variable() - retrieve value of a UEFI variable +* +* This function implements the GetVariable runtime service. +* +* See the Unified Extensible Firmware Interface (UEFI) specification for +* details. +* +* @variable_name: name of the variable +* @vendor: vendor GUID +* @attributes: attributes of the variable +* @data_size: size of the buffer to which the variable value is copied +* @data: buffer to which the variable value is copied +* Return: status code +*/ +efi_status_t EFIAPI efi_get_variable(u16 *variable_name, +const efi_guid_t *vendor,u32 *attributes, +efi_uintn_t *data_size,void *data) +{ +efi_status_t ret; + +EFI_ENTRY ("\"%ls\" %pUl %p %p %p" , variable_name, vendor, attributes, + data_size, data); + + ret =efi_get_volatile_variable (variable_name, vendor, attributes, + data_size, data); +if (ret ==EFI_NOT_FOUND ) + ret =efi_get_nonvolatile_variable (variable_name, vendor, + attributes, data_size, data); +return EFI_EXIT (ret); }@@ -274,6 +587,7 @@ static efi_status_t parse_uboot_variable(char *variable, {char *guid, *name, *end, c;unsigned long name_len; +u64 time;u16 *p; guid =strchr (variable,'_' );@@ -308,7 +622,7 @@ static efi_status_t parse_uboot_variable(char *variable, *(name -1 ) = c;/* attributes */ -parse_attr (end, attributes); +parse_attr (end, attributes, &time);return EFI_SUCCESS ; }@@ -390,7 +704,7 @@ efi_status_t EFIAPI efi_get_next_variable_name(efi_uintn_t *variable_name_size, list_len =hexport_r (&env_htab,'\n' ,H_MATCH_REGEX |H_MATCH_KEY , &efi_variables_list,0 ,1 , regexlist); -/* 1 indicates that no match was found */ +if (list_len <=1 )return EFI_EXIT (EFI_NOT_FOUND );@@ -403,143 +717,286 @@ efi_status_t EFIAPI efi_get_next_variable_name(efi_uintn_t *variable_name_size,return EFI_EXIT (ret); } -/** -* efi_set_variable() - set value of a UEFI variable -* -* This function implements the SetVariable runtime service. -* -* See the Unified Extensible Firmware Interface (UEFI) specification for -* details. -* -* @variable_name: name of the variable -* @vendor: vendor GUID -* @attributes: attributes of the variable -* @data_size: size of the buffer with the variable value -* @data: buffer with the variable value -* Return: status code -*/ -efi_status_t EFIAPI efi_set_variable(u16 *variable_name, -const efi_guid_t *vendor,u32 attributes, -efi_uintn_t data_size,const void *data) +static +efi_status_t EFIAPI efi_set_variable_common(u16 *variable_name, +const efi_guid_t *vendor, +u32 attributes, +efi_uintn_t data_size, +const void *data, +bool ro_check, +bool is_non_volatile) { -char *native_name =NULL , *val =NULL , *s; -const char *old_val; -size_t old_size; -efi_status_t ret =EFI_SUCCESS ; +char *native_name =NULL , *old_data =NULL , *val =NULL , *s; +efi_uintn_t old_size; +bool append, delete; +u64 time =0 ;u32 attr; +efi_status_t ret =EFI_SUCCESS ; -EFI_ENTRY ("\"%ls\" %pUl %x %zu %p" , variable_name, vendor, attributes, - data_size, data); +debug ("%s: set '%s'\n" , __func__, native_name);if (!variable_name || !*variable_name || !vendor || ((attributes &EFI_VARIABLE_RUNTIME_ACCESS ) && !(attributes &EFI_VARIABLE_BOOTSERVICE_ACCESS ))) { ret =EFI_INVALID_PARAMETER ; - goto out; + goto err; } ret =efi_to_native (&native_name, variable_name, vendor);if (ret) - goto out; + goto err; + +/* check if a variable exists */ + old_size =0 ; + attr =0 ; + ret =EFI_CALL (efi_get_variable (variable_name, vendor, &attr, + &old_size,NULL )); +if (ret ==EFI_BUFFER_TOO_SMALL ) { +if ((is_non_volatile && !(attr &EFI_VARIABLE_NON_VOLATILE )) || + (!is_non_volatile && (attr &EFI_VARIABLE_NON_VOLATILE ))) { + ret =EFI_INVALID_PARAMETER ; + goto err; + } + } - old_val =env_get (native_name); -if (old_val) { - old_val =parse_attr (old_val, &attr); + append = !!(attributes &EFI_VARIABLE_APPEND_WRITE ); + attributes &= ~(u32 )EFI_VARIABLE_APPEND_WRITE ; + delete = !append && (!data_size || !attributes); -/* check read-only first */ -if (attr &READ_ONLY ) { +/* check attributes */ +if (old_size) { +if (ro_check && (attr &READ_ONLY )) { ret =EFI_WRITE_PROTECTED ; - goto out; - } - -if ((data_size ==0 && - !(attributes &EFI_VARIABLE_APPEND_WRITE )) || - !attributes) { -/* delete the variable: */ -env_set (native_name,NULL ); - ret =EFI_SUCCESS ; - goto out; + goto err; }/* attributes won't be changed */ -if (attr != (attributes & ~EFI_VARIABLE_APPEND_WRITE )) { +if (!delete && + ((ro_check && attr != attributes) || + (!ro_check && ((attr & ~(u32 )READ_ONLY ) + != (attributes & ~(u32 )READ_ONLY ))))) { ret =EFI_INVALID_PARAMETER ; - goto out; - } - -if (attributes &EFI_VARIABLE_APPEND_WRITE ) { -if (!prefix (old_val,"(blob)" )) { - ret =EFI_DEVICE_ERROR ; - goto out; - } - old_size =strlen (old_val); - }else { - old_size =0 ; + goto err; } }else { -if (data_size ==0 || !attributes || - (attributes &EFI_VARIABLE_APPEND_WRITE )) { +if (delete || append) {/* * Trying to delete or to update a non-existent * variable. */ ret =EFI_NOT_FOUND ; - goto out; + goto err; + } + } + +if (((!u16_strcmp (variable_name,L"PK" ) || + !u16_strcmp (variable_name,L"KEK" )) && + !guidcmp (vendor, &efi_global_variable_guid)) || + ((!u16_strcmp (variable_name,L"db" ) || + !u16_strcmp (variable_name,L"dbx" )) && + !guidcmp (vendor, &efi_guid_image_security_database))) { +/* authentication is mandatory */ +if (!(attributes & +EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS )) { +debug ("%ls: AUTHENTICATED_WRITE_ACCESS required\n" , + variable_name); + ret =EFI_INVALID_PARAMETER ; + goto err; } + } + +/* authenticate a variable */ +if (IS_ENABLED (CONFIG_EFI_SECURE_BOOT )) { +if (attributes &EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS ) { + ret =EFI_INVALID_PARAMETER ; + goto err; + } +if (attributes & +EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS ) { + ret =efi_variable_authenticate (variable_name, vendor, + &data_size, &data, + attributes, &attr, + &time); +if (ret !=EFI_SUCCESS ) + goto err; + +/* last chance to check for delete */ +if (!data_size) + delete = true; + } + }else { +if (attributes & + (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS | +EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS )) { +debug ("Secure boot is not configured\n" ); + ret =EFI_INVALID_PARAMETER ; + goto err; + } + } + +/* delete a variable */ +if (delete) { +/* !old_size case has been handled before */ + val =NULL ; + ret =EFI_SUCCESS ; + goto out; + } +if (append) { + old_data =malloc (old_size); +if (!old_data) { +return EFI_OUT_OF_RESOURCES ; + goto err; + } + ret =EFI_CALL (efi_get_variable (variable_name, vendor, + &attr, &old_size, old_data)); +if (ret !=EFI_SUCCESS ) + goto err; + }else { old_size =0 ; } - val =malloc (old_size +2 * data_size - +strlen ("{ro,run,boot,nv}(blob)" ) +1 ); + val =malloc (2 * old_size +2 * data_size + +strlen ("{ro,run,boot,nv,time=0123456701234567}(blob)" ) + +1 );if (!val) { ret =EFI_OUT_OF_RESOURCES ; - goto out; + goto err; } s = val; -/* store attributes */ - attributes &= (EFI_VARIABLE_NON_VOLATILE | +/* +* store attributes +*/ + attributes &= (READ_ONLY | +EFI_VARIABLE_NON_VOLATILE |EFI_VARIABLE_BOOTSERVICE_ACCESS | -EFI_VARIABLE_RUNTIME_ACCESS ); +EFI_VARIABLE_RUNTIME_ACCESS | +EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS ); s +=sprintf (s,"{" );while (attributes) { -u32 attr =1 << (ffs (attributes) -1 ); + attr =1 << (ffs (attributes) -1 ); -if (attr ==EFI_VARIABLE_NON_VOLATILE ) +if (attr ==READ_ONLY ) { + s +=sprintf (s,"ro" ); + }else if (attr ==EFI_VARIABLE_NON_VOLATILE ) { s +=sprintf (s,"nv" ); -else if (attr ==EFI_VARIABLE_BOOTSERVICE_ACCESS ) + }else if (attr ==EFI_VARIABLE_BOOTSERVICE_ACCESS ) { s +=sprintf (s,"boot" ); -else if (attr ==EFI_VARIABLE_RUNTIME_ACCESS ) + }else if (attr ==EFI_VARIABLE_RUNTIME_ACCESS ) { s +=sprintf (s,"run" ); + }else if (attr == +EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS ) { + s +=sprintf (s,"time=" ); + s =bin2hex (s, (u8 *)&time,sizeof (time)); + } attributes &= ~attr;if (attributes) s +=sprintf (s,"," ); } s +=sprintf (s,"}" ); - -if (old_size) -/* APPEND_WRITE */ - s +=sprintf (s, old_val); -else - s +=sprintf (s,"(blob)" ); + s +=sprintf (s,"(blob)" );/* store payload: */ +if (append) + s =bin2hex (s, old_data, old_size); s =bin2hex (s, data, data_size); *s ='\0' ;EFI_PRINT ("setting: %s=%s\n" , native_name, val); +out:if (env_set (native_name, val)) ret =EFI_DEVICE_ERROR ; +else + ret =EFI_SUCCESS ; -out: +err:free (native_name); +free (old_data);free (val); -return EFI_EXIT (ret);
lib/efi_loader/efi_variable.c efi_status_t EFIAPI efi_get_next_variable_name(efi_uintn_t *variable_name_size, · line 930
Quoted 10 lines
lib/efi_loader/efi_variable.c +10
-0+return ret; +} + +static +efi_status_t EFIAPI efi_set_volatile_variable(u16 *variable_name, +const efi_guid_t *vendor, +u32 attributes, +efi_uintn_t data_size, +const void *data, +bool ro_check)
Why would we need such a function which is only used once? Better simply call efi_set_variable_common() with the correct value of ro_check.
lib/efi_loader/efi_variable.c efi_status_t EFIAPI efi_get_next_variable_name(efi_uintn_t *variable_name_size, · line 941
Quoted 11 lines
lib/efi_loader/efi_variable.c +11
-0+{ +return efi_set_variable_common (variable_name, vendor, attributes, + data_size, data, ro_check, false); +} + +efi_status_t EFIAPI efi_set_nonvolatile_variable(u16 *variable_name, +const efi_guid_t *vendor, +u32 attributes, +efi_uintn_t data_size, +const void *data, +bool ro_check)
Same here.
Quoted 5 lines
lib/efi_loader/efi_variable.c +5
-0+{ +efi_status_t ret; + + ret =efi_set_variable_common (variable_name, vendor, attributes, + data_size, data, ro_check, true);
lib/efi_loader/efi_variable.c efi_status_t EFIAPI efi_get_next_variable_name(efi_uintn_t *variable_name_size, · line 956
Quoted 10 lines
lib/efi_loader/efi_variable.c +10
-0+ +return ret; +} + +static efi_status_t efi_set_variable_internal (u16 *variable_name, +const efi_guid_t *vendor, +u32 attributes, +efi_uintn_t data_size, +const void *data, +bool ro_check)
I think this function should be exported. This will allow to avoid EFI_CALL in efi_bootmgr.c and efi_setup.c.
lib/efi_loader/efi_variable.c efi_status_t EFIAPI efi_get_next_variable_name(efi_uintn_t *variable_name_size, · line 958
Quoted 2 lines
lib/efi_loader/efi_variable.c +2
-0+{ +efi_status_t ret;
Your could simply write:
ret = efi_set_variable_common(variable_name, vendor, attributes,
data_size, data, ro_check, !!(attributes & EFI_VARIABLE_NON_VOLATILE));
Best regards
Heinrich
Quoted 45 lines
lib/efi_loader/efi_variable.c +41
-0+ +if (attributes &EFI_VARIABLE_NON_VOLATILE ) + ret =efi_set_nonvolatile_variable (variable_name, vendor, + attributes, + data_size, data, ro_check); +else + ret =efi_set_volatile_variable (variable_name, vendor, + attributes, data_size, data, + ro_check); + +return ret; +} + +/** +* efi_set_variable() - set value of a UEFI variable +* +* This function implements the SetVariable runtime service. +* +* See the Unified Extensible Firmware Interface (UEFI) specification for +* details. +* +* @variable_name: name of the variable +* @vendor: vendor GUID +* @attributes: attributes of the variable +* @data_size: size of the buffer with the variable value +* @data: buffer with the variable value +* Return: status code +*/ +efi_status_t EFIAPI efi_set_variable(u16 *variable_name, +const efi_guid_t *vendor,u32 attributes, +efi_uintn_t data_size,const void *data) +{ +EFI_ENTRY ("\"%ls\" %pUl %x %zu %p" , variable_name, vendor, attributes, + data_size, data); + +/* READ_ONLY bit is not part of API */ + attributes &= ~(u32 )READ_ONLY ; + +return EFI_EXIT (efi_set_variable_internal (variable_name, vendor, + attributes, data_size, data, + true)); }/**
PATCH v5 4/16 efi_loader: variable: support variable authentication
Quoted 806 lines · Heinrich Schuchardt
Quoted 805 lines · AKASHI Takahiro
With this commit,
EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESSis supported for authenticated variables and the system secure state will transfer between setup mode and user mode as UEFI specification section 32.3 describes.Internally, authentication data is stored as part of authenticated variable's value. It is nothing but a pkcs7 message (but we need some wrapper, see
efi_variable_parse_signature()) and will be validated byefi_variable_authenticate(), henceefi_signature_verify_with_db().Associated time value will be encoded in "{...,time=...}" along with other UEFI variable's attributes.
Signed-off-by AKASHI Takahiro <[email protected]>--- include/efi_loader.h | 3 + lib/efi_loader/efi_variable.c | 665 ++++++++++++++++++++++++++++------ 2 files changed, 564 insertions(+), 104 deletions(-)include/efi_loader.h +3
-0@@ -184,6 +184,7 @@ extern const efi_guid_t efi_guid_image_security_database;extern const efi_guid_t efi_guid_sha256;extern const efi_guid_t efi_guid_cert_x509;extern const efi_guid_t efi_guid_cert_x509_sha256; +extern const efi_guid_t efi_guid_cert_type_pkcs7;/* GUID of RNG protocol */ extern const efi_guid_t efi_guid_rng_protocol;@@ -738,6 +739,8 @@ efi_status_t efi_image_region_add(struct efi_image_regions *regs,void efi_sigstore_free (struct efi_signature_store *sigstore);struct efi_signature_store *efi_sigstore_parse_sigdb (u16 *name); + +bool efi_secure_boot_enabled (void );#endif /* CONFIG_EFI_SECURE_BOOT */ #else /* CONFIG_IS_ENABLED(EFI_LOADER) */ lib/efi_loader/efi_variable.c +482
-103@@ -10,8 +10,14 @@#include <env_internal.h> #include <hexdump.h> #include <malloc.h> +#include <rtc.h> #include <search.h> +#include <linux/compat.h> #include <u-boot/crc.h> +#include "../lib/crypto/pkcs7_parser.h" + +const efi_guid_t efi_guid_cert_type_pkcs7 =EFI_CERT_TYPE_PKCS7_GUID ; +static bool efi_secure_boot;#define READ_ONLY BIT(31)@@ -108,7 +114,7 @@ static const char *prefix(const char *str, const char *prefix)* @attrp: pointer to UEFI attributes * Return: pointer to remainder of U-Boot variable value */ -static const char *parse_attr (const char *str,u32 *attrp) +static const char *parse_attr (const char *str,u32 *attrp,u64 *timep) {u32 attr =0 ;char sep ='{' ;@@ -131,6 +137,12 @@ static const char *parse_attr(const char *str, u32 *attrp) attr |=EFI_VARIABLE_BOOTSERVICE_ACCESS ; }else if ((s =prefix (str,"run" ))) { attr |=EFI_VARIABLE_RUNTIME_ACCESS ; + }else if ((s =prefix (str,"time=" ))) { + attr |=EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS ; +hex2bin ((u8 *)timep, s,sizeof (*timep)); + s +=sizeof (*timep) *2 ; + }else if (*str =='}' ) { +break ; }else {printf ("invalid attribute: %s\n" , str);break ;@@ -148,48 +160,291 @@ static const char *parse_attr(const char *str, u32 *attrp) }/** -* efi_get_variable() - retrieve value of a UEFI variable +* efi_secure_boot_enabled - return if secure boot is enabled or not * -* This function implements the GetVariable runtime service. +* Return: true if enabled, false if disabled +*/ +bool efi_secure_boot_enabled (void ) +{ +return efi_secure_boot; +} + +#ifdef CONFIG_EFI_SECURE_BOOT +static u8 pkcs7_hdr[] = { +/* SEQUENCE */ +0x30 ,0x82 ,0x05 ,0xc7 , +/* OID: pkcs7-signedData */ +0x06 ,0x09 ,0x2a ,0x86 ,0x48 ,0x86 ,0xf7 ,0x0d ,0x01 ,0x07 ,0x02 , +/* Context Structured? */ +0xa0 ,0x82 ,0x05 ,0xb8 , +}; + +/** +* efi_variable_parse_signature - parse a signature in variable +* @buf: Pointer to variable's value +* @buflen: Length of @buf * -* See the Unified Extensible Firmware Interface (UEFI) specification for -* details. +* Parse a signature embedded in variable's value and instantiate +* a pkcs7_message structure. Since pkcs7_parse_message() accepts only +* pkcs7's signedData, some header needed be prepended for correctly +* parsing authentication data, particularly for variable's. * -* @variable_name: name of the variable -* @vendor: vendor GUID -* @attributes: attributes of the variable -* @data_size: size of the buffer to which the variable value is copied -* @data: buffer to which the variable value is copied -* Return: status code +* Return: Pointer to pkcs7_message structure on success, NULL on error */ -efi_status_t EFIAPI efi_get_variable(u16 *variable_name, -const efi_guid_t *vendor,u32 *attributes, -efi_uintn_t *data_size,void *data) +static struct pkcs7_message *efi_variable_parse_signature (const void *buf, +size_t buflen) +{ +u8 *ebuf; +size_t ebuflen, len; +struct pkcs7_message *msg; + +/* +* This is the best assumption to check if the binary is +* already in a form of pkcs7's signedData. +*/ +if (buflen >sizeof (pkcs7_hdr) && + !memcmp (&((u8 *)buf)[4 ], &pkcs7_hdr[4 ],11 )) { + msg =pkcs7_parse_message (buf, buflen); + goto out; + } + +/* +* Otherwise, we should add a dummy prefix sequence for pkcs7 +* message parser to be able to process. +* NOTE: EDK2 also uses similar hack in WrapPkcs7Data() +* in CryptoPkg/Library/BaseCryptLib/Pk/CryptPkcs7VerifyCommon.c +* TODO: +* The header should be composed in a more refined manner. +*/ +debug ("Makeshift prefix added to authentication data\n" ); + ebuflen =sizeof (pkcs7_hdr) + buflen; +if (ebuflen <=0x7f ) { +debug ("Data is too short\n" ); +return NULL ; + } + + ebuf =malloc (ebuflen); +if (!ebuf) { +debug ("Out of memory\n" ); +return NULL ; + } + +memcpy (ebuf, pkcs7_hdr,sizeof (pkcs7_hdr)); +memcpy (ebuf +sizeof (pkcs7_hdr), buf, buflen); + len = ebuflen -4 ; + ebuf[2 ] = (len >>8 ) &0xff ; + ebuf[3 ] = len &0xff ; + len = ebuflen -0x13 ; + ebuf[0x11 ] = (len >>8 ) &0xff ; + ebuf[0x12 ] = len &0xff ; + + msg =pkcs7_parse_message (ebuf, ebuflen); + +free (ebuf); + +out: +if (IS_ERR (msg)) +return NULL ; + +return msg; +} + +/** +* efi_variable_authenticate - authenticate a variable +* @variable: Variable name in u16 +* @vendor: Guid of variable +* @data_size: Size of @data +* @data: Pointer to variable's value +* @given_attr: Attributes to be given at SetVariable() +* @env_attr: Attributes that an existing variable holds +* @time: signed time that an existing variable holds +* +* Called by efi_set_variable() to verify that the input is correct. +* Will replace the given data pointer with another that points to +* the actual data to store in the internal memory. +* On success, @data and @data_size will be replaced with variable's +* actual data, excluding authentication data, and its size, and variable's +* attributes and signed time will also be returned in @env_attr and @time, +* respectively. +* +* Return: EFI_SUCCESS on success, status code (negative) on error +*/ +static efi_status_t efi_variable_authenticate (u16 *variable, +const efi_guid_t *vendor, +efi_uintn_t *data_size, +const void **data,u32 given_attr, +u32 *env_attr,u64 *time) +{ +const struct efi_variable_authentication_2 *auth; +struct efi_signature_store *truststore, *truststore2; +struct pkcs7_message *var_sig; +struct efi_image_regions *regs; +struct efi_time timestamp; +struct rtc_time tm; +u64 new_time; +efi_status_t ret; + + var_sig =NULL ; + truststore =NULL ; + truststore2 =NULL ; + regs =NULL ; + ret =EFI_SECURITY_VIOLATION ; + +if (*data_size <sizeof (struct efi_variable_authentication_2 )) + goto err; + +/* authentication data */ + auth = *data; +if (*data_size < (sizeof (auth->time_stamp) + + auth->auth_info.hdr.dwLength)) + goto err; + +if (guidcmp (&auth->auth_info.cert_type, &efi_guid_cert_type_pkcs7)) + goto err; + + *data +=sizeof (auth->time_stamp) + auth->auth_info.hdr.dwLength; + *data_size -= (sizeof (auth->time_stamp) + + auth->auth_info.hdr.dwLength); + +memcpy (×tamp, &auth->time_stamp,sizeof (timestamp)); +memset (&tm,0 ,sizeof (tm)); + tm.tm_year = timestamp.year; + tm.tm_mon = timestamp.month; + tm.tm_mday = timestamp.day; + tm.tm_hour = timestamp.hour; + tm.tm_min = timestamp.minute; + tm.tm_sec = timestamp.second; + new_time =rtc_mktime (&tm); + +if (!efi_secure_boot_enabled ()) { +/* finished checking */ + *time = new_time; +return EFI_SUCCESS ; + } + +if (new_time <= *time) + goto err; + +/* data to be digested */ + regs =calloc (sizeof (*regs) +sizeof (struct image_region ) *5 ,1 ); +if (!regs) + goto err; + regs->max =5 ; +efi_image_region_add (regs, (uint8_t *)variable, + (uint8_t *)variable + +u16_strlen (variable) *sizeof (u16),1 ); +efi_image_region_add (regs, (uint8_t *)vendor, + (uint8_t *)vendor +sizeof (*vendor),1 ); +efi_image_region_add (regs, (uint8_t *)&given_attr, + (uint8_t *)&given_attr +sizeof (given_attr),1 ); +efi_image_region_add (regs, (uint8_t *)×tamp, + (uint8_t *)×tamp +sizeof (timestamp),1 ); +efi_image_region_add (regs, (uint8_t *)*data, + (uint8_t *)*data + *data_size,1 ); + +/* variable's signature list */ +if (auth->auth_info.hdr.dwLength <sizeof (auth->auth_info)) + goto err; + var_sig =efi_variable_parse_signature (auth->auth_info.cert_data, + auth->auth_info.hdr.dwLength + -sizeof (auth->auth_info)); +if (IS_ERR (var_sig)) { +debug ("Parsing variable's signature failed\n" ); + var_sig =NULL ; + goto err; + } + +/* signature database used for authentication */ +if (u16_strcmp (variable,L"PK" ) ==0 || +u16_strcmp (variable,L"KEK" ) ==0 ) { +/* with PK */ + truststore =efi_sigstore_parse_sigdb (L"PK" ); +if (!truststore) + goto err; + }else if (u16_strcmp (variable,L"db" ) ==0 || +u16_strcmp (variable,L"dbx" ) ==0 ) { +/* with PK and KEK */ + truststore =efi_sigstore_parse_sigdb (L"KEK" ); + truststore2 =efi_sigstore_parse_sigdb (L"PK" ); + +if (!truststore) { +if (!truststore2) + goto err; + + truststore = truststore2; + truststore2 =NULL ; + } + }else { +/* TODO: support private authenticated variables */ + goto err; + } + +/* verify signature */ +if (efi_signature_verify_with_sigdb (regs, var_sig, truststore,NULL )) { +debug ("Verified\n" ); + }else { +if (truststore2 && +efi_signature_verify_with_sigdb (regs, var_sig, + truststore2,NULL )) { +debug ("Verified\n" ); + }else { +debug ("Verifying variable's signature failed\n" ); + goto err; + } + } + +/* finished checking */ + *time =rtc_mktime (&tm); + ret =EFI_SUCCESS ; + +err: +efi_sigstore_free (truststore); +efi_sigstore_free (truststore2); +pkcs7_free_message (var_sig); +free (regs); + +return ret; +} +#else +static efi_status_t efi_variable_authenticate (u16 *variable, +const efi_guid_t *vendor, +efi_uintn_t *data_size, +const void **data,u32 given_attr, +u32 *env_attr,u64 *time) +{ +return EFI_SUCCESS ; +} +#endif /* CONFIG_EFI_SECURE_BOOT */ + +static +efi_status_t EFIAPI efi_get_variable_common(u16 *variable_name, +const efi_guid_t *vendor, +u32 *attributes, +efi_uintn_t *data_size,void *data, +bool is_non_volatile) {char *native_name;efi_status_t ret;unsigned long in_size; -const char *val, *s; +const char *val =NULL , *s; +u64 time =0 ;u32 attr; -EFI_ENTRY ("\"%ls\" %pUl %p %p %p" , variable_name, vendor, attributes, - data_size, data); -if (!variable_name || !vendor || !data_size)return EFI_EXIT (EFI_INVALID_PARAMETER ); ret =efi_to_native (&native_name, variable_name, vendor);if (ret) -return EFI_EXIT (ret); +return ret;EFI_PRINT ("get '%s'\n" , native_name); val =env_get (native_name);free (native_name);if (!val) -return EFI_EXIT (EFI_NOT_FOUND ); +return EFI_NOT_FOUND ; - val =parse_attr (val, &attr); + val =parse_attr (val, &attr, &time); in_size = *data_size;@@ -198,7 +453,7 @@ efi_status_t EFIAPI efi_get_variable(u16 *variable_name,/* number of hexadecimal digits must be even */ if (len &1 ) -return EFI_EXIT (EFI_DEVICE_ERROR ); +return EFI_DEVICE_ERROR ;/* two characters per byte: */ len /=2 ;@@ -209,11 +464,13 @@ efi_status_t EFIAPI efi_get_variable(u16 *variable_name, goto out; } -if (!data) -return EFI_EXIT (EFI_INVALID_PARAMETER ); +if (!data) { +debug ("Variable with no data shouldn't exist.\n" ); +return EFI_INVALID_PARAMETER ; + }if (hex2bin (data, s, len)) -return EFI_EXIT (EFI_DEVICE_ERROR ); +return EFI_DEVICE_ERROR ;EFI_PRINT ("got value: \"%s\"\n" , s); }else if ((s =prefix (val,"(utf8)" ))) {@@ -226,8 +483,10 @@ efi_status_t EFIAPI efi_get_variable(u16 *variable_name, goto out; } -if (!data) -return EFI_EXIT (EFI_INVALID_PARAMETER ); +if (!data) { +debug ("Variable with no data shouldn't exist.\n" ); +return EFI_INVALID_PARAMETER ; + }memcpy (data, s, len); ((char *)data)[len] ='\0' ;@@ -235,13 +494,67 @@ efi_status_t EFIAPI efi_get_variable(u16 *variable_name,EFI_PRINT ("got value: \"%s\"\n" , (char *)data); }else {EFI_PRINT ("invalid value: '%s'\n" , val); -return EFI_EXIT (EFI_DEVICE_ERROR ); +return EFI_DEVICE_ERROR ; } out:if (attributes) *attributes = attr &EFI_VARIABLE_MASK ; +return ret; +} + +static +efi_status_t EFIAPI efi_get_volatile_variable(u16 *variable_name, +const efi_guid_t *vendor, +u32 *attributes, +efi_uintn_t *data_size, +void *data) +{ +return efi_get_variable_common (variable_name, vendor, attributes, + data_size, data, false); +} + +efi_status_t EFIAPI efi_get_nonvolatile_variable(u16 *variable_name, +const efi_guid_t *vendor, +u32 *attributes, +efi_uintn_t *data_size, +void *data) +{ +return efi_get_variable_common (variable_name, vendor, attributes, + data_size, data, true); +} + +/** +* efi_efi_get_variable() - retrieve value of a UEFI variable +* +* This function implements the GetVariable runtime service. +* +* See the Unified Extensible Firmware Interface (UEFI) specification for +* details. +* +* @variable_name: name of the variable +* @vendor: vendor GUID +* @attributes: attributes of the variable +* @data_size: size of the buffer to which the variable value is copied +* @data: buffer to which the variable value is copied +* Return: status code +*/ +efi_status_t EFIAPI efi_get_variable(u16 *variable_name, +const efi_guid_t *vendor,u32 *attributes, +efi_uintn_t *data_size,void *data) +{ +efi_status_t ret; + +EFI_ENTRY ("\"%ls\" %pUl %p %p %p" , variable_name, vendor, attributes, + data_size, data); + + ret =efi_get_volatile_variable (variable_name, vendor, attributes, + data_size, data); +if (ret ==EFI_NOT_FOUND ) + ret =efi_get_nonvolatile_variable (variable_name, vendor, + attributes, data_size, data); +return EFI_EXIT (ret); }@@ -274,6 +587,7 @@ static efi_status_t parse_uboot_variable(char *variable, {char *guid, *name, *end, c;unsigned long name_len; +u64 time;u16 *p; guid =strchr (variable,'_' );@@ -308,7 +622,7 @@ static efi_status_t parse_uboot_variable(char *variable, *(name -1 ) = c;/* attributes */ -parse_attr (end, attributes); +parse_attr (end, attributes, &time);return EFI_SUCCESS ; }@@ -390,7 +704,7 @@ efi_status_t EFIAPI efi_get_next_variable_name(efi_uintn_t *variable_name_size, list_len =hexport_r (&env_htab,'\n' ,H_MATCH_REGEX |H_MATCH_KEY , &efi_variables_list,0 ,1 , regexlist); -/* 1 indicates that no match was found */ +if (list_len <=1 )return EFI_EXIT (EFI_NOT_FOUND );@@ -403,143 +717,286 @@ efi_status_t EFIAPI efi_get_next_variable_name(efi_uintn_t *variable_name_size,return EFI_EXIT (ret); } -/** -* efi_set_variable() - set value of a UEFI variable -* -* This function implements the SetVariable runtime service. -* -* See the Unified Extensible Firmware Interface (UEFI) specification for -* details. -* -* @variable_name: name of the variable -* @vendor: vendor GUID -* @attributes: attributes of the variable -* @data_size: size of the buffer with the variable value -* @data: buffer with the variable value -* Return: status code -*/ -efi_status_t EFIAPI efi_set_variable(u16 *variable_name, -const efi_guid_t *vendor,u32 attributes, -efi_uintn_t data_size,const void *data) +static +efi_status_t EFIAPI efi_set_variable_common(u16 *variable_name, +const efi_guid_t *vendor, +u32 attributes, +efi_uintn_t data_size, +const void *data, +bool ro_check, +bool is_non_volatile) { -char *native_name =NULL , *val =NULL , *s; -const char *old_val; -size_t old_size; -efi_status_t ret =EFI_SUCCESS ; +char *native_name =NULL , *old_data =NULL , *val =NULL , *s; +efi_uintn_t old_size; +bool append, delete; +u64 time =0 ;u32 attr; +efi_status_t ret =EFI_SUCCESS ; -EFI_ENTRY ("\"%ls\" %pUl %x %zu %p" , variable_name, vendor, attributes, - data_size, data); +debug ("%s: set '%s'\n" , __func__, native_name);if (!variable_name || !*variable_name || !vendor || ((attributes &EFI_VARIABLE_RUNTIME_ACCESS ) && !(attributes &EFI_VARIABLE_BOOTSERVICE_ACCESS ))) { ret =EFI_INVALID_PARAMETER ; - goto out; + goto err; } ret =efi_to_native (&native_name, variable_name, vendor);if (ret) - goto out; + goto err; + +/* check if a variable exists */ + old_size =0 ; + attr =0 ; + ret =EFI_CALL (efi_get_variable (variable_name, vendor, &attr, + &old_size,NULL )); +if (ret ==EFI_BUFFER_TOO_SMALL ) { +if ((is_non_volatile && !(attr &EFI_VARIABLE_NON_VOLATILE )) || + (!is_non_volatile && (attr &EFI_VARIABLE_NON_VOLATILE ))) { + ret =EFI_INVALID_PARAMETER ; + goto err; + } + } - old_val =env_get (native_name); -if (old_val) { - old_val =parse_attr (old_val, &attr); + append = !!(attributes &EFI_VARIABLE_APPEND_WRITE ); + attributes &= ~(u32 )EFI_VARIABLE_APPEND_WRITE ; + delete = !append && (!data_size || !attributes); -/* check read-only first */ -if (attr &READ_ONLY ) { +/* check attributes */ +if (old_size) { +if (ro_check && (attr &READ_ONLY )) { ret =EFI_WRITE_PROTECTED ; - goto out; - } - -if ((data_size ==0 && - !(attributes &EFI_VARIABLE_APPEND_WRITE )) || - !attributes) { -/* delete the variable: */ -env_set (native_name,NULL ); - ret =EFI_SUCCESS ; - goto out; + goto err; }/* attributes won't be changed */ -if (attr != (attributes & ~EFI_VARIABLE_APPEND_WRITE )) { +if (!delete && + ((ro_check && attr != attributes) || + (!ro_check && ((attr & ~(u32 )READ_ONLY ) + != (attributes & ~(u32 )READ_ONLY ))))) { ret =EFI_INVALID_PARAMETER ; - goto out; - } - -if (attributes &EFI_VARIABLE_APPEND_WRITE ) { -if (!prefix (old_val,"(blob)" )) { - ret =EFI_DEVICE_ERROR ; - goto out; - } - old_size =strlen (old_val); - }else { - old_size =0 ; + goto err; } }else { -if (data_size ==0 || !attributes || - (attributes &EFI_VARIABLE_APPEND_WRITE )) { +if (delete || append) {/* * Trying to delete or to update a non-existent * variable. */ ret =EFI_NOT_FOUND ; - goto out; + goto err; + } + } + +if (((!u16_strcmp (variable_name,L"PK" ) || + !u16_strcmp (variable_name,L"KEK" )) && + !guidcmp (vendor, &efi_global_variable_guid)) || + ((!u16_strcmp (variable_name,L"db" ) || + !u16_strcmp (variable_name,L"dbx" )) && + !guidcmp (vendor, &efi_guid_image_security_database))) { +/* authentication is mandatory */ +if (!(attributes & +EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS )) { +debug ("%ls: AUTHENTICATED_WRITE_ACCESS required\n" , + variable_name); + ret =EFI_INVALID_PARAMETER ; + goto err; } + } + +/* authenticate a variable */ +if (IS_ENABLED (CONFIG_EFI_SECURE_BOOT )) { +if (attributes &EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS ) { + ret =EFI_INVALID_PARAMETER ; + goto err; + } +if (attributes & +EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS ) { + ret =efi_variable_authenticate (variable_name, vendor, + &data_size, &data, + attributes, &attr, + &time); +if (ret !=EFI_SUCCESS ) + goto err; + +/* last chance to check for delete */ +if (!data_size) + delete = true; + } + }else { +if (attributes & + (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS | +EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS )) { +debug ("Secure boot is not configured\n" ); + ret =EFI_INVALID_PARAMETER ; + goto err; + } + } + +/* delete a variable */ +if (delete) { +/* !old_size case has been handled before */ + val =NULL ; + ret =EFI_SUCCESS ; + goto out; + } +if (append) { + old_data =malloc (old_size); +if (!old_data) { +return EFI_OUT_OF_RESOURCES ; + goto err; + } + ret =EFI_CALL (efi_get_variable (variable_name, vendor, + &attr, &old_size, old_data)); +if (ret !=EFI_SUCCESS ) + goto err; + }else { old_size =0 ; } - val =malloc (old_size +2 * data_size - +strlen ("{ro,run,boot,nv}(blob)" ) +1 ); + val =malloc (2 * old_size +2 * data_size + +strlen ("{ro,run,boot,nv,time=0123456701234567}(blob)" ) + +1 );if (!val) { ret =EFI_OUT_OF_RESOURCES ; - goto out; + goto err; } s = val; -/* store attributes */ - attributes &= (EFI_VARIABLE_NON_VOLATILE | +/* +* store attributes +*/ + attributes &= (READ_ONLY | +EFI_VARIABLE_NON_VOLATILE |EFI_VARIABLE_BOOTSERVICE_ACCESS | -EFI_VARIABLE_RUNTIME_ACCESS ); +EFI_VARIABLE_RUNTIME_ACCESS | +EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS ); s +=sprintf (s,"{" );while (attributes) { -u32 attr =1 << (ffs (attributes) -1 ); + attr =1 << (ffs (attributes) -1 ); -if (attr ==EFI_VARIABLE_NON_VOLATILE ) +if (attr ==READ_ONLY ) { + s +=sprintf (s,"ro" ); + }else if (attr ==EFI_VARIABLE_NON_VOLATILE ) { s +=sprintf (s,"nv" ); -else if (attr ==EFI_VARIABLE_BOOTSERVICE_ACCESS ) + }else if (attr ==EFI_VARIABLE_BOOTSERVICE_ACCESS ) { s +=sprintf (s,"boot" ); -else if (attr ==EFI_VARIABLE_RUNTIME_ACCESS ) + }else if (attr ==EFI_VARIABLE_RUNTIME_ACCESS ) { s +=sprintf (s,"run" ); + }else if (attr == +EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS ) { + s +=sprintf (s,"time=" ); + s =bin2hex (s, (u8 *)&time,sizeof (time)); + } attributes &= ~attr;if (attributes) s +=sprintf (s,"," ); } s +=sprintf (s,"}" ); - -if (old_size) -/* APPEND_WRITE */ - s +=sprintf (s, old_val); -else - s +=sprintf (s,"(blob)" ); + s +=sprintf (s,"(blob)" );/* store payload: */ +if (append) + s =bin2hex (s, old_data, old_size); s =bin2hex (s, data, data_size); *s ='\0' ;EFI_PRINT ("setting: %s=%s\n" , native_name, val); +out:if (env_set (native_name, val)) ret =EFI_DEVICE_ERROR ; +else + ret =EFI_SUCCESS ; -out: +err:free (native_name); +free (old_data);free (val);
Quoted 14 lines
lib/efi_loader/efi_variable.c efi_status_t EFIAPI efi_get_next_variable_name(efi_uintn_t *variable_name_size, · line 930
Quoted 11 lines
lib/efi_loader/efi_variable.c +10
-1-return EFI_EXIT (ret); +return ret; +} + +static +efi_status_t EFIAPI efi_set_volatile_variable(u16 *variable_name, +const efi_guid_t *vendor, +u32 attributes, +efi_uintn_t data_size, +const void *data, +bool ro_check)Why would we need such a function which is only used once? Better simply call
efi_set_variable_common()with the correct value ofro_check.
This function, as well as _nonvolatile version, does exist here so that we will be able to take a different approach of implementing "secure storage."
As volatile variables don't need any backend storage, we may want to use the same function for volatile variables and yet implement platform-specific non-volatile variables for some reason. This will give us more flexibility, and yet we have optimal performance with compiler optimization for the current code.
Thanks,
-Takahiro Akashi
Quoted 92 lines
lib/efi_loader/efi_variable.c efi_status_t EFIAPI efi_get_next_variable_name(efi_uintn_t *variable_name_size, · line 941
Quoted 11 lines
lib/efi_loader/efi_variable.c +11
-0+{ +return efi_set_variable_common (variable_name, vendor, attributes, + data_size, data, ro_check, false); +} + +efi_status_t EFIAPI efi_set_nonvolatile_variable(u16 *variable_name, +const efi_guid_t *vendor, +u32 attributes, +efi_uintn_t data_size, +const void *data, +bool ro_check)Same here.
Quoted 5 lines
lib/efi_loader/efi_variable.c +5
-0+{ +efi_status_t ret; + + ret =efi_set_variable_common (variable_name, vendor, attributes, + data_size, data, ro_check, true);lib/efi_loader/efi_variable.c efi_status_t EFIAPI efi_get_next_variable_name(efi_uintn_t *variable_name_size, · line 956
Quoted 10 lines
lib/efi_loader/efi_variable.c +10
-0+ +return ret; +} + +static efi_status_t efi_set_variable_internal (u16 *variable_name, +const efi_guid_t *vendor, +u32 attributes, +efi_uintn_t data_size, +const void *data, +bool ro_check)I think this function should be exported. This will allow to avoid
EFI_CALLinefi_bootmgr.candefi_setup.c.lib/efi_loader/efi_variable.c efi_status_t EFIAPI efi_get_next_variable_name(efi_uintn_t *variable_name_size, · line 958
Quoted 2 lines
lib/efi_loader/efi_variable.c +2
-0+{ +efi_status_t ret;Your could simply write:
ret = efi_set_variable_common(variable_name, vendor, attributes,
data_size, data, ro_check, !!(attributes & EFI_VARIABLE_NON_VOLATILE));Best regards
Heinrich
Quoted 45 lines
lib/efi_loader/efi_variable.c +41
-0+ +if (attributes &EFI_VARIABLE_NON_VOLATILE ) + ret =efi_set_nonvolatile_variable (variable_name, vendor, + attributes, + data_size, data, ro_check); +else + ret =efi_set_volatile_variable (variable_name, vendor, + attributes, data_size, data, + ro_check); + +return ret; +} + +/** +* efi_set_variable() - set value of a UEFI variable +* +* This function implements the SetVariable runtime service. +* +* See the Unified Extensible Firmware Interface (UEFI) specification for +* details. +* +* @variable_name: name of the variable +* @vendor: vendor GUID +* @attributes: attributes of the variable +* @data_size: size of the buffer with the variable value +* @data: buffer with the variable value +* Return: status code +*/ +efi_status_t EFIAPI efi_set_variable(u16 *variable_name, +const efi_guid_t *vendor,u32 attributes, +efi_uintn_t data_size,const void *data) +{ +EFI_ENTRY ("\"%ls\" %pUl %x %zu %p" , variable_name, vendor, attributes, + data_size, data); + +/* READ_ONLY bit is not part of API */ + attributes &= ~(u32 )READ_ONLY ; + +return EFI_EXIT (efi_set_variable_internal (variable_name, vendor, + attributes, data_size, data, + true)); }/**
PATCH v5 4/16 efi_loader: variable: support variable authentication
In reply to AKASHI TakahiroQuoted 60 lines · AKASHI Takahiro
With this commit,
EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESSis supported for authenticated variables and the system secure state will transfer between setup mode and user mode as UEFI specification section 32.3 describes.Internally, authentication data is stored as part of authenticated variable's value. It is nothing but a pkcs7 message (but we need some wrapper, see
efi_variable_parse_signature()) and will be validated byefi_variable_authenticate(), henceefi_signature_verify_with_db().Associated time value will be encoded in "{...,time=...}" along with other UEFI variable's attributes.
Signed-off-by AKASHI Takahiro <[email protected]>--- include/efi_loader.h | 3 + lib/efi_loader/efi_variable.c | 665 ++++++++++++++++++++++++++++------ 2 files changed, 564 insertions(+), 104 deletions(-)include/efi_loader.h +3
-0@@ -184,6 +184,7 @@ extern const efi_guid_t efi_guid_image_security_database;extern const efi_guid_t efi_guid_sha256;extern const efi_guid_t efi_guid_cert_x509;extern const efi_guid_t efi_guid_cert_x509_sha256; +extern const efi_guid_t efi_guid_cert_type_pkcs7;/* GUID of RNG protocol */ extern const efi_guid_t efi_guid_rng_protocol;@@ -738,6 +739,8 @@ efi_status_t efi_image_region_add(struct efi_image_regions *regs,void efi_sigstore_free (struct efi_signature_store *sigstore);struct efi_signature_store *efi_sigstore_parse_sigdb (u16 *name); + +bool efi_secure_boot_enabled (void );#endif /* CONFIG_EFI_SECURE_BOOT */ #else /* CONFIG_IS_ENABLED(EFI_LOADER) */ lib/efi_loader/efi_variable.c +6
-0@@ -10,8 +10,14 @@#include <env_internal.h> #include <hexdump.h> #include <malloc.h> +#include <rtc.h> #include <search.h> +#include <linux/compat.h> #include <u-boot/crc.h> +#include "../lib/crypto/pkcs7_parser.h" + +const efi_guid_t efi_guid_cert_type_pkcs7 =EFI_CERT_TYPE_PKCS7_GUID ; +static bool efi_secure_boot;#define READ_ONLY BIT(31)
lib/efi_loader/efi_variable.c static const char *prefix(const char *str, const char *prefix) · line 117
Quoted 5 lines
lib/efi_loader/efi_variable.c +1
-1@@ -108,7 +114,7 @@ static const char *prefix(const char *str, const char *prefix)* @attrp: pointer to UEFI attributes * Return: pointer to remainder of U-Boot variable value */ -static const char *parse_attr (const char *str,u32 *attrp) +static const char *parse_attr (const char *str,u32 *attrp,u64 *timep)
./lib/efi_loader/efi_variable.c:128: warning: Function parameter or member 'timep' not described in 'parse_attr'
All 'make htmldocs' warnings will be treated as errors after upcoming Travis CI changes.
Best regards
Heinrich
Quoted 824 lines
lib/efi_loader/efi_variable.c +554
-103{u32 attr =0 ;char sep ='{' ;@@ -131,6 +137,12 @@ static const char *parse_attr(const char *str, u32 *attrp) attr |=EFI_VARIABLE_BOOTSERVICE_ACCESS ; }else if ((s =prefix (str,"run" ))) { attr |=EFI_VARIABLE_RUNTIME_ACCESS ; + }else if ((s =prefix (str,"time=" ))) { + attr |=EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS ; +hex2bin ((u8 *)timep, s,sizeof (*timep)); + s +=sizeof (*timep) *2 ; + }else if (*str =='}' ) { +break ; }else {printf ("invalid attribute: %s\n" , str);break ;@@ -148,48 +160,291 @@ static const char *parse_attr(const char *str, u32 *attrp) }/** -* efi_get_variable() - retrieve value of a UEFI variable +* efi_secure_boot_enabled - return if secure boot is enabled or not * -* This function implements the GetVariable runtime service. +* Return: true if enabled, false if disabled +*/ +bool efi_secure_boot_enabled (void ) +{ +return efi_secure_boot; +} + +#ifdef CONFIG_EFI_SECURE_BOOT +static u8 pkcs7_hdr[] = { +/* SEQUENCE */ +0x30 ,0x82 ,0x05 ,0xc7 , +/* OID: pkcs7-signedData */ +0x06 ,0x09 ,0x2a ,0x86 ,0x48 ,0x86 ,0xf7 ,0x0d ,0x01 ,0x07 ,0x02 , +/* Context Structured? */ +0xa0 ,0x82 ,0x05 ,0xb8 , +}; + +/** +* efi_variable_parse_signature - parse a signature in variable +* @buf: Pointer to variable's value +* @buflen: Length of @buf * -* See the Unified Extensible Firmware Interface (UEFI) specification for -* details. +* Parse a signature embedded in variable's value and instantiate +* a pkcs7_message structure. Since pkcs7_parse_message() accepts only +* pkcs7's signedData, some header needed be prepended for correctly +* parsing authentication data, particularly for variable's. * -* @variable_name: name of the variable -* @vendor: vendor GUID -* @attributes: attributes of the variable -* @data_size: size of the buffer to which the variable value is copied -* @data: buffer to which the variable value is copied -* Return: status code +* Return: Pointer to pkcs7_message structure on success, NULL on error */ -efi_status_t EFIAPI efi_get_variable(u16 *variable_name, -const efi_guid_t *vendor,u32 *attributes, -efi_uintn_t *data_size,void *data) +static struct pkcs7_message *efi_variable_parse_signature (const void *buf, +size_t buflen) +{ +u8 *ebuf; +size_t ebuflen, len; +struct pkcs7_message *msg; + +/* +* This is the best assumption to check if the binary is +* already in a form of pkcs7's signedData. +*/ +if (buflen >sizeof (pkcs7_hdr) && + !memcmp (&((u8 *)buf)[4 ], &pkcs7_hdr[4 ],11 )) { + msg =pkcs7_parse_message (buf, buflen); + goto out; + } + +/* +* Otherwise, we should add a dummy prefix sequence for pkcs7 +* message parser to be able to process. +* NOTE: EDK2 also uses similar hack in WrapPkcs7Data() +* in CryptoPkg/Library/BaseCryptLib/Pk/CryptPkcs7VerifyCommon.c +* TODO: +* The header should be composed in a more refined manner. +*/ +debug ("Makeshift prefix added to authentication data\n" ); + ebuflen =sizeof (pkcs7_hdr) + buflen; +if (ebuflen <=0x7f ) { +debug ("Data is too short\n" ); +return NULL ; + } + + ebuf =malloc (ebuflen); +if (!ebuf) { +debug ("Out of memory\n" ); +return NULL ; + } + +memcpy (ebuf, pkcs7_hdr,sizeof (pkcs7_hdr)); +memcpy (ebuf +sizeof (pkcs7_hdr), buf, buflen); + len = ebuflen -4 ; + ebuf[2 ] = (len >>8 ) &0xff ; + ebuf[3 ] = len &0xff ; + len = ebuflen -0x13 ; + ebuf[0x11 ] = (len >>8 ) &0xff ; + ebuf[0x12 ] = len &0xff ; + + msg =pkcs7_parse_message (ebuf, ebuflen); + +free (ebuf); + +out: +if (IS_ERR (msg)) +return NULL ; + +return msg; +} + +/** +* efi_variable_authenticate - authenticate a variable +* @variable: Variable name in u16 +* @vendor: Guid of variable +* @data_size: Size of @data +* @data: Pointer to variable's value +* @given_attr: Attributes to be given at SetVariable() +* @env_attr: Attributes that an existing variable holds +* @time: signed time that an existing variable holds +* +* Called by efi_set_variable() to verify that the input is correct. +* Will replace the given data pointer with another that points to +* the actual data to store in the internal memory. +* On success, @data and @data_size will be replaced with variable's +* actual data, excluding authentication data, and its size, and variable's +* attributes and signed time will also be returned in @env_attr and @time, +* respectively. +* +* Return: EFI_SUCCESS on success, status code (negative) on error +*/ +static efi_status_t efi_variable_authenticate (u16 *variable, +const efi_guid_t *vendor, +efi_uintn_t *data_size, +const void **data,u32 given_attr, +u32 *env_attr,u64 *time) +{ +const struct efi_variable_authentication_2 *auth; +struct efi_signature_store *truststore, *truststore2; +struct pkcs7_message *var_sig; +struct efi_image_regions *regs; +struct efi_time timestamp; +struct rtc_time tm; +u64 new_time; +efi_status_t ret; + + var_sig =NULL ; + truststore =NULL ; + truststore2 =NULL ; + regs =NULL ; + ret =EFI_SECURITY_VIOLATION ; + +if (*data_size <sizeof (struct efi_variable_authentication_2 )) + goto err; + +/* authentication data */ + auth = *data; +if (*data_size < (sizeof (auth->time_stamp) + + auth->auth_info.hdr.dwLength)) + goto err; + +if (guidcmp (&auth->auth_info.cert_type, &efi_guid_cert_type_pkcs7)) + goto err; + + *data +=sizeof (auth->time_stamp) + auth->auth_info.hdr.dwLength; + *data_size -= (sizeof (auth->time_stamp) + + auth->auth_info.hdr.dwLength); + +memcpy (×tamp, &auth->time_stamp,sizeof (timestamp)); +memset (&tm,0 ,sizeof (tm)); + tm.tm_year = timestamp.year; + tm.tm_mon = timestamp.month; + tm.tm_mday = timestamp.day; + tm.tm_hour = timestamp.hour; + tm.tm_min = timestamp.minute; + tm.tm_sec = timestamp.second; + new_time =rtc_mktime (&tm); + +if (!efi_secure_boot_enabled ()) { +/* finished checking */ + *time = new_time; +return EFI_SUCCESS ; + } + +if (new_time <= *time) + goto err; + +/* data to be digested */ + regs =calloc (sizeof (*regs) +sizeof (struct image_region ) *5 ,1 ); +if (!regs) + goto err; + regs->max =5 ; +efi_image_region_add (regs, (uint8_t *)variable, + (uint8_t *)variable + +u16_strlen (variable) *sizeof (u16),1 ); +efi_image_region_add (regs, (uint8_t *)vendor, + (uint8_t *)vendor +sizeof (*vendor),1 ); +efi_image_region_add (regs, (uint8_t *)&given_attr, + (uint8_t *)&given_attr +sizeof (given_attr),1 ); +efi_image_region_add (regs, (uint8_t *)×tamp, + (uint8_t *)×tamp +sizeof (timestamp),1 ); +efi_image_region_add (regs, (uint8_t *)*data, + (uint8_t *)*data + *data_size,1 ); + +/* variable's signature list */ +if (auth->auth_info.hdr.dwLength <sizeof (auth->auth_info)) + goto err; + var_sig =efi_variable_parse_signature (auth->auth_info.cert_data, + auth->auth_info.hdr.dwLength + -sizeof (auth->auth_info)); +if (IS_ERR (var_sig)) { +debug ("Parsing variable's signature failed\n" ); + var_sig =NULL ; + goto err; + } + +/* signature database used for authentication */ +if (u16_strcmp (variable,L"PK" ) ==0 || +u16_strcmp (variable,L"KEK" ) ==0 ) { +/* with PK */ + truststore =efi_sigstore_parse_sigdb (L"PK" ); +if (!truststore) + goto err; + }else if (u16_strcmp (variable,L"db" ) ==0 || +u16_strcmp (variable,L"dbx" ) ==0 ) { +/* with PK and KEK */ + truststore =efi_sigstore_parse_sigdb (L"KEK" ); + truststore2 =efi_sigstore_parse_sigdb (L"PK" ); + +if (!truststore) { +if (!truststore2) + goto err; + + truststore = truststore2; + truststore2 =NULL ; + } + }else { +/* TODO: support private authenticated variables */ + goto err; + } + +/* verify signature */ +if (efi_signature_verify_with_sigdb (regs, var_sig, truststore,NULL )) { +debug ("Verified\n" ); + }else { +if (truststore2 && +efi_signature_verify_with_sigdb (regs, var_sig, + truststore2,NULL )) { +debug ("Verified\n" ); + }else { +debug ("Verifying variable's signature failed\n" ); + goto err; + } + } + +/* finished checking */ + *time =rtc_mktime (&tm); + ret =EFI_SUCCESS ; + +err: +efi_sigstore_free (truststore); +efi_sigstore_free (truststore2); +pkcs7_free_message (var_sig); +free (regs); + +return ret; +} +#else +static efi_status_t efi_variable_authenticate (u16 *variable, +const efi_guid_t *vendor, +efi_uintn_t *data_size, +const void **data,u32 given_attr, +u32 *env_attr,u64 *time) +{ +return EFI_SUCCESS ; +} +#endif /* CONFIG_EFI_SECURE_BOOT */ + +static +efi_status_t EFIAPI efi_get_variable_common(u16 *variable_name, +const efi_guid_t *vendor, +u32 *attributes, +efi_uintn_t *data_size,void *data, +bool is_non_volatile) {char *native_name;efi_status_t ret;unsigned long in_size; -const char *val, *s; +const char *val =NULL , *s; +u64 time =0 ;u32 attr; -EFI_ENTRY ("\"%ls\" %pUl %p %p %p" , variable_name, vendor, attributes, - data_size, data); -if (!variable_name || !vendor || !data_size)return EFI_EXIT (EFI_INVALID_PARAMETER ); ret =efi_to_native (&native_name, variable_name, vendor);if (ret) -return EFI_EXIT (ret); +return ret;EFI_PRINT ("get '%s'\n" , native_name); val =env_get (native_name);free (native_name);if (!val) -return EFI_EXIT (EFI_NOT_FOUND ); +return EFI_NOT_FOUND ; - val =parse_attr (val, &attr); + val =parse_attr (val, &attr, &time); in_size = *data_size;@@ -198,7 +453,7 @@ efi_status_t EFIAPI efi_get_variable(u16 *variable_name,/* number of hexadecimal digits must be even */ if (len &1 ) -return EFI_EXIT (EFI_DEVICE_ERROR ); +return EFI_DEVICE_ERROR ;/* two characters per byte: */ len /=2 ;@@ -209,11 +464,13 @@ efi_status_t EFIAPI efi_get_variable(u16 *variable_name, goto out; } -if (!data) -return EFI_EXIT (EFI_INVALID_PARAMETER ); +if (!data) { +debug ("Variable with no data shouldn't exist.\n" ); +return EFI_INVALID_PARAMETER ; + }if (hex2bin (data, s, len)) -return EFI_EXIT (EFI_DEVICE_ERROR ); +return EFI_DEVICE_ERROR ;EFI_PRINT ("got value: \"%s\"\n" , s); }else if ((s =prefix (val,"(utf8)" ))) {@@ -226,8 +483,10 @@ efi_status_t EFIAPI efi_get_variable(u16 *variable_name, goto out; } -if (!data) -return EFI_EXIT (EFI_INVALID_PARAMETER ); +if (!data) { +debug ("Variable with no data shouldn't exist.\n" ); +return EFI_INVALID_PARAMETER ; + }memcpy (data, s, len); ((char *)data)[len] ='\0' ;@@ -235,13 +494,67 @@ efi_status_t EFIAPI efi_get_variable(u16 *variable_name,EFI_PRINT ("got value: \"%s\"\n" , (char *)data); }else {EFI_PRINT ("invalid value: '%s'\n" , val); -return EFI_EXIT (EFI_DEVICE_ERROR ); +return EFI_DEVICE_ERROR ; } out:if (attributes) *attributes = attr &EFI_VARIABLE_MASK ; +return ret; +} + +static +efi_status_t EFIAPI efi_get_volatile_variable(u16 *variable_name, +const efi_guid_t *vendor, +u32 *attributes, +efi_uintn_t *data_size, +void *data) +{ +return efi_get_variable_common (variable_name, vendor, attributes, + data_size, data, false); +} + +efi_status_t EFIAPI efi_get_nonvolatile_variable(u16 *variable_name, +const efi_guid_t *vendor, +u32 *attributes, +efi_uintn_t *data_size, +void *data) +{ +return efi_get_variable_common (variable_name, vendor, attributes, + data_size, data, true); +} + +/** +* efi_efi_get_variable() - retrieve value of a UEFI variable +* +* This function implements the GetVariable runtime service. +* +* See the Unified Extensible Firmware Interface (UEFI) specification for +* details. +* +* @variable_name: name of the variable +* @vendor: vendor GUID +* @attributes: attributes of the variable +* @data_size: size of the buffer to which the variable value is copied +* @data: buffer to which the variable value is copied +* Return: status code +*/ +efi_status_t EFIAPI efi_get_variable(u16 *variable_name, +const efi_guid_t *vendor,u32 *attributes, +efi_uintn_t *data_size,void *data) +{ +efi_status_t ret; + +EFI_ENTRY ("\"%ls\" %pUl %p %p %p" , variable_name, vendor, attributes, + data_size, data); + + ret =efi_get_volatile_variable (variable_name, vendor, attributes, + data_size, data); +if (ret ==EFI_NOT_FOUND ) + ret =efi_get_nonvolatile_variable (variable_name, vendor, + attributes, data_size, data); +return EFI_EXIT (ret); }@@ -274,6 +587,7 @@ static efi_status_t parse_uboot_variable(char *variable, {char *guid, *name, *end, c;unsigned long name_len; +u64 time;u16 *p; guid =strchr (variable,'_' );@@ -308,7 +622,7 @@ static efi_status_t parse_uboot_variable(char *variable, *(name -1 ) = c;/* attributes */ -parse_attr (end, attributes); +parse_attr (end, attributes, &time);return EFI_SUCCESS ; }@@ -390,7 +704,7 @@ efi_status_t EFIAPI efi_get_next_variable_name(efi_uintn_t *variable_name_size, list_len =hexport_r (&env_htab,'\n' ,H_MATCH_REGEX |H_MATCH_KEY , &efi_variables_list,0 ,1 , regexlist); -/* 1 indicates that no match was found */ +if (list_len <=1 )return EFI_EXIT (EFI_NOT_FOUND );@@ -403,143 +717,286 @@ efi_status_t EFIAPI efi_get_next_variable_name(efi_uintn_t *variable_name_size,return EFI_EXIT (ret); } -/** -* efi_set_variable() - set value of a UEFI variable -* -* This function implements the SetVariable runtime service. -* -* See the Unified Extensible Firmware Interface (UEFI) specification for -* details. -* -* @variable_name: name of the variable -* @vendor: vendor GUID -* @attributes: attributes of the variable -* @data_size: size of the buffer with the variable value -* @data: buffer with the variable value -* Return: status code -*/ -efi_status_t EFIAPI efi_set_variable(u16 *variable_name, -const efi_guid_t *vendor,u32 attributes, -efi_uintn_t data_size,const void *data) +static +efi_status_t EFIAPI efi_set_variable_common(u16 *variable_name, +const efi_guid_t *vendor, +u32 attributes, +efi_uintn_t data_size, +const void *data, +bool ro_check, +bool is_non_volatile) { -char *native_name =NULL , *val =NULL , *s; -const char *old_val; -size_t old_size; -efi_status_t ret =EFI_SUCCESS ; +char *native_name =NULL , *old_data =NULL , *val =NULL , *s; +efi_uintn_t old_size; +bool append, delete; +u64 time =0 ;u32 attr; +efi_status_t ret =EFI_SUCCESS ; -EFI_ENTRY ("\"%ls\" %pUl %x %zu %p" , variable_name, vendor, attributes, - data_size, data); +debug ("%s: set '%s'\n" , __func__, native_name);if (!variable_name || !*variable_name || !vendor || ((attributes &EFI_VARIABLE_RUNTIME_ACCESS ) && !(attributes &EFI_VARIABLE_BOOTSERVICE_ACCESS ))) { ret =EFI_INVALID_PARAMETER ; - goto out; + goto err; } ret =efi_to_native (&native_name, variable_name, vendor);if (ret) - goto out; + goto err; + +/* check if a variable exists */ + old_size =0 ; + attr =0 ; + ret =EFI_CALL (efi_get_variable (variable_name, vendor, &attr, + &old_size,NULL )); +if (ret ==EFI_BUFFER_TOO_SMALL ) { +if ((is_non_volatile && !(attr &EFI_VARIABLE_NON_VOLATILE )) || + (!is_non_volatile && (attr &EFI_VARIABLE_NON_VOLATILE ))) { + ret =EFI_INVALID_PARAMETER ; + goto err; + } + } - old_val =env_get (native_name); -if (old_val) { - old_val =parse_attr (old_val, &attr); + append = !!(attributes &EFI_VARIABLE_APPEND_WRITE ); + attributes &= ~(u32 )EFI_VARIABLE_APPEND_WRITE ; + delete = !append && (!data_size || !attributes); -/* check read-only first */ -if (attr &READ_ONLY ) { +/* check attributes */ +if (old_size) { +if (ro_check && (attr &READ_ONLY )) { ret =EFI_WRITE_PROTECTED ; - goto out; - } - -if ((data_size ==0 && - !(attributes &EFI_VARIABLE_APPEND_WRITE )) || - !attributes) { -/* delete the variable: */ -env_set (native_name,NULL ); - ret =EFI_SUCCESS ; - goto out; + goto err; }/* attributes won't be changed */ -if (attr != (attributes & ~EFI_VARIABLE_APPEND_WRITE )) { +if (!delete && + ((ro_check && attr != attributes) || + (!ro_check && ((attr & ~(u32 )READ_ONLY ) + != (attributes & ~(u32 )READ_ONLY ))))) { ret =EFI_INVALID_PARAMETER ; - goto out; - } - -if (attributes &EFI_VARIABLE_APPEND_WRITE ) { -if (!prefix (old_val,"(blob)" )) { - ret =EFI_DEVICE_ERROR ; - goto out; - } - old_size =strlen (old_val); - }else { - old_size =0 ; + goto err; } }else { -if (data_size ==0 || !attributes || - (attributes &EFI_VARIABLE_APPEND_WRITE )) { +if (delete || append) {/* * Trying to delete or to update a non-existent * variable. */ ret =EFI_NOT_FOUND ; - goto out; + goto err; + } + } + +if (((!u16_strcmp (variable_name,L"PK" ) || + !u16_strcmp (variable_name,L"KEK" )) && + !guidcmp (vendor, &efi_global_variable_guid)) || + ((!u16_strcmp (variable_name,L"db" ) || + !u16_strcmp (variable_name,L"dbx" )) && + !guidcmp (vendor, &efi_guid_image_security_database))) { +/* authentication is mandatory */ +if (!(attributes & +EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS )) { +debug ("%ls: AUTHENTICATED_WRITE_ACCESS required\n" , + variable_name); + ret =EFI_INVALID_PARAMETER ; + goto err; } + } + +/* authenticate a variable */ +if (IS_ENABLED (CONFIG_EFI_SECURE_BOOT )) { +if (attributes &EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS ) { + ret =EFI_INVALID_PARAMETER ; + goto err; + } +if (attributes & +EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS ) { + ret =efi_variable_authenticate (variable_name, vendor, + &data_size, &data, + attributes, &attr, + &time); +if (ret !=EFI_SUCCESS ) + goto err; + +/* last chance to check for delete */ +if (!data_size) + delete = true; + } + }else { +if (attributes & + (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS | +EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS )) { +debug ("Secure boot is not configured\n" ); + ret =EFI_INVALID_PARAMETER ; + goto err; + } + } + +/* delete a variable */ +if (delete) { +/* !old_size case has been handled before */ + val =NULL ; + ret =EFI_SUCCESS ; + goto out; + } +if (append) { + old_data =malloc (old_size); +if (!old_data) { +return EFI_OUT_OF_RESOURCES ; + goto err; + } + ret =EFI_CALL (efi_get_variable (variable_name, vendor, + &attr, &old_size, old_data)); +if (ret !=EFI_SUCCESS ) + goto err; + }else { old_size =0 ; } - val =malloc (old_size +2 * data_size - +strlen ("{ro,run,boot,nv}(blob)" ) +1 ); + val =malloc (2 * old_size +2 * data_size + +strlen ("{ro,run,boot,nv,time=0123456701234567}(blob)" ) + +1 );if (!val) { ret =EFI_OUT_OF_RESOURCES ; - goto out; + goto err; } s = val; -/* store attributes */ - attributes &= (EFI_VARIABLE_NON_VOLATILE | +/* +* store attributes +*/ + attributes &= (READ_ONLY | +EFI_VARIABLE_NON_VOLATILE |EFI_VARIABLE_BOOTSERVICE_ACCESS | -EFI_VARIABLE_RUNTIME_ACCESS ); +EFI_VARIABLE_RUNTIME_ACCESS | +EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS ); s +=sprintf (s,"{" );while (attributes) { -u32 attr =1 << (ffs (attributes) -1 ); + attr =1 << (ffs (attributes) -1 ); -if (attr ==EFI_VARIABLE_NON_VOLATILE ) +if (attr ==READ_ONLY ) { + s +=sprintf (s,"ro" ); + }else if (attr ==EFI_VARIABLE_NON_VOLATILE ) { s +=sprintf (s,"nv" ); -else if (attr ==EFI_VARIABLE_BOOTSERVICE_ACCESS ) + }else if (attr ==EFI_VARIABLE_BOOTSERVICE_ACCESS ) { s +=sprintf (s,"boot" ); -else if (attr ==EFI_VARIABLE_RUNTIME_ACCESS ) + }else if (attr ==EFI_VARIABLE_RUNTIME_ACCESS ) { s +=sprintf (s,"run" ); + }else if (attr == +EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS ) { + s +=sprintf (s,"time=" ); + s =bin2hex (s, (u8 *)&time,sizeof (time)); + } attributes &= ~attr;if (attributes) s +=sprintf (s,"," ); } s +=sprintf (s,"}" ); - -if (old_size) -/* APPEND_WRITE */ - s +=sprintf (s, old_val); -else - s +=sprintf (s,"(blob)" ); + s +=sprintf (s,"(blob)" );/* store payload: */ +if (append) + s =bin2hex (s, old_data, old_size); s =bin2hex (s, data, data_size); *s ='\0' ;EFI_PRINT ("setting: %s=%s\n" , native_name, val); +out:if (env_set (native_name, val)) ret =EFI_DEVICE_ERROR ; +else + ret =EFI_SUCCESS ; -out: +err:free (native_name); +free (old_data);free (val); -return EFI_EXIT (ret); +return ret; +} + +static +efi_status_t EFIAPI efi_set_volatile_variable(u16 *variable_name, +const efi_guid_t *vendor, +u32 attributes, +efi_uintn_t data_size, +const void *data, +bool ro_check) +{ +return efi_set_variable_common (variable_name, vendor, attributes, + data_size, data, ro_check, false); +} + +efi_status_t EFIAPI efi_set_nonvolatile_variable(u16 *variable_name, +const efi_guid_t *vendor, +u32 attributes, +efi_uintn_t data_size, +const void *data, +bool ro_check) +{ +efi_status_t ret; + + ret =efi_set_variable_common (variable_name, vendor, attributes, + data_size, data, ro_check, true); + +return ret; +} + +static efi_status_t efi_set_variable_internal (u16 *variable_name, +const efi_guid_t *vendor, +u32 attributes, +efi_uintn_t data_size, +const void *data, +bool ro_check) +{ +efi_status_t ret; + +if (attributes &EFI_VARIABLE_NON_VOLATILE ) + ret =efi_set_nonvolatile_variable (variable_name, vendor, + attributes, + data_size, data, ro_check); +else + ret =efi_set_volatile_variable (variable_name, vendor, + attributes, data_size, data, + ro_check); + +return ret; +} + +/** +* efi_set_variable() - set value of a UEFI variable +* +* This function implements the SetVariable runtime service. +* +* See the Unified Extensible Firmware Interface (UEFI) specification for +* details. +* +* @variable_name: name of the variable +* @vendor: vendor GUID +* @attributes: attributes of the variable +* @data_size: size of the buffer with the variable value +* @data: buffer with the variable value +* Return: status code +*/ +efi_status_t EFIAPI efi_set_variable(u16 *variable_name, +const efi_guid_t *vendor,u32 attributes, +efi_uintn_t data_size,const void *data) +{ +EFI_ENTRY ("\"%ls\" %pUl %x %zu %p" , variable_name, vendor, attributes, + data_size, data); + +/* READ_ONLY bit is not part of API */ + attributes &= ~(u32 )READ_ONLY ; + +return EFI_EXIT (efi_set_variable_internal (variable_name, vendor, + attributes, data_size, data, + true)); }/**
PATCH v5 4/16 efi_loader: variable: support variable authentication
Quoted 70 lines · Heinrich Schuchardt
Quoted 60 lines · AKASHI Takahiro
With this commit,
EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESSis supported for authenticated variables and the system secure state will transfer between setup mode and user mode as UEFI specification section 32.3 describes.Internally, authentication data is stored as part of authenticated variable's value. It is nothing but a pkcs7 message (but we need some wrapper, see
efi_variable_parse_signature()) and will be validated byefi_variable_authenticate(), henceefi_signature_verify_with_db().Associated time value will be encoded in "{...,time=...}" along with other UEFI variable's attributes.
Signed-off-by AKASHI Takahiro <[email protected]>--- include/efi_loader.h | 3 + lib/efi_loader/efi_variable.c | 665 ++++++++++++++++++++++++++++------ 2 files changed, 564 insertions(+), 104 deletions(-)include/efi_loader.h +3
-0@@ -184,6 +184,7 @@ extern const efi_guid_t efi_guid_image_security_database;extern const efi_guid_t efi_guid_sha256;extern const efi_guid_t efi_guid_cert_x509;extern const efi_guid_t efi_guid_cert_x509_sha256; +extern const efi_guid_t efi_guid_cert_type_pkcs7;/* GUID of RNG protocol */ extern const efi_guid_t efi_guid_rng_protocol;@@ -738,6 +739,8 @@ efi_status_t efi_image_region_add(struct efi_image_regions *regs,void efi_sigstore_free (struct efi_signature_store *sigstore);struct efi_signature_store *efi_sigstore_parse_sigdb (u16 *name); + +bool efi_secure_boot_enabled (void );#endif /* CONFIG_EFI_SECURE_BOOT */ #else /* CONFIG_IS_ENABLED(EFI_LOADER) */ lib/efi_loader/efi_variable.c +6
-0@@ -10,8 +10,14 @@#include <env_internal.h> #include <hexdump.h> #include <malloc.h> +#include <rtc.h> #include <search.h> +#include <linux/compat.h> #include <u-boot/crc.h> +#include "../lib/crypto/pkcs7_parser.h" + +const efi_guid_t efi_guid_cert_type_pkcs7 =EFI_CERT_TYPE_PKCS7_GUID ; +static bool efi_secure_boot;#define READ_ONLY BIT(31)lib/efi_loader/efi_variable.c static const char *prefix(const char *str, const char *prefix) · line 117
Quoted 5 lines
lib/efi_loader/efi_variable.c +1
-1@@ -108,7 +114,7 @@ static const char *prefix(const char *str, const char *prefix)* @attrp: pointer to UEFI attributes * Return: pointer to remainder of U-Boot variable value */ -static const char *parse_attr (const char *str,u32 *attrp) +static const char *parse_attr (const char *str,u32 *attrp,u64 *timep)
./lib/efi_loader/efi_variable.c:128: warning: Function parameter or member 'timep' not described in 'parse_attr'
Quoted 2 lines
All 'make htmldocs' warnings will be treated as errors after upcoming Travis CI changes.
Okay, I fixed all of them.
Thanks,
-Takahiro Akashi
Quoted 829 lines
Best regards
Heinrich
Quoted 824 lines
lib/efi_loader/efi_variable.c +554
-103{u32 attr =0 ;char sep ='{' ;@@ -131,6 +137,12 @@ static const char *parse_attr(const char *str, u32 *attrp) attr |=EFI_VARIABLE_BOOTSERVICE_ACCESS ; }else if ((s =prefix (str,"run" ))) { attr |=EFI_VARIABLE_RUNTIME_ACCESS ; + }else if ((s =prefix (str,"time=" ))) { + attr |=EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS ; +hex2bin ((u8 *)timep, s,sizeof (*timep)); + s +=sizeof (*timep) *2 ; + }else if (*str =='}' ) { +break ; }else {printf ("invalid attribute: %s\n" , str);break ;@@ -148,48 +160,291 @@ static const char *parse_attr(const char *str, u32 *attrp) }/** -* efi_get_variable() - retrieve value of a UEFI variable +* efi_secure_boot_enabled - return if secure boot is enabled or not * -* This function implements the GetVariable runtime service. +* Return: true if enabled, false if disabled +*/ +bool efi_secure_boot_enabled (void ) +{ +return efi_secure_boot; +} + +#ifdef CONFIG_EFI_SECURE_BOOT +static u8 pkcs7_hdr[] = { +/* SEQUENCE */ +0x30 ,0x82 ,0x05 ,0xc7 , +/* OID: pkcs7-signedData */ +0x06 ,0x09 ,0x2a ,0x86 ,0x48 ,0x86 ,0xf7 ,0x0d ,0x01 ,0x07 ,0x02 , +/* Context Structured? */ +0xa0 ,0x82 ,0x05 ,0xb8 , +}; + +/** +* efi_variable_parse_signature - parse a signature in variable +* @buf: Pointer to variable's value +* @buflen: Length of @buf * -* See the Unified Extensible Firmware Interface (UEFI) specification for -* details. +* Parse a signature embedded in variable's value and instantiate +* a pkcs7_message structure. Since pkcs7_parse_message() accepts only +* pkcs7's signedData, some header needed be prepended for correctly +* parsing authentication data, particularly for variable's. * -* @variable_name: name of the variable -* @vendor: vendor GUID -* @attributes: attributes of the variable -* @data_size: size of the buffer to which the variable value is copied -* @data: buffer to which the variable value is copied -* Return: status code +* Return: Pointer to pkcs7_message structure on success, NULL on error */ -efi_status_t EFIAPI efi_get_variable(u16 *variable_name, -const efi_guid_t *vendor,u32 *attributes, -efi_uintn_t *data_size,void *data) +static struct pkcs7_message *efi_variable_parse_signature (const void *buf, +size_t buflen) +{ +u8 *ebuf; +size_t ebuflen, len; +struct pkcs7_message *msg; + +/* +* This is the best assumption to check if the binary is +* already in a form of pkcs7's signedData. +*/ +if (buflen >sizeof (pkcs7_hdr) && + !memcmp (&((u8 *)buf)[4 ], &pkcs7_hdr[4 ],11 )) { + msg =pkcs7_parse_message (buf, buflen); + goto out; + } + +/* +* Otherwise, we should add a dummy prefix sequence for pkcs7 +* message parser to be able to process. +* NOTE: EDK2 also uses similar hack in WrapPkcs7Data() +* in CryptoPkg/Library/BaseCryptLib/Pk/CryptPkcs7VerifyCommon.c +* TODO: +* The header should be composed in a more refined manner. +*/ +debug ("Makeshift prefix added to authentication data\n" ); + ebuflen =sizeof (pkcs7_hdr) + buflen; +if (ebuflen <=0x7f ) { +debug ("Data is too short\n" ); +return NULL ; + } + + ebuf =malloc (ebuflen); +if (!ebuf) { +debug ("Out of memory\n" ); +return NULL ; + } + +memcpy (ebuf, pkcs7_hdr,sizeof (pkcs7_hdr)); +memcpy (ebuf +sizeof (pkcs7_hdr), buf, buflen); + len = ebuflen -4 ; + ebuf[2 ] = (len >>8 ) &0xff ; + ebuf[3 ] = len &0xff ; + len = ebuflen -0x13 ; + ebuf[0x11 ] = (len >>8 ) &0xff ; + ebuf[0x12 ] = len &0xff ; + + msg =pkcs7_parse_message (ebuf, ebuflen); + +free (ebuf); + +out: +if (IS_ERR (msg)) +return NULL ; + +return msg; +} + +/** +* efi_variable_authenticate - authenticate a variable +* @variable: Variable name in u16 +* @vendor: Guid of variable +* @data_size: Size of @data +* @data: Pointer to variable's value +* @given_attr: Attributes to be given at SetVariable() +* @env_attr: Attributes that an existing variable holds +* @time: signed time that an existing variable holds +* +* Called by efi_set_variable() to verify that the input is correct. +* Will replace the given data pointer with another that points to +* the actual data to store in the internal memory. +* On success, @data and @data_size will be replaced with variable's +* actual data, excluding authentication data, and its size, and variable's +* attributes and signed time will also be returned in @env_attr and @time, +* respectively. +* +* Return: EFI_SUCCESS on success, status code (negative) on error +*/ +static efi_status_t efi_variable_authenticate (u16 *variable, +const efi_guid_t *vendor, +efi_uintn_t *data_size, +const void **data,u32 given_attr, +u32 *env_attr,u64 *time) +{ +const struct efi_variable_authentication_2 *auth; +struct efi_signature_store *truststore, *truststore2; +struct pkcs7_message *var_sig; +struct efi_image_regions *regs; +struct efi_time timestamp; +struct rtc_time tm; +u64 new_time; +efi_status_t ret; + + var_sig =NULL ; + truststore =NULL ; + truststore2 =NULL ; + regs =NULL ; + ret =EFI_SECURITY_VIOLATION ; + +if (*data_size <sizeof (struct efi_variable_authentication_2 )) + goto err; + +/* authentication data */ + auth = *data; +if (*data_size < (sizeof (auth->time_stamp) + + auth->auth_info.hdr.dwLength)) + goto err; + +if (guidcmp (&auth->auth_info.cert_type, &efi_guid_cert_type_pkcs7)) + goto err; + + *data +=sizeof (auth->time_stamp) + auth->auth_info.hdr.dwLength; + *data_size -= (sizeof (auth->time_stamp) + + auth->auth_info.hdr.dwLength); + +memcpy (×tamp, &auth->time_stamp,sizeof (timestamp)); +memset (&tm,0 ,sizeof (tm)); + tm.tm_year = timestamp.year; + tm.tm_mon = timestamp.month; + tm.tm_mday = timestamp.day; + tm.tm_hour = timestamp.hour; + tm.tm_min = timestamp.minute; + tm.tm_sec = timestamp.second; + new_time =rtc_mktime (&tm); + +if (!efi_secure_boot_enabled ()) { +/* finished checking */ + *time = new_time; +return EFI_SUCCESS ; + } + +if (new_time <= *time) + goto err; + +/* data to be digested */ + regs =calloc (sizeof (*regs) +sizeof (struct image_region ) *5 ,1 ); +if (!regs) + goto err; + regs->max =5 ; +efi_image_region_add (regs, (uint8_t *)variable, + (uint8_t *)variable + +u16_strlen (variable) *sizeof (u16),1 ); +efi_image_region_add (regs, (uint8_t *)vendor, + (uint8_t *)vendor +sizeof (*vendor),1 ); +efi_image_region_add (regs, (uint8_t *)&given_attr, + (uint8_t *)&given_attr +sizeof (given_attr),1 ); +efi_image_region_add (regs, (uint8_t *)×tamp, + (uint8_t *)×tamp +sizeof (timestamp),1 ); +efi_image_region_add (regs, (uint8_t *)*data, + (uint8_t *)*data + *data_size,1 ); + +/* variable's signature list */ +if (auth->auth_info.hdr.dwLength <sizeof (auth->auth_info)) + goto err; + var_sig =efi_variable_parse_signature (auth->auth_info.cert_data, + auth->auth_info.hdr.dwLength + -sizeof (auth->auth_info)); +if (IS_ERR (var_sig)) { +debug ("Parsing variable's signature failed\n" ); + var_sig =NULL ; + goto err; + } + +/* signature database used for authentication */ +if (u16_strcmp (variable,L"PK" ) ==0 || +u16_strcmp (variable,L"KEK" ) ==0 ) { +/* with PK */ + truststore =efi_sigstore_parse_sigdb (L"PK" ); +if (!truststore) + goto err; + }else if (u16_strcmp (variable,L"db" ) ==0 || +u16_strcmp (variable,L"dbx" ) ==0 ) { +/* with PK and KEK */ + truststore =efi_sigstore_parse_sigdb (L"KEK" ); + truststore2 =efi_sigstore_parse_sigdb (L"PK" ); + +if (!truststore) { +if (!truststore2) + goto err; + + truststore = truststore2; + truststore2 =NULL ; + } + }else { +/* TODO: support private authenticated variables */ + goto err; + } + +/* verify signature */ +if (efi_signature_verify_with_sigdb (regs, var_sig, truststore,NULL )) { +debug ("Verified\n" ); + }else { +if (truststore2 && +efi_signature_verify_with_sigdb (regs, var_sig, + truststore2,NULL )) { +debug ("Verified\n" ); + }else { +debug ("Verifying variable's signature failed\n" ); + goto err; + } + } + +/* finished checking */ + *time =rtc_mktime (&tm); + ret =EFI_SUCCESS ; + +err: +efi_sigstore_free (truststore); +efi_sigstore_free (truststore2); +pkcs7_free_message (var_sig); +free (regs); + +return ret; +} +#else +static efi_status_t efi_variable_authenticate (u16 *variable, +const efi_guid_t *vendor, +efi_uintn_t *data_size, +const void **data,u32 given_attr, +u32 *env_attr,u64 *time) +{ +return EFI_SUCCESS ; +} +#endif /* CONFIG_EFI_SECURE_BOOT */ + +static +efi_status_t EFIAPI efi_get_variable_common(u16 *variable_name, +const efi_guid_t *vendor, +u32 *attributes, +efi_uintn_t *data_size,void *data, +bool is_non_volatile) {char *native_name;efi_status_t ret;unsigned long in_size; -const char *val, *s; +const char *val =NULL , *s; +u64 time =0 ;u32 attr; -EFI_ENTRY ("\"%ls\" %pUl %p %p %p" , variable_name, vendor, attributes, - data_size, data); -if (!variable_name || !vendor || !data_size)return EFI_EXIT (EFI_INVALID_PARAMETER ); ret =efi_to_native (&native_name, variable_name, vendor);if (ret) -return EFI_EXIT (ret); +return ret;EFI_PRINT ("get '%s'\n" , native_name); val =env_get (native_name);free (native_name);if (!val) -return EFI_EXIT (EFI_NOT_FOUND ); +return EFI_NOT_FOUND ; - val =parse_attr (val, &attr); + val =parse_attr (val, &attr, &time); in_size = *data_size;@@ -198,7 +453,7 @@ efi_status_t EFIAPI efi_get_variable(u16 *variable_name,/* number of hexadecimal digits must be even */ if (len &1 ) -return EFI_EXIT (EFI_DEVICE_ERROR ); +return EFI_DEVICE_ERROR ;/* two characters per byte: */ len /=2 ;@@ -209,11 +464,13 @@ efi_status_t EFIAPI efi_get_variable(u16 *variable_name, goto out; } -if (!data) -return EFI_EXIT (EFI_INVALID_PARAMETER ); +if (!data) { +debug ("Variable with no data shouldn't exist.\n" ); +return EFI_INVALID_PARAMETER ; + }if (hex2bin (data, s, len)) -return EFI_EXIT (EFI_DEVICE_ERROR ); +return EFI_DEVICE_ERROR ;EFI_PRINT ("got value: \"%s\"\n" , s); }else if ((s =prefix (val,"(utf8)" ))) {@@ -226,8 +483,10 @@ efi_status_t EFIAPI efi_get_variable(u16 *variable_name, goto out; } -if (!data) -return EFI_EXIT (EFI_INVALID_PARAMETER ); +if (!data) { +debug ("Variable with no data shouldn't exist.\n" ); +return EFI_INVALID_PARAMETER ; + }memcpy (data, s, len); ((char *)data)[len] ='\0' ;@@ -235,13 +494,67 @@ efi_status_t EFIAPI efi_get_variable(u16 *variable_name,EFI_PRINT ("got value: \"%s\"\n" , (char *)data); }else {EFI_PRINT ("invalid value: '%s'\n" , val); -return EFI_EXIT (EFI_DEVICE_ERROR ); +return EFI_DEVICE_ERROR ; } out:if (attributes) *attributes = attr &EFI_VARIABLE_MASK ; +return ret; +} + +static +efi_status_t EFIAPI efi_get_volatile_variable(u16 *variable_name, +const efi_guid_t *vendor, +u32 *attributes, +efi_uintn_t *data_size, +void *data) +{ +return efi_get_variable_common (variable_name, vendor, attributes, + data_size, data, false); +} + +efi_status_t EFIAPI efi_get_nonvolatile_variable(u16 *variable_name, +const efi_guid_t *vendor, +u32 *attributes, +efi_uintn_t *data_size, +void *data) +{ +return efi_get_variable_common (variable_name, vendor, attributes, + data_size, data, true); +} + +/** +* efi_efi_get_variable() - retrieve value of a UEFI variable +* +* This function implements the GetVariable runtime service. +* +* See the Unified Extensible Firmware Interface (UEFI) specification for +* details. +* +* @variable_name: name of the variable +* @vendor: vendor GUID +* @attributes: attributes of the variable +* @data_size: size of the buffer to which the variable value is copied +* @data: buffer to which the variable value is copied +* Return: status code +*/ +efi_status_t EFIAPI efi_get_variable(u16 *variable_name, +const efi_guid_t *vendor,u32 *attributes, +efi_uintn_t *data_size,void *data) +{ +efi_status_t ret; + +EFI_ENTRY ("\"%ls\" %pUl %p %p %p" , variable_name, vendor, attributes, + data_size, data); + + ret =efi_get_volatile_variable (variable_name, vendor, attributes, + data_size, data); +if (ret ==EFI_NOT_FOUND ) + ret =efi_get_nonvolatile_variable (variable_name, vendor, + attributes, data_size, data); +return EFI_EXIT (ret); }@@ -274,6 +587,7 @@ static efi_status_t parse_uboot_variable(char *variable, {char *guid, *name, *end, c;unsigned long name_len; +u64 time;u16 *p; guid =strchr (variable,'_' );@@ -308,7 +622,7 @@ static efi_status_t parse_uboot_variable(char *variable, *(name -1 ) = c;/* attributes */ -parse_attr (end, attributes); +parse_attr (end, attributes, &time);return EFI_SUCCESS ; }@@ -390,7 +704,7 @@ efi_status_t EFIAPI efi_get_next_variable_name(efi_uintn_t *variable_name_size, list_len =hexport_r (&env_htab,'\n' ,H_MATCH_REGEX |H_MATCH_KEY , &efi_variables_list,0 ,1 , regexlist); -/* 1 indicates that no match was found */ +if (list_len <=1 )return EFI_EXIT (EFI_NOT_FOUND );@@ -403,143 +717,286 @@ efi_status_t EFIAPI efi_get_next_variable_name(efi_uintn_t *variable_name_size,return EFI_EXIT (ret); } -/** -* efi_set_variable() - set value of a UEFI variable -* -* This function implements the SetVariable runtime service. -* -* See the Unified Extensible Firmware Interface (UEFI) specification for -* details. -* -* @variable_name: name of the variable -* @vendor: vendor GUID -* @attributes: attributes of the variable -* @data_size: size of the buffer with the variable value -* @data: buffer with the variable value -* Return: status code -*/ -efi_status_t EFIAPI efi_set_variable(u16 *variable_name, -const efi_guid_t *vendor,u32 attributes, -efi_uintn_t data_size,const void *data) +static +efi_status_t EFIAPI efi_set_variable_common(u16 *variable_name, +const efi_guid_t *vendor, +u32 attributes, +efi_uintn_t data_size, +const void *data, +bool ro_check, +bool is_non_volatile) { -char *native_name =NULL , *val =NULL , *s; -const char *old_val; -size_t old_size; -efi_status_t ret =EFI_SUCCESS ; +char *native_name =NULL , *old_data =NULL , *val =NULL , *s; +efi_uintn_t old_size; +bool append, delete; +u64 time =0 ;u32 attr; +efi_status_t ret =EFI_SUCCESS ; -EFI_ENTRY ("\"%ls\" %pUl %x %zu %p" , variable_name, vendor, attributes, - data_size, data); +debug ("%s: set '%s'\n" , __func__, native_name);if (!variable_name || !*variable_name || !vendor || ((attributes &EFI_VARIABLE_RUNTIME_ACCESS ) && !(attributes &EFI_VARIABLE_BOOTSERVICE_ACCESS ))) { ret =EFI_INVALID_PARAMETER ; - goto out; + goto err; } ret =efi_to_native (&native_name, variable_name, vendor);if (ret) - goto out; + goto err; + +/* check if a variable exists */ + old_size =0 ; + attr =0 ; + ret =EFI_CALL (efi_get_variable (variable_name, vendor, &attr, + &old_size,NULL )); +if (ret ==EFI_BUFFER_TOO_SMALL ) { +if ((is_non_volatile && !(attr &EFI_VARIABLE_NON_VOLATILE )) || + (!is_non_volatile && (attr &EFI_VARIABLE_NON_VOLATILE ))) { + ret =EFI_INVALID_PARAMETER ; + goto err; + } + } - old_val =env_get (native_name); -if (old_val) { - old_val =parse_attr (old_val, &attr); + append = !!(attributes &EFI_VARIABLE_APPEND_WRITE ); + attributes &= ~(u32 )EFI_VARIABLE_APPEND_WRITE ; + delete = !append && (!data_size || !attributes); -/* check read-only first */ -if (attr &READ_ONLY ) { +/* check attributes */ +if (old_size) { +if (ro_check && (attr &READ_ONLY )) { ret =EFI_WRITE_PROTECTED ; - goto out; - } - -if ((data_size ==0 && - !(attributes &EFI_VARIABLE_APPEND_WRITE )) || - !attributes) { -/* delete the variable: */ -env_set (native_name,NULL ); - ret =EFI_SUCCESS ; - goto out; + goto err; }/* attributes won't be changed */ -if (attr != (attributes & ~EFI_VARIABLE_APPEND_WRITE )) { +if (!delete && + ((ro_check && attr != attributes) || + (!ro_check && ((attr & ~(u32 )READ_ONLY ) + != (attributes & ~(u32 )READ_ONLY ))))) { ret =EFI_INVALID_PARAMETER ; - goto out; - } - -if (attributes &EFI_VARIABLE_APPEND_WRITE ) { -if (!prefix (old_val,"(blob)" )) { - ret =EFI_DEVICE_ERROR ; - goto out; - } - old_size =strlen (old_val); - }else { - old_size =0 ; + goto err; } }else { -if (data_size ==0 || !attributes || - (attributes &EFI_VARIABLE_APPEND_WRITE )) { +if (delete || append) {/* * Trying to delete or to update a non-existent * variable. */ ret =EFI_NOT_FOUND ; - goto out; + goto err; + } + } + +if (((!u16_strcmp (variable_name,L"PK" ) || + !u16_strcmp (variable_name,L"KEK" )) && + !guidcmp (vendor, &efi_global_variable_guid)) || + ((!u16_strcmp (variable_name,L"db" ) || + !u16_strcmp (variable_name,L"dbx" )) && + !guidcmp (vendor, &efi_guid_image_security_database))) { +/* authentication is mandatory */ +if (!(attributes & +EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS )) { +debug ("%ls: AUTHENTICATED_WRITE_ACCESS required\n" , + variable_name); + ret =EFI_INVALID_PARAMETER ; + goto err; } + } + +/* authenticate a variable */ +if (IS_ENABLED (CONFIG_EFI_SECURE_BOOT )) { +if (attributes &EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS ) { + ret =EFI_INVALID_PARAMETER ; + goto err; + } +if (attributes & +EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS ) { + ret =efi_variable_authenticate (variable_name, vendor, + &data_size, &data, + attributes, &attr, + &time); +if (ret !=EFI_SUCCESS ) + goto err; + +/* last chance to check for delete */ +if (!data_size) + delete = true; + } + }else { +if (attributes & + (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS | +EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS )) { +debug ("Secure boot is not configured\n" ); + ret =EFI_INVALID_PARAMETER ; + goto err; + } + } + +/* delete a variable */ +if (delete) { +/* !old_size case has been handled before */ + val =NULL ; + ret =EFI_SUCCESS ; + goto out; + } +if (append) { + old_data =malloc (old_size); +if (!old_data) { +return EFI_OUT_OF_RESOURCES ; + goto err; + } + ret =EFI_CALL (efi_get_variable (variable_name, vendor, + &attr, &old_size, old_data)); +if (ret !=EFI_SUCCESS ) + goto err; + }else { old_size =0 ; } - val =malloc (old_size +2 * data_size - +strlen ("{ro,run,boot,nv}(blob)" ) +1 ); + val =malloc (2 * old_size +2 * data_size + +strlen ("{ro,run,boot,nv,time=0123456701234567}(blob)" ) + +1 );if (!val) { ret =EFI_OUT_OF_RESOURCES ; - goto out; + goto err; } s = val; -/* store attributes */ - attributes &= (EFI_VARIABLE_NON_VOLATILE | +/* +* store attributes +*/ + attributes &= (READ_ONLY | +EFI_VARIABLE_NON_VOLATILE |EFI_VARIABLE_BOOTSERVICE_ACCESS | -EFI_VARIABLE_RUNTIME_ACCESS ); +EFI_VARIABLE_RUNTIME_ACCESS | +EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS ); s +=sprintf (s,"{" );while (attributes) { -u32 attr =1 << (ffs (attributes) -1 ); + attr =1 << (ffs (attributes) -1 ); -if (attr ==EFI_VARIABLE_NON_VOLATILE ) +if (attr ==READ_ONLY ) { + s +=sprintf (s,"ro" ); + }else if (attr ==EFI_VARIABLE_NON_VOLATILE ) { s +=sprintf (s,"nv" ); -else if (attr ==EFI_VARIABLE_BOOTSERVICE_ACCESS ) + }else if (attr ==EFI_VARIABLE_BOOTSERVICE_ACCESS ) { s +=sprintf (s,"boot" ); -else if (attr ==EFI_VARIABLE_RUNTIME_ACCESS ) + }else if (attr ==EFI_VARIABLE_RUNTIME_ACCESS ) { s +=sprintf (s,"run" ); + }else if (attr == +EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS ) { + s +=sprintf (s,"time=" ); + s =bin2hex (s, (u8 *)&time,sizeof (time)); + } attributes &= ~attr;if (attributes) s +=sprintf (s,"," ); } s +=sprintf (s,"}" ); - -if (old_size) -/* APPEND_WRITE */ - s +=sprintf (s, old_val); -else - s +=sprintf (s,"(blob)" ); + s +=sprintf (s,"(blob)" );/* store payload: */ +if (append) + s =bin2hex (s, old_data, old_size); s =bin2hex (s, data, data_size); *s ='\0' ;EFI_PRINT ("setting: %s=%s\n" , native_name, val); +out:if (env_set (native_name, val)) ret =EFI_DEVICE_ERROR ; +else + ret =EFI_SUCCESS ; -out: +err:free (native_name); +free (old_data);free (val); -return EFI_EXIT (ret); +return ret; +} + +static +efi_status_t EFIAPI efi_set_volatile_variable(u16 *variable_name, +const efi_guid_t *vendor, +u32 attributes, +efi_uintn_t data_size, +const void *data, +bool ro_check) +{ +return efi_set_variable_common (variable_name, vendor, attributes, + data_size, data, ro_check, false); +} + +efi_status_t EFIAPI efi_set_nonvolatile_variable(u16 *variable_name, +const efi_guid_t *vendor, +u32 attributes, +efi_uintn_t data_size, +const void *data, +bool ro_check) +{ +efi_status_t ret; + + ret =efi_set_variable_common (variable_name, vendor, attributes, + data_size, data, ro_check, true); + +return ret; +} + +static efi_status_t efi_set_variable_internal (u16 *variable_name, +const efi_guid_t *vendor, +u32 attributes, +efi_uintn_t data_size, +const void *data, +bool ro_check) +{ +efi_status_t ret; + +if (attributes &EFI_VARIABLE_NON_VOLATILE ) + ret =efi_set_nonvolatile_variable (variable_name, vendor, + attributes, + data_size, data, ro_check); +else + ret =efi_set_volatile_variable (variable_name, vendor, + attributes, data_size, data, + ro_check); + +return ret; +} + +/** +* efi_set_variable() - set value of a UEFI variable +* +* This function implements the SetVariable runtime service. +* +* See the Unified Extensible Firmware Interface (UEFI) specification for +* details. +* +* @variable_name: name of the variable +* @vendor: vendor GUID +* @attributes: attributes of the variable +* @data_size: size of the buffer with the variable value +* @data: buffer with the variable value +* Return: status code +*/ +efi_status_t EFIAPI efi_set_variable(u16 *variable_name, +const efi_guid_t *vendor,u32 attributes, +efi_uintn_t data_size,const void *data) +{ +EFI_ENTRY ("\"%ls\" %pUl %x %zu %p" , variable_name, vendor, attributes, + data_size, data); + +/* READ_ONLY bit is not part of API */ + attributes &= ~(u32 )READ_ONLY ; + +return EFI_EXIT (efi_set_variable_internal (variable_name, vendor, + attributes, data_size, data, + true)); }/**
PATCH v5 5/16 efi_loader: variable: add secure boot state transition
In reply to AKASHI TakahiroUEFI specification defines several global variables which are related to the current secure boot state. In this commit, those values will be maintained according to operations. Currently, AuditMode and DeployedMode are defined but not implemented.
--- lib/efi_loader/efi_variable.c | 231 +++++++++++++++++++++++++++++++++- 1 file changed, 228 insertions(+), 3 deletions(-)
lib/efi_loader/efi_variable.c +228 -3
@@ -16,8 +16,16 @@#include <u-boot/crc.h> #include "../lib/crypto/pkcs7_parser.h" +enum efi_secure_mode { +EFI_MODE_SETUP , +EFI_MODE_USER , +EFI_MODE_AUDIT , +EFI_MODE_DEPLOYED , +}; +const efi_guid_t efi_guid_cert_type_pkcs7 =EFI_CERT_TYPE_PKCS7_GUID ;static bool efi_secure_boot; +static int efi_secure_mode;#define READ_ONLY BIT(31)
@@ -159,6 +167,210 @@ static const char *parse_attr(const char *str, u32 *attrp, u64 *timep)return str; } +static efi_status_t efi_set_variable_internal (u16 *variable_name, +const efi_guid_t *vendor, +u32 attributes, +efi_uintn_t data_size, +const void *data, +bool ro_check); + +/** +* efi_transfer_secure_state - handle a secure boot state transition +* @mode: new state +* +* Depending on @mode, secure boot related variables are updated. +* Those variables are *read-only* for users, efi_set_variable_internal() +* is called here. +* +* Return: EFI_SUCCESS on success, status code (negative) on error +*/ +static efi_status_t efi_transfer_secure_state (enum efi_secure_mode mode) +{ +u32 attributes; +u8 val; +efi_status_t ret; + +debug ("Secure state from %d to %d\n" , efi_secure_mode, mode); + + attributes =EFI_VARIABLE_BOOTSERVICE_ACCESS | +EFI_VARIABLE_RUNTIME_ACCESS ; +if (mode ==EFI_MODE_DEPLOYED ) { + val =1 ; + ret =efi_set_variable_internal (L"SecureBoot" , + &efi_global_variable_guid, + attributes |READ_ONLY , +sizeof (val), &val, + false); +if (ret !=EFI_SUCCESS ) + goto err; + val =0 ; + ret =efi_set_variable_internal (L"SetupMode" , + &efi_global_variable_guid, + attributes |READ_ONLY , +sizeof (val), &val, + false); +if (ret !=EFI_SUCCESS ) + goto err; + val =0 ; + ret =efi_set_variable_internal (L"AuditMode" , + &efi_global_variable_guid, + attributes |READ_ONLY , +sizeof (val), &val, + false); +if (ret !=EFI_SUCCESS ) + goto err; + val =1 ; + ret =efi_set_variable_internal (L"DeployedMode" , + &efi_global_variable_guid, + attributes |READ_ONLY , +sizeof (val), &val, + false); +if (ret !=EFI_SUCCESS ) + goto err; + + efi_secure_boot = true; + }else if (mode ==EFI_MODE_AUDIT ) { + ret =efi_set_variable_internal (L"PK" , + &efi_global_variable_guid, + attributes, +0 ,NULL , + false); +if (ret !=EFI_SUCCESS ) + goto err; + val =0 ; + ret =efi_set_variable_internal (L"SecureBoot" , + &efi_global_variable_guid, + attributes |READ_ONLY , +sizeof (val), &val, + false); +if (ret !=EFI_SUCCESS ) + goto err; + val =1 ; + ret =efi_set_variable_internal (L"SetupMode" , + &efi_global_variable_guid, + attributes |READ_ONLY , +sizeof (val), &val, + false); +if (ret !=EFI_SUCCESS ) + goto err; + val =1 ; + ret =efi_set_variable_internal (L"AuditMode" , + &efi_global_variable_guid, + attributes |READ_ONLY , +sizeof (val), &val, + false); +if (ret !=EFI_SUCCESS ) + goto err; + val =0 ; + ret =efi_set_variable_internal (L"DeployedMode" , + &efi_global_variable_guid, + attributes |READ_ONLY , +sizeof (val), &val, + false); +if (ret !=EFI_SUCCESS ) + goto err; + + efi_secure_boot = true; + }else if (mode ==EFI_MODE_USER ) { + val =1 ; + ret =efi_set_variable_internal (L"SecureBoot" , + &efi_global_variable_guid, + attributes |READ_ONLY , +sizeof (val), &val, + false); +if (ret !=EFI_SUCCESS ) + goto err; + val =0 ; + ret =efi_set_variable_internal (L"SetupMode" , + &efi_global_variable_guid, + attributes |READ_ONLY , +sizeof (val), &val, + false); +if (ret !=EFI_SUCCESS ) + goto err; + val =0 ; + ret =efi_set_variable_internal (L"AuditMode" , + &efi_global_variable_guid, + attributes, +sizeof (val), &val, + false); +if (ret !=EFI_SUCCESS ) + goto err; + val =0 ; + ret =efi_set_variable_internal (L"DeployedMode" , + &efi_global_variable_guid, + attributes, +sizeof (val), &val, + false); +if (ret !=EFI_SUCCESS ) + goto err; + + efi_secure_boot = true; + }else if (mode ==EFI_MODE_SETUP ) { + val =0 ; + ret =efi_set_variable_internal (L"SecureBoot" , + &efi_global_variable_guid, + attributes |READ_ONLY , +sizeof (val), &val, + false); +if (ret !=EFI_SUCCESS ) + goto err; + val =1 ; + ret =efi_set_variable_internal (L"SetupMode" , + &efi_global_variable_guid, + attributes |READ_ONLY , +sizeof (val), &val, + false); +if (ret !=EFI_SUCCESS ) + goto err; + val =0 ; + ret =efi_set_variable_internal (L"AuditMode" , + &efi_global_variable_guid, + attributes, +sizeof (val), &val, + false); +if (ret !=EFI_SUCCESS ) + goto err; + val =0 ; + ret =efi_set_variable_internal (L"DeployedMode" , + &efi_global_variable_guid, + attributes |READ_ONLY , +sizeof (val), &val, + false); +if (ret !=EFI_SUCCESS ) + goto err; + }else { +return EFI_INVALID_PARAMETER ; + } + +return EFI_SUCCESS ; + +err: +/* TODO: What action should be taken here? */ +printf ("ERROR: Secure state transition failed\n" ); +return ret; +} + +/** +* efi_init_secure_state - initialize secure boot state +* +* Return: EFI_SUCCESS on success, status code (negative) on error +*/ +static efi_status_t efi_init_secure_state (void ) +{ +efi_uintn_t size =0 ; +efi_status_t ret; + + ret =EFI_CALL (efi_get_variable (L"PK" , &efi_global_variable_guid, +NULL , &size,NULL )); +if (ret ==EFI_BUFFER_TOO_SMALL &&IS_ENABLED (CONFIG_EFI_SECURE_BOOT )) + ret =efi_transfer_secure_state (EFI_MODE_USER ); +else + ret =efi_transfer_secure_state (EFI_MODE_SETUP ); + +return ret; +} +/** * efi_secure_boot_enabled - return if secure boot is enabled or not *
@@ -908,10 +1120,19 @@ efi_status_t EFIAPI efi_set_variable_common(u16 *variable_name,EFI_PRINT ("setting: %s=%s\n" , native_name, val); out: -if (env_set (native_name, val)) +if (env_set (native_name, val)) { ret =EFI_DEVICE_ERROR ; -else + }else { +if ((u16_strcmp (variable_name,L"PK" ) ==0 && +guidcmp (vendor, &efi_global_variable_guid) ==0 )) { + ret =efi_transfer_secure_state ( + (delete ?EFI_MODE_SETUP : +EFI_MODE_USER )); +if (ret !=EFI_SUCCESS ) + goto err; + } ret =EFI_SUCCESS ; + } err:free (native_name);
@@ -1096,5 +1317,9 @@ void efi_variables_boot_exit_notify(void)*/ efi_status_t efi_init_variables (void ) { -return EFI_SUCCESS ; +efi_status_t ret; + + ret =efi_init_secure_state (); + +return ret; }
Signature · 1 line
2.24.0
PATCH v5 6/16 efi_loader: variable: add VendorKeys variable
In reply to AKASHI TakahiroThe following variable is exported as UEFI specification defines:
VendorKeys: whether the system is configured to use only vendor-provided
keys or not
The value will have to be modified if a platform has its own way of initializing signature database, in particular, PK.
--- lib/efi_loader/efi_variable.c | 69 ++++++++++++++++++++++++++++++++--- 1 file changed, 63 insertions(+), 6 deletions(-)
lib/efi_loader/efi_variable.c +63 -6
@@ -26,6 +26,7 @@ enum efi_secure_mode {const efi_guid_t efi_guid_cert_type_pkcs7 =EFI_CERT_TYPE_PKCS7_GUID ;static bool efi_secure_boot;static int efi_secure_mode; +static u8 efi_vendor_keys;#define READ_ONLY BIT(31)
@@ -343,6 +344,8 @@ static efi_status_t efi_transfer_secure_state(enum efi_secure_mode mode)return EFI_INVALID_PARAMETER ; } + efi_secure_mode = mode; +return EFI_SUCCESS ; err:
@@ -358,16 +361,46 @@ err:*/ static efi_status_t efi_init_secure_state (void ) { -efi_uintn_t size =0 ; +enum efi_secure_mode mode; +efi_uintn_t size;efi_status_t ret; +/* +* TODO: +* Since there is currently no "platform-specific" installation +* method of Platform Key, we can't say if VendorKeys is 0 or 1 +* precisely. +*/ + + size =0 ; ret =EFI_CALL (efi_get_variable (L"PK" , &efi_global_variable_guid,NULL , &size,NULL )); -if (ret ==EFI_BUFFER_TOO_SMALL &&IS_ENABLED (CONFIG_EFI_SECURE_BOOT )) - ret =efi_transfer_secure_state (EFI_MODE_USER ); -else - ret =efi_transfer_secure_state (EFI_MODE_SETUP ); +if (ret ==EFI_BUFFER_TOO_SMALL ) { +if (IS_ENABLED (CONFIG_EFI_SECURE_BOOT )) + mode =EFI_MODE_USER ; +else + mode =EFI_MODE_SETUP ; + + efi_vendor_keys =0 ; + }else if (ret ==EFI_NOT_FOUND ) { + mode =EFI_MODE_SETUP ; + efi_vendor_keys =1 ; + }else { + goto err; + } + ret =efi_transfer_secure_state (mode); +if (ret ==EFI_SUCCESS ) + ret =efi_set_variable_internal (L"VendorKeys" , + &efi_global_variable_guid, +EFI_VARIABLE_BOOTSERVICE_ACCESS + |EFI_VARIABLE_RUNTIME_ACCESS + |READ_ONLY , +sizeof (efi_vendor_keys), + &efi_vendor_keys, + false); + +err:return ret; }
@@ -1123,6 +1156,8 @@ out:if (env_set (native_name, val)) { ret =EFI_DEVICE_ERROR ; }else { +bool vendor_keys_modified = false; +if ((u16_strcmp (variable_name,L"PK" ) ==0 &&guidcmp (vendor, &efi_global_variable_guid) ==0 )) { ret = efi_transfer_secure_state(
@@ -1130,8 +1165,30 @@ out: EFI_MODE_USER));if (ret !=EFI_SUCCESS ) goto err; + +if (efi_secure_mode !=EFI_MODE_SETUP ) + vendor_keys_modified = true; + }else if ((u16_strcmp (variable_name,L"KEK" ) ==0 && +guidcmp (vendor, &efi_global_variable_guid) ==0 )) { +if (efi_secure_mode !=EFI_MODE_SETUP ) + vendor_keys_modified = true; + } + +/* update VendorKeys */ +if (vendor_keys_modified & efi_vendor_keys) { + efi_vendor_keys =0 ; + ret =efi_set_variable_internal ( +L"VendorKeys" , + &efi_global_variable_guid, +EFI_VARIABLE_BOOTSERVICE_ACCESS + |EFI_VARIABLE_RUNTIME_ACCESS + |READ_ONLY , +sizeof (efi_vendor_keys), + &efi_vendor_keys, + false); + }else { + ret =EFI_SUCCESS ; } - ret =EFI_SUCCESS ; } err:
Signature · 1 line
2.24.0
PATCH v5 7/16 efi_loader: image_loader: support image authentication
In reply to AKASHI TakahiroWith this commit, image validation can be enforced, as UEFI specification section 32.5 describes, if CONFIG_EFI_SECURE_BOOT is enabled.
Currently we support
* authentication based on db and dbx,
so dbx-validated image will always be rejected.
* following signature types:
EFI_CERT_SHA256_GUID (SHA256 digest for unsigned images) EFI_CERT_X509_GUID (x509 certificate for signed images)
Timestamp-based certificate revocation is not supported here.
Internally, authentication data is stored in one of certificates tables of PE image (See efi_image_parse()) and will be verified by efi_image_authenticate() before loading a given image.
It seems that UEFI specification defines the verification process in a bit ambiguous way. I tried to implement it as closely to as EDK2 does.
--- include/efi_loader.h | 13 +- lib/efi_loader/efi_boottime.c | 10 +- lib/efi_loader/efi_image_loader.c | 460 +++++++++++++++++++++++++++++- 3 files changed, 467 insertions(+), 16 deletions(-)
include/efi_loader.h +12 -1
@@ -11,6 +11,7 @@#include <common.h> #include <part_efi.h> #include <efi_api.h> +#include <pe.h> static inline int guidcmp (const void *g1,const void *g2) {
@@ -263,6 +264,11 @@ struct efi_object {enum efi_object_type type; }; +enum efi_image_auth_status { +EFI_IMAGE_AUTH_FAILED =0 , +EFI_IMAGE_AUTH_PASSED , +}; +/** * struct efi_loaded_image_obj - handle of a loaded image *
@@ -282,6 +288,7 @@ struct efi_loaded_image_obj { EFIAPI efi_status_t (*entry)(efi_handle_t image_handle,struct efi_system_table *st);u16 image_type; +enum efi_image_auth_status auth_status; };/**
@@ -414,7 +421,8 @@ efi_status_t efi_set_watchdog(unsigned long timeout);/* Called from places to check whether a timer expired */ void efi_timer_check (void );/* PE loader implementation */ -efi_status_t efi_load_pe (struct efi_loaded_image_obj *handle,void *efi, +efi_status_t efi_load_pe (struct efi_loaded_image_obj *handle, +void *efi,size_t efi_size,struct efi_loaded_image *loaded_image_info);/* Called once to store the pristine gd pointer */ void efi_save_gd (void );
@@ -741,6 +749,9 @@ void efi_sigstore_free(struct efi_signature_store *sigstore);struct efi_signature_store *efi_sigstore_parse_sigdb (u16 *name);bool efi_secure_boot_enabled (void ); + +bool efi_image_parse (void *efi,size_t len,struct efi_image_regions **regp, +WIN_CERTIFICATE **auth,size_t *auth_len);#endif /* CONFIG_EFI_SECURE_BOOT */ #else /* CONFIG_IS_ENABLED(EFI_LOADER) */
lib/efi_loader/efi_boottime.c +8 -2
@@ -1882,12 +1882,12 @@ efi_status_t EFIAPI efi_load_image(bool boot_policy,efi_dp_split_file_path (file_path, &dp, &fp); ret =efi_setup_loaded_image (dp, fp, image_obj, &info);if (ret ==EFI_SUCCESS ) - ret =efi_load_pe (*image_obj, dest_buffer, info); + ret =efi_load_pe (*image_obj, dest_buffer, source_size, info);if (!source_buffer)/* Release buffer to which file was loaded */ efi_free_pages ((uintptr_t )dest_buffer,efi_size_in_pages (source_size)); -if (ret ==EFI_SUCCESS ) { +if (ret ==EFI_SUCCESS || ret ==EFI_SECURITY_VIOLATION ) { info->system_table = &systab; info->parent_handle = parent_image; }else {
@@ -2885,10 +2885,16 @@ efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,EFI_ENTRY ("%p, %p, %p" , image_handle, exit_data_size, exit_data); +if (!efi_search_obj (image_handle)) +return EFI_EXIT (EFI_INVALID_PARAMETER ); +/* Check parameters */ if (image_obj->header.type !=EFI_OBJECT_TYPE_LOADED_IMAGE )return EFI_EXIT (EFI_INVALID_PARAMETER ); +if (image_obj->auth_status !=EFI_IMAGE_AUTH_PASSED ) +return EFI_EXIT (EFI_SECURITY_VIOLATION ); + ret =EFI_CALL (efi_open_protocol (image_handle, &efi_guid_loaded_image, &info,NULL ,NULL ,EFI_OPEN_PROTOCOL_GET_PROTOCOL ));
lib/efi_loader/efi_image_loader.c +447 -13
537 lines not on this page · raw message
Signature · 1 line
2.24.0
PATCH v5 7/16 efi_loader: image_loader: support image authentication
Quoted 15 lines · AKASHI Takahiro
With this commit, image validation can be enforced, as UEFI specification section 32.5 describes, if
CONFIG_EFI_SECURE_BOOTis enabled.Currently we support
* authentication based on db and dbx,
so dbx-validated image will always be rejected.
* following signature types:
EFI_CERT_SHA256_GUID(SHA256 digest for unsigned images)EFI_CERT_X509_GUID(x509 certificate for signed images)
Timestamp-based certificate revocation is not supported here.Internally, authentication data is stored in one of certificates tables of PE image (See
efi_image_parse()) and will be verified byefi_image_authenticate()before loading a given image.
Quoted 5 lines
It seems that UEFI specification defines the verification process in a bit ambiguous way. I tried to implement it as closely to as EDK2 does.
Signed-off-by AKASHI Takahiro <[email protected]>
According to git bisect this patch breaks the test test/py/tests/test_efi_fit.py.
Best regards
Heinrich
PATCH v5 7/16 efi_loader: image_loader: support image authentication
Quoted 16 lines · Heinrich Schuchardt
Quoted 15 lines · AKASHI Takahiro
With this commit, image validation can be enforced, as UEFI specification section 32.5 describes, if
CONFIG_EFI_SECURE_BOOTis enabled.Currently we support
* authentication based on db and dbx,
so dbx-validated image will always be rejected.
* following signature types:
EFI_CERT_SHA256_GUID(SHA256 digest for unsigned images)EFI_CERT_X509_GUID(x509 certificate for signed images)
Timestamp-based certificate revocation is not supported here.Internally, authentication data is stored in one of certificates tables of PE image (See
efi_image_parse()) and will be verified byefi_image_authenticate()before loading a given image.
Quoted 8 lines
Quoted 5 lines
It seems that UEFI specification defines the verification process in a bit ambiguous way. I tried to implement it as closely to as EDK2 does.
Signed-off-by AKASHI Takahiro <[email protected]>According to git bisect this patch breaks the test
test/py/tests/test_efi_fit.py.
This error only occurs on "compressed" FIT images. While I'm not sure whether it is directly related to efi support in bootm or not, I've fixed it any way.
Thanks,
-Takahiro Akashi
Quoted 3 lines
Best regards
Heinrich
PATCH v5 7/16 efi_loader: image_loader: support image authentication
Quoted 21 lines · AKASHI Takahiro
Quoted 20 lines · Heinrich Schuchardt
Quoted 19 lines · AKASHI Takahiro
With this commit, image validation can be enforced, as UEFI specification section 32.5 describes, if
CONFIG_EFI_SECURE_BOOTis enabled.Currently we support
* authentication based on db and dbx,
so dbx-validated image will always be rejected.
* following signature types:
EFI_CERT_SHA256_GUID(SHA256 digest for unsigned images)EFI_CERT_X509_GUID(x509 certificate for signed images)
Timestamp-based certificate revocation is not supported here.Internally, authentication data is stored in one of certificates tables of PE image (See
efi_image_parse()) and will be verified byefi_image_authenticate()before loading a given image.It seems that UEFI specification defines the verification process in a bit ambiguous way. I tried to implement it as closely to as EDK2 does.
Quoted 8 lines
Quoted 4 lines
Quoted 1 line
Signed-off-by AKASHI Takahiro <[email protected]>According to git bisect this patch breaks the test
test/py/tests/test_efi_fit.py.This error only occurs on "compressed" FIT images. While I'm not sure whether it is directly related to efi support in bootm or not, I've fixed it any way.
Hello Takahiro,
where can I find the fix?
Best regards
Heinrich
Quoted 8 lines
Thanks,
-Takahiro AkashiQuoted 3 lines
Best regards
Heinrich
PATCH v5 7/16 efi_loader: image_loader: support image authentication
Quoted 27 lines · Heinrich Schuchardt
Quoted 26 lines · AKASHI Takahiro
Quoted 24 lines · Heinrich Schuchardt
Quoted 15 lines · AKASHI Takahiro
With this commit, image validation can be enforced, as UEFI specification section 32.5 describes, if
CONFIG_EFI_SECURE_BOOTis enabled.Currently we support
* authentication based on db and dbx,
so dbx-validated image will always be rejected.
* following signature types:
EFI_CERT_SHA256_GUID(SHA256 digest for unsigned images)EFI_CERT_X509_GUID(x509 certificate for signed images)
Timestamp-based certificate revocation is not supported here.Internally, authentication data is stored in one of certificates tables of PE image (See
efi_image_parse()) and will be verified byefi_image_authenticate()before loading a given image.Quoted 5 lines
It seems that UEFI specification defines the verification process in a bit ambiguous way. I tried to implement it as closely to as EDK2 does.
Signed-off-by AKASHI Takahiro <[email protected]>According to git bisect this patch breaks the test
test/py/tests/test_efi_fit.py.
Quoted 7 lines
Quoted 3 lines
This error only occurs on "compressed" FIT images. While I'm not sure whether it is directly related to efi support in bootm or not, I've fixed it any way.
Hello Takahiro,
where can I find the fix?
Only in my local repository.
Since I'm running Travis CI now, I will post a new version once the test is completed AND if you have no more comments on my v5.
Thanks,
-Takahiro Akashi
Quoted 13 lines
Best regards
Heinrich
Quoted 8 lines
Thanks,
-Takahiro AkashiQuoted 3 lines
Best regards
Heinrich
PATCH v5 7/16 efi_loader: image_loader: support image authentication
In reply to AKASHI TakahiroQuoted 133 lines · AKASHI Takahiro
With this commit, image validation can be enforced, as UEFI specification section 32.5 describes, if
CONFIG_EFI_SECURE_BOOTis enabled.Currently we support
* authentication based on db and dbx,
so dbx-validated image will always be rejected.
* following signature types:
EFI_CERT_SHA256_GUID(SHA256 digest for unsigned images)EFI_CERT_X509_GUID(x509 certificate for signed images)
Timestamp-based certificate revocation is not supported here.Internally, authentication data is stored in one of certificates tables of PE image (See
efi_image_parse()) and will be verified byefi_image_authenticate()before loading a given image.It seems that UEFI specification defines the verification process in a bit ambiguous way. I tried to implement it as closely to as EDK2 does.
Signed-off-by AKASHI Takahiro <[email protected]>--- include/efi_loader.h | 13 +- lib/efi_loader/efi_boottime.c | 10 +- lib/efi_loader/efi_image_loader.c | 460 +++++++++++++++++++++++++++++- 3 files changed, 467 insertions(+), 16 deletions(-)include/efi_loader.h +12
-1@@ -11,6 +11,7 @@#include <common.h> #include <part_efi.h> #include <efi_api.h> +#include <pe.h> static inline int guidcmp (const void *g1,const void *g2) {@@ -263,6 +264,11 @@ struct efi_object {enum efi_object_type type; }; +enum efi_image_auth_status { +EFI_IMAGE_AUTH_FAILED =0 , +EFI_IMAGE_AUTH_PASSED , +}; +/** * struct efi_loaded_image_obj - handle of a loaded image * @@ -282,6 +288,7 @@ struct efi_loaded_image_obj { EFIAPI efi_status_t (*entry)(efi_handle_t image_handle,struct efi_system_table *st);u16 image_type; +enum efi_image_auth_status auth_status; };/** @@ -414,7 +421,8 @@ efi_status_t efi_set_watchdog(unsigned long timeout);/* Called from places to check whether a timer expired */ void efi_timer_check (void );/* PE loader implementation */ -efi_status_t efi_load_pe (struct efi_loaded_image_obj *handle,void *efi, +efi_status_t efi_load_pe (struct efi_loaded_image_obj *handle, +void *efi,size_t efi_size,struct efi_loaded_image *loaded_image_info);/* Called once to store the pristine gd pointer */ void efi_save_gd (void );@@ -741,6 +749,9 @@ void efi_sigstore_free(struct efi_signature_store *sigstore);struct efi_signature_store *efi_sigstore_parse_sigdb (u16 *name);bool efi_secure_boot_enabled (void ); + +bool efi_image_parse (void *efi,size_t len,struct efi_image_regions **regp, +WIN_CERTIFICATE **auth,size_t *auth_len);#endif /* CONFIG_EFI_SECURE_BOOT */ #else /* CONFIG_IS_ENABLED(EFI_LOADER) */ lib/efi_loader/efi_boottime.c +8
-2@@ -1882,12 +1882,12 @@ efi_status_t EFIAPI efi_load_image(bool boot_policy,efi_dp_split_file_path (file_path, &dp, &fp); ret =efi_setup_loaded_image (dp, fp, image_obj, &info);if (ret ==EFI_SUCCESS ) - ret =efi_load_pe (*image_obj, dest_buffer, info); + ret =efi_load_pe (*image_obj, dest_buffer, source_size, info);if (!source_buffer)/* Release buffer to which file was loaded */ efi_free_pages ((uintptr_t )dest_buffer,efi_size_in_pages (source_size)); -if (ret ==EFI_SUCCESS ) { +if (ret ==EFI_SUCCESS || ret ==EFI_SECURITY_VIOLATION ) { info->system_table = &systab; info->parent_handle = parent_image; }else {@@ -2885,10 +2885,16 @@ efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,EFI_ENTRY ("%p, %p, %p" , image_handle, exit_data_size, exit_data); +if (!efi_search_obj (image_handle)) +return EFI_EXIT (EFI_INVALID_PARAMETER ); +/* Check parameters */ if (image_obj->header.type !=EFI_OBJECT_TYPE_LOADED_IMAGE )return EFI_EXIT (EFI_INVALID_PARAMETER ); +if (image_obj->auth_status !=EFI_IMAGE_AUTH_PASSED ) +return EFI_EXIT (EFI_SECURITY_VIOLATION ); + ret =EFI_CALL (efi_open_protocol (image_handle, &efi_guid_loaded_image, &info,NULL ,NULL ,EFI_OPEN_PROTOCOL_GET_PROTOCOL ));lib/efi_loader/efi_image_loader.c +3
-0@@ -10,7 +10,10 @@#include <common.h> #include <cpu_func.h> #include <efi_loader.h> +#include <malloc.h> #include <pe.h> +#include <sort.h> +#include "../lib/crypto/pkcs7_parser.h" const efi_guid_t efi_global_variable_guid =EFI_GLOBAL_VARIABLE_GUID ;const efi_guid_t efi_guid_device_path =EFI_DEVICE_PATH_PROTOCOL_GUID ;@@ -206,6 +209,367 @@ static void efi_set_code_and_data_type( } }
lib/efi_loader/efi_image_loader.c static void efi_set_code_and_data_type( · line 221
Quoted 10 lines
lib/efi_loader/efi_image_loader.c +10
-0+#ifdef CONFIG_EFI_SECURE_BOOT +/** +* cmp_pe_section - compare two sections +* @arg1: Pointer to first section +* @arg2: Pointer to second section +* +* Compare two sections in PE image. +* +* Return: -1, 0, 1 respectively if arg1 < arg2, arg1 == arg2 or +arg1 > arg2
make htmldocs creates a warning ./lib/efi_loader/efi_image_loader.c:222: warning: bad line:
arg1 > arg2
All warnings in 'make htmldocs' will be treated as errors in an upcoming 'make htmldocs' build step for Travis CI.
Quoted 10 lines
lib/efi_loader/efi_image_loader.c +10
-0+*/ +static int cmp_pe_section (const void *arg1,const void *arg2) +{ +const IMAGE_SECTION_HEADER *section1 = arg1, *section2 = arg2; + +if (section1->VirtualAddress < section2->VirtualAddress) +return -1 ; +else if (section1->VirtualAddress == section2->VirtualAddress) +return 0 ; +else
lib/efi_loader/efi_image_loader.c static void efi_set_code_and_data_type( · line 241
Quoted 10 lines
lib/efi_loader/efi_image_loader.c +10
-0+return 1 ; +} + +/** +* efi_image_parse - parse a PE image +* @efi: Pointer to image +* @len: Size of @efi +* @regs: Pointer to a list of regions +* @auth: Pointer to a pointer to authentication data in PE +* @auth_len: Size of @auth
./lib/efi_loader/efi_image_loader.c:253: warning: Function parameter or member 'regp' not described in 'efi_image_parse'
Quoted 335 lines
lib/efi_loader/efi_image_loader.c +331
-0+* +* Parse image binary in PE32(+) format, assuming that sanity of PE image +* has been checked by a caller. +* On success, an address of authentication data in @efi and its size will +* be returned in @auth and @auth_len, respectively. +* +* Return: true on success, false on error +*/ +bool efi_image_parse (void *efi,size_t len,struct efi_image_regions **regp, +WIN_CERTIFICATE **auth,size_t *auth_len) +{ +struct efi_image_regions *regs; +IMAGE_DOS_HEADER *dos; +IMAGE_NT_HEADERS32 *nt; +IMAGE_SECTION_HEADER *sections, **sorted; +int num_regions, num_sections, i; +int ctidx =IMAGE_DIRECTORY_ENTRY_SECURITY ; +u32 align, size, authsz, authoff; +size_t bytes_hashed; + + dos = (void *)efi; + nt = (void *)(efi + dos->e_lfanew); + +/* +* Count maximum number of regions to be digested. +* We don't have to have an exact number here. +* See efi_image_region_add()'s in parsing below. +*/ + num_regions =3 ;/* for header */ + num_regions += nt->FileHeader.NumberOfSections; + num_regions++;/* for extra */ + + regs =calloc (sizeof (*regs) +sizeof (struct image_region ) * num_regions, +1 ); +if (!regs) + goto err; + regs->max = num_regions; + +/* +* Collect data regions for hash calculation +* 1. File headers +*/ +if (nt->OptionalHeader.Magic ==IMAGE_NT_OPTIONAL_HDR64_MAGIC ) { +IMAGE_NT_HEADERS64 *nt64 = (void *)nt; +IMAGE_OPTIONAL_HEADER64 *opt = &nt64->OptionalHeader; + +/* Skip CheckSum */ +efi_image_region_add (regs, efi, &opt->CheckSum,0 ); +if (nt64->OptionalHeader.NumberOfRvaAndSizes <= ctidx) { +efi_image_region_add (regs, + &opt->CheckSum +1 , + efi + opt->SizeOfHeaders,0 ); + }else { +/* Skip Certificates Table */ +efi_image_region_add (regs, + &opt->CheckSum +1 , + &opt->DataDirectory[ctidx],0 ); +efi_image_region_add (regs, + &opt->DataDirectory[ctidx] +1 , + efi + opt->SizeOfHeaders,0 ); + } + + bytes_hashed = opt->SizeOfHeaders; + align = opt->FileAlignment; + authoff = opt->DataDirectory[ctidx].VirtualAddress; + authsz = opt->DataDirectory[ctidx].Size; + }else if (nt->OptionalHeader.Magic ==IMAGE_NT_OPTIONAL_HDR32_MAGIC ) { +IMAGE_OPTIONAL_HEADER32 *opt = &nt->OptionalHeader; + +efi_image_region_add (regs, efi, &opt->CheckSum,0 ); +efi_image_region_add (regs, &opt->CheckSum +1 , + &opt->DataDirectory[ctidx],0 ); +efi_image_region_add (regs, &opt->DataDirectory[ctidx] +1 , + efi + opt->SizeOfHeaders,0 ); + + bytes_hashed = opt->SizeOfHeaders; + align = opt->FileAlignment; + authoff = opt->DataDirectory[ctidx].VirtualAddress; + authsz = opt->DataDirectory[ctidx].Size; + }else { +debug ("%s: Invalid optional header magic %x\n" , __func__, + nt->OptionalHeader.Magic); + goto err; + } + +/* 2. Sections */ + num_sections = nt->FileHeader.NumberOfSections; + sections = (void *)((uint8_t *)&nt->OptionalHeader + + nt->FileHeader.SizeOfOptionalHeader); + sorted =calloc (sizeof (IMAGE_SECTION_HEADER *), num_sections); +if (!sorted) { +debug ("%s: Out of memory\n" , __func__); + goto err; + } + +/* +* Make sure the section list is in ascending order. +*/ +for (i =0 ; i < num_sections; i++) + sorted[i] = §ions[i]; +qsort (sorted, num_sections,sizeof (§ions[0 ]), cmp_pe_section); + +for (i =0 ; i < num_sections; i++) { +if (!sorted[i]->SizeOfRawData) +continue ; + + size = (sorted[i]->SizeOfRawData + align -1 ) & ~(align -1 ); +efi_image_region_add (regs, efi + sorted[i]->PointerToRawData, + efi + sorted[i]->PointerToRawData + size, +0 ); +debug ("section[%d](%s): raw: 0x%x-0x%x, virt: %x-%x\n" , + i, sorted[i]->Name, + sorted[i]->PointerToRawData, + sorted[i]->PointerToRawData + size, + sorted[i]->VirtualAddress, + sorted[i]->VirtualAddress + + sorted[i]->Misc.VirtualSize); + + bytes_hashed += size; + } +free (sorted); + +/* 3. Extra data excluding Certificates Table */ +if (bytes_hashed + authsz < len) { +debug ("extra data for hash: %lu\n" , + len - (bytes_hashed + authsz)); +efi_image_region_add (regs, efi + bytes_hashed, + efi + len - authsz,0 ); + } + +/* Return Certificates Table */ +if (authsz) { +if (len < authoff + authsz) { +debug ("%s: Size for auth too large: %u >= %zu\n" , + __func__, authsz, len - authoff); + goto err; + } +if (authsz <sizeof (*auth)) { +debug ("%s: Size for auth too small: %u < %zu\n" , + __func__, authsz,sizeof (*auth)); + goto err; + } + *auth = efi + authoff; + *auth_len = authsz; +debug ("WIN_CERTIFICATE: 0x%x, size: 0x%x\n" , authoff, authsz); + }else { + *auth =NULL ; + *auth_len =0 ; + } + + *regp = regs; + +return true; + +err: +free (regs); + +return false; +} + +/** +* efi_image_unsigned_authenticate - authenticate unsigned image with +* SHA256 hash +* @regs: List of regions to be verified +* +* If an image is not signed, it doesn't have a signature. In this case, +* its message digest is calculated and it will be compared with one of +* hash values stored in signature databases. +* +* Return: true if authenticated, false if not +*/ +static bool efi_image_unsigned_authenticate (struct efi_image_regions *regs) +{ +struct efi_signature_store *db =NULL , *dbx =NULL ; +bool ret = false; + + dbx =efi_sigstore_parse_sigdb (L"dbx" ); +if (!dbx) { +debug ("Getting signature database(dbx) failed\n" ); + goto out; + } + + db =efi_sigstore_parse_sigdb (L"db" ); +if (!db) { +debug ("Getting signature database(db) failed\n" ); + goto out; + } + +/* try black-list first */ +if (efi_signature_verify_with_sigdb (regs,NULL , dbx,NULL )) { +debug ("Image is not signed and rejected by \"dbx\"\n" ); + goto out; + } + +/* try white-list */ +if (efi_signature_verify_with_sigdb (regs,NULL , db,NULL )) + ret = true; +else +debug ("Image is not signed and not found in \"db\" or \"dbx\"\n" ); + +out: +efi_sigstore_free (db); +efi_sigstore_free (dbx); + +return ret; +} + +/** +* efi_image_authenticate - verify a signature of signed image +* @efi: Pointer to image +* @len: Size of @efi +* +* A signed image should have its signature stored in a table of its PE header. +* So if an image is signed and only if if its signature is verified using +* signature databases, an image is authenticated. +* If an image is not signed, its validity is checked by using +* efi_image_unsigned_authenticated(). +* TODO: +* When AuditMode==0, if the image's signature is not found in +* the authorized database, or is found in the forbidden database, +* the image will not be started and instead, information about it +* will be placed in this table. +* When AuditMode==1, an EFI_IMAGE_EXECUTION_INFO element is created +* in the EFI_IMAGE_EXECUTION_INFO_TABLE for every certificate found +* in the certificate table of every image that is validated. +* +* Return: true if authenticated, false if not +*/ +static bool efi_image_authenticate (void *efi,size_t len) +{ +struct efi_image_regions *regs =NULL ; +WIN_CERTIFICATE *wincerts =NULL , *wincert; +size_t wincerts_len; +struct pkcs7_message *msg =NULL ; +struct efi_signature_store *db =NULL , *dbx =NULL ; +struct x509_certificate *cert =NULL ; +bool ret = false; + +if (!efi_secure_boot_enabled ()) +return true; + +if (!efi_image_parse (efi, len, ®s, &wincerts, + &wincerts_len)) { +debug ("Parsing PE executable image failed\n" ); +return false; + } + +if (!wincerts) { +/* The image is not signed */ + ret =efi_image_unsigned_authenticate (regs); +free (regs); + +return ret; + } + +/* +* verify signature using db and dbx +*/ + db =efi_sigstore_parse_sigdb (L"db" ); +if (!db) { +debug ("Getting signature database(db) failed\n" ); + goto err; + } + + dbx =efi_sigstore_parse_sigdb (L"dbx" ); +if (!dbx) { +debug ("Getting signature database(dbx) failed\n" ); + goto err; + } + +/* go through WIN_CERTIFICATE list */ +for (wincert = wincerts; + (void *)wincert < (void *)wincerts + wincerts_len; + wincert = (void *)wincert +ALIGN (wincert->dwLength,8 )) { +if (wincert->dwLength <sizeof (*wincert)) { +debug ("%s: dwLength too small: %u < %zu\n" , + __func__, wincert->dwLength,sizeof (*wincert)); + goto err; + } + msg =pkcs7_parse_message ((void *)wincert +sizeof (*wincert), + wincert->dwLength -sizeof (*wincert)); +if (!msg) { +debug ("Parsing image's signature failed\n" ); + goto err; + } + +/* try black-list first */ +if (efi_signature_verify_with_sigdb (regs, msg, dbx,NULL )) { +debug ("Signature was rejected by \"dbx\"\n" ); + goto err; + } + +if (!efi_signature_verify_signers (msg, dbx)) { +debug ("Signer was rejected by \"dbx\"\n" ); + goto err; + }else { + ret = true; + } + +/* try white-list */ +if (!efi_signature_verify_with_sigdb (regs, msg, db, &cert)) { +debug ("Verifying signature with \"db\" failed\n" ); + goto err; + }else { + ret = true; + } + +if (!efi_signature_verify_cert (cert, dbx)) { +debug ("Certificate was rejected by \"dbx\"\n" ); + goto err; + }else { + ret = true; + } + } + +err: +x509_free_certificate (cert); +efi_sigstore_free (db); +efi_sigstore_free (dbx); +pkcs7_free_message (msg); +free (regs); + +return ret; +} +#else +static bool efi_image_authenticate (void *efi,size_t len) +{ +return true; +} +#endif /* CONFIG_EFI_SECURE_BOOT */ +/** * efi_load_pe() - relocate EFI binary *
lib/efi_loader/efi_image_loader.c static void efi_set_code_and_data_type( · line 583
Quoted 3 lines
lib/efi_loader/efi_image_loader.c +0
-0@@ -217,7 +581,8 @@ static void efi_set_code_and_data_type(* @loaded_image_info: loaded image protocol * Return: status code */
./lib/efi_loader/efi_image_loader.c:588: warning: Function parameter or member 'efi_size' not described in 'efi_load_pe'
Best regards
Heinrich
Quoted 165 lines
lib/efi_loader/efi_image_loader.c +83
-13-efi_status_t efi_load_pe (struct efi_loaded_image_obj *handle,void *efi, +efi_status_t efi_load_pe (struct efi_loaded_image_obj *handle, +void *efi,size_t efi_size,struct efi_loaded_image *loaded_image_info) {IMAGE_NT_HEADERS32 *nt;@@ -232,17 +597,57 @@ efi_status_t efi_load_pe(struct efi_loaded_image_obj *handle, void *efi,uint64_t image_base;unsigned long virt_size =0 ;int supported =0 ; +void *new_efi =NULL ; +size_t new_efi_size; +efi_status_t ret; + +/* +* Size must be 8-byte aligned and the trailing bytes must be +* zero'ed. Otherwise hash value may be incorrect. +*/ +if (efi_size &0x7 ) { + new_efi_size = (efi_size +0x7 ) & ~0x7ULL ; + new_efi =calloc (new_efi_size,1 ); +if (!new_efi) +return EFI_OUT_OF_RESOURCES ; +memcpy (new_efi, efi, efi_size); + efi = new_efi; + efi_size = new_efi_size; + } + +/* Sanity check for a file header */ +if (efi_size <sizeof (*dos)) { +printf ("%s: Truncated DOS Header\n" , __func__); + ret =EFI_LOAD_ERROR ; + goto err; + } dos = efi;if (dos->e_magic !=IMAGE_DOS_SIGNATURE ) {printf ("%s: Invalid DOS Signature\n" , __func__); -return EFI_LOAD_ERROR ; + ret =EFI_LOAD_ERROR ; + goto err; + } + +/* assume sizeof(IMAGE_NT_HEADERS32) <= sizeof(IMAGE_NT_HEADERS64) */ +if (efi_size < dos->e_lfanew +sizeof (IMAGE_NT_HEADERS32 )) { +printf ("%s: Invalid offset for Extended Header\n" , __func__); + ret =EFI_LOAD_ERROR ; + goto err; } nt = (void *) ((char *)efi + dos->e_lfanew); +if ((nt->OptionalHeader.Magic ==IMAGE_NT_OPTIONAL_HDR64_MAGIC ) && + (efi_size < dos->e_lfanew +sizeof (IMAGE_NT_HEADERS64 ))) { +printf ("%s: Invalid offset for Extended Header\n" , __func__); + ret =EFI_LOAD_ERROR ; + goto err; + } +if (nt->Signature !=IMAGE_NT_SIGNATURE ) {printf ("%s: Invalid NT Signature\n" , __func__); -return EFI_LOAD_ERROR ; + ret =EFI_LOAD_ERROR ; + goto err; }for (i =0 ; machines[i]; i++)@@ -254,14 +659,29 @@ efi_status_t efi_load_pe(struct efi_loaded_image_obj *handle, void *efi,if (!supported) {printf ("%s: Machine type 0x%04x is not supported\n" , __func__, nt->FileHeader.Machine); -return EFI_LOAD_ERROR ; + ret =EFI_LOAD_ERROR ; + goto err; } -/* Calculate upper virtual address boundary */ num_sections = nt->FileHeader.NumberOfSections; sections = (void *)&nt->OptionalHeader + nt->FileHeader.SizeOfOptionalHeader; +if (efi_size < ((void *)sections +sizeof (sections[0 ]) * num_sections + - efi)) { +printf ("%s: Invalid number of sections: %d\n" , + __func__, num_sections); + ret =EFI_LOAD_ERROR ; + goto err; + } + +/* Authenticate an image */ +if (efi_image_authenticate (efi, efi_size)) + handle->auth_status =EFI_IMAGE_AUTH_PASSED ; +else + handle->auth_status =EFI_IMAGE_AUTH_FAILED ; + +/* Calculate upper virtual address boundary */ for (i = num_sections -1 ; i >=0 ; i--) {IMAGE_SECTION_HEADER *sec = §ions[i]; virt_size = max_t(unsigned long, virt_size,@@ -280,7 +700,8 @@ efi_status_t efi_load_pe(struct efi_loaded_image_obj *handle, void *efi,if (!efi_reloc) {printf ("%s: Could not allocate %lu bytes\n" , __func__, virt_size); -return EFI_OUT_OF_RESOURCES ; + ret =EFI_OUT_OF_RESOURCES ; + goto err; } handle->entry = efi_reloc + opt->AddressOfEntryPoint; rel_size = opt->DataDirectory[rel_idx].Size;@@ -296,7 +717,8 @@ efi_status_t efi_load_pe(struct efi_loaded_image_obj *handle, void *efi,if (!efi_reloc) {printf ("%s: Could not allocate %lu bytes\n" , __func__, virt_size); -return EFI_OUT_OF_RESOURCES ; + ret =EFI_OUT_OF_RESOURCES ; + goto err; } handle->entry = efi_reloc + opt->AddressOfEntryPoint; rel_size = opt->DataDirectory[rel_idx].Size;@@ -305,13 +727,16 @@ efi_status_t efi_load_pe(struct efi_loaded_image_obj *handle, void *efi, }else {printf ("%s: Invalid optional header magic %x\n" , __func__, nt->OptionalHeader.Magic); -return EFI_LOAD_ERROR ; + ret =EFI_LOAD_ERROR ; + goto err; }/* Copy PE headers */ -memcpy (efi_reloc, efi,sizeof (*dos) +sizeof (*nt) - + nt->FileHeader.SizeOfOptionalHeader - + num_sections *sizeof (IMAGE_SECTION_HEADER )); +memcpy (efi_reloc, efi, +sizeof (*dos) + +sizeof (*nt) + + nt->FileHeader.SizeOfOptionalHeader + + num_sections *sizeof (IMAGE_SECTION_HEADER ));/* Load sections into RAM */ for (i = num_sections -1 ; i >=0 ; i--) {@@ -328,7 +753,8 @@ efi_status_t efi_load_pe(struct efi_loaded_image_obj *handle, void *efi, (unsigned long)image_base) != EFI_SUCCESS) {efi_free_pages ((uintptr_t ) efi_reloc, (virt_size +EFI_PAGE_MASK ) >>EFI_PAGE_SHIFT ); -return EFI_LOAD_ERROR ; + ret =EFI_LOAD_ERROR ; + goto err; }/* Flush cache */ @@ -340,5 +766,13 @@ efi_status_t efi_load_pe(struct efi_loaded_image_obj *handle, void *efi, loaded_image_info->image_base = efi_reloc; loaded_image_info->image_size = virt_size; -return EFI_SUCCESS ; +if (handle->auth_status ==EFI_IMAGE_AUTH_PASSED ) +return EFI_SUCCESS ; +else +return EFI_SECURITY_VIOLATION ; + +err: +free (new_efi); + +return ret; }
PATCH v5 7/16 efi_loader: image_loader: support image authentication
Quoted 147 lines · Heinrich Schuchardt
Quoted 133 lines · AKASHI Takahiro
With this commit, image validation can be enforced, as UEFI specification section 32.5 describes, if
CONFIG_EFI_SECURE_BOOTis enabled.Currently we support
* authentication based on db and dbx,
so dbx-validated image will always be rejected.
* following signature types:
EFI_CERT_SHA256_GUID(SHA256 digest for unsigned images)EFI_CERT_X509_GUID(x509 certificate for signed images)
Timestamp-based certificate revocation is not supported here.Internally, authentication data is stored in one of certificates tables of PE image (See
efi_image_parse()) and will be verified byefi_image_authenticate()before loading a given image.It seems that UEFI specification defines the verification process in a bit ambiguous way. I tried to implement it as closely to as EDK2 does.
Signed-off-by AKASHI Takahiro <[email protected]>--- include/efi_loader.h | 13 +- lib/efi_loader/efi_boottime.c | 10 +- lib/efi_loader/efi_image_loader.c | 460 +++++++++++++++++++++++++++++- 3 files changed, 467 insertions(+), 16 deletions(-)include/efi_loader.h +12
-1@@ -11,6 +11,7 @@#include <common.h> #include <part_efi.h> #include <efi_api.h> +#include <pe.h> static inline int guidcmp (const void *g1,const void *g2) {@@ -263,6 +264,11 @@ struct efi_object {enum efi_object_type type; }; +enum efi_image_auth_status { +EFI_IMAGE_AUTH_FAILED =0 , +EFI_IMAGE_AUTH_PASSED , +}; +/** * struct efi_loaded_image_obj - handle of a loaded image * @@ -282,6 +288,7 @@ struct efi_loaded_image_obj { EFIAPI efi_status_t (*entry)(efi_handle_t image_handle,struct efi_system_table *st);u16 image_type; +enum efi_image_auth_status auth_status; };/** @@ -414,7 +421,8 @@ efi_status_t efi_set_watchdog(unsigned long timeout);/* Called from places to check whether a timer expired */ void efi_timer_check (void );/* PE loader implementation */ -efi_status_t efi_load_pe (struct efi_loaded_image_obj *handle,void *efi, +efi_status_t efi_load_pe (struct efi_loaded_image_obj *handle, +void *efi,size_t efi_size,struct efi_loaded_image *loaded_image_info);/* Called once to store the pristine gd pointer */ void efi_save_gd (void );@@ -741,6 +749,9 @@ void efi_sigstore_free(struct efi_signature_store *sigstore);struct efi_signature_store *efi_sigstore_parse_sigdb (u16 *name);bool efi_secure_boot_enabled (void ); + +bool efi_image_parse (void *efi,size_t len,struct efi_image_regions **regp, +WIN_CERTIFICATE **auth,size_t *auth_len);#endif /* CONFIG_EFI_SECURE_BOOT */ #else /* CONFIG_IS_ENABLED(EFI_LOADER) */ lib/efi_loader/efi_boottime.c +8
-2@@ -1882,12 +1882,12 @@ efi_status_t EFIAPI efi_load_image(bool boot_policy,efi_dp_split_file_path (file_path, &dp, &fp); ret =efi_setup_loaded_image (dp, fp, image_obj, &info);if (ret ==EFI_SUCCESS ) - ret =efi_load_pe (*image_obj, dest_buffer, info); + ret =efi_load_pe (*image_obj, dest_buffer, source_size, info);if (!source_buffer)/* Release buffer to which file was loaded */ efi_free_pages ((uintptr_t )dest_buffer,efi_size_in_pages (source_size)); -if (ret ==EFI_SUCCESS ) { +if (ret ==EFI_SUCCESS || ret ==EFI_SECURITY_VIOLATION ) { info->system_table = &systab; info->parent_handle = parent_image; }else {@@ -2885,10 +2885,16 @@ efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,EFI_ENTRY ("%p, %p, %p" , image_handle, exit_data_size, exit_data); +if (!efi_search_obj (image_handle)) +return EFI_EXIT (EFI_INVALID_PARAMETER ); +/* Check parameters */ if (image_obj->header.type !=EFI_OBJECT_TYPE_LOADED_IMAGE )return EFI_EXIT (EFI_INVALID_PARAMETER ); +if (image_obj->auth_status !=EFI_IMAGE_AUTH_PASSED ) +return EFI_EXIT (EFI_SECURITY_VIOLATION ); + ret =EFI_CALL (efi_open_protocol (image_handle, &efi_guid_loaded_image, &info,NULL ,NULL ,EFI_OPEN_PROTOCOL_GET_PROTOCOL ));lib/efi_loader/efi_image_loader.c +3
-0@@ -10,7 +10,10 @@#include <common.h> #include <cpu_func.h> #include <efi_loader.h> +#include <malloc.h> #include <pe.h> +#include <sort.h> +#include "../lib/crypto/pkcs7_parser.h" const efi_guid_t efi_global_variable_guid =EFI_GLOBAL_VARIABLE_GUID ;const efi_guid_t efi_guid_device_path =EFI_DEVICE_PATH_PROTOCOL_GUID ;@@ -206,6 +209,367 @@ static void efi_set_code_and_data_type( } }lib/efi_loader/efi_image_loader.c static void efi_set_code_and_data_type( · line 221
Quoted 10 lines
lib/efi_loader/efi_image_loader.c +10
-0+#ifdef CONFIG_EFI_SECURE_BOOT +/** +* cmp_pe_section - compare two sections +* @arg1: Pointer to first section +* @arg2: Pointer to second section +* +* Compare two sections in PE image. +* +* Return: -1, 0, 1 respectively if arg1 < arg2, arg1 == arg2 or +arg1 > arg2 make htmldocs creates a warning
./lib/efi_loader/efi_image_loader.c:222: warning: bad line:
Quoted 4 lines
arg1 > arg2All warnings in 'make htmldocs' will be treated as errors in an upcoming 'make htmldocs' build step for Travis CI.
Okay, I fixed all of them in efi_image_loader.c.
Thanks,
-Takahiro Akashi
Quoted 536 lines
Quoted 10 lines
lib/efi_loader/efi_image_loader.c +10
-0+*/ +static int cmp_pe_section (const void *arg1,const void *arg2) +{ +const IMAGE_SECTION_HEADER *section1 = arg1, *section2 = arg2; + +if (section1->VirtualAddress < section2->VirtualAddress) +return -1 ; +else if (section1->VirtualAddress == section2->VirtualAddress) +return 0 ; +else lib/efi_loader/efi_image_loader.c static void efi_set_code_and_data_type( · line 241
Quoted 10 lines
lib/efi_loader/efi_image_loader.c +10
-0+return 1 ; +} + +/** +* efi_image_parse - parse a PE image +* @efi: Pointer to image +* @len: Size of @efi +* @regs: Pointer to a list of regions +* @auth: Pointer to a pointer to authentication data in PE +* @auth_len: Size of @auth
./lib/efi_loader/efi_image_loader.c:253: warning: Function parameter or member 'regp' not described in 'efi_image_parse'Quoted 335 lines
lib/efi_loader/efi_image_loader.c +331
-0+* +* Parse image binary in PE32(+) format, assuming that sanity of PE image +* has been checked by a caller. +* On success, an address of authentication data in @efi and its size will +* be returned in @auth and @auth_len, respectively. +* +* Return: true on success, false on error +*/ +bool efi_image_parse (void *efi,size_t len,struct efi_image_regions **regp, +WIN_CERTIFICATE **auth,size_t *auth_len) +{ +struct efi_image_regions *regs; +IMAGE_DOS_HEADER *dos; +IMAGE_NT_HEADERS32 *nt; +IMAGE_SECTION_HEADER *sections, **sorted; +int num_regions, num_sections, i; +int ctidx =IMAGE_DIRECTORY_ENTRY_SECURITY ; +u32 align, size, authsz, authoff; +size_t bytes_hashed; + + dos = (void *)efi; + nt = (void *)(efi + dos->e_lfanew); + +/* +* Count maximum number of regions to be digested. +* We don't have to have an exact number here. +* See efi_image_region_add()'s in parsing below. +*/ + num_regions =3 ;/* for header */ + num_regions += nt->FileHeader.NumberOfSections; + num_regions++;/* for extra */ + + regs =calloc (sizeof (*regs) +sizeof (struct image_region ) * num_regions, +1 ); +if (!regs) + goto err; + regs->max = num_regions; + +/* +* Collect data regions for hash calculation +* 1. File headers +*/ +if (nt->OptionalHeader.Magic ==IMAGE_NT_OPTIONAL_HDR64_MAGIC ) { +IMAGE_NT_HEADERS64 *nt64 = (void *)nt; +IMAGE_OPTIONAL_HEADER64 *opt = &nt64->OptionalHeader; + +/* Skip CheckSum */ +efi_image_region_add (regs, efi, &opt->CheckSum,0 ); +if (nt64->OptionalHeader.NumberOfRvaAndSizes <= ctidx) { +efi_image_region_add (regs, + &opt->CheckSum +1 , + efi + opt->SizeOfHeaders,0 ); + }else { +/* Skip Certificates Table */ +efi_image_region_add (regs, + &opt->CheckSum +1 , + &opt->DataDirectory[ctidx],0 ); +efi_image_region_add (regs, + &opt->DataDirectory[ctidx] +1 , + efi + opt->SizeOfHeaders,0 ); + } + + bytes_hashed = opt->SizeOfHeaders; + align = opt->FileAlignment; + authoff = opt->DataDirectory[ctidx].VirtualAddress; + authsz = opt->DataDirectory[ctidx].Size; + }else if (nt->OptionalHeader.Magic ==IMAGE_NT_OPTIONAL_HDR32_MAGIC ) { +IMAGE_OPTIONAL_HEADER32 *opt = &nt->OptionalHeader; + +efi_image_region_add (regs, efi, &opt->CheckSum,0 ); +efi_image_region_add (regs, &opt->CheckSum +1 , + &opt->DataDirectory[ctidx],0 ); +efi_image_region_add (regs, &opt->DataDirectory[ctidx] +1 , + efi + opt->SizeOfHeaders,0 ); + + bytes_hashed = opt->SizeOfHeaders; + align = opt->FileAlignment; + authoff = opt->DataDirectory[ctidx].VirtualAddress; + authsz = opt->DataDirectory[ctidx].Size; + }else { +debug ("%s: Invalid optional header magic %x\n" , __func__, + nt->OptionalHeader.Magic); + goto err; + } + +/* 2. Sections */ + num_sections = nt->FileHeader.NumberOfSections; + sections = (void *)((uint8_t *)&nt->OptionalHeader + + nt->FileHeader.SizeOfOptionalHeader); + sorted =calloc (sizeof (IMAGE_SECTION_HEADER *), num_sections); +if (!sorted) { +debug ("%s: Out of memory\n" , __func__); + goto err; + } + +/* +* Make sure the section list is in ascending order. +*/ +for (i =0 ; i < num_sections; i++) + sorted[i] = §ions[i]; +qsort (sorted, num_sections,sizeof (§ions[0 ]), cmp_pe_section); + +for (i =0 ; i < num_sections; i++) { +if (!sorted[i]->SizeOfRawData) +continue ; + + size = (sorted[i]->SizeOfRawData + align -1 ) & ~(align -1 ); +efi_image_region_add (regs, efi + sorted[i]->PointerToRawData, + efi + sorted[i]->PointerToRawData + size, +0 ); +debug ("section[%d](%s): raw: 0x%x-0x%x, virt: %x-%x\n" , + i, sorted[i]->Name, + sorted[i]->PointerToRawData, + sorted[i]->PointerToRawData + size, + sorted[i]->VirtualAddress, + sorted[i]->VirtualAddress + + sorted[i]->Misc.VirtualSize); + + bytes_hashed += size; + } +free (sorted); + +/* 3. Extra data excluding Certificates Table */ +if (bytes_hashed + authsz < len) { +debug ("extra data for hash: %lu\n" , + len - (bytes_hashed + authsz)); +efi_image_region_add (regs, efi + bytes_hashed, + efi + len - authsz,0 ); + } + +/* Return Certificates Table */ +if (authsz) { +if (len < authoff + authsz) { +debug ("%s: Size for auth too large: %u >= %zu\n" , + __func__, authsz, len - authoff); + goto err; + } +if (authsz <sizeof (*auth)) { +debug ("%s: Size for auth too small: %u < %zu\n" , + __func__, authsz,sizeof (*auth)); + goto err; + } + *auth = efi + authoff; + *auth_len = authsz; +debug ("WIN_CERTIFICATE: 0x%x, size: 0x%x\n" , authoff, authsz); + }else { + *auth =NULL ; + *auth_len =0 ; + } + + *regp = regs; + +return true; + +err: +free (regs); + +return false; +} + +/** +* efi_image_unsigned_authenticate - authenticate unsigned image with +* SHA256 hash +* @regs: List of regions to be verified +* +* If an image is not signed, it doesn't have a signature. In this case, +* its message digest is calculated and it will be compared with one of +* hash values stored in signature databases. +* +* Return: true if authenticated, false if not +*/ +static bool efi_image_unsigned_authenticate (struct efi_image_regions *regs) +{ +struct efi_signature_store *db =NULL , *dbx =NULL ; +bool ret = false; + + dbx =efi_sigstore_parse_sigdb (L"dbx" ); +if (!dbx) { +debug ("Getting signature database(dbx) failed\n" ); + goto out; + } + + db =efi_sigstore_parse_sigdb (L"db" ); +if (!db) { +debug ("Getting signature database(db) failed\n" ); + goto out; + } + +/* try black-list first */ +if (efi_signature_verify_with_sigdb (regs,NULL , dbx,NULL )) { +debug ("Image is not signed and rejected by \"dbx\"\n" ); + goto out; + } + +/* try white-list */ +if (efi_signature_verify_with_sigdb (regs,NULL , db,NULL )) + ret = true; +else +debug ("Image is not signed and not found in \"db\" or \"dbx\"\n" ); + +out: +efi_sigstore_free (db); +efi_sigstore_free (dbx); + +return ret; +} + +/** +* efi_image_authenticate - verify a signature of signed image +* @efi: Pointer to image +* @len: Size of @efi +* +* A signed image should have its signature stored in a table of its PE header. +* So if an image is signed and only if if its signature is verified using +* signature databases, an image is authenticated. +* If an image is not signed, its validity is checked by using +* efi_image_unsigned_authenticated(). +* TODO: +* When AuditMode==0, if the image's signature is not found in +* the authorized database, or is found in the forbidden database, +* the image will not be started and instead, information about it +* will be placed in this table. +* When AuditMode==1, an EFI_IMAGE_EXECUTION_INFO element is created +* in the EFI_IMAGE_EXECUTION_INFO_TABLE for every certificate found +* in the certificate table of every image that is validated. +* +* Return: true if authenticated, false if not +*/ +static bool efi_image_authenticate (void *efi,size_t len) +{ +struct efi_image_regions *regs =NULL ; +WIN_CERTIFICATE *wincerts =NULL , *wincert; +size_t wincerts_len; +struct pkcs7_message *msg =NULL ; +struct efi_signature_store *db =NULL , *dbx =NULL ; +struct x509_certificate *cert =NULL ; +bool ret = false; + +if (!efi_secure_boot_enabled ()) +return true; + +if (!efi_image_parse (efi, len, ®s, &wincerts, + &wincerts_len)) { +debug ("Parsing PE executable image failed\n" ); +return false; + } + +if (!wincerts) { +/* The image is not signed */ + ret =efi_image_unsigned_authenticate (regs); +free (regs); + +return ret; + } + +/* +* verify signature using db and dbx +*/ + db =efi_sigstore_parse_sigdb (L"db" ); +if (!db) { +debug ("Getting signature database(db) failed\n" ); + goto err; + } + + dbx =efi_sigstore_parse_sigdb (L"dbx" ); +if (!dbx) { +debug ("Getting signature database(dbx) failed\n" ); + goto err; + } + +/* go through WIN_CERTIFICATE list */ +for (wincert = wincerts; + (void *)wincert < (void *)wincerts + wincerts_len; + wincert = (void *)wincert +ALIGN (wincert->dwLength,8 )) { +if (wincert->dwLength <sizeof (*wincert)) { +debug ("%s: dwLength too small: %u < %zu\n" , + __func__, wincert->dwLength,sizeof (*wincert)); + goto err; + } + msg =pkcs7_parse_message ((void *)wincert +sizeof (*wincert), + wincert->dwLength -sizeof (*wincert)); +if (!msg) { +debug ("Parsing image's signature failed\n" ); + goto err; + } + +/* try black-list first */ +if (efi_signature_verify_with_sigdb (regs, msg, dbx,NULL )) { +debug ("Signature was rejected by \"dbx\"\n" ); + goto err; + } + +if (!efi_signature_verify_signers (msg, dbx)) { +debug ("Signer was rejected by \"dbx\"\n" ); + goto err; + }else { + ret = true; + } + +/* try white-list */ +if (!efi_signature_verify_with_sigdb (regs, msg, db, &cert)) { +debug ("Verifying signature with \"db\" failed\n" ); + goto err; + }else { + ret = true; + } + +if (!efi_signature_verify_cert (cert, dbx)) { +debug ("Certificate was rejected by \"dbx\"\n" ); + goto err; + }else { + ret = true; + } + } + +err: +x509_free_certificate (cert); +efi_sigstore_free (db); +efi_sigstore_free (dbx); +pkcs7_free_message (msg); +free (regs); + +return ret; +} +#else +static bool efi_image_authenticate (void *efi,size_t len) +{ +return true; +} +#endif /* CONFIG_EFI_SECURE_BOOT */ +/** * efi_load_pe() - relocate EFI binary * lib/efi_loader/efi_image_loader.c static void efi_set_code_and_data_type( · line 583
Quoted 3 lines
lib/efi_loader/efi_image_loader.c +0
-0@@ -217,7 +581,8 @@ static void efi_set_code_and_data_type(* @loaded_image_info: loaded image protocol * Return: status code */
./lib/efi_loader/efi_image_loader.c:588: warning: Function parameter or member 'efi_size' not described in 'efi_load_pe'Best regards
Heinrich
Quoted 165 lines
lib/efi_loader/efi_image_loader.c +83
-13-efi_status_t efi_load_pe (struct efi_loaded_image_obj *handle,void *efi, +efi_status_t efi_load_pe (struct efi_loaded_image_obj *handle, +void *efi,size_t efi_size,struct efi_loaded_image *loaded_image_info) {IMAGE_NT_HEADERS32 *nt;@@ -232,17 +597,57 @@ efi_status_t efi_load_pe(struct efi_loaded_image_obj *handle, void *efi,uint64_t image_base;unsigned long virt_size =0 ;int supported =0 ; +void *new_efi =NULL ; +size_t new_efi_size; +efi_status_t ret; + +/* +* Size must be 8-byte aligned and the trailing bytes must be +* zero'ed. Otherwise hash value may be incorrect. +*/ +if (efi_size &0x7 ) { + new_efi_size = (efi_size +0x7 ) & ~0x7ULL ; + new_efi =calloc (new_efi_size,1 ); +if (!new_efi) +return EFI_OUT_OF_RESOURCES ; +memcpy (new_efi, efi, efi_size); + efi = new_efi; + efi_size = new_efi_size; + } + +/* Sanity check for a file header */ +if (efi_size <sizeof (*dos)) { +printf ("%s: Truncated DOS Header\n" , __func__); + ret =EFI_LOAD_ERROR ; + goto err; + } dos = efi;if (dos->e_magic !=IMAGE_DOS_SIGNATURE ) {printf ("%s: Invalid DOS Signature\n" , __func__); -return EFI_LOAD_ERROR ; + ret =EFI_LOAD_ERROR ; + goto err; + } + +/* assume sizeof(IMAGE_NT_HEADERS32) <= sizeof(IMAGE_NT_HEADERS64) */ +if (efi_size < dos->e_lfanew +sizeof (IMAGE_NT_HEADERS32 )) { +printf ("%s: Invalid offset for Extended Header\n" , __func__); + ret =EFI_LOAD_ERROR ; + goto err; } nt = (void *) ((char *)efi + dos->e_lfanew); +if ((nt->OptionalHeader.Magic ==IMAGE_NT_OPTIONAL_HDR64_MAGIC ) && + (efi_size < dos->e_lfanew +sizeof (IMAGE_NT_HEADERS64 ))) { +printf ("%s: Invalid offset for Extended Header\n" , __func__); + ret =EFI_LOAD_ERROR ; + goto err; + } +if (nt->Signature !=IMAGE_NT_SIGNATURE ) {printf ("%s: Invalid NT Signature\n" , __func__); -return EFI_LOAD_ERROR ; + ret =EFI_LOAD_ERROR ; + goto err; }for (i =0 ; machines[i]; i++)@@ -254,14 +659,29 @@ efi_status_t efi_load_pe(struct efi_loaded_image_obj *handle, void *efi,if (!supported) {printf ("%s: Machine type 0x%04x is not supported\n" , __func__, nt->FileHeader.Machine); -return EFI_LOAD_ERROR ; + ret =EFI_LOAD_ERROR ; + goto err; } -/* Calculate upper virtual address boundary */ num_sections = nt->FileHeader.NumberOfSections; sections = (void *)&nt->OptionalHeader + nt->FileHeader.SizeOfOptionalHeader; +if (efi_size < ((void *)sections +sizeof (sections[0 ]) * num_sections + - efi)) { +printf ("%s: Invalid number of sections: %d\n" , + __func__, num_sections); + ret =EFI_LOAD_ERROR ; + goto err; + } + +/* Authenticate an image */ +if (efi_image_authenticate (efi, efi_size)) + handle->auth_status =EFI_IMAGE_AUTH_PASSED ; +else + handle->auth_status =EFI_IMAGE_AUTH_FAILED ; + +/* Calculate upper virtual address boundary */ for (i = num_sections -1 ; i >=0 ; i--) {IMAGE_SECTION_HEADER *sec = §ions[i]; virt_size = max_t(unsigned long, virt_size,@@ -280,7 +700,8 @@ efi_status_t efi_load_pe(struct efi_loaded_image_obj *handle, void *efi,if (!efi_reloc) {printf ("%s: Could not allocate %lu bytes\n" , __func__, virt_size); -return EFI_OUT_OF_RESOURCES ; + ret =EFI_OUT_OF_RESOURCES ; + goto err; } handle->entry = efi_reloc + opt->AddressOfEntryPoint; rel_size = opt->DataDirectory[rel_idx].Size;@@ -296,7 +717,8 @@ efi_status_t efi_load_pe(struct efi_loaded_image_obj *handle, void *efi,if (!efi_reloc) {printf ("%s: Could not allocate %lu bytes\n" , __func__, virt_size); -return EFI_OUT_OF_RESOURCES ; + ret =EFI_OUT_OF_RESOURCES ; + goto err; } handle->entry = efi_reloc + opt->AddressOfEntryPoint; rel_size = opt->DataDirectory[rel_idx].Size;@@ -305,13 +727,16 @@ efi_status_t efi_load_pe(struct efi_loaded_image_obj *handle, void *efi, }else {printf ("%s: Invalid optional header magic %x\n" , __func__, nt->OptionalHeader.Magic); -return EFI_LOAD_ERROR ; + ret =EFI_LOAD_ERROR ; + goto err; }/* Copy PE headers */ -memcpy (efi_reloc, efi,sizeof (*dos) +sizeof (*nt) - + nt->FileHeader.SizeOfOptionalHeader - + num_sections *sizeof (IMAGE_SECTION_HEADER )); +memcpy (efi_reloc, efi, +sizeof (*dos) + +sizeof (*nt) + + nt->FileHeader.SizeOfOptionalHeader + + num_sections *sizeof (IMAGE_SECTION_HEADER ));/* Load sections into RAM */ for (i = num_sections -1 ; i >=0 ; i--) {@@ -328,7 +753,8 @@ efi_status_t efi_load_pe(struct efi_loaded_image_obj *handle, void *efi, (unsigned long)image_base) != EFI_SUCCESS) {efi_free_pages ((uintptr_t ) efi_reloc, (virt_size +EFI_PAGE_MASK ) >>EFI_PAGE_SHIFT ); -return EFI_LOAD_ERROR ; + ret =EFI_LOAD_ERROR ; + goto err; }/* Flush cache */ @@ -340,5 +766,13 @@ efi_status_t efi_load_pe(struct efi_loaded_image_obj *handle, void *efi, loaded_image_info->image_base = efi_reloc; loaded_image_info->image_size = virt_size; -return EFI_SUCCESS ; +if (handle->auth_status ==EFI_IMAGE_AUTH_PASSED ) +return EFI_SUCCESS ; +else +return EFI_SECURITY_VIOLATION ; + +err: +free (new_efi); + +return ret; }
PATCH v5 8/16 efi_loader: set up secure boot
In reply to AKASHI TakahiroThe following variable is exported as UEFI specification defines:
SignatureSupport: array of GUIDs representing the type of signatures
supported by the platform firmware
--- lib/efi_loader/efi_setup.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+)
lib/efi_loader/efi_setup.c +38 -0
@@ -82,6 +82,39 @@ out:return ret; } +#ifdef CONFIG_EFI_SECURE_BOOT +/** +* efi_init_secure_boot - initialize secure boot state +* +* Return: EFI_SUCCESS on success, status code (negative) on error +*/ +static efi_status_t efi_init_secure_boot (void ) +{ +efi_guid_t signature_types[] = { +EFI_CERT_SHA256_GUID , +EFI_CERT_X509_GUID , + }; +efi_status_t ret; + +/* TODO: read-only */ + ret =EFI_CALL (efi_set_variable (L"SignatureSupport" , + &efi_global_variable_guid, +EFI_VARIABLE_BOOTSERVICE_ACCESS + |EFI_VARIABLE_RUNTIME_ACCESS , +sizeof (signature_types), + &signature_types)); +if (ret !=EFI_SUCCESS ) +printf ("EFI: cannot initialize SignatureSupport variable\n" ); + +return ret; +} +#else +static efi_status_t efi_init_secure_boot (void ) +{ +return EFI_SUCCESS ; +} +#endif /* CONFIG_EFI_SECURE_BOOT */ +/** * efi_init_obj_list() - Initialize and populate EFI object list *
@@ -122,6 +155,11 @@ efi_status_t efi_init_obj_list(void)if (ret !=EFI_SUCCESS ) goto out; +/* Secure boot */ + ret =efi_init_secure_boot (); +if (ret !=EFI_SUCCESS ) + goto out; +/* Indicate supported runtime services */ ret =efi_init_runtime_supported ();if (ret !=EFI_SUCCESS )
Signature · 1 line
2.24.0
PATCH v5 9/16 cmd: env: use appropriate guid for authenticated UEFI variable
In reply to AKASHI TakahiroA signature database variable is associated with a specific guid. For convenience, if user doesn't supply any guid info, "env set|print -e" should complement it.
--- cmd/nvedit_efi.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-)
cmd/nvedit_efi.c +14 -4
@@ -41,6 +41,11 @@ static const struct { } efi_guid_text[] = { /* signature database */ {EFI_GLOBAL_VARIABLE_GUID, "EFI_GLOBAL_VARIABLE_GUID"}, + {EFI_IMAGE_SECURITY_DATABASE_GUID, "EFI_IMAGE_SECURITY_DATABASE_GUID"}, + /* certificate type */ + {EFI_CERT_SHA256_GUID, "EFI_CERT_SHA256_GUID"}, + {EFI_CERT_X509_GUID, "EFI_CERT_X509_GUID"}, + {EFI_CERT_TYPE_PKCS7_GUID, "EFI_CERT_TYPE_PKCS7_GUID"}, }; /* "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" */
@@ -525,9 +530,9 @@ int do_env_set_efi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])if (*ep !=',' )return CMD_RET_USAGE ; +/* 0 should be allowed for delete */ size =simple_strtoul (++ep,NULL ,16 ); -if (!size) -return CMD_RET_FAILURE ; + value_on_memory = true; }else if (!strcmp (argv[0 ],"-v" )) { verbose = true;
@@ -539,8 +544,13 @@ int do_env_set_efi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])return CMD_RET_USAGE ; var_name = argv[0 ]; -if (default_guid) - guid = efi_global_variable_guid; +if (default_guid) { +if (!strcmp (var_name,"db" ) || !strcmp (var_name,"dbx" ) || + !strcmp (var_name,"dbt" )) + guid = efi_guid_image_security_database; +else + guid = efi_global_variable_guid; + }if (verbose) { printf("GUID: %s\n", efi_guid_to_str((const efi_guid_t *)
Signature · 1 line
2.24.0
PATCH v5 10/16 cmd: env: add "-at" option to "env set -e" command
In reply to AKASHI TakahiroWith "-at" option, EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS will be passed to SetVariable() to authenticate the variable.
--- cmd/nvedit.c | 5 +++-- cmd/nvedit_efi.c | 5 ++++- 2 files changed, 7 insertions(+), 3 deletions(-)
cmd/nvedit.c +3 -2
@@ -1417,7 +1417,7 @@ static char env_help_text[] =#endif #endif #if defined(CONFIG_CMD_NVEDIT_EFI ) -"env set -e [-nv][-bs][-rt][-a][-i addr,size][-v] name [arg ...]\n" +"env set -e [-nv][-bs][-rt][-at][-a][-i addr,size][-v] name [arg ...]\n" " - set UEFI variable; unset if '-i' or 'arg' not specified\n"#endif "env set [-f] name [arg ...]\n" ;
@@ -1479,13 +1479,14 @@ U_BOOT_CMD_COMPLETE( setenv, CONFIG_SYS_MAXARGS, 0, do_env_set, "set environment variables", #if defined(CONFIG_CMD_NVEDIT_EFI) -"-e [-guid guid][-nv][-bs][-rt][-a][-v]\n" +"-e [-guid guid][-nv][-bs][-rt][-at][-a][-v]\n" " [-i addr,size name], or [name [value ...]]\n" " - set UEFI variable 'name' to 'value' ...'\n" " \"-guid\": set vendor guid\n" " \"-nv\": set non-volatile attribute\n" " \"-bs\": set boot-service attribute\n" " \"-rt\": set runtime attribute\n" +" \"-at\": set time-based authentication attribute\n" " \"-a\": append-write\n" " \"-i addr,size\": use <addr,size> as variable's value\n" " \"-v\": verbose message\n"
cmd/nvedit_efi.c +4 -1
@@ -458,7 +458,7 @@ out:* Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE * * This function is for "env set -e" or "setenv -e" command: -* => env set -e [-guid guid][-nv][-bs][-rt][-a][-v] +* => env set -e [-guid guid][-nv][-bs][-rt][-at][-a][-v] * [-i address,size] var, or * var [value ...] * Encode values specified and set given UEFI variable.
@@ -517,6 +517,9 @@ int do_env_set_efi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) attributes |=EFI_VARIABLE_RUNTIME_ACCESS ; }else if (!strcmp (argv[0 ],"-nv" )) { attributes |=EFI_VARIABLE_NON_VOLATILE ; + }else if (!strcmp (argv[0 ],"-at" )) { + attributes |= +EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS ; }else if (!strcmp (argv[0 ],"-a" )) { attributes |=EFI_VARIABLE_APPEND_WRITE ; }else if (!strcmp (argv[0 ],"-i" )) {
Signature · 1 line
2.24.0
PATCH v5 11/16 cmd: efidebug: add "test bootmgr" sub-command
In reply to AKASHI TakahiroThis sub-command will be used to test image authentication, in particular, a case where efi_load_image() failed with EFI_SECURITY_VIOLATION but we still want to try efi_start_image(). We won't run such a case under normal bootmgr because it simply refuses to call efi_start_image() if anything but EFI_SUCCESS is returned when loading an image.
--- cmd/efidebug.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 77 insertions(+), 1 deletion(-)
cmd/efidebug.c +77 -1
@@ -1076,6 +1076,78 @@ static int do_efi_boot_opt(cmd_tbl_t *cmdtp, int flag,return cp->cmd (cmdtp, flag, argc, argv); } +/** +* do_efi_test_bootmgr() - run simple bootmgr for test +* +* @cmdtp: Command table +* @flag: Command flag +* @argc: Number of arguments +* @argv: Argument array +* Return: CMD_RET_SUCCESS on success, +* CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure +* +* Implement efidebug "test bootmgr" sub-command. +* Run simple bootmgr for test. +* +* efidebug test bootmgr +*/ +static int do_efi_test_bootmgr (cmd_tbl_t *cmdtp,int flag, +int argc,char *const argv[]) +{ +efi_handle_t image; +efi_uintn_t exit_data_size =0 ; +u16 *exit_data =NULL ; +efi_status_t ret; + + ret =efi_bootmgr_load (&image); +printf ("efi_bootmgr_load() returned: %ld\n" , ret & ~EFI_ERROR_MASK ); + +/* We call efi_start_image() even if error for test purpose. */ + ret =EFI_CALL (efi_start_image (image, &exit_data_size, &exit_data)); +printf ("efi_start_image() returned: %ld\n" , ret & ~EFI_ERROR_MASK ); +if (ret && exit_data) +efi_free_pool (exit_data); + +efi_restore_gd (); + +return CMD_RET_SUCCESS ; +} + +static cmd_tbl_t cmd_efidebug_test_sub[] = { +U_BOOT_CMD_MKENT (bootmgr,CONFIG_SYS_MAXARGS ,1 , do_efi_test_bootmgr, +"" ,"" ), +}; + +/** +* do_efi_test() - manage UEFI load options +* +* @cmdtp: Command table +* @flag: Command flag +* @argc: Number of arguments +* @argv: Argument array +* Return: CMD_RET_SUCCESS on success, +* CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure +* +* Implement efidebug "test" sub-command. +*/ +static int do_efi_test (cmd_tbl_t *cmdtp,int flag, +int argc,char *const argv[]) +{ +cmd_tbl_t *cp; + +if (argc <2 ) +return CMD_RET_USAGE ; + + argc--; argv++; + + cp =find_cmd_tbl (argv[0 ], cmd_efidebug_test_sub, +ARRAY_SIZE (cmd_efidebug_test_sub)); +if (!cp) +return CMD_RET_USAGE ; + +return cp->cmd (cmdtp, flag, argc, argv); +} +static cmd_tbl_t cmd_efidebug_sub[] = {U_BOOT_CMD_MKENT (boot,CONFIG_SYS_MAXARGS ,1 , do_efi_boot_opt,"" ,"" ), U_BOOT_CMD_MKENT(devices, CONFIG_SYS_MAXARGS, 1, do_efi_show_devices,
@@ -1090,6 +1162,8 @@ static cmd_tbl_t cmd_efidebug_sub[] = { "", ""), U_BOOT_CMD_MKENT(tables, CONFIG_SYS_MAXARGS, 1, do_efi_show_tables, "", ""), + U_BOOT_CMD_MKENT(test, CONFIG_SYS_MAXARGS, 1, do_efi_test, + "", ""), };/**
@@ -1159,7 +1233,9 @@ static char efidebug_help_text[] ="efidebug memmap\n" " - show UEFI memory map\n" "efidebug tables\n" -" - show UEFI configuration tables\n" ; +" - show UEFI configuration tables\n" +"efidebug test bootmgr\n" +" - run simple bootmgr for test\n" ;#endif U_BOOT_CMD(
Signature · 1 line
2.24.0
PATCH v5 12/16 efi_loader, pytest: set up secure boot environment
In reply to AKASHI TakahiroA fixture for UEFI secure boot tests (image authentication and variable authentication) is defined. A small file system with test data in a single partition formatted in fat is created.
This test requires efitools v1.5.2 or later. If the system's efitools is older, you have to build it on your own and define EFITOOLS_PATH.
--- test/py/README.md | 8 ++ test/py/tests/test_efi_secboot/conftest.py | 151 +++++++++++++++++++++ test/py/tests/test_efi_secboot/defs.py | 21 +++ 3 files changed, 180 insertions(+) create mode 100644 test/py/tests/test_efi_secboot/conftest.py create mode 100644 test/py/tests/test_efi_secboot/defs.py
test/py/README.md +8 -0
@@ -37,7 +37,15 @@ will be required. The following is an incomplete list: | openssl | | sudo OR guestmount | | e2fsprogs | +| util-linux | +| coreutils | | dosfstools | +| efitools | +| mount | +| mtools | +| sbsigntool | +| udisks2 | + Please use the apporirate commands for your distribution to match these tools up with the package that provides them.
test/py/tests/test_efi_secboot/conftest.py new file +151 -0
151 lines not on this page · raw message
test/py/tests/test_efi_secboot/defs.py new file +21 -0
@@ -0,0 +1,21 @@ +# SPDX-License-Identifier: GPL-2.0+ + +# Disk image name +EFI_SECBOOT_IMAGE_NAME='test_efi_secboot.img' + +# Size in MiB +EFI_SECBOOT_IMAGE_SIZE=16 +EFI_SECBOOT_PART_SIZE=8 + +# Partition file system type +EFI_SECBOOT_FS_TYPE='vfat' + +# Owner guid +GUID='11111111-2222-3333-4444-123456789abc' + +# v1.5.1 or earlier of efitools has a bug in sha256 calculation, and +# you need build a newer version on your own. +EFITOOLS_PATH='' + +# Hello World application for sandbox +HELLO_PATH=''
Signature · 1 line
2.24.0
PATCH v5 13/16 efi_loader, pytest: add UEFI secure boot tests (authenticated variables)
In reply to AKASHI TakahiroProvide a couple of test cases for variable authentication.
--- .../py/tests/test_efi_secboot/test_authvar.py | 282 ++++++++++++++++++ 1 file changed, 282 insertions(+) create mode 100644 test/py/tests/test_efi_secboot/test_authvar.py
test/py/tests/test_efi_secboot/test_authvar.py new file +282 -0
@@ -0,0 +1,282 @@ +# SPDX-License-Identifier: GPL-2.0+ +# Copyright (c) 2019, Linaro Limited +# Author: AKASHI Takahiro <[email protected]> +# +# U-Boot UEFI: Variable Authentication Test + +""" +This test verifies variable authentication +""" + +import pytest +import re +from defs import * + + at pytest.mark.boardspec('sandbox') + at pytest.mark.buildconfigspec('efi_secure_boot') + at pytest.mark.buildconfigspec('cmd_fat') + at pytest.mark.buildconfigspec('cmd_nvedit_efi') + at pytest.mark.slow +class TestEfiAuthVar(object): + def test_efi_var_auth1(self, u_boot_console, efi_boot_env): + """ + Test Case 1 - Install signature database + """ + u_boot_console.restart_uboot() + disk_img = efi_boot_env + with u_boot_console.log.section('Test Case 1a'): + # Test Case 1a, Initial secure state + output = u_boot_console.run_command_list([ + 'host bind 0 %s' % disk_img, + 'printenv -e SecureBoot']) + assert('00000000: 00' in ''.join(output)) + + output = u_boot_console.run_command( + 'printenv -e SetupMode') + assert('00000000: 01' in output) + + with u_boot_console.log.section('Test Case 1b'): + # Test Case 1b, PK without AUTHENTICATED_WRITE_ACCESS + output = u_boot_console.run_command_list([ + 'fatload host 0:1 4000000 PK.auth', + 'setenv -e -nv -bs -rt -i 4000000,$filesize PK']) + assert(re.search('Failed to set EFI variable', ''.join(output))) + + with u_boot_console.log.section('Test Case 1c'): + # Test Case 1c, install PK + output = u_boot_console.run_command_list([ + 'fatload host 0:1 4000000 PK.auth', + 'setenv -e -nv -bs -rt -at -i 4000000,$filesize PK', + 'printenv -e -n PK']) + assert(re.search('PK:', ''.join(output))) + + output = u_boot_console.run_command( + 'printenv -e SecureBoot') + assert('00000000: 01' in output) + output = u_boot_console.run_command( + 'printenv -e SetupMode') + assert('00000000: 00' in output) + + with u_boot_console.log.section('Test Case 1d'): + # Test Case 1d, db/dbx without KEK + output = u_boot_console.run_command_list([ + 'fatload host 0:1 4000000 db.auth', + 'setenv -e -nv -bs -rt -at -i 4000000,$filesize db']) + assert(re.search('Failed to set EFI variable', ''.join(output))) + + output = u_boot_console.run_command_list([ + 'fatload host 0:1 4000000 db.auth', + 'setenv -e -nv -bs -rt -at -i 4000000,$filesize dbx']) + assert(re.search('Failed to set EFI variable', ''.join(output))) + + with u_boot_console.log.section('Test Case 1e'): + # Test Case 1e, install KEK + output = u_boot_console.run_command_list([ + 'fatload host 0:1 4000000 KEK.auth', + 'setenv -e -nv -bs -rt -i 4000000,$filesize KEK']) + assert(re.search('Failed to set EFI variable', ''.join(output))) + + output = u_boot_console.run_command_list([ + 'fatload host 0:1 4000000 KEK.auth', + 'setenv -e -nv -bs -rt -at -i 4000000,$filesize KEK', + 'printenv -e -n KEK']) + assert(re.search('KEK:', ''.join(output))) + + output = u_boot_console.run_command( + 'printenv -e SecureBoot') + assert('00000000: 01' in output) + + with u_boot_console.log.section('Test Case 1f'): + # Test Case 1f, install db + output = u_boot_console.run_command_list([ + 'fatload host 0:1 4000000 db.auth', + 'setenv -e -nv -bs -rt -i 4000000,$filesize db']) + assert(re.search('Failed to set EFI variable', ''.join(output))) + + output = u_boot_console.run_command_list([ + 'fatload host 0:1 4000000 db.auth', + 'setenv -e -nv -bs -rt -at -i 4000000,$filesize db', + 'printenv -e -n -guid d719b2cb-3d3a-4596-a3bc-dad00e67656f db']) + assert(not re.search('Failed to set EFI variable', ''.join(output))) + assert(re.search('db:', ''.join(output))) + + output = u_boot_console.run_command( + 'printenv -e SecureBoot') + assert('00000000: 01' in output) + + with u_boot_console.log.section('Test Case 1g'): + # Test Case 1g, install dbx + output = u_boot_console.run_command_list([ + 'fatload host 0:1 4000000 db.auth', + 'setenv -e -nv -bs -rt -i 4000000,$filesize db']) + assert(re.search('Failed to set EFI variable', ''.join(output))) + + output = u_boot_console.run_command_list([ + 'fatload host 0:1 4000000 db.auth', + 'setenv -e -nv -bs -rt -at -i 4000000,$filesize db', + 'printenv -e -n -guid d719b2cb-3d3a-4596-a3bc-dad00e67656f db']) + assert(not re.search('Failed to set EFI variable', ''.join(output))) + assert(re.search('db:', ''.join(output))) + + output = u_boot_console.run_command( + 'printenv -e SecureBoot') + assert('00000000: 01' in output) + + def test_efi_var_auth2(self, u_boot_console, efi_boot_env): + """ + Test Case 2 - Update database by overwriting + """ + u_boot_console.restart_uboot() + disk_img = efi_boot_env + with u_boot_console.log.section('Test Case 2a'): + # Test Case 2a, update without AUTHENTICATED_WRITE_ACCESS + output = u_boot_console.run_command_list([ + 'host bind 0 %s' % disk_img, + 'fatload host 0:1 4000000 PK.auth', + 'setenv -e -nv -bs -rt -at -i 4000000,$filesize PK', + 'fatload host 0:1 4000000 KEK.auth', + 'setenv -e -nv -bs -rt -at -i 4000000,$filesize KEK', + 'fatload host 0:1 4000000 db.auth', + 'setenv -e -nv -bs -rt -at -i 4000000,$filesize db', + 'printenv -e -n -guid d719b2cb-3d3a-4596-a3bc-dad00e67656f db']) + assert(not re.search('Failed to set EFI variable', ''.join(output))) + assert(re.search('db:', ''.join(output))) + + output = u_boot_console.run_command_list([ + 'fatload host 0:1 4000000 db1.auth', + 'setenv -e -nv -bs -rt -i 4000000,$filesize db']) + assert(re.search('Failed to set EFI variable', ''.join(output))) + + with u_boot_console.log.section('Test Case 2b'): + # Test Case 2b, update without correct signature + output = u_boot_console.run_command_list([ + 'fatload host 0:1 4000000 db.esl', + 'setenv -e -nv -bs -rt -at -i 4000000,$filesize db']) + assert(re.search('Failed to set EFI variable', ''.join(output))) + + with u_boot_console.log.section('Test Case 2c'): + # Test Case 2c, update with correct signature + output = u_boot_console.run_command_list([ + 'fatload host 0:1 4000000 db1.auth', + 'setenv -e -nv -bs -rt -at -i 4000000,$filesize db', + 'printenv -e -n -guid d719b2cb-3d3a-4596-a3bc-dad00e67656f db']) + assert(not re.search('Failed to set EFI variable', ''.join(output))) + assert(re.search('db:', ''.join(output))) + + def test_efi_var_auth3(self, u_boot_console, efi_boot_env): + """ + Test Case 3 - Append database + """ + u_boot_console.restart_uboot() + disk_img = efi_boot_env + with u_boot_console.log.section('Test Case 3a'): + # Test Case 3a, update without AUTHENTICATED_WRITE_ACCESS + output = u_boot_console.run_command_list([ + 'host bind 0 %s' % disk_img, + 'fatload host 0:1 4000000 PK.auth', + 'setenv -e -nv -bs -rt -at -i 4000000,$filesize PK', + 'fatload host 0:1 4000000 KEK.auth', + 'setenv -e -nv -bs -rt -at -i 4000000,$filesize KEK', + 'fatload host 0:1 4000000 db.auth', + 'setenv -e -nv -bs -rt -at -i 4000000,$filesize db', + 'printenv -e -n -guid d719b2cb-3d3a-4596-a3bc-dad00e67656f db']) + assert(not re.search('Failed to set EFI variable', ''.join(output))) + assert(re.search('db:', ''.join(output))) + + output = u_boot_console.run_command_list([ + 'fatload host 0:1 4000000 db1.auth', + 'setenv -e -nv -bs -rt -a -i 4000000,$filesize db']) + assert(re.search('Failed to set EFI variable', ''.join(output))) + + with u_boot_console.log.section('Test Case 3b'): + # Test Case 3b, update without correct signature + output = u_boot_console.run_command_list([ + 'fatload host 0:1 4000000 db.esl', + 'setenv -e -nv -bs -rt -at -a -i 4000000,$filesize db']) + assert(re.search('Failed to set EFI variable', ''.join(output))) + + with u_boot_console.log.section('Test Case 3c'): + # Test Case 3c, update with correct signature + output = u_boot_console.run_command_list([ + 'fatload host 0:1 4000000 db1.auth', + 'setenv -e -nv -bs -rt -at -a -i 4000000,$filesize db', + 'printenv -e -n -guid d719b2cb-3d3a-4596-a3bc-dad00e67656f db']) + assert(not re.search('Failed to set EFI variable', ''.join(output))) + assert(re.search('db:', ''.join(output))) + + def test_efi_var_auth4(self, u_boot_console, efi_boot_env): + """ + Test Case 4 - Delete database without authentication + """ + u_boot_console.restart_uboot() + disk_img = efi_boot_env + with u_boot_console.log.section('Test Case 4a'): + # Test Case 4a, update without AUTHENTICATED_WRITE_ACCESS + output = u_boot_console.run_command_list([ + 'host bind 0 %s' % disk_img, + 'fatload host 0:1 4000000 PK.auth', + 'setenv -e -nv -bs -rt -at -i 4000000,$filesize PK', + 'fatload host 0:1 4000000 KEK.auth', + 'setenv -e -nv -bs -rt -at -i 4000000,$filesize KEK', + 'fatload host 0:1 4000000 db.auth', + 'setenv -e -nv -bs -rt -at -i 4000000,$filesize db', + 'printenv -e -n -guid d719b2cb-3d3a-4596-a3bc-dad00e67656f db']) + assert(not re.search('Failed to set EFI variable', ''.join(output))) + assert(re.search('db:', ''.join(output))) + + output = u_boot_console.run_command_list([ + 'setenv -e -nv -bs -rt db', + 'printenv -e -n -guid d719b2cb-3d3a-4596-a3bc-dad00e67656f db']) + assert(re.search('Failed to set EFI variable', ''.join(output))) + assert(re.search('db:', ''.join(output))) + + with u_boot_console.log.section('Test Case 4b'): + # Test Case 4b, update without correct signature/data + output = u_boot_console.run_command_list([ + 'setenv -e -nv -bs -rt -at db', + 'printenv -e -n -guid d719b2cb-3d3a-4596-a3bc-dad00e67656f db']) + assert(re.search('Failed to set EFI variable', ''.join(output))) + assert(re.search('db:', ''.join(output))) + + def test_efi_var_auth5(self, u_boot_console, efi_boot_env): + """ + Test Case 5 - Uninstall(delete) PK + """ + u_boot_console.restart_uboot() + disk_img = efi_boot_env + with u_boot_console.log.section('Test Case 5a'): + # Test Case 5a, Uninstall PK without correct signature + output = u_boot_console.run_command_list([ + 'host bind 0 %s' % disk_img, + 'fatload host 0:1 4000000 PK.auth', + 'setenv -e -nv -bs -rt -at -i 4000000,$filesize PK', + 'fatload host 0:1 4000000 KEK.auth', + 'setenv -e -nv -bs -rt -at -i 4000000,$filesize KEK', + 'fatload host 0:1 4000000 db.auth', + 'setenv -e -nv -bs -rt -at -i 4000000,$filesize db', + 'printenv -e -n PK']) + assert(not re.search('Failed to set EFI variable', ''.join(output))) + assert(re.search('PK:', ''.join(output))) + + output = u_boot_console.run_command_list([ + 'fatload host 0:1 4000000 PK_null.esl', + 'setenv -e -nv -bs -rt -at -i 4000000,$filesize PK', + 'printenv -e -n PK']) + assert(re.search('Failed to set EFI variable', ''.join(output))) + assert(re.search('PK:', ''.join(output))) + + with u_boot_console.log.section('Test Case 5b'): + # Test Case 5b, Uninstall PK with correct signature + output = u_boot_console.run_command_list([ + 'fatload host 0:1 4000000 PK_null.auth', + 'setenv -e -nv -bs -rt -at -i 4000000,$filesize PK', + 'printenv -e -n PK']) + assert(not re.search('Failed to set EFI variable', ''.join(output))) + assert(re.search('\"PK\" not defined', ''.join(output))) + + output = u_boot_console.run_command( + 'printenv -e SecureBoot') + assert('00000000: 00' in output) + output = u_boot_console.run_command( + 'printenv -e SetupMode') + assert('00000000: 01' in output)
Signature · 1 line
2.24.0
PATCH v5 14/16 efi_loader, pytest: add UEFI secure boot tests (image)
In reply to AKASHI TakahiroProvide test cases for
* image authentication for signed images
(test_efi_secboot/test_signed.py)
* image authentication for unsigned images
(test_efi_secboot/test_unsigned.py)
--- test/py/tests/test_efi_secboot/test_signed.py | 117 +++++++++++++++++ .../tests/test_efi_secboot/test_unsigned.py | 121 ++++++++++++++++++ 2 files changed, 238 insertions(+) create mode 100644 test/py/tests/test_efi_secboot/test_signed.py create mode 100644 test/py/tests/test_efi_secboot/test_unsigned.py
test/py/tests/test_efi_secboot/test_signed.py new file +117 -0
@@ -0,0 +1,117 @@ +# SPDX-License-Identifier: GPL-2.0+ +# Copyright (c) 2019, Linaro Limited +# Author: AKASHI Takahiro <[email protected]> +# +# U-Boot UEFI: Signed Image Authentication Test + +""" +This test verifies image authentication for signed images. +""" + +import pytest +import re +from defs import * + + at pytest.mark.boardspec('sandbox') + at pytest.mark.buildconfigspec('efi_secure_boot') + at pytest.mark.buildconfigspec('cmd_efidebug') + at pytest.mark.buildconfigspec('cmd_fat') + at pytest.mark.buildconfigspec('cmd_nvedit_efi') + at pytest.mark.slow +class TestEfiSignedImage(object): + def test_efi_signed_image_auth1(self, u_boot_console, efi_boot_env): + """ + Test Case 1 - authenticated by db + """ + u_boot_console.restart_uboot() + disk_img = efi_boot_env + with u_boot_console.log.section('Test Case 1a'): + # Test Case 1a, run signed image if no db/dbx + output = u_boot_console.run_command_list([ + 'host bind 0 %s' % disk_img, + 'efidebug boot add 1 HELLO1 host 0:1 /helloworld.efi.signed ""', + 'efidebug boot next 1', + 'bootefi bootmgr']) + assert(re.search('Hello, world!', ''.join(output))) + + with u_boot_console.log.section('Test Case 1b'): + # Test Case 1b, run unsigned image if no db/dbx + output = u_boot_console.run_command_list([ + 'efidebug boot add 2 HELLO2 host 0:1 /helloworld.efi ""', + 'efidebug boot next 2', + 'bootefi bootmgr']) + assert(re.search('Hello, world!', ''.join(output))) + + with u_boot_console.log.section('Test Case 1c'): + # Test Case 1c, not authenticated by db + output = u_boot_console.run_command_list([ + 'fatload host 0:1 4000000 db.auth', + 'setenv -e -nv -bs -rt -at -i 4000000,$filesize db', + 'fatload host 0:1 4000000 KEK.auth', + 'setenv -e -nv -bs -rt -at -i 4000000,$filesize KEK', + 'fatload host 0:1 4000000 PK.auth', + 'setenv -e -nv -bs -rt -at -i 4000000,$filesize PK']) + assert(not re.search('Failed to set EFI variable', ''.join(output))) + output = u_boot_console.run_command_list([ + 'efidebug boot next 2', + 'bootefi bootmgr']) + assert(re.search('\'HELLO2\' failed', ''.join(output))) + output = u_boot_console.run_command_list([ + 'efidebug boot next 2', + 'efidebug test bootmgr']) + assert(re.search('efi_start_image\(\) returned: 26', + ''.join(output))) + assert(not re.search('Hello, world!', ''.join(output))) + + with u_boot_console.log.section('Test Case 1d'): + # Test Case 1d, authenticated by db + output = u_boot_console.run_command_list([ + 'efidebug boot next 1', + 'bootefi bootmgr']) + assert(re.search('Hello, world!', ''.join(output))) + + def test_efi_signed_image_auth2(self, u_boot_console, efi_boot_env): + """ + Test Case 2 - rejected by dbx + """ + u_boot_console.restart_uboot() + disk_img = efi_boot_env + with u_boot_console.log.section('Test Case 2a'): + # Test Case 2a, rejected by dbx + output = u_boot_console.run_command_list([ + 'host bind 0 %s' % disk_img, + 'fatload host 0:1 4000000 db.auth', + 'setenv -e -nv -bs -rt -at -i 4000000,$filesize dbx', + 'fatload host 0:1 4000000 KEK.auth', + 'setenv -e -nv -bs -rt -at -i 4000000,$filesize KEK', + 'fatload host 0:1 4000000 PK.auth', + 'setenv -e -nv -bs -rt -at -i 4000000,$filesize PK']) + assert(not re.search('Failed to set EFI variable', ''.join(output))) + output = u_boot_console.run_command_list([ + 'efidebug boot add 1 HELLO host 0:1 /helloworld.efi.signed ""', + 'efidebug boot next 1', + 'bootefi bootmgr']) + assert(re.search('\'HELLO\' failed', ''.join(output))) + output = u_boot_console.run_command_list([ + 'efidebug boot next 1', + 'efidebug test bootmgr']) + assert(re.search('efi_start_image\(\) returned: 26', + ''.join(output))) + assert(not re.search('Hello, world!', ''.join(output))) + + with u_boot_console.log.section('Test Case 2b'): + # Test Case 2b, rejected by dbx even if db allows + output = u_boot_console.run_command_list([ + 'fatload host 0:1 4000000 db.auth', + 'setenv -e -nv -bs -rt -at -i 4000000,$filesize db']) + assert(not re.search('Failed to set EFI variable', ''.join(output))) + output = u_boot_console.run_command_list([ + 'efidebug boot next 1', + 'bootefi bootmgr']) + assert(re.search('\'HELLO\' failed', ''.join(output))) + output = u_boot_console.run_command_list([ + 'efidebug boot next 1', + 'efidebug test bootmgr']) + assert(re.search('efi_start_image\(\) returned: 26', + ''.join(output))) + assert(not re.search('Hello, world!', ''.join(output)))
test/py/tests/test_efi_secboot/test_unsigned.py new file +121 -0
121 lines not on this page · raw message
Signature · 1 line
2.24.0
PATCH v5 15/16 sandbox: add extra configurations for UEFI and related tests
In reply to AKASHI TakahiroAdding those extra configurations allows us to successfully run UEFI secure boot pytest on Travis CI.
--- configs/sandbox64_defconfig | 3 +++ configs/sandbox_defconfig | 3 +++ 2 files changed, 6 insertions(+)
configs/sandbox64_defconfig +3 -0
@@ -27,6 +27,7 @@ CONFIG_CMD_ASKENV=y CONFIG_CMD_GREPENV=y CONFIG_CMD_ENV_CALLBACK=y CONFIG_CMD_ENV_FLAGS=y +CONFIG_CMD_NVEDIT_EFI=y CONFIG_LOOPW=y CONFIG_CMD_MD5SUM=y CONFIG_CMD_MEMINFO=y
@@ -53,6 +54,7 @@ CONFIG_CMD_DNS=y CONFIG_CMD_LINK_LOCAL=y CONFIG_CMD_ETHSW=y CONFIG_CMD_BMP=y +CONFIG_CMD_EFIDEBUG=y CONFIG_CMD_TIME=y CONFIG_CMD_TIMER=y CONFIG_CMD_SOUND=y
@@ -205,6 +207,7 @@ CONFIG_RSA_VERIFY_WITH_PKEY=y CONFIG_TPM=y CONFIG_LZ4=y CONFIG_ERRNO_STR=y +CONFIG_EFI_SECURE_BOOT=y CONFIG_TEST_FDTDEC=y CONFIG_UNIT_TEST=y CONFIG_UT_TIME=y
configs/sandbox_defconfig +3 -0
@@ -30,6 +30,7 @@ CONFIG_CMD_ASKENV=y CONFIG_CMD_GREPENV=y CONFIG_CMD_ENV_CALLBACK=y CONFIG_CMD_ENV_FLAGS=y +CONFIG_CMD_NVEDIT_EFI=y CONFIG_LOOPW=y CONFIG_CMD_MD5SUM=y CONFIG_CMD_MEMINFO=y
@@ -61,6 +62,7 @@ CONFIG_CMD_LINK_LOCAL=y CONFIG_CMD_ETHSW=y CONFIG_CMD_BMP=y CONFIG_CMD_BOOTCOUNT=y +CONFIG_CMD_EFIDEBUG=y CONFIG_CMD_TIME=y CONFIG_CMD_TIMER=y CONFIG_CMD_SOUND=y
@@ -230,6 +232,7 @@ CONFIG_RSA_VERIFY_WITH_PKEY=y CONFIG_TPM=y CONFIG_LZ4=y CONFIG_ERRNO_STR=y +CONFIG_EFI_SECURE_BOOT=y CONFIG_TEST_FDTDEC=y CONFIG_UNIT_TEST=y CONFIG_UT_TIME=y
Signature · 1 line
2.24.0
PATCH v5 16/16 travis: add packages for UEFI secure boot test
In reply to AKASHI TakahiroPytest for UEFI secure boot will use several host commands.
In particular, Test setup relies on efitools, whose version must be v1.5.2 or later. So fetch a new version of deb package directly. Please note it has a dependency on mtools, which must also be installed along wih efitools.
In addition, the path, '/sbin', is added to PATH for use of sgdisk and mkfs.
--- .travis.yml | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)
.travis.yml +10 -1
@@ -38,6 +38,14 @@ addons: - libisl15 - clang-7 - srecord + - coreutils + - util-linux + - dosfstools + - gdisk + - mount + - mtools + - openssl + - sbsigntool install: # Clone uboot-test-hooks
@@ -58,10 +66,11 @@ install: - mkdir ~/grub2-arm64 - ( cd ~/grub2-arm64; wget -O - http://download.opensuse.org/ports/aarch64/distribution/leap/42.2/repo/oss/suse/aarch64/grub2-arm64-efi-2.02~beta2-87.1.aarch64.rpm | rpm2cpio | cpio -di ) - wget http://mirrors.kernel.org/ubuntu/pool/main/m/mpfr4/libmpfr4_3.1.4-1_amd64.deb && sudo dpkg -i libmpfr4_3.1.4-1_amd64.deb && rm libmpfr4_3.1.4-1_amd64.deb + - wget http://mirrors.kernel.org/ubuntu/pool/universe/e/efitools/efitools_1.8.1-0ubuntu2_amd64.deb && sudo dpkg -i efitools_1.8.1-0ubuntu2_amd64.deb && rm efitools_1.8.1-0ubuntu2_amd64.deb env: global: - - PATH=/tmp/qemu-install/bin:/tmp/uboot-test-hooks/bin:/usr/bin:/bin:/usr/local/bin + - PATH=/tmp/qemu-install/bin:/tmp/uboot-test-hooks/bin:/sbin:/usr/bin:/bin:/usr/local/bin - PYTHONPATH=/tmp/uboot-test-hooks/py/travis-ci - BUILD_DIR=build - HOSTCC="cc"
Signature · 1 line
2.24.0
PATCH v5 16/16 travis: add packages for UEFI secure boot test
Quoted 10 lines · AKASHI Takahiro
Pytest for UEFI secure boot will use several host commands.
In particular, Test setup relies on efitools, whose version must be v1.5.2 or later. So fetch a new version of deb package directly. Please note it has a dependency on mtools, which must also be installed along wih efitools.In addition, the path, '/sbin', is added to PATH for use of sgdisk and mkfs.
Signed-off-by AKASHI Takahiro <[email protected]>
You are adding some packages here. Don't you need the same packages in the Docker image used by Gitlab CI and Azure CI?
Cf. https://gitlab.denx.de/u-boot/gitlab-ci-runner
Best regards
Heinrich
Quoted 37 lines
--- .travis.yml | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-).travis.yml +10
-1@@ -38,6 +38,14 @@ addons: - libisl15 - clang-7 - srecord + - coreutils + - util-linux + - dosfstools + - gdisk + - mount + - mtools + - openssl + - sbsigntool install: # Clone uboot-test-hooks@@ -58,10 +66,11 @@ install: - mkdir ~/grub2-arm64 - ( cd ~/grub2-arm64; wget -O - http://download.opensuse.org/ports/aarch64/distribution/leap/42.2/repo/oss/suse/aarch64/grub2-arm64-efi-2.02~beta2-87.1.aarch64.rpm | rpm2cpio | cpio -di ) - wget http://mirrors.kernel.org/ubuntu/pool/main/m/mpfr4/libmpfr4_3.1.4-1_amd64.deb && sudo dpkg -i libmpfr4_3.1.4-1_amd64.deb && rm libmpfr4_3.1.4-1_amd64.deb + - wget http://mirrors.kernel.org/ubuntu/pool/universe/e/efitools/efitools_1.8.1-0ubuntu2_amd64.deb && sudo dpkg -i efitools_1.8.1-0ubuntu2_amd64.deb && rm efitools_1.8.1-0ubuntu2_amd64.deb env: global: - - PATH=/tmp/qemu-install/bin:/tmp/uboot-test-hooks/bin:/usr/bin:/bin:/usr/local/bin + - PATH=/tmp/qemu-install/bin:/tmp/uboot-test-hooks/bin:/sbin:/usr/bin:/bin:/usr/local/bin - PYTHONPATH=/tmp/uboot-test-hooks/py/travis-ci - BUILD_DIR=build - HOSTCC="cc"
PATCH v5 16/16 travis: add packages for UEFI secure boot test
Quoted 7 lines · Heinrich Schuchardt
Quoted 6 lines · AKASHI Takahiro
Pytest for UEFI secure boot will use several host commands.
In particular, Test setup relies on efitools, whose version must be v1.5.2 or later. So fetch a new version of deb package directly. Please note it has a dependency on mtools, which must also be installed along wih efitools.
Quoted 7 lines
Quoted 4 lines
In addition, the path, '/sbin', is added to PATH for use of sgdisk and mkfs.
Signed-off-by AKASHI Takahiro <[email protected]>You are adding some packages here. Don't you need the same packages in the Docker image used by Gitlab CI and Azure CI?
You're right, but I expect that Tom will take care of this as far as he remembers that he has said so.
Thanks,
-Takahiro Akashi
Quoted 44 lines
Cf. https://gitlab.denx.de/u-boot/gitlab-ci-runner
Best regards
Heinrich
Quoted 37 lines
--- .travis.yml | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-).travis.yml +10
-1@@ -38,6 +38,14 @@ addons: - libisl15 - clang-7 - srecord + - coreutils + - util-linux + - dosfstools + - gdisk + - mount + - mtools + - openssl + - sbsigntool install: # Clone uboot-test-hooks@@ -58,10 +66,11 @@ install: - mkdir ~/grub2-arm64 - ( cd ~/grub2-arm64; wget -O - http://download.opensuse.org/ports/aarch64/distribution/leap/42.2/repo/oss/suse/aarch64/grub2-arm64-efi-2.02~beta2-87.1.aarch64.rpm | rpm2cpio | cpio -di ) - wget http://mirrors.kernel.org/ubuntu/pool/main/m/mpfr4/libmpfr4_3.1.4-1_amd64.deb && sudo dpkg -i libmpfr4_3.1.4-1_amd64.deb && rm libmpfr4_3.1.4-1_amd64.deb + - wget http://mirrors.kernel.org/ubuntu/pool/universe/e/efitools/efitools_1.8.1-0ubuntu2_amd64.deb && sudo dpkg -i efitools_1.8.1-0ubuntu2_amd64.deb && rm efitools_1.8.1-0ubuntu2_amd64.deb env: global: - - PATH=/tmp/qemu-install/bin:/tmp/uboot-test-hooks/bin:/usr/bin:/bin:/usr/local/bin + - PATH=/tmp/qemu-install/bin:/tmp/uboot-test-hooks/bin:/sbin:/usr/bin:/bin:/usr/local/bin - PYTHONPATH=/tmp/uboot-test-hooks/py/travis-ci - BUILD_DIR=build - HOSTCC="cc"
v5 cover efi_loader: add secure boot support
In reply to AKASHI TakahiroQuoted 14 lines · AKASHI Takahiro
# Documentation for UEFI secure boot on U-Boot will be submitted in
# a separate patch in near future.One of major missing features in current UEFI implementation is "secure boot." The ultimate goal of my attempt is to implement image authentication based on signature and provide UEFI secure boot support which would be fully compliant with UEFI specification, section 32[1].
(The code was originally developed by Patrick Wildt.)Please note, however, this patch doesn't work on its own; there are a couple of functional dependencies[2] and [3], that I have submitted before. For complete workable patch set, see my repository[4], which also contains experimental timestamp-based revocation suuport.
Quoted 6 lines
My "non-volatile" support[5], which is under discussion, is not mandatory and so not included here, but this inevitably implies that, for example, signature database variables, like db and dbx, won't be persistent unless you explicitly run "env save" command.
Anyhow, Linaro is also working on implementing real "secure storage" solution based on TF-A and OP-TEE.
In the patch series I am missing a patch providing the documentation explaining how to set up secure boot with U-Boot. doc/uefi/uefi.rst would be a good place for it.
I guess the description should include:
- which certificates have to be created and how to generate these
- which variables have to be initialized with which values
- how the images can be signed
Best regards
Heinrich
Quoted 176 lines
Supported features:
* image authentication based on db and dbx
* supported signature types areEFI_CERT_SHA256_GUID(SHA256 digest for unsigned images)EFI_CERT_X509_GUID(x509 certificate for signed images)
* SecureBoot/SignatureSupport variables
* SetupMode and user mode
* variable authentication based on PK and KEKEFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
* basic pytest test casesUnsupported features: (marked as TODO in most cases in the source code,
and won't be included in this series)
* hash algorithms other than SHA256
* dbt: timestamp(RFC6131)-based certificate revocation
* dbr: OS recovery
* xxxDefault: default values for signature stores
* transition to AuditMode and DeployedMode
* recording rejected images inEFI_IMAGE_EXECUTION_INFO_TABLE
* verification "policy", in particular, check against signature's owner
* private authenticated variables
* variable authentication withEFI_VARIABLE_ENHANCED_AUTHENTICATED_ACCESS
* real secure storage support, including hardware-specific PK (Platform Key) installationTODO's other than "Unsupported features": (won't be included in this series)
* fail recovery, in particular, in modifying authenticated variables
* support read-only attributes of well-defined global variables in particular, "SignatureSupport"
* Extensive test suite (or more test cases) to confirm compatibility with EDK2
=> I requested EDK SCT community to add tests[6].Test:
* My pytest, included in this patch set, passed.
*efi_selftestpassed. (At least no regression.)
* Travis CI tests have passed.Known issues:
* efitools is used in pytest, and its version must be v1.5.2 or later. (Solution: You can defineEFITOOLS_PATHindefs.pyfor your own efitools.)Hints about how to use:
(Please see other documents, or my pytest scripts, for details.)
* You can create your own certificates with openssl.
* You can sign your application with sbsign (on Ubuntu).
* You can create raw data for signature database with efitools, and install/manage authenticated variables with "env -set -e" command or efitools' "UpdateVars.efi" application.[1] https://uefi.org/sites/default/files/resources/UEFI_Spec_2_8_final.pdf
[2] https://lists.denx.de/pipermail/u-boot/2019-November/390127.html
(import x509/pkcs7 parsers from linux)
[3] https://lists.denx.de/pipermail/u-boot/2020-January/398057.html
(extendrsa_verify()for UEFI secure boot)
[4] http://git.linaro.org/people/takahiro.akashi/u-boot.git/ efi/secboot
[5] https://lists.denx.de/pipermail/u-boot/2019-September/382835.html
(non-volatile variables support)
[6] https://bugzilla.tianocore.org/show_bug.cgi?id=2230Changes in v5 (Jan 28, 2020)
* rebased to pre-v2020.04-rc1 (fixed some merge conflicts)
* remove already-merged commits (v4's patch#1)
* fix a compile error caused by gcc 9.x (patch#4)
* returnSECURITY_VIOLATIONinstead ofACCESS_DENIEDif authentication fails (patch#7)
* useqsort()for section sorting (patch#7)
* add "efidebug test" sub-command (patch#11)
* addefi_start_image(SECURITY_VIOLATION)test (patch#14)Changes in v4 (Dec 18, 2019)
* adjustEFI_SECURE_BOOTdependencies due to a change of RSA extension patch v5 (patch#2)
* change "imply" to "select" against kconfig dependencies (patch#2)
* otherwise, no functional changesChanges in v3 (Dec 9, 2019)
* allow for arbitrary number of regions inefi_image_region_add()(patch#3, #5 and #8)
* remove a redundant check in a while loop atefi_sigstore_free()(patch#4)Changes in v2 (Nov 26, 2019)
* rebased to v2020.01-rc3
* renameIMAGE_DIRECTORY_ENTRY_CERTTABLEtoIMAGE_DIRECTORY_ENTRY_SECURITY(patch#1,#9)
* add comments (patch#1)
* drop v1's patch#2 as it is no longer necessary
* drop v1's patch#3 as other "SECURE_BOOT" architectures have renamed this option and no longer use it
* add structure descriptions (patch#3)
* rework hash calculation code inefi_signature_verify()and remove an odd constant, WinIndrectSha256 (patch#3)
* move travis.yml changes to a separate patch (patch#12, #16)
*yield_fixture()->fixture()(patch#12)
* callconsole.restart_uboot()at every test case (13,#14)
* add patch#15; enable UEFI-related configurations by default on sandbox
* add patch#16; modify Travis CI environment to run UEFI secure boot testChanges in v1 (Nov 13, 2019)
* rebased to v2020.01-rc
* remove already-merged patches
* re-work the patch set for easier reviews, including
- move a config definition patch forward (patch#4)
- refactor/rename verification functions (patch#5/#10)
- split signature database parser as a separate patch (patch#6)
- split secure state transition code as a separate patch (patch#8)
- move most part ofinit_secure_boot()intoinit_variables()(patch#8)
- split test environment setup from test patches (patch#14)
* add function descriptions (patch#5-#11)
* make sure the section list is sorted in ascending order in hash calculation of PE image (patch#10)
* add a new "-at" (authenticated access) option to "env -e" (patch#13)
* list required host packages, in particular udisks2, in pytest (patch#14)
* modifyconftest.pyto run under python3 (patch#14)
* use a partition on a disk instead of a whole disk without partition table (patch#14)
* reduce dependency on efitools, yet relying on its host tools (patch#14)
* modify pytests to catch up wth latest changes of "env -e" syntax (patch#15,#16)RFC (Sept 18, 2019)
AKASHI Takahiro (16):
efi_loader: addCONFIG_EFI_SECURE_BOOTconfig option
efi_loader: add signature verification functions
efi_loader: add signature database parser
efi_loader: variable: support variable authentication
efi_loader: variable: add secure boot state transition
efi_loader: variable: add VendorKeys variable
efi_loader:image_loader: support image authentication
efi_loader: set up secure boot
cmd: env: use appropriate guid for authenticated UEFI variable
cmd: env: add "-at" option to "env set -e" command
cmd: efidebug: add "test bootmgr" sub-commandefi_loader, pytest: set up secure boot environmentefi_loader, pytest: add UEFI secure boot tests (authenticated
variables)
efi_loader, pytest: add UEFI secure boot tests (image)
sandbox: add extra configurations for UEFI and related tests
travis: add packages for UEFI secure boot test.travis.yml | 11 +- cmd/efidebug.c | 78 +- cmd/nvedit.c | 5 +- cmd/nvedit_efi.c | 23 +- configs/sandbox64_defconfig | 3 + configs/sandbox_defconfig | 3 + include/efi_api.h | 87 ++ include/efi_loader.h | 91 +- lib/efi_loader/Kconfig | 18 + lib/efi_loader/Makefile | 1 + lib/efi_loader/efi_boottime.c | 10 +- lib/efi_loader/efi_image_loader.c | 460 ++++++++- lib/efi_loader/efi_setup.c | 38 + lib/efi_loader/efi_signature.c | 809 +++++++++++++++ lib/efi_loader/efi_variable.c | 951 ++++++++++++++++-- test/py/README.md | 8 + test/py/tests/test_efi_secboot/conftest.py | 151 +++ test/py/tests/test_efi_secboot/defs.py | 21 + .../py/tests/test_efi_secboot/test_authvar.py | 282 ++++++ test/py/tests/test_efi_secboot/test_signed.py | 117 +++ .../tests/test_efi_secboot/test_unsigned.py | 121 +++ 21 files changed, 3157 insertions(+), 131 deletions(-)create mode 100644
lib/efi_loader/efi_signature.ccreate mode 100644test/py/tests/test_efi_secboot/conftest.pycreate mode 100644test/py/tests/test_efi_secboot/defs.pycreate mode 100644test/py/tests/test_efi_secboot/test_authvar.pycreate mode 100644test/py/tests/test_efi_secboot/test_signed.pycreate mode 100644test/py/tests/test_efi_secboot/test_unsigned.py
v5 cover efi_loader: add secure boot support
Quoted 22 lines · Heinrich Schuchardt
Quoted 14 lines · AKASHI Takahiro
# Documentation for UEFI secure boot on U-Boot will be submitted in
# a separate patch in near future.One of major missing features in current UEFI implementation is "secure boot." The ultimate goal of my attempt is to implement image authentication based on signature and provide UEFI secure boot support which would be fully compliant with UEFI specification, section 32[1].
(The code was originally developed by Patrick Wildt.)Please note, however, this patch doesn't work on its own; there are a couple of functional dependencies[2] and [3], that I have submitted before. For complete workable patch set, see my repository[4], which also contains experimental timestamp-based revocation suuport.
Quoted 6 lines
My "non-volatile" support[5], which is under discussion, is not mandatory and so not included here, but this inevitably implies that, for example, signature database variables, like db and dbx, won't be persistent unless you explicitly run "env save" command.
Anyhow, Linaro is also working on implementing real "secure storage" solution based on TF-A and OP-TEE.
Quoted 3 lines
In the patch series I am missing a patch providing the documentation explaining how to set up secure boot with U-Boot.
doc/uefi/uefi.rstwould be a good place for it.
See:
https://lists.denx.de/pipermail/u-boot/2020-February/399446.html
I posted this patch as a separate one because I believe that we can discuss separately from the code.
-Takahiro Akashi
Quoted 187 lines
I guess the description should include:
- which certificates have to be created and how to generate these
- which variables have to be initialized with which values
- how the images can be signedBest regards
Heinrich
Quoted 176 lines
Supported features:
* image authentication based on db and dbx
* supported signature types areEFI_CERT_SHA256_GUID(SHA256 digest for unsigned images)EFI_CERT_X509_GUID(x509 certificate for signed images)
* SecureBoot/SignatureSupport variables
* SetupMode and user mode
* variable authentication based on PK and KEKEFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
* basic pytest test casesUnsupported features: (marked as TODO in most cases in the source code,
and won't be included in this series)
* hash algorithms other than SHA256
* dbt: timestamp(RFC6131)-based certificate revocation
* dbr: OS recovery
* xxxDefault: default values for signature stores
* transition to AuditMode and DeployedMode
* recording rejected images inEFI_IMAGE_EXECUTION_INFO_TABLE
* verification "policy", in particular, check against signature's owner
* private authenticated variables
* variable authentication withEFI_VARIABLE_ENHANCED_AUTHENTICATED_ACCESS
* real secure storage support, including hardware-specific PK (Platform Key) installationTODO's other than "Unsupported features": (won't be included in this series)
* fail recovery, in particular, in modifying authenticated variables
* support read-only attributes of well-defined global variables in particular, "SignatureSupport"
* Extensive test suite (or more test cases) to confirm compatibility with EDK2
=> I requested EDK SCT community to add tests[6].Test:
* My pytest, included in this patch set, passed.
*efi_selftestpassed. (At least no regression.)
* Travis CI tests have passed.Known issues:
* efitools is used in pytest, and its version must be v1.5.2 or later. (Solution: You can defineEFITOOLS_PATHindefs.pyfor your own efitools.)Hints about how to use:
(Please see other documents, or my pytest scripts, for details.)
* You can create your own certificates with openssl.
* You can sign your application with sbsign (on Ubuntu).
* You can create raw data for signature database with efitools, and install/manage authenticated variables with "env -set -e" command or efitools' "UpdateVars.efi" application.[1] https://uefi.org/sites/default/files/resources/UEFI_Spec_2_8_final.pdf
[2] https://lists.denx.de/pipermail/u-boot/2019-November/390127.html
(import x509/pkcs7 parsers from linux)
[3] https://lists.denx.de/pipermail/u-boot/2020-January/398057.html
(extendrsa_verify()for UEFI secure boot)
[4] http://git.linaro.org/people/takahiro.akashi/u-boot.git/ efi/secboot
[5] https://lists.denx.de/pipermail/u-boot/2019-September/382835.html
(non-volatile variables support)
[6] https://bugzilla.tianocore.org/show_bug.cgi?id=2230Changes in v5 (Jan 28, 2020)
* rebased to pre-v2020.04-rc1 (fixed some merge conflicts)
* remove already-merged commits (v4's patch#1)
* fix a compile error caused by gcc 9.x (patch#4)
* returnSECURITY_VIOLATIONinstead ofACCESS_DENIEDif authentication fails (patch#7)
* useqsort()for section sorting (patch#7)
* add "efidebug test" sub-command (patch#11)
* addefi_start_image(SECURITY_VIOLATION)test (patch#14)Changes in v4 (Dec 18, 2019)
* adjustEFI_SECURE_BOOTdependencies due to a change of RSA extension patch v5 (patch#2)
* change "imply" to "select" against kconfig dependencies (patch#2)
* otherwise, no functional changesChanges in v3 (Dec 9, 2019)
* allow for arbitrary number of regions inefi_image_region_add()(patch#3, #5 and #8)
* remove a redundant check in a while loop atefi_sigstore_free()(patch#4)Changes in v2 (Nov 26, 2019)
* rebased to v2020.01-rc3
* renameIMAGE_DIRECTORY_ENTRY_CERTTABLEtoIMAGE_DIRECTORY_ENTRY_SECURITY(patch#1,#9)
* add comments (patch#1)
* drop v1's patch#2 as it is no longer necessary
* drop v1's patch#3 as other "SECURE_BOOT" architectures have renamed this option and no longer use it
* add structure descriptions (patch#3)
* rework hash calculation code inefi_signature_verify()and remove an odd constant, WinIndrectSha256 (patch#3)
* move travis.yml changes to a separate patch (patch#12, #16)
*yield_fixture()->fixture()(patch#12)
* callconsole.restart_uboot()at every test case (13,#14)
* add patch#15; enable UEFI-related configurations by default on sandbox
* add patch#16; modify Travis CI environment to run UEFI secure boot testChanges in v1 (Nov 13, 2019)
* rebased to v2020.01-rc
* remove already-merged patches
* re-work the patch set for easier reviews, including
- move a config definition patch forward (patch#4)
- refactor/rename verification functions (patch#5/#10)
- split signature database parser as a separate patch (patch#6)
- split secure state transition code as a separate patch (patch#8)
- move most part ofinit_secure_boot()intoinit_variables()(patch#8)
- split test environment setup from test patches (patch#14)
* add function descriptions (patch#5-#11)
* make sure the section list is sorted in ascending order in hash calculation of PE image (patch#10)
* add a new "-at" (authenticated access) option to "env -e" (patch#13)
* list required host packages, in particular udisks2, in pytest (patch#14)
* modifyconftest.pyto run under python3 (patch#14)
* use a partition on a disk instead of a whole disk without partition table (patch#14)
* reduce dependency on efitools, yet relying on its host tools (patch#14)
* modify pytests to catch up wth latest changes of "env -e" syntax (patch#15,#16)RFC (Sept 18, 2019)
AKASHI Takahiro (16):
efi_loader: addCONFIG_EFI_SECURE_BOOTconfig option
efi_loader: add signature verification functions
efi_loader: add signature database parser
efi_loader: variable: support variable authentication
efi_loader: variable: add secure boot state transition
efi_loader: variable: add VendorKeys variable
efi_loader:image_loader: support image authentication
efi_loader: set up secure boot
cmd: env: use appropriate guid for authenticated UEFI variable
cmd: env: add "-at" option to "env set -e" command
cmd: efidebug: add "test bootmgr" sub-commandefi_loader, pytest: set up secure boot environmentefi_loader, pytest: add UEFI secure boot tests (authenticated
variables)
efi_loader, pytest: add UEFI secure boot tests (image)
sandbox: add extra configurations for UEFI and related tests
travis: add packages for UEFI secure boot test.travis.yml | 11 +- cmd/efidebug.c | 78 +- cmd/nvedit.c | 5 +- cmd/nvedit_efi.c | 23 +- configs/sandbox64_defconfig | 3 + configs/sandbox_defconfig | 3 + include/efi_api.h | 87 ++ include/efi_loader.h | 91 +- lib/efi_loader/Kconfig | 18 + lib/efi_loader/Makefile | 1 + lib/efi_loader/efi_boottime.c | 10 +- lib/efi_loader/efi_image_loader.c | 460 ++++++++- lib/efi_loader/efi_setup.c | 38 + lib/efi_loader/efi_signature.c | 809 +++++++++++++++ lib/efi_loader/efi_variable.c | 951 ++++++++++++++++-- test/py/README.md | 8 + test/py/tests/test_efi_secboot/conftest.py | 151 +++ test/py/tests/test_efi_secboot/defs.py | 21 + .../py/tests/test_efi_secboot/test_authvar.py | 282 ++++++ test/py/tests/test_efi_secboot/test_signed.py | 117 +++ .../tests/test_efi_secboot/test_unsigned.py | 121 +++ 21 files changed, 3157 insertions(+), 131 deletions(-)create mode 100644
lib/efi_loader/efi_signature.ccreate mode 100644test/py/tests/test_efi_secboot/conftest.pycreate mode 100644test/py/tests/test_efi_secboot/defs.pycreate mode 100644test/py/tests/test_efi_secboot/test_authvar.pycreate mode 100644test/py/tests/test_efi_secboot/test_signed.pycreate mode 100644test/py/tests/test_efi_secboot/test_unsigned.py
v5 cover efi_loader: add secure boot support
In reply to AKASHI TakahiroQuoted 14 lines · AKASHI Takahiro
# Documentation for UEFI secure boot on U-Boot will be submitted in
# a separate patch in near future.One of major missing features in current UEFI implementation is "secure boot." The ultimate goal of my attempt is to implement image authentication based on signature and provide UEFI secure boot support which would be fully compliant with UEFI specification, section 32[1].
(The code was originally developed by Patrick Wildt.)Please note, however, this patch doesn't work on its own; there are a couple of functional dependencies[2] and [3], that I have submitted before. For complete workable patch set, see my repository[4], which also contains experimental timestamp-based revocation suuport.
Quoted 6 lines
My "non-volatile" support[5], which is under discussion, is not mandatory and so not included here, but this inevitably implies that, for example, signature database variables, like db and dbx, won't be persistent unless you explicitly run "env save" command.
Anyhow, Linaro is also working on implementing real "secure storage" solution based on TF-A and OP-TEE.
This patch series causes a failure in the Python test for EFI fit images. Please, compare
https://gitlab.denx.de/u-boot/custodians/u-boot-efi/pipelines/2279
and
https://gitlab.denx.de/u-boot/custodians/u-boot-efi/pipelines/2276
The failure is also reproducible in Travis CI:
https://travis-ci.org/xypron2/u-boot/builds/654050686
Best regards
Heinrich
Quoted 176 lines
Supported features:
* image authentication based on db and dbx
* supported signature types areEFI_CERT_SHA256_GUID(SHA256 digest for unsigned images)EFI_CERT_X509_GUID(x509 certificate for signed images)
* SecureBoot/SignatureSupport variables
* SetupMode and user mode
* variable authentication based on PK and KEKEFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
* basic pytest test casesUnsupported features: (marked as TODO in most cases in the source code,
and won't be included in this series)
* hash algorithms other than SHA256
* dbt: timestamp(RFC6131)-based certificate revocation
* dbr: OS recovery
* xxxDefault: default values for signature stores
* transition to AuditMode and DeployedMode
* recording rejected images inEFI_IMAGE_EXECUTION_INFO_TABLE
* verification "policy", in particular, check against signature's owner
* private authenticated variables
* variable authentication withEFI_VARIABLE_ENHANCED_AUTHENTICATED_ACCESS
* real secure storage support, including hardware-specific PK (Platform Key) installationTODO's other than "Unsupported features": (won't be included in this series)
* fail recovery, in particular, in modifying authenticated variables
* support read-only attributes of well-defined global variables in particular, "SignatureSupport"
* Extensive test suite (or more test cases) to confirm compatibility with EDK2
=> I requested EDK SCT community to add tests[6].Test:
* My pytest, included in this patch set, passed.
*efi_selftestpassed. (At least no regression.)
* Travis CI tests have passed.Known issues:
* efitools is used in pytest, and its version must be v1.5.2 or later. (Solution: You can defineEFITOOLS_PATHindefs.pyfor your own efitools.)Hints about how to use:
(Please see other documents, or my pytest scripts, for details.)
* You can create your own certificates with openssl.
* You can sign your application with sbsign (on Ubuntu).
* You can create raw data for signature database with efitools, and install/manage authenticated variables with "env -set -e" command or efitools' "UpdateVars.efi" application.[1] https://uefi.org/sites/default/files/resources/UEFI_Spec_2_8_final.pdf
[2] https://lists.denx.de/pipermail/u-boot/2019-November/390127.html
(import x509/pkcs7 parsers from linux)
[3] https://lists.denx.de/pipermail/u-boot/2020-January/398057.html
(extendrsa_verify()for UEFI secure boot)
[4] http://git.linaro.org/people/takahiro.akashi/u-boot.git/ efi/secboot
[5] https://lists.denx.de/pipermail/u-boot/2019-September/382835.html
(non-volatile variables support)
[6] https://bugzilla.tianocore.org/show_bug.cgi?id=2230Changes in v5 (Jan 28, 2020)
* rebased to pre-v2020.04-rc1 (fixed some merge conflicts)
* remove already-merged commits (v4's patch#1)
* fix a compile error caused by gcc 9.x (patch#4)
* returnSECURITY_VIOLATIONinstead ofACCESS_DENIEDif authentication fails (patch#7)
* useqsort()for section sorting (patch#7)
* add "efidebug test" sub-command (patch#11)
* addefi_start_image(SECURITY_VIOLATION)test (patch#14)Changes in v4 (Dec 18, 2019)
* adjustEFI_SECURE_BOOTdependencies due to a change of RSA extension patch v5 (patch#2)
* change "imply" to "select" against kconfig dependencies (patch#2)
* otherwise, no functional changesChanges in v3 (Dec 9, 2019)
* allow for arbitrary number of regions inefi_image_region_add()(patch#3, #5 and #8)
* remove a redundant check in a while loop atefi_sigstore_free()(patch#4)Changes in v2 (Nov 26, 2019)
* rebased to v2020.01-rc3
* renameIMAGE_DIRECTORY_ENTRY_CERTTABLEtoIMAGE_DIRECTORY_ENTRY_SECURITY(patch#1,#9)
* add comments (patch#1)
* drop v1's patch#2 as it is no longer necessary
* drop v1's patch#3 as other "SECURE_BOOT" architectures have renamed this option and no longer use it
* add structure descriptions (patch#3)
* rework hash calculation code inefi_signature_verify()and remove an odd constant, WinIndrectSha256 (patch#3)
* move travis.yml changes to a separate patch (patch#12, #16)
*yield_fixture()->fixture()(patch#12)
* callconsole.restart_uboot()at every test case (13,#14)
* add patch#15; enable UEFI-related configurations by default on sandbox
* add patch#16; modify Travis CI environment to run UEFI secure boot testChanges in v1 (Nov 13, 2019)
* rebased to v2020.01-rc
* remove already-merged patches
* re-work the patch set for easier reviews, including
- move a config definition patch forward (patch#4)
- refactor/rename verification functions (patch#5/#10)
- split signature database parser as a separate patch (patch#6)
- split secure state transition code as a separate patch (patch#8)
- move most part ofinit_secure_boot()intoinit_variables()(patch#8)
- split test environment setup from test patches (patch#14)
* add function descriptions (patch#5-#11)
* make sure the section list is sorted in ascending order in hash calculation of PE image (patch#10)
* add a new "-at" (authenticated access) option to "env -e" (patch#13)
* list required host packages, in particular udisks2, in pytest (patch#14)
* modifyconftest.pyto run under python3 (patch#14)
* use a partition on a disk instead of a whole disk without partition table (patch#14)
* reduce dependency on efitools, yet relying on its host tools (patch#14)
* modify pytests to catch up wth latest changes of "env -e" syntax (patch#15,#16)RFC (Sept 18, 2019)
AKASHI Takahiro (16):
efi_loader: addCONFIG_EFI_SECURE_BOOTconfig option
efi_loader: add signature verification functions
efi_loader: add signature database parser
efi_loader: variable: support variable authentication
efi_loader: variable: add secure boot state transition
efi_loader: variable: add VendorKeys variable
efi_loader:image_loader: support image authentication
efi_loader: set up secure boot
cmd: env: use appropriate guid for authenticated UEFI variable
cmd: env: add "-at" option to "env set -e" command
cmd: efidebug: add "test bootmgr" sub-commandefi_loader, pytest: set up secure boot environmentefi_loader, pytest: add UEFI secure boot tests (authenticated
variables)
efi_loader, pytest: add UEFI secure boot tests (image)
sandbox: add extra configurations for UEFI and related tests
travis: add packages for UEFI secure boot test.travis.yml | 11 +- cmd/efidebug.c | 78 +- cmd/nvedit.c | 5 +- cmd/nvedit_efi.c | 23 +- configs/sandbox64_defconfig | 3 + configs/sandbox_defconfig | 3 + include/efi_api.h | 87 ++ include/efi_loader.h | 91 +- lib/efi_loader/Kconfig | 18 + lib/efi_loader/Makefile | 1 + lib/efi_loader/efi_boottime.c | 10 +- lib/efi_loader/efi_image_loader.c | 460 ++++++++- lib/efi_loader/efi_setup.c | 38 + lib/efi_loader/efi_signature.c | 809 +++++++++++++++ lib/efi_loader/efi_variable.c | 951 ++++++++++++++++-- test/py/README.md | 8 + test/py/tests/test_efi_secboot/conftest.py | 151 +++ test/py/tests/test_efi_secboot/defs.py | 21 + .../py/tests/test_efi_secboot/test_authvar.py | 282 ++++++ test/py/tests/test_efi_secboot/test_signed.py | 117 +++ .../tests/test_efi_secboot/test_unsigned.py | 121 +++ 21 files changed, 3157 insertions(+), 131 deletions(-)create mode 100644
lib/efi_loader/efi_signature.ccreate mode 100644test/py/tests/test_efi_secboot/conftest.pycreate mode 100644test/py/tests/test_efi_secboot/defs.pycreate mode 100644test/py/tests/test_efi_secboot/test_authvar.pycreate mode 100644test/py/tests/test_efi_secboot/test_signed.pycreate mode 100644test/py/tests/test_efi_secboot/test_unsigned.py