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

systemd / systemd / 23518499972

24 Mar 2026 09:45PM UTC coverage: 72.567% (-0.01%) from 72.581%
23518499972

push

github

web-flow
resolved: add "static RRs" concept (#41213)

split out of #40980

301 of 337 new or added lines in 9 files covered. (89.32%)

3451 existing lines in 67 files now uncovered.

316989 of 436822 relevant lines covered (72.57%)

1153112.24 hits per line

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

74.79
/src/bootctl/bootctl-install.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <stdlib.h>
4
#include <unistd.h>
5

6
#include "sd-device.h"
7
#include "sd-varlink.h"
8

9
#include "alloc-util.h"
10
#include "blockdev-util.h"
11
#include "boot-entry.h"
12
#include "bootctl.h"
13
#include "bootctl-install.h"
14
#include "bootctl-random-seed.h"
15
#include "bootctl-util.h"
16
#include "chase.h"
17
#include "copy.h"
18
#include "dirent-util.h"
19
#include "efi-api.h"
20
#include "efi-fundamental.h"
21
#include "efivars.h"
22
#include "env-file.h"
23
#include "fd-util.h"
24
#include "fileio.h"
25
#include "find-esp.h"
26
#include "fs-util.h"
27
#include "glyph-util.h"
28
#include "id128-util.h"
29
#include "install-file.h"
30
#include "io-util.h"
31
#include "json-util.h"
32
#include "kernel-config.h"
33
#include "log.h"
34
#include "openssl-util.h"
35
#include "parse-argument.h"
36
#include "path-util.h"
37
#include "pe-binary.h"
38
#include "rm-rf.h"
39
#include "stat-util.h"
40
#include "string-table.h"
41
#include "string-util.h"
42
#include "strv.h"
43
#include "time-util.h"
44
#include "tmpfile-util.h"
45
#include "umask-util.h"
46
#include "utf8.h"
47

48
typedef enum InstallOperation {
49
        INSTALL_NEW,
50
        INSTALL_UPDATE,
51
        INSTALL_REMOVE,
52
        INSTALL_TEST,
53
        _INSTALL_OPERATION_MAX,
54
        _INSTALL_OPERATION_INVALID = -1,
55
} InstallOperation;
56

57
typedef struct InstallContext {
58
        InstallOperation operation;
59
        bool graceful;
60
        char *root;
61
        int root_fd;
62
        sd_id128_t machine_id;
63
        char *install_layout;
64
        BootEntryTokenType entry_token_type;
65
        char *entry_token;
66
        int make_entry_directory; /* tri-state */
67
        InstallSource install_source;
68
        char *esp_path;
69
        int esp_fd;
70
        uint32_t esp_part;
71
        uint64_t esp_pstart;
72
        uint64_t esp_psize;
73
        sd_id128_t esp_uuid;
74
        char *xbootldr_path;
75
        int xbootldr_fd;
76
#if HAVE_OPENSSL
77
        X509 *secure_boot_certificate;
78
        EVP_PKEY *secure_boot_private_key;
79
#endif
80
        int touch_variables; /* tri-state */
81
} InstallContext;
82

83
#define INSTALL_CONTEXT_NULL                                            \
84
        (InstallContext) {                                              \
85
                .operation = _INSTALL_OPERATION_INVALID,                \
86
                .root_fd = -EBADF,                                      \
87
                .entry_token_type = _BOOT_ENTRY_TOKEN_TYPE_INVALID,     \
88
                .make_entry_directory = -1,                             \
89
                .install_source = _INSTALL_SOURCE_INVALID,              \
90
                .esp_part = UINT32_MAX,                                 \
91
                .esp_pstart = UINT64_MAX,                               \
92
                .esp_psize = UINT64_MAX,                                \
93
                .esp_fd = -EBADF,                                       \
94
                .xbootldr_fd = -EBADF,                                  \
95
                .touch_variables = -1,                                  \
96
        }
97

98
static const char* install_operation_table[_INSTALL_OPERATION_MAX] = {
99
        [INSTALL_NEW]    = "new",
100
        [INSTALL_UPDATE] = "update",
101
        [INSTALL_REMOVE] = "remove",
102
        [INSTALL_TEST]   = "test",
103
};
104

105
DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(install_operation, InstallOperation);
×
106

107
static void install_context_done(InstallContext *c) {
328✔
108
        assert(c);
328✔
109

110
        c->root = mfree(c->root);
328✔
111
        c->root_fd = safe_close(c->root_fd);
328✔
112
        c->install_layout = mfree(c->install_layout);
328✔
113
        c->entry_token = mfree(c->entry_token);
328✔
114
        c->esp_path = mfree(c->esp_path);
328✔
115
        c->esp_fd = safe_close(c->esp_fd);
328✔
116
        c->xbootldr_path = mfree(c->xbootldr_path);
328✔
117
        c->xbootldr_fd = safe_close(c->xbootldr_fd);
328✔
118
#if HAVE_OPENSSL
119
        if (c->secure_boot_private_key) {
328✔
120
                EVP_PKEY_free(c->secure_boot_private_key);
1✔
121
                c->secure_boot_private_key = NULL;
1✔
122
        }
123
        if (c->secure_boot_certificate) {
328✔
124
                X509_free(c->secure_boot_certificate);
1✔
125
                c->secure_boot_certificate = NULL;
1✔
126
        }
127
#endif
128
}
328✔
129

130
static int install_context_from_cmdline(
164✔
131
                InstallContext *ret,
132
                InstallOperation operation) {
133

134
        int r;
164✔
135

136
        assert(ret);
164✔
137
        assert(operation >= 0);
164✔
138
        assert(operation < _INSTALL_OPERATION_MAX);
164✔
139

140
        _cleanup_(install_context_done) InstallContext b = INSTALL_CONTEXT_NULL;
164✔
141
        b.operation = operation;
164✔
142
        b.graceful = arg_graceful() == ARG_GRACEFUL_FORCE ||
164✔
143
                (operation == INSTALL_UPDATE && arg_graceful() != ARG_GRACEFUL_NO);
124✔
144
        b.machine_id = arg_machine_id;
164✔
145
        b.entry_token_type = arg_entry_token_type;
164✔
146
        b.make_entry_directory = arg_make_entry_directory;
164✔
147
        b.install_source = arg_install_source;
164✔
148

149
        if (strdup_to(&b.entry_token, arg_entry_token) < 0 ||
164✔
150
            strdup_to(&b.install_layout, arg_install_layout) < 0)
164✔
151
                return log_oom();
×
152

153
        if (arg_root) {
164✔
154
                b.root_fd = open(arg_root, O_CLOEXEC|O_DIRECTORY|O_PATH);
28✔
155
                if (b.root_fd < 0)
28✔
156
                        return log_error_errno(errno, "Failed to open root directory '%s': %m", arg_root);
×
157

158
                r = strdup_to(&b.root, arg_root);
28✔
159
                if (r < 0)
28✔
160
                        return log_oom();
×
161
        } else
162
                b.root_fd = XAT_FDROOT;
136✔
163

164
        r = acquire_esp(/* unprivileged_mode= */ false,
328✔
165
                        b.graceful,
164✔
166
                        &b.esp_part,
167
                        &b.esp_pstart,
168
                        &b.esp_psize,
169
                        &b.esp_uuid,
170
                        /* ret_devid= */ NULL);
171
        /* If --graceful is specified and we can't find an ESP, handle this cleanly */
172
        if (r < 0 && (!b.graceful || r != -ENOKEY))
164✔
173
                return r;
174

175
        if (r >= 0) { /* An ESP has been found */
164✔
176
                assert(arg_esp_path);
164✔
177

178
                if (arg_root) {
164✔
179
                        const char *e = path_startswith(arg_esp_path, arg_root);
28✔
180
                        if (!e)
28✔
181
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "ESP path '%s' not below specified root '%s', refusing.", arg_esp_path, arg_root);
×
182

183
                        r = strdup_to(&b.esp_path, e);
28✔
184
                } else
185
                        r = strdup_to(&b.esp_path, arg_esp_path);
136✔
186
                if (r < 0)
164✔
187
                        return log_oom();
×
188
        }
189

190
        r = acquire_xbootldr(
164✔
191
                        /* unprivileged_mode= */ false,
192
                        /* ret_uuid= */ NULL,
193
                        /* ret_devid= */ NULL);
194
        if (r < 0)
164✔
195
                return r;
196
        if (r > 0) { /* XBOOTLDR has been found */
164✔
197
                assert(arg_xbootldr_path);
28✔
198

199
                if (arg_root) {
28✔
200
                        const char *e = path_startswith(arg_xbootldr_path, arg_root);
28✔
201
                        if (!e)
28✔
202
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "XBOOTLDR path '%s' not below specified root '%s', refusing.", arg_xbootldr_path, arg_root);
×
203

204
                        r = strdup_to(&b.xbootldr_path, e);
28✔
205
                } else
206
                        r = strdup_to(&b.xbootldr_path, arg_xbootldr_path);
×
207
                if (r < 0)
28✔
208
                        return log_oom();
×
209
        }
210

211
        *ret = TAKE_GENERIC(b, InstallContext, INSTALL_CONTEXT_NULL);
164✔
212

213
        return !!ret->esp_path; /* return positive if we found an ESP */
164✔
214
}
215

216
static int acquire_esp_fd(InstallContext *c) {
852✔
217
        int r;
852✔
218

219
        assert(c);
852✔
220

221
        if (c->esp_fd >= 0)
852✔
222
                return c->esp_fd;
852✔
223

224
        assert(c->esp_path);
164✔
225

226
        _cleanup_free_ char *j = path_join(c->root, c->esp_path);
328✔
227
        if (!j)
164✔
228
                return log_oom();
×
229

230
        r = chaseat(c->root_fd,
328✔
231
                    c->esp_path,
164✔
232
                    CHASE_AT_RESOLVE_IN_ROOT|CHASE_TRIGGER_AUTOFS|CHASE_MUST_BE_DIRECTORY,
233
                    /* ret_path= */ NULL,
234
                    &c->esp_fd);
235
        if (r < 0)
164✔
236
                return log_error_errno(r, "Failed to open ESP '%s': %m", j);
×
237

238
        return c->esp_fd;
164✔
239
}
240

241
static int acquire_dollar_boot_fd(InstallContext *c) {
291✔
242
        int r;
291✔
243

244
        assert(c);
291✔
245

246
        if (c->xbootldr_fd >= 0)
291✔
247
                return c->xbootldr_fd;
291✔
248

249
        if (!c->xbootldr_path)
277✔
250
                return acquire_esp_fd(c);
257✔
251

252
        _cleanup_free_ char *j = path_join(c->root, c->xbootldr_path);
40✔
253
        if (!j)
20✔
254
                return log_oom();
×
255

256
        r = chaseat(c->root_fd,
40✔
257
                    c->xbootldr_path,
20✔
258
                    CHASE_AT_RESOLVE_IN_ROOT|CHASE_TRIGGER_AUTOFS|CHASE_MUST_BE_DIRECTORY,
259
                    /* ret_path= */ NULL,
260
                    &c->xbootldr_fd);
261
        if (r < 0)
20✔
262
                return log_error_errno(r, "Failed to open XBOOTLDR '%s': %m", j);
×
263

264
        return c->xbootldr_fd;
20✔
265
}
266

267
static const char* dollar_boot_path(InstallContext *c) {
291✔
268
        assert(c);
291✔
269

270
        return c->xbootldr_path ?: c->esp_path;
291✔
271
}
272

273
static bool should_touch_install_variables(InstallContext *c) {
147✔
274
        assert(c);
147✔
275

276
        if (c->touch_variables >= 0)
147✔
277
                return c->touch_variables;
×
278

279
        if (!is_efi_boot())  /* NB: this internally checks if we run in a container */
147✔
280
                return false;
281

282
        return empty_or_root(c->root);
38✔
283
}
284

285
static int load_etc_machine_id(InstallContext *c) {
150✔
286
        int r;
150✔
287

288
        assert(c);
150✔
289

290
        r = id128_get_machine_at(c->root_fd, &c->machine_id);
150✔
291
        if (ERRNO_IS_NEG_MACHINE_ID_UNSET(r)) /* Not set or empty */
150✔
292
                return 0;
293
        if (r < 0)
130✔
294
                return log_error_errno(r, "Failed to get machine-id: %m");
×
295

296
        log_debug("Loaded machine ID %s from '%s/etc/machine-id'.", strempty(c->root), SD_ID128_TO_STRING(c->machine_id));
248✔
297
        return 0;
130✔
298
}
299

