]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
8c1be37e LP |
2 | #pragma once |
3 | ||
8c1be37e LP |
4 | #include <stdbool.h> |
5 | ||
dccca82b LP |
6 | #include "sd-id128.h" |
7 | ||
18d73705 | 8 | #include "list.h" |
6aa05ebd | 9 | #include "loop-util.h" |
8c1be37e LP |
10 | #include "macro.h" |
11 | ||
12 | typedef struct DissectedImage DissectedImage; | |
13 | typedef struct DissectedPartition DissectedPartition; | |
18b5886e | 14 | typedef struct DecryptedImage DecryptedImage; |
18d73705 | 15 | typedef struct MountOptions MountOptions; |
89e62e0b | 16 | typedef struct VeritySettings VeritySettings; |
8c1be37e LP |
17 | |
18 | struct DissectedPartition { | |
19 | bool found:1; | |
20 | bool rw:1; | |
de98f631 | 21 | bool growfs:1; |
8c1be37e LP |
22 | int partno; /* -1 if there was no partition and the images contains a file system directly */ |
23 | int architecture; /* Intended architecture: either native, secondary or unset (-1). */ | |
be30ad41 | 24 | sd_id128_t uuid; /* Partition entry UUID as reported by the GPT */ |
8c1be37e LP |
25 | char *fstype; |
26 | char *node; | |
08fe0a53 | 27 | char *label; |
18b5886e LP |
28 | char *decrypted_node; |
29 | char *decrypted_fstype; | |
18d73705 | 30 | char *mount_options; |
8c1be37e LP |
31 | }; |
32 | ||
569a0e42 | 33 | typedef enum PartitionDesignator { |
8c1be37e LP |
34 | PARTITION_ROOT, |
35 | PARTITION_ROOT_SECONDARY, /* Secondary architecture */ | |
aee36b4e LP |
36 | PARTITION_USR, |
37 | PARTITION_USR_SECONDARY, | |
8c1be37e LP |
38 | PARTITION_HOME, |
39 | PARTITION_SRV, | |
40 | PARTITION_ESP, | |
a8c47660 | 41 | PARTITION_XBOOTLDR, |
8c1be37e | 42 | PARTITION_SWAP, |
4623e8e6 LP |
43 | PARTITION_ROOT_VERITY, /* verity data for the PARTITION_ROOT partition */ |
44 | PARTITION_ROOT_SECONDARY_VERITY, /* verity data for the PARTITION_ROOT_SECONDARY partition */ | |
aee36b4e LP |
45 | PARTITION_USR_VERITY, |
46 | PARTITION_USR_SECONDARY_VERITY, | |
d4dffb85 LP |
47 | PARTITION_TMP, |
48 | PARTITION_VAR, | |
8c1be37e | 49 | _PARTITION_DESIGNATOR_MAX, |
2d93c20e | 50 | _PARTITION_DESIGNATOR_INVALID = -EINVAL, |
569a0e42 | 51 | } PartitionDesignator; |
8c1be37e | 52 | |
08fe0a53 LP |
53 | static inline bool PARTITION_DESIGNATOR_VERSIONED(PartitionDesignator d) { |
54 | /* Returns true for all designators where we want to support a concept of "versioning", i.e. which | |
55 | * likely contain software binaries (or hashes thereof) that make sense to be versioned as a | |
56 | * whole. We use this check to automatically pick the newest version of these partitions, by version | |
57 | * comparing the partition labels. */ | |
58 | ||
59 | return IN_SET(d, | |
60 | PARTITION_ROOT, | |
61 | PARTITION_ROOT_SECONDARY, | |
62 | PARTITION_USR, | |
63 | PARTITION_USR_SECONDARY, | |
64 | PARTITION_ROOT_VERITY, | |
65 | PARTITION_ROOT_SECONDARY_VERITY, | |
66 | PARTITION_USR_VERITY, | |
67 | PARTITION_USR_SECONDARY_VERITY); | |
68 | } | |
69 | ||
569a0e42 | 70 | static inline PartitionDesignator PARTITION_VERITY_OF(PartitionDesignator p) { |
aee36b4e LP |
71 | switch (p) { |
72 | ||
73 | case PARTITION_ROOT: | |
4623e8e6 | 74 | return PARTITION_ROOT_VERITY; |
aee36b4e LP |
75 | |
76 | case PARTITION_ROOT_SECONDARY: | |
4623e8e6 | 77 | return PARTITION_ROOT_SECONDARY_VERITY; |
aee36b4e LP |
78 | |
79 | case PARTITION_USR: | |
80 | return PARTITION_USR_VERITY; | |
81 | ||
82 | case PARTITION_USR_SECONDARY: | |
83 | return PARTITION_USR_SECONDARY_VERITY; | |
84 | ||
85 | default: | |
86 | return _PARTITION_DESIGNATOR_INVALID; | |
87 | } | |
4623e8e6 LP |
88 | } |
89 | ||
18b5886e | 90 | typedef enum DissectImageFlags { |
ef9c184d | 91 | DISSECT_IMAGE_DEVICE_READ_ONLY = 1 << 0, /* Make device read-only */ |
2d3a5a73 LP |
92 | DISSECT_IMAGE_DISCARD_ON_LOOP = 1 << 1, /* Turn on "discard" if on a loop device and file system supports it */ |
93 | DISSECT_IMAGE_DISCARD = 1 << 2, /* Turn on "discard" if file system supports it, on all block devices */ | |
94 | DISSECT_IMAGE_DISCARD_ON_CRYPTO = 1 << 3, /* Turn on "discard" also on crypto devices */ | |
ef9c184d LP |
95 | DISSECT_IMAGE_DISCARD_ANY = DISSECT_IMAGE_DISCARD_ON_LOOP | |
96 | DISSECT_IMAGE_DISCARD | | |
97 | DISSECT_IMAGE_DISCARD_ON_CRYPTO, | |
2d3a5a73 | 98 | DISSECT_IMAGE_GPT_ONLY = 1 << 4, /* Only recognize images with GPT partition tables */ |
4b5de5dd | 99 | DISSECT_IMAGE_GENERIC_ROOT = 1 << 5, /* If no partition table or only single generic partition, assume it's the root fs */ |
aee36b4e LP |
100 | DISSECT_IMAGE_MOUNT_ROOT_ONLY = 1 << 6, /* Mount only the root and /usr partitions */ |
101 | DISSECT_IMAGE_MOUNT_NON_ROOT_ONLY = 1 << 7, /* Mount only the non-root and non-/usr partitions */ | |
5238e957 | 102 | DISSECT_IMAGE_VALIDATE_OS = 1 << 8, /* Refuse mounting images that aren't identifiable as OS images */ |
9ccb531a LB |
103 | DISSECT_IMAGE_VALIDATE_OS_EXT = 1 << 9, /* Refuse mounting images that aren't identifiable as OS extension images */ |
104 | DISSECT_IMAGE_NO_UDEV = 1 << 10, /* Don't wait for udev initializing things */ | |
105 | DISSECT_IMAGE_RELAX_VAR_CHECK = 1 << 11, /* Don't insist that the UUID of /var is hashed from /etc/machine-id */ | |
106 | DISSECT_IMAGE_FSCK = 1 << 12, /* File system check the partition before mounting (no effect when combined with DISSECT_IMAGE_READ_ONLY) */ | |
107 | DISSECT_IMAGE_NO_PARTITION_TABLE = 1 << 13, /* Only recognize single file system images */ | |
108 | DISSECT_IMAGE_VERITY_SHARE = 1 << 14, /* When activating a verity device, reuse existing one if already open */ | |
109 | DISSECT_IMAGE_MKDIR = 1 << 15, /* Make top-level directory to mount right before mounting, if missing */ | |
110 | DISSECT_IMAGE_USR_NO_ROOT = 1 << 16, /* If no root fs is in the image, but /usr is, then allow this (so that we can mount the rootfs as tmpfs or so */ | |
111 | DISSECT_IMAGE_REQUIRE_ROOT = 1 << 17, /* Don't accept disks without root partition (or at least /usr partition if DISSECT_IMAGE_USR_NO_ROOT is set) */ | |
112 | DISSECT_IMAGE_MOUNT_READ_ONLY = 1 << 18, /* Make mounts read-only */ | |
ef9c184d LP |
113 | DISSECT_IMAGE_READ_ONLY = DISSECT_IMAGE_DEVICE_READ_ONLY | |
114 | DISSECT_IMAGE_MOUNT_READ_ONLY, | |
9ccb531a LB |
115 | DISSECT_IMAGE_GROWFS = 1 << 19, /* Grow file systems in partitions marked for that to the size of the partitions after mount */ |
116 | DISSECT_IMAGE_MOUNT_IDMAPPED = 1 << 20, /* Mount mounts with kernel 5.12-style userns ID mapping, if file system type doesn't support uid=/gid= */ | |
18b5886e | 117 | } DissectImageFlags; |
8c1be37e LP |
118 | |
119 | struct DissectedImage { | |
4623e8e6 | 120 | bool encrypted:1; |
c3c88d67 LP |
121 | bool has_verity:1; /* verity available in image, but not necessarily used */ |
122 | bool verity_ready:1; /* verity available, fully specified and usable */ | |
e7cbe5cb | 123 | bool single_file_system:1; /* MBR/GPT or single file system */ |
3b925504 | 124 | |
8c1be37e | 125 | DissectedPartition partitions[_PARTITION_DESIGNATOR_MAX]; |
3b925504 | 126 | |
593fe6c0 | 127 | char *image_name; |
3b925504 LP |
128 | char *hostname; |
129 | sd_id128_t machine_id; | |
130 | char **machine_info; | |
131 | char **os_release; | |
7718ac97 | 132 | char **extension_release; |
8c1be37e LP |
133 | }; |
134 | ||
18d73705 | 135 | struct MountOptions { |
569a0e42 | 136 | PartitionDesignator partition_designator; |
18d73705 LB |
137 | char *options; |
138 | LIST_FIELDS(MountOptions, mount_options); | |
139 | }; | |
140 | ||
89e62e0b LP |
141 | struct VeritySettings { |
142 | /* Binary root hash for the Verity Merkle tree */ | |
143 | void *root_hash; | |
144 | size_t root_hash_size; | |
145 | ||
146 | /* PKCS#7 signature of the above */ | |
147 | void *root_hash_sig; | |
148 | size_t root_hash_sig_size; | |
149 | ||
150 | /* Path to the verity data file, if stored externally */ | |
151 | char *data_path; | |
aee36b4e LP |
152 | |
153 | /* PARTITION_ROOT or PARTITION_USR, depending on what these Verity settings are for */ | |
154 | PartitionDesignator designator; | |
89e62e0b LP |
155 | }; |
156 | ||
aee36b4e LP |
157 | #define VERITY_SETTINGS_DEFAULT { \ |
158 | .designator = _PARTITION_DESIGNATOR_INVALID \ | |
159 | } | |
160 | ||
18d73705 LB |
161 | MountOptions* mount_options_free_all(MountOptions *options); |
162 | DEFINE_TRIVIAL_CLEANUP_FUNC(MountOptions*, mount_options_free_all); | |
569a0e42 | 163 | const char* mount_options_from_designator(const MountOptions *options, PartitionDesignator designator); |
18d73705 | 164 | |
c34b75a1 | 165 | int probe_filesystem(const char *node, char **ret_fstype); |
a3642997 LB |
166 | int dissect_image(int fd, const VeritySettings *verity, const MountOptions *mount_options, uint64_t diskseq, uint64_t uevent_seqnum_not_before, usec_t timestamp_not_before, DissectImageFlags flags, DissectedImage **ret); |
167 | int dissect_image_and_warn(int fd, const char *name, const VeritySettings *verity, const MountOptions *mount_options, uint64_t diskseq, uint64_t uevent_seqnum_not_before, usec_t timestamp_not_before, DissectImageFlags flags, DissectedImage **ret); | |
8c1be37e LP |
168 | |
169 | DissectedImage* dissected_image_unref(DissectedImage *m); | |
170 | DEFINE_TRIVIAL_CLEANUP_FUNC(DissectedImage*, dissected_image_unref); | |
171 | ||
89e62e0b LP |
172 | int dissected_image_decrypt(DissectedImage *m, const char *passphrase, const VeritySettings *verity, DissectImageFlags flags, DecryptedImage **ret); |
173 | int dissected_image_decrypt_interactively(DissectedImage *m, const char *passphrase, const VeritySettings *verity, DissectImageFlags flags, DecryptedImage **ret); | |
21b61b1d LP |
174 | int dissected_image_mount(DissectedImage *m, const char *dest, uid_t uid_shift, uid_t uid_range, DissectImageFlags flags); |
175 | int dissected_image_mount_and_warn(DissectedImage *m, const char *where, uid_t uid_shift, uid_t uid_range, DissectImageFlags flags); | |
18b5886e | 176 | |
3b925504 LP |
177 | int dissected_image_acquire_metadata(DissectedImage *m); |
178 | ||
18b5886e LP |
179 | DecryptedImage* decrypted_image_unref(DecryptedImage *p); |
180 | DEFINE_TRIVIAL_CLEANUP_FUNC(DecryptedImage*, decrypted_image_unref); | |
181 | int decrypted_image_relinquish(DecryptedImage *d); | |
8c1be37e | 182 | |
569a0e42 LP |
183 | const char* partition_designator_to_string(PartitionDesignator d) _const_; |
184 | PartitionDesignator partition_designator_from_string(const char *name) _pure_; | |
78ebe980 | 185 | |
89e62e0b LP |
186 | int verity_settings_load(VeritySettings *verity, const char *image, const char *root_hash_path, const char *root_hash_sig_path); |
187 | void verity_settings_done(VeritySettings *verity); | |
188 | ||
569a0e42 LP |
189 | bool dissected_image_can_do_verity(const DissectedImage *image, PartitionDesignator d); |
190 | bool dissected_image_has_verity(const DissectedImage *image, PartitionDesignator d); | |
6aa05ebd LP |
191 | |
192 | int mount_image_privately_interactively(const char *path, DissectImageFlags flags, char **ret_directory, LoopDevice **ret_loop_device, DecryptedImage **ret_decrypted_image); | |
4beda316 | 193 | |
93f59701 | 194 | int verity_dissect_and_mount(const char *src, const char *dest, const MountOptions *options, const char *required_host_os_release_id, const char *required_host_os_release_version_id, const char *required_host_os_release_sysext_level); |