From 901024eea18ddd3ac27c0e69a69a48d2b23bdfe2 Mon Sep 17 00:00:00 2001 From: Daan De Meyer Date: Wed, 25 Mar 2026 14:23:11 +0100 Subject: [PATCH 1/2] swtpm: Properly configure state directory Otherwise it will unconditionally try to use /var/lib/xxx which won't work when running unprivileged. --- src/shared/swtpm-util.c | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/src/shared/swtpm-util.c b/src/shared/swtpm-util.c index 836f877e12..1a475f0e08 100644 --- a/src/shared/swtpm-util.c +++ b/src/shared/swtpm-util.c @@ -7,6 +7,7 @@ #include "alloc-util.h" #include "escape.h" #include "fd-util.h" +#include "fileio.h" #include "json-util.h" #include "log.h" #include "memfd-util.h" @@ -120,6 +121,43 @@ int manufacture_swtpm(const char *state_dir, const char *secret) { } } + /* Create custom swtpm config files so that swtpm_localca uses our state directory instead of + * the system-wide /var/lib/swtpm-localca/ which may not be writable. */ + _cleanup_free_ char *localca_conf = path_join(state_dir, "swtpm-localca.conf"); + if (!localca_conf) + return log_oom(); + + r = write_string_filef( + localca_conf, + WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_TRUNCATE|WRITE_STRING_FILE_MKDIR_0755, + "statedir = %1$s\n" + "signingkey = %1$s/signing-private-key.pem\n" + "issuercert = %1$s/issuer-certificate.pem\n" + "certserial = %1$s/certserial\n", + state_dir); + if (r < 0) + return log_error_errno(r, "Failed to write swtpm-localca.conf: %m"); + + _cleanup_free_ char *swtpm_localca = NULL; + r = find_executable("swtpm_localca", &swtpm_localca); + if (r < 0) + return log_error_errno(r, "Failed to find 'swtpm_localca' binary: %m"); + + _cleanup_free_ char *setup_conf = path_join(state_dir, "swtpm_setup.conf"); + if (!setup_conf) + return log_oom(); + + r = write_string_filef( + setup_conf, + WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_TRUNCATE|WRITE_STRING_FILE_MKDIR_0755, + "create_certs_tool = %1$s\n" + "create_certs_tool_config = %2$s\n" + "create_certs_tool_options = /etc/swtpm-localca.options\n", + swtpm_localca, + localca_conf); + if (r < 0) + return log_error_errno(r, "Failed to write swtpm_setup.conf: %m"); + strv_free(args); args = strv_new(swtpm_setup, "--tpm-state", state_dir, @@ -129,7 +167,8 @@ int manufacture_swtpm(const char *state_dir, const char *secret) { "--createek", "--create-ek-cert", "--create-platform-cert", - "--not-overwrite"); + "--not-overwrite", + "--config", setup_conf); if (!args) return log_oom(); From 82d69529cb6be01c65eec69ba8189b9bac9ba3a3 Mon Sep 17 00:00:00 2001 From: Daan De Meyer Date: Wed, 25 Mar 2026 15:34:58 +0100 Subject: [PATCH 2/2] swtpm-util: Write our own CA options rather than using the distro ones --- src/shared/swtpm-util.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/shared/swtpm-util.c b/src/shared/swtpm-util.c index 1a475f0e08..55e3f2f34c 100644 --- a/src/shared/swtpm-util.c +++ b/src/shared/swtpm-util.c @@ -138,6 +138,19 @@ int manufacture_swtpm(const char *state_dir, const char *secret) { if (r < 0) return log_error_errno(r, "Failed to write swtpm-localca.conf: %m"); + _cleanup_free_ char *localca_options = path_join(state_dir, "swtpm-localca.options"); + if (!localca_options) + return log_oom(); + + r = write_string_file( + localca_options, + "--platform-manufacturer systemd\n" + "--platform-version 2.1\n" + "--platform-model swtpm\n", + WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_TRUNCATE|WRITE_STRING_FILE_MKDIR_0755); + if (r < 0) + return log_error_errno(r, "Failed to write swtpm-localca.options: %m"); + _cleanup_free_ char *swtpm_localca = NULL; r = find_executable("swtpm_localca", &swtpm_localca); if (r < 0) @@ -152,9 +165,10 @@ int manufacture_swtpm(const char *state_dir, const char *secret) { WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_TRUNCATE|WRITE_STRING_FILE_MKDIR_0755, "create_certs_tool = %1$s\n" "create_certs_tool_config = %2$s\n" - "create_certs_tool_options = /etc/swtpm-localca.options\n", + "create_certs_tool_options = %3$s\n", swtpm_localca, - localca_conf); + localca_conf, + localca_options); if (r < 0) return log_error_errno(r, "Failed to write swtpm_setup.conf: %m");