vTPM: a TPM command that takes swtpm longer than 100 ms fails the guest's TPM permanently (tpm_recv: error -5)

オープン
#9,053 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
64/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
rust
領域
backend

調査の方向性

Start in tpm/src/emulator.rs, especially prepare_data_fd and Emulator::deliver_request, then check the response handling in devices/src/tpm.rs. Read the receive timeout and current recvfrom logic first. Implement complete response reads with a bounded wait and validate the declared size; done when delayed and partial swtpm responses are handled without leaving a stale response for the next command.

索引モデルが issue の本文から書いたものです。

説明

Describe the bug

Emulator::deliver_request reads each TPM response from swtpm with a single
recvfrom on the data socket, which prepare_data_fd gives a 100 ms
SO_RCVTIMEO:

If swtpm needs more than 100 ms to answer a command (key generation such as
TPM2_CreatePrimary on a loaded host does), recvfrom returns EAGAIN and
deliver_request fails. The CRB device then sets CTRL_STS.tpmSts:

The guest's tpm_crb driver returns -EIO when that bit is set, and nothing
clears it, so the TPM is unusable for the rest of the guest's life. Two
further effects:

  1. swtpm still finishes the command and writes its response, which stays
    queued on the socket and would be read as the response to the next
    command.
  2. On a SOCK_STREAM socket a single recvfrom can return part of a
    response, which is treated as complete.

Nothing is logged by Cloud Hypervisor (the failure is folded into
request_completed(status)), and swtpm logs nothing because from its side
the command succeeded.

The same code is on main as of 2026-10-11.

Impact we observed

Unattended Linux installs that seal a LUKS volume to the vTPM (Kairos, with
encrypted_partitions) failed in 5 of about 28 runs on one host, more often
when several guests installed at once. Guest kernel log at the failure,
0.2 to 0.4 s after the installer first opened the TPM:

kernel: tpm tpm0: tpm_transmit: tpm_recv: error -5
kernel: tpm tpm0: tpm_transmit: tpm_recv: error -5
systemd-udevd: ERROR:tcti:src/tss2-tcti/tcti-device.c:489:Tss2_Tcti_Device_Init() Failed to read response header fd 51, got errno 5: I/O error
(udev-worker): tpmrm0: Failed to open device node '/dev/tpmrm0': State not recoverable

To Reproduce

The fault becomes deterministic when swtpm is slowed down. With swtpm
running as a systemd unit, cap its CPU before the guest boots:

swtpm socket --tpm2 --tpmstate dir=/var/lib/vtpm --ctrl type=unixio,path=/run/vtpm/swtpm.sock
# as a unit, then:
systemctl set-property --runtime <swtpm unit> CPUQuota=3%

cloud-hypervisor --api-socket path=/run/vm/api.sock ...
# VM config with: "tpm": {"socket": "/run/vtpm/swtpm.sock"}

With swtpm at 3 % CPU, the Linux guest's boot failed already at the
kernel's TPM probe:

kernel: tpm tpm0: tpm_transmit: tpm_recv: error -5
kernel: tpm tpm0: tpm_transmit: tpm_recv: error -5
kernel: tpm_crb_acpi MSFT0101:00: probe with driver tpm_crb_acpi failed with error -5
systemd: Timed out waiting for device /dev/tpmrm0.

Without the throttle the failure is intermittent and load-dependent, as
above.

Expected behaviour

A slow command completes slowly. The kernel allows TPM2 commands up to
TPM2_DURATION_LONG_LONG (300 s); swtpm taking a few hundred milliseconds
should not fail the TPM. QEMU's swtpm backend reads the response header and
then the declared length, without a short per-read timeout.

Suggested fix

In deliver_request, read the 10-byte response header, then exactly the
responseSize it declares, looping over short reads; treat
EAGAIN/EINTR from the 100 ms socket timeout as "keep waiting" until an
overall deadline (we used 120 s), and reject a declared size smaller than a
header or larger than the buffer.

We tested this change on v53.0. Under the same 3 % CPU throttle that failed
the boot, the guest's TPM works and the encrypted install completes in
normal time, and 10 of 10 unthrottled installs (6 concurrent) succeeded. We
have the patch with unit tests (a response split across a delay longer than
the socket timeout, the deadline, an impossible size, swtpm closing
mid-response) and can share it.

Version

Output of cloud-hypervisor --version: cloud-hypervisor v53.0

Did you build from source: no, the cloud-hypervisor-static v53.0 release asset.

VM configuration

VMs created through the REST API (vm.create, vm.boot) with
"tpm": {"socket": "<swtpm ctrl socket>"}, 2 vCPUs, 4 GiB, firmware
CLOUDHV.fd (edk2 ch-97eeb7b09), virtio-blk disks, one virtio-net
interface on a tap.

Guest OS version details: Kairos Hadron v0.4.0 (core), installer and
installed system; tpm_crb driver.

Host OS version details: Debian 13 (trixie), kernel 6.12.100+deb13-amd64,
2× Intel Xeon E5-2630 v3 (32 threads); swtpm 0.7.1, libtpms 0.9.2.
swtpm started as
swtpm socket --tpm2 --tpmstate dir=<dir> --ctrl type=unixio,path=<socket>.

Logs

Output of cloud-hypervisor -v: nothing is logged for the failed command
(see above).

Linux kernel output: as quoted above.

主要言語
Rust
スター
6.3k
フォーク
786
平均マージ
1日 3時間
マージ済み PR(30日)
195

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

cloud-hypervisor/cloud-hypervisor のほかの issue

cloud-hypervisor/cloud-hypervisor の issue をすべて見る

似ている issue

Rust の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。