300
static int load_etc_machine_info(InstallContext *c) {
150✔
301
        /* systemd v250 added support to store the kernel-install layout setting and the machine ID to use
302
         * for setting up the ESP in /etc/machine-info. The newer /etc/kernel/entry-token file, as well as
303
         * the $layout field in /etc/kernel/install.conf are better replacements for this though, hence this
304
         * has been deprecated and is only returned for compatibility. */
305
        _cleanup_free_ char *s = NULL, *layout = NULL;
×
306
        int r;
150✔
307

308
        assert(c);
150✔
309

310
        _cleanup_free_ char *j = path_join(c->root, "/etc/machine-info");
300✔
311
        if (!j)
150✔
312
                return log_oom();
×
313

314
        _cleanup_close_ int fd =
150✔
315
                chase_and_openat(
150✔
316
                                c->root_fd,
317
                                "/etc/machine-info",
318
                                CHASE_AT_RESOLVE_IN_ROOT|CHASE_MUST_BE_REGULAR,
319
                                O_RDONLY|O_CLOEXEC,
320
                                /* ret_path= */ NULL);
321
        if (fd == -ENOENT)
150✔
322
                return 0;
323
        if (fd < 0)
×
324
                return log_error_errno(fd, "Failed to open '%s': %m", j);
×
325

326
        r = parse_env_file_fd(
×
327
                        fd, "/etc/machine-info",
328
                        "KERNEL_INSTALL_LAYOUT", &layout,
329
                        "KERNEL_INSTALL_MACHINE_ID", &s);
330
        if (r < 0)
×
331
                return log_error_errno(r, "Failed to parse '%s': %m", j);
×
332

333
        if (!isempty(s)) {
×
334
                if (!arg_quiet)
×
335
                        log_notice("Read $KERNEL_INSTALL_MACHINE_ID from '%s'. "
×
336
                                   "Please move it to '%s/etc/kernel/entry-token'.", j, strempty(c->root));
337

338
                r = sd_id128_from_string(s, &c->machine_id);
×
339
                if (r < 0)
×
340
                        return log_error_errno(r, "Failed to parse KERNEL_INSTALL_MACHINE_ID=\"%s\" in '%s': %m", s, j);
×
341

342
                log_debug("Loaded KERNEL_INSTALL_MACHINE_ID=\"%s\" from '%s'.",
×
343
                          SD_ID128_TO_STRING(c->machine_id), j);
344
        }
345

346
        if (!isempty(layout)) {
150✔
347
                if (!arg_quiet)
×
348
                        log_notice("Read $KERNEL_INSTALL_LAYOUT from '%s'. "
×
349
                                   "Please move it to the layout= setting of '%s/etc/kernel/install.conf'.", j, strempty(c->root));
350

351
                log_debug("KERNEL_INSTALL_LAYOUT=\"%s\" is specified in '%s'.", layout, j);
×
352
                free_and_replace(c->install_layout, layout);
×
353
        }
354

355
        return 0;
356
}
357

358
static int load_kernel_install_layout(InstallContext *c) {
150✔
359
        _cleanup_free_ char *layout = NULL;
150✔
360
        int r;
150✔
361

362
        assert(c);
150✔
363

364
        const char *e = secure_getenv("KERNEL_INSTALL_CONF_ROOT");
150✔
365
        r = load_kernel_install_conf_at(
150✔
366
                        e ? NULL : c->root,
367
                        e ? XAT_FDROOT : c->root_fd,
368
                        e,
369
                        /* ret_machine_id= */ NULL,
370
                        /* ret_boot_root= */ NULL,
371
                        &layout,
372
                        /* ret_initrd_generator= */ NULL,
373
                        /* ret_uki_generator= */ NULL);
374
        if (r <= 0)
150✔
375
                return r;
376

377
        if (!isempty(layout)) {
150✔
378
                log_debug("layout=\"%s\" is specified in config.", layout);
×
379
                free_and_replace(c->install_layout, layout);
×
380
        }
381

382
        return 0;
383
}
384

385
static bool use_boot_loader_spec_type1(InstallContext *c) {
150✔
386
        assert(c);
150✔
387
        /* If the layout is not specified, or if it is set explicitly to "bls" we assume Boot Loader
388
         * Specification Type #1 is the chosen format for our boot loader entries */
389
        return !c->install_layout || streq(c->install_layout, "bls");
150✔
390
}
391

392
static int settle_make_entry_directory(InstallContext *c) {
150✔
393
        int r;
150✔
394

395
        assert(c);
150✔
396

397
        r = load_etc_machine_id(c);
150✔
398
        if (r < 0)
150✔
399
                return r;
400

401
        r = load_etc_machine_info(c);
150✔
402
        if (r < 0)
150✔
403
                return r;
404

405
        r = load_kernel_install_layout(c);
150✔
406
        if (r < 0)
150✔
407
                return r;
408

409
        const char *e = secure_getenv("KERNEL_INSTALL_CONF_ROOT");
150✔
410
        r = boot_entry_token_ensure_at(
150✔
411
                        e ? XAT_FDROOT : c->root_fd,
412
                        e,
413
                        c->machine_id,
414
                        /* machine_id_is_random= */ false,
415
                        &c->entry_token_type,
416
                        &c->entry_token);
417
        if (r < 0)
150✔
418
                return r;
419

420
        log_debug("Using entry token: %s", c->entry_token);
150✔
421

422
        bool layout_type1 = use_boot_loader_spec_type1(c);
150✔
423
        if (c->make_entry_directory < 0) { /* Automatic mode */
150✔
424
                if (layout_type1) {
×
425
                        if (c->entry_token_type == BOOT_ENTRY_TOKEN_MACHINE_ID) {
×
426
                                _cleanup_free_ char *j = path_join(c->root, "/etc/machine-id");
×
427
                                if (!j)
×
428
                                        return log_oom();
×
429

430
                                _cleanup_close_ int fd = -EBADF;
×
431
                                r = chaseat(c->root_fd,
×
432
                                            "/etc/machine-id",
433
                                            CHASE_AT_RESOLVE_IN_ROOT|CHASE_MUST_BE_REGULAR,
434
                                            /* ret_path= */ NULL,
435
                                            &fd);
436
                                if (r < 0)
×
437
                                        return log_debug_errno(r, "Unable to open '%s': %m", j);
×
438

439
                                r = fd_is_temporary_fs(fd);
×
440
                                if (r < 0)
×
441
                                        return log_debug_errno(r, "Couldn't determine whether '%s' is on a temporary file system: %m", j);
×
442

443
                                c->make_entry_directory = r == 0;
×
444
                        } else
445
                                c->make_entry_directory = true;
×
446
                } else
447
                        c->make_entry_directory = false;
×
448
        }
449

450
        if (c->make_entry_directory > 0 && !layout_type1)
150✔
451
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
452
                                       "KERNEL_INSTALL_LAYOUT=\"%s\" is configured, but Boot Loader Specification Type #1 entry directory creation was requested.",
453
                                       c->install_layout);
454

455
        return 0;
456
}
457

458
static int compare_product(const char *a, const char *b) {
248✔
459
        size_t x, y;
248✔
460

461
        assert(a);
248✔
462
        assert(b);
248✔
463

464
        x = strcspn(a, " ");
248✔
465
        y = strcspn(b, " ");
248✔
466
        if (x != y)
248✔
467
                return x < y ? -1 : x > y ? 1 : 0;
×
468

469
        return strncmp(a, b, x);
248✔
470
}
471

472
static int compare_version(const char *a, const char *b) {
248✔
473
        assert(a);
248✔
474
        assert(b);
248✔
475

476
        a += strcspn(a, " ");
248✔
477
        a += strspn(a, " ");
248✔
478
        b += strcspn(b, " ");
248✔
479
        b += strspn(b, " ");
248✔
480

481
        return strverscmp_improved(a, b);
248✔
482
}
483

484
static int version_check(int fd_from, const char *from, int fd_to, const char *to) {
248✔
485
        _cleanup_free_ char *a = NULL, *b = NULL;
248✔
486
        int r;
248✔
487

488
        assert(fd_from >= 0);
248✔
489
        assert(from);
248✔
490
        assert(fd_to >= 0);
248✔
491
        assert(to);
248✔
492

493
        /* Does not reposition file offset */
494

495
        r = get_file_version(fd_from, &a);
248✔
496
        if (r == -ESRCH)
248✔
497
                return log_notice_errno(r, "Source file \"%s\" does not carry version information!", from);
×
498
        if (r < 0)
248✔
499
                return r;
500

501
        r = get_file_version(fd_to, &b);
248✔
502
        if (r == -ESRCH)
248✔
503
                return log_info_errno(r, "Skipping \"%s\", it's owned by another boot loader (no version info found).", to);
×
504
        if (r < 0)
248✔
505
                return r;
506
        if (compare_product(a, b) != 0)
248✔
507
                return log_info_errno(SYNTHETIC_ERRNO(ESRCH),
×
508
                                      "Skipping \"%s\", it's owned by another boot loader.", to);
509

510
        r = compare_version(a, b);
248✔
511
        log_debug("Comparing versions: \"%s\" %s \"%s\"", a, comparison_operator(r), b);
484✔
512
        if (r < 0)
248✔
513
                return log_warning_errno(SYNTHETIC_ERRNO(ESTALE),
×
514
                                         "Skipping \"%s\", newer boot loader version in place already.", to);
515
        if (r == 0)
248✔
516
                return log_info_errno(SYNTHETIC_ERRNO(ESTALE),
248✔
517
                                      "Skipping \"%s\", same boot loader version in place already.", to);
518

519
        return 0;
520
}
521

522
static int copy_file_with_version_check(
286✔
523
                const char *source_path,
524
                int source_fd,
525
                const char *dest_path,
526
                int dest_parent_fd,
527
                const char *dest_filename,
528
                int dest_fd,
529
                bool force) {
530

531
        int r;
286✔
532

533
        assert(source_path);
286✔
534
        assert(source_fd >= 0);
286✔
535
        assert(dest_path);
286✔
536
        assert(dest_parent_fd >= 0);
286✔
537
        assert(dest_filename);
286✔
538

539
        if (!force && dest_fd >= 0) {
286✔
540
                r = version_check(source_fd, source_path, dest_fd, dest_path);
248✔
541
                if (r < 0)
248✔
542
                        return r;
286✔
543
        }
544

545
        _cleanup_free_ char *t = NULL;
38✔
546
        _cleanup_close_ int write_fd = -EBADF;
38✔
547
        write_fd = open_tmpfile_linkable_at(dest_parent_fd, dest_filename, O_WRONLY|O_CLOEXEC, &t);
38✔
548
        if (write_fd < 0)
38✔
549
                return log_error_errno(write_fd, "Failed to open \"%s\" for writing: %m", dest_path);
×
550

551
        CLEANUP_TMPFILE_AT(dest_parent_fd, t);
38✔
552

553
        /* Reset file offset before we start copying, since we copy this file multiple times, and the offset
554
         * might be left at the end of the file. (Resetting before rather than after a copy attempt is safer
555
         * because a previous attempt might have failed half-way, leaving the file offset at some undefined
556
         * place.) */
557
        r = copy_bytes(source_fd, write_fd, UINT64_MAX, COPY_REFLINK|COPY_SEEK0_SOURCE);
38✔
558
        if (r < 0)
38✔
UNCOV
559
                return log_error_errno(r, "Failed to copy data from \"%s\" to \"%s\": %m", source_path, dest_path);
×
560

561
        (void) copy_times(source_fd, write_fd, /* flags= */ 0);
38✔
562
        (void) fchmod(write_fd, 0644);
38✔
563

564
        r = link_tmpfile_at(write_fd, dest_parent_fd, t, dest_filename, LINK_TMPFILE_REPLACE|LINK_TMPFILE_SYNC);
38✔
565
        if (r < 0)
38✔
UNCOV
566
                return log_error_errno(r, "Failed to move data from \"%s\" to \"%s\": %m", source_path, dest_path);
×
567

568
        t = mfree(t); /* disarm CLEANUP_TMPFILE_AT() */
38✔
569

570
        log_info("Copied \"%s\" to \"%s\".", source_path, dest_path);
38✔
571
        return 0;
572
}
573

