• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemd / systemd / 15057632786

15 May 2025 09:01PM UTC coverage: 72.267% (+0.02%) from 72.244%
15057632786

push

github

bluca
man: document how to hook stuff into system wakeup

Fixes: #6364

298523 of 413084 relevant lines covered (72.27%)

738132.88 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/veritysetup/veritysetup-generator.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <stdlib.h>
4
#include <sys/stat.h>
5
#include <unistd.h>
6

7
#include "sd-id128.h"
8

9
#include "alloc-util.h"
10
#include "fd-util.h"
11
#include "fileio.h"
12
#include "fstab-util.h"
13
#include "generator.h"
14
#include "hexdecoct.h"
15
#include "log.h"
16
#include "parse-util.h"
17
#include "path-util.h"
18
#include "proc-cmdline.h"
19
#include "specifier.h"
20
#include "string-util.h"
21
#include "strv.h"
22
#include "unit-name.h"
23

24
static const char *arg_dest = NULL;
25
static bool arg_enabled = true;
26
static bool arg_read_veritytab = true;
27
static const char *arg_veritytab = NULL;
28
static char *arg_root_hash = NULL;
29
static char *arg_root_data_what = NULL;
30
static char *arg_root_hash_what = NULL;
31
static char *arg_root_options = NULL;
32
static char *arg_usr_hash = NULL;
33
static char *arg_usr_data_what = NULL;
34
static char *arg_usr_hash_what = NULL;
35
static char *arg_usr_options = NULL;
36

37
STATIC_DESTRUCTOR_REGISTER(arg_root_hash, freep);
×
38
STATIC_DESTRUCTOR_REGISTER(arg_root_data_what, freep);
×
39
STATIC_DESTRUCTOR_REGISTER(arg_root_hash_what, freep);
×
40
STATIC_DESTRUCTOR_REGISTER(arg_root_options, freep);
×
41
STATIC_DESTRUCTOR_REGISTER(arg_usr_hash, freep);
×
42
STATIC_DESTRUCTOR_REGISTER(arg_usr_data_what, freep);
×
43
STATIC_DESTRUCTOR_REGISTER(arg_usr_hash_what, freep);
×
44
STATIC_DESTRUCTOR_REGISTER(arg_usr_options, freep);
×
45

46
static int create_special_device(
×
47
                const char *name,
48
                const char *roothash,
49
                const char *data_what,
50
                const char *hash_what,
51
                const char *options) {
52

53
        _cleanup_free_ char *u = NULL, *v = NULL, *d = NULL, *e = NULL;
×
54
        _cleanup_fclose_ FILE *f = NULL;
×
55
        int r;
×
56

57
        /* Creates a systemd-veritysetup@.service instance for the special kernel cmdline specified root + usr devices. */
58

59
        assert(name);
×
60

61
        /* If all three pieces of information are missing, then verity is turned off */
62
        if (!roothash && !data_what && !hash_what)
×
63
                return 0;
64

65
        /* if one of them is missing however, the data is simply incomplete and this is an error */
66
        if (!roothash)
×
67
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Verity information for %s incomplete, root hash unspecified.", name);
×
68
        if (!data_what)
×
69
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Verity information for %s incomplete, data device unspecified.", name);
×
70
        if (!hash_what)
×
71
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Verity information for %s incomplete, hash device unspecified.", name);
×
72

73
        _cleanup_free_ char *service = NULL;
×
74
        r = unit_name_build("systemd-veritysetup", name, ".service", &service);
×
75
        if (r < 0)
×
76
                return log_error_errno(r, "Failed to generate unit name: %m");
×
77

78
        log_debug("Using %s verity data device %s, hash device %s, options %s, and hash %s.", name, data_what, hash_what, options, roothash);
×
79

80
        u = fstab_node_to_udev_node(data_what);
×
81
        if (!u)
×
82
                return log_oom();
×
83
        v = fstab_node_to_udev_node(hash_what);
×
84
        if (!v)
×
85
                return log_oom();
×
86

87
        r = unit_name_from_path(u, ".device", &d);
×
88
        if (r < 0)
×
89
                return log_error_errno(r, "Failed to generate unit name: %m");
×
90
        r = unit_name_from_path(v, ".device", &e);
×
91
        if (r < 0)
×
92
                return log_error_errno(r, "Failed to generate unit name: %m");
×
93

94
        r = generator_open_unit_file(arg_dest, "/proc/cmdline", service, &f);
×
95
        if (r < 0)
×
96
                return r;
97

98
        r = generator_write_veritysetup_unit_section(f, "/proc/cmdline");
×
99
        if (r < 0)
×
100
                return r;
101

102
        fprintf(f,
×
103
                "Before=veritysetup.target\n"
104
                "BindsTo=%1$s %2$s\n"
105
                "After=%1$s %2$s\n",
106
                d, e);
107

108
        r = generator_write_veritysetup_service_section(f, name, u, v, roothash, options);
×
109
        if (r < 0)
×
110
                return r;
111

112
        r = fflush_and_check(f);
×
113
        if (r < 0)
×
114
                return log_error_errno(r, "Failed to write file unit %s: %m", service);
×
115

116
        r = generator_add_symlink(arg_dest, "veritysetup.target", "requires", service);
×
117
        if (r < 0)
×
118
                return r;
×
119

120
        return 0;
121
}
122

