git: fbb1b886cbd0 - main - bhyve: Reinitialise swtpm's TPM at startup
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Thu, 26 Nov 2026 16:08:39 UTC
The branch main has been updated by kevans:
URL: https://cgit.FreeBSD.org/src/commit/?id=fbb1b886cbd0bdf5a67db45689bc77fa9faff23a
commit fbb1b886cbd0bdf5a67db45689bc77fa9faff23a
Author: Quentin Thébault <quentin.thebault@defenso.fr>
AuthorDate: 2026-11-26 16:07:05 +0000
Commit: Kyle Evans <kevans@FreeBSD.org>
CommitDate: 2026-11-26 16:08:16 +0000
bhyve: Reinitialise swtpm's TPM at startup
bhyve does not currently reinitialise swtpm on startup, which results in
PCRs being carried over across reboots. This means that any guest VM
implementing measured boot controls, such as Windows with BitLocker and
Secure Boot enabled, will consider the system compromised and refuse to
start or ask for a recovery key.
swtpm has a control channel that allows us to send commands for that
purpose: CMD_INIT shuts the TPM down and starts it again clearing the
PCRs in the process, like a cold boot on real hardware.
Connect a second socket when the new ctrl_path option is set and send
CMD_INIT there. Without the option nothing changes.
MFC after: 2 weeks
Sponsored by: Defenso
Signed-off-by: Quentin Thébault <quentin.thebault@defenso.fr>
Reviewed by: kevans, novel
Differential Revision: https://reviews.freebsd.org/D59322
---
usr.sbin/bhyve/bhyve.8 | 18 ++++++
usr.sbin/bhyve/bhyve_config.5 | 8 ++-
usr.sbin/bhyve/tpm_emul_swtpm.c | 133 +++++++++++++++++++++++++++++++++++++---
3 files changed, 151 insertions(+), 8 deletions(-)
diff --git a/usr.sbin/bhyve/bhyve.8 b/usr.sbin/bhyve/bhyve.8
index fdb3cafa5d0c..ac7c7cf2b04e 100644
--- a/usr.sbin/bhyve/bhyve.8
+++ b/usr.sbin/bhyve/bhyve.8
@@ -853,6 +853,24 @@ Version of the TPM device according to the TCG specification.
Defaults to
.Cm 2.0 ,
which is the only version currently supported.
+.It Cm ctrl_path= Ns Ar path
+Path of the UNIX domain socket that
+.Cm swtpm
+was given as its control channel, via
+.Cm --ctrl Cm type=unixio,path= Ns Ar path .
+Only meaningful for the
+.Cm swtpm
+backend.
+.Pp
+This is required for
+.Nm
+to tell
+.Cm swtpm
+to reinitialise the TPM as the virtual machine starts, which resets the
+platform configuration registers just as a cold boot of a physical machine
+would.
+It is required for guests using measured boot such as Windows with BitLocker
+and Secure Boot enabled.
.El
.Pp
When using a TPM with UEFI firmware, the boot ROM should be configured
diff --git a/usr.sbin/bhyve/bhyve_config.5 b/usr.sbin/bhyve/bhyve_config.5
index 55742b6fd5a8..e08e4516ae18 100644
--- a/usr.sbin/bhyve/bhyve_config.5
+++ b/usr.sbin/bhyve/bhyve_config.5
@@ -685,7 +685,7 @@ node rather than under the LPC TPM device's node.
Only one TPM device is supported.
The following nodes are available under
.Va tpm :
-.Bl -column "tpm.version" "Format" "Default"
+.Bl -column "tpm.ctrl_path" "Format" "Default"
.It Sy Name Ta Sy Format Ta Sy Default Ta Sy Description
.It Va tpm.path Ta string Ta Ta
Path to the TPM backend.
@@ -696,6 +696,12 @@ this is either the host TPM device, typically
or any UNIX domain socket on which a
.Cm swtpm
process is listening.
+.It Va tpm.ctrl_path Ta string Ta Ta
+Path to the
+.Cm swtpm
+control socket.
+When set, the TPM is reset each time the guest starts, so a reboot clears PCRs.
+This is required for guests using measured boot.
.It Va tpm.type Ta string Ta Ta
Type of the TPM device passed to the guest.
This can be either
diff --git a/usr.sbin/bhyve/tpm_emul_swtpm.c b/usr.sbin/bhyve/tpm_emul_swtpm.c
index aaa0c9c358cd..aa3eebe2a0e6 100644
--- a/usr.sbin/bhyve/tpm_emul_swtpm.c
+++ b/usr.sbin/bhyve/tpm_emul_swtpm.c
@@ -3,10 +3,14 @@
*
* Copyright (c) 2024 Hans Rosenfeld
* Author: Hans Rosenfeld <rosenfeld@grumpf.hope-2000.org>
+ * Copyright (c) 2026 Defenso SAS
+ * Author: Quentin Thébault <quentin.thebault@defenso.fr>
*/
#include <sys/types.h>
+#include <sys/endian.h>
#include <sys/socket.h>
+#include <sys/time.h>
#include <sys/un.h>
#include <err.h>
@@ -24,8 +28,93 @@
struct tpm_swtpm {
int fd;
+ int ctrl_fd;
};
+#define SWTPM_CMD_INIT 2
+#define SWTPM_CTRL_TIMEOUT_SEC 10
+
+struct swtpm_init_req {
+ uint32_t cmd;
+ uint32_t init_flags;
+};
+
+static int
+tpm_swtpm_ctrl_init(struct tpm_swtpm *tpm, const char *ctrl_path)
+{
+ struct sockaddr_un ctrl_addr;
+ struct swtpm_init_req req;
+ struct timeval tv;
+ uint32_t res;
+ ssize_t len;
+
+ tpm->ctrl_fd = socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
+ if (tpm->ctrl_fd < 0) {
+ warnx("%s: unable to open tpm control socket", __func__);
+ return (ENOENT);
+ }
+
+ tv.tv_sec = SWTPM_CTRL_TIMEOUT_SEC;
+ tv.tv_usec = 0;
+ if (setsockopt(tpm->ctrl_fd, SOL_SOCKET, SO_RCVTIMEO, &tv,
+ sizeof (tv)) == -1 ||
+ setsockopt(tpm->ctrl_fd, SOL_SOCKET, SO_SNDTIMEO, &tv,
+ sizeof (tv)) == -1) {
+ warn("%s: unable to set tpm control socket timeout", __func__);
+ return (errno);
+ }
+
+ bzero(&ctrl_addr, sizeof (ctrl_addr));
+ ctrl_addr.sun_family = AF_UNIX;
+ if (strlcpy(ctrl_addr.sun_path, ctrl_path,
+ sizeof (ctrl_addr.sun_path)) >= sizeof (ctrl_addr.sun_path)) {
+ warnx("%s: tpm control socket path \"%s\" is too long "
+ "(limit %zu)", __func__, ctrl_path,
+ sizeof (ctrl_addr.sun_path) - 1);
+ return (ENAMETOOLONG);
+ }
+
+ if (connect(tpm->ctrl_fd, (struct sockaddr *)&ctrl_addr,
+ sizeof (ctrl_addr)) == -1) {
+ warnx("%s: unable to connect to tpm control socket \"%s\"",
+ __func__, ctrl_path);
+ return (ENOENT);
+ }
+
+ req.cmd = htobe32(SWTPM_CMD_INIT);
+ req.init_flags = 0;
+
+ len = send(tpm->ctrl_fd, &req, sizeof (req), MSG_NOSIGNAL);
+ if (len == -1) {
+ warn("%s: failed to send CMD_INIT", __func__);
+ return (errno);
+ }
+ if (len != (ssize_t)sizeof (req)) {
+ warnx("%s: short write sending CMD_INIT (sent %zd of %zu)",
+ __func__, len, sizeof (req));
+ return (EIO);
+ }
+
+ len = recv(tpm->ctrl_fd, &res, sizeof (res), MSG_WAITALL);
+ if (len == -1) {
+ warn("%s: no reply to CMD_INIT", __func__);
+ return (errno);
+ }
+ if (len != (ssize_t)sizeof (res)) {
+ warnx("%s: truncated reply to CMD_INIT (read %zd of %zu)",
+ __func__, len, sizeof (res));
+ return (EIO);
+ }
+
+ if (res != 0) {
+ warnx("%s: swtpm failed to initialise the TPM: 0x%x", __func__,
+ be32toh(res));
+ return (EIO);
+ }
+
+ return (0);
+}
+
struct tpm_resp_hdr {
uint16_t tag;
uint32_t len;
@@ -36,41 +125,69 @@ static int
tpm_swtpm_init(void **sc, nvlist_t *nvl)
{
struct tpm_swtpm *tpm;
- const char *path;
+ const char *path, *ctrl_path;
struct sockaddr_un tpm_addr;
+ int error;
tpm = calloc(1, sizeof (struct tpm_swtpm));
if (tpm == NULL) {
warnx("%s: failed to allocate tpm_swtpm", __func__);
return (ENOMEM);
}
+ tpm->ctrl_fd = -1;
+ tpm->fd = -1;
path = get_config_value_node(nvl, "path");
if (path == NULL) {
warnx("%s: no socket path specified", __func__);
- return (ENOENT);
+ error = ENOENT;
+ goto fail;
}
tpm->fd = socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
if (tpm->fd < 0) {
warnx("%s: unable to open tpm socket", __func__);
- return (ENOENT);
+ error = ENOENT;
+ goto fail;
}
bzero(&tpm_addr, sizeof (tpm_addr));
tpm_addr.sun_family = AF_UNIX;
- strlcpy(tpm_addr.sun_path, path, sizeof (tpm_addr.sun_path) - 1);
+ if (strlcpy(tpm_addr.sun_path, path,
+ sizeof (tpm_addr.sun_path)) >= sizeof (tpm_addr.sun_path)) {
+ warnx("%s: tpm data socket path \"%s\" is too long "
+ "(limit %zu)", __func__, path,
+ sizeof (tpm_addr.sun_path) - 1);
+ error = ENAMETOOLONG;
+ goto fail;
+ }
if (connect(tpm->fd, (struct sockaddr *)&tpm_addr, sizeof (tpm_addr)) ==
-1) {
warnx("%s: unable to connect to tpm socket \"%s\"", __func__,
path);
- return (ENOENT);
+ error = ENOENT;
+ goto fail;
+ }
+
+ ctrl_path = get_config_value_node(nvl, "ctrl_path");
+ if (ctrl_path != NULL) {
+ error = tpm_swtpm_ctrl_init(tpm, ctrl_path);
+ if (error != 0)
+ goto fail;
}
*sc = tpm;
return (0);
+
+fail:
+ if (tpm->ctrl_fd >= 0)
+ close(tpm->ctrl_fd);
+ if (tpm->fd >= 0)
+ close(tpm->fd);
+ free(tpm);
+ return (error);
}
static int
@@ -93,7 +210,7 @@ tpm_swtpm_execute_cmd(void *sc, void *cmd, uint32_t cmd_size, void *rsp,
if (len != cmd_size) {
warn("%s: cmd write failed (bytes written: %zd / %d)", __func__,
len, cmd_size);
- return (EFAULT);
+ return (EIO);
}
len = recv(tpm->fd, rsp, rsp_size, 0);
@@ -102,7 +219,7 @@ tpm_swtpm_execute_cmd(void *sc, void *cmd, uint32_t cmd_size, void *rsp,
if (len < (ssize_t)sizeof(struct tpm_resp_hdr)) {
warn("%s: rsp read failed (bytes read: %zd / %d)", __func__,
len, rsp_size);
- return (EFAULT);
+ return (EIO);
}
return (0);
@@ -119,6 +236,8 @@ tpm_swtpm_deinit(void *sc)
if (tpm->fd >= 0)
close(tpm->fd);
+ if (tpm->ctrl_fd >= 0)
+ close(tpm->ctrl_fd);
free(tpm);
}