From 3539f5dd1a9056dabf716011f30b25ce2738ab6a Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Mon, 23 Nov 2026 15:13:50 +1100 Subject: [PATCH 1/4] soc: apple: aurora-boot-security: add KUnit tests The boot security record decides what /sys/firmware/aurora/boot-security reports and whether the chain counts as verified. Add suites that build the record in the live devicetree with an of_changeset and call the driver's own functions: - the Stage 1 decision for every combination of stage, policy and verdict, one valid entry among many, none among many, values without a NUL terminator and string lists; - the effective chain state: "verified" only when the record says exactly that and an enforcing Stage 1 verified it, demoted otherwise; - what each sysfs attribute shows for absent, empty, unterminated, short, long and over-a-page values, with a guard area after the buffer; - init on a real /chosen record: none, a compatible that is not v1, a verified chain, a verified claim over an auditing Stage 1, and v1 listed second. Tests only, no functional change. Signed-off-by: Ryan Murray --- drivers/soc/apple/Kconfig | 17 + drivers/soc/apple/aurora-boot-security-test.c | 1047 +++++++++++++++++ drivers/soc/apple/aurora-boot-security.c | 4 + .../apple/aurora-boot-security.kunitconfig | 6 + 4 files changed, 1074 insertions(+) create mode 100644 drivers/soc/apple/aurora-boot-security-test.c create mode 100644 drivers/soc/apple/aurora-boot-security.kunitconfig diff --git a/drivers/soc/apple/Kconfig b/drivers/soc/apple/Kconfig index a9cf80938dc4e..d89e81cc29a1c 100644 --- a/drivers/soc/apple/Kconfig +++ b/drivers/soc/apple/Kconfig @@ -433,6 +433,23 @@ config APPLE_AURORA_BOOT_SECURITY_LOCKDOWN locked down by this option; lockdown= on the command line still applies. +config APPLE_AURORA_BOOT_SECURITY_KUNIT_TEST + bool "KUnit tests for the Aurora boot security record" if !KUNIT_ALL_TESTS + depends on APPLE_AURORA_BOOT_SECURITY=y && KUNIT=y && OF_DYNAMIC + default KUNIT_ALL_TESTS + help + Build boot records in the live device tree and check what the + driver decides from them: which Stage 1 measurement makes the + chain enforced, when a claimed "verified" state is demoted, which + records are ignored, and what every file in + /sys/firmware/aurora/boot_security/ shows for absent, empty, + unterminated, short, long and oversized properties. The init + cases add and remove a test /chosen/aurora,boot-security and are + skipped when APPLE_AURORA_BOOT_SECURITY_LOCKDOWN is set, so the + test kernel is never locked down. No hardware is touched. + + If unsure, say N. + endmenu endif diff --git a/drivers/soc/apple/aurora-boot-security-test.c b/drivers/soc/apple/aurora-boot-security-test.c new file mode 100644 index 0000000000000..9131de01943b4 --- /dev/null +++ b/drivers/soc/apple/aurora-boot-security-test.c @@ -0,0 +1,1047 @@ +// SPDX-License-Identifier: GPL-2.0-only OR MIT +/* + * KUnit tests for the Aurora boot security record reader. + * + * Each case builds a boot record in the live device tree with an + * of_changeset (a test node under the root, or the real + * /chosen/aurora,boot-security for the init cases) and runs the driver's + * own decision and sysfs show functions on it: which Stage 1 measurement + * counts as enforcing, how the chain state is demoted, and what every + * sysfs file prints for absent, empty, unterminated, short, long and + * oversized properties. No hardware is touched. + * + * Included from aurora-boot-security.c. + */ + +#include +#include + +#define BS_GUARD 0xa5 + +/* No property of this name: bs_meas() leaves it out. */ +#define BS_ABSENT NULL + +struct bs_ctx { + struct of_changeset cs; + struct device_node *np; + bool applied; +}; + +struct bs_meas_spec { + const char *stage, *policy, *component, *verdict; + const u8 *sha; + int sha_len; /* -1: no sha256 property */ +}; + +static void bs_reset_globals(void) +{ + rec = NULL; + chain_state = "none"; + stage1_enforced = false; + lockdown_reason = "no"; +} + +/* A property with exactly @len bytes of @val, no terminator added. */ +static void bs_raw(struct kunit *test, struct device_node *np, + const char *name, const void *val, int len) +{ + struct bs_ctx *ctx = test->priv; + struct property *p = kzalloc(sizeof(*p), GFP_KERNEL); + + KUNIT_ASSERT_NOT_NULL(test, p); + p->name = kstrdup(name, GFP_KERNEL); + /* Like an FDT property: a valid pointer even when empty. */ + p->value = kzalloc(len + 1, GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, p->name); + KUNIT_ASSERT_NOT_NULL(test, p->value); + memcpy(p->value, val, len); + p->length = len; + /* Freed with the node when the changeset is destroyed. */ + KUNIT_ASSERT_EQ(test, of_changeset_add_property(&ctx->cs, np, p), 0); +} + +static void bs_str(struct kunit *test, struct device_node *np, + const char *name, const char *s) +{ + struct bs_ctx *ctx = test->priv; + + if (!s) + return; + KUNIT_ASSERT_EQ(test, of_changeset_add_prop_string(&ctx->cs, np, name, s), 0); +} + +static struct device_node *bs_record(struct kunit *test, + struct device_node *parent, + const char *name, const char *compatible) +{ + struct bs_ctx *ctx = test->priv; + + ctx->np = of_changeset_create_node(&ctx->cs, parent, name); + KUNIT_ASSERT_NOT_NULL(test, ctx->np); + bs_str(test, ctx->np, "compatible", compatible); + return ctx->np; +} + +/* A test record under the root, not where the driver looks at boot. */ +static struct device_node *bs_test_record(struct kunit *test) +{ + if (!of_root) + kunit_skip(test, "no live device tree"); + return bs_record(test, of_root, "kunit-aurora-boot-security", + "aurora,boot-security-v1"); +} + +/* + * Add the measurements in boot order. An attached node goes to the head + * of its parent's child list, so create them last to first. + */ +static void bs_meas(struct kunit *test, const struct bs_meas_spec *m, int n) +{ + struct bs_ctx *ctx = test->priv; + char name[24]; + int i; + + for (i = n - 1; i >= 0; i--) { + struct device_node *c; + + snprintf(name, sizeof(name), "measurement@%d", i); + c = of_changeset_create_node(&ctx->cs, ctx->np, name); + KUNIT_ASSERT_NOT_NULL(test, c); + bs_str(test, c, "stage", m[i].stage); + bs_str(test, c, "policy", m[i].policy); + bs_str(test, c, "component", m[i].component); + bs_str(test, c, "verdict", m[i].verdict); + if (m[i].sha_len >= 0) + bs_raw(test, c, "sha256", m[i].sha, m[i].sha_len); + } +} + +static void bs_apply(struct kunit *test) +{ + struct bs_ctx *ctx = test->priv; + + KUNIT_ASSERT_EQ(test, of_changeset_apply(&ctx->cs), 0); + ctx->applied = true; + rec = ctx->np; +} + +/* What aurora_boot_security_init() derives from the record. */ +static void bs_decide(void) +{ + stage1_enforced = stage1_enforced_verified(); + chain_state = effective_state(); +} + +/* A page for a show function, with a guard page behind it. */ +static char *bs_page(struct kunit *test) +{ + char *buf = kunit_kzalloc(test, 2 * PAGE_SIZE, GFP_KERNEL); + + KUNIT_ASSERT_NOT_NULL(test, buf); + KUNIT_ASSERT_TRUE(test, PAGE_ALIGNED(buf)); + memset(buf + PAGE_SIZE, BS_GUARD, PAGE_SIZE); + return buf; +} + +static void bs_expect_guard(struct kunit *test, const char *buf, size_t from, + size_t to) +{ + size_t i; + + for (i = from; i < to; i++) + if (buf[i] != (char)BS_GUARD) + break; + KUNIT_EXPECT_EQ_MSG(test, i, to, "wrote past the buffer at %zu", i); +} + +static int bs_init(struct kunit *test) +{ + struct bs_ctx *ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL); + + if (!ctx) + return -ENOMEM; + of_changeset_init(&ctx->cs); + test->priv = ctx; + bs_reset_globals(); + return 0; +} + +static void bs_exit(struct kunit *test) +{ + struct bs_ctx *ctx = test->priv; + + bs_reset_globals(); + if (ctx->applied) + of_changeset_revert(&ctx->cs); + of_changeset_destroy(&ctx->cs); +} + +static const u8 bs_sha[33] = { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, + 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, + 0x20, +}; + +#define BS_SHA_HEX "000102030405060708090a0b0c0d0e0f" \ + "101112131415161718191a1b1c1d1e1f" + +/* --- Which measurement makes the chain enforced ------------------------ */ + +static void bs_stage1_no_measurements(struct kunit *test) +{ + struct device_node *np = bs_test_record(test); + + bs_str(test, np, "state", "verified"); + bs_apply(test); + bs_decide(); + KUNIT_EXPECT_FALSE(test, stage1_enforced); + KUNIT_EXPECT_STREQ(test, chain_state, "partial"); +} + +/* One measurement without a hash. */ +#define BS_M(stage, policy, verdict) { stage, policy, "next", verdict, NULL, -1 } + +struct bs_one_case { + const char *desc; + struct bs_meas_spec m; + bool enforced; +}; + +static const struct bs_one_case bs_one_cases[] = { + { "stage1 enforce verified", BS_M("stage1", "enforce", "verified"), true }, + { "stage1 audit verified", BS_M("stage1", "audit", "verified"), false }, + { "stage1 off verified", BS_M("stage1", "off", "verified"), false }, + { "stage1 enforce failed", BS_M("stage1", "enforce", "failed"), false }, + { "stage1 enforce unverified", BS_M("stage1", "enforce", "unverified"), false }, + { "stage1 enforce no verdict", BS_M("stage1", "enforce", BS_ABSENT), false }, + { "stage1 enforce empty verdict", BS_M("stage1", "enforce", ""), false }, + { "stage1 no policy verified", BS_M("stage1", BS_ABSENT, "verified"), false }, + { "stage2 enforce verified", BS_M("stage2", "enforce", "verified"), false }, + { "uboot enforce verified", BS_M("uboot", "enforce", "verified"), false }, + { "no stage enforce verified", BS_M(BS_ABSENT, "enforce", "verified"), false }, + { "empty stage enforce verified", BS_M("", "enforce", "verified"), false }, + { "Stage1 Enforce Verified", BS_M("Stage1", "Enforce", "Verified"), false }, + { "stage1x prefix", BS_M("stage1x", "enforce", "verified"), false }, + { "stage1 enforced prefix", BS_M("stage1", "enforced", "verified"), false }, + { "stage1 enforce verified-ish", BS_M("stage1", "enforce", "verified "), false }, +}; + +KUNIT_ARRAY_PARAM_DESC(bs_one, bs_one_cases, desc); + +static void bs_stage1_one_measurement(struct kunit *test) +{ + const struct bs_one_case *p = test->param_value; + + bs_test_record(test); + bs_meas(test, &p->m, 1); + bs_apply(test); + KUNIT_EXPECT_EQ(test, stage1_enforced_verified(), p->enforced); +} + +/* Any one enforcing, verified Stage 1 entry counts, wherever it is. */ +static void bs_stage1_among_others(struct kunit *test) +{ + static const struct bs_meas_spec m[] = { + { "stage1", "audit", "stage2", "verified", NULL, -1 }, + { "stage2", "audit", "u-boot", "failed", NULL, -1 }, + { "uboot", "off", "kernel", "measured", NULL, -1 }, + { "stage1", "enforce", "stage2", "verified", NULL, -1 }, + }; + + bs_test_record(test); + bs_meas(test, m, ARRAY_SIZE(m)); + bs_apply(test); + KUNIT_EXPECT_TRUE(test, stage1_enforced_verified()); +} + +static void bs_stage1_none_among_many(struct kunit *test) +{ + static const struct bs_meas_spec m[] = { + { "stage1", "audit", "stage2", "verified", NULL, -1 }, + { "stage1", "enforce", "stage2", "failed", NULL, -1 }, + { "stage2", "enforce", "u-boot", "verified", NULL, -1 }, + { "uboot", "enforce", "kernel", "verified", NULL, -1 }, + }; + + bs_test_record(test); + bs_meas(test, m, ARRAY_SIZE(m)); + bs_apply(test); + KUNIT_EXPECT_FALSE(test, stage1_enforced_verified()); +} + +/* A string property without its terminator is not that string. */ +static void bs_stage1_unterminated(struct kunit *test) +{ + static const char * const names[] = { "stage", "policy", "verdict" }; + static const char * const vals[] = { "stage1", "enforce", "verified" }; + struct bs_ctx *ctx = test->priv; + int bad, i; + + for (bad = -1; bad < (int)ARRAY_SIZE(names); bad++) { + struct device_node *c; + + KUNIT_ASSERT_EQ(test, bs_init(test), 0); + ctx = test->priv; + bs_test_record(test); + c = of_changeset_create_node(&ctx->cs, ctx->np, "measurement@0"); + KUNIT_ASSERT_NOT_NULL(test, c); + for (i = 0; i < ARRAY_SIZE(names); i++) + bs_raw(test, c, names[i], vals[i], + strlen(vals[i]) + (i != bad)); + bs_apply(test); + KUNIT_EXPECT_EQ_MSG(test, stage1_enforced_verified(), bad < 0, + "unterminated %s", bad < 0 ? "none" : names[bad]); + bs_exit(test); + } + /* bs_exit() runs again after the case: leave it nothing to undo. */ + ctx = test->priv; + of_changeset_init(&ctx->cs); + ctx->applied = false; +} + +/* Only the first string of a string list is read. */ +static void bs_stage1_string_list(struct kunit *test) +{ + static const char lists[] = "audit\0enforce"; + struct bs_ctx *ctx = test->priv; + struct device_node *c; + + bs_test_record(test); + c = of_changeset_create_node(&ctx->cs, ctx->np, "measurement@0"); + KUNIT_ASSERT_NOT_NULL(test, c); + bs_str(test, c, "stage", "stage1"); + bs_raw(test, c, "policy", lists, sizeof(lists)); + bs_str(test, c, "verdict", "verified"); + bs_apply(test); + KUNIT_EXPECT_FALSE(test, stage1_enforced_verified()); +} + +/* --- The chain state ---------------------------------------------------- */ + +struct bs_state_case { + const char *desc; + const char *state; /* NULL: no state property */ + int len; /* bytes of @state to store; 0: with its NUL */ + bool enforced; + const char *expect; /* NULL: anything but "verified" */ +}; + +static const struct bs_state_case bs_state_cases[] = { + { "absent, not enforced", NULL, 0, false, "unverified" }, + { "absent, enforced", NULL, 0, true, "unverified" }, + { "empty", "", 0, true, "unverified" }, + { "zero length", "", -1, true, "unverified" }, + { "verified, enforced", "verified", 0, true, "verified" }, + { "verified, not enforced", "verified", 0, false, "partial" }, + { "partial, enforced", "partial", 0, true, "partial" }, + { "partial, not enforced", "partial", 0, false, "partial" }, + { "unverified, enforced", "unverified", 0, true, "unverified" }, + { "unterminated verified, enforced", "verified", 8, true, "unverified" }, + { "unterminated verified, not enforced", "verified", 8, false, "unverified" }, + { "truncated verifie, enforced", "verifie", 0, true, NULL }, + { "Verified, not enforced", "Verified", 0, false, NULL }, + { "verified space, not enforced", "verified ", 0, false, NULL }, + { "verified newline, not enforced", "verified\n", 0, false, NULL }, + { "verifiedx, not enforced", "verifiedx", 0, false, NULL }, +}; + +KUNIT_ARRAY_PARAM_DESC(bs_state, bs_state_cases, desc); + +static void bs_state_demotion(struct kunit *test) +{ + const struct bs_state_case *p = test->param_value; + struct device_node *np = bs_test_record(test); + + if (p->state) + bs_raw(test, np, "state", p->state, + p->len > 0 ? p->len : + p->len < 0 ? 0 : strlen(p->state) + 1); + bs_apply(test); + stage1_enforced = p->enforced; + if (p->expect) + KUNIT_EXPECT_STREQ(test, effective_state(), p->expect); + else + KUNIT_EXPECT_STRNEQ(test, effective_state(), "verified"); +} + +/* A record that names the second string of a list is not verified. */ +static void bs_state_string_list(struct kunit *test) +{ + static const char list[] = "partial\0verified"; + struct device_node *np = bs_test_record(test); + + bs_raw(test, np, "state", list, sizeof(list)); + bs_apply(test); + stage1_enforced = true; + KUNIT_EXPECT_STREQ(test, effective_state(), "partial"); +} + +static void bs_state_and_advisory_show(struct kunit *test) +{ + static const struct bs_meas_spec m[] = { + { "stage1", "enforce", "stage2", "verified", NULL, -1 }, + { "stage2", "enforce", "u-boot", "verified", NULL, -1 }, + }; + struct device_node *np = bs_test_record(test); + char *buf = bs_page(test); + + bs_str(test, np, "state", "verified"); + bs_meas(test, m, ARRAY_SIZE(m)); + bs_apply(test); + bs_decide(); + + KUNIT_EXPECT_EQ(test, state_show(NULL, &state_attr, buf), 9); + KUNIT_EXPECT_STREQ(test, buf, "verified\n"); + KUNIT_EXPECT_EQ(test, advisory_show(NULL, &advisory_attr, buf), 2); + KUNIT_EXPECT_STREQ(test, buf, "0\n"); +} + +static void bs_state_and_advisory_show_audit(struct kunit *test) +{ + static const struct bs_meas_spec m[] = { + { "stage1", "audit", "stage2", "verified", NULL, -1 }, + { "stage2", "enforce", "u-boot", "verified", NULL, -1 }, + }; + struct device_node *np = bs_test_record(test); + char *buf = bs_page(test); + + bs_str(test, np, "state", "verified"); + bs_meas(test, m, ARRAY_SIZE(m)); + bs_apply(test); + bs_decide(); + + state_show(NULL, &state_attr, buf); + KUNIT_EXPECT_STREQ(test, buf, "partial\n"); + advisory_show(NULL, &advisory_attr, buf); + KUNIT_EXPECT_STREQ(test, buf, "1\n"); +} + +/* Before a record is read (or without one) the files say so. */ +static void bs_state_show_defaults(struct kunit *test) +{ + char *buf = bs_page(test); + + state_show(NULL, &state_attr, buf); + KUNIT_EXPECT_STREQ(test, buf, "none\n"); + advisory_show(NULL, &advisory_attr, buf); + KUNIT_EXPECT_STREQ(test, buf, "1\n"); + KUNIT_EXPECT_EQ(test, lockdown_show(NULL, &lockdown_attr, buf), 3); + KUNIT_EXPECT_STREQ(test, buf, "no\n"); +} + +/* --- policy ------------------------------------------------------------- */ + +static void bs_policy_show_case(struct kunit *test, + const struct bs_meas_spec *m, int n, + const char *expect) +{ + char *buf = bs_page(test); + + bs_test_record(test); + bs_meas(test, m, n); + bs_apply(test); + KUNIT_EXPECT_EQ(test, policy_show(NULL, &policy_attr, buf), + (ssize_t)strlen(expect)); + KUNIT_EXPECT_STREQ(test, buf, expect); +} + +static void bs_policy_show_empty(struct kunit *test) +{ + bs_policy_show_case(test, NULL, 0, "\n"); +} + +static void bs_policy_show_chain(struct kunit *test) +{ + static const struct bs_meas_spec m[] = { + { "stage1", "enforce", "stage2", "verified", NULL, -1 }, + { "stage2", "audit", "u-boot", "verified", NULL, -1 }, + { "uboot", "off", "kernel", "measured", NULL, -1 }, + }; + + bs_policy_show_case(test, m, ARRAY_SIZE(m), + "stage1 enforce, stage2 audit, uboot off\n"); +} + +/* A stage that measured several components is listed once. */ +static void bs_policy_show_collapses_repeats(struct kunit *test) +{ + static const struct bs_meas_spec m[] = { + { "stage1", "enforce", "stage2", "verified", NULL, -1 }, + { "stage2", "audit", "u-boot", "verified", NULL, -1 }, + { "stage2", "audit", "dtb", "verified", NULL, -1 }, + { "uboot", "off", "kernel", "measured", NULL, -1 }, + { "uboot", "off", "initrd", "measured", NULL, -1 }, + }; + + bs_policy_show_case(test, m, ARRAY_SIZE(m), + "stage1 enforce, stage2 audit, uboot off\n"); +} + +/* Only consecutive repeats collapse: a stage that comes back is listed. */ +static void bs_policy_show_returning_stage(struct kunit *test) +{ + static const struct bs_meas_spec m[] = { + { "stage1", "enforce", "stage2", "verified", NULL, -1 }, + { "stage2", "audit", "u-boot", "verified", NULL, -1 }, + { "stage1", "enforce", "late", "verified", NULL, -1 }, + }; + + bs_policy_show_case(test, m, ARRAY_SIZE(m), + "stage1 enforce, stage2 audit, stage1 enforce\n"); +} + +static void bs_policy_show_no_policy(struct kunit *test) +{ + static const struct bs_meas_spec m[] = { + { "stage1", BS_ABSENT, "stage2", "verified", NULL, -1 }, + { "stage2", "", "u-boot", "verified", NULL, -1 }, + }; + + bs_policy_show_case(test, m, ARRAY_SIZE(m), "stage1 , stage2\x20\n"); +} + +/* More stages than a page holds: truncated inside the page. */ +static void bs_policy_show_bounded(struct kunit *test) +{ + struct bs_meas_spec *m; + char *buf = bs_page(test); + char *stage; + ssize_t ret; + int i, n = PAGE_SIZE / 64 + 8; + + m = kunit_kcalloc(test, n, sizeof(*m), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, m); + for (i = 0; i < n; i++) { + /* 100 distinct characters per stage: no two in a row match. */ + stage = kunit_kzalloc(test, 101, GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, stage); + memset(stage, 'a' + i % 2, 100); + m[i] = (struct bs_meas_spec){ stage, "enforce", "c", "verified", + NULL, -1 }; + } + bs_test_record(test); + bs_meas(test, m, n); + bs_apply(test); + ret = policy_show(NULL, &policy_attr, buf); + KUNIT_EXPECT_EQ(test, ret, (ssize_t)PAGE_SIZE - 1); + KUNIT_EXPECT_EQ(test, strlen(buf), (size_t)ret); + bs_expect_guard(test, buf, PAGE_SIZE, 2 * PAGE_SIZE); +} + +/* --- stage1_flavour, uefi_secure_boot ------------------------------------ */ + +struct bs_str_case { + const char *desc; + bool uefi; /* uefi-secure-boot, else stage1-flavour */ + const char *val; /* NULL: absent */ + int len; /* 0: with its NUL, else bytes stored */ + const char *expect; +}; + +static const struct bs_str_case bs_str_cases[] = { + { "flavour absent", false, NULL, 0, "unknown\n" }, + { "flavour empty", false, "", 0, "unknown\n" }, + { "flavour zero length", false, "", -1, "unknown\n" }, + { "flavour dev", false, "dev", 0, "dev\n" }, + { "flavour secure", false, "secure", 0, "secure\n" }, + { "flavour unterminated", false, "secure", 6, "unknown\n" }, + { "flavour list", false, "dev\0secure", 11, "dev\n" }, + { "uefi absent", true, NULL, 0, "unknown\n" }, + { "uefi empty", true, "", 0, "unknown\n" }, + { "uefi enforcing", true, "enforcing", 0, "enforcing\n" }, + { "uefi audit", true, "audit", 0, "audit\n" }, + { "uefi off", true, "off", 0, "off\n" }, + { "uefi unterminated", true, "enforcing", 9, "unknown\n" }, +}; + +KUNIT_ARRAY_PARAM_DESC(bs_str, bs_str_cases, desc); + +static void bs_string_show(struct kunit *test) +{ + const struct bs_str_case *p = test->param_value; + const char *name = p->uefi ? "uefi-secure-boot" : "stage1-flavour"; + struct device_node *np = bs_test_record(test); + char *buf = bs_page(test); + ssize_t ret; + + if (p->val) + bs_raw(test, np, name, p->val, + p->len > 0 ? p->len : + p->len < 0 ? 0 : strlen(p->val) + 1); + bs_apply(test); + if (p->uefi) + ret = uefi_secure_boot_show(NULL, &uefi_secure_boot_attr, buf); + else + ret = stage1_flavour_show(NULL, &stage1_flavour_attr, buf); + KUNIT_EXPECT_EQ(test, ret, (ssize_t)strlen(p->expect)); + KUNIT_EXPECT_STREQ(test, buf, p->expect); +} + +/* A page-long flavour is cut inside the page. */ +static void bs_string_show_long(struct kunit *test) +{ + struct device_node *np = bs_test_record(test); + char *buf = bs_page(test); + char *val = kunit_kzalloc(test, 2 * PAGE_SIZE, GFP_KERNEL); + + KUNIT_ASSERT_NOT_NULL(test, val); + memset(val, 'x', 2 * PAGE_SIZE - 1); + bs_raw(test, np, "stage1-flavour", val, 2 * PAGE_SIZE); + bs_apply(test); + KUNIT_EXPECT_EQ(test, stage1_flavour_show(NULL, &stage1_flavour_attr, buf), + (ssize_t)PAGE_SIZE - 1); + bs_expect_guard(test, buf, PAGE_SIZE, 2 * PAGE_SIZE); +} + +/* --- lp_coih ------------------------------------------------------------ */ + +struct bs_coih_case { + const char *desc; + int len; /* -1: absent */ + int hex_bytes; /* bytes printed; 0: "unknown" */ +}; + +static const struct bs_coih_case bs_coih_cases[] = { + { "absent", -1, 0 }, + { "empty", 0, 0 }, + { "one byte", 1, 1 }, + { "four bytes", 4, 4 }, + { "sha384 size", 48, 48 }, + { "64 bytes", 64, 64 }, + { "65 bytes", 65, 64 }, + { "200 bytes", 200, 64 }, +}; + +KUNIT_ARRAY_PARAM_DESC(bs_coih, bs_coih_cases, desc); + +static void bs_lp_coih_show(struct kunit *test) +{ + const struct bs_coih_case *p = test->param_value; + struct device_node *np = bs_test_record(test); + char *buf = bs_page(test); + char *expect; + u8 *val; + int i; + + expect = kunit_kzalloc(test, 2 * 64 + 2, GFP_KERNEL); + val = kunit_kzalloc(test, max(p->len, 1), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, expect); + KUNIT_ASSERT_NOT_NULL(test, val); + for (i = 0; i < p->len; i++) + val[i] = 0xc0 + i; + if (p->len >= 0) + bs_raw(test, np, "apple,lp-coih", val, p->len); + bs_apply(test); + + if (p->hex_bytes) { + for (i = 0; i < p->hex_bytes; i++) + snprintf(expect + 2 * i, 3, "%02x", val[i]); + strcat(expect, "\n"); + } else { + strscpy(expect, "unknown\n", 2 * 64 + 2); + } + KUNIT_EXPECT_EQ(test, lp_coih_show(NULL, &lp_coih_attr, buf), + (ssize_t)strlen(expect)); + KUNIT_EXPECT_STREQ(test, buf, expect); +} + +/* --- measurements ------------------------------------------------------- */ + +static void bs_measurements_show_case(struct kunit *test, + const struct bs_meas_spec *m, int n, + const char *expect) +{ + char *buf = bs_page(test); + + bs_test_record(test); + bs_meas(test, m, n); + bs_apply(test); + KUNIT_EXPECT_EQ(test, measurements_show(NULL, &measurements_attr, buf), + (ssize_t)strlen(expect)); + KUNIT_EXPECT_STREQ(test, buf, expect); +} + +static void bs_measurements_none(struct kunit *test) +{ + bs_measurements_show_case(test, NULL, 0, ""); +} + +static void bs_measurements_chain(struct kunit *test) +{ + static const struct bs_meas_spec m[] = { + { "stage1", "enforce", "stage2", "verified", bs_sha, 32 }, + { "stage2", "audit", "u-boot", "verified", bs_sha, 32 }, + { "uboot", "off", "kernel", "measured", bs_sha, 32 }, + }; + static const char expect[] = + "stage1 enforce stage2 verified " BS_SHA_HEX "\n" + "stage2 audit u-boot verified " BS_SHA_HEX "\n" + "uboot off kernel measured " BS_SHA_HEX "\n"; + + bs_measurements_show_case(test, m, ARRAY_SIZE(m), expect); +} + +/* Only a 32-byte sha256 is printed; any other length prints no hash. */ +static void bs_measurements_bad_hash(struct kunit *test) +{ + static const struct bs_meas_spec m[] = { + { "stage1", "enforce", "short", "verified", bs_sha, 31 }, + { "stage1", "enforce", "long", "verified", bs_sha, 33 }, + { "stage1", "enforce", "empty", "verified", bs_sha, 0 }, + { "stage1", "enforce", "one", "verified", bs_sha, 1 }, + { "stage1", "enforce", "absent", "verified", NULL, -1 }, + { "stage1", "enforce", "good", "verified", bs_sha, 32 }, + }; + static const char expect[] = + "stage1 enforce short verified\x20\n" + "stage1 enforce long verified\x20\n" + "stage1 enforce empty verified\x20\n" + "stage1 enforce one verified\x20\n" + "stage1 enforce absent verified\x20\n" + "stage1 enforce good verified " BS_SHA_HEX "\n"; + + bs_measurements_show_case(test, m, ARRAY_SIZE(m), expect); +} + +static void bs_measurements_missing_fields(struct kunit *test) +{ + static const struct bs_meas_spec m[] = { + { BS_ABSENT, BS_ABSENT, BS_ABSENT, BS_ABSENT, NULL, -1 }, + { "stage2", BS_ABSENT, "u-boot", BS_ABSENT, bs_sha, 32 }, + }; + static const char expect[] = + " \x20\n" + "stage2 u-boot " BS_SHA_HEX "\n"; + + bs_measurements_show_case(test, m, ARRAY_SIZE(m), expect); +} + +/* format_measurements() never writes past the size it is given. */ +static void bs_format_measurements_bounded(struct kunit *test) +{ + static const struct bs_meas_spec m[] = { + { "stage1", "enforce", "stage2", "verified", bs_sha, 32 }, + { "stage2", "audit", "u-boot", "verified", bs_sha, 32 }, + }; + static const size_t sizes[] = { 1, 2, 16, 95, 96, 97, 98, 190, 191, 300 }; + static const char full[] = + "stage1 enforce stage2 verified " BS_SHA_HEX "\n" + "stage2 audit u-boot verified " BS_SHA_HEX "\n"; + char *buf = kunit_kzalloc(test, 512, GFP_KERNEL); + ssize_t ret; + int i; + + KUNIT_ASSERT_NOT_NULL(test, buf); + bs_test_record(test); + bs_meas(test, m, ARRAY_SIZE(m)); + bs_apply(test); + + for (i = 0; i < ARRAY_SIZE(sizes); i++) { + size_t sz = sizes[i], want = min(sz - 1, strlen(full)); + + memset(buf, BS_GUARD, 512); + ret = format_measurements(buf, sz); + KUNIT_EXPECT_EQ_MSG(test, ret, (ssize_t)want, "size %zu", sz); + KUNIT_EXPECT_EQ_MSG(test, buf[want], 0, "size %zu", sz); + /* A cut first line leaves no room for the second. */ + if (sz <= 97) + KUNIT_EXPECT_EQ_MSG(test, strncmp(buf, full, want), 0, + "size %zu", sz); + bs_expect_guard(test, buf, sz, 512); + } +} + +/* Far more measurements than a page holds. */ +static void bs_measurements_show_bounded(struct kunit *test) +{ + static const struct bs_meas_spec one = { + "stage1", "enforce", "stage2", "verified", bs_sha, 32 + }; + struct bs_meas_spec *m; + char *buf = bs_page(test); + ssize_t ret; + int i, n = PAGE_SIZE / 64 + 8; + + m = kunit_kcalloc(test, n, sizeof(*m), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, m); + for (i = 0; i < n; i++) + m[i] = one; + bs_test_record(test); + bs_meas(test, m, n); + bs_apply(test); + ret = measurements_show(NULL, &measurements_attr, buf); + KUNIT_EXPECT_EQ(test, ret, (ssize_t)PAGE_SIZE - 1); + KUNIT_EXPECT_EQ(test, strlen(buf), (size_t)ret); + bs_expect_guard(test, buf, PAGE_SIZE, 2 * PAGE_SIZE); +} + +/* The IMA text is built from the same fields; without IMA a no-op. */ +static void bs_measure_record_full_page(struct kunit *test) +{ + static const struct bs_meas_spec one = { + "stage1", "enforce", "stage2", "verified", bs_sha, 32 + }; + struct bs_meas_spec *m; + int i, n = PAGE_SIZE / 64 + 8; + + m = kunit_kcalloc(test, n, sizeof(*m), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, m); + for (i = 0; i < n; i++) + m[i] = one; + bs_test_record(test); + bs_meas(test, m, n); + bs_apply(test); + bs_decide(); + measure_record(); +} + +static struct kunit_case bs_cases[] = { + KUNIT_CASE(bs_stage1_no_measurements), + KUNIT_CASE_PARAM(bs_stage1_one_measurement, bs_one_gen_params), + KUNIT_CASE(bs_stage1_among_others), + KUNIT_CASE(bs_stage1_none_among_many), + KUNIT_CASE(bs_stage1_unterminated), + KUNIT_CASE(bs_stage1_string_list), + KUNIT_CASE_PARAM(bs_state_demotion, bs_state_gen_params), + KUNIT_CASE(bs_state_string_list), + KUNIT_CASE(bs_state_and_advisory_show), + KUNIT_CASE(bs_state_and_advisory_show_audit), + KUNIT_CASE(bs_state_show_defaults), + KUNIT_CASE(bs_policy_show_empty), + KUNIT_CASE(bs_policy_show_chain), + KUNIT_CASE(bs_policy_show_collapses_repeats), + KUNIT_CASE(bs_policy_show_returning_stage), + KUNIT_CASE(bs_policy_show_no_policy), + KUNIT_CASE(bs_policy_show_bounded), + KUNIT_CASE_PARAM(bs_string_show, bs_str_gen_params), + KUNIT_CASE(bs_string_show_long), + KUNIT_CASE_PARAM(bs_lp_coih_show, bs_coih_gen_params), + KUNIT_CASE(bs_measurements_none), + KUNIT_CASE(bs_measurements_chain), + KUNIT_CASE(bs_measurements_bad_hash), + KUNIT_CASE(bs_measurements_missing_fields), + KUNIT_CASE(bs_format_measurements_bounded), + KUNIT_CASE(bs_measurements_show_bounded), + KUNIT_CASE(bs_measure_record_full_page), + {} +}; + +static struct kunit_suite bs_suite = { + .name = "aurora-boot-security", + .init = bs_init, + .exit = bs_exit, + .test_cases = bs_cases, +}; + +/* --- The init path, on the real /chosen/aurora,boot-security ----------- */ + +static int bs_init_init(struct kunit *test) +{ + struct device_node *np; + int ret = bs_init(test); + + if (ret) + return ret; + /* Never lock down the test kernel. */ + if (IS_ENABLED(CONFIG_APPLE_AURORA_BOOT_SECURITY_LOCKDOWN)) + kunit_skip(test, "would lock down the kernel"); + if (!of_chosen) + kunit_skip(test, "no /chosen"); + np = of_find_node_by_path("/chosen/aurora,boot-security"); + of_node_put(np); + if (np || aurora_kobj) + kunit_skip(test, "this kernel booted with a record"); + return 0; +} + +/* What aurora_boot_security_exit() undoes, which is gone when built in. */ +static void bs_init_exit(struct kunit *test) +{ + if (bs_kobj) { + sysfs_remove_groups(bs_kobj, bs_groups); + kobject_put(bs_kobj); + bs_kobj = NULL; + } + kobject_put(aurora_kobj); + aurora_kobj = NULL; + of_node_put(rec); + bs_exit(test); +} + +static struct device_node *bs_chosen_record(struct kunit *test, + const char *compatible) +{ + return bs_record(test, of_chosen, "aurora,boot-security", compatible); +} + +static void bs_init_apply(struct kunit *test) +{ + struct bs_ctx *ctx = test->priv; + + KUNIT_ASSERT_EQ(test, of_changeset_apply(&ctx->cs), 0); + ctx->applied = true; +} + +static void __init bs_init_no_record(struct kunit *test) +{ + KUNIT_EXPECT_EQ(test, aurora_boot_security_init(), -ENODEV); + KUNIT_EXPECT_NULL(test, rec); + KUNIT_EXPECT_NULL(test, aurora_kobj); + KUNIT_EXPECT_STREQ(test, chain_state, "none"); + KUNIT_EXPECT_FALSE(test, stage1_enforced); +} + +/* A record that is not v1 is ignored, its claims and all. */ +static void __init bs_init_wrong_compatible(struct kunit *test) +{ + static const char * const compats[] = { + NULL, "", "aurora,boot-security-v2", "aurora,boot-security", + }; + static const struct bs_meas_spec m[] = { + { "stage1", "enforce", "stage2", "verified", NULL, -1 }, + }; + int i; + + for (i = 0; i < ARRAY_SIZE(compats); i++) { + struct device_node *np; + + np = bs_chosen_record(test, compats[i]); + bs_str(test, np, "state", "verified"); + bs_meas(test, m, ARRAY_SIZE(m)); + bs_init_apply(test); + KUNIT_EXPECT_EQ_MSG(test, aurora_boot_security_init(), -ENODEV, + "compatible %s", compats[i] ?: "absent"); + KUNIT_EXPECT_NULL(test, rec); + KUNIT_EXPECT_NULL(test, aurora_kobj); + KUNIT_EXPECT_STREQ(test, chain_state, "none"); + KUNIT_EXPECT_FALSE(test, stage1_enforced); + /* The node's reference was dropped: the revert frees it. */ + bs_exit(test); + KUNIT_ASSERT_EQ(test, bs_init(test), 0); + } +} + +static void __init bs_init_check_files(struct kunit *test) +{ + static const char * const files[] = { + "state", "advisory", "policy", "stage1_flavour", + "uefi_secure_boot", "lockdown", "lp_coih", "measurements", + }; + struct kernfs_node *kn; + int i; + + KUNIT_ASSERT_NOT_NULL(test, aurora_kobj); + KUNIT_ASSERT_NOT_NULL(test, bs_kobj); + KUNIT_EXPECT_PTR_EQ(test, aurora_kobj->parent, firmware_kobj); + KUNIT_EXPECT_STREQ(test, kobject_name(aurora_kobj), "aurora"); + KUNIT_EXPECT_STREQ(test, kobject_name(bs_kobj), "boot_security"); + for (i = 0; i < ARRAY_SIZE(files); i++) { + kn = kernfs_find_and_get(bs_kobj->sd, files[i]); + KUNIT_EXPECT_NOT_NULL_MSG(test, kn, "no %s", files[i]); + kernfs_put(kn); + } +} + +static void __init bs_init_verified(struct kunit *test) +{ + static const struct bs_meas_spec m[] = { + { "stage1", "enforce", "stage2", "verified", bs_sha, 32 }, + { "stage2", "enforce", "u-boot", "verified", bs_sha, 32 }, + }; + struct bs_ctx *ctx = test->priv; + struct device_node *np = bs_chosen_record(test, "aurora,boot-security-v1"); + char *buf = bs_page(test); + + bs_str(test, np, "state", "verified"); + bs_str(test, np, "stage1-flavour", "secure"); + bs_str(test, np, "uefi-secure-boot", "audit"); + bs_meas(test, m, ARRAY_SIZE(m)); + bs_init_apply(test); + + KUNIT_ASSERT_EQ(test, aurora_boot_security_init(), 0); + KUNIT_EXPECT_PTR_EQ(test, rec, ctx->np); + KUNIT_EXPECT_TRUE(test, stage1_enforced); + KUNIT_EXPECT_STREQ(test, chain_state, "verified"); + KUNIT_EXPECT_STREQ(test, lockdown_reason, "no"); + bs_init_check_files(test); + + state_attr.show(bs_kobj, &state_attr, buf); + KUNIT_EXPECT_STREQ(test, buf, "verified\n"); + advisory_attr.show(bs_kobj, &advisory_attr, buf); + KUNIT_EXPECT_STREQ(test, buf, "0\n"); + policy_attr.show(bs_kobj, &policy_attr, buf); + KUNIT_EXPECT_STREQ(test, buf, "stage1 enforce, stage2 enforce\n"); + stage1_flavour_attr.show(bs_kobj, &stage1_flavour_attr, buf); + KUNIT_EXPECT_STREQ(test, buf, "secure\n"); + uefi_secure_boot_attr.show(bs_kobj, &uefi_secure_boot_attr, buf); + KUNIT_EXPECT_STREQ(test, buf, "audit\n"); +} + +/* The record claims verified, but Stage 1 only audited. */ +static void __init bs_init_claims_verified(struct kunit *test) +{ + static const struct bs_meas_spec m[] = { + { "stage1", "audit", "stage2", "verified", NULL, -1 }, + { "stage2", "enforce", "u-boot", "verified", NULL, -1 }, + }; + struct device_node *np = bs_chosen_record(test, "aurora,boot-security-v1"); + char *buf = bs_page(test); + + bs_str(test, np, "state", "verified"); + bs_meas(test, m, ARRAY_SIZE(m)); + bs_init_apply(test); + + KUNIT_ASSERT_EQ(test, aurora_boot_security_init(), 0); + KUNIT_EXPECT_FALSE(test, stage1_enforced); + KUNIT_EXPECT_STREQ(test, chain_state, "partial"); + bs_init_check_files(test); + state_attr.show(bs_kobj, &state_attr, buf); + KUNIT_EXPECT_STREQ(test, buf, "partial\n"); + advisory_attr.show(bs_kobj, &advisory_attr, buf); + KUNIT_EXPECT_STREQ(test, buf, "1\n"); + stage1_flavour_attr.show(bs_kobj, &stage1_flavour_attr, buf); + KUNIT_EXPECT_STREQ(test, buf, "unknown\n"); +} + +/* v1 later in the compatible list; an empty record is "unverified". */ +static void __init bs_init_compatible_list(struct kunit *test) +{ + static const char compat[] = "vendor,other\0aurora,boot-security-v1"; + struct device_node *np = bs_chosen_record(test, NULL); + char *buf = bs_page(test); + + bs_raw(test, np, "compatible", compat, sizeof(compat)); + bs_init_apply(test); + + KUNIT_ASSERT_EQ(test, aurora_boot_security_init(), 0); + KUNIT_EXPECT_NOT_NULL(test, rec); + KUNIT_EXPECT_STREQ(test, chain_state, "unverified"); + bs_init_check_files(test); + policy_attr.show(bs_kobj, &policy_attr, buf); + KUNIT_EXPECT_STREQ(test, buf, "\n"); + /* Nothing to list: nothing written, not even a newline. */ + KUNIT_EXPECT_EQ(test, measurements_attr.show(bs_kobj, &measurements_attr, buf), 0); + lp_coih_attr.show(bs_kobj, &lp_coih_attr, buf); + KUNIT_EXPECT_STREQ(test, buf, "unknown\n"); +} + +static struct kunit_case __refdata bs_init_cases[] = { + KUNIT_CASE(bs_init_no_record), + KUNIT_CASE(bs_init_wrong_compatible), + KUNIT_CASE(bs_init_verified), + KUNIT_CASE(bs_init_claims_verified), + KUNIT_CASE(bs_init_compatible_list), + {} +}; + +static struct kunit_suite bs_init_suite = { + .name = "aurora-boot-security-init", + .init = bs_init_init, + .exit = bs_init_exit, + .test_cases = bs_init_cases, +}; + +kunit_test_suites(&bs_suite); +kunit_test_init_section_suites(&bs_init_suite); diff --git a/drivers/soc/apple/aurora-boot-security.c b/drivers/soc/apple/aurora-boot-security.c index 6ccd35c40649e..efa3c20b781ad 100644 --- a/drivers/soc/apple/aurora-boot-security.c +++ b/drivers/soc/apple/aurora-boot-security.c @@ -315,3 +315,7 @@ module_init(aurora_boot_security_init); module_exit(aurora_boot_security_exit); MODULE_DESCRIPTION("Report the boot chain record left by aura and U-Boot"); MODULE_LICENSE("Dual MIT/GPL"); + +#if IS_ENABLED(CONFIG_APPLE_AURORA_BOOT_SECURITY_KUNIT_TEST) +#include "aurora-boot-security-test.c" +#endif diff --git a/drivers/soc/apple/aurora-boot-security.kunitconfig b/drivers/soc/apple/aurora-boot-security.kunitconfig new file mode 100644 index 0000000000000..e6cbacaeac374 --- /dev/null +++ b/drivers/soc/apple/aurora-boot-security.kunitconfig @@ -0,0 +1,6 @@ +CONFIG_KUNIT=y +CONFIG_ARCH_APPLE=y +CONFIG_OF=y +CONFIG_OF_OVERLAY=y +CONFIG_APPLE_AURORA_BOOT_SECURITY=y +CONFIG_APPLE_AURORA_BOOT_SECURITY_KUNIT_TEST=y From 0935b9d9185ed93f10432992df81d873cd207956 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Mon, 23 Nov 2026 19:12:01 +1100 Subject: [PATCH 2/4] soc: apple: isolate boot-security KUnit fixtures from live state Check for live state before initializing either fixture and only tear down resources owned by the test. Skip the IMA submission case when IMA is enabled, and let the changeset own the fixture nodes. Signed-off-by: Ryan Murray --- drivers/soc/apple/Kconfig | 5 +- drivers/soc/apple/aurora-boot-security-test.c | 116 ++++++++++++------ 2 files changed, 84 insertions(+), 37 deletions(-) diff --git a/drivers/soc/apple/Kconfig b/drivers/soc/apple/Kconfig index d89e81cc29a1c..c0bca07a85c9e 100644 --- a/drivers/soc/apple/Kconfig +++ b/drivers/soc/apple/Kconfig @@ -445,8 +445,9 @@ config APPLE_AURORA_BOOT_SECURITY_KUNIT_TEST /sys/firmware/aurora/boot_security/ shows for absent, empty, unterminated, short, long and oversized properties. The init cases add and remove a test /chosen/aurora,boot-security and are - skipped when APPLE_AURORA_BOOT_SECURITY_LOCKDOWN is set, so the - test kernel is never locked down. No hardware is touched. + skipped when APPLE_AURORA_BOOT_SECURITY_LOCKDOWN is set. Both + suites skip while the boot-security state is in use. The IMA + formatting case skips when IMA is enabled. No hardware is touched. If unsure, say N. diff --git a/drivers/soc/apple/aurora-boot-security-test.c b/drivers/soc/apple/aurora-boot-security-test.c index 9131de01943b4..fcba1e9d71927 100644 --- a/drivers/soc/apple/aurora-boot-security-test.c +++ b/drivers/soc/apple/aurora-boot-security-test.c @@ -3,7 +3,7 @@ * KUnit tests for the Aurora boot security record reader. * * Each case builds a boot record in the live device tree with an - * of_changeset (a test node under the root, or the real + * of_changeset (a test node under /chosen, or the real * /chosen/aurora,boot-security for the init cases) and runs the driver's * own decision and sysfs show functions on it: which Stage 1 measurement * counts as enforcing, how the chain state is demoted, and what every @@ -25,6 +25,9 @@ struct bs_ctx { struct of_changeset cs; struct device_node *np; bool applied; + const char *old_chain_state; + const char *old_lockdown_reason; + bool old_stage1_enforced; }; struct bs_meas_spec { @@ -47,17 +50,28 @@ static void bs_raw(struct kunit *test, struct device_node *np, { struct bs_ctx *ctx = test->priv; struct property *p = kzalloc(sizeof(*p), GFP_KERNEL); + int ret; KUNIT_ASSERT_NOT_NULL(test, p); p->name = kstrdup(name, GFP_KERNEL); /* Like an FDT property: a valid pointer even when empty. */ p->value = kzalloc(len + 1, GFP_KERNEL); - KUNIT_ASSERT_NOT_NULL(test, p->name); - KUNIT_ASSERT_NOT_NULL(test, p->value); + if (!p->name || !p->value) { + kfree(p->value); + kfree(p->name); + kfree(p); + KUNIT_FAIL_AND_ABORT(test, "could not allocate property %s", name); + } memcpy(p->value, val, len); p->length = len; /* Freed with the node when the changeset is destroyed. */ - KUNIT_ASSERT_EQ(test, of_changeset_add_property(&ctx->cs, np, p), 0); + ret = of_changeset_add_property(&ctx->cs, np, p); + if (ret) { + kfree(p->value); + kfree(p->name); + kfree(p); + KUNIT_FAIL_AND_ABORT(test, "could not add property %s: %d", name, ret); + } } static void bs_str(struct kunit *test, struct device_node *np, @@ -70,24 +84,37 @@ static void bs_str(struct kunit *test, struct device_node *np, KUNIT_ASSERT_EQ(test, of_changeset_add_prop_string(&ctx->cs, np, name, s), 0); } +static struct device_node *bs_create_node(struct kunit *test, + struct device_node *parent, + const char *name) +{ + struct bs_ctx *ctx = test->priv; + struct device_node *np; + + np = of_changeset_create_node(&ctx->cs, parent, name); + KUNIT_ASSERT_NOT_NULL(test, np); + /* The changeset holds a reference until bs_exit(). */ + of_node_put(np); + return np; +} + static struct device_node *bs_record(struct kunit *test, struct device_node *parent, const char *name, const char *compatible) { struct bs_ctx *ctx = test->priv; - ctx->np = of_changeset_create_node(&ctx->cs, parent, name); - KUNIT_ASSERT_NOT_NULL(test, ctx->np); + ctx->np = bs_create_node(test, parent, name); bs_str(test, ctx->np, "compatible", compatible); return ctx->np; } -/* A test record under the root, not where the driver looks at boot. */ +/* Avoid adding platform devices while exercising the record reader. */ static struct device_node *bs_test_record(struct kunit *test) { - if (!of_root) - kunit_skip(test, "no live device tree"); - return bs_record(test, of_root, "kunit-aurora-boot-security", + if (!of_chosen) + kunit_skip(test, "no /chosen"); + return bs_record(test, of_chosen, "kunit-aurora-boot-security", "aurora,boot-security-v1"); } @@ -105,8 +132,7 @@ static void bs_meas(struct kunit *test, const struct bs_meas_spec *m, int n) struct device_node *c; snprintf(name, sizeof(name), "measurement@%d", i); - c = of_changeset_create_node(&ctx->cs, ctx->np, name); - KUNIT_ASSERT_NOT_NULL(test, c); + c = bs_create_node(test, ctx->np, name); bs_str(test, c, "stage", m[i].stage); bs_str(test, c, "policy", m[i].policy); bs_str(test, c, "component", m[i].component); @@ -156,12 +182,18 @@ static void bs_expect_guard(struct kunit *test, const char *buf, size_t from, static int bs_init(struct kunit *test) { - struct bs_ctx *ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL); + struct bs_ctx *ctx; + if (rec || aurora_kobj || bs_kobj) + kunit_skip(test, "boot-security state is in use"); + ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL); if (!ctx) return -ENOMEM; of_changeset_init(&ctx->cs); test->priv = ctx; + ctx->old_chain_state = chain_state; + ctx->old_lockdown_reason = lockdown_reason; + ctx->old_stage1_enforced = stage1_enforced; bs_reset_globals(); return 0; } @@ -170,7 +202,13 @@ static void bs_exit(struct kunit *test) { struct bs_ctx *ctx = test->priv; - bs_reset_globals(); + if (!ctx) + return; + test->priv = NULL; + rec = NULL; + chain_state = ctx->old_chain_state; + lockdown_reason = ctx->old_lockdown_reason; + stage1_enforced = ctx->old_stage1_enforced; if (ctx->applied) of_changeset_revert(&ctx->cs); of_changeset_destroy(&ctx->cs); @@ -276,17 +314,17 @@ static void bs_stage1_unterminated(struct kunit *test) { static const char * const names[] = { "stage", "policy", "verdict" }; static const char * const vals[] = { "stage1", "enforce", "verified" }; - struct bs_ctx *ctx = test->priv; + struct bs_ctx *ctx; int bad, i; for (bad = -1; bad < (int)ARRAY_SIZE(names); bad++) { struct device_node *c; - KUNIT_ASSERT_EQ(test, bs_init(test), 0); + if (bad >= 0) + KUNIT_ASSERT_EQ(test, bs_init(test), 0); ctx = test->priv; bs_test_record(test); - c = of_changeset_create_node(&ctx->cs, ctx->np, "measurement@0"); - KUNIT_ASSERT_NOT_NULL(test, c); + c = bs_create_node(test, ctx->np, "measurement@0"); for (i = 0; i < ARRAY_SIZE(names); i++) bs_raw(test, c, names[i], vals[i], strlen(vals[i]) + (i != bad)); @@ -295,10 +333,6 @@ static void bs_stage1_unterminated(struct kunit *test) "unterminated %s", bad < 0 ? "none" : names[bad]); bs_exit(test); } - /* bs_exit() runs again after the case: leave it nothing to undo. */ - ctx = test->priv; - of_changeset_init(&ctx->cs); - ctx->applied = false; } /* Only the first string of a string list is read. */ @@ -309,8 +343,7 @@ static void bs_stage1_string_list(struct kunit *test) struct device_node *c; bs_test_record(test); - c = of_changeset_create_node(&ctx->cs, ctx->np, "measurement@0"); - KUNIT_ASSERT_NOT_NULL(test, c); + c = bs_create_node(test, ctx->np, "measurement@0"); bs_str(test, c, "stage", "stage1"); bs_raw(test, c, "policy", lists, sizeof(lists)); bs_str(test, c, "verdict", "verified"); @@ -511,6 +544,8 @@ static void bs_policy_show_bounded(struct kunit *test) ssize_t ret; int i, n = PAGE_SIZE / 64 + 8; + if (IS_ENABLED(CONFIG_IMA)) + kunit_skip(test, "IMA is enabled"); m = kunit_kcalloc(test, n, sizeof(*m), GFP_KERNEL); KUNIT_ASSERT_NOT_NULL(test, m); for (i = 0; i < n; i++) { @@ -786,6 +821,8 @@ static void bs_measure_record_full_page(struct kunit *test) struct bs_meas_spec *m; int i, n = PAGE_SIZE / 64 + 8; + if (IS_ENABLED(CONFIG_IMA)) + kunit_skip(test, "IMA is enabled"); m = kunit_kcalloc(test, n, sizeof(*m), GFP_KERNEL); KUNIT_ASSERT_NOT_NULL(test, m); for (i = 0; i < n; i++) @@ -840,10 +877,6 @@ static struct kunit_suite bs_suite = { static int bs_init_init(struct kunit *test) { struct device_node *np; - int ret = bs_init(test); - - if (ret) - return ret; /* Never lock down the test kernel. */ if (IS_ENABLED(CONFIG_APPLE_AURORA_BOOT_SECURITY_LOCKDOWN)) kunit_skip(test, "would lock down the kernel"); @@ -851,14 +884,16 @@ static int bs_init_init(struct kunit *test) kunit_skip(test, "no /chosen"); np = of_find_node_by_path("/chosen/aurora,boot-security"); of_node_put(np); - if (np || aurora_kobj) + if (np) kunit_skip(test, "this kernel booted with a record"); - return 0; + return bs_init(test); } /* What aurora_boot_security_exit() undoes, which is gone when built in. */ static void bs_init_exit(struct kunit *test) { + if (!test->priv) + return; if (bs_kobj) { sysfs_remove_groups(bs_kobj, bs_groups); kobject_put(bs_kobj); @@ -884,9 +919,20 @@ static void bs_init_apply(struct kunit *test) ctx->applied = true; } +static int __init bs_run_driver_init(void) +{ + int ret = aurora_boot_security_init(); + + if (ret) { + aurora_kobj = NULL; + bs_kobj = NULL; + } + return ret; +} + static void __init bs_init_no_record(struct kunit *test) { - KUNIT_EXPECT_EQ(test, aurora_boot_security_init(), -ENODEV); + KUNIT_EXPECT_EQ(test, bs_run_driver_init(), -ENODEV); KUNIT_EXPECT_NULL(test, rec); KUNIT_EXPECT_NULL(test, aurora_kobj); KUNIT_EXPECT_STREQ(test, chain_state, "none"); @@ -911,7 +957,7 @@ static void __init bs_init_wrong_compatible(struct kunit *test) bs_str(test, np, "state", "verified"); bs_meas(test, m, ARRAY_SIZE(m)); bs_init_apply(test); - KUNIT_EXPECT_EQ_MSG(test, aurora_boot_security_init(), -ENODEV, + KUNIT_EXPECT_EQ_MSG(test, bs_run_driver_init(), -ENODEV, "compatible %s", compats[i] ?: "absent"); KUNIT_EXPECT_NULL(test, rec); KUNIT_EXPECT_NULL(test, aurora_kobj); @@ -960,7 +1006,7 @@ static void __init bs_init_verified(struct kunit *test) bs_meas(test, m, ARRAY_SIZE(m)); bs_init_apply(test); - KUNIT_ASSERT_EQ(test, aurora_boot_security_init(), 0); + KUNIT_ASSERT_EQ(test, bs_run_driver_init(), 0); KUNIT_EXPECT_PTR_EQ(test, rec, ctx->np); KUNIT_EXPECT_TRUE(test, stage1_enforced); KUNIT_EXPECT_STREQ(test, chain_state, "verified"); @@ -993,7 +1039,7 @@ static void __init bs_init_claims_verified(struct kunit *test) bs_meas(test, m, ARRAY_SIZE(m)); bs_init_apply(test); - KUNIT_ASSERT_EQ(test, aurora_boot_security_init(), 0); + KUNIT_ASSERT_EQ(test, bs_run_driver_init(), 0); KUNIT_EXPECT_FALSE(test, stage1_enforced); KUNIT_EXPECT_STREQ(test, chain_state, "partial"); bs_init_check_files(test); @@ -1015,7 +1061,7 @@ static void __init bs_init_compatible_list(struct kunit *test) bs_raw(test, np, "compatible", compat, sizeof(compat)); bs_init_apply(test); - KUNIT_ASSERT_EQ(test, aurora_boot_security_init(), 0); + KUNIT_ASSERT_EQ(test, bs_run_driver_init(), 0); KUNIT_EXPECT_NOT_NULL(test, rec); KUNIT_EXPECT_STREQ(test, chain_state, "unverified"); bs_init_check_files(test); From c283241f8ad80cced13c086e79f3b8b7eff38a04 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Mon, 23 Nov 2026 19:20:06 +1100 Subject: [PATCH 3/4] soc: apple: keep policy formatting coverage with IMA enabled The policy formatting case does not call IMA. Only skip the case that calls the measurement submission path. Signed-off-by: Ryan Murray --- drivers/soc/apple/aurora-boot-security-test.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/drivers/soc/apple/aurora-boot-security-test.c b/drivers/soc/apple/aurora-boot-security-test.c index fcba1e9d71927..b7d941eef7cf0 100644 --- a/drivers/soc/apple/aurora-boot-security-test.c +++ b/drivers/soc/apple/aurora-boot-security-test.c @@ -544,8 +544,6 @@ static void bs_policy_show_bounded(struct kunit *test) ssize_t ret; int i, n = PAGE_SIZE / 64 + 8; - if (IS_ENABLED(CONFIG_IMA)) - kunit_skip(test, "IMA is enabled"); m = kunit_kcalloc(test, n, sizeof(*m), GFP_KERNEL); KUNIT_ASSERT_NOT_NULL(test, m); for (i = 0; i < n; i++) { From 166c4b6e9682472b28573f3e42b258296cb7b324 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Mon, 23 Nov 2026 20:08:10 +1100 Subject: [PATCH 4/4] soc: apple: support modular boot security KUnit tests Keep init-section tests built in and let modular KUnit exercise the record reader. Allocate page-aligned sysfs fixtures and cover test-key verdicts and enforcing dev-flavour records. Signed-off-by: Ryan Murray --- drivers/soc/apple/Kconfig | 12 +- drivers/soc/apple/Makefile | 3 + drivers/soc/apple/aurora-boot-security-test.c | 143 ++++++++++++------ drivers/soc/apple/aurora-boot-security-test.h | 35 +++++ drivers/soc/apple/aurora-boot-security.c | 29 ++++ 5 files changed, 167 insertions(+), 55 deletions(-) create mode 100644 drivers/soc/apple/aurora-boot-security-test.h diff --git a/drivers/soc/apple/Kconfig b/drivers/soc/apple/Kconfig index c0bca07a85c9e..cc7f2dd050a4b 100644 --- a/drivers/soc/apple/Kconfig +++ b/drivers/soc/apple/Kconfig @@ -434,8 +434,8 @@ config APPLE_AURORA_BOOT_SECURITY_LOCKDOWN applies. config APPLE_AURORA_BOOT_SECURITY_KUNIT_TEST - bool "KUnit tests for the Aurora boot security record" if !KUNIT_ALL_TESTS - depends on APPLE_AURORA_BOOT_SECURITY=y && KUNIT=y && OF_DYNAMIC + tristate "KUnit tests for the Aurora boot security record" if !KUNIT_ALL_TESTS + depends on APPLE_AURORA_BOOT_SECURITY && KUNIT && OF_DYNAMIC default KUNIT_ALL_TESTS help Build boot records in the live device tree and check what the @@ -443,11 +443,11 @@ config APPLE_AURORA_BOOT_SECURITY_KUNIT_TEST chain enforced, when a claimed "verified" state is demoted, which records are ignored, and what every file in /sys/firmware/aurora/boot_security/ shows for absent, empty, - unterminated, short, long and oversized properties. The init - cases add and remove a test /chosen/aurora,boot-security and are - skipped when APPLE_AURORA_BOOT_SECURITY_LOCKDOWN is set. Both + unterminated, short, long and oversized properties. The built-in + init suite adds and removes a test /chosen/aurora,boot-security + and skips when APPLE_AURORA_BOOT_SECURITY_LOCKDOWN is set. Both suites skip while the boot-security state is in use. The IMA - formatting case skips when IMA is enabled. No hardware is touched. + submission case skips when IMA is enabled. No hardware is touched. If unsure, say N. diff --git a/drivers/soc/apple/Makefile b/drivers/soc/apple/Makefile index 233df860395b8..fb18257b174f2 100644 --- a/drivers/soc/apple/Makefile +++ b/drivers/soc/apple/Makefile @@ -71,3 +71,6 @@ obj-$(CONFIG_APPLE_REGVIEW) += apple-regview.o apple-regview-y := regview.o obj-$(CONFIG_APPLE_AURORA_BOOT_SECURITY) += aurora-boot-security.o +ifeq ($(CONFIG_APPLE_AURORA_BOOT_SECURITY_KUNIT_TEST),m) +obj-m += aurora-boot-security-test.o +endif diff --git a/drivers/soc/apple/aurora-boot-security-test.c b/drivers/soc/apple/aurora-boot-security-test.c index b7d941eef7cf0..9c580c6eec6cd 100644 --- a/drivers/soc/apple/aurora-boot-security-test.c +++ b/drivers/soc/apple/aurora-boot-security-test.c @@ -10,11 +10,17 @@ * sysfs file prints for absent, empty, unterminated, short, long and * oversized properties. No hardware is touched. * - * Included from aurora-boot-security.c. + * The init suite is built into the driver while .init.text is live. */ #include #include +#include +#include +#include +#include + +#include "aurora-boot-security-test.h" #define BS_GUARD 0xa5 @@ -38,10 +44,10 @@ struct bs_meas_spec { static void bs_reset_globals(void) { - rec = NULL; - chain_state = "none"; - stage1_enforced = false; - lockdown_reason = "no"; + *aurora_bs_test.rec = NULL; + *aurora_bs_test.chain_state = "none"; + *aurora_bs_test.stage1_enforced = false; + *aurora_bs_test.lockdown_reason = "no"; } /* A property with exactly @len bytes of @val, no terminator added. */ @@ -72,6 +78,10 @@ static void bs_raw(struct kunit *test, struct device_node *np, kfree(p); KUNIT_FAIL_AND_ABORT(test, "could not add property %s: %d", name, ret); } + + /* The detached fixture node owns this even if apply is never called. */ + p->next = np->deadprops; + np->deadprops = p; } static void bs_str(struct kunit *test, struct device_node *np, @@ -148,22 +158,30 @@ static void bs_apply(struct kunit *test) KUNIT_ASSERT_EQ(test, of_changeset_apply(&ctx->cs), 0); ctx->applied = true; - rec = ctx->np; + *aurora_bs_test.rec = ctx->np; } /* What aurora_boot_security_init() derives from the record. */ static void bs_decide(void) { - stage1_enforced = stage1_enforced_verified(); - chain_state = effective_state(); + *aurora_bs_test.stage1_enforced = aurora_bs_test.stage1_enforced_verified(); + *aurora_bs_test.chain_state = aurora_bs_test.effective_state(); +} + +static ssize_t bs_show(struct kobj_attribute *attr, char *buf) +{ + return attr->show(NULL, attr, buf); } -/* A page for a show function, with a guard page behind it. */ +KUNIT_DEFINE_ACTION_WRAPPER(bs_vfree, vfree, const void *); + +/* A page for a show function, with a second page for the canary. */ static char *bs_page(struct kunit *test) { - char *buf = kunit_kzalloc(test, 2 * PAGE_SIZE, GFP_KERNEL); + char *buf = vzalloc(2 * PAGE_SIZE); KUNIT_ASSERT_NOT_NULL(test, buf); + KUNIT_ASSERT_EQ(test, kunit_add_action_or_reset(test, bs_vfree, buf), 0); KUNIT_ASSERT_TRUE(test, PAGE_ALIGNED(buf)); memset(buf + PAGE_SIZE, BS_GUARD, PAGE_SIZE); return buf; @@ -184,16 +202,16 @@ static int bs_init(struct kunit *test) { struct bs_ctx *ctx; - if (rec || aurora_kobj || bs_kobj) + if (*aurora_bs_test.rec || *aurora_bs_test.aurora_kobj || *aurora_bs_test.bs_kobj) kunit_skip(test, "boot-security state is in use"); ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL); if (!ctx) return -ENOMEM; of_changeset_init(&ctx->cs); test->priv = ctx; - ctx->old_chain_state = chain_state; - ctx->old_lockdown_reason = lockdown_reason; - ctx->old_stage1_enforced = stage1_enforced; + ctx->old_chain_state = *aurora_bs_test.chain_state; + ctx->old_lockdown_reason = *aurora_bs_test.lockdown_reason; + ctx->old_stage1_enforced = *aurora_bs_test.stage1_enforced; bs_reset_globals(); return 0; } @@ -205,10 +223,10 @@ static void bs_exit(struct kunit *test) if (!ctx) return; test->priv = NULL; - rec = NULL; - chain_state = ctx->old_chain_state; - lockdown_reason = ctx->old_lockdown_reason; - stage1_enforced = ctx->old_stage1_enforced; + *aurora_bs_test.rec = NULL; + *aurora_bs_test.chain_state = ctx->old_chain_state; + *aurora_bs_test.lockdown_reason = ctx->old_lockdown_reason; + *aurora_bs_test.stage1_enforced = ctx->old_stage1_enforced; if (ctx->applied) of_changeset_revert(&ctx->cs); of_changeset_destroy(&ctx->cs); @@ -234,8 +252,8 @@ static void bs_stage1_no_measurements(struct kunit *test) bs_str(test, np, "state", "verified"); bs_apply(test); bs_decide(); - KUNIT_EXPECT_FALSE(test, stage1_enforced); - KUNIT_EXPECT_STREQ(test, chain_state, "partial"); + KUNIT_EXPECT_FALSE(test, *aurora_bs_test.stage1_enforced); + KUNIT_EXPECT_STREQ(test, *aurora_bs_test.chain_state, "partial"); } /* One measurement without a hash. */ @@ -251,6 +269,7 @@ static const struct bs_one_case bs_one_cases[] = { { "stage1 enforce verified", BS_M("stage1", "enforce", "verified"), true }, { "stage1 audit verified", BS_M("stage1", "audit", "verified"), false }, { "stage1 off verified", BS_M("stage1", "off", "verified"), false }, + { "stage1 enforce test key", BS_M("stage1", "enforce", "test-key"), false }, { "stage1 enforce failed", BS_M("stage1", "enforce", "failed"), false }, { "stage1 enforce unverified", BS_M("stage1", "enforce", "unverified"), false }, { "stage1 enforce no verdict", BS_M("stage1", "enforce", BS_ABSENT), false }, @@ -275,7 +294,26 @@ static void bs_stage1_one_measurement(struct kunit *test) bs_test_record(test); bs_meas(test, &p->m, 1); bs_apply(test); - KUNIT_EXPECT_EQ(test, stage1_enforced_verified(), p->enforced); + KUNIT_EXPECT_EQ(test, aurora_bs_test.stage1_enforced_verified(), p->enforced); +} + +/* Report the build flavour separately from the recorded verdict. */ +static void bs_stage1_dev_enforce(struct kunit *test) +{ + const struct bs_meas_spec m = BS_M("stage1", "enforce", "verified"); + struct device_node *np = bs_test_record(test); + char *buf = bs_page(test); + + bs_str(test, np, "state", "verified"); + bs_str(test, np, "stage1-flavour", "dev"); + bs_meas(test, &m, 1); + bs_apply(test); + bs_decide(); + + KUNIT_EXPECT_TRUE(test, *aurora_bs_test.stage1_enforced); + KUNIT_EXPECT_STREQ(test, *aurora_bs_test.chain_state, "verified"); + bs_show(aurora_bs_test.stage1_flavour, buf); + KUNIT_EXPECT_STREQ(test, buf, "dev\n"); } /* Any one enforcing, verified Stage 1 entry counts, wherever it is. */ @@ -291,7 +329,7 @@ static void bs_stage1_among_others(struct kunit *test) bs_test_record(test); bs_meas(test, m, ARRAY_SIZE(m)); bs_apply(test); - KUNIT_EXPECT_TRUE(test, stage1_enforced_verified()); + KUNIT_EXPECT_TRUE(test, aurora_bs_test.stage1_enforced_verified()); } static void bs_stage1_none_among_many(struct kunit *test) @@ -306,7 +344,7 @@ static void bs_stage1_none_among_many(struct kunit *test) bs_test_record(test); bs_meas(test, m, ARRAY_SIZE(m)); bs_apply(test); - KUNIT_EXPECT_FALSE(test, stage1_enforced_verified()); + KUNIT_EXPECT_FALSE(test, aurora_bs_test.stage1_enforced_verified()); } /* A string property without its terminator is not that string. */ @@ -329,7 +367,7 @@ static void bs_stage1_unterminated(struct kunit *test) bs_raw(test, c, names[i], vals[i], strlen(vals[i]) + (i != bad)); bs_apply(test); - KUNIT_EXPECT_EQ_MSG(test, stage1_enforced_verified(), bad < 0, + KUNIT_EXPECT_EQ_MSG(test, aurora_bs_test.stage1_enforced_verified(), bad < 0, "unterminated %s", bad < 0 ? "none" : names[bad]); bs_exit(test); } @@ -348,7 +386,7 @@ static void bs_stage1_string_list(struct kunit *test) bs_raw(test, c, "policy", lists, sizeof(lists)); bs_str(test, c, "verdict", "verified"); bs_apply(test); - KUNIT_EXPECT_FALSE(test, stage1_enforced_verified()); + KUNIT_EXPECT_FALSE(test, aurora_bs_test.stage1_enforced_verified()); } /* --- The chain state ---------------------------------------------------- */ @@ -392,11 +430,11 @@ static void bs_state_demotion(struct kunit *test) p->len > 0 ? p->len : p->len < 0 ? 0 : strlen(p->state) + 1); bs_apply(test); - stage1_enforced = p->enforced; + *aurora_bs_test.stage1_enforced = p->enforced; if (p->expect) - KUNIT_EXPECT_STREQ(test, effective_state(), p->expect); + KUNIT_EXPECT_STREQ(test, aurora_bs_test.effective_state(), p->expect); else - KUNIT_EXPECT_STRNEQ(test, effective_state(), "verified"); + KUNIT_EXPECT_STRNEQ(test, aurora_bs_test.effective_state(), "verified"); } /* A record that names the second string of a list is not verified. */ @@ -407,8 +445,8 @@ static void bs_state_string_list(struct kunit *test) bs_raw(test, np, "state", list, sizeof(list)); bs_apply(test); - stage1_enforced = true; - KUNIT_EXPECT_STREQ(test, effective_state(), "partial"); + *aurora_bs_test.stage1_enforced = true; + KUNIT_EXPECT_STREQ(test, aurora_bs_test.effective_state(), "partial"); } static void bs_state_and_advisory_show(struct kunit *test) @@ -425,9 +463,9 @@ static void bs_state_and_advisory_show(struct kunit *test) bs_apply(test); bs_decide(); - KUNIT_EXPECT_EQ(test, state_show(NULL, &state_attr, buf), 9); + KUNIT_EXPECT_EQ(test, bs_show(aurora_bs_test.state, buf), 9); KUNIT_EXPECT_STREQ(test, buf, "verified\n"); - KUNIT_EXPECT_EQ(test, advisory_show(NULL, &advisory_attr, buf), 2); + KUNIT_EXPECT_EQ(test, bs_show(aurora_bs_test.advisory, buf), 2); KUNIT_EXPECT_STREQ(test, buf, "0\n"); } @@ -445,9 +483,9 @@ static void bs_state_and_advisory_show_audit(struct kunit *test) bs_apply(test); bs_decide(); - state_show(NULL, &state_attr, buf); + bs_show(aurora_bs_test.state, buf); KUNIT_EXPECT_STREQ(test, buf, "partial\n"); - advisory_show(NULL, &advisory_attr, buf); + bs_show(aurora_bs_test.advisory, buf); KUNIT_EXPECT_STREQ(test, buf, "1\n"); } @@ -456,11 +494,11 @@ static void bs_state_show_defaults(struct kunit *test) { char *buf = bs_page(test); - state_show(NULL, &state_attr, buf); + bs_show(aurora_bs_test.state, buf); KUNIT_EXPECT_STREQ(test, buf, "none\n"); - advisory_show(NULL, &advisory_attr, buf); + bs_show(aurora_bs_test.advisory, buf); KUNIT_EXPECT_STREQ(test, buf, "1\n"); - KUNIT_EXPECT_EQ(test, lockdown_show(NULL, &lockdown_attr, buf), 3); + KUNIT_EXPECT_EQ(test, bs_show(aurora_bs_test.lockdown, buf), 3); KUNIT_EXPECT_STREQ(test, buf, "no\n"); } @@ -475,7 +513,7 @@ static void bs_policy_show_case(struct kunit *test, bs_test_record(test); bs_meas(test, m, n); bs_apply(test); - KUNIT_EXPECT_EQ(test, policy_show(NULL, &policy_attr, buf), + KUNIT_EXPECT_EQ(test, bs_show(aurora_bs_test.policy, buf), (ssize_t)strlen(expect)); KUNIT_EXPECT_STREQ(test, buf, expect); } @@ -557,7 +595,7 @@ static void bs_policy_show_bounded(struct kunit *test) bs_test_record(test); bs_meas(test, m, n); bs_apply(test); - ret = policy_show(NULL, &policy_attr, buf); + ret = bs_show(aurora_bs_test.policy, buf); KUNIT_EXPECT_EQ(test, ret, (ssize_t)PAGE_SIZE - 1); KUNIT_EXPECT_EQ(test, strlen(buf), (size_t)ret); bs_expect_guard(test, buf, PAGE_SIZE, 2 * PAGE_SIZE); @@ -605,9 +643,9 @@ static void bs_string_show(struct kunit *test) p->len < 0 ? 0 : strlen(p->val) + 1); bs_apply(test); if (p->uefi) - ret = uefi_secure_boot_show(NULL, &uefi_secure_boot_attr, buf); + ret = bs_show(aurora_bs_test.uefi_secure_boot, buf); else - ret = stage1_flavour_show(NULL, &stage1_flavour_attr, buf); + ret = bs_show(aurora_bs_test.stage1_flavour, buf); KUNIT_EXPECT_EQ(test, ret, (ssize_t)strlen(p->expect)); KUNIT_EXPECT_STREQ(test, buf, p->expect); } @@ -617,13 +655,13 @@ static void bs_string_show_long(struct kunit *test) { struct device_node *np = bs_test_record(test); char *buf = bs_page(test); - char *val = kunit_kzalloc(test, 2 * PAGE_SIZE, GFP_KERNEL); + char *val = vzalloc(2 * PAGE_SIZE); KUNIT_ASSERT_NOT_NULL(test, val); memset(val, 'x', 2 * PAGE_SIZE - 1); bs_raw(test, np, "stage1-flavour", val, 2 * PAGE_SIZE); bs_apply(test); - KUNIT_EXPECT_EQ(test, stage1_flavour_show(NULL, &stage1_flavour_attr, buf), + KUNIT_EXPECT_EQ(test, bs_show(aurora_bs_test.stage1_flavour, buf), (ssize_t)PAGE_SIZE - 1); bs_expect_guard(test, buf, PAGE_SIZE, 2 * PAGE_SIZE); } @@ -675,7 +713,7 @@ static void bs_lp_coih_show(struct kunit *test) } else { strscpy(expect, "unknown\n", 2 * 64 + 2); } - KUNIT_EXPECT_EQ(test, lp_coih_show(NULL, &lp_coih_attr, buf), + KUNIT_EXPECT_EQ(test, bs_show(aurora_bs_test.lp_coih, buf), (ssize_t)strlen(expect)); KUNIT_EXPECT_STREQ(test, buf, expect); } @@ -691,7 +729,7 @@ static void bs_measurements_show_case(struct kunit *test, bs_test_record(test); bs_meas(test, m, n); bs_apply(test); - KUNIT_EXPECT_EQ(test, measurements_show(NULL, &measurements_attr, buf), + KUNIT_EXPECT_EQ(test, bs_show(aurora_bs_test.measurements, buf), (ssize_t)strlen(expect)); KUNIT_EXPECT_STREQ(test, buf, expect); } @@ -775,7 +813,7 @@ static void bs_format_measurements_bounded(struct kunit *test) size_t sz = sizes[i], want = min(sz - 1, strlen(full)); memset(buf, BS_GUARD, 512); - ret = format_measurements(buf, sz); + ret = aurora_bs_test.format_measurements(buf, sz); KUNIT_EXPECT_EQ_MSG(test, ret, (ssize_t)want, "size %zu", sz); KUNIT_EXPECT_EQ_MSG(test, buf[want], 0, "size %zu", sz); /* A cut first line leaves no room for the second. */ @@ -804,7 +842,7 @@ static void bs_measurements_show_bounded(struct kunit *test) bs_test_record(test); bs_meas(test, m, n); bs_apply(test); - ret = measurements_show(NULL, &measurements_attr, buf); + ret = bs_show(aurora_bs_test.measurements, buf); KUNIT_EXPECT_EQ(test, ret, (ssize_t)PAGE_SIZE - 1); KUNIT_EXPECT_EQ(test, strlen(buf), (size_t)ret); bs_expect_guard(test, buf, PAGE_SIZE, 2 * PAGE_SIZE); @@ -829,12 +867,13 @@ static void bs_measure_record_full_page(struct kunit *test) bs_meas(test, m, n); bs_apply(test); bs_decide(); - measure_record(); + aurora_bs_test.measure_record(); } static struct kunit_case bs_cases[] = { KUNIT_CASE(bs_stage1_no_measurements), KUNIT_CASE_PARAM(bs_stage1_one_measurement, bs_one_gen_params), + KUNIT_CASE(bs_stage1_dev_enforce), KUNIT_CASE(bs_stage1_among_others), KUNIT_CASE(bs_stage1_none_among_many), KUNIT_CASE(bs_stage1_unterminated), @@ -870,6 +909,9 @@ static struct kunit_suite bs_suite = { .test_cases = bs_cases, }; +kunit_test_suite(bs_suite); + +#if IS_BUILTIN(CONFIG_APPLE_AURORA_BOOT_SECURITY_KUNIT_TEST) /* --- The init path, on the real /chosen/aurora,boot-security ----------- */ static int bs_init_init(struct kunit *test) @@ -1087,5 +1129,8 @@ static struct kunit_suite bs_init_suite = { .test_cases = bs_init_cases, }; -kunit_test_suites(&bs_suite); kunit_test_init_section_suites(&bs_init_suite); +#else +MODULE_IMPORT_NS("EXPORTED_FOR_KUNIT_TESTING"); +MODULE_LICENSE("Dual MIT/GPL"); +#endif diff --git a/drivers/soc/apple/aurora-boot-security-test.h b/drivers/soc/apple/aurora-boot-security-test.h new file mode 100644 index 0000000000000..4c221d1dae3b5 --- /dev/null +++ b/drivers/soc/apple/aurora-boot-security-test.h @@ -0,0 +1,35 @@ +/* SPDX-License-Identifier: GPL-2.0-only OR MIT */ +#ifndef _AURORA_BOOT_SECURITY_TEST_H +#define _AURORA_BOOT_SECURITY_TEST_H + +#include + +struct device_node; +struct kobject; +struct kobj_attribute; + +/* Test-only access to the record reader and its sysfs callbacks. */ +struct aurora_boot_security_test { + struct device_node **rec; + struct kobject **aurora_kobj; + struct kobject **bs_kobj; + const char **chain_state; + bool *stage1_enforced; + const char **lockdown_reason; + bool (*stage1_enforced_verified)(void); + const char *(*effective_state)(void); + ssize_t (*format_measurements)(char *buf, size_t size); + void (*measure_record)(void); + struct kobj_attribute *state; + struct kobj_attribute *advisory; + struct kobj_attribute *policy; + struct kobj_attribute *stage1_flavour; + struct kobj_attribute *uefi_secure_boot; + struct kobj_attribute *lockdown; + struct kobj_attribute *lp_coih; + struct kobj_attribute *measurements; +}; + +extern const struct aurora_boot_security_test aurora_bs_test; + +#endif diff --git a/drivers/soc/apple/aurora-boot-security.c b/drivers/soc/apple/aurora-boot-security.c index efa3c20b781ad..04bd01d5b8e38 100644 --- a/drivers/soc/apple/aurora-boot-security.c +++ b/drivers/soc/apple/aurora-boot-security.c @@ -317,5 +317,34 @@ MODULE_DESCRIPTION("Report the boot chain record left by aura and U-Boot"); MODULE_LICENSE("Dual MIT/GPL"); #if IS_ENABLED(CONFIG_APPLE_AURORA_BOOT_SECURITY_KUNIT_TEST) +#include + +#include "aurora-boot-security-test.h" + +const struct aurora_boot_security_test aurora_bs_test = { + .rec = &rec, + .aurora_kobj = &aurora_kobj, + .bs_kobj = &bs_kobj, + .chain_state = &chain_state, + .stage1_enforced = &stage1_enforced, + .lockdown_reason = &lockdown_reason, + .stage1_enforced_verified = stage1_enforced_verified, + .effective_state = effective_state, + .format_measurements = format_measurements, + .measure_record = measure_record, + .state = &state_attr, + .advisory = &advisory_attr, + .policy = &policy_attr, + .stage1_flavour = &stage1_flavour_attr, + .uefi_secure_boot = &uefi_secure_boot_attr, + .lockdown = &lockdown_attr, + .lp_coih = &lp_coih_attr, + .measurements = &measurements_attr, +}; +#endif + +#if IS_MODULE(CONFIG_APPLE_AURORA_BOOT_SECURITY_KUNIT_TEST) +EXPORT_SYMBOL_IF_KUNIT(aurora_bs_test); +#elif IS_BUILTIN(CONFIG_APPLE_AURORA_BOOT_SECURITY_KUNIT_TEST) #include "aurora-boot-security-test.c" #endif