From patchwork Thu Aug 24 08:17:58 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: AKASHI Takahiro X-Patchwork-Id: 110881 Delivered-To: patch@linaro.org Received: by 10.140.95.78 with SMTP id h72csp5315858qge; Thu, 24 Aug 2017 01:19:36 -0700 (PDT) X-Received: by 10.84.142.129 with SMTP id 1mr1708274plx.179.1503562776692; Thu, 24 Aug 2017 01:19:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1503562776; cv=none; d=google.com; s=arc-20160816; b=bKJwczftm7hYjlnNuBr2/DptZhkfY7ivQCgYldiGm2tsJjGcIbFjH+B74VTTwRsoMe gGNWGxLzs9j92xPE+T2OsXKP3fnU1AEQ0ItUEehzuLJLa0MdsHxI+Jhuf++KAmwxNurz 2pw9OVeNp3EByIUC+g3VDriYSA+VEaUbGgSZ68d7hcnK0ySxqYmPr7sAWNGYGvHdwihz +b27+mj234M5Dy50sGAYLh2MLG0fVM9uA2/nnxPxyEuKWwg1HJnC7bk5IL7prMw9D8kr 83FRZpdraYLzfH0fUZWqqUdk5wKDzyag6cF09WjeBBTk6nM19ShY741WOmy09gASPs0e 707w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=3Wc3VF3K3Rr5czr1jOJUnQoDSz7V0DBKvp0WD3BA0Zg=; b=bemJJtl3bhO1u7Jz4BN2Bl6Bn+vWR5t6WEjDnpWTDXjtfTJietQFSUxdMz9+JuD81o ZqpGzhQ/WdO50x76KFHulm7ryEApvAdyfE92PwI7lk+UQ45Zc/KKWe5lEsb3MqAbayM9 ZbsOZ56EUaNB4ZkWy+QwO1FoOHQlcGgN8mPF5oy0rguR0SoZqVbOUzII/4u5AP96xuDP 5sbe6Fi3Vw41inrin4Hf1PreDoXO23fYGMLtypCIndCazhuIavmyV0HFOKRJJ51pznK5 aQhBficZcWIn8zdvnxLxeZCGzae87FQrFHY3B8++1lJZCN96G/wLuUB/WtjmyF19gtmR PsnQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=ZlHEvf+L; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id g33si2561477plb.846.2017.08.24.01.19.36; Thu, 24 Aug 2017 01:19:36 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=ZlHEvf+L; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752254AbdHXITU (ORCPT + 26 others); Thu, 24 Aug 2017 04:19:20 -0400 Received: from mail-pg0-f46.google.com ([74.125.83.46]:36598 "EHLO mail-pg0-f46.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751368AbdHXIRe (ORCPT ); Thu, 24 Aug 2017 04:17:34 -0400 Received: by mail-pg0-f46.google.com with SMTP id 83so13437767pgb.3 for ; Thu, 24 Aug 2017 01:17:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=3Wc3VF3K3Rr5czr1jOJUnQoDSz7V0DBKvp0WD3BA0Zg=; b=ZlHEvf+LH7OtwKmQLVetbwRRQz+CLUaiYUN+U6e8g5s1Nw+NPH8/NT4WFYLhIXmAu2 V8r+f+FWyIs+vWWTV+tckHg/t60tj6CqcJetpooIPrDXgH+lF5lvLsCuYiAWV3N93x5X UyaTsssHdCIeXb78HWiyD7G9p6OGpmLKFfgT4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=3Wc3VF3K3Rr5czr1jOJUnQoDSz7V0DBKvp0WD3BA0Zg=; b=mEbj7VWgCE7QQTcoUTEzHHWD/afle4PbIfK7+y+TPSscN3VZCghr3oREz6tCSsGJ0i ZDHXMLmsK/Yu4Bws2tY0RjrACETNXxzfIxDzwlpLGO/QyvNh4749xUx6Vrq8CzJUrb5t 4pOLT6C25AxC4bv9F7Ld74vn68rUeUA4NEXW3nu67W5/TubpBLk1NORzsVIpG4yT0Uwh 0eGDqmpwScWzJv42vaBq0FF+CQjPqOc4t7P3yGYPD6am1cbVmKFJTnsySixLMbPdxT94 P+k2qSjRz7Eid7QUbQiq8NICCmq2dRW6q9uX4WPnRRwOJDrWM6reOMC1eKH3JxKZ8D/8 bs/w== X-Gm-Message-State: AHYfb5h8bRFA8fdK4CLlhN7F6cRxlXBXyW3bkqVtbKjztTrif8S64yCo qaXcWaO4O8fkOvhT X-Received: by 10.98.30.131 with SMTP id e125mr5753954pfe.244.1503562653436; Thu, 24 Aug 2017 01:17:33 -0700 (PDT) Received: from linaro.org ([121.95.100.191]) by smtp.googlemail.com with ESMTPSA id x11sm7187363pfk.43.2017.08.24.01.17.32 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 24 Aug 2017 01:17:33 -0700 (PDT) From: AKASHI Takahiro To: catalin.marinas@arm.com, will.deacon@arm.com, bauerman@linux.vnet.ibm.com, dhowells@redhat.com, vgoyal@redhat.com, herbert@gondor.apana.org.au, davem@davemloft.net, akpm@linux-foundation.org, mpe@ellerman.id.au, dyoung@redhat.com, bhe@redhat.com, arnd@arndb.de, ard.biesheuvel@linaro.org Cc: kexec@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [PATCH 01/14] MODSIGN: Export module signature definitions Date: Thu, 24 Aug 2017 17:17:58 +0900 Message-Id: <20170824081811.19299-2-takahiro.akashi@linaro.org> X-Mailer: git-send-email 2.14.1 In-Reply-To: <20170824081811.19299-1-takahiro.akashi@linaro.org> References: <20170824081811.19299-1-takahiro.akashi@linaro.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Thiago Jung Bauermann IMA will use the module_signature format for append signatures, so export the relevant definitions and factor out the code which verifies that the appended signature trailer is valid. Also, create a CONFIG_MODULE_SIG_FORMAT option so that IMA can select it and be able to use validate_module_signature without having to depend on CONFIG_MODULE_SIG. Signed-off-by: Thiago Jung Bauermann --- include/linux/module.h | 3 -- include/linux/module_signature.h | 47 +++++++++++++++++++++++++ init/Kconfig | 6 +++- kernel/Makefile | 2 +- kernel/module.c | 1 + kernel/module_signing.c | 74 +++++++++++++++++----------------------- 6 files changed, 85 insertions(+), 48 deletions(-) create mode 100644 include/linux/module_signature.h -- 2.14.1 diff --git a/include/linux/module.h b/include/linux/module.h index e7bdd549e527..672ad2016262 100644 --- a/include/linux/module.h +++ b/include/linux/module.h @@ -23,9 +23,6 @@ #include #include -/* In stripped ARM and x86-64 modules, ~ is surprisingly rare. */ -#define MODULE_SIG_STRING "~Module signature appended~\n" - /* Not Yet Implemented */ #define MODULE_SUPPORTED_DEVICE(name) diff --git a/include/linux/module_signature.h b/include/linux/module_signature.h new file mode 100644 index 000000000000..e80728e5b86c --- /dev/null +++ b/include/linux/module_signature.h @@ -0,0 +1,47 @@ +/* Module signature handling. + * + * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved. + * Written by David Howells (dhowells@redhat.com) + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public Licence + * as published by the Free Software Foundation; either version + * 2 of the Licence, or (at your option) any later version. + */ + +#ifndef _LINUX_MODULE_SIGNATURE_H +#define _LINUX_MODULE_SIGNATURE_H + +/* In stripped ARM and x86-64 modules, ~ is surprisingly rare. */ +#define MODULE_SIG_STRING "~Module signature appended~\n" + +enum pkey_id_type { + PKEY_ID_PGP, /* OpenPGP generated key ID */ + PKEY_ID_X509, /* X.509 arbitrary subjectKeyIdentifier */ + PKEY_ID_PKCS7, /* Signature in PKCS#7 message */ +}; + +/* + * Module signature information block. + * + * The constituents of the signature section are, in order: + * + * - Signer's name + * - Key identifier + * - Signature data + * - Information block + */ +struct module_signature { + u8 algo; /* Public-key crypto algorithm [0] */ + u8 hash; /* Digest algorithm [0] */ + u8 id_type; /* Key identifier type [PKEY_ID_PKCS7] */ + u8 signer_len; /* Length of signer's name [0] */ + u8 key_id_len; /* Length of key identifier [0] */ + u8 __pad[3]; + __be32 sig_len; /* Length of signature data */ +}; + +int validate_module_sig(const struct module_signature *ms, size_t file_len); +int mod_verify_sig(const void *mod, unsigned long *_modlen); + +#endif /* _LINUX_MODULE_SIGNATURE_H */ diff --git a/init/Kconfig b/init/Kconfig index 8514b25db21c..c3ac1170b93a 100644 --- a/init/Kconfig +++ b/init/Kconfig @@ -1734,7 +1734,7 @@ config MODULE_SRCVERSION_ALL config MODULE_SIG bool "Module signature verification" depends on MODULES - select SYSTEM_DATA_VERIFICATION + select MODULE_SIG_FORMAT help Check modules for valid signatures upon load: the signature is simply appended to the module. For more information see @@ -1749,6 +1749,10 @@ config MODULE_SIG debuginfo strip done by some packagers (such as rpmbuild) and inclusion into an initramfs that wants the module size reduced. +config MODULE_SIG_FORMAT + def_bool n + select SYSTEM_DATA_VERIFICATION + config MODULE_SIG_FORCE bool "Require modules to be validly signed" depends on MODULE_SIG diff --git a/kernel/Makefile b/kernel/Makefile index 4cb8e8b23c6e..d5f9748ab19f 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -56,7 +56,7 @@ obj-y += up.o endif obj-$(CONFIG_UID16) += uid16.o obj-$(CONFIG_MODULES) += module.o -obj-$(CONFIG_MODULE_SIG) += module_signing.o +obj-$(CONFIG_MODULE_SIG_FORMAT) += module_signing.o obj-$(CONFIG_KALLSYMS) += kallsyms.o obj-$(CONFIG_BSD_PROCESS_ACCT) += acct.o obj-$(CONFIG_CRASH_CORE) += crash_core.o diff --git a/kernel/module.c b/kernel/module.c index 40f983cbea81..52921fccb51a 100644 --- a/kernel/module.c +++ b/kernel/module.c @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include diff --git a/kernel/module_signing.c b/kernel/module_signing.c index 937c844bee4a..204c60d4cc9f 100644 --- a/kernel/module_signing.c +++ b/kernel/module_signing.c @@ -11,36 +11,38 @@ #include #include +#include #include #include #include #include "module-internal.h" -enum pkey_id_type { - PKEY_ID_PGP, /* OpenPGP generated key ID */ - PKEY_ID_X509, /* X.509 arbitrary subjectKeyIdentifier */ - PKEY_ID_PKCS7, /* Signature in PKCS#7 message */ -}; - -/* - * Module signature information block. - * - * The constituents of the signature section are, in order: +/** + * validate_module_sig - validate that the given signature is sane * - * - Signer's name - * - Key identifier - * - Signature data - * - Information block + * @ms: Signature to validate. + * @file_len: Size of the file to which @ms is appended. */ -struct module_signature { - u8 algo; /* Public-key crypto algorithm [0] */ - u8 hash; /* Digest algorithm [0] */ - u8 id_type; /* Key identifier type [PKEY_ID_PKCS7] */ - u8 signer_len; /* Length of signer's name [0] */ - u8 key_id_len; /* Length of key identifier [0] */ - u8 __pad[3]; - __be32 sig_len; /* Length of signature data */ -}; +int validate_module_sig(const struct module_signature *ms, size_t file_len) +{ + if (be32_to_cpu(ms->sig_len) >= file_len - sizeof(*ms)) + return -EBADMSG; + else if (ms->id_type != PKEY_ID_PKCS7) { + pr_err("Module is not signed with expected PKCS#7 message\n"); + return -ENOPKG; + } else if (ms->algo != 0 || + ms->hash != 0 || + ms->signer_len != 0 || + ms->key_id_len != 0 || + ms->__pad[0] != 0 || + ms->__pad[1] != 0 || + ms->__pad[2] != 0) { + pr_err("PKCS#7 signature info has unexpected non-zero params\n"); + return -EBADMSG; + } + + return 0; +} /* * Verify the signature on a module. @@ -49,6 +51,7 @@ int mod_verify_sig(const void *mod, unsigned long *_modlen) { struct module_signature ms; size_t modlen = *_modlen, sig_len; + int ret; pr_devel("==>%s(,%zu)\n", __func__, modlen); @@ -56,30 +59,15 @@ int mod_verify_sig(const void *mod, unsigned long *_modlen) return -EBADMSG; memcpy(&ms, mod + (modlen - sizeof(ms)), sizeof(ms)); - modlen -= sizeof(ms); + + ret = validate_module_sig(&ms, modlen); + if (ret) + return ret; sig_len = be32_to_cpu(ms.sig_len); - if (sig_len >= modlen) - return -EBADMSG; - modlen -= sig_len; + modlen -= sig_len + sizeof(ms); *_modlen = modlen; - if (ms.id_type != PKEY_ID_PKCS7) { - pr_err("Module is not signed with expected PKCS#7 message\n"); - return -ENOPKG; - } - - if (ms.algo != 0 || - ms.hash != 0 || - ms.signer_len != 0 || - ms.key_id_len != 0 || - ms.__pad[0] != 0 || - ms.__pad[1] != 0 || - ms.__pad[2] != 0) { - pr_err("PKCS#7 signature info has unexpected non-zero params\n"); - return -EBADMSG; - } - return verify_pkcs7_signature(mod, modlen, mod + modlen, sig_len, NULL, VERIFYING_MODULE_SIGNATURE, NULL, NULL);