Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/apple.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ jobs:
make O="build-$config" CROSS_COMPILE=aarch64-linux-gnu- ARCH=arm -j2
python3 tools/aurora_release.py --version "$RELEASE_VERSION" --config "$config" --build-dir "build-$config" --compiler aarch64-linux-gnu-gcc
done
# aurora-secboot fills this region before it signs Stage 2.
python3 tools/aurora_uefi_seed.py build-apple_m1/u-boot-nodtb.bin
cd dist
sha256sum --check SHA256SUMS
- uses: actions/upload-artifact@v7
Expand Down
137 changes: 137 additions & 0 deletions arch/arm/mach-apple/board.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
#include <dm.h>
#include <dm/uclass-internal.h>
#include <command.h>
#include <console.h>
#include <efi_loader.h>
#include <efi_variable.h>
#include <env.h>
#include <env_internal.h>
#include <fs.h>
#include <u-boot/uuid.h>
#include <lmb.h>
Expand All @@ -20,6 +23,7 @@
#include <asm/armv8/mmu.h>
#include <asm/global_data.h>
#include <asm/io.h>
#include <asm/sections.h>
#include <asm/system.h>
#include <asm/arch/rtkit.h>
#include <asm/arch/j713_fdt.h>
Expand Down Expand Up @@ -1496,6 +1500,136 @@ char *env_fat_get_dev_part(void)
return asahi_esp_devpart();
}

/*
* Aura's enforce policy.
*
* aura records what each boot stage checked in /chosen/aurora,boot-security
* of the devicetree it hands over. When any stage enforced, or the record
* says the chain up to here was verified, the boot stays on a verified
* path: U-Boot itself was checked, but what it reads from the ESP is not,
* so it must not read its saved environment, offer a console, or start
* anything UEFI Secure Boot did not verify, audit build or not. The keys
* come from the variable seed built into this image
* (CONFIG_EFI_VARIABLES_PRESEED), which aurora-secboot fills before it
* signs Stage 2.
*/
static bool apple_boot_enforced(void)
{
ofnode node, entry;
const char *val;

node = ofnode_path("/chosen/aurora,boot-security");
if (!ofnode_valid(node))
return false;

val = ofnode_read_string(node, "policy");
if (val && !strcmp(val, "enforce"))
return true;
val = ofnode_read_string(node, "state");
if (val && !strcmp(val, "verified"))
return true;
ofnode_for_each_subnode(entry, node) {
val = ofnode_read_string(entry, "policy");
if (val && !strcmp(val, "enforce"))
return true;
}

return false;
}

/* Secure Boot refuses, rather than audits, under an enforcing aura. */
bool efi_aurora_enforce_policy(void)
{
return apple_boot_enforced();
}

/*
* A saved environment on the ESP could change the boot, so under the
* enforce policy the built-in one is used and nothing is saved. Otherwise
* the locations are the build's: a FAT build reads and writes uboot.env as
* before, without the nowhere location as a fallback.
*/
enum env_location env_get_location(enum env_operation op, int prio)
{
enum env_location loc;

if (CONFIG_IS_ENABLED(ENV_IS_NOWHERE) && apple_boot_enforced())
return prio ? ENVL_UNKNOWN : ENVL_NOWHERE;

loc = arch_env_get_location(op, prio);
if (IS_ENABLED(CONFIG_ENV_IS_IN_FAT) && loc == ENVL_NOWHERE)
return ENVL_UNKNOWN;
return loc;
}

/* Whether the built-in variable seed enrols a platform key. */
static bool apple_seed_has_pk(void)
{
#if CONFIG_IS_ENABLED(EFI_VARIABLES_PRESEED)
const struct efi_var_file *file = (void *)__efi_var_file_begin;
const struct efi_var_entry *var;
const u8 *end, *data;
static const u16 pk[] = u"PK";

if (file->magic != EFI_VAR_FILE_MAGIC ||
file->length < sizeof(*file) || file->length > EFI_VAR_BUF_SIZE)
return false;

end = (const u8 *)file + file->length;
for (var = file->var; (const u8 *)(var + 1) < end;
var = (void *)ALIGN((uintptr_t)data + var->length, 8)) {
size_t room = (end - (const u8 *)var->name) / sizeof(u16);
size_t len = u16_strnlen(var->name, room);

if (len == room)
return false;
data = (const u8 *)(var->name + len + 1);
if (var->length > end - data)
return false;
if (var->length && !u16_strcmp(var->name, pk) &&
!guidcmp(&var->guid, &efi_global_variable_guid))
return true;
}
#endif

return false;
}