123
static int create_root_device(void) {
×
124
        return create_special_device("root", arg_root_hash, arg_root_data_what, arg_root_hash_what, arg_root_options);
×
125
}
126

127
static int create_usr_device(void) {
×
128
        return create_special_device("usr", arg_usr_hash, arg_usr_data_what, arg_usr_hash_what, arg_usr_options);
×
129
}
130

131
static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
×
132
        int r;
×
133

134
        assert(key);
×
135

136
        if (streq(key, "systemd.verity")) {
×
137

138
                r = value ? parse_boolean(value) : 1;
×
139
                if (r < 0)
×
140
                        log_warning("Failed to parse verity= kernel command line switch %s. Ignoring.", value);
×
141
                else
142
                        arg_enabled = r;
×
143

144
        } else if (streq(key, "veritytab")) {
×
145

146
                r = value ? parse_boolean(value) : 1;
×
147
                if (r < 0)
×
148
                        log_warning("Failed to parse veritytab= kernel command line switch %s. Ignoring.", value);
×
149
                else
150
                        arg_read_veritytab = r;
×
151

152
        } else if (streq(key, "roothash")) {
×
153

154
                if (proc_cmdline_value_missing(key, value))
×
155
                        return 0;
156

157
                r = free_and_strdup(&arg_root_hash, value);
×
158
                if (r < 0)
×
159
                        return log_oom();
×
160

161
        } else if (proc_cmdline_key_streq(key, "systemd.verity_root_data")) {
×
162

163
                if (proc_cmdline_value_missing(key, value))
×
164
                        return 0;
165

166
                r = free_and_strdup(&arg_root_data_what, value);
×
167
                if (r < 0)
×
168
                        return log_oom();
×
169

170
        } else if (proc_cmdline_key_streq(key, "systemd.verity_root_hash")) {
×
171

172
                if (proc_cmdline_value_missing(key, value))
×
173
                        return 0;
174

175
                r = free_and_strdup(&arg_root_hash_what, value);
×
176
                if (r < 0)
×
177
                        return log_oom();
×
178

179
        } else if (proc_cmdline_key_streq(key, "systemd.verity_root_options")) {
×
180

181
                if (proc_cmdline_value_missing(key, value))
×
182
                        return 0;
183

184
                r = free_and_strdup(&arg_root_options, value);
×
185
                if (r < 0)
×
186
                        return log_oom();
×
187

188
        } else if (streq(key, "usrhash")) {
×
189

190
                if (proc_cmdline_value_missing(key, value))
×
191
                        return 0;
192

193
                r = free_and_strdup(&arg_usr_hash, value);
×
194
                if (r < 0)
×
195
                        return log_oom();
×
196

197
        } else if (proc_cmdline_key_streq(key, "systemd.verity_usr_data")) {
×
198

199
                if (proc_cmdline_value_missing(key, value))
×
200
                        return 0;
201

202
                r = free_and_strdup(&arg_usr_data_what, value);
×
203
                if (r < 0)
×
204
                        return log_oom();
×
205

206
        } else if (proc_cmdline_key_streq(key, "systemd.verity_usr_hash")) {
×
207

208
                if (proc_cmdline_value_missing(key, value))
×
209
                        return 0;
210

211
                r = free_and_strdup(&arg_usr_hash_what, value);
×
212
                if (r < 0)
×
213
                        return log_oom();
×
214

215
        } else if (proc_cmdline_key_streq(key, "systemd.verity_usr_options")) {
×
216

217
                if (proc_cmdline_value_missing(key, value))
×
218
                        return 0;
219

220
                r = free_and_strdup(&arg_usr_options, value);
×
221
                if (r < 0)
×
222
                        return log_oom();
×
223
        }
