Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp2751813pxb; Mon, 31 Jan 2022 03:36:31 -0800 (PST) X-Google-Smtp-Source: ABdhPJxDd73p1tMx8H0zZHAuS9+DicH026VNgUn2v/1zLc6Lqf5qSqVVxELFq+f+7VNMnAixnwEa X-Received: by 2002:a63:4e5c:: with SMTP id o28mr120167pgl.263.1643628990790; Mon, 31 Jan 2022 03:36:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643628990; cv=none; d=google.com; s=arc-20160816; b=cSvGLBYmKSBML7hzVCkK/qC3DjFYoUJIN7vIyyRTkc4Oy/FFvfOi58CEQJdfzFFbyQ iZik7jD69zJUAtJvPgae2KUhAz7HZ6xyDMViWLMV2fl5E5Bwzvre38nc/CADxXSBuazg Pv0RxuQKvush3pL/nik+48INpancf6x4NeZlymO98nf/PgxpL/bcqAflXOYzDe86+oxd aawPGOyw3UyxNkrC3Ua+xrSWzxDh5+BLxye/oAYaTpfpvin7l1FECJgWIde55rlIJSMk wmLSwpAb4fNmfYr6xn93ZUDFiizaWQTSQtyyGIuesmx3iOXCg6vUPKQtP2UOPJMTpOcW ehtA== 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=ht4TIlDcHgebbMTmRHom2WWk1vPWsK5LmrKtQWe+Ves=; b=B1nYunufNaRKXed/LIRTmVZv+Ane9f1DoliiU8zI8QNKP5wBLvVvtwW3kXgLZ7R3Wy AiM+I3VB2w71EQj41V90INHVtlTWXrPe4MYsCL4/bUCAVVzjMN1jS2/o4DXRMtLC+3kJ wJVZwHYYABGy6AYIVjR7uj16CkZQAHpruqPxZeouUNTF3kzWXynWR8Tl0xvUGfCBFiNk JUdTfG0bpMzFCaxcGLm67HgqXcnDklm0hfOVgYVD7duBJWu5He2yV6oasNhfxopLUg4w 6DOElNmNcUaSxEJcQRxn2mCx+MP3v7zrQqMSDa+nUiYyjRQcF3a8JZY9mkcuE3V5h3gV RaIA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=YqpLryaC; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id i9si13662643plr.128.2022.01.31.03.36.19; Mon, 31 Jan 2022 03:36:30 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=YqpLryaC; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S1351292AbiA1UkK (ORCPT + 99 others); Fri, 28 Jan 2022 15:40:10 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:28342 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344437AbiA1Ujr (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-465-xEUXT96IM_WXIhDMxwbmUQ-1; Fri, 28 Jan 2022 15:39:46 -0500 X-MC-Unique: xEUXT96IM_WXIhDMxwbmUQ-1 Received: by mail-wm1-f71.google.com with SMTP id o194-20020a1ca5cb000000b00350b177fb22so5663579wme.3 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=gFQDCe3hSadBoaoKWVHKTq8nlRxuZFD+mizZI0CD+NbFEqROTOaOHh8VfYrWXKeiGj /KgP2JgHL1+Ph2Tmm1qkcidMTn1Y6LSWgXeqUFqjHjeVeuhlBKO6R/toBzTdbu08cFgI 8uYULNxaQXDwq95Oet8wj6K5VyHhxDAuUsowUrPtCFttLbDIis9+Z/csJKp/p0Xg+X59 mGixThYr6GI40L5nG1895iXLcmrAHDgSs3WMwkESnXcrBPQWNnl5fsoJOO7oBtYy4MRt RyMDbrHxw4nHHkSrKam6N7Y4hBBQ+76IoTJbxN1Ea6IQ5zZf8VEizk5ump2vpwvfa9ut 0mhQ== X-Gm-Message-State: AOAM533gINGmI6A91Ue8t476HrWjUySfkN8twdqg7b6OgADPPBFgEg9y vTWzZ6Ncg8kC17eEwXWJ+3uURxbcbtNkVVJjB0Jii2dxCpj46X4DI6TW0Ke1lmA5btIlFMJuvFU aeVdq02x3ErlssAcbiPxZO5A= X-Received: by 2002:a05:600c:3b2a:: with SMTP id m42mr17175249wms.58.1643402384755; Fri, 28 Jan 2022 12:39:44 -0800 (PST) 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 Content-Transfer-Encoding: 8bit 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 --- 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); +} -- 2.34.1