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

systemd / systemd / 12877533250

20 Jan 2025 11:16PM UTC coverage: 0.117%. Remained the same
12877533250

push

github

web-flow
pidfd: cache our own pidfd inode id, and use it at various places (#36060)

This is split out of and preparation for #35224, but makes a ton of
sense on its own

0 of 95 new or added lines in 10 files covered. (0.0%)

4667 existing lines in 34 files now uncovered.

478 of 408040 relevant lines covered (0.12%)

1.45 hits per line

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

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

3
#include <errno.h>
4
#include <fcntl.h>
5
#include <sys/stat.h>
6
#include <sys/types.h>
7

8
#include "alloc-util.h"
9
#include "cryptsetup-util.h"
10
#include "dropin.h"
11
#include "escape.h"
12
#include "fd-util.h"
13
#include "fileio.h"
14
#include "fstab-util.h"
15
#include "generator.h"
16
#include "hashmap.h"
17
#include "id128-util.h"
18
#include "log.h"
19
#include "mkdir.h"
20
#include "parse-util.h"
21
#include "path-util.h"
22
#include "proc-cmdline.h"
23
#include "specifier.h"
24
#include "string-util.h"
25
#include "strv.h"
26
#include "unit-name.h"
27

28
typedef struct crypto_device {
29
        char *uuid;
30
        char *keyfile;
31
        char *keydev;
32
        char *headerdev;
33
        char *datadev;
34
        char *name;
35
        char *options;
36
        bool create;
37
} crypto_device;
38

39
static const char *arg_dest = NULL;
40
static bool arg_enabled = true;
41
static bool arg_read_crypttab = true;
42
static const char *arg_crypttab = NULL;
43
static const char *arg_runtime_directory = NULL;
44
static bool arg_allow_list = false;
45
static Hashmap *arg_disks = NULL;
46
static char *arg_default_options = NULL;
47
static char *arg_default_keyfile = NULL;
48

49
STATIC_DESTRUCTOR_REGISTER(arg_disks, hashmap_freep);
×
50
STATIC_DESTRUCTOR_REGISTER(arg_default_options, freep);
×
UNCOV
51
STATIC_DESTRUCTOR_REGISTER(arg_default_keyfile, freep);
×
52

53
static int split_locationspec(const char *locationspec, char **ret_file, char **ret_device) {
×
54
        _cleanup_free_ char *file = NULL, *device = NULL;
×
UNCOV
55
        const char *c;
×
56

57
        assert(ret_file);
×
UNCOV
58
        assert(ret_device);
×
59

60
        if (!locationspec) {
×
61
                *ret_file = *ret_device = NULL;
×
UNCOV
62
                return 0;
×
63
        }
64

65
        c = strrchr(locationspec, ':');
×
UNCOV
66
        if (c) {
×
67
                /* The device part has to be either an absolute path to device node (/dev/something,
68
                 * /dev/foo/something, or even possibly /dev/foo/something:part), or a fstab device
69
                 * specification starting with LABEL= or similar. The file part has the same syntax.
70
                 *
71
                 * Let's try to guess if the second part looks like a device specification, or just part of a
72
                 * filename with a colon. fstab_node_to_udev_node() will convert the fstab device syntax to
73
                 * an absolute path. If we didn't get an absolute path, assume that it is just part of the
74
                 * first file argument. */
75

76
                device = fstab_node_to_udev_node(c + 1);
×
77
                if (!device)
×
UNCOV
78
                        return log_oom();
×
79

80
                if (path_is_absolute(device))
×
UNCOV
81
                        file = strndup(locationspec, c-locationspec);
×
82
                else {
UNCOV
83
                        log_debug("Location specification argument contains a colon, but \"%s\" doesn't look like a device specification.\n"
×
84
                                  "Assuming that \"%s\" is a single device specification.",
85
                                  c + 1, locationspec);
86
                        device = mfree(device);
×
UNCOV
87
                        c = NULL;
×
88
                }
89
        }
90

UNCOV
91
        if (!c)
×
92
                /* No device specified */
UNCOV
93
                file = strdup(locationspec);
×
94

95
        if (!file)
×
UNCOV
96
                return log_oom();
×
97

98
        *ret_file = TAKE_PTR(file);
×
UNCOV
99
        *ret_device = TAKE_PTR(device);
×
100

UNCOV
101
        return 0;
×
102
}
103

UNCOV
104
static int generate_device_mount(
×
105
                const char *name,
106
                const char *device,
107
                const char *type_prefix, /* "keydev" or "headerdev" */
108
                const char *device_timeout,
109
                bool canfail,
110
                bool readonly,
111
                char **unit,
112
                char **mount) {
113

114
        _cleanup_free_ char *u = NULL, *where = NULL, *name_escaped = NULL, *device_unit = NULL;
×
115
        _cleanup_fclose_ FILE *f = NULL;
×
116
        int r;
×
UNCOV
117
        usec_t timeout_us;
×
118

119
        assert(name);
×
120
        assert(device);
×
121
        assert(unit);
×
UNCOV
122
        assert(mount);
×
123

124
        r = mkdir_parents(arg_runtime_directory, 0755);
×
UNCOV
125
        if (r < 0)
×
126
                return r;
127

128
        r = mkdir(arg_runtime_directory, 0700);
×
129
        if (r < 0 && errno != EEXIST)
×
UNCOV
130
                return -errno;
×
131

132
        name_escaped = cescape(name);
×
UNCOV
133
        if (!name_escaped)
×
134
                return -ENOMEM;
135

136
        where = strjoin(arg_runtime_directory, "/", type_prefix, "-", name_escaped);
×
UNCOV
137
        if (!where)
×
138
                return -ENOMEM;
139

140
        r = mkdir(where, 0700);
×
141
        if (r < 0 && errno != EEXIST)
×
UNCOV
142
                return -errno;
×
143

144
        r = unit_name_from_path(where, ".mount", &u);
×
UNCOV
145
        if (r < 0)
×
146
                return r;
147

148
        r = generator_open_unit_file(arg_dest, NULL, u, &f);
×
UNCOV
149
        if (r < 0)
×
150
                return r;
151

UNCOV
152
        fprintf(f,
×
153
                "[Unit]\n"
154
                "DefaultDependencies=no\n\n"
155
                "[Mount]\n"
156
                "What=%s\n"
157
                "Where=%s\n"
158
                "Options=%s%s\n", device, where, readonly ? "ro" : "rw", canfail ? ",nofail" : "");
159

160
        if (device_timeout) {
×
161
                r = parse_sec_fix_0(device_timeout, &timeout_us);
×
162
                if (r >= 0) {
×
163
                        r = unit_name_from_path(device, ".device", &device_unit);
×
164
                        if (r < 0)
×
UNCOV
165
                                return log_error_errno(r, "Failed to generate unit name: %m");
×
166

UNCOV
167
                        r = write_drop_in_format(arg_dest, device_unit, 90, "device-timeout",
×
168
                                "# Automatically generated by systemd-cryptsetup-generator \n\n"
169
                                "[Unit]\nJobRunningTimeoutSec=%s", device_timeout);
170
                        if (r < 0)
×
UNCOV
171
                                return log_error_errno(r, "Failed to write device drop-in: %m");
×
172

173
                } else
UNCOV
174
                        log_warning_errno(r, "Failed to parse %s, ignoring: %m", device_timeout);
×
175

176
        }
177

178
        r = fflush_and_check(f);
×
UNCOV
179
        if (r < 0)
×
180
                return r;
181

182
        *unit = TAKE_PTR(u);
×
UNCOV
183
        *mount = TAKE_PTR(where);
×
184

UNCOV
185
        return 0;
×
186
}
187

UNCOV
188
static int generate_device_umount(const char *name,
×
189
                                  const char *device_mount,
190
                                  const char *type_prefix, /* "keydev" or "headerdev" */
191
                                  char **ret_umount_unit) {
192
        _cleanup_fclose_ FILE *f = NULL;
×
193
        _cleanup_free_ char *u = NULL, *name_escaped = NULL, *mount = NULL;
×
UNCOV
194
        int r;
×
195

196
        assert(name);
×
UNCOV
197
        assert(ret_umount_unit);
×
198

199
        name_escaped = cescape(name);
×
UNCOV
200
        if (!name_escaped)
×
201
                return -ENOMEM;
202

203
        u = strjoin(type_prefix, "-", name_escaped, "-umount.service");
×
UNCOV
204
        if (!u)
×
205
                return -ENOMEM;
206

207
        r = unit_name_from_path(device_mount, ".mount", &mount);
×
UNCOV
208
        if (r < 0)
×
209
                return r;
210

211
        r = generator_open_unit_file(arg_dest, NULL, u, &f);
×
UNCOV
212
        if (r < 0)
×
213
                return r;
214

UNCOV
215
        fprintf(f,
×
216
                "[Unit]\n"
217
                "DefaultDependencies=no\n"
218
                "After=%s\n\n"
219
                "[Service]\n"
220
                "ExecStart=-" UMOUNT_PATH " %s\n\n", mount, device_mount);
221

222
        r = fflush_and_check(f);
×
UNCOV
223
        if (r < 0)
×
224
                return r;
225

226
        *ret_umount_unit = TAKE_PTR(u);
×
UNCOV
227
        return 0;
×
228
}
229

230
static int print_dependencies(FILE *f, const char* device_path, const char* timeout_value, bool canfail) {
×
UNCOV
231
        int r;
×
232

233
        assert(f);
×
UNCOV
234
        assert(device_path);
×
235

UNCOV
236
        if (!mangle_none(device_path))
×
237
                /* None, nothing to do */
UNCOV
238
                return 0;
×
239

UNCOV
240
        if (PATH_IN_SET(device_path,
×
241
                        "/dev/urandom",
242
                        "/dev/random",
243
                        "/dev/hw_random",
244
                        "/dev/hwrng")) {
245
                /* RNG device, add random dep */
246
                fputs("After=systemd-random-seed.service\n", f);
×
UNCOV
247
                return 0;
×
248
        }
249

250
        _cleanup_free_ char *udev_node = fstab_node_to_udev_node(device_path);
×
251
        if (!udev_node)
×
UNCOV
252
                return log_oom();
×
253

UNCOV
254
        if (path_equal(udev_node, "/dev/null"))
×
255
                return 0;
256

UNCOV
257
        if (path_startswith(udev_node, "/dev/")) {
×
258
                /* We are dealing with a block device, add dependency for corresponding unit */
UNCOV
259
                _cleanup_free_ char *unit = NULL;
×
260

261
                r = unit_name_from_path(udev_node, ".device", &unit);
×
262
                if (r < 0)
×
UNCOV
263
                        return log_error_errno(r, "Failed to generate unit name: %m");
×
264

265
                fprintf(f, "After=%1$s\n", unit);
×
266
                if (canfail) {
×
267
                        fprintf(f, "Wants=%1$s\n", unit);
×
268
                        if (timeout_value) {
×
UNCOV
269
                                r = write_drop_in_format(arg_dest, unit, 90, "device-timeout",
×
270
                                        "# Automatically generated by systemd-cryptsetup-generator \n\n"
271
                                        "[Unit]\nJobRunningTimeoutSec=%s", timeout_value);
272
                                if (r < 0)
×
UNCOV
273
                                        return log_error_errno(r, "Failed to write device drop-in: %m");
×
274
                        }
275
                } else
UNCOV
276
                        fprintf(f, "Requires=%1$s\n", unit);
×
277
        } else {
278
                /* Regular file, add mount dependency */
279
                _cleanup_free_ char *escaped_path = specifier_escape(device_path);
×
280
                if (!escaped_path)
×
UNCOV
281
                        return log_oom();
×
282

UNCOV
283
                fprintf(f, "%s=%s\n", canfail ? "WantsMountsFor" : "RequiresMountsFor", escaped_path);
×
284
        }
285

286
        return 0;
287
}
288

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

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

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

UNCOV
301
static int create_disk(
×
302
                const char *name,
303
                const char *device,
304
                const char *key_file,
305
                const char *keydev,
306
                const char *headerdev,
307
                const char *options,
308
                const char *source) {
309

310
        _cleanup_free_ char *n = NULL, *d = NULL, *u = NULL, *e = NULL,
×
311
                *keydev_mount = NULL, *keyfile_timeout_value = NULL,
×
312
                *filtered = NULL, *u_escaped = NULL, *name_escaped = NULL, *header_path = NULL, *key_file_buffer = NULL,
×
313
                *tmp_fstype = NULL, *filtered_header = NULL, *headerdev_mount = NULL;
×
314
        _cleanup_fclose_ FILE *f = NULL;
×
315
        const char *dmname;
×
316
        bool noauto, nofail, swap, netdev;
×
UNCOV
317
        int r, detached_header, keyfile_can_timeout, tmp;
×
318

319
        assert(name);
×
UNCOV
320
        assert(device);
×
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
        swap = fstab_test_option(options, "swap\0");
×
UNCOV
325
        netdev = fstab_test_option(options, "_netdev\0");
×
326

UNCOV
327
        keyfile_can_timeout = fstab_filter_options(options,
×
328
                                                   "keyfile-timeout\0",
329
                                                   NULL, &keyfile_timeout_value, NULL, NULL);
330
        if (keyfile_can_timeout < 0)
×
UNCOV
331
                return log_error_errno(keyfile_can_timeout, "Failed to parse keyfile-timeout= option value: %m");
×
332

UNCOV
333
        detached_header = fstab_filter_options(
×
334
                options,
335
                "header\0",
336
                NULL,
337
                &header_path,
338
                NULL,
339
                headerdev ? &filtered_header : NULL);
340
        if (detached_header < 0)
×
UNCOV
341
                return log_error_errno(detached_header, "Failed to parse header= option value: %m");
×
342

343
        tmp = fstab_filter_options(options, "tmp\0", NULL, &tmp_fstype, NULL, NULL);
×
344
        if (tmp < 0)
×
UNCOV
345
                return log_error_errno(tmp, "Failed to parse tmp= option value: %m");
×
346

347
        if (tmp && swap)
×
UNCOV
348
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
349
                                       "Device '%s' cannot be both 'tmp' and 'swap'. Ignoring.",
350
                                       name);
351

352
        name_escaped = specifier_escape(name);
×
353
        if (!name_escaped)
×
UNCOV
354
                return log_oom();
×
355

356
        e = unit_name_escape(name);
×
357
        if (!e)
×
UNCOV
358
                return log_oom();
×
359

360
        u = fstab_node_to_udev_node(device);
×
361
        if (!u)
×
UNCOV
362
                return log_oom();
×
363

364
        r = unit_name_build("systemd-cryptsetup", e, ".service", &n);
×
365
        if (r < 0)
×
UNCOV
366
                return log_error_errno(r, "Failed to generate unit name: %m");
×
367

368
        u_escaped = specifier_escape(u);
×
369
        if (!u_escaped)
×
UNCOV
370
                return log_oom();
×
371

372
        r = unit_name_from_path(u, ".device", &d);
×
373
        if (r < 0)
×
UNCOV
374
                return log_error_errno(r, "Failed to generate unit name: %m");
×
375

376
        if (keydev && !key_file)
×
UNCOV
377
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
378
                                       "Key device is specified, but path to the key file is missing.");
