Skip to content

feat(tpm): a passkey-sealed TPM 2.0 for the guest (kwtpm) - #106

Open
vakharwalad23 wants to merge 13 commits into
pithings:mainfrom
vakharwalad23:feat/kwtpm
Open

vakharwalad23 wants to merge 13 commits into
pithings:mainfrom
vakharwalad23:feat/kwtpm

Conversation

@vakharwalad23

Copy link
Copy Markdown

Summary

The guest gets a stock TPM 2.0 (/dev/tpm0, /dev/tpmrm0) through a new
paravirtual chip driver, kwtpm, whose engine is libtpms (TPM 2.0 revision
1.83) compiled to wasm and run by the SDK in a worker of its own, outside
kernel memory. The TPM's state can persist, encrypted under a random data
key that a key file (Node), a recovery code or a passkey's WebAuthn PRF
output (browsers) unwraps. Stock tpm2-tools, systemd-cryptenroll --tpm2-device=auto (LUKS), tpm2-pkcs11 (SSH, git signing) and
tpm2_quote with measured boot work unchanged. Nothing is fetched or
started without the tpm option; the kernel grows by 39 KB lowered (14 KB
gzip).

It is a software TPM sealed by hardware, not a hardware TPM; the README
says what it protects and what it does not.

New files carry the contributor's copyright line (existing files keep
yours, untouched), and the two kernel patches (kwtpm, the dm-crypt park
point) are authored by the contributor without a Signed-off-by. Happy
to switch the headers to the project's single copyright line, or for
you to take the kernel patches' authorship as with #59, if you prefer.

Changes

  • build(tpm): tpm/build.sh builds libtpms 0.10.2 on OpenSSL 3.5.9's
    libcrypto with zig cc into kwtpm.wasm (1.11 MB, 386 KB brotli; three
    small WASI patches), js/core/tpm-engine.ts runs it.
  • feat(sdk): createKernel({ tpm }), vm.tpm, TpmDevice / TpmHost,
    the engine worker, stores (fileTpmStore, idbTpmStore), sealers
    (keySealer, recoverySealer, passkeySealer), durable NV before the
    response, snapshot and restore (sealed volatile state, the disk-style
    generation rule), worker mode, measured boot (PCRs 4, 8, 9).
  • feat(kernel): wasm: kwtpm, a TPM 2.0 chip backed by the JavaScript host (TPM_CHIP_FLAG_SYNC, IRQ 15, late registration, CPU 0 hand-off on
    SMP), and dm-crypt for LUKS with a park point for dmcrypt_write (a
    generic patch; +192 KB lowered, 69 KB gzip).
  • feat(cli): --tpm[=fake], --tpm-state, --tpm-key,
    --tpm-measure, --tpm-event-log.
  • feat(guest): kwhotplug gives /dev/tpmrm0 mode 0666 when the chip
    registers.
  • feat(web): Settings > TPM, the #tb-tpm applet, passkey unlock,
    recovery code, export, import, clear, ?tpm=0, and resume of a
    suspended session with one click.
  • build(release): the release builds and attaches kwtpm.wasm;
    THIRD-PARTY.md lists libtpms, OpenSSL and wasi-libc.
  • docs(tpm): README section, .agents/kernel/devices.md kwtpm,
    js.md, config.md sizes, parking.md, testing.md.

