From 86c9d34380b0074dab1ba89a569a94280d6999c4 Mon Sep 17 00:00:00 2001 From: "tpearson@raptorengineering.com" Date: Thu, 18 Aug 2016 04:45:47 -0500 Subject: Add support for GPG signature enforcement on booted kernels and related blobs This can be used to implement a form of organization-controlled secure boot, whereby kernels may be loaded from a variety of sources but they will only boot if a valid signature file is found for each component, and only if the signature is listed in the /etc/pb-lockdown file. Signed-off-by: Timothy Pearson Signed-off-by: Samuel Mendoza-Jonas (Minor build fixes and gpgme.m4, comment on secure boot in gpg.c) --- discover/Makefile.am | 3 +- discover/boot.c | 144 +++++++++++++++++++++++++++++++++++++++------- discover/boot.h | 36 ++++++++++++ discover/device-handler.c | 6 ++ discover/device-handler.h | 1 + discover/grub2/builtins.c | 8 +++ discover/kboot-parser.c | 6 ++ discover/pxe-parser.c | 7 +++ discover/user-event.c | 16 +++++- discover/yaboot-parser.c | 7 +++ 10 files changed, 208 insertions(+), 26 deletions(-) (limited to 'discover') diff --git a/discover/Makefile.am b/discover/Makefile.am index 899c9a6..4a6cbd0 100644 --- a/discover/Makefile.am +++ b/discover/Makefile.am @@ -58,7 +58,8 @@ discover_pb_discover_LDADD = \ discover/grub2/grub2-parser.ro \ discover/platform.ro \ $(core_lib) \ - $(UDEV_LIBS) + $(UDEV_LIBS) \ + $(GPGME_LIBS) discover_pb_discover_LDFLAGS = \ $(AM_LDFLAGS) \ diff --git a/discover/boot.c b/discover/boot.c index 04e7a54..c4ddfef 100644 --- a/discover/boot.c +++ b/discover/boot.c @@ -26,27 +26,14 @@ #include "resource.h" #include "platform.h" +#include + static const char *boot_hook_dir = PKG_SYSCONF_DIR "/boot.d"; enum { BOOT_HOOK_EXIT_OK = 0, BOOT_HOOK_EXIT_UPDATE = 2, }; -struct boot_task { - struct load_url_result *image; - struct load_url_result *initrd; - struct load_url_result *dtb; - const char *local_image; - const char *local_initrd; - const char *local_dtb; - const char *args; - const char *boot_tty; - boot_status_fn status_fn; - void *status_arg; - bool dry_run; - bool cancelled; -}; - /** * kexec_load - kexec load helper. */ @@ -59,20 +46,39 @@ static int kexec_load(struct boot_task *boot_task) char *s_dtb = NULL; char *s_args = NULL; + boot_task->local_initrd_override = NULL; + boot_task->local_dtb_override = NULL; + boot_task->local_image_override = NULL; + + if ((result = gpg_validate_boot_files(boot_task))) { + if (result == KEXEC_LOAD_SIGNATURE_FAILURE) { + pb_log("%s: Aborting kexec due to" + " signature verification failure\n", __func__); + goto abort_kexec; + } + } + + const char* local_initrd = (boot_task->local_initrd_override) ? + boot_task->local_initrd_override : boot_task->local_initrd; + const char* local_dtb = (boot_task->local_dtb_override) ? + boot_task->local_dtb_override : boot_task->local_dtb; + const char* local_image = (boot_task->local_image_override) ? + boot_task->local_image_override : boot_task->local_image; + p = argv; *p++ = pb_system_apps.kexec; /* 1 */ *p++ = "-l"; /* 2 */ - if (boot_task->local_initrd) { + if (local_initrd) { s_initrd = talloc_asprintf(boot_task, "--initrd=%s", - boot_task->local_initrd); + local_initrd); assert(s_initrd); *p++ = s_initrd; /* 3 */ } - if (boot_task->local_dtb) { + if (local_dtb) { s_dtb = talloc_asprintf(boot_task, "--dtb=%s", - boot_task->local_dtb); + local_dtb); assert(s_dtb); *p++ = s_dtb; /* 4 */ } @@ -82,7 +88,7 @@ static int kexec_load(struct boot_task *boot_task) assert(s_args); *p++ = s_args; /* 5 */ - *p++ = boot_task->local_image; /* 6 */ + *p++ = local_image; /* 6 */ *p++ = NULL; /* 7 */ result = process_run_simple_argv(boot_task, argv); @@ -90,6 +96,9 @@ static int kexec_load(struct boot_task *boot_task) if (result) pb_log("%s: failed: (%d)\n", __func__, result); +abort_kexec: + gpg_validate_boot_files_cleanup(boot_task); + return result; } @@ -376,23 +385,69 @@ static void boot_process(struct load_url_result *result, void *data) check_load(task, "dtb", task->dtb)) goto no_load; + if (task->verify_signature) { + if (load_pending(task->image_signature) || + load_pending(task->initrd_signature) || + load_pending(task->dtb_signature) || + load_pending(task->cmdline_signature)) + return; + + if (check_load(task, "kernel image signature", + task->image_signature) || + check_load(task, "initrd signature", + task->initrd_signature) || + check_load(task, "dtb signature", + task->dtb_signature) || + check_load(task, "command line signature", + task->cmdline_signature)) + goto no_sig_load; + } + /* we make a copy of the local paths, as the boot hooks might update * and/or create these */ task->local_image = task->image ? task->image->local : NULL; task->local_initrd = task->initrd ? task->initrd->local : NULL; task->local_dtb = task->dtb ? task->dtb->local : NULL; + if (task->verify_signature) { + task->local_image_signature = task->image_signature ? + task->image_signature->local : NULL; + task->local_initrd_signature = task->initrd_signature ? + task->initrd_signature->local : NULL; + task->local_dtb_signature = task->dtb_signature ? + task->dtb_signature->local : NULL; + task->local_cmdline_signature = task->cmdline_signature ? + task->cmdline_signature->local : NULL; + } + run_boot_hooks(task); update_status(task->status_fn, task->status_arg, BOOT_STATUS_INFO, _("performing kexec_load")); rc = kexec_load(task); - if (rc) { + if (rc == KEXEC_LOAD_SIGNATURE_FAILURE) { + update_status(task->status_fn, task->status_arg, + BOOT_STATUS_ERROR, + _("signature verification failed")); + } + else if (rc == KEXEC_LOAD_SIG_SETUP_INVALID) { + update_status(task->status_fn, task->status_arg, + BOOT_STATUS_ERROR, + _("invalid signature configuration")); + } + else if (rc) { update_status(task->status_fn, task->status_arg, - BOOT_STATUS_ERROR, _("kexec load failed")); + BOOT_STATUS_ERROR, + _("kexec load failed")); } +no_sig_load: + cleanup_load(task->image_signature); + cleanup_load(task->initrd_signature); + cleanup_load(task->dtb_signature); + cleanup_load(task->cmdline_signature); + no_load: cleanup_load(task->image); cleanup_load(task->initrd); @@ -433,6 +488,8 @@ struct boot_task *boot(void *ctx, struct discover_boot_option *opt, boot_status_fn status_fn, void *status_arg) { struct pb_url *image = NULL, *initrd = NULL, *dtb = NULL; + struct pb_url *image_sig = NULL, *initrd_sig = NULL, *dtb_sig = NULL, + *cmdline_sig = NULL; const struct config *config; struct boot_task *boot_task; const char *boot_desc; @@ -476,6 +533,8 @@ struct boot_task *boot(void *ctx, struct discover_boot_option *opt, boot_task->status_fn = status_fn; boot_task->status_arg = status_arg; + boot_task->verify_signature = (lockdown_status() == PB_LOCKDOWN_SIGN); + if (cmd && cmd->boot_args) { boot_task->args = talloc_strdup(boot_task, cmd->boot_args); } else if (opt && opt->option->boot_args) { @@ -492,11 +551,52 @@ struct boot_task *boot(void *ctx, struct discover_boot_option *opt, boot_task->boot_tty = config ? config->boot_tty : NULL; } + if (boot_task->verify_signature) { + if (cmd && cmd->args_sig_file) { + cmdline_sig = pb_url_parse(opt, cmd->args_sig_file); + } else if (opt && opt->args_sig_file) { + cmdline_sig = opt->args_sig_file->url; + } else { + pb_log("%s: no command line signature file" + " specified\n", __func__); + update_status(status_fn, status_arg, BOOT_STATUS_INFO, + _("Boot failed: no command line" + " signature file specified")); + talloc_free(boot_task); + return NULL; + } + } + /* start async loads for boot resources */ rc = start_url_load(boot_task, "kernel image", image, &boot_task->image) || start_url_load(boot_task, "initrd", initrd, &boot_task->initrd) || start_url_load(boot_task, "dtb", dtb, &boot_task->dtb); + if (boot_task->verify_signature) { + /* Generate names of associated signature files and load */ + if (image) { + image_sig = gpg_get_signature_url(ctx, image); + rc |= start_url_load(boot_task, + "kernel image signature", image_sig, + &boot_task->image_signature); + } + if (initrd) { + initrd_sig = gpg_get_signature_url(ctx, initrd); + rc |= start_url_load(boot_task, "initrd signature", + initrd_sig, &boot_task->initrd_signature); + } + if (dtb) { + dtb_sig = gpg_get_signature_url(ctx, dtb); + rc |= start_url_load(boot_task, "dtb signature", + dtb_sig, &boot_task->dtb_signature); + } + + rc |= start_url_load(boot_task, + "kernel command line signature", cmdline_sig, + &boot_task->cmdline_signature); + } + + /* If all URLs are local, we may be done. */ if (rc) { /* Don't call boot_cancel() to preserve the status update */ boot_task->cancelled = true; diff --git a/discover/boot.h b/discover/boot.h index ec61703..2190495 100644 --- a/discover/boot.h +++ b/discover/boot.h @@ -1,6 +1,9 @@ #ifndef _BOOT_H #define _BOOT_H +#include +#include "device-handler.h" + struct boot_option; struct boot_command; @@ -11,4 +14,37 @@ struct boot_task *boot(void *ctx, struct discover_boot_option *opt, boot_status_fn status_fn, void *status_arg); void boot_cancel(struct boot_task *task); + +struct boot_task { + struct load_url_result *image; + struct load_url_result *initrd; + struct load_url_result *dtb; + const char *local_image; + const char *local_initrd; + const char *local_dtb; + char *local_image_override; + char *local_initrd_override; + char *local_dtb_override; + const char *args; + const char *boot_tty; + boot_status_fn status_fn; + void *status_arg; + bool dry_run; + bool cancelled; + bool verify_signature; + struct load_url_result *image_signature; + struct load_url_result *initrd_signature; + struct load_url_result *dtb_signature; + struct load_url_result *cmdline_signature; + const char *local_image_signature; + const char *local_initrd_signature; + const char *local_dtb_signature; + const char *local_cmdline_signature; +}; + +enum { + KEXEC_LOAD_SIG_SETUP_INVALID = 253, + KEXEC_LOAD_SIGNATURE_FAILURE = 254, +}; + #endif /* _BOOT_H */ diff --git a/discover/device-handler.c b/discover/device-handler.c index c31fcea..f6b6d22 100644 --- a/discover/device-handler.c +++ b/discover/device-handler.c @@ -613,6 +613,7 @@ static bool __attribute__((used)) boot_option_is_resolved( return resource_is_resolved(opt->boot_image) && resource_is_resolved(opt->initrd) && resource_is_resolved(opt->dtb) && + resource_is_resolved(opt->args_sig_file) && resource_is_resolved(opt->icon); } @@ -638,6 +639,8 @@ static bool boot_option_resolve(struct discover_boot_option *opt, return resource_resolve(opt->boot_image, "boot_image", opt, handler) && resource_resolve(opt->initrd, "initrd", opt, handler) && resource_resolve(opt->dtb, "dtb", opt, handler) && + resource_resolve(opt->args_sig_file, "args_sig_file", opt, + handler) && resource_resolve(opt->icon, "icon", opt, handler); } @@ -652,6 +655,7 @@ static void boot_option_finalise(struct device_handler *handler, assert(!opt->option->dtb_file); assert(!opt->option->icon_file); assert(!opt->option->device_id); + assert(!opt->option->args_sig_file); if (opt->boot_image) opt->option->boot_image_file = opt->boot_image->url->full; @@ -661,6 +665,8 @@ static void boot_option_finalise(struct device_handler *handler, opt->option->dtb_file = opt->dtb->url->full; if (opt->icon) opt->option->icon_file = opt->icon->url->full; + if (opt->args_sig_file) + opt->option->args_sig_file = opt->args_sig_file->url->full; opt->option->device_id = opt->device->device->id; diff --git a/discover/device-handler.h b/discover/device-handler.h index b6f9fd5..f785ccc 100644 --- a/discover/device-handler.h +++ b/discover/device-handler.h @@ -48,6 +48,7 @@ struct discover_boot_option { struct resource *boot_image; struct resource *initrd; struct resource *dtb; + struct resource *args_sig_file; struct resource *icon; }; diff --git a/discover/grub2/builtins.c b/discover/grub2/builtins.c index 8bff732..c16b639 100644 --- a/discover/grub2/builtins.c +++ b/discover/grub2/builtins.c @@ -6,7 +6,9 @@ #include #include #include +#include +#include "discover/resource.h" #include "discover/parser.h" #include "grub2.h" @@ -69,6 +71,12 @@ static int builtin_linux(struct grub2_script *script, opt->option->boot_args = talloc_asprintf_append( opt->option->boot_args, " %s", argv[i]); + + char* args_sigfile_default = talloc_asprintf(opt, + "%s.cmdline.sig", argv[1]); + opt->args_sig_file = create_grub2_resource(opt, script->ctx->device, + root, args_sigfile_default); + talloc_free(args_sigfile_default); return 0; } diff --git a/discover/kboot-parser.c b/discover/kboot-parser.c index cebe787..f7f75e0 100644 --- a/discover/kboot-parser.c +++ b/discover/kboot-parser.c @@ -96,6 +96,12 @@ out_add: d_opt->boot_image = create_devpath_resource(d_opt, conf->dc->device, value); + char* args_sigfile_default = talloc_asprintf(d_opt, + "%s.cmdline.sig", value); + d_opt->args_sig_file = create_devpath_resource(d_opt, + conf->dc->device, args_sigfile_default); + talloc_free(args_sigfile_default); + if (root) { opt->boot_args = talloc_asprintf(opt, "root=%s %s", root, args); talloc_free(args); diff --git a/discover/pxe-parser.c b/discover/pxe-parser.c index 4481e5d..4aae8b1 100644 --- a/discover/pxe-parser.c +++ b/discover/pxe-parser.c @@ -166,6 +166,13 @@ static void pxe_process_pair(struct conf_context *ctx, url = pxe_url_join(ctx->dc, ctx->dc->conf_url, value); opt->boot_image = create_url_resource(opt, url); + char* args_sigfile_default = talloc_asprintf(opt, + "%s.cmdline.sig", value); + url = pxe_url_join(ctx->dc, ctx->dc->conf_url, + args_sigfile_default); + opt->args_sig_file = create_url_resource(opt, url); + talloc_free(args_sigfile_default); + } else if (streq(name, "INITRD")) { url = pxe_url_join(ctx->dc, ctx->dc->conf_url, value); opt->initrd = create_url_resource(opt, url); diff --git a/discover/user-event.c b/discover/user-event.c index 7350b6c..6ea754f 100644 --- a/discover/user-event.c +++ b/discover/user-event.c @@ -82,7 +82,7 @@ static void user_event_print_event(struct event __attribute__((unused)) *event) } static struct resource *user_event_resource(struct discover_boot_option *opt, - struct event *event) + struct event *event, bool gen_boot_args_sigfile) { const char *siaddr, *boot_file; struct resource *res; @@ -101,7 +101,16 @@ static struct resource *user_event_resource(struct discover_boot_option *opt, return NULL; } - url_str = talloc_asprintf(opt, "%s%s/%s", "tftp://", siaddr, boot_file); + if (gen_boot_args_sigfile) { + char* args_sigfile_default = talloc_asprintf(opt, + "%s.cmdline.sig", boot_file); + url_str = talloc_asprintf(opt, "%s%s/%s", "tftp://", siaddr, + args_sigfile_default); + talloc_free(args_sigfile_default); + } + else + url_str = talloc_asprintf(opt, "%s%s/%s", "tftp://", siaddr, + boot_file); url = pb_url_parse(opt, url_str); talloc_free(url_str); @@ -143,12 +152,13 @@ static int parse_user_event(struct discover_context *ctx, struct event *event) opt->id = talloc_asprintf(opt, "%s#%s", dev->id, val); opt->name = talloc_strdup(opt, val); - d_opt->boot_image = user_event_resource(d_opt, event); + d_opt->boot_image = user_event_resource(d_opt, event, false); if (!d_opt->boot_image) { pb_log("%s: no boot image found for %s!\n", __func__, opt->name); goto fail_opt; } + d_opt->args_sig_file = user_event_resource(d_opt, event, true); val = event_get_param(event, "rootpath"); if (val) { diff --git a/discover/yaboot-parser.c b/discover/yaboot-parser.c index aa99392..b62f39d 100644 --- a/discover/yaboot-parser.c +++ b/discover/yaboot-parser.c @@ -114,6 +114,13 @@ static void yaboot_finish(struct conf_context *conf) /* populate the boot option from state data */ state->opt->boot_image = create_yaboot_devpath_resource(state, conf, state->boot_image); + + char* args_sigfile_default = talloc_asprintf(opt, + "%s.cmdline.sig", state->boot_image); + state->opt->args_sig_file = create_yaboot_devpath_resource(state, + conf, args_sigfile_default); + talloc_free(args_sigfile_default); + if (state->initrd) { state->opt->initrd = create_yaboot_devpath_resource(state, conf, state->initrd); -- cgit v1.2.1