379

380
        r = generator_open_unit_file(arg_dest, NULL, n, &f);
×
UNCOV
381
        if (r < 0)
×
382
                return r;
383

384
        r = generator_write_cryptsetup_unit_section(f, source);
×
UNCOV
385
        if (r < 0)
×
386
                return r;
387

388
        if (netdev)
×
UNCOV
389
                fprintf(f, "After=remote-fs-pre.target\n");
×
390

391
        /* If initrd takes care of attaching the disk then it should also detach it during shutdown. */
392
        if (!attach_in_initrd(name, options))
×
UNCOV
393
                fprintf(f,
×
394
                        "Conflicts=umount.target\n"
395
                        "Before=umount.target\n");
396

397
        if (keydev) {
×
UNCOV
398
                _cleanup_free_ char *unit = NULL, *umount_unit = NULL;
×
399

UNCOV
400
                r = generate_device_mount(
×
401
                        name,
402
                        keydev,
403
                        "keydev",
404
                        keyfile_timeout_value,
405
                        /* canfail = */ keyfile_can_timeout > 0,
406
                        /* readonly= */ true,
407
                        &unit,
408
                        &keydev_mount);
409
                if (r < 0)
×
UNCOV
410
                        return log_error_errno(r, "Failed to generate keydev mount unit: %m");
×
411

412
                r = generate_device_umount(name, keydev_mount, "keydev", &umount_unit);
×
413
                if (r < 0)
×
UNCOV
414
                        return log_error_errno(r, "Failed to generate keydev umount unit: %m");
×
415

416
                key_file_buffer = path_join(keydev_mount, key_file);
×
417
                if (!key_file_buffer)
×
UNCOV
418
                        return log_oom();
×
419

UNCOV
420
                key_file = key_file_buffer;
×
421

422
                fprintf(f, "After=%s\n", unit);
×
423
                if (keyfile_can_timeout > 0)
×
UNCOV
424
                        fprintf(f, "Wants=%s\n", unit);
×
425
                else
UNCOV
426
                        fprintf(f, "Requires=%s\n", unit);
×
427

428
                if (umount_unit)
×
UNCOV
429
                        fprintf(f,
×
430
                                "Wants=%s\n"
431
                                "Before=%s\n",
432
                                umount_unit,
433
                                umount_unit
434
                        );
435
        }