Testing

  • pnpm typecheck, pnpm typecheck:web, pnpm test, pnpm test:api,
    pnpm test:usb, pnpm test:bluetooth, pnpm test:serial, pnpm test:camera, kwtest on 2 CPUs, web/test/open.test.ts: green.
  • pnpm test:tpm: host unit tests, the engine, and test/tpm/tpmtest.c
    in the guest on FakeTpmBackend and libtpms, UP inline and 2 CPUs in
    worker mode, a sealed state across boots (and a wrong key refused), a
    snapshot keeping PCRs and an RM-held object, a stale restore refused.
  • node test/tpm/tools-check.mjs (and --worker --cpus 2): tpm2-tools on
    Alpine (getcap, getrandom, ECC/RSA primaries, create, load, unseal), as
    the image's user, and a measured boot quote verified by
    test/tpm/verify-quote.mjs.
  • node test/tpm/luks-check.mjs: Debian, systemd-cryptenroll --tpm2-device=auto --tpm2-pcrs=4+8+9, unlock without a passphrase in
    the next boot, a snapshot with the mapping open, refusal under another
    command line.
  • node test/tpm/ssh-check.mjs: Debian, tpm2-pkcs11 key, ssh login, git commit -S and git verify-commit.
  • node test/tpm/page-check.mjs (and --inline): headless Chromium with
    a virtual passkey (PRF): locked at boot, enrolment, guest TPM command,
    sealed record, reload unlock, suspend and one-click resume, ?tpm=0.
  • A real passkey, by hand: Dia (Chromium-based) on macOS with an iCloud
    Keychain passkey (Touch ID), the page from pnpm dev with ?tpm=1
    (Debian, 4 CPUs in worker mode, data disk in OPFS): Unlock made the
    passkey and showed the recovery code, the chip registered after the
    unlock (the boot did not wait), /dev/tpmrm0 answered a raw
    TPM2_GetRandom as the image's user, after a reload the same passkey
    opened the stored record again (macOS's Touch ID sheet), and a
    suspended session resumed with its TPM after Unlock and resume on the
    Resume card (one passkey prompt). The same flow also worked with a
    Google Password Manager passkey.
  • Not run here: the release workflow (maintainer's CI), other
    authenticators (YubiKey and other security keys, Windows Hello), Safari
    and Firefox.

The TPM engine for the guest's upcoming TPM 2.0 (kwtpm): libtpms 0.10.2
(TPM 2.0 revision 1.83, the engine behind swtpm) on OpenSSL 3.5's
libcrypto, compiled by zig cc to one wasm32-wasi reactor, kwtpm.wasm
(1.11 MB, 386 KB brotli). libtpms was chosen over the TCG reference code
for its state: versioned, backward-compatible permanent and volatile
blobs with an API to take and restore them, which persistence and
snapshots need; the reference code's NV is a raw memory image.

tpm/build.sh fetches the pinned sources (tpm/pins.sh), applies three
small libtpms patches for WASI (byte order, system headers, and hash
method signatures that a wasm call_indirect checks), builds a libcrypto
without the algorithms libtpms never calls, and links tpm/kwtpm.c, the
glue that keeps the NV in memory and exports commands and state.
setup.sh builds it as an optional step and pnpm build ships it as
dist/kwtpm.wasm when it exists.

js/core/tpm-engine.ts runs the module with its own small WASI shim
(random_get and clock_time_get are the only real imports), and
test/tpm-engine.test.ts drives it with raw TPM 2.0 commands, a sealed
object round trip and a state round trip.
createKernel({ tpm }) serves the kernel's kwtpm chip driver (next
commit) with libtpms (kwtpm.wasm) in a worker of its own, so the guest
gets /dev/tpm0 and /dev/tpmrm0 with the TPM's secrets outside kernel
memory. Without the option nothing is fetched or started, and the
kernel registers nothing.

The two halves follow kwbt's: TpmDevice is the kernel's end (four
imports that copy during the call, IRQ 15), TpmHost the owner's end and
vm.tpm. The engine never runs on a kernel thread, since an RSA key takes
50 to 600 ms in wasm, and a response that changed the permanent state
waits until the store has it, so a closed tab never loses an NV write
the guest saw succeed. In worker mode the engine and the record stay on
the owner and the commands cross as "tpm" batches.

State persists in a TpmStore (fileTpmStore on Node, idbTpmStore in
browsers, held by one machine at a time) as a record: the permanent
state under AES-256-GCM with a random data key, which each slot wraps
under a key its sealer derives: keySealer (a key file),
recoverySealer (a printed code) or passkeySealer (WebAuthn's PRF
extension, browsers). Losing one way in does not lose the TPM. A store
without a sealer is refused unless insecure is set, which says so at
every start.

A snapshot keeps the engine's volatile state (PCRs, loaded objects,
sessions) sealed under the data key, with the record's uid and
generation; a restore loads the engine before the kernel resumes and,
as with a disk's write generation, refuses a record that moved on since.
The TPM's part joins the promises a snapshot already awaits for its
disks. With measure, the host extends PCRs 4, 8 and 9 with the kernel,
the command line, the initramfs and the distro before the guest sees
the TPM, and vm.tpm.eventLog() lists what it measured.