224

225
        return 0;
226
}
227

228
static int determine_device(
×
229
                const char *name,
230
                const char *hash,
231
                char **data_what,
232
                char **hash_what) {
233

234
        sd_id128_t data_uuid, verity_uuid;
×
235
        _cleanup_free_ void *m = NULL;
×
236
        size_t l;
×
237
        int r;
×
238

239
        assert(name);
×
240
        assert(data_what);
×
241
        assert(hash_what);
×
242

243
        if (!hash)
×
244
                return 0;
245

246
        if (*data_what && *hash_what)
×
247
                return 0;
248

249
        r = unhexmem(hash, &m, &l);
×
250
        if (r < 0)
×
251
                return log_error_errno(r, "Failed to parse hash: %s", hash);
×
252
        if (l < sizeof(sd_id128_t)) {
×
253
                log_debug("Root hash for %s is shorter than 128 bits (32 characters), ignoring for discovering verity partition.", name);
×
254
                return 0;
×
255
        }
256

257
        if (!*data_what) {
×
258
                memcpy(&data_uuid, m, sizeof(data_uuid));
×
259

260
                *data_what = path_join("/dev/disk/by-partuuid", SD_ID128_TO_UUID_STRING(data_uuid));
×
261
                if (!*data_what)
×
262
                        return log_oom();
×
263
        }
264

265
        if (!*hash_what) {
×
266
                memcpy(&verity_uuid, (uint8_t*) m + l - sizeof(verity_uuid), sizeof(verity_uuid));
×
267

268
                *hash_what = path_join("/dev/disk/by-partuuid", SD_ID128_TO_UUID_STRING(verity_uuid));
×
269
                if (!*hash_what)
×
270
                        return log_oom();
×
271
        }
272

273
        log_info("Using data device %s and hash device %s for %s.", *data_what, *hash_what, name);
×
274

275
        return 1;
276
}
277

278
static int determine_devices(void) {
×
279
        int r;
×
280

281
        r = determine_device("root", arg_root_hash, &arg_root_data_what, &arg_root_hash_what);
×
282
        if (r < 0)
×
283
                return r;
284

285
        return determine_device("usr", arg_usr_hash, &arg_usr_data_what, &arg_usr_hash_what);
×
286
}
287

288
static bool attach_in_initrd(const char *name, const char *options) {
×
289
        assert(name);
×
290

291
        /* Imply x-initrd.attach in case the volume name is among those defined in the Discoverable Partition
292
         * Specification for partitions that we require to be mounted during the initrd → host transition,
293
         * i.e. for the root fs itself, and /usr/. This mirrors similar behaviour in
294
         * systemd-fstab-generator. */
295

296
        return fstab_test_option(options, "x-initrd.attach\0") ||
×
297
                STR_IN_SET(name, "root", "usr");
×
298
}
299

