| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <fcntl.h> |
| 4 |
#include <linux/magic.h> |
| 5 |
#include <sys/stat.h> |
| 6 |
#include <unistd.h> |
| 7 |
|
| 8 |
#include "alloc-util.h" |
| 9 |
#include "blockdev-util.h" |
| 10 |
#include "build-path.h" |
| 11 |
#include "chase.h" |
| 12 |
#include "device-util.h" |
| 13 |
#include "devnum-util.h" |
| 14 |
#include "dirent-util.h" |
| 15 |
#include "env-util.h" |
| 16 |
#include "errno-util.h" |
| 17 |
#include "fd-util.h" |
| 18 |
#include "fdisk-util.h" |
| 19 |
#include "fileio.h" |
| 20 |
#include "find-esp.h" |
| 21 |
#include "glyph-util.h" |
| 22 |
#include "gpt.h" |
| 23 |
#include "hexdecoct.h" |
| 24 |
#include "import-util.h" |
| 25 |
#include "iovec-util.h" |
| 26 |
#include "pidref.h" |
| 27 |
#include "process-util.h" |
| 28 |
#include "sort-util.h" |
| 29 |
#include "stat-util.h" |
| 30 |
#include "string-table.h" |
| 31 |
#include "strv.h" |
| 32 |
#include "sysupdate-cache.h" |
| 33 |
#include "sysupdate-instance.h" |
| 34 |
#include "sysupdate-partition.h" |
| 35 |
#include "sysupdate-pattern.h" |
| 36 |
#include "sysupdate-resource.h" |
| 37 |
#include "time-util.h" |
| 38 |
#include "utf8.h" |
| 39 |
|
| 40 |
void resource_destroy(Resource *rr) { |
| 41 |
assert(rr); |
| 42 |
|
| 43 |
free(rr->path); |
| 44 |
strv_free(rr->patterns); |
| 45 |
|
| 46 |
for (size_t i = 0; i < rr->n_instances; i++) |
| 47 |
instance_free(rr->instances[i]); |
| 48 |
free(rr->instances); |
| 49 |
} |
| 50 |
|
| 51 |
static int resource_add_instance( |
| 52 |
Resource *rr, |
| 53 |
const char *path, |
| 54 |
const InstanceMetadata *f, |
| 55 |
Instance **ret) { |
| 56 |
|
| 57 |
Instance *i; |
| 58 |
int r; |
| 59 |
|
| 60 |
assert(rr); |
| 61 |
assert(path); |
| 62 |
assert(f); |
| 63 |
assert(f->version); |
| 64 |
|
| 65 |
if (!GREEDY_REALLOC(rr->instances, rr->n_instances + 1)) |
| 66 |
return log_oom(); |
| 67 |
|
| 68 |
r = instance_new(rr, path, f, &i); |
| 69 |
if (r < 0) |
| 70 |
return r; |
| 71 |
|
| 72 |
rr->instances[rr->n_instances++] = i; |
| 73 |
|
| 74 |
if (ret) |
| 75 |
*ret = i; |
| 76 |
|
| 77 |
return 0; |
| 78 |
} |
| 79 |
|
| 80 |
static int resource_load_from_directory_recursive( |
| 81 |
Resource *rr, |
| 82 |
DIR* d, |
| 83 |
const char* relpath, |
| 84 |
const char* relpath_for_matching, |
| 85 |
mode_t m, |
| 86 |
bool ancestor_is_partial, |
| 87 |
bool ancestor_is_pending) { |
| 88 |
int r; |
| 89 |
|
| 90 |
for (;;) { |
| 91 |
_cleanup_(instance_metadata_destroy) InstanceMetadata extracted_fields = INSTANCE_METADATA_NULL; |
| 92 |
_cleanup_free_ char *joined = NULL, *rel_joined = NULL; |
| 93 |
_cleanup_free_ char *rel_joined_for_matching = NULL; |
| 94 |
Instance *instance; |
| 95 |
struct dirent *de; |
| 96 |
const char *de_d_name_stripped; |
| 97 |
struct stat st; |
| 98 |
bool is_partial = ancestor_is_partial, is_pending = ancestor_is_pending; |
| 99 |
const char *stripped; |
| 100 |
|
| 101 |
errno = 0; |
| 102 |
de = readdir_no_dot(d); |
| 103 |
if (!de) { |
| 104 |
if (errno != 0) |
| 105 |
return log_error_errno(errno, "Failed to read directory '%s': %m", rr->path); |
| 106 |
break; |
| 107 |
} |
| 108 |
|
| 109 |
switch (de->d_type) { |
| 110 |
|
| 111 |
case DT_UNKNOWN: |
| 112 |
break; |
| 113 |
|
| 114 |
case DT_DIR: |
| 115 |
if (!IN_SET(m, S_IFDIR, S_IFREG)) |
| 116 |
continue; |
| 117 |
|
| 118 |
break; |
| 119 |
|
| 120 |
case DT_REG: |
| 121 |
if (m != S_IFREG) |
| 122 |
continue; |
| 123 |
break; |
| 124 |
|
| 125 |
default: |
| 126 |
continue; |
| 127 |
} |
| 128 |
|
| 129 |
if (fstatat(dirfd(d), de->d_name, &st, AT_NO_AUTOMOUNT) < 0) { |
| 130 |
if (errno == ENOENT) /* Gone by now? */ |
| 131 |
continue; |
| 132 |
|
| 133 |
return log_error_errno(errno, "Failed to stat %s/%s: %m", rr->path, de->d_name); |
| 134 |
} |
| 135 |
|
| 136 |
if (!(S_ISDIR(st.st_mode) && S_ISREG(m)) && ((st.st_mode & S_IFMT) != m)) |
| 137 |
continue; |
| 138 |
|
| 139 |
if ((stripped = startswith(de->d_name, ".sysupdate.partial."))) { |
| 140 |
de_d_name_stripped = stripped; |
| 141 |
is_partial = true; |
| 142 |
is_pending = false; |
| 143 |
} else if ((stripped = startswith(de->d_name, ".sysupdate.pending."))) { |
| 144 |
de_d_name_stripped = stripped; |
| 145 |
is_pending = true; |
| 146 |
is_partial = false; |
| 147 |
} else |
| 148 |
de_d_name_stripped = de->d_name; |
| 149 |
|
| 150 |
rel_joined = path_join(relpath, de->d_name); |
| 151 |
if (!rel_joined) |
| 152 |
return log_oom(); |
| 153 |
|
| 154 |
/* Match against the filename with any `.sysupdate.partial.` (etc.) prefix stripped, so the |
| 155 |
* user’s patterns still apply. But don’t use the stripped version in any paths or recursion. */ |
| 156 |
rel_joined_for_matching = path_join(relpath_for_matching, de_d_name_stripped); |
| 157 |
if (!rel_joined_for_matching) |
| 158 |
return log_oom(); |
| 159 |
|
| 160 |
r = pattern_match_many(rr->patterns, rel_joined_for_matching, &extracted_fields); |
| 161 |
if (r == PATTERN_MATCH_RETRY) { |
| 162 |
_cleanup_closedir_ DIR *subdir = NULL; |
| 163 |
|
| 164 |
subdir = xopendirat(dirfd(d), rel_joined, 0); |
| 165 |
if (!subdir) |
| 166 |
continue; |
| 167 |
|
| 168 |
r = resource_load_from_directory_recursive(rr, subdir, rel_joined, rel_joined_for_matching, m, is_partial, is_pending); |
| 169 |
if (r < 0) |
| 170 |
return r; |
| 171 |
if (r == 0) |
| 172 |
continue; |
| 173 |
} |
| 174 |
else if (r < 0) |
| 175 |
return log_error_errno(r, "Failed to match pattern: %m"); |
| 176 |
else if (r == PATTERN_MATCH_NO) |
| 177 |
continue; |
| 178 |
|
| 179 |
if (de->d_type == DT_DIR && m != S_IFDIR) |
| 180 |
continue; |
| 181 |
|
| 182 |
joined = path_join(rr->path, rel_joined); |
| 183 |
if (!joined) |
| 184 |
return log_oom(); |
| 185 |
|
| 186 |
r = resource_add_instance(rr, joined, &extracted_fields, &instance); |
| 187 |
if (r < 0) |
| 188 |
return r; |
| 189 |
|
| 190 |
/* Inherit these from the source, if not explicitly overwritten */ |
| 191 |
if (instance->metadata.mtime == USEC_INFINITY) |
| 192 |
instance->metadata.mtime = timespec_load(&st.st_mtim) ?: USEC_INFINITY; |
| 193 |
|
| 194 |
if (instance->metadata.mode == MODE_INVALID) |
| 195 |
instance->metadata.mode = st.st_mode & 0775; /* mask out world-writability and suid and stuff, for safety */ |
| 196 |
|
| 197 |
/* Can’t be both partial and pending. */ |
| 198 |
assert(!(is_partial && is_pending)); |
| 199 |
|
| 200 |
instance->is_partial = is_partial; |
| 201 |
instance->is_pending = is_pending; |
| 202 |
} |
| 203 |
|
| 204 |
return 0; |
| 205 |
} |
| 206 |
|
| 207 |
static int resource_load_from_directory( |
| 208 |
Resource *rr, |
| 209 |
mode_t m) { |
| 210 |
_cleanup_closedir_ DIR *d = NULL; |
| 211 |
|
| 212 |
assert(rr); |
| 213 |
assert(IN_SET(rr->type, RESOURCE_TAR, RESOURCE_REGULAR_FILE, RESOURCE_DIRECTORY, RESOURCE_SUBVOLUME)); |
| 214 |
assert(IN_SET(m, S_IFREG, S_IFDIR)); |
| 215 |
|
| 216 |
d = opendir(rr->path); |
| 217 |
if (!d) { |
| 218 |
if (errno == ENOENT) { |
| 219 |
log_debug_errno(errno, "Directory %s does not exist, not loading any resources: %m", rr->path); |
| 220 |
return 0; |
| 221 |
} |
| 222 |
|
| 223 |
return log_error_errno(errno, "Failed to open directory '%s': %m", rr->path); |
| 224 |
} |
| 225 |
|
| 226 |
return resource_load_from_directory_recursive(rr, d, NULL, NULL, m, false, false); |
| 227 |
} |
| 228 |
|
| 229 |
static int resource_load_from_blockdev(Resource *rr) { |
| 230 |
_cleanup_(fdisk_unref_contextp) struct fdisk_context *c = NULL; |
| 231 |
_cleanup_(fdisk_unref_tablep) struct fdisk_table *t = NULL; |
| 232 |
size_t n_partitions; |
| 233 |
int r; |
| 234 |
|
| 235 |
assert(rr); |
| 236 |
|
| 237 |
r = DLOPEN_FDISK(LOG_DEBUG, SD_ELF_NOTE_DLOPEN_PRIORITY_RECOMMENDED); |
| 238 |
if (r < 0) |
| 239 |
return r; |
| 240 |
|
| 241 |
r = fdisk_new_context_at(AT_FDCWD, rr->path, /* read_only= */ true, /* sector_size= */ UINT32_MAX, &c); |
| 242 |
if (r < 0) |
| 243 |
return log_error_errno(r, "Failed to create fdisk context from '%s': %m", rr->path); |
| 244 |
|
| 245 |
if (!sym_fdisk_is_labeltype(c, FDISK_DISKLABEL_GPT)) |
| 246 |
return log_error_errno(SYNTHETIC_ERRNO(EHWPOISON), "Disk %s has no GPT disk label, not suitable.", rr->path); |
| 247 |
|
| 248 |
r = sym_fdisk_get_partitions(c, &t); |
| 249 |
if (r < 0) |
| 250 |
return log_error_errno(r, "Failed to acquire partition table: %m"); |
| 251 |
|
| 252 |
n_partitions = sym_fdisk_table_get_nents(t); |
| 253 |
for (size_t i = 0; i < n_partitions; i++) { |
| 254 |
_cleanup_(instance_metadata_destroy) InstanceMetadata extracted_fields = INSTANCE_METADATA_NULL; |
| 255 |
_cleanup_(partition_info_destroy) PartitionInfo pinfo = PARTITION_INFO_NULL; |
| 256 |
Instance *instance; |
| 257 |
bool is_partial = false, is_pending = false; |
| 258 |
|
| 259 |
r = read_partition_info(c, t, i, &pinfo); |
| 260 |
if (r < 0) |
| 261 |
return r; |
| 262 |
if (r == 0) /* not assigned */ |
| 263 |
continue; |
| 264 |
|
| 265 |
/* Check if partition type matches, either directly or via derived partial/pending type |
| 266 |
* UUIDs. The derived UUIDs are computed from the configured partition type by hashing it |
| 267 |
* with a fixed app-specific ID, so we can detect the state without relying on label |
| 268 |
* prefixes. */ |
| 269 |
if (rr->partition_type_set) { |
| 270 |
sd_id128_t partial_type, pending_type; |
| 271 |
|
| 272 |
r = gpt_partition_type_uuid_for_sysupdate_partial(rr->partition_type.uuid, &partial_type); |
| 273 |
if (r < 0) |
| 274 |
return log_error_errno(r, "Failed to derive partial partition type UUID: %m"); |
| 275 |
|
| 276 |
r = gpt_partition_type_uuid_for_sysupdate_pending(rr->partition_type.uuid, &pending_type); |
| 277 |
if (r < 0) |
| 278 |
return log_error_errno(r, "Failed to derive pending partition type UUID: %m"); |
| 279 |
|
| 280 |
if (sd_id128_equal(pinfo.type, partial_type)) |
| 281 |
is_partial = true; |
| 282 |
else if (sd_id128_equal(pinfo.type, pending_type)) |
| 283 |
is_pending = true; |
| 284 |
else if (!sd_id128_equal(pinfo.type, rr->partition_type.uuid)) |
| 285 |
continue; |
| 286 |
} |
| 287 |
|
| 288 |
/* A label of "_empty" means "not used so far" for us */ |
| 289 |
if (streq_ptr(pinfo.label, "_empty")) { |
| 290 |
rr->n_empty++; |
| 291 |
continue; |
| 292 |
} |
| 293 |
|
| 294 |
r = pattern_match_many(rr->patterns, pinfo.label, &extracted_fields); |
| 295 |
if (r < 0) |
| 296 |
return log_error_errno(r, "Failed to match pattern: %m"); |
| 297 |
if (IN_SET(r, PATTERN_MATCH_NO, PATTERN_MATCH_RETRY)) |
| 298 |
continue; |
| 299 |
|
| 300 |
r = resource_add_instance(rr, pinfo.device, &extracted_fields, &instance); |
| 301 |
if (r < 0) |
| 302 |
return r; |
| 303 |
|
| 304 |
instance->partition_info = pinfo; |
| 305 |
pinfo = (PartitionInfo) PARTITION_INFO_NULL; |
| 306 |
|
| 307 |
/* Inherit data from source if not configured explicitly */ |
| 308 |
if (!instance->metadata.partition_uuid_set) { |
| 309 |
instance->metadata.partition_uuid = instance->partition_info.uuid; |
| 310 |
instance->metadata.partition_uuid_set = true; |
| 311 |
} |
| 312 |
|
| 313 |
if (!instance->metadata.partition_flags_set) { |
| 314 |
instance->metadata.partition_flags = instance->partition_info.flags; |
| 315 |
instance->metadata.partition_flags_set = true; |
| 316 |
} |
| 317 |
|
| 318 |
if (instance->metadata.read_only < 0) |
| 319 |
instance->metadata.read_only = instance->partition_info.read_only; |
| 320 |
|
| 321 |
/* Can’t be both partial and pending. */ |
| 322 |
assert(!(is_partial && is_pending)); |
| 323 |
|
| 324 |
instance->is_partial = is_partial; |
| 325 |
instance->is_pending = is_pending; |
| 326 |
} |
| 327 |
|
| 328 |
return 0; |
| 329 |
} |
| 330 |
|
| 331 |
static int download_manifest( |
| 332 |
const char *url, |
| 333 |
bool verify_signature, |
| 334 |
char **ret_buffer, |
| 335 |
size_t *ret_size) { |
| 336 |
|
| 337 |
_cleanup_free_ char *buffer = NULL, *suffixed_url = NULL; |
| 338 |
_cleanup_close_pair_ int pfd[2] = EBADF_PAIR; |
| 339 |
_cleanup_fclose_ FILE *manifest = NULL; |
| 340 |
size_t size = 0; |
| 341 |
int r; |
| 342 |
|
| 343 |
assert(url); |
| 344 |
assert(ret_buffer); |
| 345 |
assert(ret_size); |
| 346 |
|
| 347 |
/* Download a SHA256SUMS file as manifest */ |
| 348 |
|
| 349 |
r = import_url_append_component(url, "SHA256SUMS", &suffixed_url); |
| 350 |
if (r < 0) |
| 351 |
return log_error_errno(r, "Failed to append SHA256SUMS to URL: %m"); |
| 352 |
|
| 353 |
if (pipe2(pfd, O_CLOEXEC) < 0) |
| 354 |
return log_error_errno(errno, "Failed to allocate pipe: %m"); |
| 355 |
|
| 356 |
log_info("%s Acquiring manifest file %s%s", glyph(GLYPH_DOWNLOAD), |
| 357 |
suffixed_url, glyph(GLYPH_ELLIPSIS)); |
| 358 |
|
| 359 |
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; |
| 360 |
r = pidref_safe_fork_full( |
| 361 |
"(sd-pull)", |
| 362 |
(int[]) { -EBADF, pfd[1], STDERR_FILENO }, |
| 363 |
NULL, 0, |
| 364 |
FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG_SIGTERM|FORK_REARRANGE_STDIO|FORK_LOG, |
| 365 |
&pidref); |
| 366 |
if (r < 0) |
| 367 |
return r; |
| 368 |
if (r == 0) { |
| 369 |
/* Child */ |
| 370 |
|
| 371 |
const char *cmdline[] = { |
| 372 |
SYSTEMD_PULL_PATH, |
| 373 |
"raw", |
| 374 |
"--direct", /* just download the specified URL, don't download anything else */ |
| 375 |
"--verify", verify_signature ? "signature" : "no", /* verify the manifest file */ |
| 376 |
suffixed_url, |
| 377 |
"-", /* write to stdout */ |
| 378 |
NULL |
| 379 |
}; |
| 380 |
|
| 381 |
r = invoke_callout_binary(SYSTEMD_PULL_PATH, (char *const*) cmdline); |
| 382 |
log_error_errno(r, "Failed to execute %s tool: %m", SYSTEMD_PULL_PATH); |
| 383 |
_exit(EXIT_FAILURE); |
| 384 |
}; |
| 385 |
|
| 386 |
pfd[1] = safe_close(pfd[1]); |
| 387 |
|
| 388 |
/* We'll first load the entire manifest into memory before parsing it. That's because the |
| 389 |
* systemd-pull tool can validate the download only after its completion, but still pass the data to |
| 390 |
* us as it runs. We thus need to check the return value of the process *before* parsing, to be |
| 391 |
* reasonably safe. */ |
| 392 |
|
| 393 |
manifest = fdopen(pfd[0], "r"); |
| 394 |
if (!manifest) |
| 395 |
return log_error_errno(errno, "Failed to allocate FILE object for manifest file: %m"); |
| 396 |
|
| 397 |
TAKE_FD(pfd[0]); |
| 398 |
|
| 399 |
r = read_full_stream(manifest, &buffer, &size); |
| 400 |
if (r < 0) |
| 401 |
return log_error_errno(r, "Failed to read manifest file from child: %m"); |
| 402 |
|
| 403 |
manifest = safe_fclose(manifest); |
| 404 |
|
| 405 |
r = pidref_wait_for_terminate_and_check("(sd-pull)", &pidref, WAIT_LOG); |
| 406 |
if (r < 0) |
| 407 |
return r; |
| 408 |
if (r != 0) |
| 409 |
return -EPROTO; |
| 410 |
|
| 411 |
*ret_buffer = TAKE_PTR(buffer); |
| 412 |
*ret_size = size; |
| 413 |
|
| 414 |
return 0; |
| 415 |
} |
| 416 |
|
| 417 |
static int process_magic_file( |
| 418 |
const char *fn, |
| 419 |
const struct iovec *hash) { |
| 420 |
|
| 421 |
int r; |
| 422 |
|
| 423 |
assert(fn); |
| 424 |
assert(iovec_is_set(hash)); |
| 425 |
|
| 426 |
/* Validates "BEST-BEFORE-*" magic files we find in SHA256SUMS manifests. For now we ignore the |
| 427 |
* contents of such files (which might change one day), and only look at the file name. |
| 428 |
* |
| 429 |
* Note that if multiple BEST-BEFORE-* files exist in the same listing we'll honour them all, and |
| 430 |
* fail whenever *any* of them indicate a date that's already in the past. */ |
| 431 |
|
| 432 |
const char *e = startswith(fn, "BEST-BEFORE-"); |
| 433 |
if (!e) |
| 434 |
return 0; |
| 435 |
|
| 436 |
/* SHA256 hash of an empty file */ |
| 437 |
static const uint8_t expected_hash[] = { |
| 438 |
0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24, |
| 439 |
0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, |
| 440 |
}; |
| 441 |
|
| 442 |
/* Even if we ignore if people have non-empty files for this file, let's nonetheless warn about it, |
| 443 |
* so that people fix it. After all we want to retain liberty to maybe one day place some useful data |
| 444 |
* inside it */ |
| 445 |
if (!iovec_equal(&IOVEC_MAKE(expected_hash, sizeof(expected_hash)), hash)) |
| 446 |
log_warning("Hash of best before marker file '%s' has unexpected value, proceeding anyway.", fn); |
| 447 |
|
| 448 |
usec_t best_before; |
| 449 |
r = parse_calendar_date(e, &best_before); |
| 450 |
if (r < 0) { |
| 451 |
log_warning_errno(r, "Found best before marker with an invalid date, ignoring: %s", fn); |
| 452 |
return 0; |
| 453 |
} |
| 454 |
|
| 455 |
usec_t nw = now(CLOCK_REALTIME); |
| 456 |
if (best_before < nw) { |
| 457 |
/* We are past the best before date! Yikes! */ |
| 458 |
|
| 459 |
r = secure_getenv_bool("SYSTEMD_SYSUPDATE_VERIFY_FRESHNESS"); |
| 460 |
if (r < 0 && r != -ENXIO) |
| 461 |
log_debug_errno(r, "Failed to parse $SYSTEMD_SYSUPDATE_VERIFY_FRESHNESS, ignoring: %m"); |
| 462 |
|
| 463 |
if (r == 0) { |
| 464 |
log_warning("Best before marker indicates out-of-date file list, but told to ignore this, hence ignoring (%s < %s).", |
| 465 |
FORMAT_TIMESTAMP(best_before), FORMAT_TIMESTAMP(nw)); |
| 466 |
return 1; /* we processed this line, don't use for pattern matching */ |
| 467 |
} |
| 468 |
|
| 469 |
return log_error_errno( |
| 470 |
SYNTHETIC_ERRNO(ESTALE), |
| 471 |
"Best before marker indicates out-of-date file list, refusing (%s < %s).", |
| 472 |
FORMAT_TIMESTAMP(best_before), FORMAT_TIMESTAMP(nw)); |
| 473 |
} |
| 474 |
|
| 475 |
log_info("Found best before marker, and it checks out, proceeding."); |
| 476 |
return 1; /* we processed this line, don't use for pattern matching */ |
| 477 |
} |
| 478 |
|
| 479 |
static int resource_load_from_web( |
| 480 |
Resource *rr, |
| 481 |
bool verify, |
| 482 |
Hashmap **web_cache) { |
| 483 |
|
| 484 |
size_t manifest_size = 0, left = 0; |
| 485 |
_cleanup_free_ char *buf = NULL; |
| 486 |
const char *manifest, *p; |
| 487 |
size_t line_nr = 1; |
| 488 |
WebCacheItem *ci; |
| 489 |
int r; |
| 490 |
|
| 491 |
assert(rr); |
| 492 |
POINTER_MAY_BE_NULL(web_cache); |
| 493 |
|
| 494 |
ci = web_cache ? web_cache_get_item(*web_cache, rr->path, verify) : NULL; |
| 495 |
if (ci) { |
| 496 |
log_debug("Manifest web cache hit for %s.", rr->path); |
| 497 |
|
| 498 |
manifest = (char*) ci->data; |
| 499 |
manifest_size = ci->size; |
| 500 |
} else { |
| 501 |
log_debug("Manifest web cache miss for %s.", rr->path); |
| 502 |
|
| 503 |
r = download_manifest(rr->path, verify, &buf, &manifest_size); |
| 504 |
if (r < 0) |
| 505 |
return r; |
| 506 |
|
| 507 |
manifest = buf; |
| 508 |
} |
| 509 |
|
| 510 |
if (memchr(manifest, 0, manifest_size)) |
| 511 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Manifest file has embedded NUL byte, refusing."); |
| 512 |
if (!utf8_is_valid_n(manifest, manifest_size)) |
| 513 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Manifest file is not valid UTF-8, refusing."); |
| 514 |
|
| 515 |
p = manifest; |
| 516 |
left = manifest_size; |
| 517 |
|
| 518 |
while (left > 0) { |
| 519 |
_cleanup_(instance_metadata_destroy) InstanceMetadata extracted_fields = INSTANCE_METADATA_NULL; |
| 520 |
_cleanup_(iovec_done) struct iovec h = {}; |
| 521 |
_cleanup_free_ char *fn = NULL; |
| 522 |
Instance *instance; |
| 523 |
const char *e; |
| 524 |
|
| 525 |
/* 64 character hash + separator + filename + newline */ |
| 526 |
if (left < 67) |
| 527 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Corrupt manifest at line %zu, refusing.", line_nr); |
| 528 |
|
| 529 |
if (p[0] == '\\') |
| 530 |
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "File names with escapes not supported in manifest at line %zu, refusing.", line_nr); |
| 531 |
|
| 532 |
r = unhexmem_full(p, 64, /* secure= */ false, &h.iov_base, &h.iov_len); |
| 533 |
if (r < 0) |
| 534 |
return log_error_errno(r, "Failed to parse digest at manifest line %zu, refusing.", line_nr); |
| 535 |
|
| 536 |
p += 64, left -= 64; |
| 537 |
|
| 538 |
if (*p != ' ') |
| 539 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Missing space separator at manifest line %zu, refusing.", line_nr); |
| 540 |
p++, left--; |
| 541 |
|
| 542 |
if (!IN_SET(*p, '*', ' ')) |
| 543 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Missing binary/text input marker at manifest line %zu, refusing.", line_nr); |
| 544 |
p++, left--; |
| 545 |
|
| 546 |
e = memchr(p, '\n', left); |
| 547 |
if (!e) |
| 548 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Truncated manifest file at line %zu, refusing.", line_nr); |
| 549 |
if (e == p) |
| 550 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Empty filename specified at manifest line %zu, refusing.", line_nr); |
| 551 |
|
| 552 |
fn = strndup(p, e - p); |
| 553 |
if (!fn) |
| 554 |
return log_oom(); |
| 555 |
|
| 556 |
if (!filename_is_valid(fn)) |
| 557 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid filename specified at manifest line %zu, refusing.", line_nr); |
| 558 |
if (string_has_cc(fn, NULL)) |
| 559 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Filename contains control characters at manifest line %zu, refusing.", line_nr); |
| 560 |
|
| 561 |
r = process_magic_file(fn, &h); |
| 562 |
if (r < 0) |
| 563 |
return r; |
| 564 |
if (r == 0) { |
| 565 |
/* If this isn't a magic file, then do the pattern matching */ |
| 566 |
|
| 567 |
r = pattern_match_many(rr->patterns, fn, &extracted_fields); |
| 568 |
if (r < 0) |
| 569 |
return log_error_errno(r, "Failed to match pattern: %m"); |
| 570 |
if (r == PATTERN_MATCH_YES) { |
| 571 |
_cleanup_free_ char *path = NULL; |
| 572 |
|
| 573 |
r = import_url_append_component(rr->path, fn, &path); |
| 574 |
if (r < 0) |
| 575 |
return log_error_errno(r, "Failed to build instance URL: %m"); |
| 576 |
|
| 577 |
r = resource_add_instance(rr, path, &extracted_fields, &instance); |
| 578 |
if (r < 0) |
| 579 |
return r; |
| 580 |
|
| 581 |
assert(h.iov_len == sizeof(instance->metadata.sha256sum)); |
| 582 |
|
| 583 |
if (instance->metadata.sha256sum_set) { |
| 584 |
if (memcmp(instance->metadata.sha256sum, h.iov_base, h.iov_len) != 0) |
| 585 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "SHA256 sum parsed from filename and manifest don't match at line %zu, refusing.", line_nr); |
| 586 |
} else { |
| 587 |
memcpy(instance->metadata.sha256sum, h.iov_base, h.iov_len); |
| 588 |
instance->metadata.sha256sum_set = true; |
| 589 |
} |
| 590 |
|
| 591 |
/* Web resources can only be a source, not a target, so |
| 592 |
* can never be partial or pending. */ |
| 593 |
instance->is_partial = false; |
| 594 |
instance->is_pending = false; |
| 595 |
} |
| 596 |
} |
| 597 |
|
| 598 |
left -= (e - p) + 1; |
| 599 |
p = e + 1; |
| 600 |
|
| 601 |
line_nr++; |
| 602 |
} |
| 603 |
|
| 604 |
if (!ci && web_cache) { |
| 605 |
r = web_cache_add_item(web_cache, rr->path, verify, manifest, manifest_size); |
| 606 |
if (r < 0) |
| 607 |
log_debug_errno(r, "Failed to add manifest '%s' to cache, ignoring: %m", rr->path); |
| 608 |
else |
| 609 |
log_debug("Added manifest '%s' to cache.", rr->path); |
| 610 |
} |
| 611 |
|
| 612 |
return 0; |
| 613 |
} |
| 614 |
|
| 615 |
static int instance_cmp(Instance *const*a, Instance *const*b) { |
| 616 |
int r; |
| 617 |
|
| 618 |
assert(a); |
| 619 |
assert(b); |
| 620 |
assert(*a); |
| 621 |
assert(*b); |
| 622 |
assert((*a)->metadata.version); |
| 623 |
assert((*b)->metadata.version); |
| 624 |
|
| 625 |
/* Newest version at the beginning */ |
| 626 |
r = strverscmp_improved((*a)->metadata.version, (*b)->metadata.version); |
| 627 |
if (r != 0) |
| 628 |
return -r; |
| 629 |
|
| 630 |
/* Instances don't have to be uniquely named (uniqueness on partition tables is not enforced at all, |
| 631 |
* and since we allow multiple matching patterns not even in directories they are unique). Hence |
| 632 |
* let's order by path as secondary ordering key. */ |
| 633 |
return path_compare((*a)->path, (*b)->path); |
| 634 |
} |
| 635 |
|
| 636 |
int resource_load_instances(Resource *rr, bool verify, Hashmap **web_cache) { |
| 637 |
int r; |
| 638 |
|
| 639 |
assert(rr); |
| 640 |
|
| 641 |
switch (rr->type) { |
| 642 |
|
| 643 |
case RESOURCE_TAR: |
| 644 |
case RESOURCE_REGULAR_FILE: |
| 645 |
r = resource_load_from_directory(rr, S_IFREG); |
| 646 |
break; |
| 647 |
|
| 648 |
case RESOURCE_DIRECTORY: |
| 649 |
case RESOURCE_SUBVOLUME: |
| 650 |
r = resource_load_from_directory(rr, S_IFDIR); |
| 651 |
break; |
| 652 |
|
| 653 |
case RESOURCE_PARTITION: |
| 654 |
r = resource_load_from_blockdev(rr); |
| 655 |
break; |
| 656 |
|
| 657 |
case RESOURCE_URL_FILE: |
| 658 |
case RESOURCE_URL_TAR: |
| 659 |
r = resource_load_from_web(rr, verify, web_cache); |
| 660 |
break; |
| 661 |
|
| 662 |
default: |
| 663 |
assert_not_reached(); |
| 664 |
} |
| 665 |
if (r < 0) |
| 666 |
return r; |
| 667 |
|
| 668 |
typesafe_qsort(rr->instances, rr->n_instances, instance_cmp); |
| 669 |
return 0; |
| 670 |
} |
| 671 |
|
| 672 |
static int instance_version_match(Instance *const*a, Instance *const*b) { |
| 673 |
assert(a); |
| 674 |
assert(b); |
| 675 |
assert(*a); |
| 676 |
assert(*b); |
| 677 |
assert((*a)->metadata.version); |
| 678 |
assert((*b)->metadata.version); |
| 679 |
|
| 680 |
/* List is sorted newest-to-oldest */ |
| 681 |
return -strverscmp_improved((*a)->metadata.version, (*b)->metadata.version); |
| 682 |
} |
| 683 |
|
| 684 |
Instance* resource_find_instance(Resource *rr, const char *version) { |
| 685 |
Instance key = { |
| 686 |
.metadata.version = (char*) version, |
| 687 |
}, *k = &key; |
| 688 |
|
| 689 |
Instance **found; |
| 690 |
found = typesafe_bsearch(&k, rr->instances, rr->n_instances, instance_version_match); |
| 691 |
if (!found) |
| 692 |
return NULL; |
| 693 |
|
| 694 |
return *found; |
| 695 |
} |
| 696 |
|
| 697 |
static int get_sysext_overlay_block(const char *p, dev_t *ret) { |
| 698 |
int r; |
| 699 |
|
| 700 |
assert(p); |
| 701 |
assert(ret); |
| 702 |
|
| 703 |
/* Tries to read the backing device information systemd-sysext puts in the virtual file |
| 704 |
* /usr/.systemd-sysext/backing */ |
| 705 |
|
| 706 |
_cleanup_free_ char *j = path_join(p, ".systemd-sysext"); |
| 707 |
if (!j) |
| 708 |
return log_oom_debug(); |
| 709 |
|
| 710 |
_cleanup_close_ int fd = open(j, O_RDONLY|O_DIRECTORY); |
| 711 |
if (fd < 0) |
| 712 |
return log_debug_errno(errno, "Failed to open '%s': %m", j); |
| 713 |
|
| 714 |
r = fd_is_fs_type(fd, OVERLAYFS_SUPER_MAGIC); |
| 715 |
if (r < 0) |
| 716 |
return log_debug_errno(r, "Failed to determine backing file system of '%s': %m", j); |
| 717 |
if (r == 0) |
| 718 |
return log_debug_errno(SYNTHETIC_ERRNO(ENOTTY), "Backing file system of '%s' is not an overlayfs.", j); |
| 719 |
|
| 720 |
_cleanup_free_ char *buf = NULL; |
| 721 |
r = read_one_line_file_at(fd, "backing", &buf); |
| 722 |
if (r < 0) |
| 723 |
return log_debug_errno(r, "Failed to read contents of '%s/backing': %m", j); |
| 724 |
|
| 725 |
r = parse_devnum(buf, ret); |
| 726 |
if (r < 0) |
| 727 |
return log_debug_errno(r, "Failed to parse contents of '%s/backing': %m", j); |
| 728 |
|
| 729 |
if (major(*ret) == 0) { /* not a block device? */ |
| 730 |
*ret = 0; |
| 731 |
return 0; |
| 732 |
} |
| 733 |
|
| 734 |
(void) block_get_originating(*ret, ret, /* recursive= */ false); |
| 735 |
return 1; |
| 736 |
} |
| 737 |
|
| 738 |
int resource_resolve_path( |
| 739 |
Resource *rr, |
| 740 |
const char *root, |
| 741 |
const char *relative_to_directory, |
| 742 |
const char *node) { |
| 743 |
|
| 744 |
_cleanup_free_ char *p = NULL; |
| 745 |
dev_t d; |
| 746 |
int r; |
| 747 |
|
| 748 |
assert(rr); |
| 749 |
|
| 750 |
if (IN_SET(rr->path_relative_to, PATH_RELATIVE_TO_ESP, PATH_RELATIVE_TO_XBOOTLDR, PATH_RELATIVE_TO_BOOT) && |
| 751 |
!IN_SET(rr->type, RESOURCE_REGULAR_FILE, RESOURCE_DIRECTORY)) |
| 752 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 753 |
"Paths relative to %s are only allowed for regular-file or directory resources.", |
| 754 |
path_relative_to_to_string(rr->path_relative_to)); |
| 755 |
|
| 756 |
if (rr->path_auto) { |
| 757 |
/* NB: If the root mount has been replaced by some form of volatile file system (overlayfs), |
| 758 |
* the original root block device node is symlinked in /run/systemd/volatile-root. Let's |
| 759 |
* follow that link here. If that doesn't exist, we check the backing device of "/usr". We |
| 760 |
* don't actually check the backing device of the root fs "/", in order to support |
| 761 |
* environments where the root fs is a tmpfs, and the OS itself placed exclusively in |
| 762 |
* /usr/. */ |
| 763 |
|
| 764 |
if (rr->type != RESOURCE_PARTITION) |
| 765 |
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), |
| 766 |
"Automatic root path discovery only supported for partition resources."); |
| 767 |
|
| 768 |
if (node) { /* If --image= is specified, directly use the loopback device */ |
| 769 |
r = free_and_strdup_warn(&rr->path, node); |
| 770 |
if (r < 0) |
| 771 |
return r; |
| 772 |
|
| 773 |
return 0; |
| 774 |
} |
| 775 |
|
| 776 |
if (root) |
| 777 |
return log_error_errno(SYNTHETIC_ERRNO(EPERM), |
| 778 |
"Block device is not allowed when using --root= mode."); |
| 779 |
|
| 780 |
struct stat orig_root_stats; |
| 781 |
r = RET_NERRNO(stat("/run/systemd/volatile-root", &orig_root_stats)); |
| 782 |
if (r < 0) { |
| 783 |
if (r != -ENOENT) |
| 784 |
return log_error_errno(r, "Failed to stat /run/systemd/volatile-root: %m"); |
| 785 |
|
| 786 |
/* volatile-root not found */ |
| 787 |
r = get_block_device_harder("/usr/", &d); |
| 788 |
if (r == 0) /* Not backed by a block device? Let's see if this is a sysext overlayfs instance */ |
| 789 |
r = get_sysext_overlay_block("/usr/", &d); |
| 790 |
if (r < 0) |
| 791 |
return log_error_errno(r, "Failed to determine block device of file system: %m"); |
| 792 |
} else if (!S_ISBLK(orig_root_stats.st_mode)) /* symlink was present but not block device */ |
| 793 |
return log_error_errno(SYNTHETIC_ERRNO(ENOTBLK), "/run/systemd/volatile-root is not linked to a block device."); |
| 794 |
else /* symlink was present and a block device */ |
| 795 |
d = orig_root_stats.st_rdev; |
| 796 |
|
| 797 |
} else if (rr->type == RESOURCE_PARTITION) { |
| 798 |
_cleanup_close_ int fd = -EBADF, real_fd = -EBADF; |
| 799 |
_cleanup_free_ char *resolved = NULL; |
| 800 |
struct stat st; |
| 801 |
|
| 802 |
r = chase(rr->path, root, CHASE_PREFIX_ROOT|CHASE_TRIGGER_AUTOFS, &resolved, &fd); |
| 803 |
if (r < 0) |
| 804 |
return log_error_errno(r, "Failed to resolve '%s': %m", rr->path); |
| 805 |
|
| 806 |
if (fstat(fd, &st) < 0) |
| 807 |
return log_error_errno(errno, "Failed to stat '%s': %m", resolved); |
| 808 |
|
| 809 |
if (S_ISBLK(st.st_mode) && root) |
| 810 |
return log_error_errno(SYNTHETIC_ERRNO(EPERM), "When using --root= or --image= access to device nodes is prohibited."); |
| 811 |
|
| 812 |
if (S_ISREG(st.st_mode) || S_ISBLK(st.st_mode)) { |
| 813 |
/* Not a directory, hence no need to find backing block device for the path */ |
| 814 |
free_and_replace(rr->path, resolved); |
| 815 |
return 0; |
| 816 |
} |
| 817 |
|
| 818 |
if (!S_ISDIR(st.st_mode)) |
| 819 |
return log_error_errno(SYNTHETIC_ERRNO(ENOTDIR), "Target path '%s' does not refer to regular file, directory or block device, refusing.", rr->path); |
| 820 |
|
| 821 |
if (node) { /* If --image= is specified all file systems are backed by the same loopback device, hence shortcut things. */ |
| 822 |
r = free_and_strdup_warn(&rr->path, node); |
| 823 |
if (r < 0) |
| 824 |
return r; |
| 825 |
|
| 826 |
return 0; |
| 827 |
} |
| 828 |
|
| 829 |
real_fd = fd_reopen(fd, O_RDONLY|O_CLOEXEC|O_DIRECTORY); |
| 830 |
if (real_fd < 0) |
| 831 |
return log_error_errno(real_fd, "Failed to convert O_PATH file descriptor for %s to regular file descriptor: %m", rr->path); |
| 832 |
|
| 833 |
r = get_block_device_harder_fd(real_fd, &d); |
| 834 |
if (r < 0) |
| 835 |
return log_error_errno(r, "Failed to determine block device of file system: %m"); |
| 836 |
|
| 837 |
} else if (RESOURCE_IS_FILESYSTEM(rr->type)) { |
| 838 |
_cleanup_free_ char *resolved = NULL, *relative_to = NULL; |
| 839 |
ChaseFlags chase_flags = CHASE_NONEXISTENT | CHASE_PREFIX_ROOT | CHASE_TRIGGER_AUTOFS; |
| 840 |
|
| 841 |
if (rr->path_relative_to == PATH_RELATIVE_TO_EXPLICIT) { |
| 842 |
assert(relative_to_directory); |
| 843 |
|
| 844 |
relative_to = strdup(relative_to_directory); |
| 845 |
if (!relative_to) |
| 846 |
return log_oom(); |
| 847 |
} else if (rr->path_relative_to == PATH_RELATIVE_TO_ROOT) { |
| 848 |
relative_to = strdup(empty_to_root(root)); |
| 849 |
if (!relative_to) |
| 850 |
return log_oom(); |
| 851 |
} else { /* boot, esp, or xbootldr */ |
| 852 |
r = 0; |
| 853 |
if (IN_SET(rr->path_relative_to, PATH_RELATIVE_TO_BOOT, PATH_RELATIVE_TO_XBOOTLDR)) |
| 854 |
r = find_xbootldr_and_warn(root, /* path= */ NULL, /* unprivileged_mode= */ -1, &relative_to, /* ret_fd= */ NULL); |
| 855 |
if (r == -ENOKEY || rr->path_relative_to == PATH_RELATIVE_TO_ESP) |
| 856 |
r = find_esp_and_warn(root, /* path= */ NULL, /* unprivileged_mode= */ -1, &relative_to, /* ret_fd= */ NULL); |
| 857 |
if (r < 0) |
| 858 |
return log_error_errno(r, "Failed to resolve $BOOT: %m"); |
| 859 |
log_debug("Resolved $BOOT to '%s'", relative_to); |
| 860 |
|
| 861 |
/* Since this partition is read from EFI, there should be no symlinks */ |
| 862 |
chase_flags |= CHASE_PROHIBIT_SYMLINKS; |
| 863 |
} |
| 864 |
|
| 865 |
r = chase(rr->path, relative_to, chase_flags, &resolved, NULL); |
| 866 |
if (r < 0) |
| 867 |
return log_error_errno(r, "Failed to resolve '%s' (relative to '%s'): %m", rr->path, relative_to); |
| 868 |
|
| 869 |
free_and_replace(rr->path, resolved); |
| 870 |
return 0; |
| 871 |
} else |
| 872 |
return 0; /* Otherwise assume there's nothing to resolve */ |
| 873 |
|
| 874 |
r = block_get_whole_disk(d, &d); |
| 875 |
if (r < 0) |
| 876 |
return log_error_errno(r, "Failed to find whole disk device for partition backing file system: %m"); |
| 877 |
if (r == 0) |
| 878 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 879 |
"File system is not placed on a partition block device, cannot determine whole block device backing root file system."); |
| 880 |
|
| 881 |
r = devname_from_devnum(S_IFBLK, d, &p); |
| 882 |
if (r < 0) |
| 883 |
return r; |
| 884 |
|
| 885 |
if (rr->path) |
| 886 |
log_info("Automatically discovered block device '%s' from '%s'.", p, rr->path); |
| 887 |
else |
| 888 |
log_info("Automatically discovered root block device '%s'.", p); |
| 889 |
|
| 890 |
free_and_replace(rr->path, p); |
| 891 |
return 1; |
| 892 |
} |
| 893 |
|
| 894 |
static const char *resource_type_table[_RESOURCE_TYPE_MAX] = { |
| 895 |
[RESOURCE_URL_FILE] = "url-file", |
| 896 |
[RESOURCE_URL_TAR] = "url-tar", |
| 897 |
[RESOURCE_TAR] = "tar", |
| 898 |
[RESOURCE_PARTITION] = "partition", |
| 899 |
[RESOURCE_REGULAR_FILE] = "regular-file", |
| 900 |
[RESOURCE_DIRECTORY] = "directory", |
| 901 |
[RESOURCE_SUBVOLUME] = "subvolume", |
| 902 |
}; |
| 903 |
|
| 904 |
DEFINE_STRING_TABLE_LOOKUP(resource_type, ResourceType); |
| 905 |
|
| 906 |
static const char *path_relative_to_table[_PATH_RELATIVE_TO_MAX] = { |
| 907 |
[PATH_RELATIVE_TO_ROOT] = "root", |
| 908 |
[PATH_RELATIVE_TO_ESP] = "esp", |
| 909 |
[PATH_RELATIVE_TO_XBOOTLDR] = "xbootldr", |
| 910 |
[PATH_RELATIVE_TO_BOOT] = "boot", |
| 911 |
[PATH_RELATIVE_TO_EXPLICIT] = "explicit", |
| 912 |
}; |
| 913 |
|
| 914 |
DEFINE_STRING_TABLE_LOOKUP(path_relative_to, PathRelativeTo); |