| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "alloc-util.h" |
| 4 |
#include "dissect-image.h" |
| 5 |
#include "extract-word.h" |
| 6 |
#include "image-policy.h" |
| 7 |
#include "log.h" |
| 8 |
#include "logarithm.h" |
| 9 |
#include "sort-util.h" |
| 10 |
#include "string-util.h" |
| 11 |
#include "strv.h" |
| 12 |
|
| 13 |
/* Rationale for the chosen syntax: |
| 14 |
* |
| 15 |
* → one line, so that it can be reasonably added to a shell command line, for example via `systemd-dissect |
| 16 |
* --image-policy=…` or to the kernel command line via `systemd.image_policy=`. |
| 17 |
* |
| 18 |
* → no use of "," or ";" as separators, so that it can be included in mount/fstab-style option strings and |
| 19 |
* doesn't require escaping. Instead, separators are ":", "=", "+" which should be fine both in shell |
| 20 |
* command lines and in mount/fstab style option strings. |
| 21 |
*/ |
| 22 |
|
| 23 |
static int partition_policy_compare(const PartitionPolicy *a, const PartitionPolicy *b) { |
| 24 |
return CMP(ASSERT_PTR(a)->designator, ASSERT_PTR(b)->designator); |
| 25 |
} |
| 26 |
|
| 27 |
static const PartitionPolicy* image_policy_bsearch(const ImagePolicy *policy, PartitionDesignator designator) { |
| 28 |
if (!policy) |
| 29 |
return NULL; |
| 30 |
|
| 31 |
return typesafe_bsearch( |
| 32 |
&(const PartitionPolicy) { .designator = designator }, |
| 33 |
ASSERT_PTR(policy)->policies, |
| 34 |
ASSERT_PTR(policy)->n_policies, |
| 35 |
partition_policy_compare); |
| 36 |
} |
| 37 |
|
| 38 |
PartitionPolicyFlags partition_policy_flags_extend(PartitionPolicyFlags flags) { |
| 39 |
/* If some parts of a flags field are left unspecified, let's fill in all options. */ |
| 40 |
|
| 41 |
/* If no protection flag is set, then this means all are set */ |
| 42 |
if ((flags & _PARTITION_POLICY_USE_MASK) == 0) |
| 43 |
flags |= PARTITION_POLICY_OPEN; |
| 44 |
|
| 45 |
/* If the gpt flags bits are not specified, set both options for each */ |
| 46 |
if ((flags & _PARTITION_POLICY_READ_ONLY_MASK) == 0) |
| 47 |
flags |= PARTITION_POLICY_READ_ONLY_ON|PARTITION_POLICY_READ_ONLY_OFF; |
| 48 |
|
| 49 |
if ((flags & _PARTITION_POLICY_GROWFS_MASK) == 0) |
| 50 |
flags |= PARTITION_POLICY_GROWFS_ON|PARTITION_POLICY_GROWFS_OFF; |
| 51 |
|
| 52 |
return flags; |
| 53 |
} |
| 54 |
|
| 55 |
PartitionPolicyFlags partition_policy_flags_reduce(PartitionPolicyFlags flags) { |
| 56 |
/* The reverse of partition_policy_flags_extend(): if some parts of the flags field allow all |
| 57 |
* possible options, let's remove it from the flags to make them shorter */ |
| 58 |
|
| 59 |
if (FLAGS_SET(flags, _PARTITION_POLICY_USE_MASK)) |
| 60 |
flags &= ~_PARTITION_POLICY_USE_MASK; |
| 61 |
if (FLAGS_SET(flags, _PARTITION_POLICY_READ_ONLY_MASK)) |
| 62 |
flags &= ~_PARTITION_POLICY_READ_ONLY_MASK; |
| 63 |
if (FLAGS_SET(flags, _PARTITION_POLICY_GROWFS_MASK)) |
| 64 |
flags &= ~_PARTITION_POLICY_GROWFS_MASK; |
| 65 |
|
| 66 |
return flags; |
| 67 |
} |
| 68 |
|
| 69 |
static PartitionPolicyFlags partition_policy_normalized_flags(const PartitionPolicy *policy) { |
| 70 |
PartitionPolicyFlags flags = ASSERT_PTR(policy)->flags; |
| 71 |
|
| 72 |
/* This normalizes the per-partition policy flags. This means if the user left some things |
| 73 |
* unspecified, we'll fill in the appropriate "dontcare" policy instead. We'll also mask out bits |
| 74 |
* that do not make any sense for specific partition types. */ |
| 75 |
|
| 76 |
flags = partition_policy_flags_extend(flags); |
| 77 |
|
| 78 |
/* If this is a verity or verity signature designator, then mask off all protection bits, this after |
| 79 |
* all needs no protection, because it *is* the protection */ |
| 80 |
if (partition_verity_hash_to_data(policy->designator) >= 0 || |
| 81 |
partition_verity_sig_to_data(policy->designator) >= 0) |
| 82 |
flags &= ~(PARTITION_POLICY_VERITY|PARTITION_POLICY_SIGNED|PARTITION_POLICY_ENCRYPTED|PARTITION_POLICY_ENCRYPTEDWITHINTEGRITY); |
| 83 |
|
| 84 |
/* if this designator has no verity concept, then mask off verity protection flags */ |
| 85 |
if (partition_verity_hash_of(policy->designator) < 0) |
| 86 |
flags &= ~(PARTITION_POLICY_VERITY|PARTITION_POLICY_SIGNED); |
| 87 |
|
| 88 |
/* If the partition must be absent, then the gpt flags don't matter */ |
| 89 |
if ((flags & _PARTITION_POLICY_USE_MASK) == PARTITION_POLICY_ABSENT) |
| 90 |
flags &= ~(_PARTITION_POLICY_READ_ONLY_MASK|_PARTITION_POLICY_GROWFS_MASK); |
| 91 |
|
| 92 |
return flags; |
| 93 |
} |
| 94 |
|
| 95 |
PartitionPolicyFlags image_policy_get(const ImagePolicy *policy, PartitionDesignator designator) { |
| 96 |
PartitionDesignator data_designator = _PARTITION_DESIGNATOR_INVALID; |
| 97 |
const PartitionPolicy *pp; |
| 98 |
|
| 99 |
/* No policy means: everything may be used in any mode */ |
| 100 |
if (!policy) |
| 101 |
return partition_policy_normalized_flags( |
| 102 |
&(const PartitionPolicy) { |
| 103 |
.flags = PARTITION_POLICY_OPEN, |
| 104 |
.designator = designator, |
| 105 |
}); |
| 106 |
|
| 107 |
pp = image_policy_bsearch(policy, designator); |
| 108 |
if (pp) |
| 109 |
return partition_policy_normalized_flags(pp); |
| 110 |
|
| 111 |
/* Hmm, so this didn't work, then let's see if we can derive some policy from the underlying data |
| 112 |
* partition in case of verity/signature partitions */ |
| 113 |
|
| 114 |
data_designator = partition_verity_hash_to_data(designator); |
| 115 |
if (data_designator >= 0) { |
| 116 |
PartitionPolicyFlags data_flags; |
| 117 |
|
| 118 |
/* So we are asked for the policy for a verity partition, and there's no explicit policy for |
| 119 |
* that case. Let's synthesize a policy from the protection setting for the underlying data |
| 120 |
* partition. */ |
| 121 |
|
| 122 |
data_flags = image_policy_get(policy, data_designator); |
| 123 |
if (data_flags < 0) |
| 124 |
return data_flags; |
| 125 |
|
| 126 |
/* We need verity if verity or verity with sig is requested */ |
| 127 |
if (!(data_flags & (PARTITION_POLICY_SIGNED|PARTITION_POLICY_VERITY))) |
| 128 |
return _PARTITION_POLICY_FLAGS_INVALID; |
| 129 |
|
| 130 |
/* If the data partition may be unused or absent, then the verity partition may too. Also, inherit the partition flags policy */ |
| 131 |
return partition_policy_normalized_flags( |
| 132 |
&(const PartitionPolicy) { |
| 133 |
.flags = PARTITION_POLICY_UNPROTECTED | (data_flags & (PARTITION_POLICY_UNUSED|PARTITION_POLICY_ABSENT)) | |
| 134 |
(data_flags & _PARTITION_POLICY_PFLAGS_MASK), |
| 135 |
.designator = designator, |
| 136 |
}); |
| 137 |
} |
| 138 |
|
| 139 |
data_designator = partition_verity_sig_to_data(designator); |
| 140 |
if (data_designator >= 0) { |
| 141 |
PartitionPolicyFlags data_flags; |
| 142 |
|
| 143 |
/* Similar case as for verity partitions, but slightly more strict rules */ |
| 144 |
|
| 145 |
data_flags = image_policy_get(policy, data_designator); |
| 146 |
if (data_flags < 0) |
| 147 |
return data_flags; |
| 148 |
|
| 149 |
if (!(data_flags & PARTITION_POLICY_SIGNED)) |
| 150 |
return _PARTITION_POLICY_FLAGS_INVALID; |
| 151 |
|
| 152 |
return partition_policy_normalized_flags( |
| 153 |
&(const PartitionPolicy) { |
| 154 |
.flags = PARTITION_POLICY_UNPROTECTED | (data_flags & (PARTITION_POLICY_UNUSED|PARTITION_POLICY_ABSENT)) | |
| 155 |
(data_flags & _PARTITION_POLICY_PFLAGS_MASK), |
| 156 |
.designator = designator, |
| 157 |
}); |
| 158 |
} |
| 159 |
|
| 160 |
return _PARTITION_POLICY_FLAGS_INVALID; /* got nothing */ |
| 161 |
} |
| 162 |
|
| 163 |
PartitionPolicyFlags image_policy_get_exhaustively(const ImagePolicy *policy, PartitionDesignator designator) { |
| 164 |
PartitionPolicyFlags flags; |
| 165 |
|
| 166 |
/* This is just like image_policy_get() but whenever there is no policy for a specific designator, we |
| 167 |
* return the default policy. */ |
| 168 |
|
| 169 |
flags = image_policy_get(policy, designator); |
| 170 |
if (flags < 0) |
| 171 |
return partition_policy_normalized_flags( |
| 172 |
&(const PartitionPolicy) { |
| 173 |
.flags = image_policy_default(policy), |
| 174 |
.designator = designator, |
| 175 |
}); |
| 176 |
|
| 177 |
return flags; |
| 178 |
} |
| 179 |
|
| 180 |
static PartitionPolicyFlags policy_flag_from_fstype(const char *s) { |
| 181 |
if (!s) |
| 182 |
return _PARTITION_POLICY_FLAGS_INVALID; |
| 183 |
|
| 184 |
if (streq(s, "btrfs")) |
| 185 |
return PARTITION_POLICY_BTRFS; |
| 186 |
if (streq(s, "erofs")) |
| 187 |
return PARTITION_POLICY_EROFS; |
| 188 |
if (streq(s, "ext4")) |
| 189 |
return PARTITION_POLICY_EXT4; |
| 190 |
if (streq(s, "f2fs")) |
| 191 |
return PARTITION_POLICY_F2FS; |
| 192 |
if (streq(s, "squashfs")) |
| 193 |
return PARTITION_POLICY_SQUASHFS; |
| 194 |
if (streq(s, "vfat")) |
| 195 |
return PARTITION_POLICY_VFAT; |
| 196 |
if (streq(s, "xfs")) |
| 197 |
return PARTITION_POLICY_XFS; |
| 198 |
|
| 199 |
return _PARTITION_POLICY_FLAGS_INVALID; |
| 200 |
} |
| 201 |
|
| 202 |
static PartitionPolicyFlags policy_flag_from_string_one(const char *s) { |
| 203 |
assert(s); |
| 204 |
|
| 205 |
/* This is a bitmask (i.e. not dense), hence we don't use the "string-table.h" stuff here. */ |
| 206 |
|
| 207 |
if (streq(s, "verity")) |
| 208 |
return PARTITION_POLICY_VERITY; |
| 209 |
if (streq(s, "signed")) |
| 210 |
return PARTITION_POLICY_SIGNED; |
| 211 |
if (streq(s, "encrypted")) |
| 212 |
return PARTITION_POLICY_ENCRYPTED; |
| 213 |
if (streq(s, "encryptedwithintegrity")) |
| 214 |
return PARTITION_POLICY_ENCRYPTEDWITHINTEGRITY; |
| 215 |
if (streq(s, "unprotected")) |
| 216 |
return PARTITION_POLICY_UNPROTECTED; |
| 217 |
if (streq(s, "unused")) |
| 218 |
return PARTITION_POLICY_UNUSED; |
| 219 |
if (streq(s, "absent")) |
| 220 |
return PARTITION_POLICY_ABSENT; |
| 221 |
if (streq(s, "open")) /* shortcut alias */ |
| 222 |
return PARTITION_POLICY_OPEN; |
| 223 |
if (streq(s, "ignore")) /* ditto */ |
| 224 |
return PARTITION_POLICY_IGNORE; |
| 225 |
if (streq(s, "read-only-on")) |
| 226 |
return PARTITION_POLICY_READ_ONLY_ON; |
| 227 |
if (streq(s, "read-only-off")) |
| 228 |
return PARTITION_POLICY_READ_ONLY_OFF; |
| 229 |
if (streq(s, "growfs-on")) |
| 230 |
return PARTITION_POLICY_GROWFS_ON; |
| 231 |
if (streq(s, "growfs-off")) |
| 232 |
return PARTITION_POLICY_GROWFS_OFF; |
| 233 |
|
| 234 |
return policy_flag_from_fstype(s); |
| 235 |
} |
| 236 |
|
| 237 |
PartitionPolicyFlags partition_policy_flags_from_string(const char *s, bool graceful) { |
| 238 |
PartitionPolicyFlags flags = 0; |
| 239 |
int r; |
| 240 |
|
| 241 |
assert(s); |
| 242 |
|
| 243 |
if (empty_or_dash(s)) |
| 244 |
return 0; |
| 245 |
|
| 246 |
for (;;) { |
| 247 |
_cleanup_free_ char *f = NULL; |
| 248 |
PartitionPolicyFlags ff; |
| 249 |
|
| 250 |
r = extract_first_word(&s, &f, "+", EXTRACT_DONT_COALESCE_SEPARATORS); |
| 251 |
if (r < 0) |
| 252 |
return r; |
| 253 |
if (r == 0) |
| 254 |
break; |
| 255 |
|
| 256 |
ff = policy_flag_from_string_one(strstrip(f)); |
| 257 |
if (ff < 0) { |
| 258 |
if (graceful) { |
| 259 |
log_debug("Unknown partition policy flag, ignoring: %s", f); |
| 260 |
continue; |
| 261 |
} |
| 262 |
return -EBADRQC; /* recognizable error */ |
| 263 |
} |
| 264 |
|
| 265 |
flags |= ff; |
| 266 |
} |
| 267 |
|
| 268 |
return flags; |
| 269 |
} |
| 270 |
|
| 271 |
static ImagePolicy* image_policy_new(size_t n_policies) { |
| 272 |
ImagePolicy *p; |
| 273 |
|
| 274 |
if (n_policies > (SIZE_MAX - offsetof(ImagePolicy, policies)) / sizeof(PartitionPolicy)) /* overflow check */ |
| 275 |
return NULL; |
| 276 |
|
| 277 |
p = malloc(offsetof(ImagePolicy, policies) + sizeof(PartitionPolicy) * n_policies); |
| 278 |
if (!p) |
| 279 |
return NULL; |
| 280 |
|
| 281 |
*p = (ImagePolicy) { |
| 282 |
.default_flags = PARTITION_POLICY_IGNORE, |
| 283 |
}; |
| 284 |
return p; |
| 285 |
} |
| 286 |
|
| 287 |
ImagePolicy* image_policy_new_from_dissected(const DissectedImage *image, const VeritySettings *verity) { |
| 288 |
assert(image); |
| 289 |
|
| 290 |
ImagePolicy *image_policy = image_policy_new(_PARTITION_DESIGNATOR_MAX); |
| 291 |
if (!image_policy) |
| 292 |
return NULL; |
| 293 |
|
| 294 |
/* Default to 'absent', only what we find is allowed to be used */ |
| 295 |
image_policy->default_flags = PARTITION_POLICY_ABSENT; |
| 296 |
|
| 297 |
for (PartitionDesignator pd = 0; pd < _PARTITION_DESIGNATOR_MAX; pd++) { |
| 298 |
PartitionPolicyFlags f = 0; |
| 299 |
|
| 300 |
if (!image->partitions[pd].found) |
| 301 |
f |= PARTITION_POLICY_ABSENT; |
| 302 |
else { |
| 303 |
if (streq_ptr(image->partitions[pd].fstype, "crypto_LUKS")) |
| 304 |
f |= PARTITION_POLICY_ENCRYPTED; |
| 305 |
else { |
| 306 |
PartitionPolicyFlags fstype_flag = policy_flag_from_fstype(image->partitions[pd].fstype); |
| 307 |
if (fstype_flag >= 0) |
| 308 |
f |= fstype_flag; |
| 309 |
} |
| 310 |
|
| 311 |
if (!verity || verity->designator < 0 || verity->designator == pd) { |
| 312 |
if (image->verity_sig_ready || (verity && iovec_is_set(&verity->root_hash_sig))) |
| 313 |
f |= PARTITION_POLICY_SIGNED; |
| 314 |
else if (image->verity_ready || (verity && iovec_is_set(&verity->root_hash))) |
| 315 |
f |= PARTITION_POLICY_VERITY; |
| 316 |
} |
| 317 |
|
| 318 |
if (!image->single_file_system) { |
| 319 |
if (image->partitions[pd].growfs) |
| 320 |
f |= PARTITION_POLICY_GROWFS_ON; |
| 321 |
else |
| 322 |
f |= PARTITION_POLICY_GROWFS_OFF; |
| 323 |
|
| 324 |
if (image->partitions[pd].rw) |
| 325 |
f |= PARTITION_POLICY_READ_ONLY_OFF; |
| 326 |
else |
| 327 |
f |= PARTITION_POLICY_READ_ONLY_ON; |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
image_policy->policies[pd] = (PartitionPolicy) { |
| 332 |
.designator = pd, |
| 333 |
.flags = f, |
| 334 |
}; |
| 335 |
image_policy->n_policies++; |
| 336 |
} |
| 337 |
|
| 338 |
return image_policy; |
| 339 |
} |
| 340 |
|
| 341 |
int image_policy_from_string(const char *s, bool graceful, ImagePolicy **ret) { |
| 342 |
_cleanup_free_ ImagePolicy *p = NULL; |
| 343 |
uint64_t dmask = 0; |
| 344 |
ImagePolicy *t; |
| 345 |
PartitionPolicyFlags symbolic_policy; |
| 346 |
int r; |
| 347 |
|
| 348 |
assert(s); |
| 349 |
assert_cc(sizeof(dmask) * 8 >= _PARTITION_DESIGNATOR_MAX); |
| 350 |
|
| 351 |
/* Recognizable errors: |
| 352 |
* |
| 353 |
* ENOTUNIQ → Two or more rules for the same partition |
| 354 |
* EBADSLT → Unknown partition designator |
| 355 |
* EBADRQC → Unknown policy flags |
| 356 |
*/ |
| 357 |
|
| 358 |
/* First, let's handle "symbolic" policies, i.e. "-", "*", "~" */ |
| 359 |
if (empty_or_dash(s)) |
| 360 |
/* ignore policy: everything may exist, but nothing used */ |
| 361 |
symbolic_policy = PARTITION_POLICY_IGNORE; |
| 362 |
else if (streq(s, "*")) |
| 363 |
/* allow policy: everything is allowed */ |
| 364 |
symbolic_policy = PARTITION_POLICY_OPEN; |
| 365 |
else if (streq(s, "~")) |
| 366 |
/* deny policy: nothing may exist */ |
| 367 |
symbolic_policy = PARTITION_POLICY_ABSENT; |
| 368 |
else |
| 369 |
symbolic_policy = _PARTITION_POLICY_FLAGS_INVALID; |
| 370 |
|
| 371 |
if (symbolic_policy >= 0) { |
| 372 |
if (!ret) |
| 373 |
return 0; |
| 374 |
|
| 375 |
p = image_policy_new(0); |
| 376 |
if (!p) |
| 377 |
return -ENOMEM; |
| 378 |
|
| 379 |
p->default_flags = symbolic_policy; |
| 380 |
*ret = TAKE_PTR(p); |
| 381 |
return 0; |
| 382 |
} |
| 383 |
|
| 384 |
/* Allocate the policy at maximum size, i.e. for all designators. We might overshoot a bit, but the |
| 385 |
* items are cheap, and we can return unused space to libc once we know we don't need it */ |
| 386 |
p = image_policy_new(_PARTITION_DESIGNATOR_MAX); |
| 387 |
if (!p) |
| 388 |
return -ENOMEM; |
| 389 |
|
| 390 |
const char *q = s; |
| 391 |
bool default_specified = false; |
| 392 |
for (;;) { |
| 393 |
_cleanup_free_ char *e = NULL, *d = NULL; |
| 394 |
PartitionDesignator designator; |
| 395 |
PartitionPolicyFlags flags; |
| 396 |
char *f, *ds, *fs; |
| 397 |
|
| 398 |
r = extract_first_word(&q, &e, ":", EXTRACT_DONT_COALESCE_SEPARATORS); |
| 399 |
if (r < 0) |
| 400 |
return r; |
| 401 |
if (r == 0) |
| 402 |
break; |
| 403 |
|
| 404 |
f = e; |
| 405 |
r = extract_first_word((const char**) &f, &d, "=", EXTRACT_DONT_COALESCE_SEPARATORS); |
| 406 |
if (r < 0) |
| 407 |
return r; |
| 408 |
if (r == 0) |
| 409 |
return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Expected designator name followed by '='; got instead: %s", e); |
| 410 |
if (!f) /* no separator? */ |
| 411 |
return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Missing '=' in policy expression: %s", e); |
| 412 |
|
| 413 |
ds = strstrip(d); |
| 414 |
if (isempty(ds)) { |
| 415 |
/* Not partition name? then it's the default policy */ |
| 416 |
if (default_specified) |
| 417 |
return log_debug_errno(SYNTHETIC_ERRNO(ENOTUNIQ), "Default partition policy flags specified more than once."); |
| 418 |
|
| 419 |
designator = _PARTITION_DESIGNATOR_INVALID; |
| 420 |
default_specified = true; |
| 421 |
} else { |
| 422 |
designator = partition_designator_from_string(ds); |
| 423 |
if (designator < 0) { |
| 424 |
if (!graceful) |
| 425 |
return log_debug_errno(SYNTHETIC_ERRNO(EBADSLT), "Unknown partition designator: %s", ds); /* recognizable error */ |
| 426 |
|
| 427 |
log_debug("Unknown partition designator, ignoring: %s", ds); |
| 428 |
continue; |
| 429 |
} |
| 430 |
if (dmask & (UINT64_C(1) << designator)) |
| 431 |
return log_debug_errno(SYNTHETIC_ERRNO(ENOTUNIQ), "Partition designator specified more than once: %s", ds); |
| 432 |
dmask |= UINT64_C(1) << designator; |
| 433 |
} |
| 434 |
|
| 435 |
fs = strstrip(f); |
| 436 |
flags = partition_policy_flags_from_string(fs, graceful); |
| 437 |
if (flags == -EBADRQC) |
| 438 |
return log_debug_errno(flags, "Unknown partition policy flag: %s", fs); |
| 439 |
if (flags < 0) |
| 440 |
return log_debug_errno(flags, "Failed to parse partition policy flags '%s': %m", fs); |
| 441 |
|
| 442 |
if (designator < 0) |
| 443 |
p->default_flags = flags; |
| 444 |
else { |
| 445 |
p->policies[p->n_policies++] = (PartitionPolicy) { |
| 446 |
.designator = designator, |
| 447 |
.flags = flags, |
| 448 |
}; |
| 449 |
} |
| 450 |
}; |
| 451 |
|
| 452 |
assert(p->n_policies <= _PARTITION_DESIGNATOR_MAX); |
| 453 |
|
| 454 |
/* Return unused space to libc */ |
| 455 |
t = realloc(p, offsetof(ImagePolicy, policies) + sizeof(PartitionPolicy) * p->n_policies); |
| 456 |
if (t) |
| 457 |
p = t; |
| 458 |
|
| 459 |
typesafe_qsort(p->policies, p->n_policies, partition_policy_compare); |
| 460 |
|
| 461 |
if (ret) |
| 462 |
*ret = TAKE_PTR(p); |
| 463 |
|
| 464 |
return 0; |
| 465 |
} |
| 466 |
|
| 467 |
int partition_policy_flags_to_string(PartitionPolicyFlags flags, bool simplify, char **ret) { |
| 468 |
_cleanup_free_ char *buf = NULL; |
| 469 |
const char *l[CONST_LOG2U(_PARTITION_POLICY_MASK + _PARTITION_POLICY_FSTYPE_MASK + 1) + 1]; /* one string per known flag at most */ |
| 470 |
size_t m = 0; |
| 471 |
|
| 472 |
assert(ret); |
| 473 |
|
| 474 |
if (flags < 0) |
| 475 |
return -EINVAL; |
| 476 |
|
| 477 |
/* If 'simplify' is false we'll output the precise value of every single flag. |
| 478 |
* |
| 479 |
* If 'simplify' is true we'll try to make the output shorter, by doing the following: |
| 480 |
* |
| 481 |
* → we'll spell the long form "verity+signed+encrypted+unprotected+unused+absent" via its |
| 482 |
* equivalent shortcut form "open" (which we happily parse btw, see above) |
| 483 |
* |
| 484 |
* → we'll spell the long form "unused+absent" via its shortcut "ignore" (which we are also happy |
| 485 |
* to parse) |
| 486 |
* |
| 487 |
* → if the read-only/growfs policy flags are both set, we suppress them. this thus removes the |
| 488 |
* distinction between "user explicitly declared don't care" and "we implied don't care because |
| 489 |
* user didn't say anything". |
| 490 |
* |
| 491 |
* net result: the resulting string is shorter, but the effective policy declared that way will have |
| 492 |
* the same results as the long form. */ |
| 493 |
|
| 494 |
if (simplify && (flags & _PARTITION_POLICY_USE_MASK) == PARTITION_POLICY_OPEN) |
| 495 |
l[m++] = "open"; |
| 496 |
else if (simplify && (flags & _PARTITION_POLICY_USE_MASK) == PARTITION_POLICY_IGNORE) |
| 497 |
l[m++] = "ignore"; |
| 498 |
else { |
| 499 |
if (flags & PARTITION_POLICY_VERITY) |
| 500 |
l[m++] = "verity"; |
| 501 |
if (flags & PARTITION_POLICY_SIGNED) |
| 502 |
l[m++] = "signed"; |
| 503 |
if (flags & PARTITION_POLICY_ENCRYPTED) |
| 504 |
l[m++] = "encrypted"; |
| 505 |
if (flags & PARTITION_POLICY_ENCRYPTEDWITHINTEGRITY) |
| 506 |
l[m++] = "encryptedwithintegrity"; |
| 507 |
if (flags & PARTITION_POLICY_UNPROTECTED) |
| 508 |
l[m++] = "unprotected"; |
| 509 |
if (flags & PARTITION_POLICY_UNUSED) |
| 510 |
l[m++] = "unused"; |
| 511 |
if (flags & PARTITION_POLICY_ABSENT) |
| 512 |
l[m++] = "absent"; |
| 513 |
} |
| 514 |
|
| 515 |
if (!simplify || (!(flags & PARTITION_POLICY_READ_ONLY_ON) != !(flags & PARTITION_POLICY_READ_ONLY_OFF))) { |
| 516 |
if (flags & PARTITION_POLICY_READ_ONLY_ON) |
| 517 |
l[m++] = "read-only-on"; |
| 518 |
if (flags & PARTITION_POLICY_READ_ONLY_OFF) |
| 519 |
l[m++] = "read-only-off"; |
| 520 |
} |
| 521 |
|
| 522 |
if (!simplify || (!(flags & PARTITION_POLICY_GROWFS_ON) != !(flags & PARTITION_POLICY_GROWFS_OFF))) { |
| 523 |
if (flags & PARTITION_POLICY_GROWFS_OFF) |
| 524 |
l[m++] = "growfs-off"; |
| 525 |
if (flags & PARTITION_POLICY_GROWFS_ON) |
| 526 |
l[m++] = "growfs-on"; |
| 527 |
} |
| 528 |
|
| 529 |
/* These flags must translate to the literal fstype name of each filesystem, as accepted by |
| 530 |
* `mount -t`. */ |
| 531 |
if (flags & PARTITION_POLICY_BTRFS) |
| 532 |
l[m++] = "btrfs"; |
| 533 |
if (flags & PARTITION_POLICY_EROFS) |
| 534 |
l[m++] = "erofs"; |
| 535 |
if (flags & PARTITION_POLICY_EXT4) |
| 536 |
l[m++] = "ext4"; |
| 537 |
if (flags & PARTITION_POLICY_F2FS) |
| 538 |
l[m++] = "f2fs"; |
| 539 |
if (flags & PARTITION_POLICY_SQUASHFS) |
| 540 |
l[m++] = "squashfs"; |
| 541 |
if (flags & PARTITION_POLICY_VFAT) |
| 542 |
l[m++] = "vfat"; |
| 543 |
if (flags & PARTITION_POLICY_XFS) |
| 544 |
l[m++] = "xfs"; |
| 545 |
|
| 546 |
if (m == 0) |
| 547 |
buf = strdup("-"); |
| 548 |
else { |
| 549 |
assert(m < ELEMENTSOF(l)); |
| 550 |
l[m] = NULL; |
| 551 |
|
| 552 |
buf = strv_join((char**) l, "+"); |
| 553 |
} |
| 554 |
if (!buf) |
| 555 |
return -ENOMEM; |
| 556 |
|
| 557 |
*ret = TAKE_PTR(buf); |
| 558 |
return (int) m; |
| 559 |
} |
| 560 |
|
| 561 |
int partition_policy_determine_fstype( |
| 562 |
const ImagePolicy *policy, |
| 563 |
PartitionDesignator designator, |
| 564 |
bool *ret_encrypted, |
| 565 |
char **ret_fstype) { |
| 566 |
|
| 567 |
_cleanup_free_ char *fstype = NULL; |
| 568 |
PartitionPolicyFlags policy_flags; |
| 569 |
int r; |
| 570 |
|
| 571 |
assert(designator >= 0 && designator < _PARTITION_DESIGNATOR_MAX); |
| 572 |
assert(ret_fstype); |
| 573 |
|
| 574 |
policy_flags = image_policy_get_exhaustively(policy, designator); |
| 575 |
if (policy_flags < 0) |
| 576 |
return policy_flags; |
| 577 |
|
| 578 |
/* The policy fstype flags translate to the literal fstype name of each filesystem. */ |
| 579 |
r = partition_policy_flags_to_string(policy_flags & _PARTITION_POLICY_FSTYPE_MASK, /* simplify= */ true, &fstype); |
| 580 |
if (r < 0) |
| 581 |
return r; |
| 582 |
/* Input must be a single filesystem type, if the policy specifies more than one, return NULL */ |
| 583 |
if (r != 1) { |
| 584 |
if (ret_encrypted) |
| 585 |
*ret_encrypted = false; |
| 586 |
*ret_fstype = NULL; |
| 587 |
return 0; |
| 588 |
} |
| 589 |
|
| 590 |
/* If the policy also allows unprotected or verity filesystems, don't set the 'encrypted' flag */ |
| 591 |
if (ret_encrypted) |
| 592 |
*ret_encrypted = (policy_flags & (PARTITION_POLICY_ENCRYPTED|PARTITION_POLICY_ENCRYPTEDWITHINTEGRITY)) && |
| 593 |
!(policy_flags & (PARTITION_POLICY_VERITY|PARTITION_POLICY_SIGNED|PARTITION_POLICY_UNPROTECTED)); |
| 594 |
*ret_fstype = TAKE_PTR(fstype); |
| 595 |
return 1; |
| 596 |
} |
| 597 |
|
| 598 |
static bool partition_policy_flags_extended_equal(PartitionPolicyFlags a, PartitionPolicyFlags b) { |
| 599 |
return partition_policy_flags_extend(a) == partition_policy_flags_extend(b); |
| 600 |
} |
| 601 |
|
| 602 |
static int image_policy_flags_all_match(const ImagePolicy *policy, PartitionPolicyFlags expected) { |
| 603 |
|
| 604 |
if (expected < 0) |
| 605 |
return -EINVAL; |
| 606 |
|
| 607 |
if (!partition_policy_flags_extended_equal(image_policy_default(policy), expected)) |
| 608 |
return false; |
| 609 |
|
| 610 |
for (PartitionDesignator d = 0; d < _PARTITION_DESIGNATOR_MAX; d++) { |
| 611 |
PartitionPolicyFlags f, w; |
| 612 |
|
| 613 |
f = image_policy_get_exhaustively(policy, d); |
| 614 |
if (f < 0) |
| 615 |
return f; |
| 616 |
|
| 617 |
w = partition_policy_normalized_flags( |
| 618 |
&(const PartitionPolicy) { |
| 619 |
.flags = expected, |
| 620 |
.designator = d, |
| 621 |
}); |
| 622 |
if (w < 0) |
| 623 |
return w; |
| 624 |
if (f != w) |
| 625 |
return false; |
| 626 |
} |
| 627 |
|
| 628 |
return true; |
| 629 |
} |
| 630 |
|
| 631 |
bool image_policy_equiv_ignore(const ImagePolicy *policy) { |
| 632 |
/* Checks if this is the ignore policy (or equivalent to it), i.e. everything is ignored, aka '-', aka '' */ |
| 633 |
return image_policy_flags_all_match(policy, PARTITION_POLICY_IGNORE); |
| 634 |
} |
| 635 |
|
| 636 |
bool image_policy_equiv_allow(const ImagePolicy *policy) { |
| 637 |
/* Checks if this is the allow policy (or equivalent to it), i.e. everything is allowed, aka '*' */ |
| 638 |
return image_policy_flags_all_match(policy, PARTITION_POLICY_OPEN); |
| 639 |
} |
| 640 |
|
| 641 |
bool image_policy_equiv_deny(const ImagePolicy *policy) { |
| 642 |
/* Checks if this is the deny policy (or equivalent to it), i.e. everything must be absent, aka '~' */ |
| 643 |
return image_policy_flags_all_match(policy, PARTITION_POLICY_ABSENT); |
| 644 |
} |
| 645 |
|
| 646 |
int image_policy_to_string(const ImagePolicy *policy, bool simplify, char **ret) { |
| 647 |
_cleanup_free_ char *s = NULL; |
| 648 |
int r; |
| 649 |
|
| 650 |
assert(ret); |
| 651 |
|
| 652 |
if (simplify) { |
| 653 |
const char *fixed; |
| 654 |
|
| 655 |
if (image_policy_equiv_allow(policy)) |
| 656 |
fixed = "*"; |
| 657 |
else if (image_policy_equiv_ignore(policy)) |
| 658 |
fixed = "-"; |
| 659 |
else if (image_policy_equiv_deny(policy)) |
| 660 |
fixed = "~"; |
| 661 |
else |
| 662 |
fixed = NULL; |
| 663 |
|
| 664 |
if (fixed) { |
| 665 |
s = strdup(fixed); |
| 666 |
if (!s) |
| 667 |
return -ENOMEM; |
| 668 |
|
| 669 |
*ret = TAKE_PTR(s); |
| 670 |
return 0; |
| 671 |
} |
| 672 |
} |
| 673 |
|
| 674 |
for (size_t i = 0; i < image_policy_n_entries(policy); i++) { |
| 675 |
const PartitionPolicy *p = policy->policies + i; |
| 676 |
_cleanup_free_ char *f = NULL; |
| 677 |
const char *t; |
| 678 |
|
| 679 |
assert(i == 0 || p->designator > policy->policies[i-1].designator); /* Validate perfect ordering */ |
| 680 |
|
| 681 |
assert_se(t = partition_designator_to_string(p->designator)); |
| 682 |
|
| 683 |
if (simplify) { |
| 684 |
/* Skip policy entries that match the default anyway */ |
| 685 |
PartitionPolicyFlags df; |
| 686 |
|
| 687 |
df = partition_policy_normalized_flags( |
| 688 |
&(const PartitionPolicy) { |
| 689 |
.flags = image_policy_default(policy), |
| 690 |
.designator = p->designator, |
| 691 |
}); |
| 692 |
if (df < 0) |
| 693 |
return df; |
| 694 |
|
| 695 |
if (df == p->flags) |
| 696 |
continue; |
| 697 |
} |
| 698 |
|
| 699 |
r = partition_policy_flags_to_string(p->flags, simplify, &f); |
| 700 |
if (r < 0) |
| 701 |
return r; |
| 702 |
|
| 703 |
if (!strextend(&s, isempty(s) ? "" : ":", t, "=", f)) |
| 704 |
return -ENOMEM; |
| 705 |
} |
| 706 |
|
| 707 |
if (!simplify || !partition_policy_flags_extended_equal(image_policy_default(policy), PARTITION_POLICY_IGNORE)) { |
| 708 |
_cleanup_free_ char *df = NULL; |
| 709 |
|
| 710 |
r = partition_policy_flags_to_string(image_policy_default(policy), simplify, &df); |
| 711 |
if (r < 0) |
| 712 |
return r; |
| 713 |
|
| 714 |
if (!strextend(&s, isempty(s) ? "" : ":", "=", df)) |
| 715 |
return -ENOMEM; |
| 716 |
} |
| 717 |
|
| 718 |
if (isempty(s)) { /* no rule and default policy? then let's return "-" */ |
| 719 |
s = strdup("-"); |
| 720 |
if (!s) |
| 721 |
return -ENOMEM; |
| 722 |
} |
| 723 |
|
| 724 |
*ret = TAKE_PTR(s); |
| 725 |
return 0; |
| 726 |
} |
| 727 |
|
| 728 |
bool image_policy_equal(const ImagePolicy *a, const ImagePolicy *b) { |
| 729 |
if (a == b) |
| 730 |
return true; |
| 731 |
if (image_policy_n_entries(a) != image_policy_n_entries(b)) |
| 732 |
return false; |
| 733 |
if (image_policy_default(a) != image_policy_default(b)) |
| 734 |
return false; |
| 735 |
for (size_t i = 0; i < image_policy_n_entries(a); i++) { |
| 736 |
if (a->policies[i].designator != b->policies[i].designator) |
| 737 |
return false; |
| 738 |
if (a->policies[i].flags != b->policies[i].flags) |
| 739 |
return false; |
| 740 |
} |
| 741 |
|
| 742 |
return true; |
| 743 |
} |
| 744 |
|
| 745 |
int image_policy_equivalent(const ImagePolicy *a, const ImagePolicy *b) { |
| 746 |
|
| 747 |
/* The image_policy_equal() function checks if the policy is defined the exact same way. This |
| 748 |
* function here instead looks at the outcome of the two policies instead. Where does this come to |
| 749 |
* different results you ask? We imply some logic regarding Verity/Encryption: when no rule is |
| 750 |
* defined for a verity partition we can synthesize it from the protection level of the data |
| 751 |
* partition it protects. Or: any per-partition rule that is identical to the default rule is |
| 752 |
* redundant, and will be recognized as such by image_policy_equivalent() but not by |
| 753 |
* image_policy_equal()- */ |
| 754 |
|
| 755 |
if (!partition_policy_flags_extended_equal(image_policy_default(a), image_policy_default(b))) |
| 756 |
return false; |
| 757 |
|
| 758 |
for (PartitionDesignator d = 0; d < _PARTITION_DESIGNATOR_MAX; d++) { |
| 759 |
PartitionPolicyFlags f, w; |
| 760 |
|
| 761 |
f = image_policy_get_exhaustively(a, d); |
| 762 |
if (f < 0) |
| 763 |
return f; |
| 764 |
|
| 765 |
w = image_policy_get_exhaustively(b, d); |
| 766 |
if (w < 0) |
| 767 |
return w; |
| 768 |
|
| 769 |
if (f != w) |
| 770 |
return false; |
| 771 |
} |
| 772 |
|
| 773 |
return true; |
| 774 |
} |
| 775 |
|
| 776 |
int config_parse_image_policy( |
| 777 |
const char *unit, |
| 778 |
const char *filename, |
| 779 |
unsigned line, |
| 780 |
const char *section, |
| 781 |
unsigned section_line, |
| 782 |
const char *lvalue, |
| 783 |
int ltype, |
| 784 |
const char *rvalue, |
| 785 |
void *data, |
| 786 |
void *userdata) { |
| 787 |
|
| 788 |
_cleanup_(image_policy_freep) ImagePolicy *np = NULL; |
| 789 |
ImagePolicy **p = ASSERT_PTR(data); |
| 790 |
int r; |
| 791 |
|
| 792 |
assert(rvalue); |
| 793 |
|
| 794 |
if (isempty(rvalue)) { |
| 795 |
*p = image_policy_free(*p); |
| 796 |
return 0; |
| 797 |
} |
| 798 |
|
| 799 |
r = image_policy_from_string(rvalue, /* graceful */ true, &np); |
| 800 |
if (r == -ENOTUNIQ) |
| 801 |
return log_syntax(unit, LOG_ERR, filename, line, r, "Duplicate rule in image policy, refusing: %s", rvalue); |
| 802 |
if (r == -EBADSLT) |
| 803 |
return log_syntax(unit, LOG_ERR, filename, line, r, "Unknown partition type in image policy, refusing: %s", rvalue); |
| 804 |
if (r == -EBADRQC) |
| 805 |
return log_syntax(unit, LOG_ERR, filename, line, r, "Unknown partition policy flag in image policy, refusing: %s", rvalue); |
| 806 |
if (r < 0) |
| 807 |
return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse image policy, refusing: %s", rvalue); |
| 808 |
|
| 809 |
return free_and_replace_full(*p, np, image_policy_free); |
| 810 |
} |
| 811 |
|
| 812 |
int parse_image_policy_argument(const char *s, ImagePolicy **policy) { |
| 813 |
_cleanup_(image_policy_freep) ImagePolicy *np = NULL; |
| 814 |
int r; |
| 815 |
|
| 816 |
assert(s); |
| 817 |
assert(policy); |
| 818 |
|
| 819 |
/* |
| 820 |
* This function is intended to be used in command line parsers. |
| 821 |
* |
| 822 |
* NOTE THAT THIS WILL FREE THE PREVIOUS ARGUMENT POINTER ON SUCCESS! |
| 823 |
* Hence, do not pass in uninitialized pointers. |
| 824 |
*/ |
| 825 |
|
| 826 |
r = image_policy_from_string(s, /* graceful= */ false, &np); |
| 827 |
if (r == -ENOTUNIQ) |
| 828 |
return log_error_errno(r, "Duplicate rule in image policy: %s", s); |
| 829 |
if (r == -EBADSLT) |
| 830 |
return log_error_errno(r, "Unknown partition type in image policy: %s", s); |
| 831 |
if (r == -EBADRQC) |
| 832 |
return log_error_errno(r, "Unknown partition policy flag in image policy: %s", s); |
| 833 |
if (r < 0) |
| 834 |
return log_error_errno(r, "Failed to parse image policy: %s", s); |
| 835 |
|
| 836 |
return free_and_replace_full(*policy, np, image_policy_free); |
| 837 |
} |
| 838 |
|
| 839 |
static bool partition_policy_flags_has_unspecified(PartitionPolicyFlags flags) { |
| 840 |
|
| 841 |
if ((flags & _PARTITION_POLICY_USE_MASK) == 0) |
| 842 |
return true; |
| 843 |
if ((flags & _PARTITION_POLICY_READ_ONLY_MASK) == 0) |
| 844 |
return true; |
| 845 |
if ((flags & _PARTITION_POLICY_GROWFS_MASK) == 0) |
| 846 |
return true; |
| 847 |
|
| 848 |
return false; |
| 849 |
} |
| 850 |
|
| 851 |
static PartitionPolicyFlags policy_flags_or(PartitionPolicyFlags a, PartitionPolicyFlags b) { |
| 852 |
return a | b; |
| 853 |
} |
| 854 |
|
| 855 |
static PartitionPolicyFlags policy_flags_and(PartitionPolicyFlags a, PartitionPolicyFlags b) { |
| 856 |
return a & b; |
| 857 |
} |
| 858 |
|
| 859 |
static int policy_intersect_or_union( |
| 860 |
const ImagePolicy *a, |
| 861 |
const ImagePolicy *b, |
| 862 |
PartitionPolicyFlags (*op)(PartitionPolicyFlags a, PartitionPolicyFlags b), |
| 863 |
ImagePolicy **ret) { |
| 864 |
|
| 865 |
_cleanup_(image_policy_freep) ImagePolicy *p = NULL; |
| 866 |
|
| 867 |
assert(op); |
| 868 |
|
| 869 |
/* Calculates the intersection or union of the specified policies, i.e. only what is permitted in |
| 870 |
* both or either. This might fail with -ENAVAIL if the intersection is an "impossible policy". For |
| 871 |
* example, if a root partition my neither be used, nor be absent, nor be unused then this is |
| 872 |
* considered "impossible". */ |
| 873 |
|
| 874 |
p = image_policy_new(_PARTITION_DESIGNATOR_MAX); |
| 875 |
if (!p) |
| 876 |
return -ENOMEM; |
| 877 |
|
| 878 |
p->default_flags = op(partition_policy_flags_extend(image_policy_default(a)), |
| 879 |
partition_policy_flags_extend(image_policy_default(b))); |
| 880 |
|
| 881 |
if (partition_policy_flags_has_unspecified(p->default_flags)) /* Intersection empty? */ |
| 882 |
return -ENAVAIL; |
| 883 |
|
| 884 |
p->default_flags = partition_policy_flags_reduce(p->default_flags); |
| 885 |
|
| 886 |
for (PartitionDesignator d = 0; d < _PARTITION_DESIGNATOR_MAX; d++) { |
| 887 |
PartitionPolicyFlags x, y, z, df; |
| 888 |
|
| 889 |
/* If this designator has no entry in either policy we don't need to include it in the intersection either. */ |
| 890 |
if (!image_policy_bsearch(a, d) && !image_policy_bsearch(b, d)) |
| 891 |
continue; |
| 892 |
|
| 893 |
/* Expand this policy flags field to the "long" form, i.e. for each part of the flags that |
| 894 |
* are left unspcified add in all possible options */ |
| 895 |
x = image_policy_get_exhaustively(a, d); |
| 896 |
if (x < 0) |
| 897 |
return x; |
| 898 |
|
| 899 |
y = image_policy_get_exhaustively(b, d); |
| 900 |
if (y < 0) |
| 901 |
return y; |
| 902 |
|
| 903 |
/* Mask it */ |
| 904 |
z = op(x, y); |
| 905 |
|
| 906 |
/* Check if the intersection is empty for this partition. If so, generate a clear error. |
| 907 |
* If the partition has to be absent, then it won't have read-only/growfs flags, as |
| 908 |
* image_policy_get_exhaustively() intentionally strips them, so skip the check. */ |
| 909 |
if (z != PARTITION_POLICY_ABSENT && partition_policy_flags_has_unspecified(z)) |
| 910 |
return -ENAVAIL; |
| 911 |
|
| 912 |
df = partition_policy_normalized_flags( |
| 913 |
&(const PartitionPolicy) { |
| 914 |
.flags = image_policy_default(p), |
| 915 |
.designator = d, |
| 916 |
}); |
| 917 |
if (df < 0) |
| 918 |
return df; |
| 919 |
if (df == z) /* Same as default? then let's skip this */ |
| 920 |
continue; |
| 921 |
|
| 922 |
/* image_policy_get_exhaustively() may have extended the flags mask to include all |
| 923 |
* read-only/growfs flags if not set. Let's remove them again, if they are both set to |
| 924 |
* minimize the policy again. */ |
| 925 |
z = partition_policy_flags_reduce(z); |
| 926 |
|
| 927 |
p->policies[p->n_policies++] = (struct PartitionPolicy) { |
| 928 |
.designator = d, |
| 929 |
.flags = z, |
| 930 |
}; |
| 931 |
} |
| 932 |
|
| 933 |
if (ret) |
| 934 |
*ret = TAKE_PTR(p); |
| 935 |
|
| 936 |
return 0; |
| 937 |
} |
| 938 |
|
| 939 |
int image_policy_intersect(const ImagePolicy *a, const ImagePolicy *b, ImagePolicy **ret) { |
| 940 |
return policy_intersect_or_union(a, b, policy_flags_and, ret); |
| 941 |
} |
| 942 |
|
| 943 |
int image_policy_union(const ImagePolicy *a, const ImagePolicy *b, ImagePolicy **ret) { |
| 944 |
return policy_intersect_or_union(a, b, policy_flags_or, ret); |
| 945 |
} |
| 946 |
|
| 947 |
ImagePolicy* image_policy_free(ImagePolicy *p) { |
| 948 |
return mfree(p); |
| 949 |
} |
| 950 |
|
| 951 |
int image_policy_ignore_designators(const ImagePolicy *p, const PartitionDesignator table[], size_t n_table, ImagePolicy **ret) { |
| 952 |
assert(p); |
| 953 |
assert(table || n_table == 0); |
| 954 |
assert(ret); |
| 955 |
|
| 956 |
/* Patches the specified image policy, replacing the policy for the specified designators by an |
| 957 |
* "ignore" policy. Returns a patched copy. This is useful in context where only some of the |
| 958 |
* available partitions shall be mounted, and hence the policy for the others really doesn't |
| 959 |
* matter. */ |
| 960 |
|
| 961 |
_cleanup_(image_policy_freep) ImagePolicy *np = image_policy_new(_PARTITION_DESIGNATOR_MAX); |
| 962 |
if (!np) |
| 963 |
return -ENOMEM; |
| 964 |
|
| 965 |
FOREACH_ARRAY(t, table, n_table) { |
| 966 |
assert(*t >= 0); |
| 967 |
assert(*t < _PARTITION_DESIGNATOR_MAX); |
| 968 |
|
| 969 |
if (image_policy_bsearch(np, *t)) |
| 970 |
continue; |
| 971 |
|
| 972 |
/* Insert an ignore policy for this entry, and sort it to the right place, so that image_policy_bsearch() can work */ |
| 973 |
np->policies[np->n_policies++] = (PartitionPolicy) { |
| 974 |
.designator = *t, |
| 975 |
.flags = PARTITION_POLICY_IGNORE, |
| 976 |
}; |
| 977 |
typesafe_qsort(np->policies, np->n_policies, partition_policy_compare); |
| 978 |
} |
| 979 |
|
| 980 |
FOREACH_ARRAY(i, p->policies, p->n_policies) { |
| 981 |
|
| 982 |
if (image_policy_bsearch(np, i->designator)) |
| 983 |
continue; |
| 984 |
|
| 985 |
/* Copy the policy entry from the old image policy, and sort it to the right place, so that image_policy_bsearch() can work */ |
| 986 |
np->policies[np->n_policies++] = *i; |
| 987 |
typesafe_qsort(np->policies, np->n_policies, partition_policy_compare); |
| 988 |
} |
| 989 |
|
| 990 |
np->default_flags = p->default_flags; |
| 991 |
|
| 992 |
/* Return unused space to libc */ |
| 993 |
ImagePolicy *t = realloc(np, offsetof(ImagePolicy, policies) + sizeof(PartitionPolicy) * np->n_policies); |
| 994 |
if (t) |
| 995 |
np = t; |
| 996 |
|
| 997 |
*ret = TAKE_PTR(np); |
| 998 |
return 0; |
| 999 |
} |
| 1000 |
|
| 1001 |
const ImagePolicy image_policy_allow = { |
| 1002 |
/* Allow policy */ |
| 1003 |
.n_policies = 0, |
| 1004 |
.default_flags = PARTITION_POLICY_OPEN, |
| 1005 |
}; |
| 1006 |
|
| 1007 |
const ImagePolicy image_policy_deny = { |
| 1008 |
/* Deny policy */ |
| 1009 |
.n_policies = 0, |
| 1010 |
.default_flags = PARTITION_POLICY_ABSENT, |
| 1011 |
}; |
| 1012 |
|
| 1013 |
const ImagePolicy image_policy_ignore = { |
| 1014 |
/* Ignore policy */ |
| 1015 |
.n_policies = 0, |
| 1016 |
.default_flags = PARTITION_POLICY_IGNORE, |
| 1017 |
}; |
| 1018 |
|
| 1019 |
const ImagePolicy image_policy_sysext = { |
| 1020 |
/* For system extensions, honour root file system, and /usr/ and ignore everything else. After all, |
| 1021 |
* we are only interested in /usr/ + /opt/ trees anyway, and that's really the only place they can |
| 1022 |
* be. */ |
| 1023 |
.n_policies = 2, |
| 1024 |
.policies = { |
| 1025 |
{ PARTITION_ROOT, PARTITION_POLICY_VERITY|PARTITION_POLICY_SIGNED|PARTITION_POLICY_ENCRYPTED|PARTITION_POLICY_ENCRYPTEDWITHINTEGRITY|PARTITION_POLICY_UNPROTECTED|PARTITION_POLICY_ABSENT }, |
| 1026 |
{ PARTITION_USR, PARTITION_POLICY_VERITY|PARTITION_POLICY_SIGNED|PARTITION_POLICY_ENCRYPTED|PARTITION_POLICY_ENCRYPTEDWITHINTEGRITY|PARTITION_POLICY_UNPROTECTED|PARTITION_POLICY_ABSENT }, |
| 1027 |
}, |
| 1028 |
.default_flags = PARTITION_POLICY_IGNORE, |
| 1029 |
}; |
| 1030 |
|
| 1031 |
const ImagePolicy image_policy_sysext_strict = { |
| 1032 |
/* For system extensions, requiring signing */ |
| 1033 |
.n_policies = 2, |
| 1034 |
.policies = { |
| 1035 |
{ PARTITION_ROOT, PARTITION_POLICY_SIGNED|PARTITION_POLICY_ABSENT }, |
| 1036 |
{ PARTITION_USR, PARTITION_POLICY_SIGNED|PARTITION_POLICY_ABSENT }, |
| 1037 |
}, |
| 1038 |
.default_flags = PARTITION_POLICY_IGNORE, |
| 1039 |
}; |
| 1040 |
|
| 1041 |
const ImagePolicy image_policy_confext = { |
| 1042 |
/* For configuration extensions, honour root file system, and ignore everything else. After all, we |
| 1043 |
* are only interested in the /etc/ tree anyway, and that's really the only place it can be. */ |
| 1044 |
.n_policies = 1, |
| 1045 |
.policies = { |
| 1046 |
{ PARTITION_ROOT, PARTITION_POLICY_VERITY|PARTITION_POLICY_SIGNED|PARTITION_POLICY_ENCRYPTED|PARTITION_POLICY_ENCRYPTEDWITHINTEGRITY|PARTITION_POLICY_UNPROTECTED|PARTITION_POLICY_ABSENT }, |
| 1047 |
}, |
| 1048 |
.default_flags = PARTITION_POLICY_IGNORE, |
| 1049 |
}; |
| 1050 |
|
| 1051 |
const ImagePolicy image_policy_confext_strict = { |
| 1052 |
.n_policies = 1, |
| 1053 |
.policies = { |
| 1054 |
{ PARTITION_ROOT, PARTITION_POLICY_SIGNED|PARTITION_POLICY_ABSENT }, |
| 1055 |
}, |
| 1056 |
.default_flags = PARTITION_POLICY_IGNORE, |
| 1057 |
}; |
| 1058 |
|
| 1059 |
const ImagePolicy image_policy_container = { |
| 1060 |
/* For systemd-nspawn containers we use all partitions, with the exception of swap */ |
| 1061 |
.n_policies = 8, |
| 1062 |
.policies = { |
| 1063 |
{ PARTITION_ROOT, PARTITION_POLICY_VERITY|PARTITION_POLICY_SIGNED|PARTITION_POLICY_ENCRYPTED|PARTITION_POLICY_ENCRYPTEDWITHINTEGRITY|PARTITION_POLICY_UNPROTECTED|PARTITION_POLICY_ABSENT }, |
| 1064 |
{ PARTITION_USR, PARTITION_POLICY_VERITY|PARTITION_POLICY_SIGNED|PARTITION_POLICY_ENCRYPTED|PARTITION_POLICY_ENCRYPTEDWITHINTEGRITY|PARTITION_POLICY_UNPROTECTED|PARTITION_POLICY_ABSENT }, |
| 1065 |
{ PARTITION_HOME, PARTITION_POLICY_ENCRYPTED|PARTITION_POLICY_ENCRYPTEDWITHINTEGRITY|PARTITION_POLICY_UNPROTECTED|PARTITION_POLICY_ABSENT }, |
| 1066 |
{ PARTITION_SRV, PARTITION_POLICY_ENCRYPTED|PARTITION_POLICY_ENCRYPTEDWITHINTEGRITY|PARTITION_POLICY_UNPROTECTED|PARTITION_POLICY_ABSENT }, |
| 1067 |
{ PARTITION_ESP, PARTITION_POLICY_UNPROTECTED|PARTITION_POLICY_ABSENT }, |
| 1068 |
{ PARTITION_XBOOTLDR, PARTITION_POLICY_UNPROTECTED|PARTITION_POLICY_ABSENT }, |
| 1069 |
{ PARTITION_TMP, PARTITION_POLICY_ENCRYPTED|PARTITION_POLICY_ENCRYPTEDWITHINTEGRITY|PARTITION_POLICY_UNPROTECTED|PARTITION_POLICY_ABSENT }, |
| 1070 |
{ PARTITION_VAR, PARTITION_POLICY_ENCRYPTED|PARTITION_POLICY_ENCRYPTEDWITHINTEGRITY|PARTITION_POLICY_UNPROTECTED|PARTITION_POLICY_ABSENT }, |
| 1071 |
}, |
| 1072 |
.default_flags = PARTITION_POLICY_IGNORE, |
| 1073 |
}; |
| 1074 |
|
| 1075 |
const ImagePolicy image_policy_host = { |
| 1076 |
/* For the host policy we basically use everything */ |
| 1077 |
.n_policies = 9, |
| 1078 |
.policies = { |
| 1079 |
{ PARTITION_ROOT, PARTITION_POLICY_VERITY|PARTITION_POLICY_SIGNED|PARTITION_POLICY_ENCRYPTED|PARTITION_POLICY_ENCRYPTEDWITHINTEGRITY|PARTITION_POLICY_UNPROTECTED|PARTITION_POLICY_ABSENT }, |
| 1080 |
{ PARTITION_USR, PARTITION_POLICY_VERITY|PARTITION_POLICY_SIGNED|PARTITION_POLICY_ENCRYPTED|PARTITION_POLICY_ENCRYPTEDWITHINTEGRITY|PARTITION_POLICY_UNPROTECTED|PARTITION_POLICY_ABSENT }, |
| 1081 |
{ PARTITION_HOME, PARTITION_POLICY_ENCRYPTED|PARTITION_POLICY_ENCRYPTEDWITHINTEGRITY|PARTITION_POLICY_UNPROTECTED|PARTITION_POLICY_ABSENT }, |
| 1082 |
{ PARTITION_SRV, PARTITION_POLICY_ENCRYPTED|PARTITION_POLICY_ENCRYPTEDWITHINTEGRITY|PARTITION_POLICY_UNPROTECTED|PARTITION_POLICY_ABSENT }, |
| 1083 |
{ PARTITION_ESP, PARTITION_POLICY_UNPROTECTED|PARTITION_POLICY_ABSENT }, |
| 1084 |
{ PARTITION_XBOOTLDR, PARTITION_POLICY_UNPROTECTED|PARTITION_POLICY_ABSENT }, |
| 1085 |
{ PARTITION_SWAP, PARTITION_POLICY_ENCRYPTED|PARTITION_POLICY_ENCRYPTEDWITHINTEGRITY|PARTITION_POLICY_UNPROTECTED|PARTITION_POLICY_ABSENT }, |
| 1086 |
{ PARTITION_TMP, PARTITION_POLICY_ENCRYPTED|PARTITION_POLICY_ENCRYPTEDWITHINTEGRITY|PARTITION_POLICY_UNPROTECTED|PARTITION_POLICY_ABSENT }, |
| 1087 |
{ PARTITION_VAR, PARTITION_POLICY_ENCRYPTED|PARTITION_POLICY_ENCRYPTEDWITHINTEGRITY|PARTITION_POLICY_UNPROTECTED|PARTITION_POLICY_ABSENT }, |
| 1088 |
}, |
| 1089 |
.default_flags = PARTITION_POLICY_IGNORE, |
| 1090 |
}; |
| 1091 |
|
| 1092 |
const ImagePolicy image_policy_service = { |
| 1093 |
/* For RootImage= in services we skip ESP/XBOOTLDR and swap */ |
| 1094 |
.n_policies = 6, |
| 1095 |
.policies = { |
| 1096 |
{ PARTITION_ROOT, PARTITION_POLICY_VERITY|PARTITION_POLICY_SIGNED|PARTITION_POLICY_ENCRYPTED|PARTITION_POLICY_ENCRYPTEDWITHINTEGRITY|PARTITION_POLICY_UNPROTECTED|PARTITION_POLICY_ABSENT }, |
| 1097 |
{ PARTITION_USR, PARTITION_POLICY_VERITY|PARTITION_POLICY_SIGNED|PARTITION_POLICY_ENCRYPTED|PARTITION_POLICY_ENCRYPTEDWITHINTEGRITY|PARTITION_POLICY_UNPROTECTED|PARTITION_POLICY_ABSENT }, |
| 1098 |
{ PARTITION_HOME, PARTITION_POLICY_ENCRYPTED|PARTITION_POLICY_ENCRYPTEDWITHINTEGRITY|PARTITION_POLICY_UNPROTECTED|PARTITION_POLICY_ABSENT }, |
| 1099 |
{ PARTITION_SRV, PARTITION_POLICY_ENCRYPTED|PARTITION_POLICY_ENCRYPTEDWITHINTEGRITY|PARTITION_POLICY_UNPROTECTED|PARTITION_POLICY_ABSENT }, |
| 1100 |
{ PARTITION_TMP, PARTITION_POLICY_ENCRYPTED|PARTITION_POLICY_ENCRYPTEDWITHINTEGRITY|PARTITION_POLICY_UNPROTECTED|PARTITION_POLICY_ABSENT }, |
| 1101 |
{ PARTITION_VAR, PARTITION_POLICY_ENCRYPTED|PARTITION_POLICY_ENCRYPTEDWITHINTEGRITY|PARTITION_POLICY_UNPROTECTED|PARTITION_POLICY_ABSENT }, |
| 1102 |
}, |
| 1103 |
.default_flags = PARTITION_POLICY_IGNORE, |
| 1104 |
}; |