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

systemd / systemd / 25084703852

28 Apr 2026 09:34PM UTC coverage: 71.849% (-0.02%) from 71.865%
25084703852

push

github

daandemeyer
ci: Reduce noise from claude-review workflow

322528 of 448894 relevant lines covered (71.85%)

1177215.84 hits per line

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

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

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

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

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

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

106
DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(install_operation, InstallOperation);
×
107

108
static void install_context_done(InstallContext *c) {
330✔
109
        assert(c);
330✔
110

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

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

135
        int r;
165✔
136

137
        assert(ret);
165✔
138
        assert(operation >= 0);
165✔
139
        assert(operation < _INSTALL_OPERATION_MAX);
165✔
140

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

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

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

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

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

177
        if (r >= 0) { /* An ESP has been found */
165✔
178
                assert(arg_esp_path);
165✔
179

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

185
                        r = strdup_to(&b.esp_path, e);
28✔
186
                } else
187
                        r = strdup_to(&b.esp_path, arg_esp_path);
137✔
188
                if (r < 0)
165✔
189
                        return log_oom();
×
190
        }
191

192
        r = acquire_xbootldr(
165✔
193
                        /* unprivileged_mode= */ false,
194
                        &b.xbootldr_fd,
195
                        /* ret_uuid= */ NULL,
196
                        /* ret_devid= */ NULL);
197
        if (r < 0)
165✔
198
                return r;
199
        if (r > 0) { /* XBOOTLDR has been found */
165✔
200
                assert(arg_xbootldr_path);
28✔
201

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

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

214
        *ret = TAKE_GENERIC(b, InstallContext, INSTALL_CONTEXT_NULL);
165✔
215

216
        return !!ret->esp_path; /* return positive if we found an ESP */
165✔
217
}
218

219
static int acquire_dollar_boot_fd(InstallContext *c) {
293✔
220
        assert(c);
293✔
221

222
        if (c->xbootldr_fd >= 0)
293✔
223
                return c->xbootldr_fd;
224

225
        if (c->esp_fd >= 0)
259✔
226
                return c->esp_fd;
227

228
        return log_error_errno(SYNTHETIC_ERRNO(EBADF), "Cannot access $BOOT, as neither ESP nor XBOOTLDR have been found.");
×
229
}
230

231
static const char* dollar_boot_path(InstallContext *c) {
293✔
232
        assert(c);
293✔
233

234
        return c->xbootldr_path ?: c->esp_path;
293✔
235
}
236

237
static bool should_touch_install_variables(InstallContext *c) {
148✔
238
        assert(c);
148✔
239

240
        if (c->touch_variables >= 0)
148✔
241
                return c->touch_variables;
×
242

243
        if (!is_efi_boot())  /* NB: this internally checks if we run in a container */
148✔
244
                return false;
245

246
        return empty_or_root(c->root);
38✔
247
}
248

249
static int load_etc_machine_id(InstallContext *c) {
151✔
250
        int r;
151✔
251

252
        assert(c);
151✔
253

254
        r = id128_get_machine_at(c->root_fd, &c->machine_id);
151✔
255
        if (ERRNO_IS_NEG_MACHINE_ID_UNSET(r)) /* Not set or empty */
151✔
256
                return 0;
257
        if (r < 0)
131✔
258
                return log_error_errno(r, "Failed to get machine-id: %m");
×
259

260
        log_debug("Loaded machine ID %s from '%s/etc/machine-id'.", strempty(c->root), SD_ID128_TO_STRING(c->machine_id));
250✔
261
        return 0;
131✔
262
}
263

264
static int load_etc_machine_info(InstallContext *c) {
151✔
265
        /* systemd v250 added support to store the kernel-install layout setting and the machine ID to use
266
         * for setting up the ESP in /etc/machine-info. The newer /etc/kernel/entry-token file, as well as
267
         * the $layout field in /etc/kernel/install.conf are better replacements for this though, hence this
268
         * has been deprecated and is only returned for compatibility. */
269
        _cleanup_free_ char *s = NULL, *layout = NULL;
×
270
        int r;
151✔
271

272
        assert(c);
151✔
273

274
        _cleanup_free_ char *j = path_join(c->root, "/etc/machine-info");
302✔
275
        if (!j)
151✔
276
                return log_oom();
×
277

278
        _cleanup_close_ int fd =
151✔
279
                chase_and_openat(
151✔
280
                                c->root_fd,
281
                                c->root_fd,
282
                                "/etc/machine-info",
283
                                CHASE_MUST_BE_REGULAR,
284
                                O_RDONLY|O_CLOEXEC,
285
                                /* ret_path= */ NULL);
286
        if (fd == -ENOENT)
151✔
287
                return 0;
288
        if (fd < 0)
×
289
                return log_error_errno(fd, "Failed to open '%s': %m", j);
×
290

291
        r = parse_env_file_fd(
×
292
                        fd, "/etc/machine-info",
293
                        "KERNEL_INSTALL_LAYOUT", &layout,
294
                        "KERNEL_INSTALL_MACHINE_ID", &s);
295
        if (r < 0)
×
296
                return log_error_errno(r, "Failed to parse '%s': %m", j);
×
297

298
        if (!isempty(s)) {
×
299
                if (!arg_quiet)
×
300
                        log_notice("Read $KERNEL_INSTALL_MACHINE_ID from '%s'. "
×
301
                                   "Please move it to '%s/etc/kernel/entry-token'.", j, strempty(c->root));
302

303
                r = sd_id128_from_string(s, &c->machine_id);
×
304
                if (r < 0)
×
305
                        return log_error_errno(r, "Failed to parse KERNEL_INSTALL_MACHINE_ID=\"%s\" in '%s': %m", s, j);
×
306

307
                log_debug("Loaded KERNEL_INSTALL_MACHINE_ID=\"%s\" from '%s'.",
×
308
                          SD_ID128_TO_STRING(c->machine_id), j);
309
        }
310

311
        if (!isempty(layout)) {
151✔
312
                if (!arg_quiet)
×
313
                        log_notice("Read $KERNEL_INSTALL_LAYOUT from '%s'. "
×
314
                                   "Please move it to the layout= setting of '%s/etc/kernel/install.conf'.", j, strempty(c->root));
315

316
                log_debug("KERNEL_INSTALL_LAYOUT=\"%s\" is specified in '%s'.", layout, j);
×
317
                free_and_replace(c->install_layout, layout);
×
318
        }
319

320
        return 0;
321
}
322

323
static int load_kernel_install_layout(InstallContext *c) {
151✔
324
        _cleanup_free_ char *layout = NULL;
151✔
325
        int r;
151✔
326

327
        assert(c);
151✔
328

329
        const char *e = secure_getenv("KERNEL_INSTALL_CONF_ROOT");
151✔
330
        r = load_kernel_install_conf_at(
151✔
331
                        e ? NULL : c->root,
332
                        e ? XAT_FDROOT : c->root_fd,
333
                        e,
334
                        /* ret_machine_id= */ NULL,
335
                        /* ret_boot_root= */ NULL,
336
                        &layout,
337
                        /* ret_initrd_generator= */ NULL,
338
                        /* ret_uki_generator= */ NULL);
339
        if (r <= 0)
151✔
340
                return r;
341

342
        if (!isempty(layout)) {
151✔
343
                log_debug("layout=\"%s\" is specified in config.", layout);
×
344
                free_and_replace(c->install_layout, layout);
×
345
        }
346

347
        return 0;
348
}
349

350
static bool use_boot_loader_spec_type1(InstallContext *c) {
151✔
351
        assert(c);
151✔
352
        /* If the layout is not specified, or if it is set explicitly to "bls" we assume Boot Loader
353
         * Specification Type #1 is the chosen format for our boot loader entries */
354
        return !c->install_layout || streq(c->install_layout, "bls");
151✔
355
}
356

357
static int settle_make_entry_directory(InstallContext *c) {
151✔
358
        int r;
151✔
359

360
        assert(c);
151✔
361

362
        r = load_etc_machine_id(c);
151✔
363
        if (r < 0)
151✔
364
                return r;
365

366
        r = load_etc_machine_info(c);
151✔
367
        if (r < 0)
151✔
368
                return r;
369

370
        r = load_kernel_install_layout(c);
151✔
371
        if (r < 0)
151✔
372
                return r;
373

374
        const char *e = secure_getenv("KERNEL_INSTALL_CONF_ROOT");
151✔
375
        r = boot_entry_token_ensure_at(
151✔
376
                        e ? XAT_FDROOT : c->root_fd,
377
                        e,
378
                        c->machine_id,
379
                        /* machine_id_is_random= */ false,
380
                        &c->entry_token_type,
381
                        &c->entry_token);
382
        if (r < 0)
151✔
383
                return r;
384

385
        log_debug("Using entry token: %s", c->entry_token);
151✔
386

387
        bool layout_type1 = use_boot_loader_spec_type1(c);
151✔
388
        if (c->make_entry_directory < 0) { /* Automatic mode */
151✔
389
                if (layout_type1) {
×
390
                        if (c->entry_token_type == BOOT_ENTRY_TOKEN_MACHINE_ID) {
×
391
                                _cleanup_free_ char *j = path_join(c->root, "/etc/machine-id");
×
392
                                if (!j)
×
393
                                        return log_oom();
×
394

395
                                _cleanup_close_ int fd = -EBADF;
×
396
                                r = chaseat(c->root_fd,
×
397
                                            c->root_fd,
398
                                            "/etc/machine-id",
399
                                            CHASE_MUST_BE_REGULAR,
400
                                            /* ret_path= */ NULL,
401
                                            &fd);
402
                                if (r < 0)
×
403
                                        return log_debug_errno(r, "Unable to open '%s': %m", j);
×
404

405
                                r = fd_is_temporary_fs(fd);
×
406
                                if (r < 0)
×
407
                                        return log_debug_errno(r, "Couldn't determine whether '%s' is on a temporary file system: %m", j);
×
408

409
                                c->make_entry_directory = r == 0;
×
410
                        } else
411
                                c->make_entry_directory = true;
×
412
                } else
413
                        c->make_entry_directory = false;
×
414
        }
415

416
        if (c->make_entry_directory > 0 && !layout_type1)
151✔
417
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
418
                                       "KERNEL_INSTALL_LAYOUT=\"%s\" is configured, but Boot Loader Specification Type #1 entry directory creation was requested.",
419
                                       c->install_layout);