574
static int mkdir_one(const char *root, int root_fd, const char *path) {
124✔
575
        int r;
124✔
576

577
        assert(root);
124✔
578
        assert(root_fd >= 0);
124✔
579
        assert(path);
124✔
580

581
        _cleanup_free_ char *p = path_join(empty_to_root(root), path);
248✔
582
        if (!p)
124✔
UNCOV
583
                return log_oom();
×
584

585
        r = chaseat(root_fd,
124✔
586
                    path,
587
                    CHASE_AT_RESOLVE_IN_ROOT|CHASE_PROHIBIT_SYMLINKS|CHASE_MKDIR_0755|CHASE_MUST_BE_DIRECTORY,
588
                    /* ret_path= */ NULL,
589
                    /* ret_fd= */ NULL);
590
        if (r < 0)
124✔
UNCOV
591
                return log_error_errno(r, "Failed to create \"%s\": %m", p);
×
592

593
        log_info("Created directory \"%s\".", p);
124✔
594
        return 0;
595
}
596

597
static const char *const esp_subdirs[] = {
598
        /* The directories to place in the ESP */
599
        "EFI",
600
        "EFI/systemd",
601
        "EFI/BOOT",
602
        "loader",
603
        "loader/keys",
604
        NULL
605
};
606

607
static const char *const dollar_boot_subdirs[] = {
608
        /* The directories to place in the XBOOTLDR partition or the ESP, depending what exists */
609
        "loader",
610
        "loader/entries",  /* Type #1 entries */
611
        "EFI",
612
        "EFI/Linux",       /* Type #2 entries */
613
        NULL
614
};
615

616
static int create_subdirs(const char *root, int root_fd, const char * const *subdirs) {
26✔
617
        int r;
26✔
618

619
        assert(root);
26✔
620
        assert(root_fd >= 0);
26✔
621

622
        STRV_FOREACH(i, subdirs) {
143✔
623
                r = mkdir_one(root, root_fd, *i);
117✔
624
                if (r < 0)
117✔
625
                        return r;
626
        }
627

628
        return 0;
629
}
630

631
static int update_efi_boot_binaries(
124✔
632
                InstallContext *c,
633
                const char *source_path,
634
                int source_fd,
635
                const char *ignore_filename) {
636

637
        int r, ret = 0;
124✔
638

639
        assert(c);
124✔
640
        assert(source_path);
124✔
641

642
        int esp_fd = acquire_esp_fd(c);
124✔
643
        if (esp_fd < 0)
124✔
644
                return esp_fd;
124✔
645

646
        _cleanup_free_ char *j = path_join(c->root, c->esp_path);
248✔
647
        if (!j)
124✔
UNCOV
648
                return log_oom();
×
649

650
        _cleanup_closedir_ DIR *d = NULL;
124✔
651
        r = chase_and_opendirat(
124✔
652
                        esp_fd,
653
                        "/EFI/BOOT",
654
                        CHASE_AT_RESOLVE_IN_ROOT|CHASE_PROHIBIT_SYMLINKS|CHASE_MUST_BE_DIRECTORY,
655
                        /* ret_path= */ NULL,
656
                        &d);
657
        if (r == -ENOENT)
124✔
658
                return 0;
659
        if (r < 0)
124✔
UNCOV
660
                return log_error_errno(r, "Failed to open directory \"%s/EFI/BOOT\": %m", j);
×
661

662
        FOREACH_DIRENT(de, d, break) {
620✔
663
                _cleanup_close_ int fd = -EBADF;
620✔
664

665
                if (!endswith_no_case(de->d_name, ".efi"))
248✔
UNCOV
666
                        continue;
×
667

668
                if (strcaseeq_ptr(ignore_filename, de->d_name))
248✔
669
                        continue;
124✔
670

671
                fd = xopenat_full(dirfd(d), de->d_name, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY|O_NOFOLLOW, XO_REGULAR, /* mode= */ MODE_INVALID);
124✔
672
                if (fd < 0)
124✔
UNCOV
673
                        return log_error_errno(fd, "Failed to open \"%s/%s\" for reading: %m", j, de->d_name);
×
674

675
                r = pe_is_native_fd(fd);
124✔
676
                if (r < 0) {
124✔
UNCOV
677
                        log_warning_errno(r, "Failed to detect if \"%s/%s\" is for native architecture, ignoring: %m", j, de->d_name);
×
UNCOV
678
                        continue;
×
679
                }
680
                if (r == 0)
124✔
681
                        continue;
124✔
682

UNCOV
683
                _cleanup_free_ char *dest_path = path_join(j, "/EFI/BOOT", de->d_name);
×
UNCOV
684
                if (!dest_path)
×
UNCOV
685
                        return log_oom();
×
686

687
                r = copy_file_with_version_check(source_path, source_fd, dest_path, dirfd(d), de->d_name, fd, /* force= */ false);
×
688
                if (IN_SET(r, -ESTALE, -ESRCH))
×
UNCOV
689
                        continue;
×
690
                RET_GATHER(ret, r);
×
691
        }
692

693
        return ret;
694
}
695

696
static int copy_one_file(
143✔
697
                InstallContext *c,
698
                const char *name,
699
                bool force) {
700

701
        int r, ret = 0;
143✔
702

703
        assert(c);
143✔
704

705
        _cleanup_free_ char *dest_name = strdup(name);
143✔
706
        if (!dest_name)
143✔
UNCOV
707
                return log_oom();
×
708
        char *s = endswith_no_case(dest_name, ".signed");
143✔
709
        if (s)
143✔
710
                *s = 0;
143✔
711

712
        _cleanup_free_ char *sp = path_join(BOOTLIBDIR, name);
286✔
713
        if (!sp)
143✔
UNCOV
714
                return log_oom();
×
715

716
        _cleanup_free_ char *source_path = NULL;
143✔
717
        _cleanup_close_ int source_fd = -EBADF;
143✔
718
        if (IN_SET(c->install_source, INSTALL_SOURCE_AUTO, INSTALL_SOURCE_IMAGE)) {
143✔
719
                source_fd = chase_and_openat(
143✔
720
                                c->root_fd,
721
                                sp,
722
                                CHASE_AT_RESOLVE_IN_ROOT|CHASE_MUST_BE_REGULAR,
723
                                O_RDONLY|O_CLOEXEC,
724
                                &source_path);
725
                if (source_fd < 0 && (source_fd != -ENOENT || c->install_source != INSTALL_SOURCE_AUTO))
143✔
UNCOV
726
                        return log_error_errno(source_fd, "Failed to resolve path '%s' under directory '%s': %m", sp, c->root);
×
727

728
                /* If we had a root directory to try, we didn't find it and we are in auto mode, retry on the host */
729
        }
730
        if (source_fd < 0) {
16✔
731
                source_fd = chase_and_open(
16✔
732
                                sp,
733
                                /* root= */ NULL,
734
                                CHASE_MUST_BE_REGULAR,
735
                                O_RDONLY|O_CLOEXEC,
736
                                &source_path);
737
                if (source_fd < 0)
16✔
UNCOV
738
                        return log_error_errno(source_fd, "Failed to resolve path '%s': %m", sp);
×
739
        }
740

741
        int esp_fd = acquire_esp_fd(c);
143✔
742
        if (esp_fd < 0)
143✔
743
                return esp_fd;
744

745
        _cleanup_free_ char *j = path_join(c->root, c->esp_path);
286✔
746
        if (!j)
143✔
UNCOV
747
                return log_oom();
×
748

749
        _cleanup_close_ int dest_parent_fd = -EBADF;
143✔
750
        r = chaseat(esp_fd,
143✔
751
                    "/EFI/systemd",
752
                    CHASE_AT_RESOLVE_IN_ROOT|CHASE_PROHIBIT_SYMLINKS|CHASE_MKDIR_0755|CHASE_MUST_BE_DIRECTORY,
753
                    /* ret_path= */ NULL,
754
                    &dest_parent_fd);
755
        if (r < 0)
143✔
UNCOV
756
                return log_error_errno(r, "Failed to resolve path '/EFI/systemd' under directory '%s': %m", j);
×
757

758
        _cleanup_free_ char *dest_path = path_join(j, "/EFI/systemd", dest_name);
286✔
759
        if (!dest_path)
143✔
UNCOV
760
                return log_oom();
×
761

762
        _cleanup_close_ int dest_fd = xopenat_full(dest_parent_fd, dest_name, O_RDONLY|O_CLOEXEC, XO_REGULAR, MODE_INVALID);
286✔
763
        if (dest_fd < 0 && dest_fd != -ENOENT)
143✔
UNCOV
764
                return log_error_errno(dest_fd, "Failed to open '%s' under '%s/EFI/systemd' directory: %m", dest_name, j);
×
765

766
        /* Note that if this fails we do the second copy anyway, but return this error code,
767
         * so we stash it away in a separate variable. */
768
        ret = copy_file_with_version_check(source_path, source_fd, dest_path, dest_parent_fd, dest_name, dest_fd, force);
143✔
769

770
        const char *e = startswith(dest_name, "systemd-boot");
143✔
771
        if (e) {
143✔
772

773
                /* Create the EFI default boot loader name (specified for removable devices) */
774
                _cleanup_free_ char *boot_dot_efi = strjoin("BOOT", e);
286✔
775
                if (!boot_dot_efi)
143✔
UNCOV
776
                        return log_oom();
×
777

778
                ascii_strupper(boot_dot_efi);
143✔
779

780
                _cleanup_close_ int default_dest_parent_fd = -EBADF;
143✔
781
                r = chaseat(esp_fd,
143✔
782
                            "/EFI/BOOT",
783
                            CHASE_AT_RESOLVE_IN_ROOT|CHASE_PROHIBIT_SYMLINKS|CHASE_MKDIR_0755|CHASE_MUST_BE_DIRECTORY,
784
                            /* ret_path= */ NULL,
785
                            &default_dest_parent_fd);
786
                if (r < 0)
143✔
UNCOV
787
                        return log_error_errno(r, "Failed to resolve path '/EFI/BOOT/' under directory '%s': %m", j);
×
788

789
                _cleanup_free_ char *default_dest_path = path_join(j, "/EFI/BOOT", boot_dot_efi);
286✔
790
                if (!default_dest_path)
143✔
UNCOV
791
                        return log_oom();
×
792

793
                _cleanup_close_ int default_dest_fd = xopenat_full(default_dest_parent_fd, boot_dot_efi, O_RDONLY|O_CLOEXEC, XO_REGULAR, MODE_INVALID);
286✔
794
                if (default_dest_fd < 0 && default_dest_fd != -ENOENT)
143✔
UNCOV
795
                        return log_error_errno(default_dest_fd, "Failed to open '%s' under '%s/EFI/BOOT' directory: %m", boot_dot_efi, j);
×
796

797
                RET_GATHER(ret, copy_file_with_version_check(source_path, source_fd, default_dest_path, default_dest_parent_fd, boot_dot_efi, default_dest_fd, force));
143✔
798

799
                /* If we were installed under any other name in /EFI/BOOT/, make sure we update those
800
                 * binaries as well. */
801
                if (!force)
143✔
802
                        RET_GATHER(ret, update_efi_boot_binaries(c, source_path, source_fd, boot_dot_efi));
124✔
803
        }
804

805
        return ret;
806
}
807

