Page MenuHomeFreeBSD

D46373.id142252.diff
No OneTemporary

D46373.id142252.diff

diff --git a/usr.sbin/bhyve/Makefile b/usr.sbin/bhyve/Makefile
--- a/usr.sbin/bhyve/Makefile
+++ b/usr.sbin/bhyve/Makefile
@@ -60,6 +60,7 @@
sockstream.c \
tpm_device.c \
tpm_emul_passthru.c \
+ tpm_emul_swtpm.c \
tpm_intf_crb.c \
tpm_ppi_qemu.c \
uart_backend.c \
diff --git a/usr.sbin/bhyve/tpm_emul_swtpm.c b/usr.sbin/bhyve/tpm_emul_swtpm.c
new file mode 100644
--- /dev/null
+++ b/usr.sbin/bhyve/tpm_emul_swtpm.c
@@ -0,0 +1,148 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2024 Hans Rosenfeld
+ * Author: Hans Rosenfeld <rosenfeld@grumpf.hope-2000.org>
+ */
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <malloc_np.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+#include <unistd.h>
+
+#include "config.h"
+#include "tpm_device.h"
+#include "tpm_emul.h"
+
+struct tpm_swtpm {
+ int fd;
+};
+
+struct tpm_req_hdr {
+ uint16_t tag;
+ uint32_t len;
+ uint32_t ordinal;
+} __packed;
+
+struct tpm_resp_hdr {
+ uint16_t tag;
+ uint32_t len;
+ uint32_t errcode;
+} __packed;
+
+static int
+tpm_swtpm_init(void **sc, nvlist_t *nvl)
+{
+ struct tpm_swtpm *tpm;
+ const char *path;
+ struct sockaddr_un tpm_addr;
+
+ tpm = calloc(1, sizeof (struct tpm_swtpm));
+ if (tpm == NULL) {
+ warnx("%s: failed to allocate tpm_swtpm", __func__);
+ return (ENOMEM);
+ }
+
+ path = get_config_value_node(nvl, "path");
+ if (path == NULL) {
+ warnx("%s: no socket path specified", __func__);
+ return (ENOENT);
+ }
+
+ tpm->fd = socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
+ if (tpm->fd < 0) {
+ warnx("%s: unable to open tpm socket", __func__);
+ return (ENOENT);
+ }
+
+ 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 (connect(tpm->fd, (struct sockaddr *)&tpm_addr, sizeof (tpm_addr)) ==
+ -1) {
+ warnx("%s: unable to connect to tpm socket \"%s\"", __func__,
+ path);
+ return (ENOENT);
+ }
+
+ *sc = tpm;
+
+ return (0);
+}
+
+static int
+tpm_swtpm_execute_cmd(void *sc, void *cmd, uint32_t cmd_size, void *rsp,
+ uint32_t rsp_size)
+{
+ struct tpm_swtpm *tpm;
+ struct tpm_req_hdr *hdr;
+ ssize_t len;
+
+ if (rsp_size < (ssize_t)sizeof(struct tpm_resp_hdr)) {
+ warn("%s: rsp_size of %u is too small", __func__, rsp_size);
+ return (EINVAL);
+ }
+
+ tpm = sc;
+
+ /*
+ * Reduce cmd_size to the size specified in the request header. This is
+ * necessary because swtpm returns an error when it is sent more data
+ * than it rightfully expects.
+ */
+ hdr = cmd;
+ if (cmd_size > be32toh(hdr->len))
+ cmd_size = be32toh(hdr->len);
+
+ len = send(tpm->fd, cmd, cmd_size, MSG_NOSIGNAL|MSG_DONTWAIT);
+ if (len == -1)
+ err(1, "%s: cmd send failed, is swtpm running?", __func__);
+ if (len != cmd_size) {
+ warn("%s: cmd write failed (bytes written: %zd / %d)", __func__,
+ len, cmd_size);
+ return (EFAULT);
+ }
+
+ len = recv(tpm->fd, rsp, rsp_size, 0);
+ if (len == -1)
+ err(1, "%s: rsp recv failed, is swtpm running?", __func__);
+ 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 (0);
+}
+
+static void
+tpm_swtpm_deinit(void *sc)
+{
+ struct tpm_swtpm *tpm;
+
+ tpm = sc;
+ if (tpm == NULL)
+ return;
+
+ if (tpm->fd >= 0)
+ close(tpm->fd);
+
+ free(tpm);
+}
+
+static const struct tpm_emul tpm_emul_swtpm = {
+ .name = "swtpm",
+ .init = tpm_swtpm_init,
+ .deinit = tpm_swtpm_deinit,
+ .execute_cmd = tpm_swtpm_execute_cmd,
+};
+TPM_EMUL_SET(tpm_emul_swtpm);
diff --git a/usr.sbin/bhyve/tpm_intf_crb.c b/usr.sbin/bhyve/tpm_intf_crb.c
--- a/usr.sbin/bhyve/tpm_intf_crb.c
+++ b/usr.sbin/bhyve/tpm_intf_crb.c
@@ -384,10 +384,10 @@
default:
/*
* The other fields are either readonly or we do not
- * support writing them.
+ * support writing them. Ignore them rather than
+ * erroring out.
*/
- error = EINVAL;
- goto err_out;
+ break;
}
}

File Metadata

Mime Type
text/plain
Expires
Thu, Feb 19, 7:53 AM (43 m, 23 s)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
28873389
Default Alt Text
D46373.id142252.diff (3 KB)

Event Timeline