436

437
        if (headerdev) {
×
UNCOV
438
                _cleanup_free_ char *unit = NULL, *umount_unit = NULL, *p = NULL;
×
439

UNCOV
440
                r = generate_device_mount(
×
441
                        name,
442
                        headerdev,
443
                        "headerdev",
444
                        NULL,
445
                        /* canfail=  */ false, /* header is always necessary */
446
                        /* readonly= */ false, /* LUKS2 recovery requires rw header access */
447
                        &unit,
448
                        &headerdev_mount);
449
                if (r < 0)
×
UNCOV
450
                        return log_error_errno(r, "Failed to generate header device mount unit: %m");
×
451

452
                r = generate_device_umount(name, headerdev_mount, "headerdev", &umount_unit);
×
453
                if (r < 0)
×
UNCOV
454
                        return log_error_errno(r, "Failed to generate header device umount unit: %m");
×
455

456
                p = path_join(headerdev_mount, header_path);
×
457
                if (!p)
×
UNCOV
458
                        return log_oom();
×
459

UNCOV
460
                free_and_replace(header_path, p);
×
461

462
                if (isempty(filtered_header))
×
UNCOV
463
                        p = strjoin("header=", header_path);
×
464
                else
UNCOV
465
                        p = strjoin(filtered_header, ",header=", header_path);
×
466

467
                if (!p)
×
UNCOV
468
                        return log_oom();
×
469

470
                free_and_replace(filtered_header, p);
×
UNCOV
471
                options = filtered_header;
×
472

UNCOV
473
                fprintf(f, "After=%s\n"
×
474
                           "Requires=%s\n", unit, unit);
475

476
                if (umount_unit)
×
UNCOV
477
                        fprintf(f,
×
478
                                "Wants=%s\n"
479
                                "Before=%s\n",
480
                                umount_unit,
481
                                umount_unit
482
                        );
483
        }
