| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <stdlib.h> |
| 4 |
#include <sys/mount.h> |
| 5 |
#include <unistd.h> |
| 6 |
|
| 7 |
#include "confidential-virt.h" |
| 8 |
#include "copy.h" |
| 9 |
#include "creds-util.h" |
| 10 |
#include "errno-util.h" |
| 11 |
#include "escape.h" |
| 12 |
#include "fileio.h" |
| 13 |
#include "format-util.h" |
| 14 |
#include "fs-util.h" |
| 15 |
#include "hexdecoct.h" |
| 16 |
#include "import-creds.h" |
| 17 |
#include "initrd-util.h" |
| 18 |
#include "io-util.h" |
| 19 |
#include "log.h" |
| 20 |
#include "mkdir.h" |
| 21 |
#include "mount-util.h" |
| 22 |
#include "mountpoint-util.h" |
| 23 |
#include "parse-util.h" |
| 24 |
#include "path-util.h" |
| 25 |
#include "proc-cmdline.h" |
| 26 |
#include "recurse-dir.h" |
| 27 |
#include "smbios11.h" |
| 28 |
#include "stat-util.h" |
| 29 |
#include "string-util.h" |
| 30 |
#include "strv.h" |
| 31 |
#include "virt.h" |
| 32 |
|
| 33 |
/* This imports credentials passed in from environments higher up (VM manager, boot loader, …) and rearranges |
| 34 |
* them so that later code can access them using our regular credential protocol |
| 35 |
* (i.e. $CREDENTIALS_DIRECTORY). It's supposed to be minimal glue to unify behaviour how PID 1 (and |
| 36 |
* generators invoked by it) can acquire credentials from outside, to mimic how we support it for containers, |
| 37 |
* but on VM/physical environments. |
| 38 |
* |
| 39 |
* This does four things: |
| 40 |
* |
| 41 |
* 1. It imports credentials picked up by sd-boot (and placed in the /.extra/credentials/ dir in the initrd) |
| 42 |
* and puts them in /run/credentials/@encrypted/. Note that during the initrd→host transition the initrd root |
| 43 |
* file system is cleaned out, thus it is essential we pick up these files before they are deleted. Note |
| 44 |
* that these credentials originate from an untrusted source, i.e. the ESP and are not |
| 45 |
* pre-authenticated. They still have to be authenticated before use. |
| 46 |
* |
| 47 |
* 2. It imports credentials from /proc/cmdline and puts them in /run/credentials/@system/. These come from a |
| 48 |
* trusted environment (i.e. the boot loader), and are typically authenticated (if authentication is done |
| 49 |
* at all). However, they are world-readable, which might be less than ideal. Hence only use this for data |
| 50 |
* that doesn't require trust. |
| 51 |
* |
| 52 |
* 3. It imports credentials passed in through qemu's fw_cfg logic. Specifically, credential data passed in |
| 53 |
* /sys/firmware/qemu_fw_cfg/by_name/opt/io.systemd.credentials/ is picked up and also placed in |
| 54 |
* /run/credentials/@system/. |
| 55 |
* |
| 56 |
* 4. It imports credentials passed in via the DMI/SMBIOS OEM string tables, quite similar to fw_cfg. It |
| 57 |
* looks for strings starting with "io.systemd.credential:" and "io.systemd.credential.binary:". Both |
| 58 |
* expect a key=value assignment, but in the latter case the value is Base64 decoded, allowing binary |
| 59 |
* credentials to be passed in. |
| 60 |
* |
| 61 |
* If it picked up any credentials it will set the $CREDENTIALS_DIRECTORY and |
| 62 |
* $ENCRYPTED_CREDENTIALS_DIRECTORY environment variables to point to these directories, so that processes |
| 63 |
* can find them there later on. If "ramfs" is available $CREDENTIALS_DIRECTORY will be backed by it (but |
| 64 |
* $ENCRYPTED_CREDENTIALS_DIRECTORY is just a regular tmpfs). |
| 65 |
* |
| 66 |
* Net result: the service manager can pick up trusted credentials from $CREDENTIALS_DIRECTORY afterwards, |
| 67 |
* and untrusted ones from $ENCRYPTED_CREDENTIALS_DIRECTORY. */ |
| 68 |
|
| 69 |
typedef struct ImportCredentialsContext { |
| 70 |
int target_dir_fd; |
| 71 |
size_t size_sum; |
| 72 |
unsigned n_credentials; |
| 73 |
} ImportCredentialsContext; |
| 74 |
|
| 75 |
static void import_credentials_context_done(ImportCredentialsContext *c) { |
| 76 |
assert(c); |
| 77 |
|
| 78 |
safe_close(c->target_dir_fd); |
| 79 |
} |
| 80 |
|
| 81 |
static int acquire_credential_directory(ImportCredentialsContext *c, const char *path, bool with_mount) { |
| 82 |
int r; |
| 83 |
|
| 84 |
assert(c); |
| 85 |
assert(path); |
| 86 |
|
| 87 |
if (c->target_dir_fd >= 0) |
| 88 |
return c->target_dir_fd; |
| 89 |
|
| 90 |
r = path_is_mount_point(path); |
| 91 |
if (r < 0) { |
| 92 |
if (r != -ENOENT) |
| 93 |
return log_error_errno(r, "Failed to determine if %s is a mount point: %m", path); |
| 94 |
|
| 95 |
r = mkdir_safe_label(path, 0700, 0, 0, MKDIR_WARN_MODE); |
| 96 |
if (r < 0) |
| 97 |
return log_error_errno(r, "Failed to create %s mount point: %m", path); |
| 98 |
|
| 99 |
r = 0; /* Now it exists and is not a mount point */ |
| 100 |
} |
| 101 |
if (r > 0) |
| 102 |
/* If already a mount point, then remount writable */ |
| 103 |
(void) mount_nofollow_verbose(LOG_WARNING, NULL, path, NULL, MS_BIND|MS_REMOUNT|credentials_fs_mount_flags(/* ro= */ false), NULL); |
| 104 |
else if (with_mount) |
| 105 |
/* If not a mount point yet, and the credentials are not encrypted, then let's try to mount a no-swap fs there */ |
| 106 |
(void) mount_credentials_fs(path); |
| 107 |
|
| 108 |
c->target_dir_fd = open(path, O_RDONLY|O_DIRECTORY|O_CLOEXEC); |
| 109 |
if (c->target_dir_fd < 0) |
| 110 |
return log_error_errno(errno, "Failed to open %s: %m", path); |
| 111 |
|
| 112 |
return c->target_dir_fd; |
| 113 |
} |
| 114 |
|
| 115 |
static int open_credential_file_for_write(int target_dir_fd, const char *dir_name, const char *n) { |
| 116 |
int fd; |
| 117 |
|
| 118 |
assert(target_dir_fd >= 0); |
| 119 |
assert(dir_name); |
| 120 |
assert(n); |
| 121 |
|
| 122 |
fd = openat(target_dir_fd, n, O_WRONLY|O_CLOEXEC|O_CREAT|O_EXCL|O_NOFOLLOW, 0400); |
| 123 |
if (fd < 0) { |
| 124 |
if (errno == EEXIST) /* In case of EEXIST we'll only debug log! */ |
| 125 |
return log_debug_errno(errno, "Credential '%s' set twice, ignoring.", n); |
| 126 |
|
| 127 |
return log_error_errno(errno, "Failed to create %s/%s: %m", dir_name, n); |
| 128 |
} |
| 129 |
|
| 130 |
return fd; |
| 131 |
} |
| 132 |
|
| 133 |
static bool credential_size_ok(const ImportCredentialsContext *c, const char *name, uint64_t size) { |
| 134 |
assert(c); |
| 135 |
assert(name); |
| 136 |
|
| 137 |
if (size > CREDENTIAL_SIZE_MAX) { |
| 138 |
log_warning("Credential '%s' is larger than allowed limit (%s > %s), skipping.", name, FORMAT_BYTES(size), FORMAT_BYTES(CREDENTIAL_SIZE_MAX)); |
| 139 |
return false; |
| 140 |
} |
| 141 |
|
| 142 |
if (size > CREDENTIALS_TOTAL_SIZE_MAX - c->size_sum) { |
| 143 |
log_warning("Accumulated credential size would be above allowed limit (%s+%s > %s), skipping '%s'.", |
| 144 |
FORMAT_BYTES(c->size_sum), FORMAT_BYTES(size), FORMAT_BYTES(CREDENTIALS_TOTAL_SIZE_MAX), name); |
| 145 |
return false; |
| 146 |
} |
| 147 |
|
| 148 |
return true; |
| 149 |
} |
| 150 |
|
| 151 |
static int finalize_credentials_dir(const char *dir, const char *envvar) { |
| 152 |
int r; |
| 153 |
|
| 154 |
assert(dir); |
| 155 |
assert(envvar); |
| 156 |
|
| 157 |
/* Try to make the credentials directory read-only now */ |
| 158 |
|
| 159 |
r = make_mount_point(dir); |
| 160 |
if (r < 0) |
| 161 |
log_warning_errno(r, "Failed to make '%s' a mount point, ignoring: %m", dir); |
| 162 |
else |
| 163 |
(void) mount_nofollow_verbose(LOG_WARNING, NULL, dir, NULL, MS_BIND|MS_REMOUNT|credentials_fs_mount_flags(/* ro= */ true), NULL); |
| 164 |
|
| 165 |
if (setenv(envvar, dir, /* overwrite= */ true) < 0) |
| 166 |
return log_error_errno(errno, "Failed to set $%s environment variable: %m", envvar); |
| 167 |
|
| 168 |
return 0; |
| 169 |
} |
| 170 |
|
| 171 |
static int import_credentials_boot(void) { |
| 172 |
_cleanup_(import_credentials_context_done) ImportCredentialsContext context = { |
| 173 |
.target_dir_fd = -EBADF, |
| 174 |
}; |
| 175 |
int r; |
| 176 |
|
| 177 |
/* systemd-stub will wrap sidecar *.cred files from the UEFI kernel image directory into initrd |
| 178 |
* cpios, so that they unpack into /.extra/. We'll pick them up from there and copy them into /run/ |
| 179 |
* so that we can access them during the entire runtime (note that the initrd file system is erased |
| 180 |
* during the initrd → host transition). Note that these credentials originate from an untrusted |
| 181 |
* source (i.e. the ESP typically) and thus need to be authenticated later. We thus put them in a |
| 182 |
* directory separate from the usual credentials which are from a trusted source. */ |
| 183 |
|
| 184 |
if (!in_initrd()) |
| 185 |
return 0; |
| 186 |
|
| 187 |
FOREACH_STRING(p, |
| 188 |
"/.extra/credentials/", /* specific to this boot menu */ |
| 189 |
"/.extra/global_credentials/") { /* boot partition wide */ |
| 190 |
|
| 191 |
_cleanup_free_ DirectoryEntries *de = NULL; |
| 192 |
_cleanup_close_ int source_dir_fd = -EBADF; |
| 193 |
|
| 194 |
source_dir_fd = open(p, O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW); |
| 195 |
if (source_dir_fd < 0) { |
| 196 |
if (errno == ENOENT) { |
| 197 |
log_debug("No credentials passed via %s.", p); |
| 198 |
continue; |
| 199 |
} |
| 200 |
|
| 201 |
log_warning_errno(errno, "Failed to open '%s', ignoring: %m", p); |
| 202 |
continue; |
| 203 |
} |
| 204 |
|
| 205 |
r = readdir_all(source_dir_fd, RECURSE_DIR_SORT|RECURSE_DIR_IGNORE_DOT, &de); |
| 206 |
if (r < 0) { |
| 207 |
log_warning_errno(r, "Failed to read '%s' contents, ignoring: %m", p); |
| 208 |
continue; |
| 209 |
} |
| 210 |
|
| 211 |
FOREACH_ARRAY(i, de->entries, de->n_entries) { |
| 212 |
const struct dirent *d = *i; |
| 213 |
_cleanup_close_ int cfd = -EBADF, nfd = -EBADF; |
| 214 |
_cleanup_free_ char *n = NULL; |
| 215 |
const char *e; |
| 216 |
struct stat st; |
| 217 |
|
| 218 |
e = endswith(d->d_name, ".cred"); |
| 219 |
if (!e) |
| 220 |
continue; |
| 221 |
|
| 222 |
/* drop .cred suffix (which we want in the ESP sidecar dir, but not for our internal |
| 223 |
* processing) */ |
| 224 |
n = strndup(d->d_name, e - d->d_name); |
| 225 |
if (!n) |
| 226 |
return log_oom(); |
| 227 |
|
| 228 |
if (!credential_name_valid(n)) { |
| 229 |
log_warning("Credential '%s' has invalid name, ignoring.", d->d_name); |
| 230 |
continue; |
| 231 |
} |
| 232 |
|
| 233 |
cfd = openat(source_dir_fd, d->d_name, O_RDONLY|O_CLOEXEC); |
| 234 |
if (cfd < 0) { |
| 235 |
log_warning_errno(errno, "Failed to open %s, ignoring: %m", d->d_name); |
| 236 |
continue; |
| 237 |
} |
| 238 |
|
| 239 |
if (fstat(cfd, &st) < 0) { |
| 240 |
log_warning_errno(errno, "Failed to stat %s, ignoring: %m", d->d_name); |
| 241 |
continue; |
| 242 |
} |
| 243 |
|
| 244 |
r = stat_verify_regular(&st); |
| 245 |
if (r < 0) { |
| 246 |
log_warning_errno(r, "Credential file %s is not a regular file, ignoring: %m", d->d_name); |
| 247 |
continue; |
| 248 |
} |
| 249 |
|
| 250 |
if (!credential_size_ok(&context, n, st.st_size)) |
| 251 |
continue; |
| 252 |
|
| 253 |
r = acquire_credential_directory(&context, ENCRYPTED_SYSTEM_CREDENTIALS_DIRECTORY, /* with_mount= */ false); |
| 254 |
if (r < 0) |
| 255 |
return r; |
| 256 |
|
| 257 |
nfd = open_credential_file_for_write(context.target_dir_fd, ENCRYPTED_SYSTEM_CREDENTIALS_DIRECTORY, n); |
| 258 |
if (nfd == -EEXIST) |
| 259 |
continue; |
| 260 |
if (nfd < 0) |
| 261 |
return nfd; |
| 262 |
|
| 263 |
r = copy_bytes(cfd, nfd, st.st_size, 0); |
| 264 |
if (r < 0) { |
| 265 |
(void) unlinkat(context.target_dir_fd, n, 0); |
| 266 |
return log_error_errno(r, "Failed to create credential '%s': %m", n); |
| 267 |
} |
| 268 |
|
| 269 |
context.size_sum += st.st_size; |
| 270 |
context.n_credentials++; |
| 271 |
|
| 272 |
log_debug("Successfully copied boot credential '%s'.", n); |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
if (context.n_credentials > 0) { |
| 277 |
log_debug("Imported %u credentials from boot loader.", context.n_credentials); |
| 278 |
|
| 279 |
r = finalize_credentials_dir(ENCRYPTED_SYSTEM_CREDENTIALS_DIRECTORY, "ENCRYPTED_CREDENTIALS_DIRECTORY"); |
| 280 |
if (r < 0) |
| 281 |
return r; |
| 282 |
} |
| 283 |
|
| 284 |
return 0; |
| 285 |
} |
| 286 |
|
| 287 |
static int proc_cmdline_callback(const char *key, const char *value, void *data) { |
| 288 |
ImportCredentialsContext *c = ASSERT_PTR(data); |
| 289 |
_cleanup_free_ void *binary = NULL; |
| 290 |
_cleanup_free_ char *n = NULL; |
| 291 |
_cleanup_close_ int nfd = -EBADF; |
| 292 |
const char *colon, *d; |
| 293 |
bool base64; |
| 294 |
size_t l; |
| 295 |
int r; |
| 296 |
|
| 297 |
assert(key); |
| 298 |
|
| 299 |
if (proc_cmdline_key_streq(key, "systemd.set_credential")) |
| 300 |
base64 = false; |
| 301 |
else if (proc_cmdline_key_streq(key, "systemd.set_credential_binary")) |
| 302 |
base64 = true; |
| 303 |
else |
| 304 |
return 0; |
| 305 |
|
| 306 |
colon = value ? strchr(value, ':') : NULL; |
| 307 |
if (!colon) { |
| 308 |
log_warning("Credential assignment through kernel command line lacks ':' character, ignoring: %s", value); |
| 309 |
return 0; |
| 310 |
} |
| 311 |
|
| 312 |
n = strndup(value, colon - value); |
| 313 |
if (!n) |
| 314 |
return log_oom(); |
| 315 |
|
| 316 |
if (!credential_name_valid(n)) { |
| 317 |
log_warning("Credential name '%s' is invalid, ignoring.", n); |
| 318 |
return 0; |
| 319 |
} |
| 320 |
|
| 321 |
colon++; |
| 322 |
|
| 323 |
if (base64) { |
| 324 |
r = unbase64mem(colon, &binary, &l); |
| 325 |
if (r < 0) { |
| 326 |
log_warning_errno(r, "Failed to decode binary credential '%s' data, ignoring: %m", n); |
| 327 |
return 0; |
| 328 |
} |
| 329 |
|
| 330 |
d = binary; |
| 331 |
} else { |
| 332 |
d = colon; |
| 333 |
l = strlen(colon); |
| 334 |
} |
| 335 |
|
| 336 |
if (!credential_size_ok(c, n, l)) |
| 337 |
return 0; |
| 338 |
|
| 339 |
r = acquire_credential_directory(c, SYSTEM_CREDENTIALS_DIRECTORY, /* with_mount= */ true); |
| 340 |
if (r < 0) |
| 341 |
return r; |
| 342 |
|
| 343 |
nfd = open_credential_file_for_write(c->target_dir_fd, SYSTEM_CREDENTIALS_DIRECTORY, n); |
| 344 |
if (nfd == -EEXIST) |
| 345 |
return 0; |
| 346 |
if (nfd < 0) |
| 347 |
return nfd; |
| 348 |
|
| 349 |
r = loop_write(nfd, d, l); |
| 350 |
if (r < 0) { |
| 351 |
(void) unlinkat(c->target_dir_fd, n, 0); |
| 352 |
return log_error_errno(r, "Failed to write credential: %m"); |
| 353 |
} |
| 354 |
|
| 355 |
c->size_sum += l; |
| 356 |
c->n_credentials++; |
| 357 |
|
| 358 |
log_debug("Successfully processed kernel command line credential '%s'.", n); |
| 359 |
|
| 360 |
return 0; |
| 361 |
} |
| 362 |
|
| 363 |
static int import_credentials_proc_cmdline(ImportCredentialsContext *c) { |
| 364 |
int r; |
| 365 |
|
| 366 |
assert(c); |
| 367 |
|
| 368 |
r = proc_cmdline_parse(proc_cmdline_callback, c, 0); |
| 369 |
if (r < 0) |
| 370 |
return log_error_errno(r, "Failed to parse /proc/cmdline: %m"); |
| 371 |
|
| 372 |
return 0; |
| 373 |
} |
| 374 |
|
| 375 |
#define QEMU_FWCFG_PATH "/sys/firmware/qemu_fw_cfg/by_name/opt/io.systemd.credentials" |
| 376 |
|
| 377 |
static int import_credentials_qemu(ImportCredentialsContext *c) { |
| 378 |
_cleanup_free_ DirectoryEntries *de = NULL; |
| 379 |
_cleanup_close_ int source_dir_fd = -EBADF; |
| 380 |
int r; |
| 381 |
|
| 382 |
assert(c); |
| 383 |
|
| 384 |
if (detect_container() > 0) /* don't access /sys/ in a container */ |
| 385 |
return 0; |
| 386 |
|
| 387 |
if (detect_confidential_virtualization() > 0) /* don't trust firmware if confidential VMs */ |
| 388 |
return 0; |
| 389 |
|
| 390 |
source_dir_fd = open(QEMU_FWCFG_PATH, O_RDONLY|O_DIRECTORY|O_CLOEXEC); |
| 391 |
if (source_dir_fd < 0) { |
| 392 |
if (errno == ENOENT) { |
| 393 |
log_debug("No credentials passed via fw_cfg."); |
| 394 |
return 0; |
| 395 |
} |
| 396 |
|
| 397 |
log_warning_errno(errno, "Failed to open '" QEMU_FWCFG_PATH "', ignoring: %m"); |
| 398 |
return 0; |
| 399 |
} |
| 400 |
|
| 401 |
r = readdir_all(source_dir_fd, RECURSE_DIR_SORT|RECURSE_DIR_IGNORE_DOT, &de); |
| 402 |
if (r < 0) { |
| 403 |
log_warning_errno(r, "Failed to read '" QEMU_FWCFG_PATH "' contents, ignoring: %m"); |
| 404 |
return 0; |
| 405 |
} |
| 406 |
|
| 407 |
FOREACH_ARRAY(i, de->entries, de->n_entries) { |
| 408 |
const struct dirent *d = *i; |
| 409 |
_cleanup_close_ int vfd = -EBADF, rfd = -EBADF, nfd = -EBADF; |
| 410 |
_cleanup_free_ char *szs = NULL; |
| 411 |
uint64_t sz; |
| 412 |
|
| 413 |
if (!credential_name_valid(d->d_name)) { |
| 414 |
log_warning("Credential '%s' has invalid name, ignoring.", d->d_name); |
| 415 |
continue; |
| 416 |
} |
| 417 |
|
| 418 |
vfd = openat(source_dir_fd, d->d_name, O_RDONLY|O_DIRECTORY|O_CLOEXEC); |
| 419 |
if (vfd < 0) { |
| 420 |
log_warning_errno(errno, "Failed to open '" QEMU_FWCFG_PATH "'/%s/, ignoring: %m", d->d_name); |
| 421 |
continue; |
| 422 |
} |
| 423 |
|
| 424 |
r = read_virtual_file_at(vfd, "size", LINE_MAX, &szs, NULL); |
| 425 |
if (r < 0) { |
| 426 |
log_warning_errno(r, "Failed to read '" QEMU_FWCFG_PATH "'/%s/size, ignoring: %m", d->d_name); |
| 427 |
continue; |
| 428 |
} |
| 429 |
|
| 430 |
r = safe_atou64(strstrip(szs), &sz); |
| 431 |
if (r < 0) { |
| 432 |
log_warning_errno(r, "Failed to parse size of credential '%s', ignoring: %s", d->d_name, szs); |
| 433 |
continue; |
| 434 |
} |
| 435 |
|
| 436 |
if (!credential_size_ok(c, d->d_name, sz)) |
| 437 |
continue; |
| 438 |
|
| 439 |
/* Ideally we'd just symlink the data here. Alas the kernel driver exports the raw file as |
| 440 |
* having size zero, and we'd rather not have applications support such credential |
| 441 |
* files. Let's hence copy the files to make them regular. */ |
| 442 |
|
| 443 |
rfd = openat(vfd, "raw", O_RDONLY|O_CLOEXEC); |
| 444 |
if (rfd < 0) { |
| 445 |
log_warning_errno(errno, "Failed to open '" QEMU_FWCFG_PATH "'/%s/raw, ignoring: %m", d->d_name); |
| 446 |
continue; |
| 447 |
} |
| 448 |
|
| 449 |
r = acquire_credential_directory(c, SYSTEM_CREDENTIALS_DIRECTORY, /* with_mount= */ true); |
| 450 |
if (r < 0) |
| 451 |
return r; |
| 452 |
|
| 453 |
nfd = open_credential_file_for_write(c->target_dir_fd, SYSTEM_CREDENTIALS_DIRECTORY, d->d_name); |
| 454 |
if (nfd == -EEXIST) |
| 455 |
continue; |
| 456 |
if (nfd < 0) |
| 457 |
return nfd; |
| 458 |
|
| 459 |
r = copy_bytes(rfd, nfd, sz, 0); |
| 460 |
if (r < 0) { |
| 461 |
(void) unlinkat(c->target_dir_fd, d->d_name, 0); |
| 462 |
return log_error_errno(r, "Failed to create credential '%s': %m", d->d_name); |
| 463 |
} |
| 464 |
|
| 465 |
c->size_sum += sz; |
| 466 |
c->n_credentials++; |
| 467 |
|
| 468 |
log_debug("Successfully copied qemu fw_cfg credential '%s'.", d->d_name); |
| 469 |
} |
| 470 |
|
| 471 |
return 0; |
| 472 |
} |
| 473 |
|
| 474 |
static int parse_smbios_strings(ImportCredentialsContext *c, const char *data, size_t size) { |
| 475 |
size_t left, skip; |
| 476 |
const char *p; |
| 477 |
int r; |
| 478 |
|
| 479 |
assert(c); |
| 480 |
assert(data || size == 0); |
| 481 |
|
| 482 |
/* Unpacks a packed series of SMBIOS OEM vendor strings. These are a series of NUL terminated |
| 483 |
* strings, one after the other. */ |
| 484 |
|
| 485 |
for (p = data, left = size; left > 0; p += skip, left -= skip) { |
| 486 |
_cleanup_free_ void *buf = NULL; |
| 487 |
_cleanup_free_ char *cn = NULL; |
| 488 |
_cleanup_close_ int nfd = -EBADF; |
| 489 |
const char *nul, *n, *eq; |
| 490 |
const void *cdata; |
| 491 |
size_t buflen, cdata_len; |
| 492 |
bool unbase64; |
| 493 |
|
| 494 |
nul = memchr(p, 0, left); |
| 495 |
if (nul) |
| 496 |
skip = (nul - p) + 1; |
| 497 |
else { |
| 498 |
nul = p + left; |
| 499 |
skip = left; |
| 500 |
} |
| 501 |
|
| 502 |
if (nul - p == 0) /* Skip empty strings */ |
| 503 |
continue; |
| 504 |
|
| 505 |
/* Only care about strings starting with either of these two prefixes */ |
| 506 |
if ((n = memory_startswith(p, nul - p, "io.systemd.credential:"))) |
| 507 |
unbase64 = false; |
| 508 |
else if ((n = memory_startswith(p, nul - p, "io.systemd.credential.binary:"))) |
| 509 |
unbase64 = true; |
| 510 |
else { |
| 511 |
_cleanup_free_ char *escaped = NULL; |
| 512 |
|
| 513 |
escaped = cescape_length(p, nul - p); |
| 514 |
log_debug("Ignoring OEM string: %s", strnull(escaped)); |
| 515 |
continue; |
| 516 |
} |
| 517 |
|
| 518 |
eq = memchr(n, '=', nul - n); |
| 519 |
if (!eq) { |
| 520 |
log_warning("SMBIOS OEM string lacks '=' character, ignoring."); |
| 521 |
continue; |
| 522 |
} |
| 523 |
|
| 524 |
cn = memdup_suffix0(n, eq - n); |
| 525 |
if (!cn) |
| 526 |
return log_oom(); |
| 527 |
|
| 528 |
if (!credential_name_valid(cn)) { |
| 529 |
log_warning("SMBIOS credential name '%s' is not valid, ignoring.", cn); |
| 530 |
continue; |
| 531 |
} |
| 532 |
|
| 533 |
/* Optionally base64 decode the data, if requested, to allow binary credentials */ |
| 534 |
if (unbase64) { |
| 535 |
r = unbase64mem_full(eq + 1, nul - (eq + 1), /* secure= */ false, &buf, &buflen); |
| 536 |
if (r < 0) { |
| 537 |
log_warning_errno(r, "Failed to base64 decode credential '%s', ignoring: %m", cn); |
| 538 |
continue; |
| 539 |
} |
| 540 |
|
| 541 |
cdata = buf; |
| 542 |
cdata_len = buflen; |
| 543 |
} else { |
| 544 |
cdata = eq + 1; |
| 545 |
cdata_len = nul - (eq + 1); |
| 546 |
} |
| 547 |
|
| 548 |
if (!credential_size_ok(c, cn, cdata_len)) |
| 549 |
continue; |
| 550 |
|
| 551 |
r = acquire_credential_directory(c, SYSTEM_CREDENTIALS_DIRECTORY, /* with_mount= */ true); |
| 552 |
if (r < 0) |
| 553 |
return r; |
| 554 |
|
| 555 |
nfd = open_credential_file_for_write(c->target_dir_fd, SYSTEM_CREDENTIALS_DIRECTORY, cn); |
| 556 |
if (nfd == -EEXIST) |
| 557 |
continue; |
| 558 |
if (nfd < 0) |
| 559 |
return nfd; |
| 560 |
|
| 561 |
r = loop_write(nfd, cdata, cdata_len); |
| 562 |
if (r < 0) { |
| 563 |
(void) unlinkat(c->target_dir_fd, cn, 0); |
| 564 |
return log_error_errno(r, "Failed to write credential: %m"); |
| 565 |
} |
| 566 |
|
| 567 |
c->size_sum += cdata_len; |
| 568 |
c->n_credentials++; |
| 569 |
|
| 570 |
log_debug("Successfully processed SMBIOS credential '%s'.", cn); |
| 571 |
} |
| 572 |
|
| 573 |
return 0; |
| 574 |
} |
| 575 |
|
| 576 |
static int import_credentials_smbios(ImportCredentialsContext *c) { |
| 577 |
int r; |
| 578 |
|
| 579 |
assert(c); |
| 580 |
|
| 581 |
/* Parses DMI OEM strings fields (SMBIOS type 11), as settable with qemu's -smbios type=11,value=… switch. */ |
| 582 |
|
| 583 |
if (detect_container() > 0) /* don't access /sys/ in a container */ |
| 584 |
return 0; |
| 585 |
|
| 586 |
if (detect_confidential_virtualization() > 0) /* don't trust firmware if confidential VMs */ |
| 587 |
return 0; |
| 588 |
|
| 589 |
for (unsigned i = 0;; i++) { |
| 590 |
_cleanup_free_ char *data = NULL; |
| 591 |
size_t size; |
| 592 |
|
| 593 |
r = read_smbios11_field(i, CREDENTIALS_TOTAL_SIZE_MAX, &data, &size); |
| 594 |
if (r == -ENOENT) /* Once we reach ENOENT there are no more DMI Type 11 fields around. */ |
| 595 |
break; |
| 596 |
if (r < 0) { |
| 597 |
log_warning_errno(r, "Failed to read SMBIOS type #11 object %u, ignoring: %m", i); |
| 598 |
break; |
| 599 |
} |
| 600 |
|
| 601 |
r = parse_smbios_strings(c, data, size); |
| 602 |
if (r < 0) |
| 603 |
return r; |
| 604 |
|
| 605 |
if (i == UINT_MAX) /* Prevent overflow */ |
| 606 |
break; |
| 607 |
} |
| 608 |
|
| 609 |
return 0; |
| 610 |
} |
| 611 |
|
| 612 |
static int import_credentials_initrd(ImportCredentialsContext *c) { |
| 613 |
_cleanup_free_ DirectoryEntries *de = NULL; |
| 614 |
_cleanup_close_ int source_dir_fd = -EBADF; |
| 615 |
int r; |
| 616 |
|
| 617 |
assert(c); |
| 618 |
|
| 619 |
/* This imports credentials from /run/credentials/@initrd/ into our credentials directory and deletes |
| 620 |
* the source directory afterwards. This is run once after the initrd → host transition. This is |
| 621 |
* supposed to establish a well-defined avenue for initrd-based host configurators to pass |
| 622 |
* credentials into the main system. */ |
| 623 |
|
| 624 |
if (in_initrd()) |
| 625 |
return 0; |
| 626 |
|
| 627 |
source_dir_fd = open("/run/credentials/@initrd", O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW); |
| 628 |
if (source_dir_fd < 0) { |
| 629 |
if (errno == ENOENT) |
| 630 |
log_debug_errno(errno, "No credentials passed from initrd."); |
| 631 |
else |
| 632 |
log_warning_errno(errno, "Failed to open '%s', ignoring: %m", "/run/credentials/@initrd"); |
| 633 |
return 0; |
| 634 |
} |
| 635 |
|
| 636 |
r = readdir_all(source_dir_fd, RECURSE_DIR_SORT|RECURSE_DIR_IGNORE_DOT, &de); |
| 637 |
if (r < 0) { |
| 638 |
log_warning_errno(r, "Failed to read '/run/credentials/@initrd' contents, ignoring: %m"); |
| 639 |
return 0; |
| 640 |
} |
| 641 |
|
| 642 |
FOREACH_ARRAY(entry, de->entries, de->n_entries) { |
| 643 |
_cleanup_close_ int cfd = -EBADF, nfd = -EBADF; |
| 644 |
const struct dirent *d = *entry; |
| 645 |
struct stat st; |
| 646 |
|
| 647 |
if (!credential_name_valid(d->d_name)) { |
| 648 |
log_warning("Credential '%s' has invalid name, ignoring.", d->d_name); |
| 649 |
continue; |
| 650 |
} |
| 651 |
|
| 652 |
cfd = openat(source_dir_fd, d->d_name, O_RDONLY|O_CLOEXEC); |
| 653 |
if (cfd < 0) { |
| 654 |
log_warning_errno(errno, "Failed to open %s, ignoring: %m", d->d_name); |
| 655 |
continue; |
| 656 |
} |
| 657 |
|
| 658 |
if (fstat(cfd, &st) < 0) { |
| 659 |
log_warning_errno(errno, "Failed to stat %s, ignoring: %m", d->d_name); |
| 660 |
continue; |
| 661 |
} |
| 662 |
|
| 663 |
r = stat_verify_regular(&st); |
| 664 |
if (r < 0) { |
| 665 |
log_warning_errno(r, "Credential file %s is not a regular file, ignoring: %m", d->d_name); |
| 666 |
continue; |
| 667 |
} |
| 668 |
|
| 669 |
if (!credential_size_ok(c, d->d_name, st.st_size)) |
| 670 |
continue; |
| 671 |
|
| 672 |
r = acquire_credential_directory(c, SYSTEM_CREDENTIALS_DIRECTORY, /* with_mount= */ true); |
| 673 |
if (r < 0) |
| 674 |
return r; |
| 675 |
|
| 676 |
nfd = open_credential_file_for_write(c->target_dir_fd, SYSTEM_CREDENTIALS_DIRECTORY, d->d_name); |
| 677 |
if (nfd == -EEXIST) |
| 678 |
continue; |
| 679 |
if (nfd < 0) |
| 680 |
return nfd; |
| 681 |
|
| 682 |
r = copy_bytes(cfd, nfd, st.st_size, 0); |
| 683 |
if (r < 0) { |
| 684 |
(void) unlinkat(c->target_dir_fd, d->d_name, 0); |
| 685 |
return log_error_errno(r, "Failed to create credential '%s': %m", d->d_name); |
| 686 |
} |
| 687 |
|
| 688 |
c->size_sum += st.st_size; |
| 689 |
c->n_credentials++; |
| 690 |
|
| 691 |
log_debug("Successfully copied initrd credential '%s'.", d->d_name); |
| 692 |
|
| 693 |
(void) unlinkat(source_dir_fd, d->d_name, 0); |
| 694 |
} |
| 695 |
|
| 696 |
source_dir_fd = safe_close(source_dir_fd); |
| 697 |
|
| 698 |
if (rmdir("/run/credentials/@initrd") < 0) |
| 699 |
log_warning_errno(errno, "Failed to remove /run/credentials/@initrd after import, ignoring: %m"); |
| 700 |
|
| 701 |
return 0; |
| 702 |
} |
| 703 |
|
| 704 |
static int import_credentials_trusted(void) { |
| 705 |
_cleanup_(import_credentials_context_done) ImportCredentialsContext c = { |
| 706 |
.target_dir_fd = -EBADF, |
| 707 |
}; |
| 708 |
int r, ret = 0; |
| 709 |
|
| 710 |
/* This is invoked during early boot when no credentials have been imported so far. (Specifically, if |
| 711 |
* the $CREDENTIALS_DIRECTORY or $ENCRYPTED_CREDENTIALS_DIRECTORY environment variables are not set |
| 712 |
* yet.) */ |
| 713 |
|
| 714 |
RET_GATHER(ret, import_credentials_qemu(&c)); |
| 715 |
RET_GATHER(ret, import_credentials_smbios(&c)); |
| 716 |
RET_GATHER(ret, import_credentials_proc_cmdline(&c)); |
| 717 |
RET_GATHER(ret, import_credentials_initrd(&c)); |
| 718 |
|
| 719 |
if (c.n_credentials > 0) { |
| 720 |
log_debug("Imported %u credentials from kernel command line/smbios/fw_cfg/initrd.", c.n_credentials); |
| 721 |
|
| 722 |
r = finalize_credentials_dir(SYSTEM_CREDENTIALS_DIRECTORY, "CREDENTIALS_DIRECTORY"); |
| 723 |
if (r < 0) |
| 724 |
return r; |
| 725 |
} |
| 726 |
|
| 727 |
return ret; |
| 728 |
} |
| 729 |
|
| 730 |
static int merge_credentials_trusted(const char *creds_dir) { |
| 731 |
_cleanup_(import_credentials_context_done) ImportCredentialsContext c = { |
| 732 |
.target_dir_fd = -EBADF, |
| 733 |
}; |
| 734 |
int r; |
| 735 |
|
| 736 |
/* This is invoked after the initrd → host transitions, when credentials already have been imported, |
| 737 |
* but we might want to import some more from the initrd. */ |
| 738 |
|
| 739 |
if (in_initrd()) |
| 740 |
return 0; |
| 741 |
|
| 742 |
/* Do not try to merge initrd credentials into foreign credentials directories */ |
| 743 |
if (!path_equal(creds_dir, SYSTEM_CREDENTIALS_DIRECTORY)) { |
| 744 |
log_debug("Not importing initrd credentials, as foreign $CREDENTIALS_DIRECTORY has been set."); |
| 745 |
return 0; |
| 746 |
} |
| 747 |
|
| 748 |
r = import_credentials_initrd(&c); |
| 749 |
|
| 750 |
if (c.n_credentials > 0) { |
| 751 |
int z; |
| 752 |
|
| 753 |
log_debug("Merged %u credentials from initrd.", c.n_credentials); |
| 754 |
|
| 755 |
z = finalize_credentials_dir(SYSTEM_CREDENTIALS_DIRECTORY, "CREDENTIALS_DIRECTORY"); |
| 756 |
if (z < 0) |
| 757 |
return z; |
| 758 |
} |
| 759 |
|
| 760 |
return r; |
| 761 |
} |
| 762 |
|
| 763 |
static int symlink_credential_dir(const char *envvar, const char *path, const char *where) { |
| 764 |
int r; |
| 765 |
|
| 766 |
assert(envvar); |
| 767 |
assert(path); |
| 768 |
assert(where); |
| 769 |
|
| 770 |
if (!path_is_valid(path) || !path_is_absolute(path)) |
| 771 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "String specified via $%s is not a valid absolute path, refusing: %s", envvar, path); |
| 772 |
|
| 773 |
/* If the env var already points to where we intend to create the symlink, then most likely we |
| 774 |
* already imported some creds earlier, and thus set the env var, and hence don't need to do |
| 775 |
* anything. */ |
| 776 |
if (path_equal(path, where)) |
| 777 |
return 0; |
| 778 |
|
| 779 |
r = symlink_idempotent(path, where, /* make_relative= */ true); |
| 780 |
if (r < 0) |
| 781 |
return log_error_errno(r, "Failed to link $%s to %s: %m", envvar, where); |
| 782 |
|
| 783 |
return 0; |
| 784 |
} |
| 785 |
|
| 786 |
static int setenv_notify_socket(void) { |
| 787 |
_cleanup_free_ char *address = NULL; |
| 788 |
int r; |
| 789 |
|
| 790 |
r = read_credential_with_decryption("vmm.notify_socket", (void **)&address, /* ret_size= */ NULL); |
| 791 |
if (r < 0) |
| 792 |
return log_warning_errno(r, "Failed to read 'vmm.notify_socket' credential, ignoring: %m"); |
| 793 |
|
| 794 |
if (isempty(address)) |
| 795 |
return 0; |
| 796 |
|
| 797 |
if (setenv("NOTIFY_SOCKET", address, /* replace= */ 1) < 0) |
| 798 |
return log_warning_errno(errno, "Failed to set $NOTIFY_SOCKET environment variable, ignoring: %m"); |
| 799 |
|
| 800 |
return 1; |
| 801 |
} |
| 802 |
|
| 803 |
static int report_credentials_per_func(const char *title, int (*get_directory_func)(const char **ret)) { |
| 804 |
_cleanup_free_ DirectoryEntries *de = NULL; |
| 805 |
_cleanup_free_ char *ll = NULL; |
| 806 |
const char *d = NULL; |
| 807 |
int r, c = 0; |
| 808 |
|
| 809 |
assert(title); |
| 810 |
assert(get_directory_func); |
| 811 |
|
| 812 |
r = get_directory_func(&d); |
| 813 |
if (r == -ENXIO) /* Env var not set */ |
| 814 |
return 0; |
| 815 |
if (r < 0) |
| 816 |
return log_warning_errno(r, "Failed to determine %s directory: %m", title); |
| 817 |
|
| 818 |
r = readdir_all_at(AT_FDCWD, d, RECURSE_DIR_SORT|RECURSE_DIR_IGNORE_DOT, &de); |
| 819 |
if (r < 0) |
| 820 |
return log_warning_errno(r, "Failed to enumerate credentials directory %s: %m", d); |
| 821 |
|
| 822 |
FOREACH_ARRAY(entry, de->entries, de->n_entries) { |
| 823 |
const struct dirent *e = *entry; |
| 824 |
|
| 825 |
if (!credential_name_valid(e->d_name)) |
| 826 |
continue; |
| 827 |
|
| 828 |
if (!strextend_with_separator(&ll, ", ", e->d_name)) |
| 829 |
return log_oom(); |
| 830 |
|
| 831 |
c++; |
| 832 |
} |
| 833 |
|
| 834 |
if (ll) |
| 835 |
log_info("Received %s: %s", title, ll); |
| 836 |
|
| 837 |
return c; |
| 838 |
} |
| 839 |
|
| 840 |
static void report_credentials(void) { |
| 841 |
int p, q; |
| 842 |
|
| 843 |
p = report_credentials_per_func("regular credentials", get_credentials_dir); |
| 844 |
q = report_credentials_per_func("untrusted credentials", get_encrypted_credentials_dir); |
| 845 |
|
| 846 |
log_full(p > 0 || q > 0 ? LOG_INFO : LOG_DEBUG, |
| 847 |
"Acquired %i regular credentials, %i untrusted credentials.", |
| 848 |
p > 0 ? p : 0, |
| 849 |
q > 0 ? q : 0); |
| 850 |
} |
| 851 |
|
| 852 |
int import_credentials(void) { |
| 853 |
const char *received_creds_dir = NULL, *received_encrypted_creds_dir = NULL; |
| 854 |
bool envvar_set = false; |
| 855 |
int r; |
| 856 |
|
| 857 |
r = get_credentials_dir(&received_creds_dir); |
| 858 |
if (r < 0 && r != -ENXIO) /* ENXIO → env var not set yet */ |
| 859 |
log_warning_errno(r, "Failed to determine credentials directory, ignoring: %m"); |
| 860 |
|
| 861 |
envvar_set = r >= 0; |
| 862 |
|
| 863 |
r = get_encrypted_credentials_dir(&received_encrypted_creds_dir); |
| 864 |
if (r < 0 && r != -ENXIO) /* ENXIO → env var not set yet */ |
| 865 |
log_warning_errno(r, "Failed to determine encrypted credentials directory, ignoring: %m"); |
| 866 |
|
| 867 |
envvar_set = envvar_set || r >= 0; |
| 868 |
|
| 869 |
if (envvar_set) { |
| 870 |
/* Maybe an earlier stage initrd already set this up? If so, don't try to import anything again. */ |
| 871 |
log_debug("Not importing credentials, $CREDENTIALS_DIRECTORY or $ENCRYPTED_CREDENTIALS_DIRECTORY already set."); |
| 872 |
|
| 873 |
/* But, let's make sure the creds are available from our regular paths. */ |
| 874 |
if (received_creds_dir) |
| 875 |
r = symlink_credential_dir("CREDENTIALS_DIRECTORY", received_creds_dir, SYSTEM_CREDENTIALS_DIRECTORY); |
| 876 |
else |
| 877 |
r = 0; |
| 878 |
|
| 879 |
if (received_encrypted_creds_dir) |
| 880 |
RET_GATHER(r, symlink_credential_dir("ENCRYPTED_CREDENTIALS_DIRECTORY", |
| 881 |
received_encrypted_creds_dir, |
| 882 |
ENCRYPTED_SYSTEM_CREDENTIALS_DIRECTORY)); |
| 883 |
|
| 884 |
RET_GATHER(r, merge_credentials_trusted(received_creds_dir)); |
| 885 |
|
| 886 |
} else { |
| 887 |
bool import; |
| 888 |
|
| 889 |
r = proc_cmdline_get_bool("systemd.import_credentials", PROC_CMDLINE_STRIP_RD_PREFIX|PROC_CMDLINE_TRUE_WHEN_MISSING, &import); |
| 890 |
if (r < 0) |
| 891 |
log_debug_errno(r, "Failed to check systemd.import_credentials= kernel command line option, proceeding: %m"); |
| 892 |
else if (!import) { |
| 893 |
log_notice("systemd.import_credentials=no is set, skipping importing of credentials."); |
| 894 |
return 0; |
| 895 |
} |
| 896 |
|
| 897 |
r = import_credentials_boot(); |
| 898 |
RET_GATHER(r, import_credentials_trusted()); |
| 899 |
} |
| 900 |
|
| 901 |
report_credentials(); |
| 902 |
|
| 903 |
/* Propagate vmm_notify_socket credential → $NOTIFY_SOCKET env var */ |
| 904 |
(void) setenv_notify_socket(); |
| 905 |
|
| 906 |
return r; |
| 907 |
} |