From patchwork Fri Jan 28 20:39:28 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aaron Tomlin X-Patchwork-Id: 12729079 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B52FCC433FE for ; Fri, 28 Jan 2022 20:39:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344495AbiA1Ujx (ORCPT ); Fri, 28 Jan 2022 15:39:53 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:22326 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344447AbiA1Ujr (ORCPT ); Fri, 28 Jan 2022 15:39:47 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1643402387; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ht4TIlDcHgebbMTmRHom2WWk1vPWsK5LmrKtQWe+Ves=; b=YqpLryaCmZ1O+eT8pq87819AzYrLe285sX+qVWYu4L/y1tbAOFoLS70lqhx38WJAdkv3Fm 0j3i4bzULye4FHHTPYvefdVECQ+WMuYSed5FhO93JzDsrqCB9J+XY4Totr4EvgC7Bvt1Va B7FzEDpKLTmUQcWZ29bMpAkVbukDRCU= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-471-zs9QgjG_MgyYzu5JslkONw-1; Fri, 28 Jan 2022 15:39:46 -0500 X-MC-Unique: zs9QgjG_MgyYzu5JslkONw-1 Received: by mail-wm1-f71.google.com with SMTP id l20-20020a05600c1d1400b0035153bf34c3so3690364wms.2 for ; Fri, 28 Jan 2022 12:39:45 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ht4TIlDcHgebbMTmRHom2WWk1vPWsK5LmrKtQWe+Ves=; b=wmFK10Z3Oo1dVr3qEIh5KNAwEr/aAtvwIXwult+n9GFzOOwEz0140AnVHxoTEhBZCQ yrT6ce4RGS/ADGxFbx9IgHd/RoC3ho8GdVc7fuQAltlxW8rcNNBKOPAzjx6aggPyy+HZ Yw1ml98KXg9geZ23GLdz4o19HY+OQKdFLy67znzNW/rTNtydwd0Y16IWh4HZqwVVsZ/K yNWXUZnjhZ5lpercMN+o1jaL7HUhb1S55/PYCznxZiqtD2/Wxu/bGr/smo13WCtuQRhk YOrE3AK0OAE+oqrVz30IevYA4RQogV1B9mBmCcgmL3LyRiaXsdHABKmQOycK1QtHCg6e y7NQ== X-Gm-Message-State: AOAM532VnUFZ+khB5LNfjXxwcCKacikF9dPSsoGSXSpjMvNJms+3Ygmg JxtlvDM7WC6osfP6J+ifEJjm/NVZ1q4OTZWcbQA2z64i6U1ElV+qKhbNp+YbZtWZ4zMIDACphP9 H8jgG0EtcoSXpsvMFR+wn99Z+ X-Received: by 2002:a05:600c:3b2a:: with SMTP id m42mr17175242wms.58.1643402384753; Fri, 28 Jan 2022 12:39:44 -0800 (PST) X-Google-Smtp-Source: ABdhPJxsPYXDh8KcoCnxc2Q7JY8QjTh6O903oitK5a0y8Rf+i5qqBNxYbWA9fluteKxP0ATcAIXkxw== X-Received: by 2002:a05:600c:3b2a:: with SMTP id m42mr17175232wms.58.1643402384555; Fri, 28 Jan 2022 12:39:44 -0800 (PST) Received: from localhost (cpc111743-lutn13-2-0-cust979.9-3.cable.virginm.net. [82.17.115.212]) by smtp.gmail.com with ESMTPSA id b15sm5248133wrs.93.2022.01.28.12.39.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 Jan 2022 12:39:44 -0800 (PST) From: Aaron Tomlin To: mcgrof@kernel.org Cc: cl@linux.com, pmladek@suse.com, mbenes@suse.cz, akpm@linux-foundation.org, jeyu@kernel.org, linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org, live-patching@vger.kernel.org, atomlin@atomlin.com, ghalat@redhat.com, allen.lkml@gmail.com Subject: [RFC PATCH v3 07/13] module: Move extra signature support out of core code Date: Fri, 28 Jan 2022 20:39:28 +0000 Message-Id: <20220128203934.600247-8-atomlin@redhat.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220128203934.600247-1-atomlin@redhat.com> References: <20220128203934.600247-1-atomlin@redhat.com> MIME-Version: 1.0 Precedence: bulk List-ID: No functional change. This patch migrates additional module signature check code from core module code into kernel/module/signing.c. Signed-off-by: Aaron Tomlin --- include/linux/module.h | 5 ++- kernel/module/internal.h | 9 +++++ kernel/module/main.c | 87 ---------------------------------------- kernel/module/signing.c | 75 ++++++++++++++++++++++++++++++++++ 4 files changed, 87 insertions(+), 89 deletions(-) diff --git a/include/linux/module.h b/include/linux/module.h index 7bf93c0d87e6..68dabc036100 100644 --- a/include/linux/module.h +++ b/include/linux/module.h @@ -758,8 +758,8 @@ static inline bool set_livepatch_module(struct module *mod) } #endif /* CONFIG_LIVEPATCH */ -bool is_module_sig_enforced(void); -void set_module_sig_enforced(void); +extern bool is_module_sig_enforced(void); +extern void set_module_sig_enforced(void); #else /* !CONFIG_MODULES... */ @@ -949,6 +949,7 @@ static inline bool module_sig_ok(struct module *module) { return true; } +#define sig_enforce false #endif /* CONFIG_MODULE_SIG */ int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *, diff --git a/kernel/module/internal.h b/kernel/module/internal.h index 0cd624179545..5aab87716d9f 100644 --- a/kernel/module/internal.h +++ b/kernel/module/internal.h @@ -80,3 +80,12 @@ static inline void module_decompress_cleanup(struct load_info *info) { } #endif + +#ifdef CONFIG_MODULE_SIG +extern int module_sig_check(struct load_info *info, int flags); +#else /* !CONFIG_MODULE_SIG */ +static int module_sig_check(struct load_info *info, int flags) +{ + return 0; +} +#endif /* !CONFIG_MODULE_SIG */ diff --git a/kernel/module/main.c b/kernel/module/main.c index 4f590b0c7aca..36ed4dd64cac 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -22,7 +22,6 @@ #include #include #include -#include #include #include #include @@ -114,28 +113,6 @@ static void module_assert_mutex_or_preempt(void) #endif } -#ifdef CONFIG_MODULE_SIG -static bool sig_enforce = IS_ENABLED(CONFIG_MODULE_SIG_FORCE); -module_param(sig_enforce, bool_enable_only, 0644); - -void set_module_sig_enforced(void) -{ - sig_enforce = true; -} -#else -#define sig_enforce false -#endif - -/* - * Export sig_enforce kernel cmdline parameter to allow other subsystems rely - * on that instead of directly to CONFIG_MODULE_SIG_FORCE config. - */ -bool is_module_sig_enforced(void) -{ - return sig_enforce; -} -EXPORT_SYMBOL(is_module_sig_enforced); - /* Block module loading/unloading? */ int modules_disabled = 0; core_param(nomodule, modules_disabled, bint, 0); @@ -2516,70 +2493,6 @@ static inline void kmemleak_load_module(const struct module *mod, } #endif -#ifdef CONFIG_MODULE_SIG -static int module_sig_check(struct load_info *info, int flags) -{ - int err = -ENODATA; - const unsigned long markerlen = sizeof(MODULE_SIG_STRING) - 1; - const char *reason; - const void *mod = info->hdr; - bool mangled_module = flags & (MODULE_INIT_IGNORE_MODVERSIONS | - MODULE_INIT_IGNORE_VERMAGIC); - /* - * Do not allow mangled modules as a module with version information - * removed is no longer the module that was signed. - */ - if (!mangled_module && - info->len > markerlen && - memcmp(mod + info->len - markerlen, MODULE_SIG_STRING, markerlen) == 0) { - /* We truncate the module to discard the signature */ - info->len -= markerlen; - err = mod_verify_sig(mod, info); - if (!err) { - info->sig_ok = true; - return 0; - } - } - - /* - * We don't permit modules to be loaded into the trusted kernels - * without a valid signature on them, but if we're not enforcing, - * certain errors are non-fatal. - */ - switch (err) { - case -ENODATA: - reason = "unsigned module"; - break; - case -ENOPKG: - reason = "module with unsupported crypto"; - break; - case -ENOKEY: - reason = "module with unavailable key"; - break; - - default: - /* - * All other errors are fatal, including lack of memory, - * unparseable signatures, and signature check failures -- - * even if signatures aren't required. - */ - return err; - } - - if (is_module_sig_enforced()) { - pr_notice("Loading of %s is rejected\n", reason); - return -EKEYREJECTED; - } - - return security_locked_down(LOCKDOWN_MODULE_SIGNATURE); -} -#else /* !CONFIG_MODULE_SIG */ -static int module_sig_check(struct load_info *info, int flags) -{ - return 0; -} -#endif /* !CONFIG_MODULE_SIG */ - static int validate_section_offset(struct load_info *info, Elf_Shdr *shdr) { #if defined(CONFIG_64BIT) diff --git a/kernel/module/signing.c b/kernel/module/signing.c index 8aeb6d2ee94b..ff41541e982a 100644 --- a/kernel/module/signing.c +++ b/kernel/module/signing.c @@ -11,9 +11,28 @@ #include #include #include +#include #include #include "internal.h" +static bool sig_enforce = IS_ENABLED(CONFIG_MODULE_SIG_FORCE); +module_param(sig_enforce, bool_enable_only, 0644); + +/* + * Export sig_enforce kernel cmdline parameter to allow other subsystems rely + * on that instead of directly to CONFIG_MODULE_SIG_FORCE config. + */ +bool is_module_sig_enforced(void) +{ + return sig_enforce; +} +EXPORT_SYMBOL(is_module_sig_enforced); + +void set_module_sig_enforced(void) +{ + sig_enforce = true; +} + /* * Verify the signature on a module. */ @@ -43,3 +62,59 @@ int mod_verify_sig(const void *mod, struct load_info *info) VERIFYING_MODULE_SIGNATURE, NULL, NULL); } + +int module_sig_check(struct load_info *info, int flags) +{ + int err = -ENODATA; + const unsigned long markerlen = sizeof(MODULE_SIG_STRING) - 1; + const char *reason; + const void *mod = info->hdr; + + /* + * Require flags == 0, as a module with version information + * removed is no longer the module that was signed + */ + if (flags == 0 && + info->len > markerlen && + memcmp(mod + info->len - markerlen, MODULE_SIG_STRING, markerlen) == 0) { + /* We truncate the module to discard the signature */ + info->len -= markerlen; + err = mod_verify_sig(mod, info); + if (!err) { + info->sig_ok = true; + return 0; + } + } + + /* + * We don't permit modules to be loaded into the trusted kernels + * without a valid signature on them, but if we're not enforcing, + * certain errors are non-fatal. + */ + switch (err) { + case -ENODATA: + reason = "unsigned module"; + break; + case -ENOPKG: + reason = "module with unsupported crypto"; + break; + case -ENOKEY: + reason = "module with unavailable key"; + break; + + default: + /* + * All other errors are fatal, including lack of memory, + * unparseable signatures, and signature check failures -- + * even if signatures aren't required. + */ + return err; + } + + if (is_module_sig_enforced()) { + pr_notice("Loading of %s is rejected\n", reason); + return -EKEYREJECTED; + } + + return security_locked_down(LOCKDOWN_MODULE_SIGNATURE); +}