]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/bootspec.c
condition: extend ConditionKernelVersion= with relative version checks
[thirdparty/systemd.git] / src / shared / bootspec.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2017 Zbigniew Jędrzejewski-Szmek
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <stdio.h>
21 #include <linux/magic.h>
22
23 #include "alloc-util.h"
24 #include "blkid-util.h"
25 #include "bootspec.h"
26 #include "conf-files.h"
27 #include "def.h"
28 #include "device-nodes.h"
29 #include "efivars.h"
30 #include "fd-util.h"
31 #include "fileio.h"
32 #include "parse-util.h"
33 #include "stat-util.h"
34 #include "string-util.h"
35 #include "strv.h"
36 #include "virt.h"
37
38 void boot_entry_free(BootEntry *entry) {
39 assert(entry);
40
41 free(entry->filename);
42 free(entry->title);
43 free(entry->show_title);
44 free(entry->version);
45 free(entry->machine_id);
46 free(entry->architecture);
47 strv_free(entry->options);
48 free(entry->kernel);
49 free(entry->efi);
50 strv_free(entry->initrd);
51 free(entry->device_tree);
52 }
53
54 int boot_entry_load(const char *path, BootEntry *entry) {
55 _cleanup_(boot_entry_free) BootEntry tmp = {};
56 _cleanup_fclose_ FILE *f = NULL;
57 unsigned line = 1;
58 char *b, *c;
59 int r;
60
61 assert(path);
62 assert(entry);
63
64 c = endswith_no_case(path, ".conf");
65 if (!c) {
66 log_error("Invalid loader entry filename: %s", path);
67 return -EINVAL;
68 }
69
70 b = basename(path);
71 tmp.filename = strndup(b, c - b);
72 if (!tmp.filename)
73 return log_oom();
74
75 f = fopen(path, "re");
76 if (!f)
77 return log_error_errno(errno, "Failed to open \"%s\": %m", path);
78
79 for (;;) {
80 _cleanup_free_ char *buf = NULL;
81 char *p;
82
83 r = read_line(f, LONG_LINE_MAX, &buf);
84 if (r == 0)
85 break;
86 if (r == -ENOBUFS)
87 return log_error_errno(r, "%s:%u: Line too long", path, line);
88 if (r < 0)
89 return log_error_errno(r, "%s:%u: Error while reading: %m", path, line);
90
91 line++;
92
93 if (IN_SET(*strstrip(buf), '#', '\0'))
94 continue;
95
96 p = strchr(buf, ' ');
97 if (!p) {
98 log_warning("%s:%u: Bad syntax", path, line);
99 continue;
100 }
101 *p = '\0';
102 p = strstrip(p + 1);
103
104 if (streq(buf, "title"))
105 r = free_and_strdup(&tmp.title, p);
106 else if (streq(buf, "version"))
107 r = free_and_strdup(&tmp.version, p);
108 else if (streq(buf, "machine-id"))
109 r = free_and_strdup(&tmp.machine_id, p);
110 else if (streq(buf, "architecture"))
111 r = free_and_strdup(&tmp.architecture, p);
112 else if (streq(buf, "options"))
113 r = strv_extend(&tmp.options, p);
114 else if (streq(buf, "linux"))
115 r = free_and_strdup(&tmp.kernel, p);
116 else if (streq(buf, "efi"))
117 r = free_and_strdup(&tmp.efi, p);
118 else if (streq(buf, "initrd"))
119 r = strv_extend(&tmp.initrd, p);
120 else if (streq(buf, "devicetree"))
121 r = free_and_strdup(&tmp.device_tree, p);
122 else {
123 log_notice("%s:%u: Unknown line \"%s\"", path, line, buf);
124 continue;
125 }
126 if (r < 0)
127 return log_error_errno(r, "%s:%u: Error while reading: %m", path, line);
128 }
129
130 *entry = tmp;
131 tmp = (BootEntry) {};
132 return 0;
133 }
134
135 void boot_config_free(BootConfig *config) {
136 unsigned i;
137
138 assert(config);
139
140 free(config->default_pattern);
141 free(config->timeout);
142 free(config->editor);
143
144 free(config->entry_oneshot);
145 free(config->entry_default);
146
147 for (i = 0; i < config->n_entries; i++)
148 boot_entry_free(config->entries + i);
149 free(config->entries);
150 }
151
152 int boot_loader_read_conf(const char *path, BootConfig *config) {
153 _cleanup_fclose_ FILE *f = NULL;
154 unsigned line = 1;
155 int r;
156
157 assert(path);
158 assert(config);
159
160 f = fopen(path, "re");
161 if (!f)
162 return log_error_errno(errno, "Failed to open \"%s\": %m", path);
163
164 for (;;) {
165 _cleanup_free_ char *buf = NULL;
166 char *p;
167
168 r = read_line(f, LONG_LINE_MAX, &buf);
169 if (r == 0)
170 break;
171 if (r == -ENOBUFS)
172 return log_error_errno(r, "%s:%u: Line too long", path, line);
173 if (r < 0)
174 return log_error_errno(r, "%s:%u: Error while reading: %m", path, line);
175
176 line++;
177
178 if (IN_SET(*strstrip(buf), '#', '\0'))
179 continue;
180
181 p = strchr(buf, ' ');
182 if (!p) {
183 log_warning("%s:%u: Bad syntax", path, line);
184 continue;
185 }
186 *p = '\0';
187 p = strstrip(p + 1);
188
189 if (streq(buf, "default"))
190 r = free_and_strdup(&config->default_pattern, p);
191 else if (streq(buf, "timeout"))
192 r = free_and_strdup(&config->timeout, p);
193 else if (streq(buf, "editor"))
194 r = free_and_strdup(&config->editor, p);
195 else {
196 log_notice("%s:%u: Unknown line \"%s\"", path, line, buf);
197 continue;
198 }
199 if (r < 0)
200 return log_error_errno(r, "%s:%u: Error while reading: %m", path, line);
201 }
202
203 return 0;
204 }
205
206 static int boot_entry_compare(const void *a, const void *b) {
207 const BootEntry *aa = a, *bb = b;
208
209 return str_verscmp(aa->filename, bb->filename);
210 }
211
212 int boot_entries_find(const char *dir, BootEntry **ret_entries, size_t *ret_n_entries) {
213 _cleanup_strv_free_ char **files = NULL;
214 char **f;
215 int r;
216 BootEntry *array = NULL;
217 size_t n_allocated = 0, n = 0;
218
219 assert(dir);
220 assert(ret_entries);
221 assert(ret_n_entries);
222
223 r = conf_files_list(&files, ".conf", NULL, 0, dir, NULL);
224 if (r < 0)
225 return log_error_errno(r, "Failed to list files in \"%s\": %m", dir);
226
227 STRV_FOREACH(f, files) {
228 if (!GREEDY_REALLOC0(array, n_allocated, n + 1))
229 return log_oom();
230
231 r = boot_entry_load(*f, array + n);
232 if (r < 0)
233 continue;
234
235 n++;
236 }
237
238 qsort_safe(array, n, sizeof(BootEntry), boot_entry_compare);
239
240 *ret_entries = array;
241 *ret_n_entries = n;
242
243 return 0;
244 }
245
246 static bool find_nonunique(BootEntry *entries, size_t n_entries, bool *arr) {
247 unsigned i, j;
248 bool non_unique = false;
249
250 assert(entries || n_entries == 0);
251 assert(arr || n_entries == 0);
252
253 for (i = 0; i < n_entries; i++)
254 arr[i] = false;
255
256 for (i = 0; i < n_entries; i++)
257 for (j = 0; j < n_entries; j++)
258 if (i != j && streq(boot_entry_title(entries + i),
259 boot_entry_title(entries + j)))
260 non_unique = arr[i] = arr[j] = true;
261
262 return non_unique;
263 }
264
265 static int boot_entries_uniquify(BootEntry *entries, size_t n_entries) {
266 char *s;
267 unsigned i;
268 int r;
269 bool arr[n_entries];
270
271 assert(entries || n_entries == 0);
272
273 /* Find _all_ non-unique titles */
274 if (!find_nonunique(entries, n_entries, arr))
275 return 0;
276
277 /* Add version to non-unique titles */
278 for (i = 0; i < n_entries; i++)
279 if (arr[i] && entries[i].version) {
280 r = asprintf(&s, "%s (%s)", boot_entry_title(entries + i), entries[i].version);
281 if (r < 0)
282 return -ENOMEM;
283
284 free_and_replace(entries[i].show_title, s);
285 }
286
287 if (!find_nonunique(entries, n_entries, arr))
288 return 0;
289
290 /* Add machine-id to non-unique titles */
291 for (i = 0; i < n_entries; i++)
292 if (arr[i] && entries[i].machine_id) {
293 r = asprintf(&s, "%s (%s)", boot_entry_title(entries + i), entries[i].machine_id);
294 if (r < 0)
295 return -ENOMEM;
296
297 free_and_replace(entries[i].show_title, s);
298 }
299
300 if (!find_nonunique(entries, n_entries, arr))
301 return 0;
302
303 /* Add file name to non-unique titles */
304 for (i = 0; i < n_entries; i++)
305 if (arr[i]) {
306 r = asprintf(&s, "%s (%s)", boot_entry_title(entries + i), entries[i].filename);
307 if (r < 0)
308 return -ENOMEM;
309
310 free_and_replace(entries[i].show_title, s);
311 }
312
313 return 0;
314 }
315
316 static int boot_entries_select_default(const BootConfig *config) {
317 int i;
318
319 assert(config);
320
321 if (config->entry_oneshot)
322 for (i = config->n_entries - 1; i >= 0; i--)
323 if (streq(config->entry_oneshot, config->entries[i].filename)) {
324 log_debug("Found default: filename \"%s\" is matched by LoaderEntryOneShot",
325 config->entries[i].filename);
326 return i;
327 }
328
329 if (config->entry_default)
330 for (i = config->n_entries - 1; i >= 0; i--)
331 if (streq(config->entry_default, config->entries[i].filename)) {
332 log_debug("Found default: filename \"%s\" is matched by LoaderEntryDefault",
333 config->entries[i].filename);
334 return i;
335 }
336
337 if (config->default_pattern)
338 for (i = config->n_entries - 1; i >= 0; i--)
339 if (fnmatch(config->default_pattern, config->entries[i].filename, FNM_CASEFOLD) == 0) {
340 log_debug("Found default: filename \"%s\" is matched by pattern \"%s\"",
341 config->entries[i].filename, config->default_pattern);
342 return i;
343 }
344
345 if (config->n_entries > 0)
346 log_debug("Found default: last entry \"%s\"", config->entries[config->n_entries - 1].filename);
347 else
348 log_debug("Found no default boot entry :(");
349
350 return config->n_entries - 1; /* -1 means "no default" */
351 }
352
353 int boot_entries_load_config(const char *esp_path, BootConfig *config) {
354 const char *p;
355 int r;
356
357 assert(esp_path);
358 assert(config);
359
360 p = strjoina(esp_path, "/loader/loader.conf");
361 r = boot_loader_read_conf(p, config);
362 if (r < 0)
363 return log_error_errno(r, "Failed to read boot config from \"%s\": %m", p);
364
365 p = strjoina(esp_path, "/loader/entries");
366 r = boot_entries_find(p, &config->entries, &config->n_entries);
367 if (r < 0)
368 return log_error_errno(r, "Failed to read boot entries from \"%s\": %m", p);
369
370 r = boot_entries_uniquify(config->entries, config->n_entries);
371 if (r < 0)
372 return log_error_errno(r, "Failed to uniquify boot entries: %m");
373
374 r = efi_get_variable_string(EFI_VENDOR_LOADER, "LoaderEntryOneShot", &config->entry_oneshot);
375 if (r < 0 && r != -ENOENT)
376 return log_error_errno(r, "Failed to read EFI var \"LoaderEntryOneShot\": %m");
377
378 r = efi_get_variable_string(EFI_VENDOR_LOADER, "LoaderEntryDefault", &config->entry_default);
379 if (r < 0 && r != -ENOENT)
380 return log_error_errno(r, "Failed to read EFI var \"LoaderEntryDefault\": %m");
381
382 config->default_entry = boot_entries_select_default(config);
383 return 0;
384 }
385
386 /********************************************************************************/
387
388 static int verify_esp(
389 const char *p,
390 bool searching,
391 bool unprivileged_mode,
392 uint32_t *ret_part,
393 uint64_t *ret_pstart,
394 uint64_t *ret_psize,
395 sd_id128_t *ret_uuid) {
396 #if HAVE_BLKID
397 _cleanup_blkid_free_probe_ blkid_probe b = NULL;
398 char t[DEV_NUM_PATH_MAX];
399 const char *v;
400 #endif
401 uint64_t pstart = 0, psize = 0;
402 struct stat st, st2;
403 const char *t2;
404 struct statfs sfs;
405 sd_id128_t uuid = SD_ID128_NULL;
406 uint32_t part = 0;
407 int r;
408
409 assert(p);
410
411 /* Non-root user can only check the status, so if an error occured in the following, it does not cause any
412 * issues. Let's also, silence the error messages. */
413
414 if (statfs(p, &sfs) < 0) {
415 /* If we are searching for the mount point, don't generate a log message if we can't find the path */
416 if (errno == ENOENT && searching)
417 return -ENOENT;
418
419 return log_full_errno(unprivileged_mode && errno == EACCES ? LOG_DEBUG : LOG_ERR, errno,
420 "Failed to check file system type of \"%s\": %m", p);
421 }
422
423 if (!F_TYPE_EQUAL(sfs.f_type, MSDOS_SUPER_MAGIC)) {
424 if (searching)
425 return -EADDRNOTAVAIL;
426
427 log_error("File system \"%s\" is not a FAT EFI System Partition (ESP) file system.", p);
428 return -ENODEV;
429 }
430
431 if (stat(p, &st) < 0)
432 return log_full_errno(unprivileged_mode && errno == EACCES ? LOG_DEBUG : LOG_ERR, errno,
433 "Failed to determine block device node of \"%s\": %m", p);
434
435 if (major(st.st_dev) == 0) {
436 log_error("Block device node of %p is invalid.", p);
437 return -ENODEV;
438 }
439
440 t2 = strjoina(p, "/..");
441 r = stat(t2, &st2);
442 if (r < 0)
443 return log_full_errno(unprivileged_mode && errno == EACCES ? LOG_DEBUG : LOG_ERR, errno,
444 "Failed to determine block device node of parent of \"%s\": %m", p);
445
446 if (st.st_dev == st2.st_dev) {
447 log_error("Directory \"%s\" is not the root of the EFI System Partition (ESP) file system.", p);
448 return -ENODEV;
449 }
450
451 /* In a container we don't have access to block devices, skip this part of the verification, we trust the
452 * container manager set everything up correctly on its own. Also skip the following verification for non-root user. */
453 if (detect_container() > 0 || unprivileged_mode)
454 goto finish;
455
456 #if HAVE_BLKID
457 xsprintf_dev_num_path(t, "block", st.st_dev);
458 errno = 0;
459 b = blkid_new_probe_from_filename(t);
460 if (!b)
461 return log_error_errno(errno ?: ENOMEM, "Failed to open file system \"%s\": %m", p);
462
463 blkid_probe_enable_superblocks(b, 1);
464 blkid_probe_set_superblocks_flags(b, BLKID_SUBLKS_TYPE);
465 blkid_probe_enable_partitions(b, 1);
466 blkid_probe_set_partitions_flags(b, BLKID_PARTS_ENTRY_DETAILS);
467
468 errno = 0;
469 r = blkid_do_safeprobe(b);
470 if (r == -2) {
471 log_error("File system \"%s\" is ambiguous.", p);
472 return -ENODEV;
473 } else if (r == 1) {
474 log_error("File system \"%s\" does not contain a label.", p);
475 return -ENODEV;
476 } else if (r != 0)
477 return log_error_errno(errno ?: EIO, "Failed to probe file system \"%s\": %m", p);
478
479 errno = 0;
480 r = blkid_probe_lookup_value(b, "TYPE", &v, NULL);
481 if (r != 0)
482 return log_error_errno(errno ?: EIO, "Failed to probe file system type \"%s\": %m", p);
483 if (!streq(v, "vfat")) {
484 log_error("File system \"%s\" is not FAT.", p);
485 return -ENODEV;
486 }
487
488 errno = 0;
489 r = blkid_probe_lookup_value(b, "PART_ENTRY_SCHEME", &v, NULL);
490 if (r != 0)
491 return log_error_errno(errno ?: EIO, "Failed to probe partition scheme \"%s\": %m", p);
492 if (!streq(v, "gpt")) {
493 log_error("File system \"%s\" is not on a GPT partition table.", p);
494 return -ENODEV;
495 }
496
497 errno = 0;
498 r = blkid_probe_lookup_value(b, "PART_ENTRY_TYPE", &v, NULL);
499 if (r != 0)
500 return log_error_errno(errno ?: EIO, "Failed to probe partition type UUID \"%s\": %m", p);
501 if (!streq(v, "c12a7328-f81f-11d2-ba4b-00a0c93ec93b")) {
502 log_error("File system \"%s\" has wrong type for an EFI System Partition (ESP).", p);
503 return -ENODEV;
504 }
505
506 errno = 0;
507 r = blkid_probe_lookup_value(b, "PART_ENTRY_UUID", &v, NULL);
508 if (r != 0)
509 return log_error_errno(errno ?: EIO, "Failed to probe partition entry UUID \"%s\": %m", p);
510 r = sd_id128_from_string(v, &uuid);
511 if (r < 0) {
512 log_error("Partition \"%s\" has invalid UUID \"%s\".", p, v);
513 return -EIO;
514 }
515
516 errno = 0;
517 r = blkid_probe_lookup_value(b, "PART_ENTRY_NUMBER", &v, NULL);
518 if (r != 0)
519 return log_error_errno(errno ?: EIO, "Failed to probe partition number \"%s\": m", p);
520 r = safe_atou32(v, &part);
521 if (r < 0)
522 return log_error_errno(r, "Failed to parse PART_ENTRY_NUMBER field.");
523
524 errno = 0;
525 r = blkid_probe_lookup_value(b, "PART_ENTRY_OFFSET", &v, NULL);
526 if (r != 0)
527 return log_error_errno(errno ?: EIO, "Failed to probe partition offset \"%s\": %m", p);
528 r = safe_atou64(v, &pstart);
529 if (r < 0)
530 return log_error_errno(r, "Failed to parse PART_ENTRY_OFFSET field.");
531
532 errno = 0;
533 r = blkid_probe_lookup_value(b, "PART_ENTRY_SIZE", &v, NULL);
534 if (r != 0)
535 return log_error_errno(errno ?: EIO, "Failed to probe partition size \"%s\": %m", p);
536 r = safe_atou64(v, &psize);
537 if (r < 0)
538 return log_error_errno(r, "Failed to parse PART_ENTRY_SIZE field.");
539 #endif
540
541 finish:
542 if (ret_part)
543 *ret_part = part;
544 if (ret_pstart)
545 *ret_pstart = pstart;
546 if (ret_psize)
547 *ret_psize = psize;
548 if (ret_uuid)
549 *ret_uuid = uuid;
550
551 return 0;
552 }
553
554 int find_esp_and_warn(
555 const char *path,
556 bool unprivileged_mode,
557 char **ret_path,
558 uint32_t *ret_part,
559 uint64_t *ret_pstart,
560 uint64_t *ret_psize,
561 sd_id128_t *ret_uuid) {
562
563 int r;
564
565 /* This logs about all errors except:
566 *
567 * -ENOKEY → when we can't find the partition
568 * -EACCESS → when unprivileged_mode is true, and we can't access something
569 */
570
571 if (path) {
572 r = verify_esp(path, false, unprivileged_mode, ret_part, ret_pstart, ret_psize, ret_uuid);
573 if (r < 0)
574 return r;
575
576 goto found;
577 }
578
579 FOREACH_STRING(path, "/efi", "/boot", "/boot/efi") {
580
581 r = verify_esp(path, true, unprivileged_mode, ret_part, ret_pstart, ret_psize, ret_uuid);
582 if (r >= 0)
583 goto found;
584 if (!IN_SET(r, -ENOENT, -EADDRNOTAVAIL)) /* This one is not it */
585 return r;
586 }
587
588 /* No logging here */
589 return -ENOKEY;
590
591 found:
592 if (ret_path) {
593 char *c;
594
595 c = strdup(path);
596 if (!c)
597 return log_oom();
598
599 *ret_path = c;
600 }
601
602 return 0;
603 }