Received: by 2002:a05:6a10:7420:0:0:0:0 with SMTP id hk32csp2201464pxb; Sat, 19 Feb 2022 05:06:27 -0800 (PST) X-Google-Smtp-Source: ABdhPJyUIEqNnHGR8x0+f1I5WGK4dOEHGCuD/rLKRJ593GYhAn3GpO5XH8AQGwoAbRW2tdGE6feV X-Received: by 2002:aa7:c789:0:b0:410:dd40:d458 with SMTP id n9-20020aa7c789000000b00410dd40d458mr13280422eds.3.1645275986961; Sat, 19 Feb 2022 05:06:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1645275986; cv=none; d=google.com; s=arc-20160816; b=XbSMB+9Z5Ca8kKpwzhEcLCo1Dla1ngCqMHE1jlqbXseSZy4sphiiXOEjilwnFY33CG b3FSAA0fQssVnvhxMsZjyXHZVUnIy/dfJNUKE4/NhSATuxxh3tWqzwQDqNEzjTNj2OeZ XP3w80GRn+zTB0x3hwRaQmUJ52FMeRQJLlakvuTmRNdgVTJKDxygQFoQVvwRzOD4L1IH 4imBudWVpRgFw4pGTmR7AKfvrzQAyWiSbuxb0JXW35v5V3S07SvMkVJlECroVV6L5S67 jLB7iCrjWN8HTCvgOWTHrh2FCcu1GcLDn0lk8sKcAj7tY2a2FRKFa/b3NXeQPinTZRRO BMCw== 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=Z5IqDksxnanH0T8Ec4A7XO1FHLTibsIpOj59su0De6E=; b=StYqAyv/SFp8DI8FkK2ADctHlgM3MqYJCfFV8iDcZmdfRKovFmcg8o0HsRG22ak+Lh Whbmiy5vPTnLcyOr6xOGLv6pV7TGyT40fkTmBIGA77vwnhkLv6qEeUE91F5JkTZnkUPv 6VlQNNIm2axyuypg2F8D6h6LNecNXmg9JpwNkdBaaRoBnQ3BlnbQ53ybDKT3Ksla8LfQ GExZJZF9umTl7+p87hryRD1VtovA5f/mTi1tE7EyO//gvwlcVqt3SO3v26fJi9QAcaaw oV6bx9cIcRCLu4xZVjPxqIAYIq114pv/QTo6YKTuP93gjZci4D8zXzlIhUhXsSJp9SPM F96g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Kr8psRz4; 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 r10si5645024eja.139.2022.02.19.05.05.53; Sat, 19 Feb 2022 05:06:26 -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=Kr8psRz4; 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 S235487AbiBRV1m (ORCPT + 99 others); Fri, 18 Feb 2022 16:27:42 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:51504 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239814AbiBRVZn (ORCPT ); Fri, 18 Feb 2022 16:25:43 -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 EF478244650 for ; Fri, 18 Feb 2022 13:25:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1645219525; 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=Z5IqDksxnanH0T8Ec4A7XO1FHLTibsIpOj59su0De6E=; b=Kr8psRz4TUME0CB9hOFdDCIfXUxhG4NggIKwaIqXb1AFfg/eFyZYcKd7qF5B4W+8qerX2W ft1yD+vl7JKl6G5iHy0hn1jpGqRJ1uqlcPP2mQgl8lar7DLRIX3IyGLnREWEnjz+NsheFK /E4eb685mx1QJQmclrhiBQ/Fml7zGqg= Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-373-42ZjoZVrM6CYy55JZ-cUzw-1; Fri, 18 Feb 2022 16:25:23 -0500 X-MC-Unique: 42ZjoZVrM6CYy55JZ-cUzw-1 Received: by mail-wr1-f71.google.com with SMTP id q21-20020adfab15000000b001e57c9a342aso4079043wrc.6 for ; Fri, 18 Feb 2022 13:25:23 -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=Z5IqDksxnanH0T8Ec4A7XO1FHLTibsIpOj59su0De6E=; b=pKGvkQG0SW1FFYEHHOUSrlESHANkmtwmu4SL3UywY4e+UneybGN/j3NZloyIRosI78 46RLhIB4F+iLohduuY92YkLt8ReC/lYT+h09DmzARkpD0VDdIzIA7SrdZqfa09UWWzlP L7A2T0Dx05f9htIqY7becMX/Nk7aw1PnSfUUBkJMU46dTxqjTCk0FIK/1bkyF7JPOu1E daduL9la5GV+RoM5aT5fnBuc7VXsnQtNTQ2j00Dt3R4g8XUd4DtrgmkwKYnTfZva17Ne xMff8AgMJz0lWcS0CthtKYPqWTOxUjZTU4fvig0u/V2lnFnzUS90kmQRdiId6LtT4U0I Lf+w== X-Gm-Message-State: AOAM530EWnDjKWB5cweLpKmclsg3cJHPf7EJWLF0OofZRQGva8nJwZfg 8gG2hIIFvlmt1xzlWS1iz1zTqn0SlEWWbbzpf8y0TuvxK5g0wR/SSUWhD/Y/b4be2gqfeCCkMfc LcTHOf2MKT4nRwPK+ZzR8RrM= X-Received: by 2002:a7b:c756:0:b0:37b:f0f3:ed71 with SMTP id w22-20020a7bc756000000b0037bf0f3ed71mr11941157wmk.16.1645219522307; Fri, 18 Feb 2022 13:25:22 -0800 (PST) X-Received: by 2002:a7b:c756:0:b0:37b:f0f3:ed71 with SMTP id w22-20020a7bc756000000b0037bf0f3ed71mr11941145wmk.16.1645219522032; Fri, 18 Feb 2022 13:25:22 -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 a18sm39460178wrg.13.2022.02.18.13.25.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Feb 2022 13:25:21 -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, joe@perches.com, christophe.leroy@csgroup.eu, msuchanek@suse.de, oleksandr@natalenko.name Subject: [PATCH v6 07/13] module: Move extra signature support out of core code Date: Fri, 18 Feb 2022 21:25:05 +0000 Message-Id: <20220218212511.887059-8-atomlin@redhat.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220218212511.887059-1-atomlin@redhat.com> References: <20220218212511.887059-1-atomlin@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.9 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW, 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. Signed-off-by: Aaron Tomlin --- kernel/module/internal.h | 9 +++++ kernel/module/main.c | 87 ---------------------------------------- kernel/module/signing.c | 77 +++++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 87 deletions(-) diff --git a/kernel/module/internal.h b/kernel/module/internal.h index f4b7e123d625..0fdd9c0cd77d 100644 --- a/kernel/module/internal.h +++ b/kernel/module/internal.h @@ -164,3 +164,12 @@ static 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