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

systemd / systemd / 21846209963

09 Feb 2026 03:52PM UTC coverage: 72.697% (-0.02%) from 72.716%
21846209963

push

github

daandemeyer
meson: guard symlinks in sysconfdir behind install_sysconfidr

Symlinks to files inside sysconfdir are now only installed if
ìnstall_sysconfdir=true (which is the default).

If sshconfdir,sshdconfdir,shellprofiledir are not inside sysconfdir and
install_sysconfidr=false, these symlinks are still installed to the
configured directory.

311951 of 429113 relevant lines covered (72.7%)

1156102.48 hits per line

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

74.98
/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);
83✔
198

199
                if (arg_root) {
83✔
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);
55✔
207
                if (r < 0)
83✔
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) {
742✔
217
        int r;
742✔
218

219
        assert(c);
742✔
220

221
        if (c->esp_fd >= 0)
742✔
222
                return c->esp_fd;
742✔
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)
222✔
250
                return acquire_esp_fd(c);
147✔
251

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

256
        r = chaseat(c->root_fd,
150✔
257
                    c->xbootldr_path,
75✔
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)
75✔
262
                return log_error_errno(r, "Failed to open XBOOTLDR '%s': %m", j);
×
263

264
        return c->xbootldr_fd;
75✔
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
        if (lseek(source_fd, 0, SEEK_SET) < 0)
38✔
558
                return log_error_errno(errno, "Failed to seek in \"%s\": %m", source_path);
×
559

560
        r = copy_bytes(source_fd, write_fd, UINT64_MAX, COPY_REFLINK);
38✔
561
        if (r < 0)
38✔
562
                return log_error_errno(r, "Failed to copy data from \"%s\" to \"%s\": %m", source_path, dest_path);
×
563

564
        (void) copy_times(source_fd, write_fd, /* flags= */ 0);
38✔
565
        (void) fchmod(write_fd, 0644);
38✔
566

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

571
        t = mfree(t); /* disarm CLEANUP_TMPFILE_AT() */
38✔
572

573
        log_info("Copied \"%s\" to \"%s\".", source_path, dest_path);
38✔
574
        return 0;
575
}
576

577
static int mkdir_one(const char *root, int root_fd, const char *path) {
124✔
578
        int r;
124✔
579

580
        assert(root);
124✔
581
        assert(root_fd >= 0);
124✔
582
        assert(path);
124✔
583

584
        _cleanup_free_ char *p = path_join(empty_to_root(root), path);
248✔
585
        if (!p)
124✔
586
                return log_oom();
×
587

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

596
        log_info("Created directory \"%s\".", p);
124✔
597
        return 0;
598
}
599

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

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

619
static int create_subdirs(const char *root, int root_fd, const char * const *subdirs) {
26✔
620
        int r;
26✔
621

622
        assert(root);
26✔
623
        assert(root_fd >= 0);
26✔
624

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

631
        return 0;
632
}
633