/*
* The only boot under the enforce policy: the removable-media EFI path of
* the internal ESP, which on the secure path is a signed UKI, started with
* UEFI Secure Boot in force. Anything else, or a return, resets.
*/
static const char * const enforced_env[][2] = {
{ "aurora_secure_boot",
"echo AURORA_UBOOT_SECURE: verified EFI on internal ESP; "
"if test -n \"${fw_dev_part}\"; then "
"load nvme ${fw_dev_part} ${kernel_addr_r} EFI/BOOT/BOOTAA64.EFI && "
"bootefi ${kernel_addr_r} ${fdtcontroladdr}; fi; "
"echo AURORA_UBOOT_SECURE_FAIL: no verified EFI application started; "
"sleep 10; reset" },
{ "aurora_secure_nokeys",
"echo AURORA_UBOOT_SECURE_FAIL: aura enforces, but this U-Boot "
"carries no Secure Boot keys; sleep 10; reset" },
};

static void apple_enforced_late_init(void)
{
bool ok = IS_ENABLED(CONFIG_EFI_SECURE_BOOT) &&
CONFIG_IS_ENABLED(ENV_IS_NOWHERE) && apple_seed_has_pk();
int i;

for (i = 0; i < ARRAY_SIZE(enforced_env); i++)
env_set(enforced_env[i][0], enforced_env[i][1]);

env_set("bootcmd", ok ? "run aurora_secure_boot; reset" :
"run aurora_secure_nokeys; reset");
/* Boot at once, without looking for a key, and never stop on Ctrl-C. */
env_set("bootdelay", "-2");
env_set("preboot", "");
disable_ctrlc(1);
}

#define KERNEL_COMP_SIZE SZ_128M

#define lmb_alloc(size, addr) lmb_alloc_mem(LMB_MEM_ALLOC_ANY, SZ_2M, addr, size, LMB_NONE)
Expand Down Expand Up @@ -1741,6 +1875,9 @@ int board_late_init(void)
if (status)
log_warning("late_init: Failed to set run time variables\n");

if (apple_boot_enforced())
apple_enforced_late_init();

return 0;
}

Expand Down
Binary file added board/apple/mac/uefi-seed.var
Binary file not shown.
7 changes: 7 additions & 0 deletions configs/apple_m1_defconfig
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ CONFIG_NR_DRAM_BANKS=16
CONFIG_DEFAULT_DEVICE_TREE="apple/t8103-j274"
CONFIG_SYS_BOOTM_LEN=0x800000
CONFIG_SYS_LOAD_ADDR=0x0
CONFIG_EFI_SECURE_BOOT=y
CONFIG_EFI_AURORA_SECBOOT_AUDIT=y
CONFIG_EFI_VARIABLES_PRESEED=y
CONFIG_EFI_VAR_SEED_FILE="board/apple/mac/uefi-seed.var"
CONFIG_FIT=y
CONFIG_FIT_SIGNATURE=y
CONFIG_BOOTSTAGE=y
CONFIG_BOOTSTAGE_FDT=y
CONFIG_BOOTDELAY=0
Expand All @@ -21,6 +27,7 @@ CONFIG_CMD_SELECT_FONT=y
CONFIG_OF_UPSTREAM_BUILD_VENDOR=y
CONFIG_OF_UPSTREAM_VENDOR="apple"
CONFIG_SYS_FDT_PAD=0x40000
CONFIG_ENV_IS_NOWHERE=y
CONFIG_NO_NET=y
CONFIG_SYS_64BIT_LBA=y
CONFIG_APPLE_SPI_KEYB=y
Expand Down
36 changes: 36 additions & 0 deletions doc/board/apple/m1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,42 @@ Instructions on how to install U-Boot on your Mac can be found at:

Just replace ``m1n1.macho`` with ``u-boot.macho`` in the instructions.

Aura's enforce policy
---------------------