test/tpm-host.test.ts covers the record, the sealers and the ordering
without a kernel; test/tpm.test.ts runs test/tpm/tpmtest.c in the guest
on FakeTpmBackend and on libtpms, UP and SMP in a worker, across a
restart with a key and across a snapshot.
A new patch, "wasm: kwtpm, a TPM 2.0 chip backed by the JavaScript
host" (drivers/char/tpm/kwtpm.c, TCG_KWTPM), placed after kwusb, with
WASM_IRQ_TPM (15) in the arch patch and TCG_TPM, TCG_KWTPM in
kw_defconfig. The kernel's TPM core provides /dev/tpm0 and the resource
manager's /dev/tpmrm0, so stock tpm2-tools and tpm2-tss need no
tpm2-abrmd.

The driver is modelled on tpm_svsm (TPM_CHIP_FLAG_SYNC, only .send):
send() leaves the command in a buffer of its own and sleeps until the
host raises IRQ 15 with its response, because commands may take long on
the host and no import may suspend. It registers the chip from a work
item once the host reports its engine ready, so the boot never waits for
the engine or for a passkey. On SMP a send on another CPU raises the
IRQ on CPU 0, whose handler talks to the host; no work item is in that
path, so the TPM core's non-blocking writes from tpm_dev_wq, a
WQ_MEM_RECLAIM workqueue, do not wait on system_percpu_wq.