634
static int update_efi_boot_binaries(
124✔
635
                InstallContext *c,
636
                const char *source_path,
637
                int source_fd,
638
                const char *ignore_filename) {
639

640
        int r, ret = 0;
124✔
641

642
        assert(c);
124✔
643
        assert(source_path);
124✔
644

645
        int esp_fd = acquire_esp_fd(c);
124✔
646
        if (esp_fd < 0)
124✔
647
                return esp_fd;
124✔
648

649
        _cleanup_free_ char *j = path_join(c->root, c->esp_path);
248✔
650
        if (!j)
124✔
651
                return log_oom();
×
652

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

665
        FOREACH_DIRENT(de, d, break) {
620✔
666
                _cleanup_close_ int fd = -EBADF;
620✔
667

668
                if (!endswith_no_case(de->d_name, ".efi"))
248✔
669
                        continue;
×
670

671
                if (strcaseeq_ptr(ignore_filename, de->d_name))
248✔
672
                        continue;
124✔
673

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

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

686
                _cleanup_free_ char *dest_path = path_join(j, "/EFI/BOOT", de->d_name);
×
687
                if (!dest_path)
×
688
                        return log_oom();
×
689

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

696
        return ret;
697
}
698

699
static int copy_one_file(
143✔
700
                InstallContext *c,
701
                const char *name,
702
                bool force) {
703

704
        int r, ret = 0;
143✔
705

706
        assert(c);
143✔
707

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

715
        _cleanup_free_ char *sp = path_join(BOOTLIBDIR, name);
286✔
716
        if (!sp)
143✔
717
                return log_oom();
×
718

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

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

744
        int esp_fd = acquire_esp_fd(c);
143✔
745
        if (esp_fd < 0)
143✔
746
                return esp_fd;
747

748
        _cleanup_free_ char *j = path_join(c->root, c->esp_path);
286✔
749
        if (!j)
143✔
750
                return log_oom();
×
751

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

761
        _cleanup_free_ char *dest_path = path_join(j, "/EFI/systemd", dest_name);
286✔
762
        if (!dest_path)
143✔
763
                return log_oom();
×
764

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

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

773
        const char *e = startswith(dest_name, "systemd-boot");
143✔
774
        if (e) {
143✔
775

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

781
                ascii_strupper(boot_dot_efi);
143✔
782

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

792
                _cleanup_free_ char *default_dest_path = path_join(j, "/EFI/BOOT", boot_dot_efi);
286✔
793
                if (!default_dest_path)
143✔
794
                        return log_oom();
×
795

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

800
                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✔
801

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

808
        return ret;
809
}
810

811
static int install_binaries(
137✔
812
                InstallContext *c,
813
                const char *arch) {
814

815
        int r;
137✔
816

817
        assert(c);
137✔
818

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

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

848
        const char *suffix = strjoina(arch, ".efi");
685✔
849
        const char *suffix_signed = strjoina(arch, ".efi.signed");
685✔
850

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

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

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

872
        return r;
873
}
874

875
static int install_loader_config(InstallContext *c) {
13✔
876
        int r;
13✔
877

878
        assert(c);
13✔
879
        assert(c->make_entry_directory >= 0);
13✔
880

881
        int esp_fd = acquire_esp_fd(c);
13✔
882
        if (esp_fd < 0)
13✔
883
                return esp_fd;
13✔
884

885
        _cleanup_free_ char *j = path_join(c->root, c->esp_path);
26✔
886
        if (!j)
13✔
887
                return log_oom();
×
888

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

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

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

910
        CLEANUP_TMPFILE_AT(loader_dir_fd, t);
13✔
911

912
        fprintf(f, "#timeout 3\n"
13✔
913
                   "#console-mode keep\n");
914

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

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

926
        t = mfree(t); /* disarm CLEANUP_TMPFILE_AT() */
13✔
927
        return 1;
13✔
928
}
929

930
static int install_loader_specification(InstallContext *c) {
134✔
931
        int r;
134✔
932

933
        assert(c);
134✔
934

935
        int dollar_boot_fd = acquire_dollar_boot_fd(c);
134✔
936
        if (dollar_boot_fd < 0)
134✔
937
                return dollar_boot_fd;
134✔
938

939
        _cleanup_free_ char *j = path_join(c->root, dollar_boot_path(c));
268✔
940
        if (!j)
134✔
941
                return log_oom();
×
942

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

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

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

964
        CLEANUP_TMPFILE_AT(loader_dir_fd, t);
13✔
965

966
        fprintf(f, "type1\n");
13✔
967

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

974
        t = mfree(t); /* disarm CLEANUP_TMPFILE_AT() */
13✔
975
        return 1;
13✔
976
}
977

978
static int install_entry_directory(InstallContext *c) {
13✔
979
        assert(c);
13✔
980
        assert(c->make_entry_directory >= 0);
13✔
981

982
        if (!c->make_entry_directory)
13✔
983
                return 0;
13✔
984

985
        assert(c->entry_token);
7✔
986

987
        int dollar_boot_fd = acquire_dollar_boot_fd(c);
7✔
988
        if (dollar_boot_fd < 0)
7✔
989
                return dollar_boot_fd;
990

991
        _cleanup_free_ char *j = path_join(c->root, dollar_boot_path(c));
14✔
992
        if (!j)
7✔
993
                return log_oom();
×
994

995
        return mkdir_one(j, dollar_boot_fd, c->entry_token);
7✔
996
}
997

998
static int install_entry_token(InstallContext *c) {
13✔
999
        int r;
13✔
1000

1001
        assert(c);
13✔
1002
        assert(c->make_entry_directory >= 0);
13✔
1003
        assert(c->entry_token);
13✔
1004

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

1008
        if (!c->make_entry_directory && c->entry_token_type == BOOT_ENTRY_TOKEN_MACHINE_ID)
13✔
1009
                return 0;
13✔
1010

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

1013
        _cleanup_free_ char *j = path_join(c->root, confdir);
26✔
1014
        if (!j)
13✔
1015
                return log_oom();
×
1016

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

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

1030
        return 0;
1031
}
1032

1033
#if HAVE_OPENSSL
1034
static int efi_timestamp(EFI_TIME *ret) {
1✔
1035
        struct tm tm = {};
1✔
1036
        int r;
1✔
1037

1038
        assert(ret);
1✔
1039

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

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

1054
        return 0;
1✔
1055
}
1056
#endif
1057

1058
static int install_secure_boot_auto_enroll(InstallContext *c) {
13✔
1059
#if HAVE_OPENSSL
1060
        int r;
13✔
1061
#endif
1062

1063
        if (!arg_secure_boot_auto_enroll)
13✔
1064
                return 0;
13✔
1065

1066
#if HAVE_OPENSSL
1067
        if (!c->secure_boot_certificate || !c->secure_boot_private_key)
1✔
1068
                return 0;
1069

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

1077
        int esp_fd = acquire_esp_fd(c);
1✔
1078
        if (esp_fd < 0)
1✔
1079
                return esp_fd;
1080

1081
        _cleanup_free_ char *j = path_join(c->root, c->esp_path);
2✔
1082
        if (!j)
1✔
1083
                return log_oom();
×
1084

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

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

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

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

1108
        EFI_TIME timestamp;
1✔
1109
        r = efi_timestamp(&timestamp);
1✔
1110
        if (r < 0)
1✔
1111
                return r;
1112

1113
        uint32_t attrs =
1✔
1114
                EFI_VARIABLE_NON_VOLATILE|
1115
                EFI_VARIABLE_BOOTSERVICE_ACCESS|
1116
                EFI_VARIABLE_RUNTIME_ACCESS|
1117
                EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS;
1118

1119
        FOREACH_STRING(db, "PK", "KEK", "db") {
4✔
1120
                _cleanup_(BIO_freep) BIO *bio = NULL;
3✔
1121

1122
                bio = BIO_new(BIO_s_mem());
3✔
1123
                if (!bio)
3✔
1124
                        return log_oom();
×
1125

1126
                _cleanup_free_ char16_t *db16 = utf8_to_utf16(db, SIZE_MAX);
6✔
1127
                if (!db16)
3✔
1128
                        return log_oom();
×
1129

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

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

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

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

1142
                if (BIO_write(bio, &timestamp, sizeof(timestamp)) < 0)
3✔
1143
                        return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to write timestamp to bio");
×
1144

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

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

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

1160
                size_t authsz = offsetof(EFI_VARIABLE_AUTHENTICATION_2, AuthInfo.CertData) + sigsz;
3✔
1161
                _cleanup_free_ EFI_VARIABLE_AUTHENTICATION_2 *auth = malloc(authsz);
×
1162
                if (!auth)
3✔
1163
                        return log_oom();
×
1164

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

1177
                memcpy(auth->AuthInfo.CertData, sig, sigsz);
3✔
1178

1179
                _cleanup_free_ char *filename = strjoin(db, ".auth");
3✔
1180
                if (!filename)
3✔
1181
                        return log_oom();
×
1182

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

1188
                CLEANUP_TMPFILE_AT(keys_fd, t);
×
1189

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

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

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

1202
                t = mfree(t); /* Disarm CLEANUP_TMPFILE_AT() */
3✔
1203

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

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

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

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

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

1230
        return true;
1231
}
1232

1233
static int find_slot(sd_id128_t uuid, const char *path, uint16_t *id) {
16✔
1234
        _cleanup_free_ uint16_t *options = NULL;
16✔
1235

1236
        int n = efi_get_boot_options(&options);
16✔
1237
        if (n < 0)
16✔
1238
                return n;
1239

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

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

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

1261
static int insert_into_order(InstallContext *c, uint16_t slot) {
13✔
1262
        _cleanup_free_ uint16_t *order = NULL;
13✔
1263
        uint16_t *t;
13✔
1264
        int n;
13✔
1265

1266
        assert(c);
13✔
1267

1268
        n = efi_get_boot_order(&order);
13✔
1269
        if (n <= 0)
13✔
1270
                /* no entry, add us */
1271
                return efi_set_boot_order(&slot, 1);
×
1272

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

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

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

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

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

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

1305
        return efi_set_boot_order(order, n + 1);
6✔
1306
}
1307

1308
static int remove_from_order(uint16_t slot) {
×
1309
        _cleanup_free_ uint16_t *order = NULL;
×
1310
        int n;
×
1311

1312
        n = efi_get_boot_order(&order);
×
1313
        if (n <= 0)
×
1314
                return n;
1315

1316
        for (int i = 0; i < n; i++) {
×
1317
                if (order[i] != slot)
×
1318
                        continue;
×
1319

1320
                if (i + 1 < n)
×
1321
                        memmove(order + i, order + i+1, (n - i) * sizeof(uint16_t));
×
1322
                return efi_set_boot_order(order, n - 1);
×
1323
        }
1324

1325
        return 0;
1326
}
1327

1328
static int pick_efi_boot_option_description(int esp_fd, char **ret) {
9✔
1329
        int r;
9✔
1330

1331
        assert(esp_fd >= 0);
9✔
1332
        assert(ret);
9✔
1333

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

1338
        const char *b = arg_efi_boot_option_description ?: "Linux Boot Manager";
9✔
1339
        if (!arg_efi_boot_option_description_with_device)
9✔
1340
                goto fallback;
9✔
1341

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

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

1358
        j = strjoin(b, " (", serial, ")");
×
1359
        if (!j)
×
1360
                return log_oom();
×
1361

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

1368
        *ret = TAKE_PTR(j);
×
1369
        return 0;
×
1370

1371
fallback:
9✔
1372
        j = strdup(b);
9✔
1373
        if (!j)
9✔
1374
                return log_oom();
×
1375

1376
        *ret = TAKE_PTR(j);
9✔
1377
        return 0;
9✔
1378
}
1379

1380
static int install_variables(
13✔
1381
                InstallContext *c,
1382
                const char *path) {
1383

1384
        uint16_t slot;
13✔
1385
        int r;
13✔
1386

1387
        assert(c);
13✔
1388

1389
        int esp_fd = acquire_esp_fd(c);
13✔
1390
        if (esp_fd < 0)
13✔
1391
                return esp_fd;
13✔
1392

1393
        _cleanup_free_ char *j = path_join(c->root, c->esp_path);
26✔
1394
        if (!j)
13✔
1395
                return log_oom();
×
1396

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

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

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

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

1421
        bool existing = r > 0;
13✔
1422

1423
        if (c->operation == INSTALL_NEW || !existing) {
13✔
1424
                _cleanup_free_ char *description = NULL;
9✔
1425

1426
                r = pick_efi_boot_option_description(esp_fd, &description);
9✔
1427
                if (r < 0)
9✔
1428
                        return r;
1429

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

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

1444
                        return c->graceful ? 0 : r;
×
1445
                }
1446

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

1452
        return insert_into_order(c, slot);
13✔
1453
}
1454

1455
static int are_we_installed(InstallContext *c) {
138✔
1456
        int r;
138✔
1457

1458
        assert(c);
138✔
1459

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

1474
        _cleanup_free_ char *p = path_join(c->esp_path, "/EFI/systemd");
276✔
1475
        if (!p)
138✔
1476
                return log_oom();
×
1477

1478
        int esp_fd = acquire_esp_fd(c);
138✔
1479
        if (esp_fd < 0)
138✔
1480
                return esp_fd;
1481

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

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

1498
        return r == 0;
131✔
1499
}
1500

1501
#if HAVE_OPENSSL
1502
static int load_secure_boot_auto_enroll(
137✔
1503
                X509 **ret_certificate,
1504
                EVP_PKEY **ret_private_key,
1505
                OpenSSLAskPasswordUI **ret_ui) {
1506

1507
        int r;
137✔
1508

1509
        assert(ret_certificate);
137✔
1510
        assert(ret_private_key);
137✔
1511
        assert(ret_ui);
137✔
1512

1513
        if (!arg_secure_boot_auto_enroll) {
137✔
1514
                *ret_certificate = NULL;
136✔
1515
                *ret_private_key = NULL;
136✔
1516
                return 0;
136✔
1517
        }
1518

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

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

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

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

1557
        *ret_certificate = TAKE_PTR(certificate);
1✔
1558

1559
        return 0;
1✔
1560
}
1561
#endif
1562

1563
static int run_install(InstallContext *c) {
137✔
1564
        int r;
137✔
1565

1566
        assert(c);
137✔
1567
        assert(c->operation >= 0);
137✔
1568

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

1580
        r = settle_make_entry_directory(c);
137✔
1581
        if (r < 0)
137✔
1582
                return r;
1583

1584
        const char *arch = arg_arch_all ? "" : get_efi_arch();
137✔
1585

1586
        int esp_fd = acquire_esp_fd(c);
137✔
1587
        if (esp_fd < 0)
137✔
1588
                return esp_fd;
1589

1590
        _cleanup_free_ char *j = path_join(c->root, c->esp_path);
274✔
1591
        if (!j)
137✔
1592
                return log_oom();
×
1593

1594
        int dollar_boot_fd = acquire_dollar_boot_fd(c);
137✔
1595
        if (dollar_boot_fd < 0)
137✔
1596
                return dollar_boot_fd;
1597

1598
        _cleanup_free_ char *w = path_join(c->root, dollar_boot_path(c));
274✔
1599
        if (!w)
137✔
1600
                return log_oom();
×
1601

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

1608
                        r = create_subdirs(j, esp_fd, esp_subdirs);
13✔
1609
                        if (r < 0)
13✔
1610
                                return r;
1611

1612
                        r = create_subdirs(w, dollar_boot_fd, dollar_boot_subdirs);
13✔
1613
                        if (r < 0)
13✔
1614
                                return r;
1615
                }
1616

1617
                r = install_binaries(c, arch);
137✔
1618
                if (r < 0)
137✔
1619
                        return r;
1620

1621
                if (c->operation == INSTALL_NEW) {
134✔
1622
                        r = install_loader_config(c);
13✔
1623
                        if (r < 0)
13✔
1624
                                return r;
1625

1626
                        r = install_entry_directory(c);
13✔
1627
                        if (r < 0)
13✔
1628
                                return r;
1629

1630
                        r = install_entry_token(c);
13✔
1631
                        if (r < 0)
13✔
1632
                                return r;
1633

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

1640
                        r = install_secure_boot_auto_enroll(c);
13✔
1641
                        if (r < 0)
13✔
1642
                                return r;
1643
                }
1644

1645
                r = install_loader_specification(c);
134✔
1646
                if (r < 0)
134✔
1647
                        return r;
1648
        }
1649

1650
        (void) sync_everything();
134✔
1651

1652
        if (!should_touch_install_variables(c))
134✔
1653
                return 0;
1654

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

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

1664
int verb_install(int argc, char *argv[], void *userdata) {
137✔
1665
        int r;
137✔
1666

1667
        /* Invoked for both "update" and "install" */
1668

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

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

1685
        return run_install(&c);
137✔
1686
}
1687

1688
static int remove_boot_efi(InstallContext *c) {
13✔
1689
        int r, n = 0;
13✔
1690

1691
        assert(c);
13✔
1692

1693
        int esp_fd = acquire_esp_fd(c);
13✔
1694
        if (esp_fd < 0)
13✔
1695
                return esp_fd;
13✔
1696

1697
        _cleanup_free_ char *w = path_join(c->root, c->esp_path);
26✔
1698
        if (!w)
13✔
1699
                return log_oom();
×
1700

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

1714
        _cleanup_free_ char *j = path_join(w, p);
26✔
1715
        if (!j)
13✔
1716
                return log_oom();
×
1717

1718
        FOREACH_DIRENT(de, d, break) {
61✔
1719
                _cleanup_close_ int fd = -EBADF;
22✔
1720

1721
                if (!endswith_no_case(de->d_name, ".efi"))
22✔
1722
                        continue;
×
1723

1724
                _cleanup_free_ char *z = path_join(j, de->d_name);
31✔
1725
                if (!z)
22✔
1726
                        return log_oom();
×
1727

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

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

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

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

1752
                log_info("Removed '%s'.", z);
13✔
1753

1754
                n++;
13✔
1755
        }
1756

1757
        log_debug("Removed %i EFI binaries from '%s'.", n, j);
13✔
1758
        return n;
1759
}
1760

1761
static int unlink_inode(const char *root, int root_fd, const char *path, mode_t type) {
271✔
1762
        int r;
271✔
1763

1764
        assert(root);
271✔
1765
        assert(root_fd >= 0);
271✔
1766
        assert(path);
271✔
1767
        assert(IN_SET(type, S_IFREG, S_IFDIR));
271✔
1768

1769
        _cleanup_free_ char *p = path_join(empty_to_root(root), path);
542✔
1770
        if (!p)
271✔
1771
                return log_oom();
×
1772

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

1785
        log_info("Removed %s\"%s\".", S_ISDIR(type) ? "directory " : "", p);
155✔
1786
        return 0;
1787
}
1788

1789
static int remove_subdirs(const char *root, int root_fd, const char *const *subdirs) {
34✔
1790
        int r = 0;
34✔
1791

1792
        assert(root);
34✔
1793
        assert(root_fd);
34✔
1794

1795
        STRV_FOREACH_BACKWARDS(i, (char**) subdirs)
332✔
1796
                RET_GATHER(r, unlink_inode(root, root_fd, *i, S_IFDIR));
149✔
1797

1798
        return r;
34✔
1799
}
1800

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

1807
        if (!c->make_entry_directory || !c->entry_token)
21✔
1808
                return 0;
1809

1810
        return unlink_inode(path, fd, c->entry_token, S_IFDIR);
10✔
1811
}
1812

1813
static int remove_binaries(InstallContext *c) {
13✔
1814
        int r;
13✔
1815

1816
        _cleanup_free_ char *p = path_join(c->root, "/EFI/systemd");
26✔
1817
        if (!p)
13✔
1818
                return log_oom();
×
1819

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

1830
                r = 0;
1831
        } else
1832
                r = rm_rf_at(efi_fd, "systemd", REMOVE_ROOT|REMOVE_PHYSICAL|REMOVE_MISSING_OK);
13✔
1833

1834
        return RET_GATHER(r, remove_boot_efi(c));
13✔
1835
}
1836

1837
static int remove_variables(sd_id128_t uuid, const char *path, bool in_order) {
3✔
1838
        uint16_t slot;
3✔
1839
        int r;
3✔
1840

1841
        r = find_slot(uuid, path, &slot);
3✔
1842
        if (r != 1)
3✔
1843
                return 0;
3✔
1844

1845
        r = efi_remove_boot_option(slot);
×
1846
        if (r < 0)
×
1847
                return r;
1848

1849
        if (in_order)
×
1850
                return remove_from_order(slot);
×
1851

1852
        return 0;
1853
}
1854

1855
static int remove_loader_variables(void) {
3✔
1856
        int r = 0;
3✔
1857

1858
        /* Remove all persistent loader variables we define */
1859

1860
        FOREACH_STRING(var,
27✔
1861
                       EFI_LOADER_VARIABLE_STR("LoaderConfigConsoleMode"),
1862
                       EFI_LOADER_VARIABLE_STR("LoaderConfigTimeout"),
1863
                       EFI_LOADER_VARIABLE_STR("LoaderConfigTimeoutOneShot"),
1864
                       EFI_LOADER_VARIABLE_STR("LoaderEntryDefault"),
1865
                       EFI_LOADER_VARIABLE_STR("LoaderEntrySysFail"),
1866
                       EFI_LOADER_VARIABLE_STR("LoaderEntryLastBooted"),
1867
                       EFI_LOADER_VARIABLE_STR("LoaderEntryOneShot"),
1868
                       EFI_LOADER_VARIABLE_STR("LoaderSystemToken")) {
1869

1870
                int q;
24✔
1871

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

1881
        return r;
3✔
1882
}
1883

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

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

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

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

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

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

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

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

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

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

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

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

1943
        (void) sync_everything();
13✔
1944

1945
        if (!should_touch_install_variables(&c))
13✔
1946
                return r;
1947

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

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

1958
int verb_is_installed(int argc, char *argv[], void *userdata) {
14✔
1959
        int r;
14✔
1960

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

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

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

1987
static JSON_DISPATCH_ENUM_DEFINE(json_dispatch_install_operation, InstallOperation, install_operation_from_string);
×
1988
static JSON_DISPATCH_ENUM_DEFINE(json_dispatch_boot_entry_token_type, BootEntryTokenType, boot_entry_token_type_from_string);
×
1989

1990
typedef struct InstallParameters {
1991
        InstallContext context;
1992
        unsigned root_fd_index;
1993
} InstallParameters;
1994

1995
static void install_parameters_done(InstallParameters *p) {
×
1996
        assert(p);
×
1997

1998
        install_context_done(&p->context);
×
1999
}
×
2000

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

2007
        int r;
×
2008

2009
        assert(link);
×
2010

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

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

2026
        r = sd_varlink_dispatch(link, parameters, dispatch_table, &p);
×
2027
        if (r != 0)
×
2028
                return r;
2029

2030
        if (!IN_SET(p.context.operation, INSTALL_NEW, INSTALL_UPDATE))
×
2031
                return sd_varlink_error_invalid_parameter_name(link, "operation");
×
2032

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

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

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

2047
                        if (empty_or_root(p.context.root))
×
2048
                                p.context.root = mfree(p.context.root);
×
2049
                }
2050
        }
2051

2052
        if (p.context.root_fd < 0 && p.context.root) {
×
2053
                p.context.root_fd = open(p.context.root, O_RDONLY|O_CLOEXEC|O_DIRECTORY);
×
2054
                if (p.context.root_fd < 0)
×
2055
                        return log_debug_errno(errno, "Failed to open '%s': %m", p.context.root);
×
2056
        }
2057

2058
        if (p.context.root_fd < 0)
×
2059
                p.context.root_fd = XAT_FDROOT;
×
2060

2061
        if (p.context.entry_token_type < 0)
×
2062
                p.context.entry_token_type = BOOT_ENTRY_TOKEN_AUTO;
×
2063

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

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

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

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