eBPF-based security platform
chain: refuse a TPM other than the one init bound
Daniel Borkmann02a82fd4ff09
diff --git a/Documentation/architecture.rst b/Documentation/architecture.rstindex 7538bb7c..2f6a625d 100644--- a/Documentation/architecture.rst+++ b/Documentation/architecture.rst@@ -293,8 +293,10 @@ serial for this singleton; a nonzero serial must match the frozen record.A certificate in another caller keyring cannot authorize a load.bpffs and keyring guards. The identity map contains the bpffs and run-device-numbers, runtime root hash, BPF keyring kind and serial, and enrolled key-serials. It is written and frozen before the admission gates attach.+numbers, runtime root hash, BPF keyring kind and serial, enrolled key+serials, the NV writer's public key and key serials, and the name of the+TPM's attestation key, which every later TPM handle is compared against.+It is written and frozen before the admission gates attach.The task map is seeded with the authenticated bootstrap task, then frozenagainst userspace writes. Runtime exec and fork hooks maintain its marks.Expected config roots remain writable only by trusted tasks preparing adiff --git a/Documentation/threat-model.rst b/Documentation/threat-model.rstindex 72c41f4f..d55173f7 100644--- a/Documentation/threat-model.rst+++ b/Documentation/threat-model.rst@@ -254,7 +254,14 @@ T5 Silent state changeis not the one ``init`` recorded, which also serialises concurrentoperators. ``shirudo status`` replays this boot's records and flagsa register it cannot explain. There is no unmeasured mode: a target- without a TPM 2.0 does not initialise. An init run from the initrd+ without a TPM 2.0 does not initialise. The TPM behind the device+ path is root's to replace in a mount namespace of its own, and an+ emulator there can carry an index of the recorded name, policy and+ reset count, since the init record is readable; every TPM handle a+ command takes first recreates the attestation key and compares its+ name with the one ``init`` froze in the identity map, which takes+ the endorsement seed no emulator has, and refuses another TPM+ before the register is read or written. An init run from the initrdleaves its record under ``/run``, labelled, until the root's runappends it; a record forged or altered there fails the replay likeany other. The init record carriesdiff --git a/Documentation/tpm.rst b/Documentation/tpm.rstindex e0e596df..675d8677 100644--- a/Documentation/tpm.rst+++ b/Documentation/tpm.rst@@ -92,7 +92,8 @@ the mapping locks remain active until process exit. Bootstrap createsthe signer in its lasting keyring with owner view/search permissions,then restricts and seals the ring. Bootstrap is trusted: productionruns in the measured initrd before untrusted workloads. The public key,-private-key ID and keyring ID are frozen in the identity map before attachment.+private-key ID and keyring ID are frozen in the identity map before+attachment, with the name of the attestation key.Failed writer creation or enforcement setup removes the writer's linksfrom the user and session keyrings.There is no additional state key or mutable binding-attempt marker.@@ -229,8 +230,12 @@ The TCTI is ``SHIRUDO_TPM_TCTI`` in the environment, else ``tpm.tcti``in ``shirudo.conf``, else ``device:/dev/tpmrm0``, the kernel resourcemanager. Initialization requires the trusted local TPM channeldescribed above. Register writes send command-bound signatures, not-the writer's private key. A substitute TPM cannot forge evidence signed by the-operator's pinned attestation key. The selected transport+the writer's private key. The device behind the path is root's to+replace, so every handle a command takes first recreates the+attestation key and compares its name with the one ``init`` froze in+the identity map; a substitute TPM is refused before the register is+read or written, and cannot forge evidence signed by the operator's+pinned attestation key either. The selected transportimplementation remains part of shirudo's trusted process code.Migration requires an updated target and initrd, adiff --git a/bpf/lib/shirudo_identity.h b/bpf/lib/shirudo_identity.hindex 44a37974..3479fed3 100644--- a/bpf/lib/shirudo_identity.h+++ b/bpf/lib/shirudo_identity.h@@ -20,6 +20,8 @@ struct shirudo_sb_id {__s32 nv_writer_ring;__s32 nv_writer_key;__u8 nv_writer_public[SHIRUDO_TPM_NV_RSA_BYTES];+ __u8 ak_name[SHIRUDO_TPM_NAME_LEN];+ __u32 ak_name_len;};struct {diff --git a/builtin-init.c b/builtin-init.cindex 7e56a71b..e3652467 100644--- a/builtin-init.c+++ b/builtin-init.c@@ -588,6 +588,31 @@ static int freeze_task_maps(struct bpf_object *obj)return rc;}+/*+ * The register is only as good as the TPM that holds it, and the device+ * the TCTI opens is root's to replace: every register write recreates the+ * attestation key, whose name no other TPM produces, and compares it with+ * the one recorded here (util/chain.c).+ */+static int record_attestation_key(struct shirudo_identity *id)+{+ struct shirudo_tpm_key ak = {};+ struct shirudo_tpm *tpm;+ int rc;++ rc = tpm_open(&tpm);+ if (rc)+ return rc;+ rc = tpm_ak_create(tpm, &ak);+ if (!rc) {+ memcpy(id->ak_name, ak.name, ak.name_len);+ id->ak_name_len = (__u32)ak.name_len;+ tpm_key_flush(tpm, &ak);+ }+ tpm_close(tpm);+ return rc;+}+static int init_lsm(void){struct bpf_link *image_links[SHIRUDO_IMAGE_OBSERVER_HOOK_COUNT] = {};@@ -711,6 +736,12 @@ static int init_lsm(void)strerror(-err));goto out;}+ err = record_attestation_key(&id);+ if (err) {+ pr_err("init: record the attestation key name: %s (tpm_error)",+ strerror(-err));+ goto out;+ }m_sb = bpf_object__find_map_by_name(obj, "shirudo_bpffs_sb");if (!m_sb) {diff --git a/include/shirudo/tpm.h b/include/shirudo/tpm.hindex 3980d6ce..03bdb237 100644--- a/include/shirudo/tpm.h+++ b/include/shirudo/tpm.h@@ -4,6 +4,7 @@#define SHIRUDO_TPM_TYPES_H#define SHIRUDO_TPM_NV_RSA_BYTES 384+#define SHIRUDO_TPM_NAME_LEN 34 /* nameAlg and a SHA-256 */#define SHIRUDO_TPM_NV_RSA_PUBLIC_AREA_LEN (24 + SHIRUDO_TPM_NV_RSA_BYTES)#endif /* SHIRUDO_TPM_TYPES_H */diff --git a/testing/tamperproof/t1131-pr-set-mm-identity.py b/testing/tamperproof/t1131-pr-set-mm-identity.pyindex e84a684b..9298839c 100644--- a/testing/tamperproof/t1131-pr-set-mm-identity.py+++ b/testing/tamperproof/t1131-pr-set-mm-identity.py@@ -77,7 +77,9 @@ class ShirudoIdentity(ctypes.Structure):("runtime_root", ctypes.c_uint8 * 32),("nv_writer_ring", ctypes.c_int32),("nv_writer_key", ctypes.c_int32),- ("nv_writer_public", ctypes.c_uint8 * 384)]+ ("nv_writer_public", ctypes.c_uint8 * 384),+ ("ak_name", ctypes.c_uint8 * 34),+ ("ak_name_len", ctypes.c_uint32)]def assert_frozen_runtime():diff --git a/util/chain.c b/util/chain.cindex 5e2dc3e0..8370b369 100644--- a/util/chain.c+++ b/util/chain.c@@ -21,6 +21,7 @@#include "chain.h"#include "config.h"#include "crypto.h"+#include "identity.h"#include "initrd.h"#include "log.h"#include "malloc.h"@@ -252,6 +253,43 @@ static int register_writer(struct shirudo_tpm *tpm,return rc ? rc : tpm_nv_set_writer(tpm, 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.+ */+int chain_tpm_bound(struct shirudo_tpm *tpm)+{+ struct shirudo_identity id = {};+ struct shirudo_tpm_key ak = {};+ int rc;++ rc = shirudo_enforcement_active();+ if (rc <= 0)+ return rc;+ rc = identity_read(&id);+ if (rc) {+ pr_err("tpm: frozen identity unreadable: %s", strerror(-rc));+ return rc;+ }+ if (id.ak_name_len != SHIRUDO_TPM_NAME_LEN) {+ pr_err("tpm: no attestation key name on record");+ return -EPROTO;+ }+ rc = tpm_ak_create(tpm, &ak);+ if (rc)+ return rc;+ if (ak.name_len != SHIRUDO_TPM_NAME_LEN ||+ !ct_eq(ak.name, id.ak_name, SHIRUDO_TPM_NAME_LEN)) {+ pr_err("tpm: not the TPM init bound (attestation key name "+ "differs)");+ rc = -EACCES;+ }+ tpm_key_flush(tpm, &ak);+ return rc;+}+int chain_init(const __u8 event[32],struct shirudo_chain_init_info *info){@@ -267,6 +305,9 @@ int chain_init(const __u8 event[32],rc = tpm_open(&tpm);if (rc)return -ENODEV;+ rc = chain_tpm_bound(tpm);+ if (rc)+ goto out;rc = tpm_reset_count(tpm, &info->reset_count);if (rc)goto out;@@ -423,6 +464,9 @@ int chain_extend(const __u8 event[32], __u8 out_chain[32])rc = tpm_open(&tpm);if (rc)return -ENODEV;+ rc = chain_tpm_bound(tpm);+ if (rc)+ goto out;rc = tpm_nv_info(tpm, SHIRUDO_NV_INDEX, &nv);if (rc)goto out;diff --git a/util/chain.h b/util/chain.hindex 1b298339..4ef6a10f 100644--- a/util/chain.h+++ b/util/chain.h@@ -108,6 +108,15 @@ int chain_extend(const __u8 event[SHIRUDO_CHAIN_DIGEST_LEN],/* The live value; -ENODEV when no register is written this boot. */int chain_read(__u8 out[SHIRUDO_CHAIN_DIGEST_LEN]);+struct shirudo_tpm;++/*+ * Whether the TPM behind an open handle is the one init recorded, by+ * the name of its attestation key: 0 when it is or enforcement is not+ * live, -EACCES for another TPM, -EPROTO without a record.+ */+int chain_tpm_bound(struct shirudo_tpm *tpm);+/* For a probe: a TPM that does not answer is not logged. */bool chain_bound(void);diff --git a/util/identity.h b/util/identity.hindex 5669ad5b..be092101 100644--- a/util/identity.h+++ b/util/identity.h@@ -29,6 +29,8 @@ struct shirudo_identity {__s32 nv_writer_ring;__s32 nv_writer_key;__u8 nv_writer_public[SHIRUDO_TPM_NV_RSA_BYTES];+ __u8 ak_name[SHIRUDO_TPM_NAME_LEN];+ __u32 ak_name_len;};#define SHIRUDO_TASK_TRUSTED 1@@ -65,7 +67,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) == 508,+_Static_assert(sizeof(struct shirudo_identity) == 548,"frozen identity ABI");/* The bpffs mount init made, which the unmount guard keeps mounted. */diff --git a/util/image_runtime.c b/util/image_runtime.cindex 67c93623..0415616f 100644--- a/util/image_runtime.c+++ b/util/image_runtime.c@@ -24,6 +24,7 @@#include <shirudo/string.h>#include "../builtin.h"+#include "chain.h"#include "config.h"#include "config_image.h"#include "config_update.h"@@ -663,6 +664,8 @@ static int open_update(void)rc = mkdir_owned(SHIRUDO_CONFIG_UPDATE_DIR);if (!rc)rc = tpm_open(&image.tpm);+ if (!rc)+ rc = chain_tpm_bound(image.tpm);if (!rc)rc = shirudo_config_update_open(SHIRUDO_CONFIG_UPDATE_DIR,&update_ops, &image,diff --git a/util/tpm_wire.h b/util/tpm_wire.hindex aaa6600f..a39cde1f 100644--- a/util/tpm_wire.h+++ b/util/tpm_wire.h@@ -39,7 +39,8 @@ static inline size_t tpm_put2b(__u8 *p, const __u8 *data, size_t len)}#define SHIRUDO_TPM_DIGEST_LEN 32-#define SHIRUDO_TPM_NAME_LEN (2 + SHIRUDO_TPM_DIGEST_LEN)+_Static_assert(SHIRUDO_TPM_NAME_LEN == 2 + SHIRUDO_TPM_DIGEST_LEN,+ "a name is the algorithm and its digest");#define SHIRUDO_TPM_ALG_RSA 0x0001u#define SHIRUDO_TPM_ALG_AES 0x0006u#define SHIRUDO_TPM_ALG_SHA256 0x000Bu