Add support for GPG signature enforcement on booted
[petitboot] / discover / boot.c
1
2 #if defined(HAVE_CONFIG_H)
3 #include "config.h"
4 #endif
5
6 #include <stdbool.h>
7 #include <stdlib.h>
8 #include <assert.h>
9 #include <dirent.h>
10 #include <string.h>
11 #include <fcntl.h>
12 #include <sys/types.h>
13
14 #include <log/log.h>
15 #include <pb-protocol/pb-protocol.h>
16 #include <process/process.h>
17 #include <system/system.h>
18 #include <talloc/talloc.h>
19 #include <url/url.h>
20 #include <util/util.h>
21 #include <i18n/i18n.h>
22
23 #include "device-handler.h"
24 #include "boot.h"
25 #include "paths.h"
26 #include "resource.h"
27 #include "platform.h"
28
29 #include <security/gpg.h>
30
31 static const char *boot_hook_dir = PKG_SYSCONF_DIR "/boot.d";
32 enum {
33         BOOT_HOOK_EXIT_OK       = 0,
34         BOOT_HOOK_EXIT_UPDATE   = 2,
35 };
36
37 /**
38  * kexec_load - kexec load helper.
39  */
40 static int kexec_load(struct boot_task *boot_task)
41 {
42         int result;
43         const char *argv[7];
44         const char **p;
45         char *s_initrd = NULL;
46         char *s_dtb = NULL;
47         char *s_args = NULL;
48
49         boot_task->local_initrd_override = NULL;
50         boot_task->local_dtb_override = NULL;
51         boot_task->local_image_override = NULL;
52
53         if ((result = gpg_validate_boot_files(boot_task))) {
54                 if (result == KEXEC_LOAD_SIGNATURE_FAILURE) {
55                         pb_log("%s: Aborting kexec due to"
56                                 " signature verification failure\n", __func__);
57                         goto abort_kexec;
58                 }
59         }
60
61         const char* local_initrd = (boot_task->local_initrd_override) ?
62                 boot_task->local_initrd_override : boot_task->local_initrd;
63         const char* local_dtb = (boot_task->local_dtb_override) ?
64                 boot_task->local_dtb_override : boot_task->local_dtb;
65         const char* local_image = (boot_task->local_image_override) ?
66                 boot_task->local_image_override : boot_task->local_image;
67
68         p = argv;
69         *p++ = pb_system_apps.kexec;    /* 1 */
70         *p++ = "-l";                    /* 2 */
71
72         if (local_initrd) {
73                 s_initrd = talloc_asprintf(boot_task, "--initrd=%s",
74                                 local_initrd);
75                 assert(s_initrd);
76                 *p++ = s_initrd;         /* 3 */
77         }
78
79         if (local_dtb) {
80                 s_dtb = talloc_asprintf(boot_task, "--dtb=%s",
81                                                 local_dtb);
82                 assert(s_dtb);
83                 *p++ = s_dtb;            /* 4 */
84         }
85
86         s_args = talloc_asprintf(boot_task, "--append=%s",
87                                 boot_task->args ?: "\"\"");
88         assert(s_args);
89         *p++ = s_args;          /* 5 */
90
91         *p++ = local_image;             /* 6 */
92         *p++ = NULL;                    /* 7 */
93
94         result = process_run_simple_argv(boot_task, argv);
95
96         if (result)
97                 pb_log("%s: failed: (%d)\n", __func__, result);
98
99 abort_kexec:
100         gpg_validate_boot_files_cleanup(boot_task);
101
102         return result;
103 }
104
105 /**
106  * kexec_reboot - Helper to boot the new kernel.
107  *
108  * Must only be called after a successful call to kexec_load().
109  */
110
111 static int kexec_reboot(struct boot_task *task)
112 {
113         int result;
114
115         /* First try running shutdown.  Init scripts should run 'exec -e' */
116         result = process_run_simple(task, pb_system_apps.shutdown, "-r",
117                         "now", NULL);
118
119         /* On error, force a kexec with the -e option */
120         if (result) {
121                 result = process_run_simple(task, pb_system_apps.kexec,
122                                                 "-e", NULL);
123         }
124
125         if (result)
126                 pb_log("%s: failed: (%d)\n", __func__, result);
127
128         /* okay, kexec -e -f */
129         if (result) {
130                 result = process_run_simple(task, pb_system_apps.kexec,
131                                                 "-e", "-f", NULL);
132         }
133
134         if (result)
135                 pb_log("%s: failed: (%d)\n", __func__, result);
136
137
138         return result;
139 }
140
141 static void __attribute__((format(__printf__, 4, 5))) update_status(
142                 boot_status_fn fn, void *arg, int type, char *fmt, ...)
143 {
144         struct boot_status status;
145         va_list ap;
146
147         va_start(ap, fmt);
148         status.message = talloc_vasprintf(NULL, fmt, ap);
149         va_end(ap);
150
151         status.type = type;
152         status.progress = -1;
153         status.detail = NULL;
154
155         pb_debug("boot status: [%d] %s\n", type, status.message);
156
157         fn(arg, &status);
158
159         talloc_free(status.message);
160 }
161
162 static void boot_hook_update_param(void *ctx, struct boot_task *task,
163                 const char *name, const char *value)
164 {
165         struct p {
166                 const char *name;
167                 const char **p;
168         } *param, params[] = {
169                 { "boot_image",         &task->local_image },
170                 { "boot_initrd",        &task->local_initrd },
171                 { "boot_dtb",           &task->local_dtb },
172                 { "boot_args",          &task->args },
173                 { NULL, NULL },
174         };
175
176         for (param = params; param->name; param++) {
177                 if (strcmp(param->name, name))
178                         continue;
179
180                 *param->p = talloc_strdup(ctx, value);
181                 return;
182         }
183 }
184
185 static void boot_hook_update(struct boot_task *task, const char *hookname,
186                 char *buf)
187 {
188         char *line, *name, *val, *sep;
189         char *saveptr = NULL;
190
191         for (;; buf = NULL) {
192
193                 line = strtok_r(buf, "\n", &saveptr);
194                 if (!line)
195                         break;
196
197                 sep = strchr(line, '=');
198                 if (!sep)
199                         continue;
200
201                 *sep = '\0';
202                 name = line;
203                 val = sep + 1;
204
205                 boot_hook_update_param(task, task, name, val);
206
207                 pb_log("boot hook %s specified %s=%s\n",
208                                 hookname, name, val);
209         }
210 }
211
212 static void boot_hook_setenv(struct boot_task *task)
213 {
214         unsetenv("boot_image");
215         unsetenv("boot_initrd");
216         unsetenv("boot_dtb");
217         unsetenv("boot_args");
218         unsetenv("boot_tty");
219
220         setenv("boot_image", task->local_image, 1);
221         if (task->local_initrd)
222                 setenv("boot_initrd", task->local_initrd, 1);
223         if (task->local_dtb)
224                 setenv("boot_dtb", task->local_dtb, 1);
225         if (task->args)
226                 setenv("boot_args", task->args, 1);
227         if (task->boot_tty)
228                 setenv("boot_tty", task->boot_tty, 1);
229 }
230
231 static int hook_filter(const struct dirent *dirent)
232 {
233         return dirent->d_type == DT_REG || dirent->d_type == DT_LNK;
234 }
235
236 static int hook_cmp(const struct dirent **a, const struct dirent **b)
237 {
238         return strcmp((*a)->d_name, (*b)->d_name);
239 }
240
241 static void run_boot_hooks(struct boot_task *task)
242 {
243         struct dirent **hooks;
244         int i, n;
245
246         n = scandir(boot_hook_dir, &hooks, hook_filter, hook_cmp);
247         if (n < 1)
248                 return;
249
250         update_status(task->status_fn, task->status_arg, BOOT_STATUS_INFO,
251                         _("running boot hooks"));
252
253         boot_hook_setenv(task);
254
255         for (i = 0; i < n; i++) {
256                 const char *argv[2] = { NULL, NULL };
257                 struct process *process;
258                 char *path;
259                 int rc;
260
261                 path = join_paths(task, boot_hook_dir, hooks[i]->d_name);
262
263                 if (access(path, X_OK)) {
264                         talloc_free(path);
265                         continue;
266                 }
267
268                 process = process_create(task);
269
270                 argv[0] = path;
271                 process->path = path;
272                 process->argv = argv;
273                 process->keep_stdout = true;
274
275                 pb_log("running boot hook %s\n", hooks[i]->d_name);
276
277                 rc = process_run_sync(process);
278                 if (rc) {
279                         pb_log("boot hook exec failed!\n");
280
281                 } else if (WIFEXITED(process->exit_status) &&
282                            WEXITSTATUS(process->exit_status)
283                                 == BOOT_HOOK_EXIT_UPDATE) {
284                         /* if the hook returned with BOOT_HOOK_EXIT_UPDATE,
285                          * then we process stdout to look for updated params
286                          */
287                         boot_hook_update(task, hooks[i]->d_name,
288                                         process->stdout_buf);
289                         boot_hook_setenv(task);
290                 }
291
292                 process_release(process);
293                 talloc_free(path);
294         }
295
296         free(hooks);
297 }
298
299 static bool load_pending(struct load_url_result *result)
300 {
301         return result && result->status == LOAD_ASYNC;
302 }
303
304 static int check_load(struct boot_task *task, const char *name,
305                 struct load_url_result *result)
306 {
307         if (!result)
308                 return 0;
309         if (result->status != LOAD_ERROR)
310                 return 0;
311
312         update_status(task->status_fn, task->status_arg,
313                         BOOT_STATUS_ERROR,
314                         _("Couldn't load %s"), name);
315         return -1;
316 }
317
318 static void cleanup_load(struct load_url_result *result)
319 {
320         if (!result)
321                 return;
322         if (result->status != LOAD_OK)
323                 return;
324         if (!result->cleanup_local)
325                 return;
326         unlink(result->local);
327 }
328
329 static void cleanup_cancellations(struct boot_task *task,
330                 struct load_url_result *cur_result)
331 {
332         struct load_url_result *result, **results[] = {
333                 &task->image, &task->initrd, &task->dtb,
334         };
335         bool pending = false;
336         unsigned int i;
337
338         for (i = 0; i < ARRAY_SIZE(results); i++) {
339                 result = *results[i];
340
341                 if (!result)
342                         continue;
343
344                 /* We need to cleanup and free any completed loads */
345                 if (result == cur_result || result->status == LOAD_OK
346                                 || result->status == LOAD_ERROR) {
347                         cleanup_load(result);
348                         talloc_free(result);
349                         *results[i] = NULL;
350
351                 /* ... and cancel any pending loads, which we'll free in
352                  * the completion callback */
353                 } else if (result->status == LOAD_ASYNC) {
354                         load_url_async_cancel(result);
355                         pending = true;
356
357                 /* if we're waiting for a cancellation, we still need to
358                  * wait for the completion before freeing the boot task */
359                 } else if (result->status == LOAD_CANCELLED) {
360                         pending = true;
361                 }
362         }
363
364         if (!pending)
365                 talloc_free(task);
366 }
367
368 static void boot_process(struct load_url_result *result, void *data)
369 {
370         struct boot_task *task = data;
371         int rc = -1;
372
373         if (task->cancelled) {
374                 cleanup_cancellations(task, result);
375                 return;
376         }
377
378         if (load_pending(task->image) ||
379                         load_pending(task->initrd) ||
380                         load_pending(task->dtb))
381                 return;
382
383         if (check_load(task, "kernel image", task->image) ||
384                         check_load(task, "initrd", task->initrd) ||
385                         check_load(task, "dtb", task->dtb))
386                 goto no_load;
387
388         if (task->verify_signature) {
389                 if (load_pending(task->image_signature) ||
390                                 load_pending(task->initrd_signature) ||
391                                 load_pending(task->dtb_signature) ||
392                                 load_pending(task->cmdline_signature))
393                         return;
394
395                 if (check_load(task, "kernel image signature",
396                                         task->image_signature) ||
397                                 check_load(task, "initrd signature",
398                                         task->initrd_signature) ||
399                                 check_load(task, "dtb signature",
400                                         task->dtb_signature) ||
401                                 check_load(task, "command line signature",
402                                         task->cmdline_signature))
403                         goto no_sig_load;
404         }
405
406         /* we make a copy of the local paths, as the boot hooks might update
407          * and/or create these */
408         task->local_image = task->image ? task->image->local : NULL;
409         task->local_initrd = task->initrd ? task->initrd->local : NULL;
410         task->local_dtb = task->dtb ? task->dtb->local : NULL;
411
412         if (task->verify_signature) {
413                 task->local_image_signature = task->image_signature ?
414                         task->image_signature->local : NULL;
415                 task->local_initrd_signature = task->initrd_signature ?
416                         task->initrd_signature->local : NULL;
417                 task->local_dtb_signature = task->dtb_signature ?
418                         task->dtb_signature->local : NULL;
419                 task->local_cmdline_signature = task->cmdline_signature ?
420                         task->cmdline_signature->local : NULL;
421         }
422
423         run_boot_hooks(task);
424
425         update_status(task->status_fn, task->status_arg, BOOT_STATUS_INFO,
426                         _("performing kexec_load"));
427
428         rc = kexec_load(task);
429         if (rc == KEXEC_LOAD_SIGNATURE_FAILURE) {
430                 update_status(task->status_fn, task->status_arg,
431                                 BOOT_STATUS_ERROR,
432                                 _("signature verification failed"));
433         }
434         else if (rc == KEXEC_LOAD_SIG_SETUP_INVALID) {
435                 update_status(task->status_fn, task->status_arg,
436                                 BOOT_STATUS_ERROR,
437                                 _("invalid signature configuration"));
438         }
439         else if (rc) {
440                 update_status(task->status_fn, task->status_arg,
441                                 BOOT_STATUS_ERROR,
442                                 _("kexec load failed"));
443         }
444
445 no_sig_load:
446         cleanup_load(task->image_signature);
447         cleanup_load(task->initrd_signature);
448         cleanup_load(task->dtb_signature);
449         cleanup_load(task->cmdline_signature);
450
451 no_load:
452         cleanup_load(task->image);
453         cleanup_load(task->initrd);
454         cleanup_load(task->dtb);
455
456         if (!rc) {
457                 update_status(task->status_fn, task->status_arg,
458                                 BOOT_STATUS_INFO,
459                                 _("performing kexec reboot"));
460
461                 rc = kexec_reboot(task);
462                 if (rc) {
463                         update_status(task->status_fn, task->status_arg,
464                                         BOOT_STATUS_ERROR,
465                                         _("kexec reboot failed"));
466                 }
467         }
468 }
469
470 static int start_url_load(struct boot_task *task, const char *name,
471                 struct pb_url *url, struct load_url_result **result)
472 {
473         if (!url)
474                 return 0;
475
476         *result = load_url_async(task, url, boot_process, task);
477         if (!*result) {
478                 update_status(task->status_fn, task->status_arg,
479                                 BOOT_STATUS_ERROR,
480                                 _("Error loading %s"), name);
481                 return -1;
482         }
483         return 0;
484 }
485
486 struct boot_task *boot(void *ctx, struct discover_boot_option *opt,
487                 struct boot_command *cmd, int dry_run,
488                 boot_status_fn status_fn, void *status_arg)
489 {
490         struct pb_url *image = NULL, *initrd = NULL, *dtb = NULL;
491         struct pb_url *image_sig = NULL, *initrd_sig = NULL, *dtb_sig = NULL,
492                 *cmdline_sig = NULL;
493         const struct config *config;
494         struct boot_task *boot_task;
495         const char *boot_desc;
496         int rc;
497
498         if (opt && opt->option->name)
499                 boot_desc = opt->option->name;
500         else if (cmd && cmd->boot_image_file)
501                 boot_desc = cmd->boot_image_file;
502         else
503                 boot_desc = _("(unknown)");
504
505         update_status(status_fn, status_arg, BOOT_STATUS_INFO,
506                         _("Booting %s."), boot_desc);
507
508         if (cmd && cmd->boot_image_file) {
509                 image = pb_url_parse(opt, cmd->boot_image_file);
510         } else if (opt && opt->boot_image) {
511                 image = opt->boot_image->url;
512         } else {
513                 pb_log("%s: no image specified\n", __func__);
514                 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
515                                 _("Boot failed: no image specified"));
516                 return NULL;
517         }
518
519         if (cmd && cmd->initrd_file) {
520                 initrd = pb_url_parse(opt, cmd->initrd_file);
521         } else if (opt && opt->initrd) {
522                 initrd = opt->initrd->url;
523         }
524
525         if (cmd && cmd->dtb_file) {
526                 dtb = pb_url_parse(opt, cmd->dtb_file);
527         } else if (opt && opt->dtb) {
528                 dtb = opt->dtb->url;
529         }
530
531         boot_task = talloc_zero(ctx, struct boot_task);
532         boot_task->dry_run = dry_run;
533         boot_task->status_fn = status_fn;
534         boot_task->status_arg = status_arg;
535
536         boot_task->verify_signature = (lockdown_status() == PB_LOCKDOWN_SIGN);
537
538         if (cmd && cmd->boot_args) {
539                 boot_task->args = talloc_strdup(boot_task, cmd->boot_args);
540         } else if (opt && opt->option->boot_args) {
541                 boot_task->args = talloc_strdup(boot_task,
542                                                 opt->option->boot_args);
543         } else {
544                 boot_task->args = NULL;
545         }
546
547         if (cmd && cmd->tty)
548                 boot_task->boot_tty = talloc_strdup(boot_task, cmd->tty);
549         else {
550                 config = config_get();
551                 boot_task->boot_tty = config ? config->boot_tty : NULL;
552         }
553
554         if (boot_task->verify_signature) {
555                 if (cmd && cmd->args_sig_file) {
556                         cmdline_sig = pb_url_parse(opt, cmd->args_sig_file);
557                 } else if (opt && opt->args_sig_file) {
558                         cmdline_sig = opt->args_sig_file->url;
559                 } else {
560                         pb_log("%s: no command line signature file"
561                                 " specified\n", __func__);
562                         update_status(status_fn, status_arg, BOOT_STATUS_INFO,
563                                         _("Boot failed: no command line"
564                                                 " signature file specified"));
565                         talloc_free(boot_task);
566                         return NULL;
567                 }
568         }
569
570         /* start async loads for boot resources */
571         rc = start_url_load(boot_task, "kernel image", image, &boot_task->image)
572           || start_url_load(boot_task, "initrd", initrd, &boot_task->initrd)
573           || start_url_load(boot_task, "dtb", dtb, &boot_task->dtb);
574
575         if (boot_task->verify_signature) {
576                 /* Generate names of associated signature files and load */
577                 if (image) {
578                         image_sig = gpg_get_signature_url(ctx, image);
579                         rc |= start_url_load(boot_task,
580                                 "kernel image signature", image_sig,
581                                 &boot_task->image_signature);
582                 }
583                 if (initrd) {
584                         initrd_sig = gpg_get_signature_url(ctx, initrd);
585                         rc |= start_url_load(boot_task, "initrd signature",
586                                 initrd_sig, &boot_task->initrd_signature);
587                 }
588                 if (dtb) {
589                         dtb_sig = gpg_get_signature_url(ctx, dtb);
590                         rc |= start_url_load(boot_task, "dtb signature",
591                                 dtb_sig, &boot_task->dtb_signature);
592                 }
593
594                 rc |= start_url_load(boot_task,
595                         "kernel command line signature", cmdline_sig,
596                         &boot_task->cmdline_signature);
597         }
598
599         /* If all URLs are local, we may be done. */
600         if (rc) {
601                 /* Don't call boot_cancel() to preserve the status update */
602                 boot_task->cancelled = true;
603                 cleanup_cancellations(boot_task, NULL);
604                 return NULL;
605         }
606
607         boot_process(NULL, boot_task);
608
609         return boot_task;
610 }
611
612 void boot_cancel(struct boot_task *task)
613 {
614         task->cancelled = true;
615
616         update_status(task->status_fn, task->status_arg, BOOT_STATUS_INFO,
617                         _("Boot cancelled"));
618
619         cleanup_cancellations(task, NULL);
620 }