Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp2492078pxp; Mon, 7 Mar 2022 17:03:41 -0800 (PST) X-Google-Smtp-Source: ABdhPJzDfH0ttBnYygM/D7vgpaghFOHzUe84bokRRDB014ZQLkzMiYz8HobbMCPngSDkbxc/OId4 X-Received: by 2002:a05:6402:1775:b0:416:537f:f422 with SMTP id da21-20020a056402177500b00416537ff422mr5390430edb.20.1646701421135; Mon, 07 Mar 2022 17:03:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646701421; cv=none; d=google.com; s=arc-20160816; b=gCP4LkKA8XfN+45FRYiqvQUqsmB+pwB9KnVOJsqIvBu97rinHf54WT4f0LxpPuPbdx 6D9fToP7KmYULma2DRn9L58KVVUnvGbgv2ZA/QibHGAWM0R2IOdWfOD4BwHAQR/L91wv HZdjpg6mZuUiJgj25GBzSzirBNqoAxw3z2aX1BILIminsl8mo2tHo+OQbqQNZmAlFH0Q vEM3kVYEJAtJVI7oF30YtBfplWaYNnQpHgMF8pBEAtxlO9pM0uXxcj2QWajkvvO9a5OV 3H6GBzEL4SPYeqhDE8i2Y6Cvzi77gEQBqhIXvgblxQZGxtPl2+RXKfAZeoR5Fqv7kJcW ffZg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=apZpBB6s8ANx6PnqHZt1W7YrWPlcLByEn5Sm08WSqaI=; b=nCS/92DdpubeHApmkWXCvVTvB8D5JZCeN6snnbxLlCI8ZTFhYvlbjNPfJLC7U79ocQ 8PqQM7zdY+Bcn0QI3pFTDGdzeRl1B3KpzCO5YWkjGbsSeMpw3qL/JqoPjbaawesRNLne rKRLMyZzse2NlDPt9kqs6AgpDGc2+E1ZEFIsaR/XmC4GVZxToC1ug0Gac9m5IAiK82G0 YLvORps1RnvGQ9O5TF+yU8ph6Krf1GwnJpuOf9K3gQYonIToSKwQHf21zfX7QScOOOzB 5CjNGKjESCbfueQT8DMS1vezZm8RV8uxk5Ah90TB0Jcf7RgU0GdmbVAmDlYedfh3toP3 J06g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=gV7qPDeQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g8-20020a17090670c800b006d0c18ecbafsi8653371ejk.474.2022.03.07.17.03.18; Mon, 07 Mar 2022 17:03:41 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=gV7qPDeQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240407AbiCGRqm (ORCPT + 99 others); Mon, 7 Mar 2022 12:46:42 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39716 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244504AbiCGRqe (ORCPT ); Mon, 7 Mar 2022 12:46:34 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 0492B90278 for ; Mon, 7 Mar 2022 09:45:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1646675128; 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=apZpBB6s8ANx6PnqHZt1W7YrWPlcLByEn5Sm08WSqaI=; b=gV7qPDeQfiNnTqBSW7qp/wdD0zK4S3NAuRzmruwWTfnYIoBAvBMjZdY51ZuRwF6THeCBTj uAuSoFW4wX9llaDiij0i3ZTQwuG28yX/bz/vZmVO2068Bt3QFU5VH2mmiLkIQlQJNN6TWM PX131tQQTzyC9K7xUXsibVSRQZ+pPks= Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-323-XqoNn1bcMZKQD39kOicFUQ-1; Mon, 07 Mar 2022 12:45:27 -0500 X-MC-Unique: XqoNn1bcMZKQD39kOicFUQ-1 Received: by mail-wr1-f72.google.com with SMTP id z1-20020adfec81000000b001f1f7e7ec99so850487wrn.17 for ; Mon, 07 Mar 2022 09:45:26 -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=apZpBB6s8ANx6PnqHZt1W7YrWPlcLByEn5Sm08WSqaI=; b=aEu30HK2TZJJQJ7QvFK1ZZK2JNHXxNimc7COyeH5B/D5+U1eYSrWcrWs5SD9NRoolc 26qkozdEt5GVrrRzF7jAYEh1TFQ8dm5mgZ5xeZSZA1kkUwM4O36xpdVXj63SHsu6PwW9 BRejiczHaQZykh2SpAqWGl3qWnKBAiwSKlORaVXBWCDAX7cqE1mAEz7OyrkZyi9g8wIi HtrScSbaehNh+4uanC5ngdZxZXdNYvsY44Mp5ta9tfjrqSqT7+M1YIlQeeCq0jy9bTT+ KO6XxQtvVKHlbWWL5fSLJgBXE5YjCiuqpPmhPJsik05Pgq3hup/XeE5iXoPhWre/4cfa XEuQ== X-Gm-Message-State: AOAM533/wMaO9pkaTj6dum6fP7SeBVMsizqlaSQv2JmIf5bVKdycYTwr yeWXBhYbnDsfB1KYp8grVl8VAEmE4tA8v1POl/Bi1zTiD97ikS/6p6WmzhtNSye/rjeun7amd+h 48MFOdZX1lMy0ZIBhMNYywAA= X-Received: by 2002:a5d:47ca:0:b0:1ef:d6ae:591a with SMTP id o10-20020a5d47ca000000b001efd6ae591amr8816973wrc.358.1646675125783; Mon, 07 Mar 2022 09:45:25 -0800 (PST) X-Received: by 2002:a5d:47ca:0:b0:1ef:d6ae:591a with SMTP id o10-20020a5d47ca000000b001efd6ae591amr8816954wrc.358.1646675125431; Mon, 07 Mar 2022 09:45:25 -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 l13-20020a5d4bcd000000b001f0620ecb3csm10779965wrt.40.2022.03.07.09.45.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Mar 2022 09:45:24 -0800 (PST) From: Aaron Tomlin To: mcgrof@kernel.org, christophe.leroy@csgroup.eu Cc: cl@linux.com, mbenes@suse.cz, akpm@linux-foundation.org, jeyu@kernel.org, linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org, void@manifault.com, atomlin@atomlin.com, allen.lkml@gmail.com, joe@perches.com, msuchanek@suse.de, oleksandr@natalenko.name, jason.wessel@windriver.com, daniel.thompson@linaro.org, hch@infradead.org, pmladek@suse.com Subject: [PATCH v10 07/14] module: Move extra signature support out of core code Date: Mon, 7 Mar 2022 17:45:02 +0000 Message-Id: <20220307174509.2887714-8-atomlin@redhat.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220307174509.2887714-1-atomlin@redhat.com> References: <20220307174509.2887714-1-atomlin@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org No functional change. This patch migrates additional module signature check code from core module code into kernel/module/signing.c. Reviewed-by: Christophe Leroy Signed-off-by: Aaron Tomlin --- include/linux/module.h | 12 +++--- kernel/module/internal.h | 9 +++++ kernel/module/main.c | 87 ---------------------------------------- kernel/module/signing.c | 77 +++++++++++++++++++++++++++++++++++ 4 files changed, 93 insertions(+), 92 deletions(-) diff --git a/include/linux/module.h b/include/linux/module.h index 7ec9715de7dc..5e2059f3afc7 100644 --- a/include/linux/module.h +++ b/include/linux/module.h @@ -672,7 +672,6 @@ static inline bool is_livepatch_module(struct module *mod) #endif } -bool is_module_sig_enforced(void); void set_module_sig_enforced(void); #else /* !CONFIG_MODULES... */ @@ -799,10 +798,6 @@ static inline bool module_requested_async_probing(struct module *module) return false; } -static inline bool is_module_sig_enforced(void) -{ - return false; -} static inline void set_module_sig_enforced(void) { @@ -854,11 +849,18 @@ static inline bool retpoline_module_ok(bool has_retpoline) #endif #ifdef CONFIG_MODULE_SIG +bool is_module_sig_enforced(void); + static inline bool module_sig_ok(struct module *module) { return module->sig_ok; } #else /* !CONFIG_MODULE_SIG */ +static inline bool is_module_sig_enforced(void) +{ + return false; +} + static inline bool module_sig_ok(struct module *module) { return true; diff --git a/kernel/module/internal.h b/kernel/module/internal.h index a6895bb5598a..d6f646a5da41 100644 --- a/kernel/module/internal.h +++ b/kernel/module/internal.h @@ -158,3 +158,12 @@ static inline int module_enforce_rwx_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs, return 0; } #endif /* CONFIG_STRICT_MODULE_RWX */ + +#ifdef CONFIG_MODULE_SIG +int module_sig_check(struct load_info *info, int flags); +#else /* !CONFIG_MODULE_SIG */ +static inline 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 5cd63f14b1ef..c63e10c61694 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -23,7 +23,6 @@ #include #include #include -#include #include #include #include @@ -127,28 +126,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); @@ -2569,70 +2546,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..85c8999dfecf 100644 --- a/kernel/module/signing.c +++ b/kernel/module/signing.c @@ -11,9 +11,29 @@ #include #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 +63,60 @@ 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; + 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); +} -- 2.34.1