420

421
        return 0;
422
}
423

424
static int compare_product(const char *a, const char *b) {
250✔
425
        size_t x, y;
250✔
426

427
        assert(a);
250✔
428
        assert(b);
250✔
429

430
        x = strcspn(a, " ");
250✔
431
        y = strcspn(b, " ");
250✔
432
        if (x != y)
250✔
433
                return x < y ? -1 : x > y ? 1 : 0;
×
434

435
        return strncmp(a, b, x);
250✔
436
}
437

438
static int compare_version(const char *a, const char *b) {
250✔
439
        assert(a);
250✔
440
        assert(b);
250✔
441

442
        a += strcspn(a, " ");
250✔
443
        a += strspn(a, " ");
250✔
444
        b += strcspn(b, " ");
250✔
445
        b += strspn(b, " ");
250✔
446

447
        return strverscmp_improved(a, b);
250✔
448
}
449

450
static int version_check(int fd_from, const char *from, int fd_to, const char *to) {
250✔
451
        _cleanup_free_ char *a = NULL, *b = NULL;
250✔
452
        int r;
250✔
453

454
        assert(fd_from >= 0);
250✔
455
        assert(from);
250✔
456
        assert(fd_to >= 0);
250✔
457
        assert(to);
250✔
458

459
        /* Does not reposition file offset */
460

461
        r = get_file_version(fd_from, &a);
250✔
462
        if (r == -ESRCH)
250✔
463
                return log_notice_errno(r, "Source file \"%s\" does not carry version information!", from);
×
464
        if (r < 0)
250✔
465
                return r;
466

467
        r = get_file_version(fd_to, &b);
250✔
468
        if (r == -ESRCH)
250✔
469
                return log_info_errno(r, "Skipping \"%s\", it's owned by another boot loader (no version info found).", to);
×
470
        if (r < 0)
250✔
471
                return r;
472
        if (compare_product(a, b) != 0)
250✔
473
                return log_info_errno(SYNTHETIC_ERRNO(ESRCH),
×
474
                                      "Skipping \"%s\", it's owned by another boot loader.", to);
475

476
        r = compare_version(a, b);
250✔
477
        log_debug("Comparing versions: \"%s\" %s \"%s\"", a, comparison_operator(r), b);
488✔
478
        if (r < 0)
250✔
479
                return log_warning_errno(SYNTHETIC_ERRNO(ESTALE),
×
480
                                         "Skipping \"%s\", newer boot loader version in place already.", to);
481
        if (r == 0)
250✔
482
                return log_info_errno(SYNTHETIC_ERRNO(ESTALE),
250✔
483
                                      "Skipping \"%s\", same boot loader version in place already.", to);
484

485
        return 0;
486
}
487

488
static int copy_file_with_version_check(
288✔
489
                const char *source_path,
490
                int source_fd,
491
                const char *dest_path,
492
                int dest_parent_fd,
493
                const char *dest_filename,
494
                int dest_fd,
495
                bool force) {
496

497
        int r;
288✔
498

499
        assert(source_path);
288✔
500
        assert(source_fd >= 0);
288✔
501
        assert(dest_path);
288✔
502
        assert(dest_parent_fd >= 0);
288✔
503
        assert(dest_filename);
288✔
504

505
        if (!force && dest_fd >= 0) {
288✔
506
                r = version_check(source_fd, source_path, dest_fd, dest_path);
250✔
507
                if (r < 0)
250✔
508
                        return r;
288✔
509
        }
510

511
        _cleanup_free_ char *t = NULL;
38✔
512
        _cleanup_close_ int write_fd = -EBADF;
38✔
513
        write_fd = open_tmpfile_linkable_at(dest_parent_fd, dest_filename, O_WRONLY|O_CLOEXEC, &t);
38✔
514
        if (write_fd < 0)
38✔
515
                return log_error_errno(write_fd, "Failed to open \"%s\" for writing: %m", dest_path);
×
516

517
        CLEANUP_TMPFILE_AT(dest_parent_fd, t);
38✔
518

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

527
        (void) copy_times(source_fd, write_fd, /* flags= */ 0);
38✔
528
        (void) fchmod(write_fd, 0644);
38✔
529

530
        r = link_tmpfile_at(write_fd, dest_parent_fd, t, dest_filename, LINK_TMPFILE_REPLACE|LINK_TMPFILE_SYNC);
38✔
531
        if (r < 0)
38✔
532
                return log_error_errno(r, "Failed to move data from \"%s\" to \"%s\": %m", source_path, dest_path);
×
533

534
        t = mfree(t); /* disarm CLEANUP_TMPFILE_AT() */
38✔
535

536
        log_info("Copied \"%s\" to \"%s\".", source_path, dest_path);
38✔
537
        return 0;
538
}
539

540
static int mkdir_one(const char *root, int root_fd, const char *path) {
124✔
541
        int r;
124✔
542

543
        assert(root);
124✔
544
        assert(root_fd >= 0);
124✔
545
        assert(path);
124✔
546

547
        _cleanup_free_ char *p = path_join(empty_to_root(root), path);
248✔
548
        if (!p)
124✔
549
                return log_oom();
×
550

551
        r = chaseat(root_fd,
124✔
552
                    root_fd,
553
                    path,
554
                    CHASE_PROHIBIT_SYMLINKS|CHASE_MKDIR_0755|CHASE_MUST_BE_DIRECTORY,
555
                    /* ret_path= */ NULL,
556
                    /* ret_fd= */ NULL);
557
        if (r < 0)
124✔
558
                return log_error_errno(r, "Failed to create \"%s\": %m", p);
×
559

560
        log_info("Created directory \"%s\".", p);
124✔
561
        return 0;
562
}
563

564
static const char *const esp_subdirs[] = {
565
        /* The directories to place in the ESP */
566
        "EFI",
567
        "EFI/systemd",
568
        "EFI/BOOT",
569
        "loader",
570
        "loader/keys",
571
        NULL
572
};
573

574
static const char *const dollar_boot_subdirs[] = {
575
        /* The directories to place in the XBOOTLDR partition or the ESP, depending what exists */
576
        "loader",
577
        "loader/entries",  /* Type #1 entries */
578
        "EFI",
579
        "EFI/Linux",       /* Type #2 entries */
580
        NULL
581
};
582

583
static int create_subdirs(const char *root, int root_fd, const char * const *subdirs) {
26✔
584
        int r;
26✔
585

586
        assert(root);
26✔
587
        assert(root_fd >= 0);
26✔
588

589
        STRV_FOREACH(i, subdirs) {
143✔
590
                r = mkdir_one(root, root_fd, *i);
117✔
591
                if (r < 0)
117✔
592
                        return r;
593
        }
594

595
        return 0;
596
}
597

598
static int update_efi_boot_binaries(
125✔
599
                InstallContext *c,
600
                const char *source_path,
601
                int source_fd,
602
                const char *ignore_filename) {
603

604
        int r, ret = 0;
125✔
605

606
        assert(c);
125✔
607
        assert(source_path);
125✔
608

609
        if (c->esp_fd < 0)
125✔
610
                return c->esp_fd;
125✔
611

612
        _cleanup_free_ char *j = path_join(c->root, c->esp_path);
250✔
613
        if (!j)
125✔
614
                return log_oom();
×
615

616
        _cleanup_closedir_ DIR *d = NULL;
125✔
617
        r = chase_and_opendirat(
125✔
618
                        c->esp_fd,
619
                        c->esp_fd,
620
                        "/EFI/BOOT",
621
                        CHASE_PROHIBIT_SYMLINKS|CHASE_MUST_BE_DIRECTORY,
622
                        /* ret_path= */ NULL,
623
                        &d);
624
        if (r == -ENOENT)
125✔
625
                return 0;
626
        if (r < 0)
125✔
627
                return log_error_errno(r, "Failed to open directory \"%s/EFI/BOOT\": %m", j);
×
628

629
        FOREACH_DIRENT(de, d, break) {
625✔
630
                _cleanup_close_ int fd = -EBADF;
625✔
631

632
                if (!endswith_no_case(de->d_name, ".efi"))
250✔
633
                        continue;
×
634

635
                if (strcaseeq_ptr(ignore_filename, de->d_name))
250✔
636
                        continue;
125✔
637

638
                fd = xopenat_full(dirfd(d), de->d_name, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY|O_NOFOLLOW, XO_REGULAR, /* mode= */ MODE_INVALID);
125✔
639
                if (fd < 0)
125✔
640
                        return log_error_errno(fd, "Failed to open \"%s/%s\" for reading: %m", j, de->d_name);
×
641

642
                r = pe_is_native_fd(fd);
125✔
643
                if (r < 0) {
125✔
644
                        log_warning_errno(r, "Failed to detect if \"%s/%s\" is for native architecture, ignoring: %m", j, de->d_name);
×
645
                        continue;
×
646
                }
647
                if (r == 0)
125✔
648
                        continue;
125✔
649

650
                _cleanup_free_ char *dest_path = path_join(j, "/EFI/BOOT", de->d_name);
×
651
                if (!dest_path)
×
652
                        return log_oom();
×
653

654
                r = copy_file_with_version_check(source_path, source_fd, dest_path, dirfd(d), de->d_name, fd, /* force= */ false);
×
655
                if (IN_SET(r, -ESTALE, -ESRCH))
×
656
                        continue;
×
657
                RET_GATHER(ret, r);
×
658
        }
659

660
        return ret;
661
}
662

