Debian Codegraph @

src/veritysetup/veritysetup-generator.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <stdlib.h>
4
5 #include "sd-id128.h"
6
7 #include "alloc-util.h"
8 #include "fd-util.h"
9 #include "fileio.h"
10 #include "fstab-util.h"
11 #include "generator.h"
12 #include "hexdecoct.h"
13 #include "log.h"
14 #include "parse-util.h"
15 #include "path-util.h"
16 #include "proc-cmdline.h"
17 #include "specifier.h"
18 #include "string-util.h"
19 #include "strv.h"
20 #include "unit-name.h"
21
22 static const char *arg_dest = NULL;
23 static bool arg_enabled = true;
24 static bool arg_read_veritytab = true;
25 static const char *arg_veritytab = NULL;
26 static char *arg_root_hash = NULL;
27 static char *arg_root_data_what = NULL;
28 static char *arg_root_hash_what = NULL;
29 static char *arg_root_options = NULL;
30 static char *arg_usr_hash = NULL;
31 static char *arg_usr_data_what = NULL;
32 static char *arg_usr_hash_what = NULL;
33 static char *arg_usr_options = NULL;
34
35 STATIC_DESTRUCTOR_REGISTER(arg_root_hash, freep);
36 STATIC_DESTRUCTOR_REGISTER(arg_root_data_what, freep);
37 STATIC_DESTRUCTOR_REGISTER(arg_root_hash_what, freep);
38 STATIC_DESTRUCTOR_REGISTER(arg_root_options, freep);
39 STATIC_DESTRUCTOR_REGISTER(arg_usr_hash, freep);
40 STATIC_DESTRUCTOR_REGISTER(arg_usr_data_what, freep);
41 STATIC_DESTRUCTOR_REGISTER(arg_usr_hash_what, freep);
42 STATIC_DESTRUCTOR_REGISTER(arg_usr_options, freep);
43
44 static int create_special_device(
45 const char *name,
46 const char *roothash,
47 const char *data_what,
48 const char *hash_what,
49 const char *options) {
50
51 _cleanup_free_ char *u = NULL, *v = NULL, *d = NULL, *e = NULL;
52 _cleanup_fclose_ FILE *f = NULL;
53 int r;
54
55 /* Creates a [email protected] instance for the special kernel cmdline specified root + usr devices. */
56
57 assert(name);
58
59 /* If all three pieces of information are missing, then verity is turned off */
60 if (!roothash && !data_what && !hash_what)
61 return 0;
62
63 /* if one of them is missing however, the data is simply incomplete and this is an error */
64 if (!roothash)
65 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Verity information for %s incomplete, root hash unspecified.", name);
66 if (!data_what)
67 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Verity information for %s incomplete, data device unspecified.", name);
68 if (!hash_what)
69 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Verity information for %s incomplete, hash device unspecified.", name);
70
71 _cleanup_free_ char *service = NULL;
72 r = unit_name_build("systemd-veritysetup", name, ".service", &service);
73 if (r < 0)
74 return log_error_errno(r, "Failed to generate unit name: %m");
75
76 log_debug("Using %s verity data device %s, hash device %s, options %s, and hash %s.", name, data_what, hash_what, options, roothash);
77
78 u = fstab_node_to_udev_node(data_what);
79 if (!u)
80 return log_oom();
81 v = fstab_node_to_udev_node(hash_what);
82 if (!v)
83 return log_oom();
84
85 r = unit_name_from_path(u, ".device", &d);
86 if (r < 0)
87 return log_error_errno(r, "Failed to generate unit name: %m");
88 r = unit_name_from_path(v, ".device", &e);
89 if (r < 0)
90 return log_error_errno(r, "Failed to generate unit name: %m");
91
92 r = generator_open_unit_file(arg_dest, "/proc/cmdline", service, &f);
93 if (r < 0)
94 return r;
95
96 r = generator_write_veritysetup_unit_section(f, "/proc/cmdline");
97 if (r < 0)
98 return r;
99
100 fprintf(f,
101 "Before=veritysetup.target\n"
102 "BindsTo=%1$s %2$s\n"
103 "After=%1$s %2$s\n",
104 d, e);
105
106 r = generator_write_veritysetup_service_section(f, name, u, v, roothash, options);
107 if (r < 0)
108 return r;
109
110 r = fflush_and_check(f);
111 if (r < 0)
112 return log_error_errno(r, "Failed to write file unit %s: %m", service);
113
114 r = generator_add_symlink(arg_dest, "veritysetup.target", "requires", service);
115 if (r < 0)
116 return r;
117
118 return 0;
119 }
120
121 static int create_root_device(void) {
122 return create_special_device("root", arg_root_hash, arg_root_data_what, arg_root_hash_what, arg_root_options);
123 }
124
125 static int create_usr_device(void) {
126 return create_special_device("usr", arg_usr_hash, arg_usr_data_what, arg_usr_hash_what, arg_usr_options);
127 }
128
129 static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
130 int r;
131
132 assert(key);
133
134 if (streq(key, "systemd.verity")) {
135
136 r = value ? parse_boolean(value) : 1;
137 if (r < 0)
138 log_warning("Failed to parse verity= kernel command line switch %s. Ignoring.", value);
139 else
140 arg_enabled = r;
141
142 } else if (streq(key, "veritytab")) {
143
144 r = value ? parse_boolean(value) : 1;
145 if (r < 0)
146 log_warning("Failed to parse veritytab= kernel command line switch %s. Ignoring.", value);
147 else
148 arg_read_veritytab = r;
149
150 } else if (streq(key, "roothash")) {
151
152 if (proc_cmdline_value_missing(key, value))
153 return 0;
154
155 r = free_and_strdup(&arg_root_hash, value);
156 if (r < 0)
157 return log_oom();
158
159 } else if (proc_cmdline_key_streq(key, "systemd.verity_root_data")) {
160
161 if (proc_cmdline_value_missing(key, value))
162 return 0;
163
164 r = free_and_strdup(&arg_root_data_what, value);
165 if (r < 0)
166 return log_oom();
167
168 } else if (proc_cmdline_key_streq(key, "systemd.verity_root_hash")) {
169
170 if (proc_cmdline_value_missing(key, value))
171 return 0;
172
173 r = free_and_strdup(&arg_root_hash_what, value);
174 if (r < 0)
175 return log_oom();
176
177 } else if (proc_cmdline_key_streq(key, "systemd.verity_root_options")) {
178
179 if (proc_cmdline_value_missing(key, value))
180 return 0;
181
182 r = free_and_strdup(&arg_root_options, value);
183 if (r < 0)
184 return log_oom();
185
186 } else if (streq(key, "usrhash")) {
187
188 if (proc_cmdline_value_missing(key, value))
189 return 0;
190
191 r = free_and_strdup(&arg_usr_hash, value);
192 if (r < 0)
193 return log_oom();
194
195 } else if (proc_cmdline_key_streq(key, "systemd.verity_usr_data")) {
196
197 if (proc_cmdline_value_missing(key, value))
198 return 0;
199
200 r = free_and_strdup(&arg_usr_data_what, value);
201 if (r < 0)
202 return log_oom();
203
204 } else if (proc_cmdline_key_streq(key, "systemd.verity_usr_hash")) {
205
206 if (proc_cmdline_value_missing(key, value))
207 return 0;
208
209 r = free_and_strdup(&arg_usr_hash_what, value);
210 if (r < 0)
211 return log_oom();
212
213 } else if (proc_cmdline_key_streq(key, "systemd.verity_usr_options")) {
214
215 if (proc_cmdline_value_missing(key, value))
216 return 0;
217
218 r = free_and_strdup(&arg_usr_options, value);
219 if (r < 0)
220 return log_oom();
221 }
222
223 return 0;
224 }
225
226 static int determine_device(
227 const char *name,
228 const char *hash,
229 char **data_what,
230 char **hash_what) {
231
232 sd_id128_t data_uuid, verity_uuid;
233 _cleanup_free_ void *m = NULL;
234 size_t l;
235 int r;
236
237 assert(name);
238 assert(data_what);
239 assert(hash_what);
240
241 if (!hash)
242 return 0;
243
244 if (*data_what && *hash_what)
245 return 0;
246
247 r = unhexmem(hash, &m, &l);
248 if (r < 0)
249 return log_error_errno(r, "Failed to parse hash: %s", hash);
250 if (l < sizeof(sd_id128_t)) {
251 log_debug("Root hash for %s is shorter than 128 bits (32 characters), ignoring for discovering verity partition.", name);
252 return 0;
253 }
254
255 if (!*data_what) {
256 memcpy(&data_uuid, m, sizeof(data_uuid));
257
258 *data_what = path_join("/dev/disk/by-partuuid", SD_ID128_TO_UUID_STRING(data_uuid));
259 if (!*data_what)
260 return log_oom();
261 }
262
263 if (!*hash_what) {
264 memcpy(&verity_uuid, (uint8_t*) m + l - sizeof(verity_uuid), sizeof(verity_uuid));
265
266 *hash_what = path_join("/dev/disk/by-partuuid", SD_ID128_TO_UUID_STRING(verity_uuid));
267 if (!*hash_what)
268 return log_oom();
269 }
270
271 log_info("Using data device %s and hash device %s for %s.", *data_what, *hash_what, name);
272
273 return 1;
274 }
275
276 static int determine_devices(void) {
277 int r;
278
279 r = determine_device("root", arg_root_hash, &arg_root_data_what, &arg_root_hash_what);
280 if (r < 0)
281 return r;
282
283 return determine_device("usr", arg_usr_hash, &arg_usr_data_what, &arg_usr_hash_what);
284 }
285
286 static bool attach_in_initrd(const char *name, const char *options) {
287 assert(name);
288
289 /* Imply x-initrd.attach in case the volume name is among those defined in the Discoverable Partition
290 * Specification for partitions that we require to be mounted during the initrd → host transition,
291 * i.e. for the root fs itself, and /usr/. This mirrors similar behaviour in
292 * systemd-fstab-generator. */
293
294 return fstab_test_option(options, "x-initrd.attach\0") ||
295 STR_IN_SET(name, "root", "usr");
296 }
297
298 static int create_veritytab_device(
299 const char *name,
300 const char *data_device,
301 const char *hash_device,
302 const char *roothash,
303 const char *options,
304 const char *source) {
305
306 _cleanup_free_ char *n = NULL, *dd = NULL, *du = NULL, *hd = NULL, *hu = NULL, *e = NULL,
307 *du_escaped = NULL, *hu_escaped = NULL, *name_escaped = NULL;
308 _cleanup_fclose_ FILE *f = NULL;
309 const char *dmname;
310 bool noauto, nofail, netdev, need_loop = false;
311 int r;
312
313 /* Creates a [email protected] instance for volumes specified in /etc/veritytab. */
314
315 assert(name);
316 assert(data_device);
317 assert(hash_device);
318 assert(roothash);
319
320 noauto = fstab_test_yes_no_option(options, "noauto\0" "auto\0");
321 nofail = fstab_test_yes_no_option(options, "nofail\0" "fail\0");
322 netdev = fstab_test_option(options, "_netdev\0");
323
324 name_escaped = specifier_escape(name);
325 if (!name_escaped)
326 return log_oom();
327
328 e = unit_name_escape(name);
329 if (!e)
330 return log_oom();
331
332 du = fstab_node_to_udev_node(data_device);
333 if (!du)
334 return log_oom();
335
336 hu = fstab_node_to_udev_node(hash_device);
337 if (!hu)
338 return log_oom();
339
340 r = unit_name_build("systemd-veritysetup", e, ".service", &n);
341 if (r < 0)
342 return log_error_errno(r, "Failed to generate unit name: %m");
343
344 du_escaped = specifier_escape(du);
345 if (!du_escaped)
346 return log_oom();
347
348 hu_escaped = specifier_escape(hu);
349 if (!hu_escaped)
350 return log_oom();
351
352 r = unit_name_from_path(du, ".device", &dd);
353 if (r < 0)
354 return log_error_errno(r, "Failed to generate unit name: %m");
355
356 r = unit_name_from_path(hu, ".device", &hd);
357 if (r < 0)
358 return log_error_errno(r, "Failed to generate unit name: %m");
359
360 r = generator_open_unit_file(arg_dest, NULL, n, &f);
361 if (r < 0)
362 return r;
363
364 r = generator_write_veritysetup_unit_section(f, source);
365 if (r < 0)
366 return r;
367
368 if (netdev)
369 fprintf(f, "After=remote-fs-pre.target\n");
370
371 /* If initrd takes care of attaching the disk then it should also detach it during shutdown. */
372 if (!attach_in_initrd(name, options))
373 fprintf(f,
374 "Conflicts=umount.target\n"
375 "Before=umount.target\n");
376
377 if (!nofail)
378 fprintf(f,
379 "Before=%s\n",
380 netdev ? "remote-veritysetup.target" : "veritysetup.target");
381
382 if (path_startswith(du, "/dev/"))
383 fprintf(f,
384 "BindsTo=%s\n"
385 "After=%s\n",
386 dd, dd);
387 else {
388 fprintf(f, "RequiresMountsFor=%s\n", du_escaped);
389 need_loop = true;
390 }
391
392 if (path_startswith(hu, "/dev/"))
393 fprintf(f,
394 "BindsTo=%s\n"
395 "After=%s\n",
396 hd, hd);
397 else {
398 fprintf(f, "RequiresMountsFor=%s\n", hu_escaped);
399 need_loop = true;
400 }
401
402 if (need_loop)
403 /* For loopback devices make sure to explicitly load loop.ko, as this code might run very
404 * early where device nodes created via systemd-tmpfiles-setup-dev.service might not be
405 * around yet. Hence let's sync on the module itself. */
406 fprintf(f,
407 "[email protected]\n"
408 "[email protected]\n");
409
410 r = generator_write_veritysetup_service_section(f, name, du, hu, roothash, options);
411 if (r < 0)
412 return r;
413
414 r = fflush_and_check(f);
415 if (r < 0)
416 return log_error_errno(r, "Failed to write unit file %s: %m", n);
417
418 if (!noauto) {
419 r = generator_add_symlink(arg_dest,
420 netdev ? "remote-veritysetup.target" : "veritysetup.target",
421 nofail ? "wants" : "requires", n);
422 if (r < 0)
423 return r;
424 }
425
426 dmname = strjoina("dev-mapper-", e, ".device");
427 return generator_add_symlink(arg_dest, dmname, "requires", n);
428 }
429
430 static int add_veritytab_devices(void) {
431 _cleanup_fclose_ FILE *f = NULL;
432 unsigned veritytab_line = 0;
433 int r;
434
435 if (!arg_read_veritytab)
436 return 0;
437
438 r = fopen_unlocked(arg_veritytab, "re", &f);
439 if (r < 0) {
440 if (errno != ENOENT)
441 log_error_errno(errno, "Failed to open %s: %m", arg_veritytab);
442 return 0;
443 }
444
445 for (;;) {
446 _cleanup_free_ char *line = NULL, *name = NULL, *data_device = NULL, *hash_device = NULL,
447 *roothash = NULL, *options = NULL;
448
449 r = read_stripped_line(f, LONG_LINE_MAX, &line);
450 if (r < 0)
451 return log_error_errno(r, "Failed to read %s: %m", arg_veritytab);
452 if (r == 0)
453 break;
454
455 veritytab_line++;
456
457 if (IN_SET(line[0], 0, '#'))
458 continue;
459
460 r = sscanf(line, "%ms %ms %ms %ms %ms", &name, &data_device, &hash_device, &roothash, &options);
461 if (!IN_SET(r, 4, 5)) {
462 log_error("Failed to parse %s:%u, ignoring.", arg_veritytab, veritytab_line);
463 continue;
464 }
465
466 r = create_veritytab_device(
467 name,
468 data_device,
469 hash_device,
470 roothash,
471 options,
472 arg_veritytab);
473 if (r < 0)
474 return r;
475 }
476
477 return 0;
478 }
479
480 static int run(const char *dest, const char *dest_early, const char *dest_late) {
481 int r;
482
483 assert_se(arg_dest = dest);
484
485 arg_veritytab = getenv("SYSTEMD_VERITYTAB") ?: "/etc/veritytab";
486
487 r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, PROC_CMDLINE_STRIP_RD_PREFIX);
488 if (r < 0)
489 return log_warning_errno(r, "Failed to parse kernel command line: %m");
490
491 if (!arg_enabled)
492 return 0;
493
494 r = add_veritytab_devices();
495 if (r < 0)
496 return r;
497
498 r = determine_devices();
499 if (r < 0)
500 return r;
501
502 r = create_root_device();
503 if (r < 0)
504 return r;
505
506 return create_usr_device();
507 }
508
509 DEFINE_MAIN_GENERATOR_FUNCTION(run);