300
static int create_veritytab_device(
×
301
                const char *name,
302
                const char *data_device,
303
                const char *hash_device,
304
                const char *roothash,
305
                const char *options,
306
                const char *source) {
307

308
        _cleanup_free_ char *n = NULL, *dd = NULL, *du = NULL, *hd = NULL, *hu = NULL, *e = NULL,
×
309
                            *du_escaped = NULL, *hu_escaped = NULL, *name_escaped = NULL;
×
310
        _cleanup_fclose_ FILE *f = NULL;
×
311
        const char *dmname;
×
312
        bool noauto, nofail, netdev, need_loop = false;
×
313
        int r;
×
314

315
        /* Creates a systemd-veritysetup@.service instance for volumes specified in /etc/veritytab. */
316

317
        assert(name);
×
318
        assert(data_device);
×
319
        assert(hash_device);
×
320
        assert(roothash);
×
321

322
        noauto = fstab_test_yes_no_option(options, "noauto\0" "auto\0");
×
323
        nofail = fstab_test_yes_no_option(options, "nofail\0" "fail\0");
×
324
        netdev = fstab_test_option(options, "_netdev\0");
×
325

326
        name_escaped = specifier_escape(name);
×
327
        if (!name_escaped)
×
328
                return log_oom();
×
329

330
        e = unit_name_escape(name);
×
331
        if (!e)
×
332
                return log_oom();
×
333

334
        du = fstab_node_to_udev_node(data_device);
×
335
        if (!du)
×
336
                return log_oom();
×
337

338
        hu = fstab_node_to_udev_node(hash_device);
×
339
        if (!hu)
×
340
                return log_oom();
×
341

342
        r = unit_name_build("systemd-veritysetup", e, ".service", &n);
×
343
        if (r < 0)
×
344
                return log_error_errno(r, "Failed to generate unit name: %m");
×
345

346
        du_escaped = specifier_escape(du);
×
347
        if (!du_escaped)
×
348
                return log_oom();
×
349

350
        hu_escaped = specifier_escape(hu);
×
351
        if (!hu_escaped)
×
352
                return log_oom();
×
353

354
        r = unit_name_from_path(du, ".device", &dd);
×
355
        if (r < 0)
×
356
                return log_error_errno(r, "Failed to generate unit name: %m");
×
357

358
        r = unit_name_from_path(hu, ".device", &hd);
×
359
        if (r < 0)
×
360
                return log_error_errno(r, "Failed to generate unit name: %m");
×
361

362
        r = generator_open_unit_file(arg_dest, NULL, n, &f);
×
363
        if (r < 0)
×
364
                return r;
365

366
        r = generator_write_veritysetup_unit_section(f, source);
×
367
        if (r < 0)
×
368
                return r;
369

370
        if (netdev)
×
371
                fprintf(f, "After=remote-fs-pre.target\n");
×
372

373
        /* If initrd takes care of attaching the disk then it should also detach it during shutdown. */
374
        if (!attach_in_initrd(name, options))
×
375
                fprintf(f,
×
376
                        "Conflicts=umount.target\n"
377
                        "Before=umount.target\n");
378

379
        if (!nofail)
×
380
                fprintf(f,
×
381
                        "Before=%s\n",
382
                        netdev ? "remote-veritysetup.target" : "veritysetup.target");
383

384
        if (path_startswith(du, "/dev/"))
×
385
                fprintf(f,
×
386
                        "BindsTo=%s\n"
387
                        "After=%s\n",
388
                        dd, dd);
389
        else {
390
                fprintf(f, "RequiresMountsFor=%s\n", du_escaped);
×
391
                need_loop = true;
392
        }
393

394
        if (path_startswith(hu, "/dev/"))
×
395
                fprintf(f,
×
396
                        "BindsTo=%s\n"
397
                        "After=%s\n",
398
                        hd, hd);
399
        else {
400
                fprintf(f, "RequiresMountsFor=%s\n", hu_escaped);
×
401
                need_loop = true;
402
        }
403

404
        if (need_loop)
×
405
                /* For loopback devices make sure to explicitly load loop.ko, as this code might run very
406
                 * early where device nodes created via systemd-tmpfiles-setup-dev.service might not be
407
                 * around yet. Hence let's sync on the module itself. */
408
                fprintf(f,
×
409
                        "Wants=modprobe@loop.service\n"
410
                        "After=modprobe@loop.service\n");
411

412
        r = generator_write_veritysetup_service_section(f, name, du, hu, roothash, options);
×
413
        if (r < 0)
×
414
                return r;
415

416
        r = fflush_and_check(f);
×
417
        if (r < 0)
×
418
                return log_error_errno(r, "Failed to write unit file %s: %m", n);
×
419

420
        if (!noauto) {
×
421
                r = generator_add_symlink(arg_dest,
×
422
                                          netdev ? "remote-veritysetup.target" : "veritysetup.target",
423
                                          nofail ? "wants" : "requires", n);
424
                if (r < 0)
×
425
                        return r;
426
        }
427

428
        dmname = strjoina("dev-mapper-", e, ".device");
×
429
        return generator_add_symlink(arg_dest, dmname, "requires", n);
×
430
}
431