484

485
        if (!nofail)
×
UNCOV
486
                fprintf(f,
×
487
                        "Before=%s\n",
488
                        netdev ? "remote-cryptsetup.target" : "cryptsetup.target");
489

490
        if (key_file && !keydev) {
×
UNCOV
491
                r = print_dependencies(f, key_file,
×
492
                        keyfile_timeout_value,
493
                        /* canfail= */ keyfile_can_timeout > 0 || nofail);
×
UNCOV
494
                if (r < 0)
×
495
                        return r;
496
        }
497

498
        /* Check if a header option was specified */
499
        if (detached_header > 0 && !headerdev) {
×
UNCOV
500
                r = print_dependencies(f, header_path,
×
501
                        /* timeout_value= */ NULL,
502
                        /* canfail= */ nofail);
UNCOV
503
                if (r < 0)
×
504
                        return r;
505
        }
506

507
        if (path_startswith(u, "/dev/"))
×
UNCOV
508
                fprintf(f,
×
509
                        "BindsTo=%s\n"
510
                        "After=%s\n",
511
                        d, d);
512
        else
513
                /* For loopback devices make sure to explicitly load loop.ko, as this code might run very
514
                 * early where device nodes created via systemd-tmpfiles-setup-dev.service might not be
515
                 * around yet. Hence let's sync on the module itself. */
UNCOV
516
                fprintf(f,
×
517
                        "RequiresMountsFor=%s\n"
518
                        "Wants=modprobe@loop.service\n"
519
                        "After=modprobe@loop.service\n",
520
                        u_escaped);
521

522
        r = generator_write_device_timeout(arg_dest, device, options, &filtered);
×
523
        if (r < 0)
×
UNCOV
524
                log_warning_errno(r, "Failed to write device timeout drop-in: %m");
×
525

526
        r = generator_write_cryptsetup_service_section(f, name, u, key_file, filtered);
×
UNCOV
527
        if (r < 0)
×
528
                return r;
529

530
        if (tmp) {
×
UNCOV
531
                _cleanup_free_ char *tmp_fstype_escaped = NULL;
×
532

533
                if (tmp_fstype) {
×
534
                        tmp_fstype_escaped = specifier_escape(tmp_fstype);
×
535
                        if (!tmp_fstype_escaped)
×
UNCOV
536
                                return log_oom();
×
537
                }
538

UNCOV
539
                fprintf(f,
×
540
                        "ExecStartPost=" LIBEXECDIR "/systemd-makefs '%s' '/dev/mapper/%s'\n",
541
                        tmp_fstype_escaped ?: "ext4", name_escaped);
542
        }
543

544
        if (swap)
×
UNCOV
545
                fprintf(f,
×
546
                        "ExecStartPost=" LIBEXECDIR "/systemd-makefs swap '/dev/mapper/%s'\n",
547
                        name_escaped);
548

549
        r = fflush_and_check(f);
×
550
        if (r < 0)
×
UNCOV
551
                return log_error_errno(r, "Failed to write unit file %s: %m", n);
×
552

553
        if (!noauto) {
×
UNCOV
554
                r = generator_add_symlink(arg_dest,
×
555
                                          netdev ? "remote-cryptsetup.target" : "cryptsetup.target",
556
                                          nofail ? "wants" : "requires", n);
UNCOV
557
                if (r < 0)
×
558
                        return r;
559
        }
560

561
        dmname = strjoina("dev-mapper-", e, ".device");
×
562
        r = generator_add_symlink(arg_dest, dmname, "requires", n);
×
UNCOV
563
        if (r < 0)
×
564
                return r;
565

566
        if (!noauto && !nofail) {
×
UNCOV
567
                r = write_drop_in(arg_dest, dmname, 40, "device-timeout",
×
568
                                  "# Automatically generated by systemd-cryptsetup-generator\n\n"
569
                                  "[Unit]\n"
570
                                  "JobTimeoutSec=infinity\n");
571
                if (r < 0)
×
UNCOV
572
                        log_warning_errno(r, "Failed to write device timeout drop-in: %m");
×
573
        }
574

575
        return 0;
576
}
577

578
static crypto_device* crypt_device_free(crypto_device *d) {
×
UNCOV
579
        if (!d)
×
580
                return NULL;
581

582
        free(d->uuid);
×
583
        free(d->keyfile);
×
584
        free(d->keydev);
×
585
        free(d->headerdev);
×
586
        free(d->datadev);
×
587
        free(d->name);
×
588
        free(d->options);
×
UNCOV
589
        return mfree(d);
×
590
}
591