663
static int copy_one_file(
144✔
664
                InstallContext *c,
665
                const char *name,
666
                bool force) {
667

668
        int r, ret = 0;
144✔
669

670
        assert(c);
144✔
671

672
        _cleanup_free_ char *dest_name = strdup(name);
144✔
673
        if (!dest_name)
144✔
674
                return log_oom();
×
675
        char *s = endswith_no_case(dest_name, ".signed");
144✔
676
        if (s)
144✔
677
                *s = 0;
144✔
678

679
        _cleanup_free_ char *sp = path_join(BOOTLIBDIR, name);
288✔
680
        if (!sp)
144✔
681
                return log_oom();
×
682

683
        _cleanup_free_ char *source_path = NULL;
144✔
684
        _cleanup_close_ int source_fd = -EBADF;
144✔
685
        if (IN_SET(c->install_source, INSTALL_SOURCE_AUTO, INSTALL_SOURCE_IMAGE)) {
144✔
686
                source_fd = chase_and_openat(
144✔
687
                                c->root_fd,
688
                                c->root_fd,
689
                                sp,
690
                                CHASE_MUST_BE_REGULAR,
691
                                O_RDONLY|O_CLOEXEC,
692
                                &source_path);
693
                if (source_fd < 0 && (source_fd != -ENOENT || c->install_source != INSTALL_SOURCE_AUTO))
144✔
694
                        return log_error_errno(source_fd, "Failed to resolve path '%s' under directory '%s': %m", sp, c->root);
×
695

696
                /* If we had a root directory to try, we didn't find it and we are in auto mode, retry on the host */
697
        }
698
        if (source_fd < 0) {
16✔
699
                source_fd = chase_and_open(
16✔
700
                                sp,
701
                                /* root= */ NULL,
702
                                CHASE_MUST_BE_REGULAR,
703
                                O_RDONLY|O_CLOEXEC,
704
                                &source_path);
705
                if (source_fd < 0)
16✔
706
                        return log_error_errno(source_fd, "Failed to resolve path '%s': %m", sp);
×
707
        }
708

709
        if (c->esp_fd < 0)
144✔
710
                return c->esp_fd;
711

712
        _cleanup_free_ char *j = path_join(c->root, c->esp_path);
288✔
713
        if (!j)
144✔
714
                return log_oom();
×
715

716
        _cleanup_close_ int dest_parent_fd = -EBADF;
144✔
717
        r = chaseat(c->esp_fd,
144✔
718
                    c->esp_fd,
719
                    "/EFI/systemd",
720
                    CHASE_PROHIBIT_SYMLINKS|CHASE_MKDIR_0755|CHASE_MUST_BE_DIRECTORY,
721
                    /* ret_path= */ NULL,
722
                    &dest_parent_fd);
723
        if (r < 0)
144✔
724
                return log_error_errno(r, "Failed to resolve path '/EFI/systemd' under directory '%s': %m", j);
×
725

726
        _cleanup_free_ char *dest_path = path_join(j, "/EFI/systemd", dest_name);
288✔
727
        if (!dest_path)
144✔
728
                return log_oom();
×
729

730
        _cleanup_close_ int dest_fd = xopenat_full(dest_parent_fd, dest_name, O_RDONLY|O_CLOEXEC, XO_REGULAR, MODE_INVALID);
288✔
731
        if (dest_fd < 0 && dest_fd != -ENOENT)
144✔
732
                return log_error_errno(dest_fd, "Failed to open '%s' under '%s/EFI/systemd' directory: %m", dest_name, j);
×
733

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

738
        const char *e = startswith(dest_name, "systemd-boot");
144✔
739
        if (e) {
144✔
740

741
                /* Create the EFI default boot loader name (specified for removable devices) */
742
                _cleanup_free_ char *boot_dot_efi = strjoin("BOOT", e);
288✔
743
                if (!boot_dot_efi)
144✔
744
                        return log_oom();
×
745

746
                ascii_strupper(boot_dot_efi);
144✔
747

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

758
                _cleanup_free_ char *default_dest_path = path_join(j, "/EFI/BOOT", boot_dot_efi);
288✔
759
                if (!default_dest_path)
144✔
760
                        return log_oom();
×
761

762
                _cleanup_close_ int default_dest_fd = xopenat_full(default_dest_parent_fd, boot_dot_efi, O_RDONLY|O_CLOEXEC, XO_REGULAR, MODE_INVALID);
288✔
763
                if (default_dest_fd < 0 && default_dest_fd != -ENOENT)
144✔
764
                        return log_error_errno(default_dest_fd, "Failed to open '%s' under '%s/EFI/BOOT' directory: %m", boot_dot_efi, j);
×
765

766
                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));
144✔
767

768
                /* If we were installed under any other name in /EFI/BOOT/, make sure we update those
769
                 * binaries as well. */
770
                if (!force)
144✔
771
                        RET_GATHER(ret, update_efi_boot_binaries(c, source_path, source_fd, boot_dot_efi));
125✔
772
        }
773

774
        return ret;
775
}
776

777
static int install_binaries(
138✔
778
                InstallContext *c,
779
                const char *arch) {
780

781
        int r;
138✔
782

783
        assert(c);
138✔
784

785
        _cleanup_free_ char *source_path = NULL;
138✔
786
        _cleanup_closedir_ DIR *d = NULL;
138✔
787
        if (IN_SET(c->install_source, INSTALL_SOURCE_AUTO, INSTALL_SOURCE_IMAGE)) {
138✔
788
                r = chase_and_opendirat(
138✔
789
                                c->root_fd,
790
                                c->root_fd,
791
                                BOOTLIBDIR,
792
                                CHASE_MUST_BE_DIRECTORY,
793
                                &source_path,
794
                                &d);
795
                if (r < 0 && (r != -ENOENT || c->install_source != INSTALL_SOURCE_AUTO))
138✔
796
                        return log_error_errno(r, "Failed to resolve path '%s' under directory '%s': %m", BOOTLIBDIR, c->root);
×
797

798
                /* If we had a root directory to try, we didn't find it and we are in auto mode, retry on the host */
799
        }
800
        if (!d) {
138✔
801
                r = chase_and_opendir(
12✔
802
                                BOOTLIBDIR,
803
                                /* root= */ NULL,
804
                                CHASE_MUST_BE_DIRECTORY,
805
                                &source_path,
806
                                &d);
807
                if (r == -ENOENT && c->graceful) {
12✔
808
                        log_debug("Source directory '%s' does not exist, ignoring.", BOOTLIBDIR);
×
809
                        return 0;
×
810
                }
811
                if (r < 0)
12✔
812
                        return log_error_errno(r, "Failed to resolve path '%s': %m", BOOTLIBDIR);
×
813
        }
814

815
        const char *suffix = strjoina(arch, ".efi");
690✔
816
        const char *suffix_signed = strjoina(arch, ".efi.signed");
690✔
817

818
        FOREACH_DIRENT(de, d, return log_error_errno(errno, "Failed to read \"%s\": %m", source_path)) {
1,518✔
819
                int k;
1,104✔
820

821
                if (endswith_no_case(de->d_name, suffix)) {
1,104✔
822
                        /* skip the .efi file, if there's a .signed version of it */
823
                        _cleanup_free_ const char *s = strjoin(de->d_name, ".signed");
288✔
824
                        if (!s)
144✔
825
                                return log_oom();
×
826
                        if (faccessat(dirfd(d), s, F_OK, 0) >= 0)
144✔
827
                                continue;
144✔
828
                } else if (!endswith_no_case(de->d_name, suffix_signed))
960✔
829
                        continue;
816✔
830

831
                k = copy_one_file(c, de->d_name, c->operation == INSTALL_NEW);
144✔
832
                /* Don't propagate an error code if no update necessary, installed version already equal or
833
                 * newer version, or other boot loader in place. */
834
                if (c->graceful && IN_SET(k, -ESTALE, -ESRCH))
144✔
835
                        continue;
122✔
836
                RET_GATHER(r, k);
22✔
837
        }
838

839
        return r;
840
}
841

842
static int install_loader_config(InstallContext *c) {
13✔
843
        int r;
13✔
844

845
        assert(c);
13✔
846
        assert(c->make_entry_directory >= 0);
13✔
847

848
        if (c->esp_fd < 0)
13✔
849
                return c->esp_fd;
13✔
850

851
        _cleanup_free_ char *j = path_join(c->root, c->esp_path);
26✔
852
        if (!j)
13✔
853
                return log_oom();
×
854

855
        _cleanup_close_ int loader_dir_fd = -EBADF;
13✔
856
        r = chaseat(c->esp_fd,
13✔
857
                    c->esp_fd,
858
                    "loader",
859
                    CHASE_PROHIBIT_SYMLINKS|CHASE_MKDIR_0755|CHASE_MUST_BE_DIRECTORY,
860
                    /* ret_path= */ NULL,
861
                    &loader_dir_fd);
862
        if (r < 0)
13✔
863
                return log_error_errno(r, "Failed to open '/loader/' directory below '%s': %m", j);
×
864

865
        if (faccessat(loader_dir_fd, "loader.conf", F_OK, AT_SYMLINK_NOFOLLOW) < 0) {
13✔
866
                if (errno != ENOENT)
13✔
867
                        return log_error_errno(errno, "Failed to check if '/loader/loader.conf' exists below '%s': %m", j);
×
868
        } else /* Silently skip creation if the file already exists (early check) */
869
                return 0;
870

871
        _cleanup_free_ char *t = NULL;
13✔
872
        _cleanup_fclose_ FILE *f = NULL;
13✔
873
        r = fopen_tmpfile_linkable_at(loader_dir_fd, "loader.conf", O_WRONLY|O_CLOEXEC, &t, &f);
13✔
874
        if (r < 0)
13✔
875
                return log_error_errno(r, "Failed to open '%s/loader/loader.conf' for writing: %m", j);
×
876

877
        CLEANUP_TMPFILE_AT(loader_dir_fd, t);
13✔
878

879
        fprintf(f, "#timeout 3\n"
13✔
880
                   "#console-mode keep\n");
881

882
        if (c->make_entry_directory) {
13✔
883
                assert(c->entry_token);
7✔
884
                fprintf(f, "default %s-*\n", c->entry_token);
7✔
885
        }
886

887
        r = flink_tmpfile_at(f, loader_dir_fd, t, "loader.conf", LINK_TMPFILE_SYNC);
13✔
888
        if (r == -EEXIST)
13✔
889
                return 0; /* Silently skip creation if the file exists now (recheck) */
890
        if (r < 0)
13✔
891
                return log_error_errno(r, "Failed to move '%s/loader/loader.conf' into place: %m", j);
×
892

893
        t = mfree(t); /* disarm CLEANUP_TMPFILE_AT() */
13✔
894
        return 1;
13✔
895
}
896

897
static int install_loader_specification(InstallContext *c) {
135✔
898
        int r;
135✔
899

900
        assert(c);
135✔
901

902
        int dollar_boot_fd = acquire_dollar_boot_fd(c);
135✔
903
        if (dollar_boot_fd < 0)
135✔
904
                return dollar_boot_fd;
135✔
905

906
        _cleanup_free_ char *j = path_join(c->root, dollar_boot_path(c));
270✔
907
        if (!j)
135✔
908
                return log_oom();
×
909

910
        _cleanup_close_ int loader_dir_fd = -EBADF;
135✔
911
        r = chaseat(dollar_boot_fd,
135✔
912
                    dollar_boot_fd,
913
                    "loader",
914
                    CHASE_PROHIBIT_SYMLINKS|CHASE_MKDIR_0755|CHASE_MUST_BE_DIRECTORY,
915
                    /* ret_path= */ NULL,
916
                    &loader_dir_fd);
917
        if (r < 0)
135✔
918
                return log_error_errno(r, "Failed to pin '/loader' directory below '%s': %m", j);
×
919

920
        if (faccessat(loader_dir_fd, "entries.srel", F_OK, AT_SYMLINK_NOFOLLOW) < 0) {
135✔
921
                if (errno != ENOENT)
13✔
922
                        return log_error_errno(errno, "Failed to check if '/loader/entries.srel' exists below '%s': %m", j);
×
923
        } else /* Silently skip creation if the file already exists (early check) */
924
                return 0;
925

926
        _cleanup_free_ char *t = NULL;
13✔
927
        _cleanup_fclose_ FILE *f = NULL;
13✔
928
        r = fopen_tmpfile_linkable_at(loader_dir_fd, "entries.srel", O_WRONLY|O_CLOEXEC, &t, &f);
13✔
929
        if (r < 0)
13✔
930
                return log_error_errno(r, "Failed to open '%s/loader/entries.srel' for writing: %m", j);
×
931

932
        CLEANUP_TMPFILE_AT(loader_dir_fd, t);
13✔
933

934
        fprintf(f, "type1\n");
13✔
935

936
        r = flink_tmpfile_at(f, loader_dir_fd, t, "entries.srel", LINK_TMPFILE_SYNC);
13✔
937
        if (r == -EEXIST)
13✔
938
                return 0; /* Silently skip creation if the file exists now (recheck) */
939
        if (r < 0)
13✔
940
                return log_error_errno(r, "Failed to move '%s/loader/entries.srel' into place: %m", j);
×
941

942
        t = mfree(t); /* disarm CLEANUP_TMPFILE_AT() */
13✔
943
        return 1;
13✔
944
}
945

946
static int install_entry_directory(InstallContext *c) {
13✔
947
        assert(c);
13✔
948
        assert(c->make_entry_directory >= 0);
13✔
949

950
        if (!c->make_entry_directory)
13✔
951
                return 0;
13✔
952

953
        assert(c->entry_token);
7✔
954

955
        int dollar_boot_fd = acquire_dollar_boot_fd(c);
7✔
956
        if (dollar_boot_fd < 0)
7✔
957
                return dollar_boot_fd;
958

959
        _cleanup_free_ char *j = path_join(c->root, dollar_boot_path(c));
14✔
960
        if (!j)
7✔
961
                return log_oom();
×
962

963
        return mkdir_one(j, dollar_boot_fd, c->entry_token);
7✔
964
}
965

966
static int install_entry_token(InstallContext *c) {
13✔
967
        int r;
13✔
968

969
        assert(c);
13✔
970
        assert(c->make_entry_directory >= 0);
13✔
971
        assert(c->entry_token);
13✔
972

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

976
        if (!c->make_entry_directory && c->entry_token_type == BOOT_ENTRY_TOKEN_MACHINE_ID)
13✔
977
                return 0;
13✔
978

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

981
        _cleanup_free_ char *j = path_join(c->root, confdir);
26✔
982
        if (!j)
13✔
983
                return log_oom();
×
984

985
        _cleanup_close_ int dfd = -EBADF;
13✔
986
        r = chaseat(c->root_fd,
13✔
987
                    c->root_fd,
988
                    confdir,
989
                    CHASE_MKDIR_0755|CHASE_MUST_BE_DIRECTORY,
990
                    /* ret_path= */ NULL,
991
                    &dfd);
992
        if (r < 0)
13✔
993
                return log_error_errno(r, "Failed to open '%s': %m", j);
×
994

995
        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✔
996
        if (r < 0)
13✔
997
                return log_error_errno(r, "Failed to write entry token '%s' to '%s/entry-token': %m", c->entry_token, j);
×
998

999
        return 0;
1000
}
1001

1002
#if HAVE_OPENSSL
1003
static int efi_timestamp(EFI_TIME *ret) {
1✔
1004
        struct tm tm = {};
1✔
1005
        int r;
1✔
1006

1007
        assert(ret);
1✔
1008

1009
        r = localtime_or_gmtime_usec(source_date_epoch_or_now(), /* utc= */ true, &tm);
1✔
1010
        if (r < 0)
1✔
1011
                return log_error_errno(r, "Failed to convert timestamp to calendar time: %m");
×
1012

1013
        *ret = (EFI_TIME) {
1✔
1014
                .Year = 1900 + tm.tm_year,
1✔
1015
                /* tm_mon starts at 0, EFI_TIME months start at 1. */
1016
                .Month = tm.tm_mon + 1,
1✔
1017
                .Day = tm.tm_mday,
1✔
1018
                .Hour = tm.tm_hour,
1✔
1019
                .Minute = tm.tm_min,
1✔
1020
                .Second = tm.tm_sec,
1✔
1021
        };
1022

1023
        return 0;
1✔
1024
}
1025
#endif
1026

1027
static int install_secure_boot_auto_enroll(InstallContext *c) {
13✔
1028
#if HAVE_OPENSSL
1029
        int r;
13✔
1030
#endif
1031

1032
        if (!arg_secure_boot_auto_enroll)
13✔
1033
                return 0;
13✔
1034

1035
#if HAVE_OPENSSL
1036
        if (!c->secure_boot_certificate || !c->secure_boot_private_key)
1✔
1037
                return 0;
1038

1039
        r = dlopen_libcrypto(LOG_DEBUG);
1✔
1040
        if (r < 0)
1✔
1041
                return r;
1042

1043
        _cleanup_free_ uint8_t *dercert = NULL;
1✔
1044
        int dercertsz;
1✔
1045
        dercertsz = sym_i2d_X509(c->secure_boot_certificate, &dercert);
1✔
1046
        if (dercertsz < 0)
1✔
1047
                return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to convert X.509 certificate to DER: %s",
×
1048
                                       sym_ERR_error_string(sym_ERR_get_error(), NULL));
1049

1050
        if (c->esp_fd < 0)
1✔
1051
                return c->esp_fd;
1052

1053
        _cleanup_free_ char *j = path_join(c->root, c->esp_path);
2✔
1054
        if (!j)
1✔
1055
                return log_oom();
×
1056

1057
        _cleanup_close_ int keys_fd = -EBADF;
1✔
1058
        r = chaseat(c->esp_fd,
1✔
1059
                    c->esp_fd,
1060
                    "loader/keys/auto",
1061
                    CHASE_PROHIBIT_SYMLINKS|CHASE_MKDIR_0755|CHASE_MUST_BE_DIRECTORY,
1062
                    /* ret_path= */ NULL,
1063
                    &keys_fd);
1064
        if (r < 0)
1✔
1065
                return log_error_errno(r, "Failed to chase /loader/keys/auto/ below '%s': %m", j);
×
1066

1067
        uint32_t siglistsz = offsetof(EFI_SIGNATURE_LIST, Signatures) + offsetof(EFI_SIGNATURE_DATA, SignatureData) + dercertsz;
1✔
1068
        /* We use malloc0() to zero-initialize the SignatureOwner field of Signatures[0]. */
1069
        _cleanup_free_ EFI_SIGNATURE_LIST *siglist = malloc0(siglistsz);
2✔
1070
        if (!siglist)
1✔
1071
                return log_oom();
×
1072

1073
        *siglist = (EFI_SIGNATURE_LIST) {
1✔
1074
                .SignatureType = EFI_CERT_X509_GUID,
1075
                .SignatureListSize = siglistsz,
1076
                .SignatureSize = offsetof(EFI_SIGNATURE_DATA, SignatureData) + dercertsz,
1✔
1077
        };
1078

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

1081
        EFI_TIME timestamp;
1✔
1082
        r = efi_timestamp(&timestamp);
1✔
1083
        if (r < 0)
1✔
1084
                return r;
1085

1086
        uint32_t attrs =
1✔
1087
                EFI_VARIABLE_NON_VOLATILE|
1088
                EFI_VARIABLE_BOOTSERVICE_ACCESS|
1089
                EFI_VARIABLE_RUNTIME_ACCESS|
1090
                EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS;
1091

1092
        FOREACH_STRING(db, "PK", "KEK", "db") {
4✔
1093
                _cleanup_(BIO_freep) BIO *bio = NULL;
×
1094

1095
                bio = sym_BIO_new(sym_BIO_s_mem());
3✔
1096
                if (!bio)
3✔
1097
                        return log_oom();
×
1098

1099
                _cleanup_free_ char16_t *db16 = utf8_to_utf16(db, SIZE_MAX);
6✔
1100
                if (!db16)
3✔
1101
                        return log_oom();
×
1102

1103
                /* Don't count the trailing NUL terminator. */
1104
                if (sym_BIO_write(bio, db16, char16_strsize(db16) - sizeof(char16_t)) < 0)
3✔
1105
                        return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to write variable name to bio");
×
1106

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

1109
                if (sym_BIO_write(bio, guid, sizeof(*guid)) < 0)
3✔
1110
                        return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to write variable GUID to bio");
×
1111

1112
                if (sym_BIO_write(bio, &attrs, sizeof(attrs)) < 0)
3✔
1113
                        return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to write variable attributes to bio");
×
1114

1115
                if (sym_BIO_write(bio, &timestamp, sizeof(timestamp)) < 0)
3✔
1116
                        return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to write timestamp to bio");
×
1117

1118
                if (sym_BIO_write(bio, siglist, siglistsz) < 0)
3✔
1119
                        return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to write signature list to bio");
×
1120

1121
                _cleanup_(PKCS7_freep) PKCS7 *p7 = NULL;
×
1122
                p7 = sym_PKCS7_sign(c->secure_boot_certificate, c->secure_boot_private_key, /* certs= */ NULL, bio, PKCS7_DETACHED|PKCS7_NOATTR|PKCS7_BINARY|PKCS7_NOSMIMECAP);
3✔
1123
                if (!p7)
3✔
1124
                        return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to calculate PKCS7 signature: %s",
×
1125
                                               sym_ERR_error_string(sym_ERR_get_error(), NULL));
1126

1127
                _cleanup_free_ uint8_t *sig = NULL;
×
1128
                int sigsz = sym_i2d_PKCS7(p7, &sig);
3✔
1129
                if (sigsz < 0)
3✔
1130
                        return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to convert PKCS7 signature to DER: %s",
×
1131
                                               sym_ERR_error_string(sym_ERR_get_error(), NULL));
1132

1133
                size_t authsz = offsetof(EFI_VARIABLE_AUTHENTICATION_2, AuthInfo.CertData) + sigsz;
3✔
1134
                _cleanup_free_ EFI_VARIABLE_AUTHENTICATION_2 *auth = malloc(authsz);
×
1135
                if (!auth)
3✔
1136
                        return log_oom();
×
1137

1138
                *auth = (EFI_VARIABLE_AUTHENTICATION_2) {
3✔
1139
                        .TimeStamp = timestamp,
1140
                        .AuthInfo = {
1141
                                .Hdr = {
1142
                                        .dwLength = offsetof(WIN_CERTIFICATE_UEFI_GUID, CertData) + sigsz,
3✔
1143
                                        .wRevision = 0x0200,
1144
                                        .wCertificateType = 0x0EF1, /* WIN_CERT_TYPE_EFI_GUID */
1145
                                },
1146
                                .CertType = EFI_CERT_TYPE_PKCS7_GUID,
1147
                        }
1148
                };
1149

1150
                memcpy(auth->AuthInfo.CertData, sig, sigsz);
3✔
1151

1152
                _cleanup_free_ char *filename = strjoin(db, ".auth");
3✔
1153
                if (!filename)
3✔
1154
                        return log_oom();
×
1155

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

1161
                CLEANUP_TMPFILE_AT(keys_fd, t);
×
1162

1163
                r = loop_write(fd, auth, authsz);
3✔
1164
                if (r < 0)
3✔
1165
                        return log_error_errno(r, "Failed to write authentication descriptor to secure boot auto-enrollment file: %m");
×
1166

1167
                r = loop_write(fd, siglist, siglistsz);
3✔
1168
                if (r < 0)
3✔
1169
                        return log_error_errno(r, "Failed to write signature list to secure boot auto-enrollment file: %m");
×
1170

1171
                r = link_tmpfile_at(fd, keys_fd, t, filename, LINK_TMPFILE_SYNC);
3✔
1172
                if (r < 0)
3✔
1173
                        return log_error_errno(errno, "Failed to link secure boot auto-enrollment file: %m");
×
1174

1175
                t = mfree(t); /* Disarm CLEANUP_TMPFILE_AT() */
3✔
1176

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

1180
        return 0;
1✔
1181
#else
1182
        return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Built without OpenSSL support, cannot set up auto-enrollment.");
1183
#endif
1184
}
1185

1186
static bool same_entry(uint16_t id, sd_id128_t uuid, const char *path) {
58✔
1187
        _cleanup_free_ char *opath = NULL;
58✔
1188
        sd_id128_t ouuid;
58✔
1189
        int r;
58✔
1190

1191
        r = efi_get_boot_option(id, NULL, &ouuid, &opath, NULL);
58✔
1192
        if (r < 0)
58✔
1193
                return false;
1194
        if (!sd_id128_equal(uuid, ouuid))
58✔
1195
                return false;
42✔
1196

1197
        /* Some motherboards convert the path to uppercase under certain circumstances
1198
         * (e.g. after booting into the Boot Menu in the ASUS ROG STRIX B350-F GAMING),
1199
         * so use case-insensitive checking */
1200
        if (!strcaseeq_ptr(path, opath))
16✔
1201
                return false;
9✔
1202

1203
        return true;
1204
}
1205

1206
static int find_slot(sd_id128_t uuid, const char *path, uint16_t *id) {
16✔
1207
        _cleanup_free_ uint16_t *options = NULL;
16✔
1208

1209
        assert(id);
16✔
1210

1211
        int n = efi_get_boot_options(&options);
16✔
1212
        if (n < 0)
16✔
1213
                return n;
1214

1215
        /* find already existing systemd-boot entry */
1216
        for (int i = 0; i < n; i++)
67✔
1217
                if (same_entry(options[i], uuid, path)) {
58✔
1218
                        *id = options[i];
7✔
1219
                        return 1;
7✔
1220
                }
1221

1222
        /* find free slot in the sorted BootXXXX variable list */
1223
        for (int i = 0; i < n; i++)
39✔
1224
                if (i != options[i]) {
30✔
1225
                        *id = i;
×
1226
                        return 0;
×
1227
                }
1228

1229
        /* use the next one */
1230
        if (n == 0xffff)
9✔
1231
                return -ENOSPC;
1232
        *id = n;
9✔
1233
        return 0;
9✔
1234
}
1235

1236
static int insert_into_order(InstallContext *c, uint16_t slot) {
13✔
1237
        _cleanup_free_ uint16_t *order = NULL;
13✔
1238
        uint16_t *t;
13✔
1239
        int n;
13✔
1240

1241
        assert(c);
13✔
1242

1243
        n = efi_get_boot_order(&order);
13✔
1244
        if (n <= 0)
13✔
1245
                /* no entry, add us */
1246
                return efi_set_boot_order(&slot, 1);
×
1247

1248
        /* are we the first and only one? */
1249
        if (n == 1 && order[0] == slot)
13✔
1250
                return 0;
1251

1252
        /* are we already in the boot order? */
1253
        for (int i = 0; i < n; i++) {
31✔
1254
                if (order[i] != slot)
25✔
1255
                        continue;
18✔
1256

1257
                /* we do not require to be the first one, all is fine */
1258
                if (c->operation != INSTALL_NEW)
7✔
1259
                        return 0;
1260

1261
                /* move us to the first slot */
1262
                memmove(order + 1, order, i * sizeof(uint16_t));
3✔
1263
                order[0] = slot;
3✔
1264
                return efi_set_boot_order(order, n);
3✔
1265
        }
1266

1267
        /* extend array */
1268
        t = reallocarray(order, n + 1, sizeof(uint16_t));
6✔
1269
        if (!t)
6✔
1270
                return -ENOMEM;
1271
        order = t;
6✔
1272

1273
        /* add us to the top or end of the list */
1274
        if (c->operation != INSTALL_NEW) {
6✔
1275
                memmove(order + 1, order, n * sizeof(uint16_t));
6✔
1276
                order[0] = slot;
6✔
1277
        } else
1278
                order[n] = slot;
×
1279

1280
        return efi_set_boot_order(order, n + 1);
6✔
1281
}
1282

1283
static int remove_from_order(uint16_t slot) {
×
1284
        _cleanup_free_ uint16_t *order = NULL;
×
1285
        int n;
×
1286

1287
        n = efi_get_boot_order(&order);
×
1288
        if (n <= 0)
×
1289
                return n;
1290

1291
        for (int i = 0; i < n; i++) {
×
1292
                if (order[i] != slot)
×
1293
                        continue;
×
1294

1295
                if (i + 1 < n)
×
1296
                        memmove(order + i, order + i+1, (n - i) * sizeof(uint16_t));
×
1297
                return efi_set_boot_order(order, n - 1);
×
1298
        }
1299

1300
        return 0;
1301
}
1302

1303
static int pick_efi_boot_option_description(int esp_fd, char **ret) {
9✔
1304
        int r;
9✔
1305

1306
        assert(esp_fd >= 0);
9✔
1307
        assert(ret);
9✔
1308

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

1313
        const char *b = arg_efi_boot_option_description ?: "Linux Boot Manager";
9✔
1314
        if (!arg_efi_boot_option_description_with_device)
9✔
1315
                goto fallback;
9✔
1316

1317
        r = block_device_new_from_fd(
×
1318
                        esp_fd,
1319
                        BLOCK_DEVICE_LOOKUP_WHOLE_DISK|BLOCK_DEVICE_LOOKUP_BACKING,
1320
                        &d);
1321
        if (r < 0) {
×
1322
                log_debug_errno(r, "Failed to find backing device of ESP: %m");
×
1323
                goto fallback;
×
1324
        }
1325

1326
        const char *serial;
×
1327
        r = sd_device_get_property_value(d, "ID_SERIAL", &serial);
×
1328
        if (r < 0) {
×
1329
                log_debug_errno(r, "Unable to read ID_SERIAL field of backing device of ESP: %m");
×
1330
                goto fallback;
×
1331
        }
1332

1333
        j = strjoin(b, " (", serial, ")");
×
1334
        if (!j)
×
1335
                return log_oom();
×
1336

1337
        if (strlen(j) > EFI_BOOT_OPTION_DESCRIPTION_MAX) {
×
1338
                log_debug("Boot option string suffixed with device serial would be too long, skipping: %s", j);
×
1339
                j = mfree(j);
×
1340
                goto fallback;
×
1341
        }
1342

1343
        *ret = TAKE_PTR(j);
×
1344
        return 0;
×
1345

1346
fallback:
9✔
1347
        j = strdup(b);
9✔
1348
        if (!j)
9✔
1349
                return log_oom();
×
1350

1351
        *ret = TAKE_PTR(j);
9✔
1352
        return 0;
9✔
1353
}
1354

1355
static int install_variables(
13✔
1356
                InstallContext *c,
1357
                const char *path) {
1358

1359
        uint16_t slot;
13✔
1360
        int r;
13✔
1361

1362
        assert(c);
13✔
1363

1364
        if (c->esp_fd < 0)
13✔
1365
                return c->esp_fd;
13✔
1366

1367
        _cleanup_free_ char *j = path_join(c->root, c->esp_path);
26✔
1368
        if (!j)
13✔
1369
                return log_oom();
×
1370

1371
        r = chase_and_accessat(
13✔
1372
                        c->esp_fd,
1373
                        c->esp_fd,
1374
                        path,
1375
                        CHASE_PROHIBIT_SYMLINKS|CHASE_MUST_BE_REGULAR,
1376
                        F_OK,
1377
                        /* ret_path= */ NULL);
1378
        if (r == -ENOENT)
13✔
1379
                return 0;
1380
        if (r < 0)
13✔
1381
                return log_error_errno(r, "Cannot access \"%s/%s\": %m", j, skip_leading_slash(path));
×
1382

1383
        r = find_slot(c->esp_uuid, path, &slot);
13✔
1384
        if (r < 0) {
13✔
1385
                int level = c->graceful ? arg_quiet ? LOG_DEBUG : LOG_INFO : LOG_ERR;
×
1386
                const char *skip = c->graceful ? ", skipping" : "";
×
1387

1388
                log_full_errno(level, r,
×
1389
                               r == -ENOENT ?
1390
                               "Failed to access EFI variables%s. Is the \"efivarfs\" filesystem mounted?" :
1391
                               "Failed to determine current boot order%s: %m", skip);
1392

1393
                return c->graceful ? 0 : r;
×
1394
        }
1395

1396
        bool existing = r > 0;
13✔
1397

1398
        if (c->operation == INSTALL_NEW || !existing) {
13✔
1399
                _cleanup_free_ char *description = NULL;
9✔
1400

1401
                r = pick_efi_boot_option_description(c->esp_fd, &description);
9✔
1402
                if (r < 0)
9✔
1403
                        return r;
1404

1405
                r = efi_add_boot_option(
9✔
1406
                                slot,
1407
                                description,
1408
                                c->esp_part,
1409
                                c->esp_pstart,
1410
                                c->esp_psize,
1411
                                c->esp_uuid,
1412
                                path);
1413
                if (r < 0) {
9✔
1414
                        int level = c->graceful ? arg_quiet ? LOG_DEBUG : LOG_INFO : LOG_ERR;
×
1415
                        const char *skip = c->graceful ? ", skipping" : "";
×
1416

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

1419
                        return c->graceful ? 0 : r;
×
1420
                }
1421

1422
                log_info("%s EFI boot entry \"%s\".",
15✔
1423
                         existing ? "Updated" : "Created",
1424
                         description);
1425
        }
1426

1427
        return insert_into_order(c, slot);
13✔
1428
}
1429

1430
static int are_we_installed(InstallContext *c) {
139✔
1431
        int r;
139✔
1432

1433
        assert(c);
139✔
1434

1435
        /* Tests whether systemd-boot is installed. It's not obvious what to use as check here: we could
1436
         * check EFI variables, we could check what binary /EFI/BOOT/BOOT*.EFI points to, or whether the
1437
         * loader entries directory exists. Here we opted to check whether /EFI/systemd/ is non-empty, which
1438
         * should be a suitable and very minimal check for a number of reasons:
1439
         *
1440
         *  → The check is architecture independent (i.e. we check if any systemd-boot loader is installed,
1441
         *    not a specific one.)
1442
         *
1443
         *  → It doesn't assume we are the only boot loader (i.e doesn't check if we own the main
1444
         *    /EFI/BOOT/BOOT*.EFI fallback binary.
1445
         *
1446
         *  → It specifically checks for systemd-boot, not for other boot loaders (which a check for
1447
         *    /boot/loader/entries would do). */
1448

1449
        _cleanup_free_ char *p = path_join(c->esp_path, "/EFI/systemd");
278✔
1450
        if (!p)
139✔
1451
                return log_oom();
×
1452

1453
        if (c->esp_fd < 0)
139✔
1454
                return c->esp_fd;
1455

1456
        _cleanup_close_ int fd = chase_and_openat(
278✔
1457
                        c->esp_fd,
1458
                        c->esp_fd,
1459
                        "/EFI/systemd",
1460
                        CHASE_PROHIBIT_SYMLINKS|CHASE_MUST_BE_DIRECTORY,
1461
                        O_RDONLY|O_CLOEXEC|O_DIRECTORY,
1462
                        /* ret_path= */ NULL);
1463
        if (fd == -ENOENT)
139✔
1464
                return 0;
1465
        if (fd < 0)
132✔
1466
                return log_error_errno(fd, "Failed to open '%s': %m", p);
×
1467

1468
        log_debug("Checking whether '%s' contains any files%s", p, glyph(GLYPH_ELLIPSIS));
251✔
1469
        r = dir_is_empty_at(fd, /* path= */ NULL, /* ignore_hidden_or_backup= */ false);
132✔
1470
        if (r < 0 && r != -ENOENT)
132✔
1471
                return log_error_errno(r, "Failed to check whether '%s' contains any files: %m", p);
×
1472

1473
        return r == 0;
132✔
1474
}
1475

1476
#if HAVE_OPENSSL
1477
static int load_secure_boot_auto_enroll(
138✔
1478
                X509 **ret_certificate,
1479
                EVP_PKEY **ret_private_key,
1480
                OpenSSLAskPasswordUI **ret_ui) {
1481

1482
        int r;
138✔
1483

1484
        assert(ret_certificate);
138✔
1485
        assert(ret_private_key);
138✔
1486
        assert(ret_ui);
138✔
1487

1488
        if (!arg_secure_boot_auto_enroll) {
138✔
1489
                *ret_certificate = NULL;
137✔
1490
                *ret_private_key = NULL;
137✔
1491
                return 0;
138✔
1492
        }
1493

1494
        if (arg_certificate_source_type == OPENSSL_CERTIFICATE_SOURCE_FILE) {
1✔
1495
                r = parse_path_argument(arg_certificate, /* suppress_root= */ false, &arg_certificate);
1✔
1496
                if (r < 0)
1✔
1497
                        return r;
1498
        }
1499

1500
        _cleanup_(X509_freep) X509 *certificate = NULL;
1✔
1501
        r = openssl_load_x509_certificate(
1✔
1502
                        arg_certificate_source_type,
1503
                        arg_certificate_source,
1504
                        arg_certificate,
1505
                        &certificate);
1506
        if (r < 0)
1✔
1507
                return log_error_errno(r, "Failed to load X.509 certificate from %s: %m", arg_certificate);
×
1508

1509
        if (arg_private_key_source_type == OPENSSL_KEY_SOURCE_FILE) {
1✔
1510
                r = parse_path_argument(arg_private_key, /* suppress_root= */ false, &arg_private_key);
1✔
1511
                if (r < 0)
1✔
1512
                        return log_error_errno(r, "Failed to parse private key path %s: %m", arg_private_key);
×
1513
        }
1514

1515
        r = openssl_load_private_key(
2✔
1516
                        arg_private_key_source_type,
1517
                        arg_private_key_source,
1518
                        arg_private_key,
1519
                        &(AskPasswordRequest) {
1✔
1520
                                .tty_fd = -EBADF,
1521
                                .id = "bootctl-private-key-pin",
1522
                                .keyring = arg_private_key,
1523
                                .credential = "bootctl.private-key-pin",
1524
                                .until = USEC_INFINITY,
1525
                                .hup_fd = -EBADF,
1526
                        },
1527
                        ret_private_key,
1528
                        ret_ui);
1529
        if (r < 0)
1✔
1530
                return log_error_errno(r, "Failed to load private key from %s: %m", arg_private_key);
×
1531

1532
        *ret_certificate = TAKE_PTR(certificate);
1✔
1533

1534
        return 0;
1✔
1535
}
1536
#endif
1537

1538
static int run_install(InstallContext *c) {
138✔
1539
        int r;
138✔
1540

1541
        assert(c);
138✔
1542
        assert(c->operation >= 0);
138✔
1543

1544
        if (c->operation == INSTALL_UPDATE) {
138✔
1545
                /* If we are updating, don't do anything if sd-boot wasn't actually installed. */
1546
                r = are_we_installed(c);
125✔
1547
                if (r < 0)
125✔
1548
                        return r;
138✔
1549
                if (r == 0) {
125✔
1550
                        log_debug("Skipping update because sd-boot is not installed in the ESP.");
×
1551
                        return 0;
×
1552
                }
1553
        }
1554

1555
        r = settle_make_entry_directory(c);
138✔
1556
        if (r < 0)
138✔
1557
                return r;
1558

1559
        const char *arch = arg_arch_all ? "" : get_efi_arch();
138✔
1560

1561
        if (c->esp_fd < 0)
138✔
1562
                return c->esp_fd;
1563

1564
        _cleanup_free_ char *j = path_join(c->root, c->esp_path);
276✔
1565
        if (!j)
138✔
1566
                return log_oom();
×
1567

1568
        int dollar_boot_fd = acquire_dollar_boot_fd(c);
138✔
1569
        if (dollar_boot_fd < 0)
138✔
1570
                return dollar_boot_fd;
1571

1572
        _cleanup_free_ char *w = path_join(c->root, dollar_boot_path(c));
276✔
1573
        if (!w)
138✔
1574
                return log_oom();
×
1575

1576
        WITH_UMASK(0002) {
276✔
1577
                if (c->operation == INSTALL_NEW) {
138✔
1578
                        /* Don't create any of these directories when we are just updating. When we update
1579
                         * we'll drop-in our files (unless there are newer ones already), but we won't create
1580
                         * the directories for them in the first place. */
1581

1582
                        r = create_subdirs(j, c->esp_fd, esp_subdirs);
13✔
1583
                        if (r < 0)
13✔
1584
                                return r;
1585

1586
                        r = create_subdirs(w, dollar_boot_fd, dollar_boot_subdirs);
13✔
1587
                        if (r < 0)
13✔
1588
                                return r;
1589
                }
1590

1591
                r = install_binaries(c, arch);
138✔
1592
                if (r < 0)
138✔
1593
                        return r;
1594

1595
                if (c->operation == INSTALL_NEW) {
135✔
1596
                        r = install_loader_config(c);
13✔
1597
                        if (r < 0)
13✔
1598
                                return r;
1599

1600
                        r = install_entry_directory(c);
13✔
1601
                        if (r < 0)
13✔
1602
                                return r;
1603

1604
                        r = install_entry_token(c);
13✔
1605
                        if (r < 0)
13✔
1606
                                return r;
1607

1608
                        if (arg_install_random_seed && !c->root) {
13✔
1609
                                r = install_random_seed(c->esp_path, c->esp_fd);
5✔
1610
                                if (r < 0)
5✔
1611
                                        return r;
1612
                        }
1613

1614
                        r = install_secure_boot_auto_enroll(c);
13✔
1615
                        if (r < 0)
13✔
1616
                                return r;
1617
                }
1618

1619
                r = install_loader_specification(c);
135✔
1620
                if (r < 0)
135✔
1621
                        return r;
1622
        }
1623

1624
        (void) sync_everything();
135✔
1625

1626
        if (!should_touch_install_variables(c))
135✔
1627
                return 0;
1628

1629
        if (arg_arch_all) {
15✔
1630
                log_info("Not changing EFI variables with --all-architectures.");
2✔
1631
                return 0;
2✔
1632
        }
1633

1634
        char *path = strjoina("/EFI/systemd/systemd-boot", arch, ".efi");
91✔
1635
        return install_variables(c, path);
13✔
1636
}
1637

1638
int verb_install(int argc, char *argv[], uintptr_t _data, void *userdata) {
138✔
1639
        int r;
138✔
1640

1641
        /* Invoked for both "update" and "install" */
1642

1643
        _cleanup_(install_context_done) InstallContext c = INSTALL_CONTEXT_NULL;
×
1644
        r = install_context_from_cmdline(&c, streq(argv[0], "install") ? INSTALL_NEW : INSTALL_UPDATE);
138✔
1645
        if (r < 0)
138✔
1646
                return r;
1647
        if (r == 0) {
138✔
1648
                log_debug("No ESP found and operating in graceful mode, skipping.");
×
1649
                return 0;
×
1650
        }
1651

1652
#if HAVE_OPENSSL
1653
        _cleanup_(openssl_ask_password_ui_freep) OpenSSLAskPasswordUI *ui = NULL;
138✔
1654
        r = load_secure_boot_auto_enroll(&c.secure_boot_certificate, &c.secure_boot_private_key, &ui);
138✔
1655
        if (r < 0)
138✔
1656
                return r;
1657
#endif
1658

1659
        return run_install(&c);
138✔
1660
}
1661

1662
static int remove_boot_efi(InstallContext *c) {
13✔
1663
        int r, n = 0;
13✔
1664

1665
        assert(c);
13✔
1666

1667
        if (c->esp_fd < 0)
13✔
1668
                return c->esp_fd;
13✔
1669

1670
        _cleanup_free_ char *w = path_join(c->root, c->esp_path);
26✔
1671
        if (!w)
13✔
1672
                return log_oom();
×
1673

1674
        _cleanup_closedir_ DIR *d = NULL;
13✔
1675
        _cleanup_free_ char *p = NULL;
13✔
1676
        r = chase_and_opendirat(
13✔
1677
                        c->esp_fd,
1678
                        c->esp_fd,
1679
                        "/EFI/BOOT",
1680
                        CHASE_PROHIBIT_SYMLINKS|CHASE_MUST_BE_DIRECTORY,
1681
                        &p,
1682
                        &d);
1683
        if (r == -ENOENT)
13✔
1684
                return 0;
1685
        if (r < 0)
13✔
1686
                return log_error_errno(r, "Failed to open directory \"%s/EFI/BOOT\": %m", w);
×
1687

1688
        _cleanup_free_ char *j = path_join(w, p);
26✔
1689
        if (!j)
13✔
1690
                return log_oom();
×
1691

1692
        FOREACH_DIRENT(de, d, break) {
61✔
1693
                _cleanup_close_ int fd = -EBADF;
22✔
1694

1695
                if (!endswith_no_case(de->d_name, ".efi"))
22✔
1696
                        continue;
×
1697

1698
                _cleanup_free_ char *z = path_join(j, de->d_name);
31✔
1699
                if (!z)
22✔
1700
                        return log_oom();
×
1701

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

1706
                r = pe_is_native_fd(fd);
22✔
1707
                if (r < 0) {
22✔
1708
                        log_warning_errno(r, "Failed to detect if '%s' is native architecture, ignoring: %m", z);
×
1709
                        continue;
×
1710
                }
1711
                if (r == 0)
22✔
1712
                        continue;
9✔
1713

1714
                _cleanup_free_ char *v = NULL;
13✔
1715
                r = get_file_version(fd, &v);
13✔
1716
                if (r == -ESRCH)
13✔
1717
                        continue;  /* No version information */
×
1718
                if (r < 0)
13✔
1719
                        return r;
1720
                if (!startswith(v, "systemd-boot "))
13✔
1721
                        continue;
×
1722

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

1726
                log_info("Removed '%s'.", z);
13✔
1727

1728
                n++;
13✔
1729
        }
1730

1731
        log_debug("Removed %i EFI binaries from '%s'.", n, j);
13✔
1732
        return n;
1733
}
1734

1735
static int unlink_inode(const char *root, int root_fd, const char *path, mode_t type) {
271✔
1736
        int r;
271✔
1737

1738
        assert(root);
271✔
1739
        assert(root_fd >= 0);
271✔
1740
        assert(path);
271✔
1741
        assert(IN_SET(type, S_IFREG, S_IFDIR));
271✔
1742

1743
        _cleanup_free_ char *p = path_join(empty_to_root(root), path);
542✔
1744
        if (!p)
271✔
1745
                return log_oom();
×
1746

1747
        r = chase_and_unlinkat(
271✔
1748
                        root_fd,
1749
                        root_fd,
1750
                        path,
1751
                        CHASE_PROHIBIT_SYMLINKS,
1752
                        S_ISDIR(type) ? AT_REMOVEDIR : 0,
271✔
1753
                        /* ret_path= */ NULL);
1754
        if (r < 0) {
271✔
1755
                bool ignore = IN_SET(r, -ENOENT, -ENOTEMPTY);
152✔
1756
                log_full_errno(ignore ? LOG_DEBUG : LOG_ERR, r, "Failed to remove '%s': %m", p);
152✔
1757
                return ignore ? 0 : r;
152✔
1758
        }
1759

1760
        log_info("Removed %s\"%s\".", S_ISDIR(type) ? "directory " : "", p);
155✔
1761
        return 0;
1762
}
1763

1764
static int remove_subdirs(const char *root, int root_fd, const char *const *subdirs) {
34✔
1765
        int r = 0;
34✔
1766

1767
        assert(root);
34✔
1768
        assert(root_fd);
34✔
1769

1770
        STRV_FOREACH_BACKWARDS(i, (char**) subdirs)
332✔
1771
                RET_GATHER(r, unlink_inode(root, root_fd, *i, S_IFDIR));
149✔
1772

1773
        return r;
34✔
1774
}
1775

1776
static int remove_entry_directory(InstallContext *c, const char *path, int fd) {
21✔
1777
        assert(c);
21✔
1778
        assert(c->make_entry_directory >= 0);
21✔
1779
        assert(path);
21✔
1780
        assert(fd >= 0);
21✔
1781

1782
        if (!c->make_entry_directory || !c->entry_token)
21✔
1783
                return 0;
1784

1785
        return unlink_inode(path, fd, c->entry_token, S_IFDIR);
10✔
1786
}
1787

1788
static int remove_binaries(InstallContext *c) {
13✔
1789
        int r;
13✔
1790

1791
        _cleanup_free_ char *p = path_join(c->root, "/EFI/systemd");
26✔
1792
        if (!p)
13✔
1793
                return log_oom();
×
1794

1795
        _cleanup_close_ int efi_fd = -EBADF;
13✔
1796
        r = chaseat(c->esp_fd,
13✔
1797
                    c->esp_fd,
1798
                    "EFI",
1799
                    CHASE_PROHIBIT_SYMLINKS|CHASE_MUST_BE_DIRECTORY,
1800
                    /* ret_path= */ NULL,
1801
                    &efi_fd);
1802
        if (r < 0) {
13✔
1803
                if (r != -ENOENT)
×
1804
                        return log_error_errno(r, "Failed to remove '%s': %m", p);
×
1805

1806
                r = 0;
1807
        } else
1808
                r = rm_rf_at(efi_fd, "systemd", REMOVE_ROOT|REMOVE_PHYSICAL|REMOVE_MISSING_OK);
13✔
1809

1810
        return RET_GATHER(r, remove_boot_efi(c));
13✔
1811
}
1812

1813
static int remove_variables(sd_id128_t uuid, const char *path, bool in_order) {
3✔
1814
        uint16_t slot;
3✔
1815
        int r;
3✔
1816

1817
        r = find_slot(uuid, path, &slot);
3✔
1818
        if (r != 1)
3✔
1819
                return 0;
3✔
1820

1821
        r = efi_remove_boot_option(slot);
×
1822
        if (r < 0)
×
1823
                return r;
1824

1825
        if (in_order)
×
1826
                return remove_from_order(slot);
×
1827

1828
        return 0;
1829
}
1830

1831
static int remove_loader_variables(void) {
3✔
1832
        int r = 0;
3✔
1833

1834
        /* Remove all persistent loader variables we define */
1835

1836
        FOREACH_STRING(var,
30✔
1837
                       EFI_LOADER_VARIABLE_STR("LoaderConfigConsoleMode"),
1838
                       EFI_LOADER_VARIABLE_STR("LoaderConfigTimeout"),
1839
                       EFI_LOADER_VARIABLE_STR("LoaderConfigTimeoutOneShot"),
1840
                       EFI_LOADER_VARIABLE_STR("LoaderEntryPreferred"),
1841
                       EFI_LOADER_VARIABLE_STR("LoaderEntryDefault"),
1842
                       EFI_LOADER_VARIABLE_STR("LoaderEntrySysFail"),
1843
                       EFI_LOADER_VARIABLE_STR("LoaderEntryLastBooted"),
1844
                       EFI_LOADER_VARIABLE_STR("LoaderEntryOneShot"),
1845
                       EFI_LOADER_VARIABLE_STR("LoaderSystemToken")) {
1846

1847
                int q;
27✔
1848

1849
                q = efi_set_variable(var, NULL, 0);
27✔
1850
                if (q == -ENOENT)
27✔
1851
                        continue;
24✔
1852
                if (q < 0)
3✔
1853
                        RET_GATHER(r, log_warning_errno(q, "Failed to remove EFI variable %s: %m", var));
×
1854
                else
1855
                        log_info("Removed EFI variable %s.", var);
3✔
1856
        }
1857

1858
        return r;
3✔
1859
}
1860