808
static int install_binaries(
137✔
809
                InstallContext *c,
810
                const char *arch) {
811

812
        int r;
137✔
813

814
        assert(c);
137✔
815

816
        _cleanup_free_ char *source_path = NULL;
137✔
817
        _cleanup_closedir_ DIR *d = NULL;
137✔
818
        if (IN_SET(c->install_source, INSTALL_SOURCE_AUTO, INSTALL_SOURCE_IMAGE)) {
137✔
819
                r = chase_and_opendirat(
137✔
820
                                c->root_fd,
821
                                BOOTLIBDIR,
822
                                CHASE_AT_RESOLVE_IN_ROOT|CHASE_MUST_BE_DIRECTORY,
823
                                &source_path,
824
                                &d);
825
                if (r < 0 && (r != -ENOENT || c->install_source != INSTALL_SOURCE_AUTO))
137✔
UNCOV
826
                        return log_error_errno(r, "Failed to resolve path '%s' under directory '%s': %m", BOOTLIBDIR, c->root);
×
827

828
                /* If we had a root directory to try, we didn't find it and we are in auto mode, retry on the host */
829
        }
830
        if (!d) {
137✔
831
                r = chase_and_opendir(
12✔
832
                                BOOTLIBDIR,
833
                                /* root= */ NULL,
834
                                CHASE_MUST_BE_DIRECTORY,
835
                                &source_path,
836
                                &d);
837
                if (r == -ENOENT && c->graceful) {
12✔
UNCOV
838
                        log_debug("Source directory '%s' does not exist, ignoring.", BOOTLIBDIR);
×
UNCOV
839
                        return 0;
×
840
                }
841
                if (r < 0)
12✔
842
                        return log_error_errno(r, "Failed to resolve path '%s': %m", BOOTLIBDIR);
×
843
        }
844

845
        const char *suffix = strjoina(arch, ".efi");
685✔
846
        const char *suffix_signed = strjoina(arch, ".efi.signed");
685✔
847

848
        FOREACH_DIRENT(de, d, return log_error_errno(errno, "Failed to read \"%s\": %m", source_path)) {
1,507✔
849
                int k;
1,096✔
850

851
                if (endswith_no_case(de->d_name, suffix)) {
1,096✔
852
                        /* skip the .efi file, if there's a .signed version of it */
853
                        _cleanup_free_ const char *s = strjoin(de->d_name, ".signed");
286✔
854
                        if (!s)
143✔
UNCOV
855
                                return log_oom();
×
856
                        if (faccessat(dirfd(d), s, F_OK, 0) >= 0)
143✔
857
                                continue;
143✔
858
                } else if (!endswith_no_case(de->d_name, suffix_signed))
953✔
859
                        continue;
810✔
860

861
                k = copy_one_file(c, de->d_name, c->operation == INSTALL_NEW);
143✔
862
                /* Don't propagate an error code if no update necessary, installed version already equal or
863
                 * newer version, or other boot loader in place. */
864
                if (c->graceful && IN_SET(k, -ESTALE, -ESRCH))
143✔
865
                        continue;
121✔
866
                RET_GATHER(r, k);
22✔
867
        }
868

869
        return r;
870
}
871

872
static int install_loader_config(InstallContext *c) {
13✔
873
        int r;
13✔
874

875
        assert(c);
13✔
876
        assert(c->make_entry_directory >= 0);
13✔
877

878
        int esp_fd = acquire_esp_fd(c);
13✔
879
        if (esp_fd < 0)
13✔
880
                return esp_fd;
13✔
881

882
        _cleanup_free_ char *j = path_join(c->root, c->esp_path);
26✔
883
        if (!j)
13✔
UNCOV
884
                return log_oom();
×
885

886
        _cleanup_close_ int loader_dir_fd = -EBADF;
13✔
887
        r = chaseat(esp_fd,
13✔
888
                    "loader",
889
                    CHASE_AT_RESOLVE_IN_ROOT|CHASE_PROHIBIT_SYMLINKS|CHASE_MKDIR_0755|CHASE_MUST_BE_DIRECTORY,
890
                    /* ret_path= */ NULL,
891
                    &loader_dir_fd);
892
        if (r < 0)
13✔
UNCOV
893
                return log_error_errno(r, "Failed to open '/loader/' directory below '%s': %m", j);
×
894

895
        if (faccessat(loader_dir_fd, "loader.conf", F_OK, AT_SYMLINK_NOFOLLOW) < 0) {
13✔
896
                if (errno != ENOENT)
13✔
UNCOV
897
                        return log_error_errno(errno, "Failed to check if '/loader/loader.conf' exists below '%s': %m", j);
×
898
        } else /* Silently skip creation if the file already exists (early check) */
899
                return 0;
900

901
        _cleanup_free_ char *t = NULL;
13✔
902
        _cleanup_fclose_ FILE *f = NULL;
13✔
903
        r = fopen_tmpfile_linkable_at(loader_dir_fd, "loader.conf", O_WRONLY|O_CLOEXEC, &t, &f);
13✔
904
        if (r < 0)
13✔
UNCOV
905
                return log_error_errno(r, "Failed to open '%s/loader/loader.conf' for writing: %m", j);
×
906

907
        CLEANUP_TMPFILE_AT(loader_dir_fd, t);
13✔
908

909
        fprintf(f, "#timeout 3\n"
13✔
910
                   "#console-mode keep\n");
911

912
        if (c->make_entry_directory) {
13✔
913
                assert(c->entry_token);
7✔
914
                fprintf(f, "default %s-*\n", c->entry_token);
7✔
915
        }
916

917
        r = flink_tmpfile_at(f, loader_dir_fd, t, "loader.conf", LINK_TMPFILE_SYNC);
13✔
918
        if (r == -EEXIST)
13✔
919
                return 0; /* Silently skip creation if the file exists now (recheck) */
920
        if (r < 0)
13✔
UNCOV
921
                return log_error_errno(r, "Failed to move '%s/loader/loader.conf' into place: %m", j);
×
922

923
        t = mfree(t); /* disarm CLEANUP_TMPFILE_AT() */
13✔
924
        return 1;
13✔
925
}
926

927
static int install_loader_specification(InstallContext *c) {
134✔
928
        int r;
134✔
929

930
        assert(c);
134✔
931

932
        int dollar_boot_fd = acquire_dollar_boot_fd(c);
134✔
933
        if (dollar_boot_fd < 0)
134✔
934
                return dollar_boot_fd;
134✔
935

936
        _cleanup_free_ char *j = path_join(c->root, dollar_boot_path(c));
268✔
937
        if (!j)
134✔
UNCOV
938
                return log_oom();
×
939

940
        _cleanup_close_ int loader_dir_fd = -EBADF;
134✔
941
        r = chaseat(dollar_boot_fd,
134✔
942
                    "loader",
943
                    CHASE_AT_RESOLVE_IN_ROOT|CHASE_PROHIBIT_SYMLINKS|CHASE_MKDIR_0755|CHASE_MUST_BE_DIRECTORY,
944
                    /* ret_path= */ NULL,
945
                    &loader_dir_fd);
946
        if (r < 0)
134✔
UNCOV
947
                return log_error_errno(r, "Failed to pin '/loader' directory below '%s': %m", j);
×
948

949
        if (faccessat(loader_dir_fd, "entries.srel", F_OK, AT_SYMLINK_NOFOLLOW) < 0) {
134✔
950
                if (errno != ENOENT)
13✔
UNCOV
951
                        return log_error_errno(errno, "Failed to check if '/loader/entries.srel' exists below '%s': %m", j);
×
952
        } else /* Silently skip creation if the file already exists (early check) */
953
                return 0;
954

955
        _cleanup_free_ char *t = NULL;
13✔
956
        _cleanup_fclose_ FILE *f = NULL;
13✔
957
        r = fopen_tmpfile_linkable_at(loader_dir_fd, "entries.srel", O_WRONLY|O_CLOEXEC, &t, &f);
13✔
958
        if (r < 0)
13✔
UNCOV
959
                return log_error_errno(r, "Failed to open '%s/loader/entries.srel' for writing: %m", j);
×
960

961
        CLEANUP_TMPFILE_AT(loader_dir_fd, t);
13✔
962

963
        fprintf(f, "type1\n");
13✔
964

965
        r = flink_tmpfile_at(f, loader_dir_fd, t, "entries.srel", LINK_TMPFILE_SYNC);
13✔
966
        if (r == -EEXIST)
13✔
967
                return 0; /* Silently skip creation if the file exists now (recheck) */
968
        if (r < 0)
13✔
UNCOV
969
                return log_error_errno(r, "Failed to move '%s/loader/entries.srel' into place: %m", j);
×
970

971
        t = mfree(t); /* disarm CLEANUP_TMPFILE_AT() */
13✔
972
        return 1;
13✔
973
}
974

975
static int install_entry_directory(InstallContext *c) {
13✔
976
        assert(c);
13✔
977
        assert(c->make_entry_directory >= 0);
13✔
978

979
        if (!c->make_entry_directory)
13✔
980
                return 0;
13✔
981

982
        assert(c->entry_token);
7✔
983

984
        int dollar_boot_fd = acquire_dollar_boot_fd(c);
7✔
985
        if (dollar_boot_fd < 0)
7✔
986
                return dollar_boot_fd;
987

988
        _cleanup_free_ char *j = path_join(c->root, dollar_boot_path(c));
14✔
989
        if (!j)
7✔
UNCOV
990
                return log_oom();
×
991

992
        return mkdir_one(j, dollar_boot_fd, c->entry_token);
7✔
993
}
994

995
static int install_entry_token(InstallContext *c) {
13✔
996
        int r;
13✔
997

998
        assert(c);
13✔
999
        assert(c->make_entry_directory >= 0);
13✔
1000
        assert(c->entry_token);
13✔
1001

1002
        /* Let's save the used entry token in /etc/kernel/entry-token if we used it to create the entry
1003
         * directory, or if anything else but the machine ID */
1004

1005
        if (!c->make_entry_directory && c->entry_token_type == BOOT_ENTRY_TOKEN_MACHINE_ID)
13✔
1006
                return 0;
13✔
1007

1008
        const char *confdir = secure_getenv("KERNEL_INSTALL_CONF_ROOT") ?: "/etc/kernel/";
13✔
1009

1010
        _cleanup_free_ char *j = path_join(c->root, confdir);
26✔
1011
        if (!j)
13✔
UNCOV
1012
                return log_oom();
×
1013

1014
        _cleanup_close_ int dfd = -EBADF;
13✔
1015
        r = chaseat(c->root_fd,
13✔
1016
                    confdir,
1017
                    CHASE_AT_RESOLVE_IN_ROOT|CHASE_MKDIR_0755|CHASE_MUST_BE_DIRECTORY,
1018
                    /* ret_path= */ NULL,
1019
                    &dfd);
1020
        if (r < 0)
13✔
UNCOV
1021
                return log_error_errno(r, "Failed to open '%s': %m", j);
×
1022

1023
        r = write_string_file_at(dfd, "entry-token", c->entry_token, WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC|WRITE_STRING_FILE_MKDIR_0755);
13✔
1024
        if (r < 0)
13✔
UNCOV
1025
                return log_error_errno(r, "Failed to write entry token '%s' to '%s/entry-token': %m", c->entry_token, j);
×
1026

1027
        return 0;
1028
}
1029

1030
#if HAVE_OPENSSL
1031
static int efi_timestamp(EFI_TIME *ret) {
1✔
1032
        struct tm tm = {};
1✔
1033
        int r;
1✔
1034

1035
        assert(ret);
1✔
1036

1037
        r = localtime_or_gmtime_usec(source_date_epoch_or_now(), /* utc= */ true, &tm);
1✔
1038
        if (r < 0)
1✔
UNCOV
1039
                return log_error_errno(r, "Failed to convert timestamp to calendar time: %m");
×
1040

1041
        *ret = (EFI_TIME) {
1✔
1042
                .Year = 1900 + tm.tm_year,
1✔
1043
                /* tm_mon starts at 0, EFI_TIME months start at 1. */
1044
                .Month = tm.tm_mon + 1,
1✔
1045
                .Day = tm.tm_mday,
1✔
1046
                .Hour = tm.tm_hour,
1✔
1047
                .Minute = tm.tm_min,
1✔
1048
                .Second = tm.tm_sec,
1✔
1049
        };
1050

1051
        return 0;
1✔
1052
}
1053
#endif
1054

1055
static int install_secure_boot_auto_enroll(InstallContext *c) {
13✔
1056
#if HAVE_OPENSSL
1057
        int r;
13✔
1058
#endif
1059

1060
        if (!arg_secure_boot_auto_enroll)
13✔
1061
                return 0;
13✔
1062

1063
#if HAVE_OPENSSL
1064
        if (!c->secure_boot_certificate || !c->secure_boot_private_key)
1✔
1065
                return 0;
1066

1067
        _cleanup_free_ uint8_t *dercert = NULL;
1✔
1068
        int dercertsz;
1✔
1069
        dercertsz = i2d_X509(c->secure_boot_certificate, &dercert);
1✔
1070
        if (dercertsz < 0)
1✔
UNCOV
1071
                return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to convert X.509 certificate to DER: %s",
×
1072
                                       ERR_error_string(ERR_get_error(), NULL));
1073

1074
        int esp_fd = acquire_esp_fd(c);
1✔
1075
        if (esp_fd < 0)
1✔
1076
                return esp_fd;
1077

1078
        _cleanup_free_ char *j = path_join(c->root, c->esp_path);
2✔
1079
        if (!j)
1✔
UNCOV
1080
                return log_oom();
×
1081

1082
        _cleanup_close_ int keys_fd = -EBADF;
1✔
1083
        r = chaseat(esp_fd,
1✔
1084
                    "loader/keys/auto",
1085
                    CHASE_AT_RESOLVE_IN_ROOT|CHASE_PROHIBIT_SYMLINKS|CHASE_MKDIR_0755|CHASE_MUST_BE_DIRECTORY,
1086
                    /* ret_path= */ NULL,
1087
                    &keys_fd);
1088
        if (r < 0)
1✔
UNCOV
1089
                return log_error_errno(r, "Failed to chase /loader/keys/auto/ below '%s': %m", j);
×
1090

1091
        uint32_t siglistsz = offsetof(EFI_SIGNATURE_LIST, Signatures) + offsetof(EFI_SIGNATURE_DATA, SignatureData) + dercertsz;
1✔
1092
        /* We use malloc0() to zero-initialize the SignatureOwner field of Signatures[0]. */
1093
        _cleanup_free_ EFI_SIGNATURE_LIST *siglist = malloc0(siglistsz);
2✔
1094
        if (!siglist)
1✔
UNCOV
1095
                return log_oom();
×
1096

1097
        *siglist = (EFI_SIGNATURE_LIST) {
1✔
1098
                .SignatureType = EFI_CERT_X509_GUID,
1099
                .SignatureListSize = siglistsz,
1100
                .SignatureSize = offsetof(EFI_SIGNATURE_DATA, SignatureData) + dercertsz,
1✔
1101
        };
1102

1103
        memcpy(siglist->Signatures[0].SignatureData, dercert, dercertsz);
1✔
1104

1105
        EFI_TIME timestamp;
1✔
1106
        r = efi_timestamp(&timestamp);
1✔
1107
        if (r < 0)
1✔
1108
                return r;
1109

1110
        uint32_t attrs =
1✔
1111
                EFI_VARIABLE_NON_VOLATILE|
1112
                EFI_VARIABLE_BOOTSERVICE_ACCESS|
1113
                EFI_VARIABLE_RUNTIME_ACCESS|
1114
                EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS;
1115

1116
        FOREACH_STRING(db, "PK", "KEK", "db") {
4✔
1117
                _cleanup_(BIO_freep) BIO *bio = NULL;
3✔
1118

1119
                bio = BIO_new(BIO_s_mem());
3✔
1120
                if (!bio)
3✔
UNCOV
1121
                        return log_oom();
×
1122

1123
                _cleanup_free_ char16_t *db16 = utf8_to_utf16(db, SIZE_MAX);
6✔
1124
                if (!db16)
3✔
UNCOV
1125
                        return log_oom();
×
1126

1127
                /* Don't count the trailing NUL terminator. */
1128
                if (BIO_write(bio, db16, char16_strsize(db16) - sizeof(char16_t)) < 0)
3✔
UNCOV
1129
                        return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to write variable name to bio");
×
1130

1131
                EFI_GUID *guid = STR_IN_SET(db, "PK", "KEK") ? &(EFI_GUID) EFI_GLOBAL_VARIABLE : &(EFI_GUID) EFI_IMAGE_SECURITY_DATABASE_GUID;
3✔
1132

1133
                if (BIO_write(bio, guid, sizeof(*guid)) < 0)
3✔
UNCOV
1134
                        return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to write variable GUID to bio");
×
1135

1136
                if (BIO_write(bio, &attrs, sizeof(attrs)) < 0)
3✔
1137
                        return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to write variable attributes to bio");
×
1138

1139
                if (BIO_write(bio, &timestamp, sizeof(timestamp)) < 0)
3✔
1140
                        return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to write timestamp to bio");
×
1141

1142
                if (BIO_write(bio, siglist, siglistsz) < 0)
3✔
1143
                        return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to write signature list to bio");
×
1144

1145
                _cleanup_(PKCS7_freep) PKCS7 *p7 = NULL;
3✔
1146
                p7 = PKCS7_sign(c->secure_boot_certificate, c->secure_boot_private_key, /* certs= */ NULL, bio, PKCS7_DETACHED|PKCS7_NOATTR|PKCS7_BINARY|PKCS7_NOSMIMECAP);
3✔
1147
                if (!p7)
3✔
UNCOV
1148
                        return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to calculate PKCS7 signature: %s",
×
1149
                                               ERR_error_string(ERR_get_error(), NULL));
1150

1151
                _cleanup_free_ uint8_t *sig = NULL;
×
1152
                int sigsz = i2d_PKCS7(p7, &sig);
3✔
1153
                if (sigsz < 0)
3✔
1154
                        return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to convert PKCS7 signature to DER: %s",
×
1155
                                               ERR_error_string(ERR_get_error(), NULL));
1156

1157
                size_t authsz = offsetof(EFI_VARIABLE_AUTHENTICATION_2, AuthInfo.CertData) + sigsz;
3✔
UNCOV
1158
                _cleanup_free_ EFI_VARIABLE_AUTHENTICATION_2 *auth = malloc(authsz);
×
1159
                if (!auth)
3✔
UNCOV
1160
                        return log_oom();
×
1161

1162
                *auth = (EFI_VARIABLE_AUTHENTICATION_2) {
3✔
1163
                        .TimeStamp = timestamp,
1164
                        .AuthInfo = {
1165
                                .Hdr = {
1166
                                        .dwLength = offsetof(WIN_CERTIFICATE_UEFI_GUID, CertData) + sigsz,
3✔
1167
                                        .wRevision = 0x0200,
1168
                                        .wCertificateType = 0x0EF1, /* WIN_CERT_TYPE_EFI_GUID */
1169
                                },
1170
                                .CertType = EFI_CERT_TYPE_PKCS7_GUID,
1171
                        }
1172
                };
1173

1174
                memcpy(auth->AuthInfo.CertData, sig, sigsz);
3✔
1175

1176
                _cleanup_free_ char *filename = strjoin(db, ".auth");
3✔
1177
                if (!filename)
3✔
UNCOV
1178
                        return log_oom();
×
1179

1180
                _cleanup_free_ char *t = NULL;
3✔
1181
                _cleanup_close_ int fd = open_tmpfile_linkable_at(keys_fd, filename, O_WRONLY|O_CLOEXEC, &t);
6✔
1182
                if (fd < 0)
3✔
UNCOV
1183
                        return log_error_errno(fd, "Failed to open secure boot auto-enrollment file for writing: %m");
×
1184

UNCOV
1185
                CLEANUP_TMPFILE_AT(keys_fd, t);
×
1186

1187
                r = loop_write(fd, auth, authsz);
3✔
1188
                if (r < 0)
3✔
UNCOV
1189
                        return log_error_errno(r, "Failed to write authentication descriptor to secure boot auto-enrollment file: %m");
×
1190

1191
                r = loop_write(fd, siglist, siglistsz);
3✔
1192
                if (r < 0)
3✔
UNCOV
1193
                        return log_error_errno(r, "Failed to write signature list to secure boot auto-enrollment file: %m");
×
1194

1195
                r = link_tmpfile_at(fd, keys_fd, t, filename, LINK_TMPFILE_SYNC);
3✔
1196
                if (r < 0)
3✔
UNCOV
1197
                        return log_error_errno(errno, "Failed to link secure boot auto-enrollment file: %m");
×
1198

1199
                t = mfree(t); /* Disarm CLEANUP_TMPFILE_AT() */
3✔
1200

1201
                log_info("Secure boot auto-enrollment file '%s/loader/keys/auto/%s' successfully written.", j, filename);
3✔
1202
        }
1203

1204
        return 0;
1✔
1205
#else
1206
        return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Built without OpenSSL support, cannot set up auto-enrollment.");
1207
#endif
1208
}
1209

1210
static bool same_entry(uint16_t id, sd_id128_t uuid, const char *path) {
58✔
1211
        _cleanup_free_ char *opath = NULL;
58✔
1212
        sd_id128_t ouuid;
58✔
1213
        int r;
58✔
1214

1215
        r = efi_get_boot_option(id, NULL, &ouuid, &opath, NULL);
58✔
1216
        if (r < 0)
58✔
1217
                return false;
1218
        if (!sd_id128_equal(uuid, ouuid))
58✔
1219
                return false;
42✔
1220

1221
        /* Some motherboards convert the path to uppercase under certain circumstances
1222
         * (e.g. after booting into the Boot Menu in the ASUS ROG STRIX B350-F GAMING),
1223
         * so use case-insensitive checking */
1224
        if (!strcaseeq_ptr(path, opath))
16✔
1225
                return false;
9✔
1226

1227
        return true;
1228
}
1229

1230
static int find_slot(sd_id128_t uuid, const char *path, uint16_t *id) {
16✔
1231
        _cleanup_free_ uint16_t *options = NULL;
16✔
1232

1233
        int n = efi_get_boot_options(&options);
16✔
1234
        if (n < 0)
16✔
1235
                return n;
1236

1237
        /* find already existing systemd-boot entry */
1238
        for (int i = 0; i < n; i++)
67✔
1239
                if (same_entry(options[i], uuid, path)) {
58✔
1240
                        *id = options[i];
7✔
1241
                        return 1;
7✔
1242
                }
1243

1244
        /* find free slot in the sorted BootXXXX variable list */
1245
        for (int i = 0; i < n; i++)
39✔
1246
                if (i != options[i]) {
30✔
UNCOV
1247
                        *id = i;
×
UNCOV
1248
                        return 0;
×
1249
                }
1250

1251
        /* use the next one */
1252
        if (n == 0xffff)
9✔
1253
                return -ENOSPC;
1254
        *id = n;
9✔
1255
        return 0;
9✔
1256
}
1257

1258
static int insert_into_order(InstallContext *c, uint16_t slot) {
13✔
1259
        _cleanup_free_ uint16_t *order = NULL;
13✔
1260
        uint16_t *t;
13✔
1261
        int n;
13✔
1262

1263
        assert(c);
13✔
1264

1265
        n = efi_get_boot_order(&order);
13✔
1266
        if (n <= 0)
13✔
1267
                /* no entry, add us */
UNCOV
1268
                return efi_set_boot_order(&slot, 1);
×
1269

1270
        /* are we the first and only one? */
1271
        if (n == 1 && order[0] == slot)
13✔
1272
                return 0;
1273

1274
        /* are we already in the boot order? */
1275
        for (int i = 0; i < n; i++) {
31✔
1276
                if (order[i] != slot)
25✔
1277
                        continue;
18✔
1278

1279
                /* we do not require to be the first one, all is fine */
1280
                if (c->operation != INSTALL_NEW)
7✔
1281
                        return 0;
1282

1283
                /* move us to the first slot */
1284
                memmove(order + 1, order, i * sizeof(uint16_t));
3✔
1285
                order[0] = slot;
3✔
1286
                return efi_set_boot_order(order, n);
3✔
1287
        }
1288

1289
        /* extend array */
1290
        t = reallocarray(order, n + 1, sizeof(uint16_t));
6✔
1291
        if (!t)
6✔
1292
                return -ENOMEM;
1293
        order = t;
6✔
1294

1295
        /* add us to the top or end of the list */
1296
        if (c->operation != INSTALL_NEW) {
6✔
1297
                memmove(order + 1, order, n * sizeof(uint16_t));
6✔
1298
                order[0] = slot;
6✔
1299
        } else
UNCOV
1300
                order[n] = slot;
×
1301

1302
        return efi_set_boot_order(order, n + 1);
6✔
1303
}
1304

UNCOV
1305
static int remove_from_order(uint16_t slot) {
×
UNCOV
1306
        _cleanup_free_ uint16_t *order = NULL;
×
UNCOV
1307
        int n;
×
1308

1309
        n = efi_get_boot_order(&order);
×
1310
        if (n <= 0)
×
1311
                return n;
1312

1313
        for (int i = 0; i < n; i++) {
×
UNCOV
1314
                if (order[i] != slot)
×
UNCOV
1315
                        continue;
×
1316

1317
                if (i + 1 < n)
×
1318
                        memmove(order + i, order + i+1, (n - i) * sizeof(uint16_t));
×
UNCOV
1319
                return efi_set_boot_order(order, n - 1);
×
1320
        }
1321

1322
        return 0;
1323
}
1324