592
static crypto_device *get_crypto_device(const char *uuid) {
×
593
        int r;
×
UNCOV
594
        crypto_device *d;
×
595

UNCOV
596
        assert(uuid);
×
597

598
        d = hashmap_get(arg_disks, uuid);
×
599
        if (!d) {
×
600
                d = new0(struct crypto_device, 1);
×
UNCOV
601
                if (!d)
×
602
                        return NULL;
603

604
                d->uuid = strdup(uuid);
×
605
                if (!d->uuid)
×
UNCOV
606
                        return mfree(d);
×
607

608
                r = hashmap_put(arg_disks, d->uuid, d);
×
609
                if (r < 0) {
×
610
                        free(d->uuid);
×
UNCOV
611
                        return mfree(d);
×
612
                }
613
        }
614

615
        return d;
616
}
617

618
static bool warn_uuid_invalid(const char *uuid, const char *key) {
×
UNCOV
619
        assert(key);
×
620

621
        if (!id128_is_valid(uuid)) {
×
622
                log_warning("Failed to parse %s= kernel command line switch. UUID is invalid, ignoring.", key);
×
UNCOV
623
                return true;
×
624
        }
625

626
        return false;
627
}
628

UNCOV
629
static int filter_header_device(const char *options,
×
630
                                char **ret_headerdev,
631
                                char **ret_filtered_headerdev_options) {
632
        int r;
×
633
        _cleanup_free_ char *headerfile = NULL, *headerdev = NULL, *headerspec = NULL,
×
UNCOV
634
                            *filtered_headerdev = NULL, *filtered_headerspec = NULL;
×
635

636
        assert(ret_headerdev);
×
UNCOV
637
        assert(ret_filtered_headerdev_options);
×
638

639
        r = fstab_filter_options(options, "header\0", NULL, &headerspec, NULL, &filtered_headerspec);
×
640
        if (r < 0)
×
UNCOV
641
                return log_error_errno(r, "Failed to parse header= option value: %m");
×
642

643
        if (r > 0) {
×
644
                r = split_locationspec(headerspec, &headerfile, &headerdev);
×
UNCOV
645
                if (r < 0)
×
646
                        return r;
647

648
                if (isempty(filtered_headerspec))
×
UNCOV
649
                        filtered_headerdev = strjoin("header=", headerfile);
×
650
                else
UNCOV
651
                        filtered_headerdev = strjoin(filtered_headerspec, ",header=", headerfile);
×
652

653
                if (!filtered_headerdev)
×
UNCOV
654
                        return log_oom();
×
655
        } else
UNCOV
656
                filtered_headerdev = TAKE_PTR(filtered_headerspec);
×
657

658
        *ret_filtered_headerdev_options = TAKE_PTR(filtered_headerdev);
×
UNCOV
659
        *ret_headerdev = TAKE_PTR(headerdev);
×
660

UNCOV
661
        return 0;
×
662
}
663

664
static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
×
665
        _cleanup_free_ char *uuid = NULL, *uuid_value = NULL;
×
666
        crypto_device *d;
×
UNCOV
667
        int r;
×
668