No bus encryption (TCG_TPM2_HMAC: the bus is the host's), no event log
(SECURITYFS) and no hwrng (HW_RANDOM is off). +39 KB lowered, +14 KB
gzip -n -9, four new imports (wasm_driver_tpm_*).
--tpm gives the guest an ephemeral TPM 2.0 (kwtpm). --tpm-state FILE
keeps its state between runs, sealed with the key in --tpm-key FILE
(made with 32 random bytes, mode 0600, when it does not exist) or
UNKERNEL_TPM_KEY (hex), so a copied state file is useless without the
key; --tpm-key none stores it unencrypted for development and is said
so at every start. --tpm=fake answers canned responses (tests).
--tpm-measure extends PCRs with what booted, and --tpm-event-log FILE
writes the measurements once the TPM is ready, for a remote verifier.
test/tpm/tools-check.mjs boots Alpine with a TPM, installs tpm2-tools
and checks properties-fixed, getrandom, ECC and RSA primaries, a sealed
secret's create, load and unseal through /dev/tpmrm0, inline and in
worker mode on several CPUs. Inline it also checks measured boot: PCRs
4, 8 and 9 against the host's event log, and a quote by an attestation
key (tpm2_createek, tpm2_createak, tpm2_quote) with a fresh nonce.

test/tpm/verify-quote.mjs is that quote's verifier, usable on its own:
the signature with the attestation key's public key, the nonce, and the
PCR digest against the event log replayed. A tampered log or a wrong
nonce must fail.
kwtpm's chip registers once the host's engine runs, after rc has set
the modes of the boot devices, and devtmpfs makes /dev/tpmrm0 root's
with mode 0600. kwhotplug now gives it 0666 on its uevent (and in its
scan), as it does for usbfs nodes, so tpm2-tss programs work as the
images' user, who is not in tss. /dev/tpm0, the raw device without the
resource manager, stays root's.

tools-check runs tpm2_getrandom as the image's user to cover it.
LUKS volumes need the device mapper and dm-crypt, which were off:
kw_defconfig turns on MD (without MD's RAID), BLK_DEV_DM, DM_CRYPT and
the AES and XTS ciphers LUKS2 uses by default. With kwtpm this lets
stock cryptsetup and systemd-cryptenroll --tpm2-device=auto bind a
volume to the TPM. +192 KB lowered, +69 KB gzip -n -9, no new imports.

dm-crypt starts a kernel thread per mapping, dmcrypt_write, whose loop
had no park point, so no snapshot could complete while a mapping
existed (the page suspends with snapshots). A new generic patch, "dm
crypt: add a park point to dmcrypt_write", parks it right after it
wakes, where it holds nothing; placed after the other park points.
The kernel keyring stays off: cryptsetup passes the volume key in the
dm table when the kernel has none.
test/tpm/luks-check.mjs runs the stock flow on Debian with a persistent
sealed TPM and measured boot: cryptsetup luksFormat a disk,
systemd-cryptenroll --tpm2-device=auto --tpm2-pcrs=4+8+9, and
systemd-cryptsetup attach with no passphrase, in that boot and the next
one. A snapshot completes while the mapping is open, and a boot with
another kernel command line (PCR 8) cannot unseal the key.
test/tpm/ssh-check.mjs makes a P-256 key in the TPM with Debian's
tpm2-pkcs11 (tpm2_ptool) as the image's user, loads it into ssh-agent
through the PKCS#11 module, logs in to an sshd that knows only its
public half, and signs a commit with it (gpg.format ssh), which git
verify-commit accepts. The private key never leaves the TPM.
Settings > TPM turns on a TPM 2.0 for the guest (kwtpm), kept in
IndexedDB kwtpm:main and sealed by a passkey through WebAuthn's PRF
extension, so the stored state is useless without it. Off by default;
?tpm=0 turns it off for one load.

The TPM waits locked until Unlock (Settings, or the new #tb-tpm applet
in the top bar, amber while locked), which calls vm.tpm.unlock() inside
the click, since WebAuthn needs the gesture. The first unlock makes the
passkey and then a recovery code, shown once, as a second way in; more
passkeys can be added. Unlock with the recovery code reboots with it
for one load. Export downloads the encrypted record; Import and Clear
act while no TPM holds the record's lock, or across a reboot.

A session suspended with a TPM resumes only after the passkey opens the
record again: the Resume card asks, and its click prefetches the PRF
output for the snapshot's record, so the restore needs no second
prompt. A session whose TPM record was written, replaced or cleared
since is discarded, as one with a changed data disk is.

kwtpm.wasm reaches the page as the Vulkan translator does (KW_TPM, the
local build, or the release's page.json); without it the option is
disabled and says why.
test/tpm/page-check.mjs drives the page with a virtual passkey from
DevTools' WebAuthn domain (user verification, the PRF extension): the
TPM waits locked at boot; the applet's Unlock makes the passkey, the
guest's /dev/tpmrm0 answers as the image's user and the record in
IndexedDB is sealed with a passkey and a recovery-code slot; a reload
opens the same record with the passkey; a suspended session resumes
with its TPM after one click on the Resume card; ?tpm=0 has no TPM. In
worker mode and with --inline.
The release builds kwtpm.wasm (tpm/build.sh) next to the Vulkan
translator, checks that the package carries it, and attaches it with
the page's files, so a page built from a release (web/scripts/kernel.ts
reads page.json) offers the TPM too. THIRD-PARTY.md lists what the
engine is made of: libtpms (BSD-3-Clause and the TCG's BSD-style
license), OpenSSL's libcrypto (Apache-2.0) and Zig's wasi-libc.
README: the TPM in the device list, a CLI line, an SDK section with the
sealers, measured boot and, in plain words, what the TPM protects and
what it does not (a software TPM sealed by hardware, not a hardware
TPM), and the engine in the package's licences.

.agents: a kwtpm section in kernel/devices.md (imports, the sync send
with its own buffers, registration, SMP, snapshots, config, dm-crypt,
checks) and a TPM section in js.md (the halves, the engine worker, the
record and its sealers, the passkey, snapshots); the IRQ, import,
driver and patch lists in kernel.md, dm-crypt's park point in
kernel/parking.md, both size measurements in kernel/config.md, and the
quick check row in testing.md.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant