Pull request efi-2022-07-rc7

UEFI:
 
 * correct verification of signed UEFI binaries
 -----BEGIN PGP SIGNATURE-----
 
 iQIzBAABCAAdFiEEbcT5xx8ppvoGt20zxIHbvCwFGsQFAmLFXSsACgkQxIHbvCwF
 GsQONg//UwDb+SeESDJPf49TH5v99l5JzcThILFJy6TEpYrM4w1Ez6n62BdNvLxI
 DKWVCF24Xk2vw0Uf4WdPoJPPkGKoKinOjv1fDzlBWrCpAxt3ecMqSAmo/J4CLNIG
 EqeC59Zxyy5eVhhOU4xXR5jOA3IcDhfT8NsPjoIyCNIN+vy1+xgTx+Z2GHZktn2R
 0SwhoMlmWZ/wUGW+N4DsYzJnPIx38t+cs3MBeUAr6QTOSCJJD8wGF97955S2X9JT
 0Vih7gw7OFoH1EmxldZHjfXOgX5Z+GLKM38ggjj0veg/SerIKFXXlnYyH2n0nAKt
 30oyy8k37snVBpP/XxBne51wR7eThElpauUDrZ5jkK17/WFdKsLRilRx1YwXXVJH
 QoMNqbbzvu7jpya+Wr2lNqMYB0q9i3heDXmpedhqt52LNuIHjAVmRD2jj7YpzgS4
 2pIW/xx3F0JnndDHaT4lfRXI72kEM74R1MC84uS9p05ONn03Vo5BFduwrZXaIws4
 rBY0XHcl/V/iKa5RXK/5YbQnTNsRIH5QI++B18fRxJKlDntCubT5cga6vnxpYk6t
 76KzqnOaBfKI8vebAPkoAY09c5tcovbrSQqOYzziqynQx7tWp0XOas4y5bGs5VaU
 pVGXp7AsoVrlHaHdnW5HwD74HmsH/1yrA9pkCIkV5w3sVuyDXjc=
 =TpNb
 -----END PGP SIGNATURE-----

Merge tag 'efi-2022-07-rc7' of https://source.denx.de/u-boot/custodians/u-boot-efi

Pull request efi-2022-07-rc7

UEFI:

* correct verification of signed UEFI binaries
This commit is contained in:
Tom Rini 2022-07-06 09:17:08 -04:00
commit 2d2c61ff04
12 changed files with 361 additions and 30 deletions

43
include/crypto/mscode.h Normal file
View file