UNCOV
669
        if (streq(key, "luks")) {
×
670

671
                r = value ? parse_boolean(value) : 1;
×
672
                if (r < 0)
×
UNCOV
673
                        log_warning("Failed to parse luks= kernel command line switch %s. Ignoring.", value);
×
674
                else
UNCOV
675
                        arg_enabled = r;
×
676

UNCOV
677
        } else if (streq(key, "luks.crypttab")) {
×
678

679
                r = value ? parse_boolean(value) : 1;
×
680
                if (r < 0)
×
UNCOV
681
                        log_warning("Failed to parse luks.crypttab= kernel command line switch %s. Ignoring.", value);
×
682
                else
UNCOV
683
                        arg_read_crypttab = r;
×
684

UNCOV
685
        } else if (streq(key, "luks.uuid")) {
×
686

UNCOV
687
                if (proc_cmdline_value_missing(key, value))
×
688
                        return 0;
689

690
                d = get_crypto_device(startswith(value, "luks-") ?: value);
×
691
                if (!d)
×
UNCOV
692
                        return log_oom();
×
693

UNCOV
694
                d->create = arg_allow_list = true;
×
695

696
        } else if (streq(key, "luks.options")) {
×
UNCOV
697
                _cleanup_free_ char *headerdev = NULL, *filtered_headerdev_options = NULL;
×
698

UNCOV
699
                if (proc_cmdline_value_missing(key, value))
×
700
                        return 0;
701

702
                r = sscanf(value, "%m[0-9a-fA-F-]=%ms", &uuid, &uuid_value);
×
703
                if (r != 2)
×
UNCOV
704
                        return free_and_strdup_warn(&arg_default_options, value);
×
705

UNCOV
706
                if (warn_uuid_invalid(uuid, key))
×
707
                        return 0;
708

709
                d = get_crypto_device(uuid);
×
710
                if (!d)
×
UNCOV
711
                        return log_oom();
×
712

713
                r = filter_header_device(uuid_value, &headerdev, &filtered_headerdev_options);
×
UNCOV
714
                if (r < 0)
×
715
                        return r;
716

717
                free_and_replace(d->options, filtered_headerdev_options);
×
718
                free_and_replace(d->headerdev, headerdev);
×
719
        } else if (streq(key, "luks.key")) {
×
720
                size_t n;
×
721
                _cleanup_free_ char *keyfile = NULL, *keydev = NULL;
×
UNCOV
722
                const char *keyspec;
×
723

UNCOV
724
                if (proc_cmdline_value_missing(key, value))
×
725
                        return 0;
726

727
                n = strspn(value, ALPHANUMERICAL "-");
×
728
                if (value[n] != '=')
×
UNCOV
729
                        return free_and_strdup_warn(&arg_default_keyfile, value);
×
730

731
                uuid = strndup(value, n);
×
732
                if (!uuid)
×
UNCOV
733
                        return log_oom();
×
734

UNCOV
735
                if (warn_uuid_invalid(uuid, key))
×
736
                        return 0;
737

738
                d = get_crypto_device(uuid);
×
739
                if (!d)
×
UNCOV
740
                        return log_oom();
×
741

742
                keyspec = value + n + 1;
×
743
                r = split_locationspec(keyspec, &keyfile, &keydev);
×
UNCOV
744
                if (r < 0)
×
745
                        return r;
746

747
                free_and_replace(d->keyfile, keyfile);
×
748
                free_and_replace(d->keydev, keydev);
×
749
        } else if (streq(key, "luks.data")) {
×
750
                size_t n;
×
UNCOV
751
                _cleanup_free_ char *datadev = NULL;
×
752

UNCOV
753
                if (proc_cmdline_value_missing(key, value))
×
754
                        return 0;
755

756
                n = strspn(value, ALPHANUMERICAL "-");
×
757
                if (value[n] != '=') {
×
758
                        log_warning("Failed to parse luks.data= kernel command line switch. UUID is invalid, ignoring.");
×
UNCOV
759
                        return 0;
×
760
                }
761

762
                uuid = strndup(value, n);
×
763
                if (!uuid)
×
UNCOV
764
                        return log_oom();
×
765

UNCOV
766
                if (warn_uuid_invalid(uuid, key))
×
767
                        return 0;
768

769
                d = get_crypto_device(uuid);
×
770
                if (!d)
×
UNCOV
771
                        return log_oom();
×
772

773
                datadev = fstab_node_to_udev_node(value + n + 1);
×
774
                if (!datadev)
×
UNCOV
775
                        return log_oom();
×
776

777
                free_and_replace(d->datadev, datadev);
×
UNCOV
778
        } else if (streq(key, "luks.name")) {
×
779

UNCOV
780
                if (proc_cmdline_value_missing(key, value))
×
781
                        return 0;
782

783
                r = sscanf(value, "%m[0-9a-fA-F-]=%ms", &uuid, &uuid_value);
×
784
                if (r == 2) {
×
785
                        d = get_crypto_device(uuid);
×
786
                        if (!d)
×
UNCOV
787
                                return log_oom();
×
788

UNCOV
789
                        d->create = arg_allow_list = true;
×
790

UNCOV
791
                        free_and_replace(d->name, uuid_value);
×
792
                } else
UNCOV
793
                        log_warning("Failed to parse luks name switch %s. Ignoring.", value);
×
794
        }
795

796
        return 0;
797
}
798

799
static int add_crypttab_device(const char *name, const char *device, const char *keyspec, const char *options) {
×
800
        _cleanup_free_ char *keyfile = NULL, *keydev = NULL, *headerdev = NULL, *filtered_header = NULL;
×
801
        crypto_device *d = NULL;
×
802
        char *uuid;
×
UNCOV
803
        int r;
×
804

805
        uuid = startswith(device, "UUID=");
×
806
        if (!uuid)
×
807
                uuid = path_startswith(device, "/dev/disk/by-uuid/");
×
808
        if (!uuid)
×
809
                uuid = startswith(name, "luks-");
×
810
        if (uuid)
×
UNCOV
811
                d = hashmap_get(arg_disks, uuid);
×
812

813
        if (arg_allow_list && !d) {
×
814
                log_info("Not creating device '%s' because it was not specified on the kernel command line.", name);
×
UNCOV
815
                return 0;
×
816
        }
817

818
        r = split_locationspec(keyspec, &keyfile, &keydev);
×
UNCOV
819
        if (r < 0)
×
820
                return r;
821

822
        if (options && (!d || !d->options)) {
×
823
                r = filter_header_device(options, &headerdev, &filtered_header);
×
UNCOV
824
                if (r < 0)
×
825
                        return r;
UNCOV
826
                options = filtered_header;
×
827
        }
828

UNCOV
829
        r = create_disk(name,
×
830
                        device,
831
                        keyfile,
832
                        keydev,
833
                        (d && d->options) ? d->headerdev : headerdev,
×
UNCOV
834
                        (d && d->options) ? d->options : options,
×
835
                        arg_crypttab);
UNCOV
836
        if (r < 0)
×
837
                return r;
838

839
        if (d)
×
UNCOV
840
                d->create = false;
×
841

842
        return 0;
843
}
844

