| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "sd-device.h" |
| 4 |
|
| 5 |
#include "alloc-util.h" |
| 6 |
#include "blkid-util.h" |
| 7 |
#include "blockdev-util.h" |
| 8 |
#include "build.h" |
| 9 |
#include "chase.h" |
| 10 |
#include "device-util.h" |
| 11 |
#include "errno-util.h" |
| 12 |
#include "fd-util.h" |
| 13 |
#include "format-table.h" |
| 14 |
#include "gpt.h" |
| 15 |
#include "initrd-util.h" |
| 16 |
#include "log.h" |
| 17 |
#include "main-func.h" |
| 18 |
#include "mountpoint-util.h" |
| 19 |
#include "options.h" |
| 20 |
#include "parse-argument.h" |
| 21 |
#include "path-util.h" |
| 22 |
#include "pretty-print.h" |
| 23 |
#include "string-util.h" |
| 24 |
#include "strv.h" |
| 25 |
#include "utf8.h" |
| 26 |
#include "xattr-util.h" |
| 27 |
|
| 28 |
static char *arg_target = NULL; |
| 29 |
static char *arg_root = NULL; |
| 30 |
|
| 31 |
STATIC_DESTRUCTOR_REGISTER(arg_target, freep); |
| 32 |
STATIC_DESTRUCTOR_REGISTER(arg_root, freep); |
| 33 |
|
| 34 |
static int help(void) { |
| 35 |
_cleanup_free_ char *link = NULL; |
| 36 |
_cleanup_(table_unrefp) Table *options = NULL; |
| 37 |
int r; |
| 38 |
|
| 39 |
r = terminal_urlify_man("[email protected]", "8", &link); |
| 40 |
if (r < 0) |
| 41 |
return log_oom(); |
| 42 |
|
| 43 |
r = option_parser_get_help_table(&options); |
| 44 |
if (r < 0) |
| 45 |
return r; |
| 46 |
|
| 47 |
printf("%s [OPTIONS...] /path/to/mountpoint\n" |
| 48 |
"\n%sCheck file system validation constraints.%s\n" |
| 49 |
"\nOptions:\n", |
| 50 |
program_invocation_short_name, |
| 51 |
ansi_highlight(), |
| 52 |
ansi_normal()); |
| 53 |
r = table_print_or_warn(options); |
| 54 |
if (r < 0) |
| 55 |
return r; |
| 56 |
|
| 57 |
printf("\nSee the %s for details.\n", link); |
| 58 |
return 0; |
| 59 |
} |
| 60 |
|
| 61 |
static int parse_argv(int argc, char *argv[]) { |
| 62 |
int r; |
| 63 |
|
| 64 |
assert(argc >= 0); |
| 65 |
assert(argv); |
| 66 |
|
| 67 |
OptionParser opts = { argc, argv }; |
| 68 |
|
| 69 |
FOREACH_OPTION_OR_RETURN(c, &opts) |
| 70 |
switch (c) { |
| 71 |
OPTION_COMMON_HELP: |
| 72 |
return help(); |
| 73 |
|
| 74 |
OPTION_COMMON_VERSION: |
| 75 |
return version(); |
| 76 |
|
| 77 |
OPTION_LONG("root", "PATH|auto", "Operate relative to the specified path"): |
| 78 |
if (streq(opts.arg, "auto")) |
| 79 |
r = free_and_strdup_warn(&arg_root, in_initrd() ? "/sysroot" : NULL); |
| 80 |
else { |
| 81 |
if (!path_is_absolute(opts.arg)) |
| 82 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 83 |
"--root= argument must be 'auto' or absolute path, got: %s", opts.arg); |
| 84 |
|
| 85 |
r = parse_path_argument(opts.arg, /* suppress_root= */ true, &arg_root); |
| 86 |
} |
| 87 |
if (r < 0) |
| 88 |
return r; |
| 89 |
break; |
| 90 |
} |
| 91 |
|
| 92 |
char **args = option_parser_get_args(&opts); |
| 93 |
|
| 94 |
if (strv_length(args) != 1) |
| 95 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 96 |
"%s expects exactly one argument (the mount point).", |
| 97 |
program_invocation_short_name); |
| 98 |
|
| 99 |
arg_target = strdup(args[0]); |
| 100 |
if (!arg_target) |
| 101 |
return log_oom(); |
| 102 |
|
| 103 |
if (arg_root && !path_startswith(arg_target, arg_root)) |
| 104 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 105 |
"Specified path '%s' does not start with specified root '%s', refusing.", |
| 106 |
arg_target, arg_root); |
| 107 |
return 1; |
| 108 |
} |
| 109 |
|
| 110 |
typedef struct ValidateFields { |
| 111 |
sd_id128_t *gpt_type_uuid; |
| 112 |
size_t n_gpt_type_uuid; |
| 113 |
char **gpt_label; |
| 114 |
char **mount_point; |
| 115 |
} ValidateFields; |
| 116 |
|
| 117 |
static void validate_fields_done(ValidateFields *f) { |
| 118 |
assert(f); |
| 119 |
|
| 120 |
free(f->gpt_type_uuid); |
| 121 |
strv_free(f->gpt_label); |
| 122 |
strv_free(f->mount_point); |
| 123 |
} |
| 124 |
|
| 125 |
static char* validate_fields_gpt_type_uuid_as_string(const ValidateFields *f) { |
| 126 |
_cleanup_free_ char *joined = NULL; |
| 127 |
|
| 128 |
assert(f); |
| 129 |
|
| 130 |
FOREACH_ARRAY(u, f->gpt_type_uuid, f->n_gpt_type_uuid) { |
| 131 |
if (!strextend_with_separator(&joined, ", ", SD_ID128_TO_UUID_STRING(*u))) |
| 132 |
return NULL; |
| 133 |
|
| 134 |
const char *id = gpt_partition_type_uuid_to_string(*u); |
| 135 |
if (id) |
| 136 |
(void) strextend(&joined, " (", id, ")"); |
| 137 |
} |
| 138 |
|
| 139 |
return TAKE_PTR(joined); |
| 140 |
} |
| 141 |
|
| 142 |
static int validate_fields_read(int fd, ValidateFields *ret) { |
| 143 |
_cleanup_(validate_fields_done) ValidateFields f = {}; |
| 144 |
int r; |
| 145 |
|
| 146 |
assert(fd >= 0); |
| 147 |
assert(ret); |
| 148 |
|
| 149 |
_cleanup_strv_free_ char **l = NULL; |
| 150 |
r = getxattr_at_strv(fd, /* path= */ NULL, "user.validatefs.gpt_type_uuid", AT_EMPTY_PATH, &l); |
| 151 |
if (r < 0) { |
| 152 |
if (r != -ENODATA && !ERRNO_IS_NOT_SUPPORTED(r)) |
| 153 |
return log_error_errno(r, "Failed to read 'user.validatefs.gpt_type_uuid' xattr: %m"); |
| 154 |
} else { |
| 155 |
STRV_FOREACH(i, l) { |
| 156 |
if (!GREEDY_REALLOC(f.gpt_type_uuid, f.n_gpt_type_uuid+1)) |
| 157 |
return log_oom(); |
| 158 |
|
| 159 |
r = sd_id128_from_string(*i, f.gpt_type_uuid + f.n_gpt_type_uuid); |
| 160 |
if (r < 0) |
| 161 |
return log_error_errno(r, "Failed to parse 'user.validatefs.gpt_type_uuid' xattr: %s", *i); |
| 162 |
|
| 163 |
f.n_gpt_type_uuid++; |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
l = strv_free(l); |
| 168 |
r = getxattr_at_strv(fd, /* path= */ NULL, "user.validatefs.gpt_label", AT_EMPTY_PATH, &l); |
| 169 |
if (r < 0) { |
| 170 |
if (r != -ENODATA && !ERRNO_IS_NOT_SUPPORTED(r)) |
| 171 |
return log_error_errno(r, "Failed to read 'user.validatefs.gpt_label' xattr: %m"); |
| 172 |
} else { |
| 173 |
STRV_FOREACH(i, l) |
| 174 |
if (!utf8_is_valid(*i) || |
| 175 |
string_has_cc(*i, /* ok= */ NULL)) |
| 176 |
return log_error_errno( |
| 177 |
SYNTHETIC_ERRNO(EINVAL), |
| 178 |
"Extended attribute 'user.validatefs.gpt_label' contains invalid characters, refusing: %s", *i); |
| 179 |
|
| 180 |
f.gpt_label = TAKE_PTR(l); |
| 181 |
} |
| 182 |
|
| 183 |
l = strv_free(l); |
| 184 |
r = getxattr_at_strv(fd, /* path= */ NULL, "user.validatefs.mount_point", AT_EMPTY_PATH, &l); |
| 185 |
if (r < 0) { |
| 186 |
if (r != -ENODATA && !ERRNO_IS_NOT_SUPPORTED(r)) |
| 187 |
return log_error_errno(r, "Failed to read 'user.validatefs.mount_point' xattr: %m"); |
| 188 |
} else { |
| 189 |
STRV_FOREACH(i, l) |
| 190 |
if (!utf8_is_valid(*i) || |
| 191 |
string_has_cc(*i, /* ok= */ NULL) || |
| 192 |
!path_is_absolute(*i) || |
| 193 |
!path_is_normalized(*i)) |
| 194 |
return log_error_errno( |
| 195 |
SYNTHETIC_ERRNO(EINVAL), |
| 196 |
"Path listed in extended attribute 'user.validatefs.mount_point' is not a valid, normalized, absolute path or contains invalid characters, refusing: %s", *i); |
| 197 |
|
| 198 |
f.mount_point = TAKE_PTR(l); |
| 199 |
} |
| 200 |
|
| 201 |
r = f.n_gpt_type_uuid > 0 || !strv_isempty(f.gpt_label) || !strv_isempty(f.mount_point); |
| 202 |
*ret = TAKE_STRUCT(f); |
| 203 |
return r; |
| 204 |
} |
| 205 |
|
| 206 |
static int validate_mount_point(const char *path, const ValidateFields *f) { |
| 207 |
assert(path); |
| 208 |
assert(f); |
| 209 |
|
| 210 |
if (strv_isempty(f->mount_point)) |
| 211 |
return 0; |
| 212 |
|
| 213 |
STRV_FOREACH(i, f->mount_point) { |
| 214 |
_cleanup_free_ char *jj = NULL; |
| 215 |
const char *j; |
| 216 |
|
| 217 |
if (arg_root) { |
| 218 |
jj = path_join(arg_root, *i); |
| 219 |
if (!jj) |
| 220 |
return log_oom(); |
| 221 |
|
| 222 |
j = jj; |
| 223 |
} else |
| 224 |
j = *i; |
| 225 |
|
| 226 |
if (path_equal(path, j)) |
| 227 |
return 0; |
| 228 |
} |
| 229 |
|
| 230 |
_cleanup_free_ char *joined = strv_join(f->mount_point, ", "); |
| 231 |
return log_error_errno( |
| 232 |
SYNTHETIC_ERRNO(EPERM), |
| 233 |
"File system is supposed to be mounted on one of %s only, but is mounted on %s, refusing.", |
| 234 |
strna(joined), path); |
| 235 |
} |
| 236 |
|
| 237 |
static int validate_gpt_label(blkid_probe b, const ValidateFields *f) { |
| 238 |
assert(b); |
| 239 |
assert(f); |
| 240 |
|
| 241 |
if (strv_isempty(f->gpt_label)) |
| 242 |
return 0; |
| 243 |
|
| 244 |
const char *v = NULL; |
| 245 |
(void) sym_blkid_probe_lookup_value(b, "PART_ENTRY_NAME", &v, /* len= */ NULL); |
| 246 |
|
| 247 |
if (strv_contains(f->gpt_label, strempty(v))) |
| 248 |
return 0; |
| 249 |
|
| 250 |
_cleanup_free_ char *joined = strv_join(f->gpt_label, "', '"); |
| 251 |
return log_error_errno( |
| 252 |
SYNTHETIC_ERRNO(EPERM), |
| 253 |
"File system is supposed to be placed in a partition with labels '%s' only, but is placed in one labelled '%s', refusing.", |
| 254 |
strna(joined), strempty(v)); |
| 255 |
} |
| 256 |
|
| 257 |
static int validate_gpt_type(blkid_probe b, const ValidateFields *f) { |
| 258 |
assert(b); |
| 259 |
assert(f); |
| 260 |
|
| 261 |
if (f->n_gpt_type_uuid == 0) |
| 262 |
return 0; |
| 263 |
|
| 264 |
const char *v = NULL; |
| 265 |
(void) sym_blkid_probe_lookup_value(b, "PART_ENTRY_TYPE", &v, /* len= */ NULL); |
| 266 |
|
| 267 |
sd_id128_t id; |
| 268 |
if (!v || sd_id128_from_string(v, &id) < 0) { |
| 269 |
_cleanup_free_ char *joined = validate_fields_gpt_type_uuid_as_string(f); |
| 270 |
return log_error_errno( |
| 271 |
SYNTHETIC_ERRNO(EPERM), |
| 272 |
"File system is supposed to be placed in a partition of type UUIDs %s only, but has no type, refusing.", |
| 273 |
strna(joined)); |
| 274 |
} |
| 275 |
|
| 276 |
FOREACH_ARRAY(u, f->gpt_type_uuid, f->n_gpt_type_uuid) |
| 277 |
if (sd_id128_equal(*u, id)) |
| 278 |
return 0; |
| 279 |
|
| 280 |
_cleanup_free_ char *joined = validate_fields_gpt_type_uuid_as_string(f); |
| 281 |
return log_error_errno( |
| 282 |
SYNTHETIC_ERRNO(EPERM), |
| 283 |
"File system is supposed to be placed in a partition of type UUIDs %s only, but has type '%s', refusing.", |
| 284 |
strna(joined), SD_ID128_TO_UUID_STRING(id)); |
| 285 |
} |
| 286 |
|
| 287 |
static int validate_gpt_metadata_one(sd_device *d, const char *path, const ValidateFields *f) { |
| 288 |
int r; |
| 289 |
|
| 290 |
assert(d); |
| 291 |
assert(f); |
| 292 |
|
| 293 |
r = DLOPEN_LIBBLKID(LOG_ERR, SD_ELF_NOTE_DLOPEN_PRIORITY_REQUIRED); |
| 294 |
if (r < 0) |
| 295 |
return r; |
| 296 |
|
| 297 |
_cleanup_close_ int block_fd = sd_device_open(d, O_RDONLY|O_CLOEXEC|O_NONBLOCK); |
| 298 |
if (block_fd < 0) |
| 299 |
return log_error_errno(block_fd, "Failed to open block device backing '%s': %m", path); |
| 300 |
|
| 301 |
_cleanup_(blkid_free_probep) blkid_probe b = sym_blkid_new_probe(); |
| 302 |
if (!b) |
| 303 |
return log_oom(); |
| 304 |
|
| 305 |
errno = 0; |
| 306 |
r = sym_blkid_probe_set_device(b, block_fd, 0, 0); |
| 307 |
if (r != 0) |
| 308 |
return log_error_errno(errno_or_else(ENOMEM), "Failed to set up block device prober for '%s': %m", path); |
| 309 |
|
| 310 |
(void) sym_blkid_probe_enable_superblocks(b, 1); |
| 311 |
(void) sym_blkid_probe_set_superblocks_flags(b, BLKID_SUBLKS_TYPE|BLKID_SUBLKS_LABEL); |
| 312 |
(void) sym_blkid_probe_enable_partitions(b, 1); |
| 313 |
(void) sym_blkid_probe_set_partitions_flags(b, BLKID_PARTS_ENTRY_DETAILS); |
| 314 |
|
| 315 |
errno = 0; |
| 316 |
r = sym_blkid_do_safeprobe(b); |
| 317 |
if (r == _BLKID_SAFEPROBE_ERROR) |
| 318 |
return log_error_errno(errno_or_else(EIO), "Failed to probe block device of '%s': %m", path); |
| 319 |
if (r == _BLKID_SAFEPROBE_AMBIGUOUS) |
| 320 |
return log_error_errno(SYNTHETIC_ERRNO(ENOPKG), "Found multiple file system labels on block device '%s'.", path); |
| 321 |
if (r == _BLKID_SAFEPROBE_NOT_FOUND) |
| 322 |
return log_error_errno(SYNTHETIC_ERRNO(ENOPKG), "Found no file system label on block device '%s'.", path); |
| 323 |
|
| 324 |
assert(r == _BLKID_SAFEPROBE_FOUND); |
| 325 |
|
| 326 |
const char *v = NULL; |
| 327 |
(void) sym_blkid_probe_lookup_value(b, "PART_ENTRY_SCHEME", &v, /* len= */ NULL); |
| 328 |
if (!streq_ptr(v, "gpt")) |
| 329 |
return log_error_errno(SYNTHETIC_ERRNO(EPERM), "File system is supposed to be on a GPT partition table, but is not, refusing."); |
| 330 |
|
| 331 |
r = validate_gpt_label(b, f); |
| 332 |
if (r < 0) |
| 333 |
return r; |
| 334 |
|
| 335 |
r = validate_gpt_type(b, f); |
| 336 |
if (r < 0) |
| 337 |
return r; |
| 338 |
|
| 339 |
return 0; |
| 340 |
} |
| 341 |
|
| 342 |
static int validate_gpt_metadata(int fd, const char *path, const ValidateFields *f) { |
| 343 |
int r; |
| 344 |
|
| 345 |
assert(fd >= 0); |
| 346 |
assert(path); |
| 347 |
assert(f); |
| 348 |
|
| 349 |
if (strv_isempty(f->gpt_label) && f->n_gpt_type_uuid == 0) |
| 350 |
return 0; |
| 351 |
|
| 352 |
_cleanup_(sd_device_unrefp) sd_device *d = NULL; |
| 353 |
r = block_device_new_from_fd(fd, BLOCK_DEVICE_LOOKUP_BACKING, &d); |
| 354 |
if (r < 0) |
| 355 |
return log_error_errno(r, "Failed to find block device backing '%s': %m", path); |
| 356 |
|
| 357 |
/* Now validate all subordinate devices individually. */ |
| 358 |
bool have_slaves = false; |
| 359 |
const char *suffix; |
| 360 |
FOREACH_DEVICE_CHILD_WITH_SUFFIX(d, child, suffix) { |
| 361 |
if (!path_startswith(suffix, "slaves")) |
| 362 |
continue; |
| 363 |
|
| 364 |
have_slaves = true; |
| 365 |
|
| 366 |
r = validate_gpt_metadata_one(child, path, f); |
| 367 |
if (r < 0) |
| 368 |
return r; |
| 369 |
} |
| 370 |
|
| 371 |
/* If this device has no subordinate devices, then validate the device itself instead */ |
| 372 |
if (!have_slaves) { |
| 373 |
r = validate_gpt_metadata_one(d, path, f); |
| 374 |
if (r < 0) |
| 375 |
return r; |
| 376 |
} |
| 377 |
|
| 378 |
return 0; |
| 379 |
} |
| 380 |
|
| 381 |
static int validate_fields_check(int fd, const char *path, const ValidateFields *f) { |
| 382 |
int r; |
| 383 |
|
| 384 |
assert(fd >= 0); |
| 385 |
assert(path); |
| 386 |
assert(f); |
| 387 |
|
| 388 |
r = validate_mount_point(path, f); |
| 389 |
if (r < 0) |
| 390 |
return r; |
| 391 |
|
| 392 |
r = validate_gpt_metadata(fd, path, f); |
| 393 |
if (r < 0) |
| 394 |
return r; |
| 395 |
|
| 396 |
log_info("File system '%s' passed validation constraints, proceeding.", path); |
| 397 |
return 0; |
| 398 |
} |
| 399 |
|
| 400 |
static int run(int argc, char *argv[]) { |
| 401 |
int r; |
| 402 |
|
| 403 |
log_setup(); |
| 404 |
|
| 405 |
r = parse_argv(argc, argv); |
| 406 |
if (r <= 0) |
| 407 |
return r; |
| 408 |
|
| 409 |
_cleanup_free_ char *resolved = NULL; |
| 410 |
_cleanup_close_ int target_fd = chase_and_open(arg_target, arg_root, CHASE_MUST_BE_DIRECTORY, O_DIRECTORY|O_CLOEXEC, &resolved); |
| 411 |
if (target_fd < 0) |
| 412 |
return log_error_errno(target_fd, "Failed to open directory '%s': %m", arg_target); |
| 413 |
|
| 414 |
r = is_mount_point_at(target_fd, /* path= */ NULL, /* flags= */ 0); |
| 415 |
if (r < 0) |
| 416 |
return log_error_errno(r, "Failed to determine whether '%s' is a mount point: %m", resolved); |
| 417 |
if (!r) |
| 418 |
return log_error_errno(SYNTHETIC_ERRNO(ENOTDIR), "Directory '%s' is not a mount point.", resolved); |
| 419 |
|
| 420 |
_cleanup_(validate_fields_done) ValidateFields f = {}; |
| 421 |
r = validate_fields_read(target_fd, &f); |
| 422 |
if (r < 0) |
| 423 |
return r; |
| 424 |
if (r == 0) { |
| 425 |
log_info("File system '%s' has no validation constraints set, not validating.", resolved); |
| 426 |
return 0; |
| 427 |
} |
| 428 |
|
| 429 |
return validate_fields_check(target_fd, resolved, &f); |
| 430 |
} |
| 431 |
|
| 432 |
DEFINE_MAIN_FUNCTION(run); |