| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <unistd.h> |
| 4 |
|
| 5 |
#include "sd-daemon.h" |
| 6 |
|
| 7 |
#include "alloc-util.h" |
| 8 |
#include "chase.h" |
| 9 |
#include "errno-util.h" |
| 10 |
#include "escape.h" |
| 11 |
#include "fd-util.h" |
| 12 |
#include "fileio.h" |
| 13 |
#include "find-esp.h" |
| 14 |
#include "fs-util.h" |
| 15 |
#include "hexdecoct.h" |
| 16 |
#include "hmac.h" |
| 17 |
#include "initrd-util.h" |
| 18 |
#include "iovec-util.h" |
| 19 |
#include "log.h" |
| 20 |
#include "main-func.h" |
| 21 |
#include "path-lookup.h" |
| 22 |
#include "path-util.h" |
| 23 |
#include "sha256.h" |
| 24 |
#include "stat-util.h" |
| 25 |
#include "string-util.h" |
| 26 |
#include "strv.h" |
| 27 |
#include "swtpm-util.h" |
| 28 |
|
| 29 |
#define BOOT_SECRET_SIZE 32U |
| 30 |
|
| 31 |
static int load_boot_secret(struct iovec *ret) { |
| 32 |
_cleanup_(iovec_done_erase) struct iovec buf = {}; |
| 33 |
int r; |
| 34 |
|
| 35 |
assert(ret); |
| 36 |
|
| 37 |
const char *bs = in_initrd() ? "/.extra/boot-secret" : "/run/systemd/stub/boot-secret"; |
| 38 |
r = read_full_file_full( |
| 39 |
AT_FDCWD, |
| 40 |
bs, |
| 41 |
UINT64_MAX, |
| 42 |
BOOT_SECRET_SIZE, |
| 43 |
READ_FULL_FILE_SECURE|READ_FULL_FILE_VERIFY_REGULAR, |
| 44 |
/* bind_name= */ NULL, |
| 45 |
(char**) &buf.iov_base, |
| 46 |
&buf.iov_len); |
| 47 |
if (r == -ENOENT) { |
| 48 |
log_warning_errno(r, "Boot secret (%s) not found, not encrypting software TPM state!", bs); |
| 49 |
*ret = (struct iovec) {}; |
| 50 |
return 0; |
| 51 |
} |
| 52 |
if (r < 0) |
| 53 |
return log_error_errno(r, "Failed to read '%s': %m", bs); |
| 54 |
|
| 55 |
if (buf.iov_len < BOOT_SECRET_SIZE) |
| 56 |
return log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE), "Boot secret too short, refusing."); |
| 57 |
|
| 58 |
*ret = TAKE_STRUCT(buf); |
| 59 |
return 1; |
| 60 |
} |
| 61 |
|
| 62 |
static int prepare_secret(const char *runtime_dir, char **ret) { |
| 63 |
int r; |
| 64 |
|
| 65 |
assert(runtime_dir); |
| 66 |
assert(ret); |
| 67 |
|
| 68 |
_cleanup_(iovec_done_erase) struct iovec boot_secret = {}; |
| 69 |
r = load_boot_secret(&boot_secret); |
| 70 |
if (r < 0) |
| 71 |
return r; |
| 72 |
if (r == 0) { |
| 73 |
*ret = NULL; |
| 74 |
return 0; |
| 75 |
} |
| 76 |
|
| 77 |
/* Derive a suitable swtpm specific secret */ |
| 78 |
static const char tag[] = "systemd swtpm tag v1"; |
| 79 |
uint8_t secret[SHA256_DIGEST_SIZE]; |
| 80 |
CLEANUP_ERASE(secret); |
| 81 |
hmac_sha256(boot_secret.iov_base, |
| 82 |
boot_secret.iov_len, |
| 83 |
tag, |
| 84 |
strlen(tag), |
| 85 |
secret); |
| 86 |
|
| 87 |
_cleanup_free_ char *p = path_join(runtime_dir, "secret"); |
| 88 |
if (!p) |
| 89 |
return log_oom(); |
| 90 |
|
| 91 |
assert_cc(sizeof(secret) >= 16); /* swtpm only wants a 16 byte key */ |
| 92 |
_cleanup_(erase_and_freep) char *h = hexmem(secret, 16); |
| 93 |
if (!h) |
| 94 |
return log_oom(); |
| 95 |
|
| 96 |
r = write_data_file_atomic_at(XAT_FDROOT, p, &IOVEC_MAKE_STRING(h), WRITE_DATA_FILE_MODE_0400); |
| 97 |
if (r < 0) |
| 98 |
return log_error_errno(r, "Failed to write secret file: %m"); |
| 99 |
|
| 100 |
*ret = TAKE_PTR(p); |
| 101 |
return 1; |
| 102 |
} |
| 103 |
|
| 104 |
static int setup_swtpm(const char *state_dir, int state_fd, const char *secret) { |
| 105 |
int r; |
| 106 |
|
| 107 |
assert(state_dir); |
| 108 |
assert(state_fd >= 0); |
| 109 |
|
| 110 |
/* Sets up the state directory via swtpm_setup */ |
| 111 |
|
| 112 |
if (in_initrd()) { |
| 113 |
/* In the initrd remove previous transient state */ |
| 114 |
r = RET_NERRNO(unlinkat(state_fd, "tpm2-00.volatilestate", /* flags= */ 0)); |
| 115 |
if (r < 0 && r != -ENOENT) |
| 116 |
return log_error_errno(r, "Failed to remove 'tpm2-00.volatilestate': %m"); |
| 117 |
} |
| 118 |
|
| 119 |
r = dir_is_empty_at(state_fd, /* path= */ NULL, /* ignore_hidden_or_backup= */ false); |
| 120 |
if (r < 0) |
| 121 |
return log_error_errno(r, "Failed to check if TPM state directory is empty: %m"); |
| 122 |
if (r == 0) { |
| 123 |
log_debug("TPM state directory is already populated, not manufacturing a TPM."); |
| 124 |
return 0; |
| 125 |
} |
| 126 |
|
| 127 |
if (!in_initrd()) |
| 128 |
return log_error_errno(SYNTHETIC_ERRNO(ESTALE), "swtpm TPM state directory has not been initialized in the initrd, refusing."); |
| 129 |
|
| 130 |
log_debug("TPM state directory is unpopulated, manufacturing a TPM."); |
| 131 |
|
| 132 |
return manufacture_swtpm(state_dir, secret); |
| 133 |
} |
| 134 |
|
| 135 |
static int run(int argc, char *argv[]) { |
| 136 |
int r; |
| 137 |
|
| 138 |
log_setup(); |
| 139 |
|
| 140 |
_cleanup_free_ char *runtime_dir = NULL; |
| 141 |
r = runtime_directory(RUNTIME_SCOPE_SYSTEM, "systemd/swtpm", &runtime_dir); |
| 142 |
if (r < 0) |
| 143 |
return log_error_errno(r, "Unable to determine runtime directory: %m"); |
| 144 |
|
| 145 |
_cleanup_free_ char *swtpm = NULL; |
| 146 |
r = find_executable("swtpm", &swtpm); |
| 147 |
if (r < 0) |
| 148 |
return log_error_errno(r, "Failed to find 'swtpm' binary: %m"); |
| 149 |
|
| 150 |
_cleanup_free_ char *state_dir = NULL; |
| 151 |
_cleanup_close_ int state_fd = -EBADF; |
| 152 |
if (in_initrd()) { |
| 153 |
/* The early ESP support uses only a single mount point, we do not need to search for it. */ |
| 154 |
r = chase("/loader/swtpm", |
| 155 |
"/sysefi", |
| 156 |
CHASE_PREFIX_ROOT|CHASE_TRIGGER_AUTOFS|CHASE_PROHIBIT_SYMLINKS|CHASE_MKDIR_0755|CHASE_MUST_BE_DIRECTORY, |
| 157 |
&state_dir, |
| 158 |
&state_fd); |
| 159 |
if (r < 0) |
| 160 |
return log_error_errno(r, "Failed to open swtpm state directory in ESP: %m"); |
| 161 |
} else { |
| 162 |
_cleanup_free_ char *esp_path = NULL; |
| 163 |
_cleanup_close_ int esp_fd = -EBADF; |
| 164 |
r = find_esp_and_warn( |
| 165 |
/* root= */ NULL, |
| 166 |
/* path= */ NULL, |
| 167 |
/* unprivileged_mode= */ false, |
| 168 |
&esp_path, |
| 169 |
&esp_fd); |
| 170 |
if (r == -ENOKEY) /* This one find_esp_and_warn() doesn't actually log about. */ |
| 171 |
return log_error_errno(r, "No ESP discovered."); |
| 172 |
if (r < 0) |
| 173 |
return r; |
| 174 |
|
| 175 |
_cleanup_free_ char *unprefixed_state_dir = NULL; |
| 176 |
r = chaseat(esp_fd, |
| 177 |
esp_fd, |
| 178 |
"/loader/swtpm", |
| 179 |
CHASE_TRIGGER_AUTOFS|CHASE_PROHIBIT_SYMLINKS|CHASE_MKDIR_0755|CHASE_MUST_BE_DIRECTORY, |
| 180 |
&unprefixed_state_dir, |
| 181 |
&state_fd); |
| 182 |
if (r < 0) |
| 183 |
return log_error_errno(r, "Failed to open swtpm state directory in ESP: %m"); |
| 184 |
|
| 185 |
state_dir = path_join(esp_path, unprefixed_state_dir); |
| 186 |
if (!state_dir) |
| 187 |
return log_oom(); |
| 188 |
} |
| 189 |
|
| 190 |
_cleanup_(unlink_and_freep) char *secret = NULL; |
| 191 |
r = prepare_secret(runtime_dir, &secret); |
| 192 |
if (r < 0) |
| 193 |
return r; |
| 194 |
|
| 195 |
r = setup_swtpm(state_dir, state_fd, secret); |
| 196 |
if (r < 0) |
| 197 |
return r; |
| 198 |
|
| 199 |
_cleanup_strv_free_ char **args = |
| 200 |
strv_new(swtpm, |
| 201 |
"chardev", |
| 202 |
"--vtpm-proxy", |
| 203 |
"--tpm2", |
| 204 |
/* Make sure that in the initrd swtpm never sends TPM2_Shutdown() for us, we want to |
| 205 |
* be able to stop the daemon after all temporarily during the initrd→host |
| 206 |
* transition. */ |
| 207 |
in_initrd() ? "--flags=startup-clear,disable-auto-shutdown" : "--flags=startup-clear"); |
| 208 |
if (!args) |
| 209 |
return log_oom(); |
| 210 |
|
| 211 |
if (strv_extendf(&args, "--ctrl=type=unixio,path=%s/socket", runtime_dir) < 0) |
| 212 |
return log_oom(); |
| 213 |
|
| 214 |
if (secret && strv_extendf(&args, "--key=file=%s,format=hex,mode=aes-cbc,remove=true", secret) < 0) |
| 215 |
return log_oom(); |
| 216 |
|
| 217 |
if (strv_extendf(&args, "--tpmstate=dir=%s,mode=0600", state_dir) < 0) |
| 218 |
return log_oom(); |
| 219 |
|
| 220 |
if (DEBUG_LOGGING) { |
| 221 |
_cleanup_free_ char *cmd = quote_command_line(args, SHELL_ESCAPE_EMPTY); |
| 222 |
|
| 223 |
log_debug("Chain-loading: %s", strnull(cmd)); |
| 224 |
} |
| 225 |
|
| 226 |
/* Ideally swtpm could send this itself, but for now let's accept it like this. */ |
| 227 |
// FIXME: remove this once swtpm 0.11 is released and hit all relevant distros. Then bump version |
| 228 |
// requirements. |
| 229 |
(void) sd_notify(/* unset_environment= */ true, "READY=1"); |
| 230 |
|
| 231 |
/* NB: if the execve() succeeds it's swtpm's job to actually unlink the secret file */ |
| 232 |
execv(swtpm, args); |
| 233 |
return log_error_errno(errno, "Failed to chainload swtpm: %m"); |
| 234 |
} |
| 235 |
|
| 236 |
DEFINE_MAIN_FUNCTION(run); |