845
static int add_crypttab_devices(void) {
×
846
        _cleanup_fclose_ FILE *f = NULL;
×
847
        unsigned crypttab_line = 0;
×
UNCOV
848
        int r, ret = 0;
×
849

UNCOV
850
        if (!arg_read_crypttab)
×
851
                return 0;
852

853
        r = fopen_unlocked(arg_crypttab, "re", &f);
×
854
        if (r < 0) {
×
855
                if (errno != ENOENT)
×
856
                        log_error_errno(errno, "Failed to open %s: %m", arg_crypttab);
×
UNCOV
857
                return 0;
×
858
        }
859

860
        for (;;) {
×
861
                _cleanup_free_ char *line = NULL, *name = NULL, *device = NULL, *keyspec = NULL, *options = NULL;
×
UNCOV
862
                int k;
×
863

864
                r = read_stripped_line(f, LONG_LINE_MAX, &line);
×
865
                if (r < 0)
×
866
                        return log_error_errno(r, "Failed to read %s: %m", arg_crypttab);
×
UNCOV
867
                if (r == 0)
×
868
                        break;
869

UNCOV
870
                crypttab_line++;
×
871

872
                if (IN_SET(line[0], 0, '#'))
×
UNCOV
873
                        continue;
×
874

875
                k = sscanf(line, "%ms %ms %ms %ms", &name, &device, &keyspec, &options);
×
876
                if (k < 2 || k > 4) {
×
877
                        log_error("Failed to parse %s:%u, ignoring.", arg_crypttab, crypttab_line);
×
UNCOV
878
                        continue;
×
879
                }
880

UNCOV
881
                RET_GATHER(ret, add_crypttab_device(name, device, keyspec, options));
×
882
        }
883

UNCOV
884
        return ret;
×
885
}
886

887
static int add_proc_cmdline_devices(void) {
×
888
        int r, ret = 0;
×
UNCOV
889
        crypto_device *d;
×
890

891
        HASHMAP_FOREACH(d, arg_disks) {
×
UNCOV
892
                _cleanup_free_ char *device = NULL;
×
893

894
                if (!d->create)
×
UNCOV
895
                        continue;
×
896

897
                if (!d->name) {
×
898
                        d->name = strjoin("luks-", d->uuid);
×
899
                        if (!d->name)
×
UNCOV
900
                                return log_oom();
×
901
                }
902

903
                device = strjoin("UUID=", d->uuid);
×
904
                if (!device)
×
UNCOV
905
                        return log_oom();
×
906

907
                r = create_disk(d->name,
×
908
                                d->datadev ?: device,
×
909
                                d->keyfile ?: arg_default_keyfile,
×
910
                                d->keydev,
×
911
                                d->headerdev,
×
UNCOV
912
                                d->options ?: arg_default_options,
×
913
                                "/proc/cmdline");
UNCOV
914
                RET_GATHER(ret, r);
×
915
        }
916

UNCOV
917
        return ret;
×
918
}
919

UNCOV
920
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(crypt_device_hash_ops, char, string_hash_func, string_compare_func,
×
921
                                              crypto_device, crypt_device_free);
922

923
static int run(const char *dest, const char *dest_early, const char *dest_late) {
×
UNCOV
924
        int r;
×
925

UNCOV
926
        assert_se(arg_dest = dest);
×
927

928
        arg_crypttab = getenv("SYSTEMD_CRYPTTAB") ?: "/etc/crypttab";
×
UNCOV
929
        arg_runtime_directory = getenv("RUNTIME_DIRECTORY") ?: "/run/systemd/cryptsetup";
×
930

931
        arg_disks = hashmap_new(&crypt_device_hash_ops);
×
932
        if (!arg_disks)
×
UNCOV
933
                return log_oom();
×
934

935
        r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, PROC_CMDLINE_STRIP_RD_PREFIX);
×
936
        if (r < 0)
×
UNCOV
937
                return log_warning_errno(r, "Failed to parse kernel command line: %m");
×
938

UNCOV
939
        if (!arg_enabled)
×
940
                return 0;
941

942
        r = add_crypttab_devices();
×
UNCOV
943
        RET_GATHER(r, add_proc_cmdline_devices());
×
944

945
        return r;
946
}
947

UNCOV
948
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