| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
#pragma once |
| 3 |
|
| 4 |
#include "conf-parser-forward.h" |
| 5 |
#include "dissect-image.h" |
| 6 |
#include "shared-forward.h" |
| 7 |
#include "gpt.h" |
| 8 |
|
| 9 |
typedef enum PartitionPolicyFlags { |
| 10 |
/* Not all policy flags really make sense on all partition types, see comments. But even if they |
| 11 |
* don't make sense we'll parse them anyway, because maybe one day we'll add them for more partition |
| 12 |
* types, too. Moreover, we allow configuring a "default" policy for all partition types for which no |
| 13 |
* explicit policy is specified. It's useful if we can use policy flags in there and apply this |
| 14 |
* default policy gracefully even to partition types where they don't really make too much sense |
| 15 |
* on. Example: a default policy of "verity+encrypted" certainly makes sense, but for /home/ |
| 16 |
* partitions this gracefully degrades to "encrypted" (as we do not have a concept of verity for |
| 17 |
* /home/), and so on. */ |
| 18 |
PARTITION_POLICY_VERITY = 1 << 0, /* must exist, activate with verity (only applies to root/usr partitions) */ |
| 19 |
PARTITION_POLICY_SIGNED = 1 << 1, /* must exist, activate with signed verity (only applies to root/usr partitions) */ |
| 20 |
PARTITION_POLICY_ENCRYPTED = 1 << 2, /* must exist, activate with LUKS encryption (applies to any data partition, but not to verity/signature partitions */ |
| 21 |
PARTITION_POLICY_ENCRYPTEDWITHINTEGRITY = 1 << 3, /* same as PARTITION_POLICY_ENCRYPTED but also requires integrity checking */ |
| 22 |
PARTITION_POLICY_UNPROTECTED = 1 << 4, /* must exist, activate without encryption/verity */ |
| 23 |
PARTITION_POLICY_UNUSED = 1 << 5, /* must exist, don't use */ |
| 24 |
PARTITION_POLICY_ABSENT = 1 << 6, /* must not exist */ |
| 25 |
PARTITION_POLICY_OPEN = PARTITION_POLICY_VERITY|PARTITION_POLICY_SIGNED|PARTITION_POLICY_ENCRYPTED|PARTITION_POLICY_ENCRYPTEDWITHINTEGRITY| |
| 26 |
PARTITION_POLICY_UNPROTECTED|PARTITION_POLICY_UNUSED|PARTITION_POLICY_ABSENT, |
| 27 |
PARTITION_POLICY_IGNORE = PARTITION_POLICY_UNUSED|PARTITION_POLICY_ABSENT, |
| 28 |
_PARTITION_POLICY_USE_MASK = PARTITION_POLICY_OPEN, |
| 29 |
|
| 30 |
PARTITION_POLICY_READ_ONLY_OFF = 1 << 7, /* State of GPT partition flag "read-only" must be on */ |
| 31 |
PARTITION_POLICY_READ_ONLY_ON = 1 << 8, |
| 32 |
_PARTITION_POLICY_READ_ONLY_MASK = PARTITION_POLICY_READ_ONLY_OFF|PARTITION_POLICY_READ_ONLY_ON, |
| 33 |
PARTITION_POLICY_GROWFS_OFF = 1 << 9, /* State of GPT partition flag "growfs" must be on */ |
| 34 |
PARTITION_POLICY_GROWFS_ON = 1 << 10, |
| 35 |
_PARTITION_POLICY_GROWFS_MASK = PARTITION_POLICY_GROWFS_OFF|PARTITION_POLICY_GROWFS_ON, |
| 36 |
_PARTITION_POLICY_PFLAGS_MASK = _PARTITION_POLICY_READ_ONLY_MASK|_PARTITION_POLICY_GROWFS_MASK, |
| 37 |
|
| 38 |
_PARTITION_POLICY_MASK = _PARTITION_POLICY_USE_MASK|_PARTITION_POLICY_READ_ONLY_MASK|_PARTITION_POLICY_GROWFS_MASK, |
| 39 |
|
| 40 |
/* Policies can impose filesystem type requirements on images. These flags translate to the literal |
| 41 |
* fstype name of each filesystem. */ |
| 42 |
PARTITION_POLICY_BTRFS = 1 << 11, |
| 43 |
PARTITION_POLICY_EROFS = 1 << 12, |
| 44 |
PARTITION_POLICY_EXT4 = 1 << 13, |
| 45 |
PARTITION_POLICY_F2FS = 1 << 14, |
| 46 |
PARTITION_POLICY_SQUASHFS = 1 << 15, |
| 47 |
PARTITION_POLICY_VFAT = 1 << 16, |
| 48 |
PARTITION_POLICY_XFS = 1 << 17, |
| 49 |
_PARTITION_POLICY_FSTYPE_MASK = PARTITION_POLICY_BTRFS|PARTITION_POLICY_EROFS|PARTITION_POLICY_EXT4| |
| 50 |
PARTITION_POLICY_F2FS|PARTITION_POLICY_SQUASHFS| |
| 51 |
PARTITION_POLICY_VFAT|PARTITION_POLICY_XFS, |
| 52 |
|
| 53 |
_PARTITION_POLICY_FLAGS_INVALID = -EINVAL, |
| 54 |
_PARTITION_POLICY_FLAGS_ERRNO_MAX = -ERRNO_MAX, /* Ensure the whole errno range fits into this enum */ |
| 55 |
} PartitionPolicyFlags; |
| 56 |
|
| 57 |
assert_cc((_PARTITION_POLICY_USE_MASK | _PARTITION_POLICY_PFLAGS_MASK) >= 0); /* ensure flags don't collide with errno range */ |
| 58 |
|
| 59 |
typedef struct PartitionPolicy { |
| 60 |
PartitionDesignator designator; |
| 61 |
PartitionPolicyFlags flags; |
| 62 |
} PartitionPolicy; |
| 63 |
|
| 64 |
typedef struct ImagePolicy { |
| 65 |
PartitionPolicyFlags default_flags; /* for any designator not listed in the list below */ |
| 66 |
size_t n_policies; |
| 67 |
PartitionPolicy policies[]; /* sorted by designator, hence suitable for binary search */ |
| 68 |
} ImagePolicy; |
| 69 |
|
| 70 |
/* Default policies for various use cases */ |
| 71 |
extern const ImagePolicy image_policy_allow; |
| 72 |
extern const ImagePolicy image_policy_deny; |
| 73 |
extern const ImagePolicy image_policy_ignore; |
| 74 |
extern const ImagePolicy image_policy_sysext; /* No verity required */ |
| 75 |
extern const ImagePolicy image_policy_sysext_strict; /* Signed verity required */ |
| 76 |
extern const ImagePolicy image_policy_confext; /* No verity required */ |
| 77 |
extern const ImagePolicy image_policy_confext_strict; /* Signed verity required */ |
| 78 |
extern const ImagePolicy image_policy_container; |
| 79 |
extern const ImagePolicy image_policy_service; |
| 80 |
extern const ImagePolicy image_policy_host; |
| 81 |
|
| 82 |
PartitionPolicyFlags image_policy_get(const ImagePolicy *policy, PartitionDesignator designator); |
| 83 |
PartitionPolicyFlags image_policy_get_exhaustively(const ImagePolicy *policy, PartitionDesignator designator); |
| 84 |
|
| 85 |
/* We want that the NULL image policy means "everything" allowed, hence use these simple accessors to make |
| 86 |
* NULL policies work reasonably */ |
| 87 |
static inline PartitionPolicyFlags image_policy_default(const ImagePolicy *policy) { |
| 88 |
return policy ? policy->default_flags : PARTITION_POLICY_OPEN; |
| 89 |
} |
| 90 |
|
| 91 |
static inline size_t image_policy_n_entries(const ImagePolicy *policy) { |
| 92 |
return policy ? policy->n_policies : 0; |
| 93 |
} |
| 94 |
|
| 95 |
PartitionPolicyFlags partition_policy_flags_extend(PartitionPolicyFlags flags); |
| 96 |
PartitionPolicyFlags partition_policy_flags_reduce(PartitionPolicyFlags flags); |
| 97 |
|
| 98 |
PartitionPolicyFlags partition_policy_flags_from_string(const char *s, bool graceful); |
| 99 |
int partition_policy_flags_to_string(PartitionPolicyFlags flags, bool simplify, char **ret); |
| 100 |
|
| 101 |
int partition_policy_determine_fstype(const ImagePolicy *policy, PartitionDesignator designator, bool *ret_encrypted, char **ret_fstype); |
| 102 |
|
| 103 |
int image_policy_from_string(const char *s, bool graceful, ImagePolicy **ret); |
| 104 |
int image_policy_to_string(const ImagePolicy *policy, bool simplify, char **ret); |
| 105 |
|
| 106 |
/* Recognizes three special policies by equivalence */ |
| 107 |
bool image_policy_equiv_ignore(const ImagePolicy *policy); |
| 108 |
bool image_policy_equiv_allow(const ImagePolicy *policy); |
| 109 |
bool image_policy_equiv_deny(const ImagePolicy *policy); |
| 110 |
|
| 111 |
bool image_policy_equal(const ImagePolicy *a, const ImagePolicy *b); /* checks if defined the same way, i.e. has literally the same ruleset */ |
| 112 |
int image_policy_equivalent(const ImagePolicy *a, const ImagePolicy *b); /* checks if the outcome is the same, i.e. for all partitions results in the same decisions. */ |
| 113 |
|
| 114 |
int image_policy_intersect(const ImagePolicy *a, const ImagePolicy *b, ImagePolicy **ret); |
| 115 |
int image_policy_union(const ImagePolicy *a, const ImagePolicy *b, ImagePolicy **ret); |
| 116 |
|
| 117 |
ImagePolicy* image_policy_new_from_dissected(const DissectedImage *image, const VeritySettings *verity); |
| 118 |
ImagePolicy* image_policy_free(ImagePolicy *p); |
| 119 |
|
| 120 |
DEFINE_TRIVIAL_CLEANUP_FUNC(ImagePolicy*, image_policy_free); |
| 121 |
|
| 122 |
CONFIG_PARSER_PROTOTYPE(config_parse_image_policy); |
| 123 |
int parse_image_policy_argument(const char *s, ImagePolicy **policy); |
| 124 |
|
| 125 |
int image_policy_ignore_designators(const ImagePolicy *p, const PartitionDesignator table[], size_t n_table, ImagePolicy **ret); |