@ -0,0 +1,43 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */
/* PE Binary parser bits
*
* Copyright (C) 2014 Red Hat, Inc. All Rights Reserved.
* Written by David Howells (dhowells@redhat.com)
*/
#include <crypto/pkcs7.h>
#ifndef __UBOOT__
#include <crypto/hash_info.h>
#endif
struct pefile_context {
#ifndef __UBOOT__
unsigned header_size;
unsigned image_checksum_offset;
unsigned cert_dirent_offset;
unsigned n_data_dirents;
unsigned n_sections;
unsigned certs_size;
unsigned sig_offset;
unsigned sig_len;
const struct section_header *secs;
#endif
/* PKCS#7 MS Individual Code Signing content */
const void *digest; /* Digest */
unsigned digest_len; /* Digest length */
const char *digest_algo; /* Digest algorithm */
};
#ifndef __UBOOT__
#define kenter(FMT, ...) \
pr_devel("==> %s("FMT")\n", __func__, ##__VA_ARGS__)
#define kleave(FMT, ...) \
pr_devel("<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
#endif
/*
* mscode_parser.c
*/
extern int mscode_parse(void *_ctx, const void *content_data, size_t data_len,
size_t asn1hdrlen);

View file

@ -931,6 +931,8 @@ struct efi_signature_store {
struct x509_certificate;
struct pkcs7_message;
bool efi_hash_regions(struct image_region *regs, int count,
void **hash, const char *hash_algo, int *len);
bool efi_signature_lookup_digest(struct efi_image_regions *regs,
struct efi_signature_store *db,
bool dbx);

View file

@ -82,4 +82,13 @@ config PKCS7_MESSAGE_PARSER
config PKCS7_VERIFY
bool
config MSCODE_PARSER
bool "MS authenticode parser"
select ASN1_DECODER
select ASN1_COMPILER
select OID_REGISTRY
help
This option provides support for parsing MicroSoft's Authenticode
in pkcs7 message.
endif # ASYMMETRIC_KEY_TYPE

View file

@ -55,3 +55,15 @@ obj-$(CONFIG_$(SPL_)PKCS7_VERIFY) += pkcs7_verify.o
$(obj)/pkcs7_parser.o: $(obj)/pkcs7.asn1.h
$(obj)/pkcs7.asn1.o: $(obj)/pkcs7.asn1.c $(obj)/pkcs7.asn1.h
#
# Signed PE binary-wrapped key handling
#
obj-$(CONFIG_$(SPL_)MSCODE_PARSER) += mscode.o
mscode-y := \
mscode_parser.o \
mscode.asn1.o
$(obj)/mscode_parser.o: $(obj)/mscode.asn1.h $(obj)/mscode.asn1.h
$(obj)/mscode.asn1.o: $(obj)/mscode.asn1.c $(obj)/mscode.asn1.h

28
lib/crypto/mscode.asn1 Normal file
View file

@ -0,0 +1,28 @@
--- Microsoft individual code signing data blob parser
---
--- Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
--- Written by David Howells (dhowells@redhat.com)
---
--- This program is free software; you can redistribute it and/or
--- modify it under the terms of the GNU General Public Licence
--- as published by the Free Software Foundation; either version
--- 2 of the Licence, or (at your option) any later version.
---
MSCode ::= SEQUENCE {
type SEQUENCE {
contentType ContentType,
parameters ANY
},
content SEQUENCE {
digestAlgorithm DigestAlgorithmIdentifier,
digest OCTET STRING ({ mscode_note_digest })
}
}
ContentType ::= OBJECT IDENTIFIER ({ mscode_note_content_type })
DigestAlgorithmIdentifier ::= SEQUENCE {
algorithm OBJECT IDENTIFIER ({ mscode_note_digest_algo }),
parameters ANY OPTIONAL
}

135
lib/crypto/mscode_parser.c Normal file
View file

@ -0,0 +1,135 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/* Parse a Microsoft Individual Code Signing blob
*
* Copyright (C) 2014 Red Hat, Inc. All Rights Reserved.
* Written by David Howells (dhowells@redhat.com)
*/
#define pr_fmt(fmt) "MSCODE: "fmt
#include <linux/kernel.h>
#ifndef __UBOOT__
#include <linux/slab.h>
#endif
#include <linux/err.h>
#include <linux/oid_registry.h>
#include <crypto/pkcs7.h>
#ifdef __UBOOT__
#include <crypto/mscode.h>
#else
#include "verify_pefile.h"
#endif
#include "mscode.asn1.h"
/*
* Parse a Microsoft Individual Code Signing blob
*/
int mscode_parse(void *_ctx, const void *content_data, size_t data_len,
size_t asn1hdrlen)
{
struct pefile_context *ctx = _ctx;
content_data -= asn1hdrlen;
data_len += asn1hdrlen;
pr_devel("Data: %zu [%*ph]\n", data_len, (unsigned)(data_len),
content_data);
return asn1_ber_decoder(&mscode_decoder, ctx, content_data, data_len);
}
/*
* Check the content type OID
*/
int mscode_note_content_type(void *context, size_t hdrlen,
unsigned char tag,
const void *value, size_t vlen)
{
enum OID oid;
oid = look_up_OID(value, vlen);
if (oid == OID__NR) {
char buffer[50];
sprint_oid(value, vlen, buffer, sizeof(buffer));
pr_err("Unknown OID: %s\n", buffer);
return -EBADMSG;
}
/*
* pesign utility had a bug where it was putting
* OID_msIndividualSPKeyPurpose instead of OID_msPeImageDataObjId
* So allow both OIDs.
*/
if (oid != OID_msPeImageDataObjId &&
oid != OID_msIndividualSPKeyPurpose) {
pr_err("Unexpected content type OID %u\n", oid);
return -EBADMSG;
}
return 0;
}
/*
* Note the digest algorithm OID
*/
int mscode_note_digest_algo(void *context, size_t hdrlen,
unsigned char tag,
const void *value, size_t vlen)
{
struct pefile_context *ctx = context;
char buffer[50];
enum OID oid;
oid = look_up_OID(value, vlen);
switch (oid) {
case OID_md4:
ctx->digest_algo = "md4";
break;
case OID_md5:
ctx->digest_algo = "md5";
break;
case OID_sha1:
ctx->digest_algo = "sha1";
break;
case OID_sha256:
ctx->digest_algo = "sha256";
break;
case OID_sha384:
ctx->digest_algo = "sha384";
break;
case OID_sha512:
ctx->digest_algo = "sha512";
break;
case OID_sha224:
ctx->digest_algo = "sha224";
break;
case OID__NR:
sprint_oid(value, vlen, buffer, sizeof(buffer));
pr_err("Unknown OID: %s\n", buffer);
return -EBADMSG;
default:
pr_err("Unsupported content type: %u\n", oid);
return -ENOPKG;
}
return 0;
}
/*
* Note the digest we're guaranteeing with this certificate
*/
int mscode_note_digest(void *context, size_t hdrlen,
unsigned char tag,
const void *value, size_t vlen)
{
struct pefile_context *ctx = context;
ctx->digest = kmemdup(value, vlen, GFP_KERNEL);
if (!ctx->digest)
return -ENOMEM;
ctx->digest_len = vlen;
return 0;
}

View file

@ -366,6 +366,7 @@ config EFI_SECURE_BOOT
select X509_CERTIFICATE_PARSER
select PKCS7_MESSAGE_PARSER
select PKCS7_VERIFY
select MSCODE_PARSER
select EFI_SIGNATURE_SUPPORT
help
Select this option to enable EFI secure boot support.

View file

@ -16,6 +16,7 @@
#include <malloc.h>
#include <pe.h>
#include <sort.h>
#include <crypto/mscode.h>
#include <crypto/pkcs7_parser.h>
#include <linux/err.h>
@ -238,7 +239,7 @@ efi_status_t efi_image_region_add(struct efi_image_regions *regs,
int i, j;
if (regs->num >= regs->max) {
EFI_PRINT("%s: no more room for regions\n", __func__);
log_err("%s: no more room for regions\n", __func__);
return EFI_OUT_OF_RESOURCES;
}
@ -263,7 +264,7 @@ efi_status_t efi_image_region_add(struct efi_image_regions *regs,
}
/* new data overlapping registered region */
EFI_PRINT("%s: new region already part of another\n", __func__);
log_err("%s: new region already part of another\n", __func__);
return EFI_INVALID_PARAMETER;
}
@ -434,8 +435,8 @@ bool efi_image_parse(void *efi, size_t len, struct efi_image_regions **regp,
bytes_hashed = opt->SizeOfHeaders;
align = opt->FileAlignment;
} else {
EFI_PRINT("%s: Invalid optional header magic %x\n", __func__,
nt->OptionalHeader.Magic);
log_err("%s: Invalid optional header magic %x\n", __func__,
nt->OptionalHeader.Magic);
goto err;
}
@ -445,7 +446,7 @@ bool efi_image_parse(void *efi, size_t len, struct efi_image_regions **regp,
nt->FileHeader.SizeOfOptionalHeader);
sorted = calloc(sizeof(IMAGE_SECTION_HEADER *), num_sections);
if (!sorted) {
EFI_PRINT("%s: Out of memory\n", __func__);
log_err("%s: Out of memory\n", __func__);
goto err;
}
@ -464,7 +465,7 @@ bool efi_image_parse(void *efi, size_t len, struct efi_image_regions **regp,
efi_image_region_add(regs, efi + sorted[i]->PointerToRawData,
efi + sorted[i]->PointerToRawData + size,
0);
EFI_PRINT("section[%d](%s): raw: 0x%x-0x%x, virt: %x-%x\n",
log_debug("section[%d](%s): raw: 0x%x-0x%x, virt: %x-%x\n",
i, sorted[i]->Name,
sorted[i]->PointerToRawData,
sorted[i]->PointerToRawData + size,
@ -478,7 +479,7 @@ bool efi_image_parse(void *efi, size_t len, struct efi_image_regions **regp,
/* 3. Extra data excluding Certificates Table */
if (bytes_hashed + authsz < len) {
EFI_PRINT("extra data for hash: %zu\n",
log_debug("extra data for hash: %zu\n",
len - (bytes_hashed + authsz));
efi_image_region_add(regs, efi + bytes_hashed,
efi + len - authsz, 0);
@ -487,18 +488,18 @@ bool efi_image_parse(void *efi, size_t len, struct efi_image_regions **regp,
/* Return Certificates Table */
if (authsz) {
if (len < authoff + authsz) {
EFI_PRINT("%s: Size for auth too large: %u >= %zu\n",
__func__, authsz, len - authoff);
log_err("%s: Size for auth too large: %u >= %zu\n",
__func__, authsz, len - authoff);
goto err;
}
if (authsz < sizeof(*auth)) {
EFI_PRINT("%s: Size for auth too small: %u < %zu\n",
__func__, authsz, sizeof(*auth));
log_err("%s: Size for auth too small: %u < %zu\n",
__func__, authsz, sizeof(*auth));
goto err;
}
*auth = efi + authoff;
*auth_len = authsz;
EFI_PRINT("WIN_CERTIFICATE: 0x%x, size: 0x%x\n", authoff,
log_debug("WIN_CERTIFICATE: 0x%x, size: 0x%x\n", authoff,
authsz);
} else {
*auth = NULL;
@ -516,6 +517,51 @@ err:
}
#ifdef CONFIG_EFI_SECURE_BOOT
/**
* efi_image_verify_digest - verify image's message digest
* @regs: Array of memory regions to digest
* @msg: Signature in pkcs7 structure
*
* @regs contains all the data in a PE image to digest. Calculate
* a hash value based on @regs and compare it with a messaged digest
* in the content (SpcPeImageData) of @msg's contentInfo.
*
* Return: true if verified, false if not
*/
static bool efi_image_verify_digest(struct efi_image_regions *regs,
struct pkcs7_message *msg)
{
struct pefile_context ctx;
void *hash;
int hash_len, ret;
const void *data;
size_t data_len;
size_t asn1hdrlen;
/* get pkcs7's contentInfo */
ret = pkcs7_get_content_data(msg, &data, &data_len, &asn1hdrlen);
if (ret < 0 || !data)
return false;
/* parse data and retrieve a message digest into ctx */
ret = mscode_parse(&ctx, data, data_len, asn1hdrlen);
if (ret < 0)
return false;
/* calculate a hash value of PE image */
hash = NULL;
if (!efi_hash_regions(regs->reg, regs->num, &hash, ctx.digest_algo,
&hash_len))
return false;
/* match the digest */
if (ctx.digest_len != hash_len || memcmp(ctx.digest, hash, hash_len))
return false;
return true;
}
/**
* efi_image_authenticate() - verify a signature of signed image
* @efi: Pointer to image
@ -549,7 +595,7 @@ static bool efi_image_authenticate(void *efi, size_t efi_size)
size_t auth_size;
bool ret = false;
EFI_PRINT("%s: Enter, %d\n", __func__, ret);
log_debug("%s: Enter, %d\n", __func__, ret);
if (!efi_secure_boot_enabled())
return true;
@ -560,7 +606,7 @@ static bool efi_image_authenticate(void *efi, size_t efi_size)
if (!efi_image_parse(new_efi, efi_size, &regs, &wincerts,
&wincerts_len)) {
EFI_PRINT("Parsing PE executable image failed\n");
log_err("Parsing PE executable image failed\n");
goto out;
}
@ -569,18 +615,18 @@ static bool efi_image_authenticate(void *efi, size_t efi_size)
*/
db = efi_sigstore_parse_sigdb(u"db");
if (!db) {
EFI_PRINT("Getting signature database(db) failed\n");
log_err("Getting signature database(db) failed\n");
goto out;
}
dbx = efi_sigstore_parse_sigdb(u"dbx");
if (!dbx) {
EFI_PRINT("Getting signature database(dbx) failed\n");
log_err("Getting signature database(dbx) failed\n");
goto out;
}
if (efi_signature_lookup_digest(regs, dbx, true)) {
EFI_PRINT("Image's digest was found in \"dbx\"\n");
log_debug("Image's digest was found in \"dbx\"\n");
goto out;
}
@ -602,12 +648,12 @@ static bool efi_image_authenticate(void *efi, size_t efi_size)
break;
if (wincert->dwLength <= sizeof(*wincert)) {
EFI_PRINT("dwLength too small: %u < %zu\n",
log_debug("dwLength too small: %u < %zu\n",
wincert->dwLength, sizeof(*wincert));
continue;
}
EFI_PRINT("WIN_CERTIFICATE_TYPE: 0x%x\n",
log_debug("WIN_CERTIFICATE_TYPE: 0x%x\n",
wincert->wCertificateType);
auth = (u8 *)wincert + sizeof(*wincert);
@ -617,12 +663,12 @@ static bool efi_image_authenticate(void *efi, size_t efi_size)
break;
if (auth_size <= sizeof(efi_guid_t)) {
EFI_PRINT("dwLength too small: %u < %zu\n",
log_debug("dwLength too small: %u < %zu\n",
wincert->dwLength, sizeof(*wincert));
continue;
}
if (guidcmp(auth, &efi_guid_cert_type_pkcs7)) {
EFI_PRINT("Certificate type not supported: %pUs\n",
log_debug("Certificate type not supported: %pUs\n",
auth);
ret = false;
goto out;
@ -632,19 +678,22 @@ static bool efi_image_authenticate(void *efi, size_t efi_size)
auth_size -= sizeof(efi_guid_t);
} else if (wincert->wCertificateType
!= WIN_CERT_TYPE_PKCS_SIGNED_DATA) {
EFI_PRINT("Certificate type not supported\n");
log_debug("Certificate type not supported\n");
ret = false;
goto out;
}
msg = pkcs7_parse_message(auth, auth_size);
if (IS_ERR(msg)) {
EFI_PRINT("Parsing image's signature failed\n");
log_err("Parsing image's signature failed\n");
msg = NULL;
continue;
}
/*
* verify signatures in pkcs7's signedInfos which are
* to authenticate the integrity of pkcs7's contentInfo.
*
* NOTE:
* UEFI specification defines two signature types possible
* in signature database:
@ -666,23 +715,32 @@ static bool efi_image_authenticate(void *efi, size_t efi_size)
/* try black-list first */
if (efi_signature_verify_one(regs, msg, dbx)) {
ret = false;
EFI_PRINT("Signature was rejected by \"dbx\"\n");
log_debug("Signature was rejected by \"dbx\"\n");
goto out;
}
if (!efi_signature_check_signers(msg, dbx)) {
ret = false;
EFI_PRINT("Signer(s) in \"dbx\"\n");
log_debug("Signer(s) in \"dbx\"\n");
goto out;
}
/* try white-list */
if (efi_signature_verify(regs, msg, db, dbx)) {
if (!efi_signature_verify(regs, msg, db, dbx)) {
log_debug("Signature was not verified by \"db\"\n");
continue;
}
/*
* now calculate an image's hash value and compare it with
* a messaged digest embedded in pkcs7's contentInfo
*/
if (efi_image_verify_digest(regs, msg)) {
ret = true;
continue;
}
EFI_PRINT("Signature was not verified by \"db\"\n");
log_debug("Message digest doesn't match\n");
}
@ -698,7 +756,7 @@ out:
if (new_efi != efi)
free(new_efi);
EFI_PRINT("%s: Exit, %d\n", __func__, ret);
log_debug("%s: Exit, %d\n", __func__, ret);
return ret;
}
#else

View file

@ -125,8 +125,8 @@ struct pkcs7_message *efi_parse_pkcs7_header(const void *buf,
*
* Return: true on success, false on error
*/
static bool efi_hash_regions(struct image_region *regs, int count,
void **hash, const char *hash_algo, int *len)
bool efi_hash_regions(struct image_region *regs, int count,
void **hash, const char *hash_algo, int *len)
{
int ret, hash_len;

View file

@ -105,6 +105,9 @@ def efi_boot_env(request, u_boot_config):
# Sign already-signed image with another key
check_call('cd %s; sbsign --key db1.key --cert db1.crt --output helloworld.efi.signed_2sigs helloworld.efi.signed'
% mnt_point, shell=True)
# Create a corrupted signed image
check_call('cd %s; sh %s/test/py/tests/test_efi_secboot/forge_image.sh helloworld.efi.signed helloworld_forged.efi.signed'
% (mnt_point, u_boot_config.source_dir), shell=True)
# Digest image
check_call('cd %s; %shash-to-efi-sig-list helloworld.efi db_hello.hash; %ssign-efi-sig-list -t "2020-04-07" -c KEK.crt -k KEK.key db db_hello.hash db_hello.auth'
% (mnt_point, EFITOOLS_PATH, EFITOOLS_PATH),

View file

@ -0,0 +1,5 @@
# SPDX-License-Identifier: GPL-2.0-or-later
#!/bin/sh
replace_exp="s/H\0e\0l\0l\0o\0/h\0E\0L\0L\0O\0/g"
perl -p -e ${replace_exp} < $1 > $2

View file

@ -334,3 +334,38 @@ class TestEfiSignedImage(object):
'efidebug test bootmgr'])
assert '\'HELLO\' failed' in ''.join(output)
assert 'efi_start_image() returned: 26' in ''.join(output)
def test_efi_signed_image_auth8(self, u_boot_console, efi_boot_env):
"""
Test Case 8 - Secure boot is in force,
Same as Test Case 2 but the image binary to be loaded
was willfully modified (forged)
Must be rejected.
"""
u_boot_console.restart_uboot()
disk_img = efi_boot_env
with u_boot_console.log.section('Test Case 8a'):
# Test Case 8a, Secure boot is not yet forced
output = u_boot_console.run_command_list([
'host bind 0 %s' % disk_img,
'efidebug boot add -b 1 HELLO1 host 0:1 /helloworld_forged.efi.signed -s ""',
'efidebug boot next 1',
'efidebug test bootmgr'])
assert('hELLO, world!' in ''.join(output))
with u_boot_console.log.section('Test Case 8b'):
# Test Case 8b, Install signature database and verify the image
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 'Failed to set EFI variable' not in ''.join(output)
output = u_boot_console.run_command_list([
'efidebug boot next 1',
'efidebug test bootmgr'])
assert(not 'hELLO, world!' in ''.join(output))
assert('\'HELLO1\' failed' in ''.join(output))
assert('efi_start_image() returned: 26' in ''.join(output))