When U-Boot is started by aura (Aurora Silicon's m1n1-based boot chain),
aura records what each stage checked in ``/chosen/aurora,boot-security`` of
the device tree it passes, and U-Boot adds an entry for every EFI image it
loads (``CONFIG_EFI_AURORA_MEASURE``). If any stage enforced
(``policy = "enforce"`` on the node or on one of its ``measurement@N``
entries) or the record says the chain is ``verified``, U-Boot itself was
checked, but nothing it reads from the ESP was, so U-Boot keeps the boot on
a verified path:

- the saved ``uboot.env`` on the ESP is not read, and ``saveenv`` stores
nothing (``CONFIG_ENV_IS_NOWHERE``; without an enforcing stage a FAT build
reads and writes ``uboot.env`` as before);
- the boot starts at once (``bootdelay`` is -2), Ctrl-C is ignored and
``preboot`` is empty, so there is no console to interrupt it;
- the only boot is ``EFI/BOOT/BOOTAA64.EFI`` on the internal ESP, started
with ``bootefi`` under UEFI Secure Boot; there is no ``booti``, bootflow,
boot script or USB boot. If it does not start, or returns, U-Boot resets;
- Secure Boot refuses an image that fails authentication even in an audit
build (``CONFIG_EFI_AURORA_SECBOOT_AUDIT``), which otherwise only records
the verdict;
- the keys (PK, KEK, db and dbx) come only from the variable seed built into
U-Boot (``CONFIG_EFI_VARIABLES_PRESEED``), never from ``ubootefi.var`` on
the ESP. ``apple_m1_defconfig`` builds in ``board/apple/mac/uefi-seed.var``:
an empty seed in a fixed 16 KiB region that ends in the marker
``AURORA-UEFI-SEED``, which ``aurora-secboot`` fills with the owner's keys
before it signs Stage 2, so aura's signature covers them.
``tools/apple_secboot_seed.py`` writes a seed in the same region format for
``apple_secboot.config`` builds, and ``tools/aurora_uefi_seed.py`` checks
an image. With no platform key in the seed, U-Boot refuses and resets.

Without an enforcing stage the default build boots as before; with keys in
its seed it audits (records each verdict and starts the image).

Debug UART
----------

Expand Down
2 changes: 2 additions & 0 deletions include/efi_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -1140,6 +1140,8 @@ struct efi_signature_store *efi_sigstore_parse_sigdb(u16 *name);

bool efi_secure_boot_enabled(void);
bool efi_secure_boot_audit(void);
/* Whether the platform requires Secure Boot to refuse rather than audit. */
bool efi_aurora_enforce_policy(void);

bool efi_capsule_auth_enabled(void);

Expand Down
5 changes: 4 additions & 1 deletion lib/efi_loader/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ obj-$(CONFIG_EFI_ECPT) += efi_conformance.o
obj-$(CONFIG_EFI_DEBUG_SUPPORT) += efi_debug_support.o

EFI_VAR_SEED_FILE := $(subst $\",,$(CONFIG_EFI_VAR_SEED_FILE))
$(obj)/efi_var_seed.o: $(srctree)/$(EFI_VAR_SEED_FILE)
# An absolute seed path is used as it is; a relative one is relative to the
# source tree, also with O=.
$(obj)/efi_var_seed.o: $(if $(filter /%,$(EFI_VAR_SEED_FILE)),,$(srctree)/)$(EFI_VAR_SEED_FILE)
AFLAGS_efi_var_seed.o += -I$(srctree)

ifeq ($(CONFIG_EFI_CAPSULE_AUTHENTICATE),y)
capsule_crt_path=($(subst $(quote),,$(CONFIG_EFI_CAPSULE_CRT_FILE)))
Expand Down
17 changes: 16 additions & 1 deletion lib/efi_loader/efi_var_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,8 @@ static efi_status_t efi_transfer_secure_state(enum efi_secure_mode mode)
if (ret != EFI_SUCCESS)
goto err;
} else if (mode == EFI_MODE_USER &&
IS_ENABLED(CONFIG_EFI_AURORA_SECBOOT_AUDIT)) {
IS_ENABLED(CONFIG_EFI_AURORA_SECBOOT_AUDIT) &&
!efi_aurora_enforce_policy()) {
/*
* Keys are installed, but images are only checked and
* recorded: tell the OS that Secure Boot is not enforced.
Expand Down Expand Up @@ -391,6 +392,20 @@ bool efi_secure_boot_audit(void)
return efi_secure_boot_audit_mode;
}

/**
* efi_aurora_enforce_policy() - whether Secure Boot must refuse, not audit
*
* An audit build (CONFIG_EFI_AURORA_SECBOOT_AUDIT) still refuses images that
* fail authentication when the platform says the boot chain before U-Boot
* enforced (on Apple silicon: aura's /chosen/aurora,boot-security).
*
* Return: true to enforce
*/
__weak bool efi_aurora_enforce_policy(void)
{
return false;
}

enum efi_auth_var_type efi_auth_var_get_type(const u16 *name,
const efi_guid_t *guid)
{
Expand Down
14 changes: 13 additions & 1 deletion tools/apple_secboot_seed.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,21 @@
The lists may hold the distribution's keys or keys the machine owner made.
A list that holds one of Microsoft's UEFI CAs is refused: they would admit
every boot loader Microsoft has signed, revoked ones included.

The file is the fixed seed region of board/apple/mac/uefi-seed.var: the
variables, zeros, and a 32-byte marker (AURORA-UEFI-SEED, the region size,
version 1) at the end of REGION_SIZE bytes. aurora-secboot finds that region
in the U-Boot of a Stage 2 and fills it with the owner's keys before it signs
Stage 2; tools/aurora_uefi_seed.py checks it.
"""
import argparse
import struct
import uuid
import zlib

MAGIC = 0x0161566966456255
REGION_SIZE = 16384
MARKER = b"AURORA-UEFI-SEED" + struct.pack("<II", REGION_SIZE, 1) + bytes(8)
GLOBAL = uuid.UUID("8be4df61-93ca-11d2-aa0d-00e098032b8c")
IMAGE_DB = uuid.UUID("d719b2cb-3d3a-4596-a3bc-dad00e67656f")
CERT_SHA256 = uuid.UUID("c1c41626-504c-4092-aca9-41f936934328")
Expand Down Expand Up @@ -89,7 +97,11 @@ def main():
ents += entry(IMAGE_DB, "db", lists["db"])
ents += entry(IMAGE_DB, "dbx", open(a.dbx, "rb").read() if a.dbx else empty_dbx())
hdr = struct.pack("<QQII", 0, MAGIC, 24 + len(ents), zlib.crc32(ents) & 0xFFFFFFFF)
open(a.output, "wb").write(hdr + ents)
seed = hdr + ents
if len(seed) > REGION_SIZE - len(MARKER):
raise SystemExit(f"the variables take {len(seed)} bytes; the seed region holds "
f"{REGION_SIZE - len(MARKER)}")
open(a.output, "wb").write(seed + bytes(REGION_SIZE - len(MARKER) - len(seed)) + MARKER)


if __name__ == "__main__":
Expand Down
57 changes: 57 additions & 0 deletions tools/aurora_uefi_seed.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/usr/bin/env python3
# SPDX-License-Identifier: GPL-2.0+
"""Check the Secure Boot variable seed region of an Apple U-Boot image.

U-Boot built with CONFIG_EFI_VARIABLES_PRESEED and board/apple/mac/uefi-seed.var
(or a seed from tools/apple_secboot_seed.py) carries a fixed-size region that
aurora-secboot fills with the owner's PK, KEK, db and dbx before it signs
Stage 2. It finds the region by the marker at its end, so the image must
hold that marker exactly once, after a valid efi_var_file header.
"""
import struct
import sys
import zlib

MAGIC = 0x0161566966456255
MARKER = b"AURORA-UEFI-SEED"


def regions(image):
"""(offset, size) of each seed region in image."""
out, at = [], image.find(MARKER)
while at >= 0:
size, version = struct.unpack_from("<II", image, at + len(MARKER))
out.append((at + 32 - size, size, version))
at = image.find(MARKER, at + 1)
return out


def check(image):
found = regions(image)
if len(found) != 1:
raise ValueError(f"{len(found)} seed regions, expected exactly one")
start, size, version = found[0]
if version != 1 or size < 4096 or start < 0:
raise ValueError(f"bad seed region: size {size}, version {version}")
reserved, magic, length, crc = struct.unpack_from("<QQII", image, start)
if reserved or magic != MAGIC or length < 24 or length > size - 32:
raise ValueError("the seed region does not start with an efi_var_file header")
if zlib.crc32(image[start + 24:start + length]) != crc:
raise ValueError("the seed region's CRC does not match")
return start, size, length


def main():
if len(sys.argv) != 2:
sys.exit(f"usage: {sys.argv[0]} u-boot-nodtb.bin")
with open(sys.argv[1], "rb") as f:
image = f.read()
try:
start, size, length = check(image)
except ValueError as e:
sys.exit(f"{sys.argv[1]}: {e}")
print(f"{sys.argv[1]}: seed region at {start:#x}, {size} bytes, {length - 24} bytes of variables")


if __name__ == "__main__":
main()