1325
static int pick_efi_boot_option_description(int esp_fd, char **ret) {
9✔
1326
        int r;
9✔
1327

1328
        assert(esp_fd >= 0);
9✔
1329
        assert(ret);
9✔
1330

1331
        /* early declarations, so that they are definitely initialized even if we follow any of the gotos */
1332
        _cleanup_(sd_device_unrefp) sd_device *d = NULL;
9✔
1333
        _cleanup_free_ char *j = NULL;
9✔
1334

1335
        const char *b = arg_efi_boot_option_description ?: "Linux Boot Manager";
9✔
1336
        if (!arg_efi_boot_option_description_with_device)
9✔
1337
                goto fallback;
9✔
1338

UNCOV
1339
        r = block_device_new_from_fd(
×
1340
                        esp_fd,
1341
                        BLOCK_DEVICE_LOOKUP_WHOLE_DISK|BLOCK_DEVICE_LOOKUP_BACKING,
1342
                        &d);
UNCOV
1343
        if (r < 0) {
×
UNCOV
1344
                log_debug_errno(r, "Failed to find backing device of ESP: %m");
×
UNCOV
1345
                goto fallback;
×
1346
        }
1347

1348
        const char *serial;
×
UNCOV
1349
        r = sd_device_get_property_value(d, "ID_SERIAL", &serial);
×
UNCOV
1350
        if (r < 0) {
×
1351
                log_debug_errno(r, "Unable to read ID_SERIAL field of backing device of ESP: %m");
×
1352
                goto fallback;
×
1353
        }
1354

1355
        j = strjoin(b, " (", serial, ")");
×
UNCOV
1356
        if (!j)
×
UNCOV
1357
                return log_oom();
×
1358

1359
        if (strlen(j) > EFI_BOOT_OPTION_DESCRIPTION_MAX) {
×
1360
                log_debug("Boot option string suffixed with device serial would be too long, skipping: %s", j);
×
UNCOV
1361
                j = mfree(j);
×
1362
                goto fallback;
×
1363
        }
1364

1365
        *ret = TAKE_PTR(j);
×
UNCOV
1366
        return 0;
×
1367

1368
fallback:
9✔
1369
        j = strdup(b);
9✔
1370
        if (!j)
9✔
UNCOV
1371
                return log_oom();
×
1372

1373
        *ret = TAKE_PTR(j);
9✔
1374
        return 0;
9✔
1375
}
1376

1377
static int install_variables(
13✔
1378
                InstallContext *c,
1379
                const char *path) {
1380

1381
        uint16_t slot;
13✔
1382
        int r;
13✔
1383

1384
        assert(c);
13✔
1385

1386
        int esp_fd = acquire_esp_fd(c);
13✔
1387
        if (esp_fd < 0)
13✔
1388
                return esp_fd;
13✔
1389

1390
        _cleanup_free_ char *j = path_join(c->root, c->esp_path);
26✔
1391
        if (!j)
13✔
UNCOV
1392
                return log_oom();
×
1393

1394
        r = chase_and_accessat(
13✔
1395
                        esp_fd,
1396
                        path,
1397
                        CHASE_AT_RESOLVE_IN_ROOT|CHASE_PROHIBIT_SYMLINKS|CHASE_MUST_BE_REGULAR,
1398
                        F_OK,
1399
                        /* ret_path= */ NULL);
1400
        if (r == -ENOENT)
13✔
1401
                return 0;
1402
        if (r < 0)
13✔
UNCOV
1403
                return log_error_errno(r, "Cannot access \"%s/%s\": %m", j, skip_leading_slash(path));
×
1404

1405
        r = find_slot(c->esp_uuid, path, &slot);
13✔
1406
        if (r < 0) {
13✔
UNCOV
1407
                int level = c->graceful ? arg_quiet ? LOG_DEBUG : LOG_INFO : LOG_ERR;
×
UNCOV
1408
                const char *skip = c->graceful ? ", skipping" : "";
×
1409

1410
                log_full_errno(level, r,
×
1411
                               r == -ENOENT ?
1412
                               "Failed to access EFI variables%s. Is the \"efivarfs\" filesystem mounted?" :
1413
                               "Failed to determine current boot order%s: %m", skip);
1414

UNCOV
1415
                return c->graceful ? 0 : r;
×
1416
        }
1417

1418
        bool existing = r > 0;
13✔
1419

1420
        if (c->operation == INSTALL_NEW || !existing) {
13✔
1421
                _cleanup_free_ char *description = NULL;
9✔
1422

1423
                r = pick_efi_boot_option_description(esp_fd, &description);
9✔
1424
                if (r < 0)
9✔
1425
                        return r;
1426

1427
                r = efi_add_boot_option(
9✔
1428
                                slot,
1429
                                description,
1430
                                c->esp_part,
1431
                                c->esp_pstart,
1432
                                c->esp_psize,
1433
                                c->esp_uuid,
1434
                                path);
1435
                if (r < 0) {
9✔
UNCOV
1436
                        int level = c->graceful ? arg_quiet ? LOG_DEBUG : LOG_INFO : LOG_ERR;
×
UNCOV
1437
                        const char *skip = c->graceful ? ", skipping" : "";
×
1438

1439
                        log_full_errno(level, r, "Failed to create EFI Boot variable entry%s: %m", skip);
×
1440

UNCOV
1441
                        return c->graceful ? 0 : r;
×
1442
                }
1443

1444
                log_info("%s EFI boot entry \"%s\".",
15✔
1445
                         existing ? "Updated" : "Created",
1446
                         description);
1447
        }
1448

1449
        return insert_into_order(c, slot);
13✔
1450
}
1451

1452
static int are_we_installed(InstallContext *c) {
138✔
1453
        int r;
138✔
1454

1455
        assert(c);
138✔
1456

1457
        /* Tests whether systemd-boot is installed. It's not obvious what to use as check here: we could
1458
         * check EFI variables, we could check what binary /EFI/BOOT/BOOT*.EFI points to, or whether the
1459
         * loader entries directory exists. Here we opted to check whether /EFI/systemd/ is non-empty, which
1460
         * should be a suitable and very minimal check for a number of reasons:
1461
         *
1462
         *  → The check is architecture independent (i.e. we check if any systemd-boot loader is installed,
1463
         *    not a specific one.)
1464
         *
1465
         *  → It doesn't assume we are the only boot loader (i.e doesn't check if we own the main
1466
         *    /EFI/BOOT/BOOT*.EFI fallback binary.
1467
         *
1468
         *  → It specifically checks for systemd-boot, not for other boot loaders (which a check for
1469
         *    /boot/loader/entries would do). */
1470

1471
        _cleanup_free_ char *p = path_join(c->esp_path, "/EFI/systemd");
276✔
1472
        if (!p)
138✔
UNCOV
1473
                return log_oom();
×
1474

1475
        int esp_fd = acquire_esp_fd(c);
138✔
1476
        if (esp_fd < 0)
138✔
1477
                return esp_fd;
1478

1479
        _cleanup_close_ int fd = chase_and_openat(
276✔
1480
                        esp_fd,
1481
                        "/EFI/systemd",
1482
                        CHASE_AT_RESOLVE_IN_ROOT|CHASE_PROHIBIT_SYMLINKS|CHASE_MUST_BE_DIRECTORY,
1483
                        O_RDONLY|O_CLOEXEC|O_DIRECTORY,
1484
                        /* ret_path= */ NULL);
1485
        if (fd == -ENOENT)
138✔
1486
                return 0;
1487
        if (fd < 0)
131✔
UNCOV
1488
                return log_error_errno(fd, "Failed to open '%s': %m", p);
×
1489

1490
        log_debug("Checking whether '%s' contains any files%s", p, glyph(GLYPH_ELLIPSIS));
249✔
1491
        r = dir_is_empty_at(fd, /* path= */ NULL, /* ignore_hidden_or_backup= */ false);
131✔
1492
        if (r < 0 && r != -ENOENT)
131✔
UNCOV
1493
                return log_error_errno(r, "Failed to check whether '%s' contains any files: %m", p);
×
1494

1495
        return r == 0;
131✔
1496
}
1497

1498
#if HAVE_OPENSSL
1499
static int load_secure_boot_auto_enroll(
137✔
1500
                X509 **ret_certificate,
1501
                EVP_PKEY **ret_private_key,
1502
                OpenSSLAskPasswordUI **ret_ui) {
1503

1504
        int r;
137✔
1505

1506
        assert(ret_certificate);
137✔
1507
        assert(ret_private_key);
137✔
1508
        assert(ret_ui);
137✔
1509

1510
        if (!arg_secure_boot_auto_enroll) {
137✔
1511
                *ret_certificate = NULL;
136✔
1512
                *ret_private_key = NULL;
136✔
1513
                return 0;
136✔
1514
        }
1515

1516
        if (arg_certificate_source_type == OPENSSL_CERTIFICATE_SOURCE_FILE) {
1✔
1517
                r = parse_path_argument(arg_certificate, /* suppress_root= */ false, &arg_certificate);
1✔
1518
                if (r < 0)
1✔
1519
                        return r;
1520
        }
1521

1522
        _cleanup_(X509_freep) X509 *certificate = NULL;
137✔
1523
        r = openssl_load_x509_certificate(
1✔
1524
                        arg_certificate_source_type,
1525
                        arg_certificate_source,
1526
                        arg_certificate,
1527
                        &certificate);
1528
        if (r < 0)
1✔
UNCOV
1529
                return log_error_errno(r, "Failed to load X.509 certificate from %s: %m", arg_certificate);
×
1530

1531
        if (arg_private_key_source_type == OPENSSL_KEY_SOURCE_FILE) {
1✔
1532
                r = parse_path_argument(arg_private_key, /* suppress_root= */ false, &arg_private_key);
1✔
1533
                if (r < 0)
1✔
UNCOV
1534
                        return log_error_errno(r, "Failed to parse private key path %s: %m", arg_private_key);
×
1535
        }
1536

1537
        r = openssl_load_private_key(
2✔
1538
                        arg_private_key_source_type,
1539
                        arg_private_key_source,
1540
                        arg_private_key,
1541
                        &(AskPasswordRequest) {
1✔
1542
                                .tty_fd = -EBADF,
1543
                                .id = "bootctl-private-key-pin",
1544
                                .keyring = arg_private_key,
1545
                                .credential = "bootctl.private-key-pin",
1546
                                .until = USEC_INFINITY,
1547
                                .hup_fd = -EBADF,
1548
                        },
1549
                        ret_private_key,
1550
                        ret_ui);
1551
        if (r < 0)
1✔
UNCOV
1552
                return log_error_errno(r, "Failed to load private key from %s: %m", arg_private_key);
×
1553

1554
        *ret_certificate = TAKE_PTR(certificate);
1✔
1555

1556
        return 0;
1✔
1557
}
1558
#endif
1559