1861
int verb_remove(int argc, char *argv[], uintptr_t _data, void *userdata) {
13✔
1862
        sd_id128_t uuid = SD_ID128_NULL;
13✔
1863
        int r;
13✔
1864

1865
        _cleanup_(install_context_done) InstallContext c = INSTALL_CONTEXT_NULL;
13✔
1866
        r = install_context_from_cmdline(&c, INSTALL_REMOVE);
13✔
1867
        if (r < 0)
13✔
1868
                return r;
1869
        if (r == 0) {
13✔
1870
                log_debug("No ESP found and operating in graceful mode, skipping.");
×
1871
                return 0;
×
1872
        }
1873

1874
        r = settle_make_entry_directory(&c);
13✔
1875
        if (r < 0)
13✔
1876
                return r;
1877

1878
        if (c.esp_fd < 0)
13✔
1879
                return c.esp_fd;
1880

1881
        _cleanup_free_ char *j = path_join(c.root, c.esp_path);
26✔
1882
        if (!j)
13✔
1883
                return log_oom();
×
1884

1885
        int dollar_boot_fd = acquire_dollar_boot_fd(&c);
13✔
1886
        if (dollar_boot_fd < 0)
13✔
1887
                return dollar_boot_fd;
1888

1889
        _cleanup_free_ char *w = path_join(c.root, dollar_boot_path(&c));
26✔
1890
        if (!w)
13✔
1891
                return log_oom();
×
1892

1893
        r = remove_binaries(&c);
13✔
1894
        RET_GATHER(r, unlink_inode(j, c.esp_fd, "/loader/loader.conf", S_IFREG));
13✔
1895
        RET_GATHER(r, unlink_inode(j, c.esp_fd, "/loader/random-seed", S_IFREG));
13✔
1896
        RET_GATHER(r, unlink_inode(j, c.esp_fd, "/loader/entries.srel", S_IFREG));
13✔
1897

1898
        FOREACH_STRING(db, "PK.auth", "KEK.auth", "db.auth") {
52✔
1899
                _cleanup_free_ char *p = path_join("/loader/keys/auto", db);
78✔
1900
                if (!p)
39✔
1901
                        return log_oom();
×
1902

1903
                RET_GATHER(r, unlink_inode(j, c.esp_fd, p, S_IFREG));
39✔
1904
        }
1905
        RET_GATHER(r, unlink_inode(j, c.esp_fd, "/loader/keys/auto", S_IFDIR));
13✔
1906
        RET_GATHER(r, unlink_inode(j, c.esp_fd, "/loader/entries.srel", S_IFREG));
13✔
1907

1908
        RET_GATHER(r, remove_subdirs(j, c.esp_fd, esp_subdirs));
13✔
1909
        RET_GATHER(r, remove_subdirs(j, c.esp_fd, dollar_boot_subdirs));
13✔
1910
        RET_GATHER(r, remove_entry_directory(&c, j, c.esp_fd));
13✔
1911

1912
        if (c.xbootldr_fd >= 0) {
13✔
1913
                /* Remove a subset of these also from the XBOOTLDR partition if it exists */
1914
                RET_GATHER(r, unlink_inode(w, c.xbootldr_fd, "/loader/entries.srel", S_IFREG));
8✔
1915
                RET_GATHER(r, remove_subdirs(w, c.xbootldr_fd, dollar_boot_subdirs));
8✔
1916
                RET_GATHER(r, remove_entry_directory(&c, w, c.xbootldr_fd));
8✔
1917
        }
1918

1919
        (void) sync_everything();
13✔
1920

1921
        if (!should_touch_install_variables(&c))
13✔
1922
                return r;
1923

1924
        if (arg_arch_all) {
5✔
1925
                log_info("Not changing EFI variables with --all-architectures.");
2✔
1926
                return r;
2✔
1927
        }
1928

1929
        char *path = strjoina("/EFI/systemd/systemd-boot", get_efi_arch(), ".efi");
21✔
1930
        RET_GATHER(r, remove_variables(uuid, path, /* in_order= */ true));
3✔
1931
        return RET_GATHER(r, remove_loader_variables());
3✔
1932
}
1933

1934
int verb_is_installed(int argc, char *argv[], uintptr_t _data, void *userdata) {
14✔
1935
        int r;
14✔
1936

1937
        _cleanup_(install_context_done) InstallContext c = INSTALL_CONTEXT_NULL;
14✔
1938
        r = install_context_from_cmdline(&c, INSTALL_TEST);
14✔
1939
        if (r < 0)
14✔
1940
                return r;
1941
        if (r == 0) {
14✔
1942
                log_debug("No ESP found and operating in graceful mode, claiming not installed.");
×
1943
                if (!arg_quiet)
×
1944
                        puts("no");
×
1945
                return EXIT_FAILURE;
×
1946
        }
1947

1948
        r = are_we_installed(&c);
14✔
1949
        if (r < 0)
14✔
1950
                return r;
1951

1952
        if (r > 0) {
14✔
1953
                if (!arg_quiet)
7✔
1954
                        puts("yes");
7✔
1955
                return EXIT_SUCCESS;
7✔
1956
        } else {
1957
                if (!arg_quiet)
7✔
1958
                        puts("no");
7✔
1959
                return EXIT_FAILURE;
7✔
1960
        }
1961
}
1962

1963
static JSON_DISPATCH_ENUM_DEFINE(json_dispatch_install_operation, InstallOperation, install_operation_from_string);
×
1964
static JSON_DISPATCH_ENUM_DEFINE(json_dispatch_boot_entry_token_type, BootEntryTokenType, boot_entry_token_type_from_string);
×
1965

1966
typedef struct InstallParameters {
1967
        InstallContext context;
1968
        unsigned root_fd_index;
1969
} InstallParameters;
1970

1971
static void install_parameters_done(InstallParameters *p) {
×
1972
        assert(p);
×
1973

1974
        install_context_done(&p->context);
×
1975
}
×
1976

1977
int vl_method_install(
×
1978
                sd_varlink *link,
1979
                sd_json_variant *parameters,
1980
                sd_varlink_method_flags_t flags,
1981
                void *userdata) {
1982

1983
        int r;
×
1984

1985
        assert(link);
×
1986

1987
        _cleanup_(install_parameters_done) InstallParameters p = {
×
1988
                .context = INSTALL_CONTEXT_NULL,
1989
                .root_fd_index = UINT_MAX,
1990
        };
1991

1992
        static const sd_json_dispatch_field dispatch_table[] = {
×
1993
                { "operation",          SD_JSON_VARIANT_STRING,        json_dispatch_install_operation,     voffsetof(p, context.operation),        SD_JSON_MANDATORY },
1994
                { "graceful",           SD_JSON_VARIANT_BOOLEAN,       sd_json_dispatch_stdbool,            voffsetof(p, context.graceful),         0                 },
1995
                { "rootFileDescriptor", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint,               voffsetof(p, root_fd_index),            0                 },
1996
                { "rootDirectory",      SD_JSON_VARIANT_STRING,        json_dispatch_path,                  voffsetof(p, context.root),             0                 },
1997
                { "bootEntryTokenType", SD_JSON_VARIANT_STRING,        json_dispatch_boot_entry_token_type, voffsetof(p, context.entry_token_type), 0                 },
1998
                { "touchVariables",     SD_JSON_VARIANT_BOOLEAN,       sd_json_dispatch_tristate,           voffsetof(p, context.touch_variables),  0                 },
1999
                {},
2000
        };
2001

2002
        r = sd_varlink_dispatch(link, parameters, dispatch_table, &p);
×
2003
        if (r != 0)
×
2004
                return r;
2005

2006
        if (!IN_SET(p.context.operation, INSTALL_NEW, INSTALL_UPDATE))
×
2007
                return sd_varlink_error_invalid_parameter_name(link, "operation");
×
2008

2009
        if (p.root_fd_index != UINT_MAX) {
×
2010
                p.context.root_fd = sd_varlink_peek_dup_fd(link, p.root_fd_index);
×
2011
                if (p.context.root_fd < 0)
×
2012
                        return log_debug_errno(p.context.root_fd, "Failed to acquire root fd from Varlink: %m");
×
2013

2014
                r = fd_verify_safe_flags_full(p.context.root_fd, O_DIRECTORY);
×
2015
                if (r < 0)
×
2016
                        return sd_varlink_error_invalid_parameter_name(link, "rootFileDescriptor");
×
2017

2018
                r = fd_verify_directory(p.context.root_fd);
×
2019
                if (r < 0)
×
2020
                        return log_debug_errno(r, "Specified file descriptor does not refer to a directory: %m");
×
2021

2022
                if (!p.context.root) {
×
2023
                        r = fd_get_path(p.context.root_fd, &p.context.root);
×
2024
                        if (r < 0)
×
2025
                                return log_debug_errno(r, "Failed to get path of file descriptor: %m");
×
2026

2027
                        if (empty_or_root(p.context.root))
×
2028
                                p.context.root = mfree(p.context.root);
×
2029
                }
2030
        } else if (p.context.root) {
×
2031
                p.context.root_fd = open(p.context.root, O_RDONLY|O_CLOEXEC|O_DIRECTORY);
×
2032
                if (p.context.root_fd < 0)
×
2033
                        return log_debug_errno(errno, "Failed to open '%s': %m", p.context.root);
×
2034
        } else
2035
                p.context.root_fd = XAT_FDROOT;
×
2036

2037
        if (p.context.entry_token_type < 0)
×
2038
                p.context.entry_token_type = BOOT_ENTRY_TOKEN_AUTO;
×
2039

2040
        r = find_esp_and_warn_at_full(
×
2041
                        p.context.root_fd,
2042
                        /* path= */ NULL,
2043
                        /* unprivileged_mode= */ false,
2044
                        &p.context.esp_path,
2045
                        &p.context.esp_fd,
2046
                        &p.context.esp_part,
2047
                        &p.context.esp_pstart,
2048
                        &p.context.esp_psize,
2049
                        &p.context.esp_uuid,
2050
                        /* ret_devid= */ NULL);
2051
        if (r == -ENOKEY)
×
2052
                return sd_varlink_error(link, "io.systemd.BootControl.NoESPFound", NULL);
×
2053
        if (r < 0)
×
2054
                return r;
2055

2056
        r = find_xbootldr_and_warn_at(
×
2057
                        p.context.root_fd,
2058
                        /* path= */ NULL,
2059
                        /* unprivileged_mode= */ false,
2060
                        &p.context.xbootldr_path,
2061
                        &p.context.xbootldr_fd);
2062
        if (r == -ENOKEY)
×
2063
                log_debug_errno(r, "Didn't find an XBOOTLDR partition, using ESP as $BOOT.");
×
2064
        else if (r < 0)
×
2065
                return r;
2066

2067
        r = run_install(&p.context);
×
2068
        if (r == -EUNATCH) /* no boot entry token is set */
×
2069
                return sd_varlink_error(link, "io.systemd.BootControl.BootEntryTokenUnavailable", NULL);
×
2070
        if (r < 0)
×
2071
                return r;
2072

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