Received: by 2002:a05:6a10:9afc:0:0:0:0 with SMTP id t28csp3309038pxm; Mon, 28 Feb 2022 17:05:07 -0800 (PST) X-Google-Smtp-Source: ABdhPJyPqp04kOTl3v7AYYgU51lkuk1nnUvCCyOlacT74pBP7ozOgQWU5bvtbGsOrtkUVc8uavrf X-Received: by 2002:a05:6a00:ccd:b0:4f1:15bd:2d94 with SMTP id b13-20020a056a000ccd00b004f115bd2d94mr24588001pfv.49.1646096707315; Mon, 28 Feb 2022 17:05:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646096707; cv=none; d=google.com; s=arc-20160816; b=M+IeEgL2WNayFJfEMicJzSG7SkuHTxO4aGTGByJ00L+oDobyie43cCmhor4SaTug+b twbsZQC1DiH64gGmDCYgzxEnAv1IT63mvdEEuaOSi8+zMrOKyNzSgst7TnW8OnYJMXUN fwuYEaQrksYs8yBzmCsJiz92y84Qq8k+ZyzK7vaNEbDWu/vFQ7xi/Ap1slsEERGCkEkn jeLWGz8WSABuYRVUPF6cUVbGFtjlrbqSg1CBmmRIvqRLS8vaKdtzveGmyC5ANbkYQMHW SKxKVChcgzTjOTXGsBo/UgLeR4jSwlOdRFkYRatpq7hjfYC69Y+FC8b9CRdvRZ6mqVaV tb9g== 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=hXeQq/NvfV1mYr3q1cCIbDEO5g1KWL7GoRmGq5OMelc=; b=W6uMcfL6RhCVRf7eQenKDJxvc7rBReuw3oybdsJoE6OLwRJBvxTRS9w6FKFKeU4hS8 /duPahnXqclufgx+3wSHTrQ1DN0PAiQWg4OCC38X8HheH49SoulnAvaO4ICn0y0kMQZp xFKnJAf4bariVMinWtJn9FwfdCJrRz+Xf0/xNgnwoMcBIBTlW+47Frq8kQVC8zAj3SCo 9vpXWFM31+58H9HfNjKp8F/7GqtEiOt/m7ZnoYLlquzDkgvtNS42iDgF9YJXRNLqD026 +iFJWAOVcpvhRac6gPZ2v7tMRIWF9riW/0y6WYErXRhM2J/077dnFRyL61oa+S1VMruW bylA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=XmmlpOVE; 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 c137-20020a63358f000000b0037589f4a3b8si11340162pga.207.2022.02.28.17.04.47; Mon, 28 Feb 2022 17:05:07 -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=XmmlpOVE; 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 S231713AbiB1Xoq (ORCPT + 99 others); Mon, 28 Feb 2022 18:44:46 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33106 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231696AbiB1Xof (ORCPT ); Mon, 28 Feb 2022 18:44:35 -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 B12EEF4D21 for ; Mon, 28 Feb 2022 15:43:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1646091823; 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=hXeQq/NvfV1mYr3q1cCIbDEO5g1KWL7GoRmGq5OMelc=; b=XmmlpOVE7/teNo/EtQrwX0qvITeQWFaz+1JOZD0RaGe4RFv7its/9FlY3If+mR+SotyfO/ AWuW3QTbFtdBJiX+LdcTipuPXZVRIHpkHEPrwLNt/NkSbGxsEIQg1dcAVS1t+3Fq5bKTFV f6xirvfAfFyJP+z2tZQxa7FGxP2lvYI= Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-615-8UsyuO42PIquz4RImVc9HA-1; Mon, 28 Feb 2022 18:43:42 -0500 X-MC-Unique: 8UsyuO42PIquz4RImVc9HA-1 Received: by mail-wr1-f70.google.com with SMTP id p18-20020adfba92000000b001e8f7697cc7so2570861wrg.20 for ; Mon, 28 Feb 2022 15:43:42 -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=hXeQq/NvfV1mYr3q1cCIbDEO5g1KWL7GoRmGq5OMelc=; b=x4/gppstJWpDL5cKXKPLgyVqu9BQRmixiDqRm+64/ZCBQ3lGAgFyxmCzC3+wt+ASqC qpRGMJT9Md9HnGoDnSdG/ny4gHUEhMB4Rx83HMy6EkpWX6ZDrZ5sYM9VIz1TxJtZPEW5 3P8fodzbDjyGzg1mQ2gZWZegYpSTfRx22OJ6F6Gyl9Bw6+QwHywOWshiS/WpeQeEL+2H 9P20pTBcktCw4Ucn6jbud+rseT62Hs6A2/+d1DRVRL7XeU0IbZSkMBhObLVZLyZycn8b nOBslyEoJrOyrI/gOEzKkqcy4pseymt6AYgadvCBYShP5NbdWtGCyQ+sscq29Ud+E8S+ CtaA== X-Gm-Message-State: AOAM530EU1iQ0jwSOVmrd2JHOzJhcs2AZuGIGfz4Cx5bQzViG0XhsTBn iaSNEEvE1ukS2wb+sLzU5EM6NUuNjTlnxS0qVyrSeNM8st4wf5iTE225kY4jkSpjjDNv6JQHu2k nuM3nIUzYMXOngXKUEo+HIhw= X-Received: by 2002:adf:fd87:0:b0:1ed:e0c3:a2d4 with SMTP id d7-20020adffd87000000b001ede0c3a2d4mr17235816wrr.374.1646091820042; Mon, 28 Feb 2022 15:43:40 -0800 (PST) X-Received: by 2002:adf:fd87:0:b0:1ed:e0c3:a2d4 with SMTP id d7-20020adffd87000000b001ede0c3a2d4mr17235798wrr.374.1646091819809; Mon, 28 Feb 2022 15:43:39 -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 n4-20020a05600c4f8400b00380e45cd564sm917233wmq.8.2022.02.28.15.43.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Feb 2022 15:43:38 -0800 (PST) From: Aaron Tomlin To: mcgrof@kernel.org, christophe.leroy@csgroup.eu, pmladek@suse.com 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 Subject: [PATCH v9 07/14] module: Move extra signature support out of core code Date: Mon, 28 Feb 2022 23:43:15 +0000 Message-Id: <20220228234322.2073104-8-atomlin@redhat.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220228234322.2073104-1-atomlin@redhat.com> References: <20220228234322.2073104-1-atomlin@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3.2 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE autolearn=unavailable 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 --- 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 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