1560
static int run_install(InstallContext *c) {
137✔
1561
        int r;
137✔
1562

1563
        assert(c);
137✔
1564
        assert(c->operation >= 0);
137✔
1565

1566
        if (c->operation == INSTALL_UPDATE) {
137✔
1567
                /* If we are updating, don't do anything if sd-boot wasn't actually installed. */
1568
                r = are_we_installed(c);
124✔
1569
                if (r < 0)
124✔
1570
                        return r;
137✔
1571
                if (r == 0) {
124✔
UNCOV
1572
                        log_debug("Skipping update because sd-boot is not installed in the ESP.");
×
UNCOV
1573
                        return 0;
×
1574
                }
1575
        }
1576

1577
        r = settle_make_entry_directory(c);
137✔
1578
        if (r < 0)
137✔
1579
                return r;
1580

1581
        const char *arch = arg_arch_all ? "" : get_efi_arch();
137✔
1582

1583
        int esp_fd = acquire_esp_fd(c);
137✔
1584
        if (esp_fd < 0)
137✔
1585
                return esp_fd;
1586

1587
        _cleanup_free_ char *j = path_join(c->root, c->esp_path);
274✔
1588
        if (!j)
137✔
UNCOV
1589
                return log_oom();
×
1590

1591
        int dollar_boot_fd = acquire_dollar_boot_fd(c);
137✔
1592
        if (dollar_boot_fd < 0)
137✔
1593
                return dollar_boot_fd;
1594

1595
        _cleanup_free_ char *w = path_join(c->root, dollar_boot_path(c));
274✔
1596
        if (!w)
137✔
UNCOV
1597
                return log_oom();
×
1598

1599
        WITH_UMASK(0002) {
274✔
1600
                if (c->operation == INSTALL_NEW) {
137✔
1601
                        /* Don't create any of these directories when we are just updating. When we update
1602
                         * we'll drop-in our files (unless there are newer ones already), but we won't create
1603
                         * the directories for them in the first place. */
1604

1605
                        r = create_subdirs(j, esp_fd, esp_subdirs);
13✔
1606
                        if (r < 0)
13✔
1607
                                return r;
1608

1609
                        r = create_subdirs(w, dollar_boot_fd, dollar_boot_subdirs);
13✔
1610
                        if (r < 0)
13✔
1611
                                return r;
1612
                }
1613

1614
                r = install_binaries(c, arch);
137✔
1615
                if (r < 0)
137✔
1616
                        return r;
1617

1618
                if (c->operation == INSTALL_NEW) {
134✔
1619
                        r = install_loader_config(c);
13✔
1620
                        if (r < 0)
13✔
1621
                                return r;
1622

1623
                        r = install_entry_directory(c);
13✔
1624
                        if (r < 0)
13✔
1625
                                return r;
1626

1627
                        r = install_entry_token(c);
13✔
1628
                        if (r < 0)
13✔
1629
                                return r;
1630

1631
                        if (arg_install_random_seed && !c->root) {
13✔
1632
                                r = install_random_seed(c->esp_path);
5✔
1633
                                if (r < 0)
5✔
1634
                                        return r;
1635
                        }
1636

1637
                        r = install_secure_boot_auto_enroll(c);
13✔
1638
                        if (r < 0)
13✔
1639
                                return r;
1640
                }
1641

1642
                r = install_loader_specification(c);
134✔
1643
                if (r < 0)
134✔
1644
                        return r;
1645
        }
1646

1647
        (void) sync_everything();
134✔
1648

1649
        if (!should_touch_install_variables(c))
134✔
1650
                return 0;
1651

1652
        if (arg_arch_all) {
15✔
1653
                log_info("Not changing EFI variables with --all-architectures.");
2✔
1654
                return 0;
2✔
1655
        }
1656

1657
        char *path = strjoina("/EFI/systemd/systemd-boot", arch, ".efi");
91✔
1658
        return install_variables(c, path);
13✔
1659
}
1660

1661
int verb_install(int argc, char *argv[], uintptr_t _data, void *userdata) {
137✔
1662
        int r;
137✔
1663

1664
        /* Invoked for both "update" and "install" */
1665

UNCOV
1666
        _cleanup_(install_context_done) InstallContext c = INSTALL_CONTEXT_NULL;
×
1667
        r = install_context_from_cmdline(&c, streq(argv[0], "install") ? INSTALL_NEW : INSTALL_UPDATE);
137✔
1668
        if (r < 0)
137✔
1669
                return r;
1670
        if (r == 0) {
137✔
UNCOV
1671
                log_debug("No ESP found and operating in graceful mode, skipping.");
×
UNCOV
1672
                return 0;
×
1673
        }
1674

1675
#if HAVE_OPENSSL
1676
        _cleanup_(openssl_ask_password_ui_freep) OpenSSLAskPasswordUI *ui = NULL;
137✔
1677
        r = load_secure_boot_auto_enroll(&c.secure_boot_certificate, &c.secure_boot_private_key, &ui);
137✔
1678
        if (r < 0)
137✔
1679
                return r;
1680
#endif
1681

1682
        return run_install(&c);
137✔
1683
}
1684

1685
static int remove_boot_efi(InstallContext *c) {
13✔
1686
        int r, n = 0;
13✔
1687

1688
        assert(c);
13✔
1689

1690
        int esp_fd = acquire_esp_fd(c);
13✔
1691
        if (esp_fd < 0)
13✔
1692
                return esp_fd;
13✔
1693

1694
        _cleanup_free_ char *w = path_join(c->root, c->esp_path);
26✔
1695
        if (!w)
13✔
UNCOV
1696
                return log_oom();
×
1697

1698
        _cleanup_closedir_ DIR *d = NULL;
13✔
1699
        _cleanup_free_ char *p = NULL;
13✔
1700
        r = chase_and_opendirat(
13✔
1701
                        esp_fd,
1702
                        "/EFI/BOOT",
1703
                        CHASE_AT_RESOLVE_IN_ROOT|CHASE_PROHIBIT_SYMLINKS|CHASE_MUST_BE_DIRECTORY,
1704
                        &p,
1705
                        &d);
1706
        if (r == -ENOENT)
13✔
1707
                return 0;
1708
        if (r < 0)
13✔
UNCOV
1709
                return log_error_errno(r, "Failed to open directory \"%s/EFI/BOOT\": %m", w);
×
1710

1711
        _cleanup_free_ char *j = path_join(w, p);
26✔
1712
        if (!j)
13✔
UNCOV
1713
                return log_oom();
×
1714

1715
        FOREACH_DIRENT(de, d, break) {
61✔
1716
                _cleanup_close_ int fd = -EBADF;
22✔
1717

1718
                if (!endswith_no_case(de->d_name, ".efi"))
22✔
UNCOV
1719
                        continue;
×
1720

1721
                _cleanup_free_ char *z = path_join(j, de->d_name);
31✔
1722
                if (!z)
22✔
UNCOV
1723
                        return log_oom();
×
1724

1725
                fd = xopenat_full(dirfd(d), de->d_name, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY|O_NOFOLLOW, XO_REGULAR, /* mode= */ MODE_INVALID);
22✔
1726
                if (fd < 0)
22✔
UNCOV
1727
                        return log_error_errno(fd, "Failed to open '%s' for reading: %m", z);
×
1728

1729
                r = pe_is_native_fd(fd);
22✔
1730
                if (r < 0) {
22✔
UNCOV
1731
                        log_warning_errno(r, "Failed to detect if '%s' is native architecture, ignoring: %m", z);
×
UNCOV
1732
                        continue;
×
1733
                }
1734
                if (r == 0)
22✔
1735
                        continue;
9✔
1736

1737
                _cleanup_free_ char *v = NULL;
13✔
1738
                r = get_file_version(fd, &v);
13✔
1739
                if (r == -ESRCH)
13✔
UNCOV
1740
                        continue;  /* No version information */
×
1741
                if (r < 0)
13✔
1742
                        return r;
1743
                if (!startswith(v, "systemd-boot "))
13✔
UNCOV
1744
                        continue;
×
1745

1746
                if (unlinkat(dirfd(d), de->d_name, 0) < 0)
13✔
1747
                        return log_error_errno(errno, "Failed to remove '%s': %m", z);
×
1748

1749
                log_info("Removed '%s'.", z);
13✔
1750

1751
                n++;
13✔
1752
        }
1753

1754
        log_debug("Removed %i EFI binaries from '%s'.", n, j);
13✔
1755
        return n;
1756
}
1757

1758
static int unlink_inode(const char *root, int root_fd, const char *path, mode_t type) {
271✔
1759
        int r;
271✔
1760

1761
        assert(root);
271✔
1762
        assert(root_fd >= 0);
271✔
1763
        assert(path);
271✔
1764
        assert(IN_SET(type, S_IFREG, S_IFDIR));
271✔
1765

1766
        _cleanup_free_ char *p = path_join(empty_to_root(root), path);
542✔
1767
        if (!p)
271✔
UNCOV
1768
                return log_oom();
×
1769

1770
        r = chase_and_unlinkat(
271✔
1771
                        root_fd,
1772
                        path,
1773
                        CHASE_AT_RESOLVE_IN_ROOT|CHASE_PROHIBIT_SYMLINKS,
1774
                        S_ISDIR(type) ? AT_REMOVEDIR : 0,
271✔
1775
                        /* ret_path= */ NULL);
1776
        if (r < 0) {
271✔
1777
                bool ignore = IN_SET(r, -ENOENT, -ENOTEMPTY);
152✔
1778
                log_full_errno(ignore ? LOG_DEBUG : LOG_ERR, r, "Failed to remove '%s': %m", p);
152✔
1779
                return ignore ? 0 : r;
152✔
1780
        }
1781

1782
        log_info("Removed %s\"%s\".", S_ISDIR(type) ? "directory " : "", p);
155✔
1783
        return 0;
1784
}
1785

1786
static int remove_subdirs(const char *root, int root_fd, const char *const *subdirs) {
34✔
1787
        int r = 0;
34✔
1788

1789
        assert(root);
34✔
1790
        assert(root_fd);
34✔
1791

1792
        STRV_FOREACH_BACKWARDS(i, (char**) subdirs)
332✔
1793
                RET_GATHER(r, unlink_inode(root, root_fd, *i, S_IFDIR));
149✔
1794

1795
        return r;
34✔
1796
}
1797

1798
static int remove_entry_directory(InstallContext *c, const char *path, int fd) {
21✔
1799
        assert(c);
21✔
1800
        assert(c->make_entry_directory >= 0);
21✔
1801
        assert(path);
21✔
1802
        assert(fd >= 0);
21✔
1803

1804
        if (!c->make_entry_directory || !c->entry_token)
21✔
1805
                return 0;
1806

1807
        return unlink_inode(path, fd, c->entry_token, S_IFDIR);
10✔
1808
}
1809

1810
static int remove_binaries(InstallContext *c) {
13✔
1811
        int r;
13✔
1812

1813
        _cleanup_free_ char *p = path_join(c->root, "/EFI/systemd");
26✔
1814
        if (!p)
13✔
UNCOV
1815
                return log_oom();
×
1816

1817
        _cleanup_close_ int efi_fd = -EBADF;
13✔
1818
        r = chaseat(c->esp_fd,
13✔
1819
                    "EFI",
1820
                    CHASE_AT_RESOLVE_IN_ROOT|CHASE_PROHIBIT_SYMLINKS|CHASE_MUST_BE_DIRECTORY,
1821
                    /* ret_path= */ NULL,
1822
                    &efi_fd);
1823
        if (r < 0) {
13✔
UNCOV
1824
                if (r != -ENOENT)
×
UNCOV
1825
                        return log_error_errno(r, "Failed to remove '%s': %m", p);
×
1826

1827
                r = 0;
1828
        } else
1829
                r = rm_rf_at(efi_fd, "systemd", REMOVE_ROOT|REMOVE_PHYSICAL|REMOVE_MISSING_OK);
13✔
1830

1831
        return RET_GATHER(r, remove_boot_efi(c));
13✔
1832
}
1833

1834
static int remove_variables(sd_id128_t uuid, const char *path, bool in_order) {
3✔
1835
        uint16_t slot;
3✔
1836
        int r;
3✔
1837

1838
        r = find_slot(uuid, path, &slot);
3✔
1839
        if (r != 1)
3✔
1840
                return 0;
3✔
1841

UNCOV
1842
        r = efi_remove_boot_option(slot);
×
UNCOV
1843
        if (r < 0)
×
1844
                return r;
1845

1846
        if (in_order)
×
UNCOV
1847
                return remove_from_order(slot);
×
1848

1849
        return 0;
1850
}
1851

1852
static int remove_loader_variables(void) {
3✔
1853
        int r = 0;
3✔
1854

1855
        /* Remove all persistent loader variables we define */
1856

1857
        FOREACH_STRING(var,
30✔
1858
                       EFI_LOADER_VARIABLE_STR("LoaderConfigConsoleMode"),
1859
                       EFI_LOADER_VARIABLE_STR("LoaderConfigTimeout"),
1860
                       EFI_LOADER_VARIABLE_STR("LoaderConfigTimeoutOneShot"),
1861
                       EFI_LOADER_VARIABLE_STR("LoaderEntryPreferred"),
1862
                       EFI_LOADER_VARIABLE_STR("LoaderEntryDefault"),
1863
                       EFI_LOADER_VARIABLE_STR("LoaderEntrySysFail"),
1864
                       EFI_LOADER_VARIABLE_STR("LoaderEntryLastBooted"),
1865
                       EFI_LOADER_VARIABLE_STR("LoaderEntryOneShot"),
1866
                       EFI_LOADER_VARIABLE_STR("LoaderSystemToken")) {
1867

1868
                int q;
27✔
1869

1870
                q = efi_set_variable(var, NULL, 0);
27✔
1871
                if (q == -ENOENT)
27✔
1872
                        continue;
24✔
1873
                if (q < 0)
3✔
UNCOV
1874
                        RET_GATHER(r, log_warning_errno(q, "Failed to remove EFI variable %s: %m", var));
×
1875
                else
1876
                        log_info("Removed EFI variable %s.", var);
3✔
1877
        }
1878

1879
        return r;
3✔
1880
}
1881

