]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/dissect-image.h
docs/RANDOM_SEEDS: update NetBSD link
[thirdparty/systemd.git] / src / shared / dissect-image.h
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 #include <stdbool.h>
5
6 #include "sd-id128.h"
7
8 #include "architecture.h"
9 #include "env-util.h"
10 #include "gpt.h"
11 #include "list.h"
12 #include "loop-util.h"
13 #include "macro.h"
14 #include "os-util.h"
15 #include "strv.h"
16
17 typedef struct DissectedImage DissectedImage;
18 typedef struct DissectedPartition DissectedPartition;
19 typedef struct DecryptedImage DecryptedImage;
20 typedef struct MountOptions MountOptions;
21 typedef struct VeritySettings VeritySettings;
22
23 struct DissectedPartition {
24 bool found:1;
25 bool ignored:1;
26 bool rw:1;
27 bool growfs:1;
28 int partno; /* -1 if there was no partition and the images contains a file system directly */
29 Architecture architecture; /* Intended architecture: either native, secondary or unset ARCHITECTURE_INVALID. */
30 sd_id128_t uuid; /* Partition entry UUID as reported by the GPT */
31 char *fstype;
32 char *node;
33 char *label;
34 char *decrypted_node;
35 char *decrypted_fstype;
36 char *mount_options;
37 int mount_node_fd;
38 uint64_t size;
39 uint64_t offset;
40 uint64_t gpt_flags;
41 int fsmount_fd;
42 };
43
44 #define DISSECTED_PARTITION_NULL \
45 ((DissectedPartition) { \
46 .partno = -1, \
47 .architecture = _ARCHITECTURE_INVALID, \
48 .mount_node_fd = -EBADF, \
49 .fsmount_fd = -EBADF, \
50 })
51 #define TAKE_PARTITION(p) \
52 ({ \
53 DissectedPartition *_pp = &(p), _p = *_pp; \
54 *_pp = DISSECTED_PARTITION_NULL; \
55 _p; \
56 })
57
58 typedef enum DissectImageFlags {
59 DISSECT_IMAGE_DEVICE_READ_ONLY = 1 << 0, /* Make device read-only */
60 DISSECT_IMAGE_DISCARD_ON_LOOP = 1 << 1, /* Turn on "discard" if on a loop device and file system supports it */
61 DISSECT_IMAGE_DISCARD = 1 << 2, /* Turn on "discard" if file system supports it, on all block devices */
62 DISSECT_IMAGE_DISCARD_ON_CRYPTO = 1 << 3, /* Turn on "discard" also on crypto devices */
63 DISSECT_IMAGE_DISCARD_ANY = DISSECT_IMAGE_DISCARD_ON_LOOP |
64 DISSECT_IMAGE_DISCARD |
65 DISSECT_IMAGE_DISCARD_ON_CRYPTO,
66 DISSECT_IMAGE_GPT_ONLY = 1 << 4, /* Only recognize images with GPT partition tables */
67 DISSECT_IMAGE_GENERIC_ROOT = 1 << 5, /* If no partition table or only single generic partition, assume it's the root fs */
68 DISSECT_IMAGE_MOUNT_ROOT_ONLY = 1 << 6, /* Mount only the root and /usr partitions */
69 DISSECT_IMAGE_MOUNT_NON_ROOT_ONLY = 1 << 7, /* Mount only the non-root and non-/usr partitions */
70 DISSECT_IMAGE_VALIDATE_OS = 1 << 8, /* Refuse mounting images that aren't identifiable as OS images */
71 DISSECT_IMAGE_VALIDATE_OS_EXT = 1 << 9, /* Refuse mounting images that aren't identifiable as OS extension images */
72 DISSECT_IMAGE_RELAX_VAR_CHECK = 1 << 10, /* Don't insist that the UUID of /var is hashed from /etc/machine-id */
73 DISSECT_IMAGE_FSCK = 1 << 11, /* File system check the partition before mounting (no effect when combined with DISSECT_IMAGE_READ_ONLY) */
74 DISSECT_IMAGE_NO_PARTITION_TABLE = 1 << 12, /* Only recognize single file system images */
75 DISSECT_IMAGE_VERITY_SHARE = 1 << 13, /* When activating a verity device, reuse existing one if already open */
76 DISSECT_IMAGE_MKDIR = 1 << 14, /* Make top-level directory to mount right before mounting, if missing */
77 DISSECT_IMAGE_USR_NO_ROOT = 1 << 15, /* 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 */
78 DISSECT_IMAGE_REQUIRE_ROOT = 1 << 16, /* Don't accept disks without root partition (or at least /usr partition if DISSECT_IMAGE_USR_NO_ROOT is set) */
79 DISSECT_IMAGE_MOUNT_READ_ONLY = 1 << 17, /* Make mounts read-only */
80 DISSECT_IMAGE_READ_ONLY = DISSECT_IMAGE_DEVICE_READ_ONLY |
81 DISSECT_IMAGE_MOUNT_READ_ONLY,
82 DISSECT_IMAGE_GROWFS = 1 << 18, /* Grow file systems in partitions marked for that to the size of the partitions after mount */
83 DISSECT_IMAGE_MOUNT_IDMAPPED = 1 << 19, /* Mount mounts with kernel 5.12-style userns ID mapping, if file system type doesn't support uid=/gid= */
84 DISSECT_IMAGE_ADD_PARTITION_DEVICES = 1 << 20, /* Create partition devices via BLKPG_ADD_PARTITION */
85 DISSECT_IMAGE_PIN_PARTITION_DEVICES = 1 << 21, /* Open dissected partitions and decrypted partitions and pin them by fd */
86 DISSECT_IMAGE_RELAX_EXTENSION_CHECK = 1 << 22, /* Don't insist that the extension-release file name matches the image name */
87 DISSECT_IMAGE_DISKSEQ_DEVNODE = 1 << 23, /* Prefer /dev/disk/by-diskseq/… device nodes */
88 DISSECT_IMAGE_ALLOW_EMPTY = 1 << 24, /* Allow that no usable partitions is present */
89 DISSECT_IMAGE_TRY_ATOMIC_MOUNT_EXCHANGE = 1 << 25, /* Try to mount the image beneath the specified mountpoint, rather than on top of it, and then umount the top */
90 DISSECT_IMAGE_ALLOW_USERSPACE_VERITY = 1 << 26, /* Allow userspace verity keyring in /etc/verity.d/ and related dirs */
91 } DissectImageFlags;
92
93 struct DissectedImage {
94 bool encrypted:1;
95 bool has_verity:1; /* verity available in image, but not necessarily used */
96 bool has_verity_sig:1; /* pkcs#7 signature embedded in image */
97 bool verity_ready:1; /* verity available, fully specified and usable */
98 bool verity_sig_ready:1; /* verity signature logic, fully specified and usable */
99 bool single_file_system:1; /* MBR/GPT or single file system */
100
101 LoopDevice *loop;
102 DissectedPartition partitions[_PARTITION_DESIGNATOR_MAX];
103 DecryptedImage *decrypted_image;
104
105 uint32_t sector_size;
106 uint64_t image_size;
107
108 char *image_name;
109 sd_id128_t image_uuid;
110
111 /* Meta information extracted from /etc/os-release and similar */
112 char *hostname;
113 sd_id128_t machine_id;
114 char **machine_info;
115 char **os_release;
116 char **initrd_release;
117 char **confext_release;
118 char **sysext_release;
119 int has_init_system;
120 };
121
122 struct MountOptions {
123 PartitionDesignator partition_designator;
124 char *options;
125 LIST_FIELDS(MountOptions, mount_options);
126 };
127
128 struct VeritySettings {
129 /* Binary root hash for the Verity Merkle tree */
130 void *root_hash;
131 size_t root_hash_size;
132
133 /* PKCS#7 signature of the above */
134 void *root_hash_sig;
135 size_t root_hash_sig_size;
136
137 /* Path to the verity data file, if stored externally */
138 char *data_path;
139
140 /* PARTITION_ROOT or PARTITION_USR, depending on what these Verity settings are for */
141 PartitionDesignator designator;
142 };
143
144 #define VERITY_SETTINGS_DEFAULT { \
145 .designator = _PARTITION_DESIGNATOR_INVALID \
146 }
147
148 /* We include image-policy.h down here, since ImagePolicy wants a complete definition of PartitionDesignator first. */
149 #include "image-policy.h"
150
151 MountOptions* mount_options_free_all(MountOptions *options);
152 DEFINE_TRIVIAL_CLEANUP_FUNC(MountOptions*, mount_options_free_all);
153 const char* mount_options_from_designator(const MountOptions *options, PartitionDesignator designator);
154
155 int probe_filesystem_full(int fd, const char *path, uint64_t offset, uint64_t size, char **ret_fstype);
156 static inline int probe_filesystem(const char *path, char **ret_fstype) {
157 return probe_filesystem_full(-1, path, 0, UINT64_MAX, ret_fstype);
158 }
159
160 int dissect_log_error(int log_level, int r, const char *name, const VeritySettings *verity);
161 int dissect_image_file(const char *path, const VeritySettings *verity, const MountOptions *mount_options, const ImagePolicy *image_policy, DissectImageFlags flags, DissectedImage **ret);
162 int dissect_image_file_and_warn(const char *path, const VeritySettings *verity, const MountOptions *mount_options, const ImagePolicy *image_policy, DissectImageFlags flags, DissectedImage **ret);
163 int dissect_loop_device(LoopDevice *loop, const VeritySettings *verity, const MountOptions *mount_options, const ImagePolicy *image_policy, DissectImageFlags flags, DissectedImage **ret);
164 int dissect_loop_device_and_warn(LoopDevice *loop, const VeritySettings *verity, const MountOptions *mount_options, const ImagePolicy *image_policy, DissectImageFlags flags, DissectedImage **ret);
165
166 DissectedImage* dissected_image_unref(DissectedImage *m);
167 DEFINE_TRIVIAL_CLEANUP_FUNC(DissectedImage*, dissected_image_unref);
168
169 int dissected_image_decrypt(DissectedImage *m, const char *passphrase, const VeritySettings *verity, DissectImageFlags flags);
170 int dissected_image_decrypt_interactively(DissectedImage *m, const char *passphrase, const VeritySettings *verity, DissectImageFlags flags);
171 int dissected_image_mount(DissectedImage *m, const char *dest, uid_t uid_shift, uid_t uid_range, int userns_fd, DissectImageFlags flags);
172 int dissected_image_mount_and_warn(DissectedImage *m, const char *where, uid_t uid_shift, uid_t uid_range, int userns_fd, DissectImageFlags flags);
173
174 int dissected_image_acquire_metadata(DissectedImage *m, DissectImageFlags extra_flags);
175
176 Architecture dissected_image_architecture(DissectedImage *m);
177
178 static inline bool dissected_image_is_bootable_os(DissectedImage *m) {
179 return m && m->has_init_system > 0;
180 }
181
182 static inline bool dissected_image_is_bootable_uefi(DissectedImage *m) {
183 return m && m->partitions[PARTITION_ESP].found && dissected_image_is_bootable_os(m);
184 }
185
186 static inline bool dissected_image_is_portable(DissectedImage *m) {
187 return m && strv_env_pairs_get(m->os_release, "PORTABLE_PREFIXES");
188 }
189
190 static inline bool dissected_image_is_initrd(DissectedImage *m) {
191 return m && !strv_isempty(m->initrd_release);
192 }
193
194 DecryptedImage* decrypted_image_ref(DecryptedImage *p);
195 DecryptedImage* decrypted_image_unref(DecryptedImage *p);
196 DEFINE_TRIVIAL_CLEANUP_FUNC(DecryptedImage*, decrypted_image_unref);
197
198 int dissected_image_relinquish(DissectedImage *m);
199
200 int verity_settings_load(VeritySettings *verity, const char *image, const char *root_hash_path, const char *root_hash_sig_path);
201 void verity_settings_done(VeritySettings *verity);
202
203 static inline bool verity_settings_data_covers(const VeritySettings *verity, PartitionDesignator d) {
204 /* Returns true if the verity settings contain sufficient information to cover the specified partition */
205 return verity &&
206 ((d >= 0 && verity->designator == d) || (d == PARTITION_ROOT && verity->designator < 0)) &&
207 verity->root_hash &&
208 verity->data_path;
209 }
210
211 int dissected_image_load_verity_sig_partition(DissectedImage *m, int fd, VeritySettings *verity);
212
213 bool dissected_image_verity_candidate(const DissectedImage *image, PartitionDesignator d);
214 bool dissected_image_verity_ready(const DissectedImage *image, PartitionDesignator d);
215 bool dissected_image_verity_sig_ready(const DissectedImage *image, PartitionDesignator d);
216
217 int mount_image_privately_interactively(const char *path, const ImagePolicy *image_policy, DissectImageFlags flags, char **ret_directory, int *ret_dir_fd, LoopDevice **ret_loop_device);
218
219 int verity_dissect_and_mount(int src_fd, const char *src, const char *dest, const MountOptions *options, const ImagePolicy *image_policy, const char *required_host_os_release_id, const char *required_host_os_release_version_id, const char *required_host_os_release_sysext_level, const char *required_host_os_release_confext_level, const char *required_sysext_scope, DissectedImage **ret_image);
220
221 int dissect_fstype_ok(const char *fstype);
222
223 int probe_sector_size(int fd, uint32_t *ret);
224 int probe_sector_size_prefer_ioctl(int fd, uint32_t *ret);
225
226 int partition_pick_mount_options(PartitionDesignator d, const char *fstype, bool rw, bool discard, char **ret_options, unsigned long *ret_ms_flags);
227
228 static inline const char *dissected_partition_fstype(const DissectedPartition *m) {
229 assert(m);
230
231 return m->decrypted_node ? m->decrypted_fstype : m->fstype;
232 }
233
234 int get_common_dissect_directory(char **ret);