Skip to content

eBPF-based security platform

tpm: Keep the NV writer in the TPM

KP Singh5c6920ccdbe6
diff --git a/Makefile.shirudo b/Makefile.shirudo
index eee6a60e..048abaf6 100644
--- a/Makefile.shirudo
+++ b/Makefile.shirudo
@@ -628,8 +628,6 @@ TEST_HELPERS := $(VERITY_TEST_HELPERS) \
testing/helper/test-upatch-link-lockdown \
testing/helper/test-exec-churn \
testing/upatch/kernel-obs/exec/exec-paths
-TEST_HELPERS += testing/helper/test-nv-writer
-TEST_HELPERS += testing/helper/test-nv-writer-cleanup
TEST_HELPERS += testing/helper/test-keyring-seal
TEST_HELPERS += testing/helper/test-tpm-preopened
@@ -671,22 +669,6 @@ testing/helper/test-nv-writer: testing/helper/test-nv-writer.c $(REGISTER_OBJS)
@printf ' CC $@\n'
$(Q)$(CC) $(CFLAGS) -o $@ $^ $(REGISTER_LIBS)
-testing/fixtures/nv-writer-fault.bpf.o: testing/fixtures/nv-writer-fault.bpf.c \
- testing/fixtures/nv-writer-fault.h $(VMLINUX_H) $(BPF_LIBBPF_HEADERS)
- @printf ' CLANG $@\n'
- $(Q)$(CLANG_BPF) -I$(dir $(VMLINUX_H)) -I$(LIBBPF_VENDOR) -c $< -o $@
-
-testing/helper/test-nv-writer-cleanup: testing/helper/test-nv-writer-cleanup.c \
- testing/fixtures/nv-writer-fault.h builtin-init.c \
- $(filter-out $(call obj,builtin-init.o),$(BUILTIN_OBJS)) \
- $(UTIL_OBJS) $(VERSION_OBJ) $(LIBBPF_A) \
- testing/fixtures/nv-writer-fault.bpf.o
- @printf ' CC $@\n'
- $(Q)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $< \
- $(filter-out testing/fixtures/nv-writer-fault.bpf.o,$(filter %.o %.a,$^)) \
- -Wl,--wrap=shirudo_image_runtime_root \
- $(STATIC_LIBS) $(DYNAMIC_LIBS)
-
testing/helper/test-keyring-seal: testing/helper/test-keyring-seal.c \
testing/helper/keyring-seal.h builtin-init.c \
$(filter-out $(call obj,builtin-init.o),$(BUILTIN_OBJS)) \
diff --git a/bpf/admission.h b/bpf/admission.h
index b03e4574..a50d10d6 100644
--- a/bpf/admission.h
+++ b/bpf/admission.h
@@ -4,6 +4,8 @@
#ifndef SHIRUDO_BPF_ADMISSION_H
#define SHIRUDO_BPF_ADMISSION_H
+#include <bpf/bpf_endian.h>
+
/*
* BPF mutations require a task marked by successful exec from the runtime
* image. Verified signed loaders perform their own calls in kernel context.
@@ -270,42 +272,16 @@ static __always_inline int shirudo_bpffs_guard(__u32 target_dev,
return -EPERM;
}
-/*
- * NV-key search can sign or invalidate even when possessed. Do not count
- * search denials: ordinary keyring traversal probes use that permission.
- */
-static __always_inline int handle_nv_sig_key(const struct shirudo_sb_id *id,
- enum key_need_perm need)
-{
- if (need == KEY_NEED_VIEW || shirudo_task_is_self(id))
- return 0;
- if (need == KEY_NEED_SEARCH)
- return -EACCES;
- return shirudo_tamper_deny(SHIRUDO_TAMPER_KEYRING, -EACCES);
-}
-
-static __always_inline int handle_nv_keyring(const struct shirudo_sb_id *id,
- enum key_need_perm need)
-{
- if (need == KEY_NEED_VIEW || need == KEY_NEED_READ ||
- shirudo_task_is_self(id))
- return 0;
- if (need == KEY_NEED_SEARCH)
- return -EACCES;
- return shirudo_tamper_deny(SHIRUDO_TAMPER_KEYRING, -EACCES);
-}
-
/*
* The trust anchors in the kernel's keyrings: the bpf and dm-verity
* keyrings init froze and the keys they hold. Root owns both with write
* and setattr permission, so without this hook a single keyctl could
* unlink, clear, revoke or invalidate the certificates every load and
* every config root is verified against. Deny those needs to every
- * task but shirudo. A
- * search is denied only through an unpossessed reference, which is
- * how keyctl reaches a key by serial to invalidate it; the kernel
- * verifies a signature by searching the keyring it possesses, and
- * that stays open to every loader.
+ * task but shirudo. A search is denied only through an unpossessed
+ * reference, which is how keyctl reaches a key by serial to invalidate
+ * it; the kernel verifies a signature by searching the keyring it
+ * possesses, and that stays open to every loader.
*/
SEC("lsm/key_permission")
int BPF_PROG(shirudo_lsm_key_permission, key_ref_t key_ref,
@@ -324,11 +300,6 @@ int BPF_PROG(shirudo_lsm_key_permission, key_ref_t key_ref,
return 0;
key = (struct key *)((unsigned long)key_ref & ~1UL);
serial = BPF_CORE_READ(key, serial);
- if (serial == id->nv_writer_key)
- return handle_nv_sig_key(id, need);
- if (serial == id->nv_writer_ring)
- return handle_nv_keyring(id, need);
-
switch (need) {
case KEY_NEED_WRITE:
case KEY_NEED_SEARCH:
@@ -355,6 +326,38 @@ int BPF_PROG(shirudo_lsm_key_permission, key_ref_t key_ref,
shirudo_tamper_deny(SHIRUDO_TAMPER_KEYRING, -EACCES);
}
+/*
+ * A TPM signature alone cannot change Shirudo's NV register. Restrict
+ * extension of its frozen index while leaving unrelated TPM signing alone.
+ */
+SEC("lsm/tpm_command")
+int BPF_PROG(shirudo_lsm_tpm_command, struct file *file,
+ const __u8 *command, size_t command_len, int ret)
+{
+ struct shirudo_sb_id *id;
+ __u32 code, index;
+
+ (void)file;
+ if (ret)
+ return ret;
+ if (command_len < 10 ||
+ bpf_probe_read_kernel(&code, sizeof(code), command + 6))
+ return shirudo_tamper_deny(SHIRUDO_TAMPER_TPM_EXTEND,
+ -EACCES);
+ if (bpf_ntohl(code) != SHIRUDO_TPM_CC_NV_EXTEND)
+ return 0;
+ if (command_len < 18 ||
+ bpf_probe_read_kernel(&index, sizeof(index), command + 14))
+ return shirudo_tamper_deny(SHIRUDO_TAMPER_TPM_EXTEND,
+ -EACCES);
+ id = shirudo_policy();
+ if (!id || (bpf_ntohl(index) == id->nv_index &&
+ !shirudo_task_is_self(id)))
+ return shirudo_tamper_deny(SHIRUDO_TAMPER_TPM_EXTEND,
+ -EACCES);
+ return 0;
+}
+
/*
* Inside shirudo's bpffs: only an authorized caller may unlink
* (the `upatch unload` path). Every other caller -- including root
diff --git a/bpf/lib/shirudo_identity.h b/bpf/lib/shirudo_identity.h
index 943f1f7f..d024e893 100644
--- a/bpf/lib/shirudo_identity.h
+++ b/bpf/lib/shirudo_identity.h
@@ -17,13 +17,13 @@ struct shirudo_sb_id {
__u32 n_keys;
__s32 key_serials[SHIRUDO_IDENTITY_KEYS_MAX];
__u8 runtime_root[32];
- __s32 nv_writer_ring;
- __s32 nv_writer_key;
+ __u8 nv_writer_seed[32];
__u8 nv_writer_public[SHIRUDO_TPM_NV_RSA_BYTES];
__u8 ak_name[SHIRUDO_TPM_NAME_LEN];
__u32 ak_name_len;
__s32 dm_verity_ring;
__s32 dm_verity_key;
+ __u32 nv_index;
};
struct {
diff --git a/bpf/lib/shirudo_tamper.h b/bpf/lib/shirudo_tamper.h
index e4ab7ee4..242bf116 100644
--- a/bpf/lib/shirudo_tamper.h
+++ b/bpf/lib/shirudo_tamper.h
@@ -45,6 +45,7 @@ enum shirudo_tamper_point {
SHIRUDO_TAMPER_PIN_ENTRY,
SHIRUDO_TAMPER_SEAL_COPY_UP,
SHIRUDO_TAMPER_KEXEC,
+ SHIRUDO_TAMPER_TPM_EXTEND,
SHIRUDO_TAMPER_MAX
};
diff --git a/builtin-init.c b/builtin-init.c
index ad141bab..1917a0fc 100644
--- a/builtin-init.c
+++ b/builtin-init.c
@@ -601,12 +601,12 @@ static int init_lsm(void)
struct bpf_link *seal_links[SHIRUDO_SEAL_HOOK_COUNT] = {};
struct bpf_link *link_bpf = NULL, *link_bpf_map = NULL;
struct bpf_object *obj = NULL, *seal_obj = NULL;
- struct bpf_link *link_keyperm = NULL;
+ struct bpf_link *link_keyperm = NULL, *link_tpm = NULL;
struct bpf_link *link_unlink = NULL;
struct shirudo_identity id = {};
struct anchor_keys keys = {};
struct bpf_program *p_pload;
- struct bpf_program *p_key;
+ struct bpf_program *p_key, *p_tpm;
key_serial_t keyring;
struct bpf_map *m_sb;
int err, map_fd, i;
@@ -615,10 +615,10 @@ static int init_lsm(void)
if (access(shirudo_gate_guards[SHIRUDO_GUARD_BPF].pin, F_OK) == 0) {
err = identity_read(&id);
- if (err || id.nv_writer_ring <= 0 ||
- id.nv_writer_key <= 0 ||
- access(SHIRUDO_KEY_PERMISSION_LINK_PIN, F_OK)) {
- pr_err("init: NV writer protection missing; reboot "
+ if (err || !(id.nv_writer_public[0] & 0x80) ||
+ id.nv_index != SHIRUDO_NV_INDEX ||
+ access(SHIRUDO_TPM_COMMAND_LINK_PIN, F_OK)) {
+ pr_err("init: TPM NV index guard missing or changed; reboot "
"with the updated initrd");
return err ? err : -EACCES;
}
@@ -704,15 +704,14 @@ static int init_lsm(void)
goto out;
}
id.n_keys = (__u32)keys.n;
+ id.nv_index = SHIRUDO_NV_INDEX;
memcpy(id.key_serials, keys.serials, keys.n * sizeof(keys.serials[0]));
err = dm_verity_anchor(&id);
if (err)
goto out;
- err = nv_writer_create(&id.nv_writer_ring, &id.nv_writer_key, id.nv_writer_public);
+ err = nv_writer_create(id.nv_writer_seed, id.nv_writer_public);
if (err) {
- pr_err("init: create RSA-3072 NV writer: %s "
- "(requires kernel PKCS#8 private-key and RSA support)",
- strerror(-err));
+ pr_err("init: create TPM RSA-3072 NV writer: %s", strerror(-err));
goto out;
}
err = record_attestation_key(&id);
@@ -771,10 +770,11 @@ static int init_lsm(void)
p_pload = guard_program(obj, SHIRUDO_GUARD_PROG_LOAD);
p_bpf_map = guard_program(obj, SHIRUDO_GUARD_BPF_MAP);
p_key = guard_program(obj, SHIRUDO_GUARD_KEY_PERMISSION);
+ p_tpm = guard_program(obj, SHIRUDO_GUARD_TPM_COMMAND);
p_bpf = guard_program(obj, SHIRUDO_GUARD_BPF);
if (!p_unlink || !p_setx || !p_rmx || !p_pload || !p_bpf_map ||
!p_bpf ||
- !p_key) {
+ !p_key || !p_tpm) {
pr_err("expected programs missing in bpf2 object");
err = -ENOENT;
goto out;
@@ -808,6 +808,11 @@ static int init_lsm(void)
pr_err("attach lsm/key_permission: %s", strerror(-err));
goto out;
}
+ err = attach_guard(p_tpm, SHIRUDO_GUARD_TPM_COMMAND, &link_tpm);
+ if (err) {
+ pr_err("attach lsm/tpm_command: %s", strerror(-err));
+ goto out;
+ }
/*
* With the other bpffs guards, so the mount is immutable before
* enforcement goes live.
@@ -875,7 +880,7 @@ out:
}
if (err) {
struct bpf_link *links[] = {
- link_bpf, link_bpf_map, link_pload, link_keyperm,
+ link_bpf, link_bpf_map, link_pload, link_keyperm, link_tpm,
link_rmx, link_setx, link_unlink,
};
@@ -893,12 +898,11 @@ out:
bpf_link__destroy(link_bpf_map);
bpf_link__destroy(link_pload);
bpf_link__destroy(link_keyperm);
+ bpf_link__destroy(link_tpm);
bpf_link__destroy(link_rmx);
bpf_link__destroy(link_setx);
bpf_link__destroy(link_unlink);
bpf_object__close(obj);
- if (err)
- nv_writer_destroy(id.nv_writer_ring);
return err;
}
diff --git a/contrib/build-test-vm-bundle.sh b/contrib/build-test-vm-bundle.sh
index e53f8623..c7b290b1 100755
--- a/contrib/build-test-vm-bundle.sh
+++ b/contrib/build-test-vm-bundle.sh
@@ -318,8 +318,6 @@ make -C "$REPO_ROOT" -j"$(nproc)" -f Makefile.shirudo \
install -d "$TMP_DIR/test-helpers/testing/helper" \
"$TMP_DIR/test-helpers/testing/fixtures" \
"$TMP_DIR/test-helpers/testing/upatch/kernel-obs/exec"
-install -m 0644 "$REPO_ROOT/testing/fixtures/nv-writer-fault.bpf.o" \
- "$TMP_DIR/test-helpers/testing/fixtures/"
while IFS= read -r helper; do
install -m 0755 "$REPO_ROOT/$helper" "$TMP_DIR/test-helpers/$helper"
done <<'EOF'
@@ -333,7 +331,6 @@ testing/helper/tpmsim-cmd
testing/helper/test-tpm-wire
testing/helper/test-nv-cphash
testing/helper/test-nv-writer
-testing/helper/test-nv-writer-cleanup
testing/helper/test-keyring-seal
testing/helper/test-tpm-preopened
testing/helper/test-config
diff --git a/contrib/test-vm-guest.sh b/contrib/test-vm-guest.sh
index 83cc4ef2..67019cc1 100755
--- a/contrib/test-vm-guest.sh
+++ b/contrib/test-vm-guest.sh
@@ -347,15 +347,11 @@ if [[ "${TEST_VM_SUITE:-all}" == register ]]; then
log "register protocol tests must precede production initialization"
exit 1
}
- log "running register protocol tests with a real kernel writer"
+ log "running register protocol tests with a TPM writer"
: >/mnt/shirudo-out/failed-tests
: >/mnt/shirudo-out/failed-suites
(
cd testing
- SHIRUDO_TEST_REGISTER_VM=1 \
- SHIRUDO_TEST_NV_CERT="$BUNDLE/fixtures/cert.der" \
- SHIRUDO_TEST_FAILURES_FILE=/mnt/shirudo-out/failed-tests \
- sh tpm/t1128-nv-writer-cleanup.sh
SHIRUDO_TEST_REGISTER_VM=1 \
SHIRUDO_TEST_FAILURES_FILE=/mnt/shirudo-out/failed-tests \
sh tpm/t0034-register.sh
diff --git a/include/shirudo/tpm.h b/include/shirudo/tpm.h
index 03bdb237..6d209a13 100644
--- a/include/shirudo/tpm.h
+++ b/include/shirudo/tpm.h
@@ -5,6 +5,7 @@
#define SHIRUDO_TPM_NV_RSA_BYTES 384
#define SHIRUDO_TPM_NAME_LEN 34 /* nameAlg and a SHA-256 */
+#define SHIRUDO_TPM_CC_NV_EXTEND 0x00000136u
#define SHIRUDO_TPM_NV_RSA_PUBLIC_AREA_LEN (24 + SHIRUDO_TPM_NV_RSA_BYTES)
#endif /* SHIRUDO_TPM_TYPES_H */
diff --git a/testing/helper/test-nv-cphash.c b/testing/helper/test-nv-cphash.c
index f1e9393c..5eebb410 100644
--- a/testing/helper/test-nv-cphash.c
+++ b/testing/helper/test-nv-cphash.c
@@ -1,14 +1,14 @@
// SPDX-License-Identifier: BSD-2-Clause
/* Copyright (c) 2026 Authors of shirudo */
+#include <errno.h>
#include <stdbool.h>
-#include <linux/keyctl.h>
#include <stdio.h>
#include <string.h>
+#include <sys/random.h>
#include "tpm_internal.h"
#include "crypto.h"
-#include "nv_writer.h"
#define TEST_NV_INDEX 0x01818E01u
@@ -56,8 +56,41 @@ out:
return rc;
}
+static int sign_digest(ESYS_CONTEXT *esys, ESYS_TR writer,
+ const __u8 digest[32],
+ __u8 signature[SHIRUDO_TPM_NV_RSA_BYTES])
+{
+ TPMT_TK_HASHCHECK validation = {
+ .tag = TPM2_ST_HASHCHECK,
+ .hierarchy = TPM2_RH_NULL,
+ };
+ TPMT_SIG_SCHEME scheme = { .scheme = TPM2_ALG_RSASSA };
+ TPMT_SIGNATURE *signed_digest = NULL;
+ TPM2B_DIGEST input = { .size = 32 };
+ int rc = 1;
+
+ scheme.details.rsassa.hashAlg = TPM2_ALG_SHA256;
+ memcpy(input.buffer, digest, input.size);
+ if (tpm_failed(Esys_Sign(esys, writer, ESYS_TR_PASSWORD,
+ ESYS_TR_NONE, ESYS_TR_NONE, &input,
+ &scheme, &validation, &signed_digest), "Sign"))
+ goto out;
+ if (!signed_digest || signed_digest->sigAlg != TPM2_ALG_RSASSA ||
+ signed_digest->signature.rsassa.hash != TPM2_ALG_SHA256 ||
+ signed_digest->signature.rsassa.sig.size != SHIRUDO_TPM_NV_RSA_BYTES) {
+ fprintf(stderr, "Sign returned an unexpected signature\n");
+ goto out;
+ }
+ memcpy(signature, signed_digest->signature.rsassa.sig.buffer,
+ SHIRUDO_TPM_NV_RSA_BYTES);
+ rc = 0;
+out:
+ Esys_Free(signed_digest);
+ return rc;
+}
+
static int authorize(struct shirudo_tpm *t, ESYS_TR writer, ESYS_TR nv,
- key_serial_t key, const __u8 event[32], const __u8 policy[32],
+ const __u8 event[32], const __u8 policy[32],
ESYS_TR *session, __u8 saved[SHIRUDO_TPM_NV_RSA_BYTES],
enum signature_fault fault)
{
@@ -83,7 +116,7 @@ static int authorize(struct shirudo_tpm *t, ESYS_TR writer, ESYS_TR nv,
if (fault == SIGNATURE_REPLAY) {
memcpy(signature, saved, sizeof(signature));
} else {
- if (nv_writer_sign_key(key, authorization.digest, signature))
+ if (sign_digest(t->esys, writer, authorization.digest, signature))
goto out;
if (fault == SIGNATURE_CORRUPT)
signature[0] ^= 1;
@@ -119,19 +152,27 @@ int main(void)
ESYS_TR writer = ESYS_TR_NONE;
struct shirudo_tpm *t = NULL;
ESYS_CONTEXT *esys = NULL;
- key_serial_t ring = 0, key = 0;
ESYS_TR nv = ESYS_TR_NONE;
+ __u8 writer_seed[32];
__u8 policy[32], zero[32] = {};
bool defined = false;
+ ssize_t got;
int rc = 1, err;
TSS2_RC trc;
if (tpm_open(&t))
goto out;
esys = t->esys;
- err = nv_writer_create(&ring, &key, writer_public);
+ do {
+ got = getrandom(writer_seed, sizeof(writer_seed), 0);
+ } while (got < 0 && errno == EINTR);
+ if (got != sizeof(writer_seed)) {
+ perror("getrandom");
+ goto out;
+ }
+ err = tpm_writer_create(t, writer_seed, writer_public);
if (err) {
- fprintf(stderr, "create kernel NV writer: %s\n", strerror(-err));
+ fprintf(stderr, "create TPM NV writer: %s\n", strerror(-err));
goto out;
}
if (tpm_policy_signed(zero, writer_public, policy)) {
@@ -139,14 +180,14 @@ int main(void)
goto out;
}
tpm_policy_command_code(policy, SHIRUDO_TPM_CC_NV_EXTEND, policy);
- if (tpm_writer_load(t, writer_public, &writer) ||
+ if (tpm_writer_load(t, writer_seed, writer_public, &writer) ||
tpm_nv_define(t, TEST_NV_INDEX, policy, NULL, 0))
goto out;
defined = true;
if (tpm_failed(Esys_TR_FromTPMPublic(esys, TEST_NV_INDEX, ESYS_TR_NONE,
ESYS_TR_NONE, ESYS_TR_NONE, &nv),
"TR_FromTPMPublic") ||
- authorize(t, writer, nv, key, seed.buffer, policy,
+ authorize(t, writer, nv, seed.buffer, policy,
&session, signature, SIGNATURE_VALID) ||
tpm_failed(Esys_NV_Extend(esys, nv, nv, session, ESYS_TR_NONE,
ESYS_TR_NONE, &seed), "NV_Extend(seed)") ||
@@ -156,7 +197,7 @@ int main(void)
ESYS_TR_NONE, ESYS_TR_NONE, &nv),
"TR_FromTPMPublic") ||
read_value(esys, nv, before) ||
- authorize(t, writer, nv, key, event_a.buffer, policy,
+ authorize(t, writer, nv, event_a.buffer, policy,
&session, signature, SIGNATURE_VALID))
goto out;
trc = Esys_NV_Extend(esys, nv, nv, session, ESYS_TR_NONE, ESYS_TR_NONE, &event_b);
@@ -171,10 +212,10 @@ int main(void)
fprintf(stderr, "rejected NV_Extend changed the scratch value\n");
goto out;
}
- if (authorize(t, writer, nv, key, event_a.buffer, policy,
+ if (authorize(t, writer, nv, event_a.buffer, policy,
&session, signature, SIGNATURE_REPLAY) ||
flush(esys, &session) ||
- authorize(t, writer, nv, key, event_a.buffer, policy,
+ authorize(t, writer, nv, event_a.buffer, policy,
&session, signature, SIGNATURE_CORRUPT) ||
flush(esys, &session) || read_value(esys, nv, after))
goto out;
@@ -182,7 +223,7 @@ int main(void)
fprintf(stderr, "replayed or corrupt signature changed the scratch value\n");
goto out;
}
- if (authorize(t, writer, nv, key, event_a.buffer, policy,
+ if (authorize(t, writer, nv, event_a.buffer, policy,
&session, signature, SIGNATURE_VALID) ||
tpm_failed(Esys_NV_Extend(esys, nv, nv, session, ESYS_TR_NONE,
ESYS_TR_NONE, &event_a), "NV_Extend(A)") ||
@@ -205,13 +246,6 @@ out:
rc = 1;
}
tpm_close(t);
- if (ring > 0) {
- err = keyring_unlink_key(KEY_SPEC_USER_KEYRING, ring);
- if (err) {
- fprintf(stderr, "unlink scratch writer keyring: %s\n", strerror(-err));
- rc = 1;
- }
- }
if (!rc) {
puts("PolicySigned: replay and corruption rejected; value unchanged");
puts("cpHash: authorized A; rejected B unchanged; extended A; scratch index removed");
diff --git a/testing/helper/test-register.c b/testing/helper/test-register.c
index b5a1f8ff..82ac9457 100644
--- a/testing/helper/test-register.c
+++ b/testing/helper/test-register.c
@@ -8,11 +8,9 @@
*/
#include <errno.h>
-#include <linux/keyctl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <sys/syscall.h>
#include <unistd.h>
#include <bpf/bpf.h>
@@ -32,27 +30,17 @@
#include "tpm.h"
#include "version.h"
-/* Provision real kernel signing authority before the VM attaches its LSM. */
+/* Record the TPM writer's public identity before the VM attaches its LSM. */
static int writer_create(void)
{
struct bpf_map_create_opts opts = { .sz = sizeof(opts) };
struct shirudo_identity id = {};
- int fd, rc, unlink_rc;
+ int fd, rc;
__u32 zero = 0;
- long ret;
- rc = nv_writer_create(&id.nv_writer_ring, &id.nv_writer_key,
- id.nv_writer_public);
+ rc = nv_writer_create(id.nv_writer_seed, id.nv_writer_public);
if (rc)
goto out;
- errno = 0;
- ret = syscall(__NR_keyctl, KEYCTL_SETPERM, id.nv_writer_ring, 0x3f3f0000L, 0, 0);
- if (ret != -1 || errno != EACCES) {
- fprintf(stderr, "writer keyring permissions were not sealed: %ld/%d\n",
- ret, errno);
- rc = ret < 0 ? -errno : -EPROTO;
- goto out;
- }
fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, "shirudo_test_id", sizeof(zero),
sizeof(id), 1, &opts);
if (fd < 0) {
@@ -66,34 +54,17 @@ static int writer_create(void)
if (!rc)
return 0;
out:
- if (id.nv_writer_ring > 0) {
- unlink_rc = keyring_unlink_key(KEY_SPEC_USER_KEYRING, id.nv_writer_ring);
- if (unlink_rc)
- fprintf(stderr, "unlink protocol-test writer: %s\n",
- strerror(-unlink_rc));
- }
- fprintf(stderr, "kernel writer setup: %s\n", strerror(-rc));
+ fprintf(stderr, "TPM writer setup: %s\n", strerror(-rc));
return 1;
}
static int writer_remove(void)
{
- struct shirudo_identity id = {};
- int rc;
-
- rc = identity_read(&id);
- if (rc) {
- fprintf(stderr, "kernel writer identity: %s\n", strerror(-rc));
- return 1;
- }
if (unlink(SHIRUDO_IDENTITY_PIN)) {
perror("unlink protocol-test identity");
return 1;
}
- rc = keyring_unlink_key(KEY_SPEC_USER_KEYRING, id.nv_writer_ring);
- if (rc)
- fprintf(stderr, "unlink protocol-test writer: %s\n", strerror(-rc));
- return rc ? 1 : 0;
+ return 0;
}
static void puthex(const __u8 *b, size_t n)
@@ -228,8 +199,9 @@ int main(int argc, char **argv)
return writer_remove();
if (!strcmp(argv[1], "writer-public") && argc == 2) {
__u8 public[SHIRUDO_TPM_NV_RSA_BYTES];
+ __u8 seed[32];
- if (nv_writer_read(public))
+ if (nv_writer_read(seed, public))
return 1;
puthex(public, sizeof(public));
return 0;
@@ -405,10 +377,13 @@ int main(int argc, char **argv)
__u8 branch_written[32], branch_init[32], policy[32];
struct shirudo_eventlog_init rec;
struct shirudo_tpm *tpm = NULL;
- __u8 values[64];
+ __u8 writer_public[SHIRUDO_TPM_NV_RSA_BYTES];
+ __u8 writer_seed[32], values[64];
if (hex_decode(argv[2], event, 32) ||
- eventlog_boot_init(&rec))
+ eventlog_boot_init(&rec) ||
+ nv_writer_read(writer_seed, writer_public) ||
+ memcmp(writer_public, rec.writer_public, sizeof(writer_public)))
return 2;
memcpy(values, rec.pcr_before, 32);
memcpy(values + 32, rec.phase_before, 32);
@@ -420,7 +395,7 @@ int main(int argc, char **argv)
}
if (tpm_open(&tpm))
return 1;
- rc = tpm_nv_set_writer(tpm, rec.writer_public);
+ rc = tpm_nv_set_writer(tpm, writer_seed, writer_public);
if (!rc)
rc = tpm_nv_extend_first(tpm, SHIRUDO_NV_INDEX,
SHIRUDO_CHAIN_PCR_MASK,
@@ -448,6 +423,7 @@ int main(int argc, char **argv)
struct shirudo_tpm_key ak_copy = {}, ek_copy = {};
__u8 writer_public[SHIRUDO_TPM_NV_RSA_BYTES];
char ts[32], boot_id[64], nv_index[16];
+ __u8 writer_seed[SHIRUDO_TPM_DIGEST_LEN];
size_t nonce_len = strlen(argv[6]) / 2;
char anchor[SHIRUDO_CHAIN_ANCHOR_MAX];
struct shirudo_tpm *tpm = NULL;
@@ -464,11 +440,11 @@ int main(int argc, char **argv)
nonce_len == 0 || nonce_len > sizeof(nonce) ||
hex_decode(argv[6], nonce, nonce_len))
return 2;
- if (nv_writer_read(writer_public))
+ if (nv_writer_read(writer_seed, writer_public))
return 1;
if (tpm_open(&tpm))
return 1;
- rc = tpm_nv_set_writer(tpm, writer_public);
+ rc = tpm_nv_set_writer(tpm, writer_seed, writer_public);
if (!rc)
rc = tpm_reset_count(tpm, &reset);
if (!rc)
diff --git a/testing/t0048-config-attest.sh b/testing/t0048-config-attest.sh
index 9eba24a4..e356eebd 100755
--- a/testing/t0048-config-attest.sh
+++ b/testing/t0048-config-attest.sh
@@ -23,14 +23,14 @@ cleanup() {
exit "$result"
}
trap cleanup EXIT HUP INT TERM
-register_writer_start || exit 1
-mkdir -m 700 "$WORK/config" "$WORK/keys"
-head -c 700 /dev/urandom >"$WORK/keys/cert.der"
-: >"$WORK/initrd-release"
export SHIRUDO_TPM_TCTI="cmd:$SIM $WORK/tpm"
export SHIRUDO_EVENTLOG_PATH="$WORK/eventlog"
export SHIRUDO_KEYS_DIR="$WORK/keys"
export SHIRUDO_INITRD_RELEASE="$WORK/initrd-release"
+register_writer_start || exit 1
+mkdir -m 700 "$WORK/config" "$WORK/keys"
+head -c 700 /dev/urandom >"$WORK/keys/cert.der"
+: >"$WORK/initrd-release"
NONCE=0102030405060708090a0b0c0d0e0f10
ROOT_A=$(python3 -c 'print("41" * 32)')
ROOT_B=$(python3 -c 'print("42" * 32)')
diff --git a/testing/tpm/t0033-tpm-wire.sh b/testing/tpm/t0033-tpm-wire.sh
index 7b792410..1f281a82 100644
--- a/testing/tpm/t0033-tpm-wire.sh
+++ b/testing/tpm/t0033-tpm-wire.sh
@@ -13,7 +13,7 @@ H=helper/test-tpm-wire
Z=0000000000000000000000000000000000000000000000000000000000000000
py() { python3 -c "$1"; }
-PUB=$(py 'import struct; print((struct.pack(">HHIHHHHHIH", 1, 11, 0x40000, 0, 16, 20, 11, 3072, 65537, 384) + bytes.fromhex("80" + "00"*382 + "01")).hex())')
+PUB=$(py 'import struct; print((struct.pack(">HHIHHHHHIH", 1, 11, 0x40072, 0, 16, 20, 11, 3072, 65537, 384) + bytes.fromhex("80" + "00"*382 + "01")).hex())')
MODULUS=$(py 'print("80" + "00"*382 + "01")')
test_expect_success 'the writer public area fixes RSA-3072, RSASSA and SHA-256' '
diff --git a/testing/tpm/t0034-register.sh b/testing/tpm/t0034-register.sh
index ab6e033a..87c665ac 100644
--- a/testing/tpm/t0034-register.sh
+++ b/testing/tpm/t0034-register.sh
@@ -23,15 +23,15 @@ cleanup() {
exit "$result"
}
trap cleanup EXIT HUP INT TERM
+export SHIRUDO_TPM_TCTI="cmd:$SIM $WORK/tpm"
+export SHIRUDO_EVENTLOG_PATH="$WORK/eventlog"
+export SHIRUDO_KEYS_DIR="$WORK/keys"
+export SHIRUDO_INITRD_RELEASE="$WORK/initrd-release"
register_writer_start || exit 1
WRITER=$(./$R writer-public) || exit 1
mkdir -p "$WORK/keys"
head -c 700 /dev/urandom >"$WORK/keys/cert.der"
: >"$WORK/initrd-release"
-export SHIRUDO_TPM_TCTI="cmd:$SIM $WORK/tpm"
-export SHIRUDO_EVENTLOG_PATH="$WORK/eventlog"
-export SHIRUDO_KEYS_DIR="$WORK/keys"
-export SHIRUDO_INITRD_RELEASE="$WORK/initrd-release"
py() { python3 -c "$1"; }
sha() { py "import hashlib; print(hashlib.sha256(bytes.fromhex(\"$1\")).hexdigest())"; }
diff --git a/util/chain.c b/util/chain.c
index 0bf275dc..b718cb7a 100644
--- a/util/chain.c
+++ b/util/chain.c
@@ -250,17 +250,17 @@ out:
static int register_writer(struct shirudo_tpm *tpm,
__u8 public[SHIRUDO_TPM_NV_RSA_BYTES])
{
+ __u8 seed[32];
int rc;
- rc = nv_writer_read(public);
- return rc ? rc : tpm_nv_set_writer(tpm, public);
+ rc = nv_writer_read(seed, public);
+ return rc ? rc : tpm_nv_set_writer(tpm, seed, public);
}
/*
- * The device the TCTI opens is root's to replace, and an emulator can
- * carry an index with the recorded name, policy and reset count, since
- * the init record is readable; the attestation key's name takes this
- * TPM's endorsement seed, which no emulator has.
+ * A different attestation key has a different Name, but the Name is
+ * public and a substitute transport can replay it. Remote evidence
+ * still needs a valid signature from the pinned key.
*/
int chain_tpm_bound(struct shirudo_tpm *tpm)
{
diff --git a/util/identity.h b/util/identity.h
index b0ea97f7..bf3c2c3f 100644
--- a/util/identity.h
+++ b/util/identity.h
@@ -26,13 +26,13 @@ struct shirudo_identity {
__u32 n_keys;
__s32 key_serials[SHIRUDO_IDENTITY_KEYS_MAX];
__u8 runtime_root[32];
- __s32 nv_writer_ring;
- __s32 nv_writer_key;
+ __u8 nv_writer_seed[32];
__u8 nv_writer_public[SHIRUDO_TPM_NV_RSA_BYTES];
__u8 ak_name[SHIRUDO_TPM_NAME_LEN];
__u32 ak_name_len;
__s32 dm_verity_ring;
__s32 dm_verity_key;
+ __u32 nv_index;
};
#define SHIRUDO_TASK_TRUSTED 1
@@ -69,7 +69,7 @@ _Static_assert(sizeof(struct shirudo_device_authority) == 48,
"device authority ABI");
_Static_assert(sizeof(struct shirudo_task_authority) == 8,
"task authority ABI");
-_Static_assert(sizeof(struct shirudo_identity) == 556,
+_Static_assert(sizeof(struct shirudo_identity) == 584,
"frozen identity ABI");
/* The bpffs mount init made, which the unmount guard keeps mounted. */
diff --git a/util/image_guards.c b/util/image_guards.c
index 402cb3c9..e134a35a 100644
--- a/util/image_guards.c
+++ b/util/image_guards.c
@@ -122,6 +122,9 @@ shirudo_gate_guards[SHIRUDO_GATE_HOOK_COUNT] = {
[SHIRUDO_GUARD_KEY_PERMISSION] = {
"shirudo_lsm_key_permission", SHIRUDO_KEY_PERMISSION_LINK_PIN,
},
+ [SHIRUDO_GUARD_TPM_COMMAND] = {
+ "shirudo_lsm_tpm_command", SHIRUDO_TPM_COMMAND_LINK_PIN,
+ },
[SHIRUDO_GUARD_BPF_MAP] = {
"shirudo_lsm_bpf_map", SHIRUDO_LSM_BPF_MAP_LINK_PIN,
},
diff --git a/util/image_guards.h b/util/image_guards.h
index b7108de5..7b3c2f10 100644
--- a/util/image_guards.h
+++ b/util/image_guards.h
@@ -24,6 +24,7 @@ enum shirudo_gate_guard {
SHIRUDO_GUARD_SETXATTR,
SHIRUDO_GUARD_REMOVEXATTR,
SHIRUDO_GUARD_KEY_PERMISSION,
+ SHIRUDO_GUARD_TPM_COMMAND,
SHIRUDO_GUARD_BPF_MAP,
SHIRUDO_GUARD_PROG_LOAD,
SHIRUDO_GUARD_BPF,
diff --git a/util/nv_writer.c b/util/nv_writer.c
index 366b6391..5424299f 100644
--- a/util/nv_writer.c
+++ b/util/nv_writer.c
@@ -2,202 +2,62 @@
/* Copyright (c) 2026 Authors of shirudo */
#include <errno.h>
-#include <linux/keyctl.h>
-#include <stdio.h>
#include <string.h>
#include <sys/random.h>
-#include <sys/syscall.h>
-#include <unistd.h>
-#include <openssl/core_names.h>
-#include <openssl/evp.h>
-#include <openssl/rsa.h>
-#include <openssl/x509.h>
-
-#include <shirudo/string.h>
-
-#include "crypto.h"
#include "identity.h"
#include "log.h"
-#include "macros.h"
-#include "malloc.h"
#include "nv_writer.h"
+#include "tpm.h"
-#define KEY_USR_VIEW 0x00010000
-#define KEY_USR_READ 0x00020000
-#define KEY_USR_WRITE 0x00040000
-#define KEY_USR_SEARCH 0x00080000
-#define KEY_USR_SETATTR 0x00200000
-#define KEY_POS_VIEW 0x01000000
-#define KEY_POS_READ 0x02000000
-#define KEY_POS_WRITE 0x04000000
-#define KEY_POS_SEARCH 0x08000000
-#define KEY_POS_LINK 0x10000000
-#define KEY_POS_SETATTR 0x20000000
-
-#define NV_RING_READ (KEY_POS_VIEW | KEY_POS_READ | KEY_POS_SEARCH | \
- KEY_USR_VIEW | KEY_USR_READ | KEY_USR_SEARCH)
-#define NV_RING_WRITE (NV_RING_READ | KEY_POS_WRITE | KEY_POS_LINK | KEY_POS_SETATTR | \
- KEY_USR_WRITE | KEY_USR_SETATTR)
-
-#define NV_WRITER_SIGN_INFO "enc=pkcs1 hash=sha256"
-
-void nv_writer_destroy(key_serial_t ring)
-{
- const key_serial_t parents[] = {
- KEY_SPEC_USER_KEYRING, KEY_SPEC_SESSION_KEYRING,
- };
- size_t i;
-
- if (ring <= 0)
- return;
- for (i = 0; i < ARRAY_SIZE(parents); i++) {
- if (syscall(__NR_keyctl, KEYCTL_UNLINK, ring, parents[i], 0, 0) < 0 &&
- errno != ENOENT)
- pr_err("nv writer: unlink ring %d from %d: %s",
- ring, parents[i], strerror(errno));
- }
-}
-
-int nv_writer_create(key_serial_t *ring, key_serial_t *writer,
+int nv_writer_create(__u8 seed[SHIRUDO_TPM_DIGEST_LEN],
__u8 public[SHIRUDO_TPM_NV_RSA_BYTES])
{
- key_serial_t writer_ring = 0, writer_key = 0;
- PKCS8_PRIV_KEY_INFO *info = NULL;
- EVP_PKEY_CTX *ctx = NULL;
- int len = 0, rc = -EIO;
- char description[64];
- EVP_PKEY *key = NULL;
- __u8 *der = NULL, *p;
- BIGNUM *n = NULL;
+ struct shirudo_tpm *t = NULL;
+ size_t off = 0;
ssize_t got;
- __u64 nonce;
- long ret;
-
- *ring = 0;
- *writer = 0;
- ctx = EVP_PKEY_CTX_new_from_name(NULL, "RSA", NULL);
- if (!ctx || EVP_PKEY_keygen_init(ctx) <= 0 ||
- EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, 3072) <= 0 ||
- EVP_PKEY_keygen(ctx, &key) <= 0 ||
- EVP_PKEY_get_bn_param(key, OSSL_PKEY_PARAM_RSA_N, &n) <= 0 ||
- BN_bn2binpad(n, public, SHIRUDO_TPM_NV_RSA_BYTES) != SHIRUDO_TPM_NV_RSA_BYTES) {
- openssl_complain("generate RSA-3072 NV writer");
- goto out;
- }
- info = EVP_PKEY2PKCS8(key);
- if (!info)
- goto out;
- len = i2d_PKCS8_PRIV_KEY_INFO(info, NULL);
- if (len <= 0)
- goto out;
- der = xmalloc(len);
- if (!der) {
- rc = -ENOMEM;
- goto out;
+ int err;
+
+ while (off < SHIRUDO_TPM_DIGEST_LEN) {
+ got = getrandom(seed + off, SHIRUDO_TPM_DIGEST_LEN - off, 0);
+ if (got < 0 && errno == EINTR)
+ continue;
+ if (got <= 0) {
+ err = got < 0 ? -errno : -EIO;
+ goto fail;
+ }
+ off += (size_t)got;
}
- p = der;
- if (i2d_PKCS8_PRIV_KEY_INFO(info, &p) != len)
- goto out;
- do {
- got = getrandom(&nonce, sizeof(nonce), 0);
- } while (got < 0 && errno == EINTR);
- if (got != sizeof(nonce)) {
- rc = got < 0 ? -errno : -EIO;
- goto out;
- }
- rc = strsprintf(description, sizeof(description), "shirudo.nv-writer.%016llx", nonce);
- if (rc < 0)
- goto out;
- ret = syscall(__NR_add_key, "keyring", description, NULL, 0,
- KEY_SPEC_SESSION_KEYRING);
- if (ret < 0)
- goto key_error;
- writer_ring = (key_serial_t)ret;
- if (syscall(__NR_keyctl, KEYCTL_SETPERM, writer_ring, NV_RING_WRITE, 0, 0) < 0 ||
- syscall(__NR_keyctl, KEYCTL_LINK, writer_ring, KEY_SPEC_USER_KEYRING, 0, 0) < 0)
- goto key_error;
- ret = syscall(__NR_add_key, "asymmetric", "shirudo.nv-writer",
- der, (size_t)len, writer_ring);
- if (ret < 0)
- goto key_error;
- writer_key = (key_serial_t)ret;
- if (syscall(__NR_keyctl, KEYCTL_SETPERM, writer_key,
- KEY_USR_VIEW | KEY_USR_SEARCH, 0, 0) < 0 ||
- syscall(__NR_keyctl, KEYCTL_RESTRICT_KEYRING, writer_ring, 0, 0, 0) < 0 ||
- syscall(__NR_keyctl, KEYCTL_SETPERM, writer_ring, NV_RING_READ, 0, 0) < 0)
- goto key_error;
- *ring = writer_ring;
- *writer = writer_key;
- rc = 0;
- goto out;
-key_error:
- rc = -errno;
- nv_writer_destroy(writer_ring);
-out:
- xfree(der, len > 0 ? (size_t)len : 0);
- PKCS8_PRIV_KEY_INFO_free(info);
- BN_free(n);
- EVP_PKEY_free(key);
- EVP_PKEY_CTX_free(ctx);
- return rc;
-}
-
-int nv_writer_read(__u8 public[SHIRUDO_TPM_NV_RSA_BYTES])
-{
- struct shirudo_identity id = {};
- int rc;
-
- rc = identity_read(&id);
- if (rc)
- goto fail;
- if (id.nv_writer_ring <= 0 || id.nv_writer_key <= 0) {
- rc = -EKEYREJECTED;
- goto fail;
- }
- memcpy(public, id.nv_writer_public, sizeof(id.nv_writer_public));
- return 0;
-fail:
- pr_err("nv writer: public key lookup failed: %s", strerror(-rc));
- return rc;
-}
-
-int nv_writer_sign_key(key_serial_t key,
- const __u8 digest[SHIRUDO_TPM_DIGEST_LEN],
- __u8 signature[SHIRUDO_TPM_NV_RSA_BYTES])
-{
- struct keyctl_pkey_params params = {
- .key_id = key,
- .in_len = SHIRUDO_TPM_DIGEST_LEN,
- .out_len = SHIRUDO_TPM_NV_RSA_BYTES,
- };
- long ret;
- int rc;
-
- ret = syscall(__NR_keyctl, KEYCTL_PKEY_SIGN, &params,
- NV_WRITER_SIGN_INFO, digest, signature);
- if (ret == SHIRUDO_TPM_NV_RSA_BYTES)
+ err = tpm_open(&t);
+ if (!err)
+ err = tpm_writer_create(t, seed, public);
+ tpm_close(t);
+ if (!err)
return 0;
- rc = ret < 0 ? -errno : -EPROTO;
- pr_err("nv writer: kernel signing failed: %s", strerror(-rc));
- return rc;
+fail:
+ pr_err("nv writer: TPM key creation failed: %s", strerror(-err));
+ explicit_bzero(seed, SHIRUDO_TPM_DIGEST_LEN);
+ memset(public, 0, SHIRUDO_TPM_NV_RSA_BYTES);
+ return err;
}
-int nv_writer_sign(const __u8 digest[SHIRUDO_TPM_DIGEST_LEN],
- __u8 signature[SHIRUDO_TPM_NV_RSA_BYTES])
+int nv_writer_read(__u8 seed[SHIRUDO_TPM_DIGEST_LEN],
+ __u8 public[SHIRUDO_TPM_NV_RSA_BYTES])
{
struct shirudo_identity id = {};
- int rc;
+ int err;
- rc = identity_read(&id);
- if (rc)
+ err = identity_read(&id);
+ if (err)
goto fail;
- if (id.nv_writer_key <= 0) {
- rc = -EKEYREJECTED;
+ if (!(id.nv_writer_public[0] & 0x80)) {
+ err = -EKEYREJECTED;
goto fail;
}
- return nv_writer_sign_key(id.nv_writer_key, digest, signature);
+ memcpy(seed, id.nv_writer_seed, SHIRUDO_TPM_DIGEST_LEN);
+ memcpy(public, id.nv_writer_public, SHIRUDO_TPM_NV_RSA_BYTES);
+ return 0;
fail:
- pr_err("nv writer: kernel signing failed: %s", strerror(-rc));
- return rc;
+ pr_err("nv writer: public key lookup failed: %s", strerror(-err));
+ return err;
}
diff --git a/util/nv_writer.h b/util/nv_writer.h
index bef46ba3..bb6a362c 100644
--- a/util/nv_writer.h
+++ b/util/nv_writer.h
@@ -3,17 +3,11 @@
#ifndef SHIRUDO_NV_WRITER_H
#define SHIRUDO_NV_WRITER_H
-#include "keyring.h"
#include "tpm_wire.h"
-int nv_writer_create(key_serial_t *ring, key_serial_t *writer,
+int nv_writer_create(__u8 seed[SHIRUDO_TPM_DIGEST_LEN],
__u8 public[SHIRUDO_TPM_NV_RSA_BYTES]);
-void nv_writer_destroy(key_serial_t ring);
-int nv_writer_read(__u8 public[SHIRUDO_TPM_NV_RSA_BYTES]);
-int nv_writer_sign(const __u8 digest[SHIRUDO_TPM_DIGEST_LEN],
- __u8 signature[SHIRUDO_TPM_NV_RSA_BYTES]);
-int nv_writer_sign_key(key_serial_t key,
- const __u8 digest[SHIRUDO_TPM_DIGEST_LEN],
- __u8 signature[SHIRUDO_TPM_NV_RSA_BYTES]);
+int nv_writer_read(__u8 seed[SHIRUDO_TPM_DIGEST_LEN],
+ __u8 public[SHIRUDO_TPM_NV_RSA_BYTES]);
#endif /* SHIRUDO_NV_WRITER_H */
diff --git a/util/paths.h b/util/paths.h
index b9fd50ba..5c02d0c7 100644
--- a/util/paths.h
+++ b/util/paths.h
@@ -72,6 +72,7 @@
#define SHIRUDO_INODE_SETXATTR_LINK_PIN SHIRUDO_BPFFS_DIR "/bpf2_inode_setxattr_link"
#define SHIRUDO_INODE_RMXATTR_LINK_PIN SHIRUDO_BPFFS_DIR "/bpf2_inode_removexattr_link"
#define SHIRUDO_KEY_PERMISSION_LINK_PIN SHIRUDO_BPFFS_DIR "/bpf2_key_permission_link"
+#define SHIRUDO_TPM_COMMAND_LINK_PIN SHIRUDO_BPFFS_DIR "/bpf2_tpm_command_link"
#define SHIRUDO_TASK_PRCTL_LINK_PIN SHIRUDO_BPFFS_DIR "/bpf2_task_prctl_link"
#define SHIRUDO_BPRM_CHECK_LINK_PIN SHIRUDO_BPFFS_DIR "/bpf2_bprm_check_security_link"
#define SHIRUDO_SEAL_FILE_OPEN_PIN SHIRUDO_BPFFS_DIR "/seal_file_open_link"
diff --git a/util/tamper.c b/util/tamper.c
index 97acd21b..a8e51f96 100644
--- a/util/tamper.c
+++ b/util/tamper.c
@@ -88,6 +88,8 @@ static const struct {
"overlay copy-up of a sealed inode" },
[SHIRUDO_TAMPER_KEXEC] = { "Kexec",
"kexec image or initramfs load" },
+ [SHIRUDO_TAMPER_TPM_EXTEND] = { "TpmExtend",
+ "extension of Shirudo's TPM NV index by another executable" },
};
const char *tamper_name(enum shirudo_tamper_point point)
diff --git a/util/tpm.c b/util/tpm.c
index a988e5c3..84152ebb 100644
--- a/util/tpm.c
+++ b/util/tpm.c
@@ -27,9 +27,9 @@
#define NV_READ_CHUNK 512u
/*
- * The env, then the file, then /dev/tpmrm0. An override is for VMs,
- * CI and a software TPM; it cannot defeat the operator-side guarantee,
- * since evidence is signed by the key the operator pinned.
+ * Before an authenticated image is active, tests can select a software
+ * TPM. Once active, require a device TCTI. The configured device path
+ * still needs protection by the deployment.
*/
int tpm_tcti(char *buf, size_t size)
{
@@ -44,6 +44,10 @@ int tpm_tcti(char *buf, size_t size)
tcti = cfg.tpm_tcti;
if (!tcti || !*tcti)
tcti = "device:/dev/tpmrm0";
+ if (shirudo_image_config_active() && strncmp(tcti, "device:", 7)) {
+ explicit_bzero(&cfg, sizeof(cfg));
+ return -EACCES;
+ }
n = strscpy(buf, tcti, size);
explicit_bzero(&cfg, sizeof(cfg)); /* scrub tpm_owner_auth */
return n < 0 ? (int)n : 0;
@@ -66,7 +70,9 @@ static int open_tpm(struct shirudo_tpm **out, bool quiet)
err = tpm_tcti(tcti, sizeof(tcti));
if (err) {
- if (!quiet)
+ if (!quiet && err == -EACCES)
+ pr_err("tpm: authenticated runtime requires a device TCTI");
+ else if (!quiet)
pr_err("tpm: the TCTI is longer than %d bytes",
SHIRUDO_TPM_TCTI_MAX - 1);
return err;
@@ -117,12 +123,14 @@ void tpm_close(struct shirudo_tpm *t)
}
int tpm_nv_set_writer(struct shirudo_tpm *t,
+ const __u8 seed[SHIRUDO_TPM_DIGEST_LEN],
const __u8 public[SHIRUDO_TPM_NV_RSA_BYTES])
{
- if (!t || !public) {
- pr_err("tpm: NV writer modulus is required");
+ if (!t || !seed || !public || !(public[0] & 0x80)) {
+ pr_err("tpm: NV writer identity is required");
return -EACCES;
}
+ memcpy(t->writer_seed, seed, sizeof(t->writer_seed));
memcpy(t->writer_public, public, sizeof(t->writer_public));
t->writer_set = true;
return 0;
@@ -469,50 +477,96 @@ int tpm_writer_prepare(struct shirudo_tpm *t, ESYS_TR session,
return err;
}
-int tpm_writer_load(struct shirudo_tpm *t,
- const __u8 public_key[SHIRUDO_TPM_NV_RSA_BYTES], ESYS_TR *key)
+/*
+ * The salt varies the owner primary each boot. The TPM's owner seed
+ * reproduces that key after a reset, while the frozen modulus refuses
+ * a replacement made under a different owner seed.
+ */
+static int tpm_writer_primary(struct shirudo_tpm *t,
+ const __u8 seed[SHIRUDO_TPM_DIGEST_LEN],
+ const __u8 expected[SHIRUDO_TPM_NV_RSA_BYTES],
+ __u8 public[SHIRUDO_TPM_NV_RSA_BYTES],
+ ESYS_TR *key)
{
__u8 public_area[SHIRUDO_TPM_NV_RSA_PUBLIC_AREA_LEN];
- __u8 expected[SHIRUDO_TPM_NAME_LEN];
- TPM2B_PUBLIC public = {};
- TPMT_PUBLIC *p = &public.publicArea;
+ __u8 computed[SHIRUDO_TPM_NAME_LEN];
+ TPML_PCR_SELECTION creation_pcr = {};
+ TPM2B_SENSITIVE_CREATE sensitive = {};
+ TPM2B_PUBLIC *out_pub = NULL;
+ struct shirudo_config cfg = {};
+ TPM2B_PUBLIC in_pub = {};
TPM2B_NAME *name = NULL;
+ TPM2B_DATA outside = {};
+ TPMT_PUBLIC *p;
TSS2_RC rc;
int err;
*key = ESYS_TR_NONE;
+ p = &in_pub.publicArea;
p->type = TPM2_ALG_RSA;
p->nameAlg = TPM2_ALG_SHA256;
- p->objectAttributes = TPMA_OBJECT_SIGN_ENCRYPT;
+ p->objectAttributes = SHIRUDO_TPM_WRITER_ATTRS;
p->parameters.rsaDetail.symmetric.algorithm = TPM2_ALG_NULL;
p->parameters.rsaDetail.scheme.scheme = TPM2_ALG_RSASSA;
p->parameters.rsaDetail.scheme.details.rsassa.hashAlg = TPM2_ALG_SHA256;
p->parameters.rsaDetail.keyBits = 3072;
p->parameters.rsaDetail.exponent = 65537;
- p->unique.rsa.size = SHIRUDO_TPM_NV_RSA_BYTES;
- memcpy(p->unique.rsa.buffer, public_key, SHIRUDO_TPM_NV_RSA_BYTES);
- rc = Esys_LoadExternal(t->esys, ESYS_TR_NONE, ESYS_TR_NONE,
- ESYS_TR_NONE, NULL, &public, ESYS_TR_RH_NULL, key);
- if (rc != TSS2_RC_SUCCESS)
- goto tss_error;
- rc = Esys_TR_GetName(t->esys, *key, &name);
- if (rc != TSS2_RC_SUCCESS || !name)
- goto tss_error;
- tpm_writer_public(public_key, public_area);
- err = tpm_object_name(public_area, sizeof(public_area), expected);
- if (err || name->size != sizeof(expected) ||
- memcmp(name->name, expected, sizeof(expected))) {
- pr_err("tpm: loaded NV writer Name differs from its public area");
+ p->unique.rsa.size = SHIRUDO_TPM_DIGEST_LEN;
+ memcpy(p->unique.rsa.buffer, seed, SHIRUDO_TPM_DIGEST_LEN);
+
+ err = config_load(&cfg);
+ if (!err)
+ err = owner_auth_set(t, cfg.tpm_owner_auth, cfg.tpm_owner_auth_len);
+ explicit_bzero(&cfg, sizeof(cfg));
+ if (err)
+ return err;
+ rc = Esys_CreatePrimary(t->esys, ESYS_TR_RH_OWNER,
+ ESYS_TR_PASSWORD, ESYS_TR_NONE, ESYS_TR_NONE,
+ &sensitive, &in_pub, &outside, &creation_pcr,
+ key, &out_pub, NULL, NULL, NULL);
+ owner_auth_clear(t);
+ if (rc != TSS2_RC_SUCCESS) {
+ pr_err("tpm: CreatePrimary(NV writer): rc=0x%08x", (__u32)rc);
+ err = rc_is_auth_failure(rc) ? -EACCES : -EIO;
+ goto cleanup;
+ }
+ if (!out_pub) {
+ err = -EPROTO;
+ goto out;
+ }
+ p = &out_pub->publicArea;
+ if (p->type != TPM2_ALG_RSA || p->nameAlg != TPM2_ALG_SHA256 ||
+ p->objectAttributes != SHIRUDO_TPM_WRITER_ATTRS ||
+ p->authPolicy.size ||
+ p->parameters.rsaDetail.symmetric.algorithm != TPM2_ALG_NULL ||
+ p->parameters.rsaDetail.scheme.scheme != TPM2_ALG_RSASSA ||
+ p->parameters.rsaDetail.scheme.details.rsassa.hashAlg != TPM2_ALG_SHA256 ||
+ p->parameters.rsaDetail.keyBits != 3072 ||
+ p->parameters.rsaDetail.exponent != 65537 ||
+ p->unique.rsa.size != SHIRUDO_TPM_NV_RSA_BYTES) {
err = -EPROTO;
goto out;
}
- err = 0;
- goto out;
-tss_error:
- pr_err("tpm: NV writer PolicySigned: rc=0x%08x", (__u32)rc);
- err = -EACCES;
+ memcpy(public, p->unique.rsa.buffer, SHIRUDO_TPM_NV_RSA_BYTES);
+ if (expected && memcmp(public, expected, SHIRUDO_TPM_NV_RSA_BYTES)) {
+ err = -EKEYREJECTED;
+ goto out;
+ }
+ tpm_writer_public(public, public_area);
+ err = tpm_object_name(public_area, sizeof(public_area), computed);
+ if (err)
+ goto out;
+ rc = Esys_TR_GetName(t->esys, *key, &name);
+ if (rc != TSS2_RC_SUCCESS || !name ||
+ name->size != sizeof(computed) ||
+ memcmp(name->name, computed, sizeof(computed)))
+ err = -EPROTO;
out:
+ if (err)
+ pr_err("tpm: NV writer public key differs from the expected key");
+cleanup:
Esys_Free(name);
+ Esys_Free(out_pub);
if (err) {
session_flush(t, *key);
*key = ESYS_TR_NONE;
@@ -520,6 +574,63 @@ out:
return err;
}
+int tpm_writer_load(struct shirudo_tpm *t,
+ const __u8 seed[SHIRUDO_TPM_DIGEST_LEN],
+ const __u8 expected_public[SHIRUDO_TPM_NV_RSA_BYTES],
+ ESYS_TR *key)
+{
+ __u8 public[SHIRUDO_TPM_NV_RSA_BYTES];
+
+ return tpm_writer_primary(t, seed, expected_public, public, key);
+}
+
+int tpm_writer_create(struct shirudo_tpm *t,
+ const __u8 seed[SHIRUDO_TPM_DIGEST_LEN],
+ __u8 public[SHIRUDO_TPM_NV_RSA_BYTES])
+{
+ ESYS_TR key = ESYS_TR_NONE;
+ int err;
+
+ err = tpm_writer_primary(t, seed, NULL, public, &key);
+ session_flush(t, key);
+ return err;
+}
+
+static int tpm_writer_sign(struct shirudo_tpm *t, ESYS_TR key,
+ const __u8 hash[SHIRUDO_TPM_DIGEST_LEN],
+ __u8 out[SHIRUDO_TPM_NV_RSA_BYTES])
+{
+ TPMT_TK_HASHCHECK validation = {
+ .tag = TPM2_ST_HASHCHECK,
+ .hierarchy = TPM2_RH_NULL,
+ };
+ TPMT_SIG_SCHEME scheme = { .scheme = TPM2_ALG_RSASSA };
+ TPMT_SIGNATURE *signature = NULL;
+ TPM2B_DIGEST digest = {
+ .size = SHIRUDO_TPM_DIGEST_LEN,
+ };
+ TSS2_RC rc;
+ int err = 0;
+
+ memcpy(digest.buffer, hash, SHIRUDO_TPM_DIGEST_LEN);
+ scheme.details.rsassa.hashAlg = TPM2_ALG_SHA256;
+ rc = Esys_Sign(t->esys, key, ESYS_TR_PASSWORD, ESYS_TR_NONE,
+ ESYS_TR_NONE, &digest, &scheme, &validation, &signature);
+ if (rc != TSS2_RC_SUCCESS) {
+ pr_err("tpm: Sign(NV writer): rc=0x%08x", (__u32)rc);
+ err = -EACCES;
+ } else if (!signature || signature->sigAlg != TPM2_ALG_RSASSA ||
+ signature->signature.rsassa.hash != TPM2_ALG_SHA256 ||
+ signature->signature.rsassa.sig.size != SHIRUDO_TPM_NV_RSA_BYTES) {
+ err = -EPROTO;
+ } else {
+ memcpy(out, signature->signature.rsassa.sig.buffer,
+ SHIRUDO_TPM_NV_RSA_BYTES);
+ }
+ Esys_Free(signature);
+ return err;
+}
+
TSS2_RC tpm_writer_authorize(struct shirudo_tpm *t, ESYS_TR key, ESYS_TR session,
const struct tpm_writer_authorization *authorization,
const __u8 signature[SHIRUDO_TPM_NV_RSA_BYTES])
@@ -546,12 +657,15 @@ static int policy_writer_signed(struct shirudo_tpm *t, ESYS_TR session,
__u32 index, const __u8 event[32])
{
struct tpm_writer_authorization authorization = {};
- __u8 signature[SHIRUDO_TPM_NV_RSA_BYTES];
+ __u8 signature[SHIRUDO_TPM_NV_RSA_BYTES] = {};
+ __u8 public[SHIRUDO_TPM_NV_RSA_BYTES];
ESYS_TR key = ESYS_TR_NONE;
struct tpm_nv_info nv;
TSS2_RC rc;
int err;
+ if (!t->writer_set)
+ return -EKEYREJECTED;
err = tpm_nv_info(t, index, &nv);
if (err)
return err;
@@ -562,19 +676,21 @@ static int policy_writer_signed(struct shirudo_tpm *t, ESYS_TR session,
err = tpm_writer_prepare(t, session, nv.name, event, &authorization);
if (err)
return err;
- err = nv_writer_sign(authorization.digest, signature);
+ err = tpm_writer_primary(t, t->writer_seed, t->writer_public, public, &key);
if (err)
return err;
- err = tpm_writer_load(t, t->writer_public, &key);
+ err = tpm_writer_sign(t, key, authorization.digest, signature);
if (err)
- return err;
+ goto out;
rc = tpm_writer_authorize(t, key, session, &authorization, signature);
- session_flush(t, key);
if (rc != TSS2_RC_SUCCESS) {
pr_err("tpm: NV writer PolicySigned: rc=0x%08x", (__u32)rc);
- return -EACCES;
+ err = -EACCES;
}
- return 0;
+out:
+ session_flush(t, key);
+ explicit_bzero(signature, sizeof(signature));
+ return err;
}
int tpm_policy_session_finish(struct shirudo_tpm *t, ESYS_TR session,
@@ -604,7 +720,7 @@ int tpm_policy_session_finish(struct shirudo_tpm *t, ESYS_TR session,
/*
* Walk one branch of the register's write policy in a real session and
- * authorize the exact NV_Extend with the kernel-held writer after PolicyOR.
+ * authorize the exact NV_Extend with the TPM-held writer after PolicyOR.
* Compare the session digest with the software policy to detect drift.
*/
static int policy_session_register(struct shirudo_tpm *t, bool init,
diff --git a/util/tpm.h b/util/tpm.h
index 7527442a..34172d52 100644
--- a/util/tpm.h
+++ b/util/tpm.h
@@ -27,7 +27,11 @@ int tpm_open(struct shirudo_tpm **out);
int tpm_open_quiet(struct shirudo_tpm **out);
void tpm_close(struct shirudo_tpm *t);
+int tpm_writer_create(struct shirudo_tpm *t,
+ const __u8 seed[SHIRUDO_TPM_DIGEST_LEN],
+ __u8 public[SHIRUDO_TPM_NV_RSA_BYTES]);
int tpm_nv_set_writer(struct shirudo_tpm *t,
+ const __u8 seed[SHIRUDO_TPM_DIGEST_LEN],
const __u8 public[SHIRUDO_TPM_NV_RSA_BYTES]);
int tpm_pcr_read(struct shirudo_tpm *t, __u32 pcr,
@@ -62,8 +66,8 @@ struct tpm_nv_info {
* the first write of a boot the init branch, which TPM2_PolicyPCR
* only satisfies while the anchoring PCR still holds the value the
* policy was computed from, every later one the written branch.
- * Both branches require the kernel-held writer's command-bound
- * signature; extending requires nv_set_writer first.
+ * Both branches require the TPM writer's command-bound signature;
+ * extending requires nv_set_writer first.
*/
int tpm_nv_info(struct shirudo_tpm *t, __u32 index,
struct tpm_nv_info *info);
diff --git a/util/tpm_internal.h b/util/tpm_internal.h
index 42060776..2739ee6d 100644
--- a/util/tpm_internal.h
+++ b/util/tpm_internal.h
@@ -11,6 +11,7 @@
struct shirudo_tpm {
TSS2_TCTI_CONTEXT *tcti;
ESYS_CONTEXT *esys;
+ __u8 writer_seed[SHIRUDO_TPM_DIGEST_LEN];
__u8 writer_public[SHIRUDO_TPM_NV_RSA_BYTES];
bool writer_set;
};
@@ -25,7 +26,9 @@ int tpm_policy_session_start(struct shirudo_tpm *t, ESYS_TR *session);
int tpm_policy_session_finish(struct shirudo_tpm *t, ESYS_TR session,
const __u8 expected[SHIRUDO_TPM_DIGEST_LEN]);
int tpm_writer_load(struct shirudo_tpm *t,
- const __u8 public[SHIRUDO_TPM_NV_RSA_BYTES], ESYS_TR *key);
+ const __u8 seed[SHIRUDO_TPM_DIGEST_LEN],
+ const __u8 expected_public[SHIRUDO_TPM_NV_RSA_BYTES],
+ ESYS_TR *key);
int tpm_writer_prepare(struct shirudo_tpm *t, ESYS_TR session,
const __u8 name[SHIRUDO_TPM_NAME_LEN],
const __u8 event[SHIRUDO_TPM_DIGEST_LEN],
diff --git a/util/tpm_wire.c b/util/tpm_wire.c
index 8e3948eb..ae8efb25 100644
--- a/util/tpm_wire.c
+++ b/util/tpm_wire.c
@@ -154,7 +154,7 @@ int tpm_policy_or(const __u8 a[32], const __u8 b[32], __u8 out[32])
}
/*
- * This fixed TPMT_PUBLIC adds a 24-byte prefix to the public-key bytes.
+ * The primary's fixed TPMT_PUBLIC adds a 24-byte prefix to the modulus.
* It excludes the outer TPM2B_PUBLIC size field.
*/
void tpm_writer_public(const __u8 modulus[SHIRUDO_TPM_NV_RSA_BYTES],
@@ -162,7 +162,7 @@ void tpm_writer_public(const __u8 modulus[SHIRUDO_TPM_NV_RSA_BYTES],
{
tpm_put16(out, SHIRUDO_TPM_ALG_RSA); /* type */
tpm_put16(out + 2, SHIRUDO_TPM_ALG_SHA256); /* nameAlg */
- tpm_put32(out + 4, 0x00040000); /* TPMA_OBJECT_SIGN_ENCRYPT */
+ tpm_put32(out + 4, SHIRUDO_TPM_WRITER_ATTRS);
tpm_put16(out + 8, 0); /* Empty authPolicy: size only. */
tpm_put16(out + 10, SHIRUDO_TPM_ALG_NULL); /* symmetric.algorithm */
tpm_put16(out + 12, SHIRUDO_TPM_ALG_RSASSA); /* scheme.scheme */
diff --git a/util/tpm_wire.h b/util/tpm_wire.h
index d1e1e13e..0039118a 100644
--- a/util/tpm_wire.h
+++ b/util/tpm_wire.h
@@ -55,8 +55,8 @@ _Static_assert(SHIRUDO_TPM_NAME_LEN == 2 + SHIRUDO_TPM_DIGEST_LEN,
#define SHIRUDO_TPM_GENERATED 0xFF544347u
#define SHIRUDO_TPM_PCR_SELECT_LEN 3
#define SHIRUDO_TPM_PCR_SELECTION_LEN (7 + SHIRUDO_TPM_PCR_SELECT_LEN)
-#define SHIRUDO_TPM_CC_NV_EXTEND 0x00000136u
#define SHIRUDO_TPM_NV_POLICY_REF "shirudo-nv-writer-v1"
+#define SHIRUDO_TPM_WRITER_ATTRS 0x00040072u
void tpm_writer_public(const __u8 modulus[SHIRUDO_TPM_NV_RSA_BYTES],
__u8 out[SHIRUDO_TPM_NV_RSA_PUBLIC_AREA_LEN]);