1882
int verb_remove(int argc, char *argv[], uintptr_t _data, void *userdata) {
13✔
1883
        sd_id128_t uuid = SD_ID128_NULL;
13✔
1884
        int r;
13✔
1885

1886
        _cleanup_(install_context_done) InstallContext c = INSTALL_CONTEXT_NULL;
13✔
1887
        r = install_context_from_cmdline(&c, INSTALL_REMOVE);
13✔
1888
        if (r < 0)
13✔
1889
                return r;
1890
        if (r == 0) {
13✔
UNCOV
1891
                log_debug("No ESP found and operating in graceful mode, skipping.");
×
UNCOV
1892
                return 0;
×
1893
        }
1894

1895
        r = settle_make_entry_directory(&c);
13✔
1896
        if (r < 0)
13✔
1897
                return r;
1898

1899
        int esp_fd = acquire_esp_fd(&c);
13✔
1900
        if (esp_fd < 0)
13✔
1901
                return esp_fd;
1902

1903
        _cleanup_free_ char *j = path_join(c.root, c.esp_path);
26✔
1904
        if (!j)
13✔
UNCOV
1905
                return log_oom();
×
1906

1907
        int dollar_boot_fd = acquire_dollar_boot_fd(&c); /* this will initialize .xbootldr_fd */
13✔
1908
        if (dollar_boot_fd < 0)
13✔
1909
                return dollar_boot_fd;
1910

1911
        _cleanup_free_ char *w = path_join(c.root, dollar_boot_path(&c));
26✔
1912
        if (!w)
13✔
UNCOV
1913
                return log_oom();
×
1914

1915
        r = remove_binaries(&c);
13✔
1916
        RET_GATHER(r, unlink_inode(j, esp_fd, "/loader/loader.conf", S_IFREG));
13✔
1917
        RET_GATHER(r, unlink_inode(j, esp_fd, "/loader/random-seed", S_IFREG));
13✔
1918
        RET_GATHER(r, unlink_inode(j, esp_fd, "/loader/entries.srel", S_IFREG));
13✔
1919

1920
        FOREACH_STRING(db, "PK.auth", "KEK.auth", "db.auth") {
52✔
1921
                _cleanup_free_ char *p = path_join("/loader/keys/auto", db);
78✔
1922
                if (!p)
39✔
UNCOV
1923
                        return log_oom();
×
1924

1925
                RET_GATHER(r, unlink_inode(j, esp_fd, p, S_IFREG));
39✔
1926
        }
1927
        RET_GATHER(r, unlink_inode(j, esp_fd, "/loader/keys/auto", S_IFDIR));
13✔
1928
        RET_GATHER(r, unlink_inode(j, esp_fd, "/loader/entries.srel", S_IFREG));
13✔
1929

1930
        RET_GATHER(r, remove_subdirs(j, esp_fd, esp_subdirs));
13✔
1931
        RET_GATHER(r, remove_subdirs(j, esp_fd, dollar_boot_subdirs));
13✔
1932
        RET_GATHER(r, remove_entry_directory(&c, j, esp_fd));
13✔
1933

1934
        if (c.xbootldr_fd >= 0) {
13✔
1935
                /* Remove a subset of these also from the XBOOTLDR partition if it exists */
1936
                RET_GATHER(r, unlink_inode(w, c.xbootldr_fd, "/loader/entries.srel", S_IFREG));
8✔
1937
                RET_GATHER(r, remove_subdirs(w, c.xbootldr_fd, dollar_boot_subdirs));
8✔
1938
                RET_GATHER(r, remove_entry_directory(&c, w, c.xbootldr_fd));
8✔
1939
        }
1940

1941
        (void) sync_everything();
13✔
1942

1943
        if (!should_touch_install_variables(&c))
13✔
1944
                return r;
1945

1946
        if (arg_arch_all) {
5✔
1947
                log_info("Not changing EFI variables with --all-architectures.");
2✔
1948
                return r;
2✔
1949
        }
1950

1951
        char *path = strjoina("/EFI/systemd/systemd-boot", get_efi_arch(), ".efi");
21✔
1952
        RET_GATHER(r, remove_variables(uuid, path, /* in_order= */ true));
3✔
1953
        return RET_GATHER(r, remove_loader_variables());
3✔
1954
}
1955

1956
int verb_is_installed(int argc, char *argv[], uintptr_t _data, void *userdata) {
14✔
1957
        int r;
14✔
1958

1959
        _cleanup_(install_context_done) InstallContext c = INSTALL_CONTEXT_NULL;
14✔
1960
        r = install_context_from_cmdline(&c, INSTALL_TEST);
14✔
1961
        if (r < 0)
14✔
1962
                return r;
1963
        if (r == 0) {
14✔
UNCOV
1964
                log_debug("No ESP found and operating in graceful mode, claiming not installed.");
×
UNCOV
1965
                if (!arg_quiet)
×
UNCOV
1966
                        puts("no");
×
1967
                return EXIT_FAILURE;
×
1968
        }
1969

1970
        r = are_we_installed(&c);
14✔
1971
        if (r < 0)
14✔
1972
                return r;
1973

1974
        if (r > 0) {
14✔
1975
                if (!arg_quiet)
7✔
1976
                        puts("yes");
7✔
1977
                return EXIT_SUCCESS;
7✔
1978
        } else {
1979
                if (!arg_quiet)
7✔
1980
                        puts("no");
7✔
1981
                return EXIT_FAILURE;
7✔
1982
        }
1983
}
1984

UNCOV
1985
static JSON_DISPATCH_ENUM_DEFINE(json_dispatch_install_operation, InstallOperation, install_operation_from_string);
×
UNCOV
1986
static JSON_DISPATCH_ENUM_DEFINE(json_dispatch_boot_entry_token_type, BootEntryTokenType, boot_entry_token_type_from_string);
×
1987

1988
typedef struct InstallParameters {
1989
        InstallContext context;
1990
        unsigned root_fd_index;
1991
} InstallParameters;
1992

UNCOV
1993
static void install_parameters_done(InstallParameters *p) {
×
UNCOV
1994
        assert(p);
×
1995

1996
        install_context_done(&p->context);
×
1997
}
×
1998

1999
int vl_method_install(
×
2000
                sd_varlink *link,
2001
                sd_json_variant *parameters,
2002
                sd_varlink_method_flags_t flags,
2003
                void *userdata) {
2004

UNCOV
2005
        int r;
×
2006

UNCOV
2007
        assert(link);
×
2008

UNCOV
2009
        _cleanup_(install_parameters_done) InstallParameters p = {
×
2010
                .context = INSTALL_CONTEXT_NULL,
2011
                .root_fd_index = UINT_MAX,
2012
        };
2013

UNCOV
2014
        static const sd_json_dispatch_field dispatch_table[] = {
×
2015
                { "operation",          SD_JSON_VARIANT_STRING,        json_dispatch_install_operation,     voffsetof(p, context.operation),        SD_JSON_MANDATORY },
2016
                { "graceful",           SD_JSON_VARIANT_BOOLEAN,       sd_json_dispatch_stdbool,            voffsetof(p, context.graceful),         0                 },
2017
                { "rootFileDescriptor", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint,               voffsetof(p, root_fd_index),            0                 },
2018
                { "rootDirectory",      SD_JSON_VARIANT_STRING,        json_dispatch_path,                  voffsetof(p, context.root),             0                 },
2019
                { "bootEntryTokenType", SD_JSON_VARIANT_STRING,        json_dispatch_boot_entry_token_type, voffsetof(p, context.entry_token_type), 0                 },
2020
                { "touchVariables",     SD_JSON_VARIANT_BOOLEAN,       sd_json_dispatch_tristate,           voffsetof(p, context.touch_variables),  0                 },
2021
                {},
2022
        };
2023

UNCOV
2024
        r = sd_varlink_dispatch(link, parameters, dispatch_table, &p);
×
UNCOV
2025
        if (r != 0)
×
2026
                return r;
2027

2028
        if (!IN_SET(p.context.operation, INSTALL_NEW, INSTALL_UPDATE))
×
UNCOV
2029
                return sd_varlink_error_invalid_parameter_name(link, "operation");
×
2030

2031
        if (p.root_fd_index != UINT_MAX) {
×
2032
                p.context.root_fd = sd_varlink_peek_dup_fd(link, p.root_fd_index);
×
UNCOV
2033
                if (p.context.root_fd < 0)
×
2034
                        return log_debug_errno(p.context.root_fd, "Failed to acquire root fd from Varlink: %m");
×
2035

2036
                r = fd_verify_safe_flags_full(p.context.root_fd, O_DIRECTORY);
×
2037
                if (r < 0)
×
UNCOV
2038
                        return sd_varlink_error_invalid_parameter_name(link, "rootFileDescriptor");
×
2039

2040
                r = fd_verify_directory(p.context.root_fd);
×
2041
                if (r < 0)
×
UNCOV
2042
                        return log_debug_errno(r, "Specified file descriptor does not refer to a directory: %m");
×
2043

2044
                if (!p.context.root) {
×
2045
                        r = fd_get_path(p.context.root_fd, &p.context.root);
×
UNCOV
2046
                        if (r < 0)
×
2047
                                return log_debug_errno(r, "Failed to get path of file descriptor: %m");
×
2048

2049
                        if (empty_or_root(p.context.root))
×
2050
                                p.context.root = mfree(p.context.root);
×
2051
                }
2052
        } else if (p.context.root) {
×
2053
                p.context.root_fd = open(p.context.root, O_RDONLY|O_CLOEXEC|O_DIRECTORY);
×
UNCOV
2054
                if (p.context.root_fd < 0)
×
2055
                        return log_debug_errno(errno, "Failed to open '%s': %m", p.context.root);
×
2056
        } else
2057
                p.context.root_fd = XAT_FDROOT;
×
2058

UNCOV
2059
        if (p.context.entry_token_type < 0)
×
2060
                p.context.entry_token_type = BOOT_ENTRY_TOKEN_AUTO;
×
2061

2062
        r = find_esp_and_warn_at_full(
×
2063
                        p.context.root_fd,
2064
                        /* path= */ NULL,
2065
                        /* unprivileged_mode= */ false,
2066
                        &p.context.esp_path,
2067
                        &p.context.esp_part,
2068
                        &p.context.esp_pstart,
2069
                        &p.context.esp_psize,
2070
                        &p.context.esp_uuid,
2071
                        /* ret_devid= */ NULL);
UNCOV
2072
        if (r == -ENOKEY)
×
UNCOV
2073
                return sd_varlink_error(link, "io.systemd.BootControl.NoESPFound", NULL);
×
UNCOV
2074
        if (r < 0)
×
2075
                return r;
2076

2077
        r = find_xbootldr_and_warn_at(
×
2078
                        p.context.root_fd,
2079
                        /* path= */ NULL,
2080
                        /* unprivileged_mode= */ false,
2081
                        &p.context.xbootldr_path);
UNCOV
2082
        if (r == -ENOKEY)
×
UNCOV
2083
                log_debug_errno(r, "Didn't find an XBOOTLDR partition, using ESP as $BOOT.");
×
UNCOV
2084
        else if (r < 0)
×
2085
                return r;
2086

2087
        r = run_install(&p.context);
×
UNCOV
2088
        if (r == -EUNATCH) /* no boot entry token is set */
×
UNCOV
2089
                return sd_varlink_error(link, "io.systemd.BootControl.BootEntryTokenUnavailable", NULL);
×
2090
        if (r < 0)
×
2091
                return r;
2092

2093
        return sd_varlink_reply(link, NULL);
×
2094
}
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