432
static int add_veritytab_devices(void) {
×
433
        _cleanup_fclose_ FILE *f = NULL;
×
434
        unsigned veritytab_line = 0;
×
435
        int r;
×
436

437
        if (!arg_read_veritytab)
×
438
                return 0;
439

440
        r = fopen_unlocked(arg_veritytab, "re", &f);
×
441
        if (r < 0) {
×
442
                if (errno != ENOENT)
×
443
                        log_error_errno(errno, "Failed to open %s: %m", arg_veritytab);
×
444
                return 0;
×
445
        }
446

447
        for (;;) {
×
448
                _cleanup_free_ char *line = NULL, *name = NULL, *data_device = NULL, *hash_device = NULL,
×
449
                                    *roothash = NULL, *options = NULL;
×
450

451
                r = read_stripped_line(f, LONG_LINE_MAX, &line);
×
452
                if (r < 0)
×
453
                        return log_error_errno(r, "Failed to read %s: %m", arg_veritytab);
×
454
                if (r == 0)
×
455
                        break;
456

457
                veritytab_line++;
×
458

459
                if (IN_SET(line[0], 0, '#'))
×
460
                        continue;
×
461

462
                r = sscanf(line, "%ms %ms %ms %ms %ms", &name, &data_device, &hash_device, &roothash, &options);
×
463
                if (!IN_SET(r, 4, 5)) {
×
464
                        log_error("Failed to parse %s:%u, ignoring.", arg_veritytab, veritytab_line);
×
465
                        continue;
×
466
                }
467

468
                r = create_veritytab_device(
×
469
                                name,
470
                                data_device,
471
                                hash_device,
472
                                roothash,
473
                                options,
474
                                arg_veritytab);
475
                if (r < 0)
×
476
                        return r;
477
        }
478

479
        return 0;
×
480
}
481

482
static int run(const char *dest, const char *dest_early, const char *dest_late) {
×
483
        int r;
×
484

485
        assert_se(arg_dest = dest);
×
486

487
        arg_veritytab = getenv("SYSTEMD_VERITYTAB") ?: "/etc/veritytab";
×
488

489
        r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, PROC_CMDLINE_STRIP_RD_PREFIX);
×
490
        if (r < 0)
×
491
                return log_warning_errno(r, "Failed to parse kernel command line: %m");
×
492

493
        if (!arg_enabled)
×
494
                return 0;
495

496
        r = add_veritytab_devices();
×
497
        if (r < 0)
×
498
                return r;
499

500
        r = determine_devices();
×
501
        if (r < 0)
×
502
                return r;
503

504
        r = create_root_device();
×
505
        if (r < 0)
×
506
                return r;
507

508
        return create_usr_device();
×
509
}
510

511
DEFINE_MAIN_GENERATOR_FUNCTION(run);
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc