Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

MODSIGN: Export module signature definitions

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 mod_check_sig() without having to depend on either
CONFIG_MODULE_SIG or CONFIG_MODULES.

s390 duplicated the definition of struct module_signature so now they can
use the new <linux/module_signature.h> header instead.

Signed-off-by: Thiago Jung Bauermann <bauerman@linux.ibm.com>
Acked-by: Jessica Yu <jeyu@kernel.org>
Reviewed-by: Philipp Rudo <prudo@linux.ibm.com>
Cc: Heiko Carstens <heiko.carstens@de.ibm.com>
Signed-off-by: Mimi Zohar <zohar@linux.ibm.com>

authored by

Thiago Jung Bauermann and committed by
Mimi Zohar
c8424e77 b36f281f

+108 -77
+1 -1
arch/s390/Kconfig
··· 538 538 539 539 config KEXEC_VERIFY_SIG 540 540 bool "Verify kernel signature during kexec_file_load() syscall" 541 - depends on KEXEC_FILE && SYSTEM_DATA_VERIFICATION 541 + depends on KEXEC_FILE && MODULE_SIG_FORMAT 542 542 help 543 543 This option makes kernel signature verification mandatory for 544 544 the kexec_file_load() syscall.
+1 -23
arch/s390/kernel/machine_kexec_file.c
··· 10 10 #include <linux/elf.h> 11 11 #include <linux/errno.h> 12 12 #include <linux/kexec.h> 13 - #include <linux/module.h> 13 + #include <linux/module_signature.h> 14 14 #include <linux/verification.h> 15 15 #include <asm/boot_data.h> 16 16 #include <asm/ipl.h> ··· 23 23 }; 24 24 25 25 #ifdef CONFIG_KEXEC_VERIFY_SIG 26 - /* 27 - * Module signature information block. 28 - * 29 - * The constituents of the signature section are, in order: 30 - * 31 - * - Signer's name 32 - * - Key identifier 33 - * - Signature data 34 - * - Information block 35 - */ 36 - struct module_signature { 37 - u8 algo; /* Public-key crypto algorithm [0] */ 38 - u8 hash; /* Digest algorithm [0] */ 39 - u8 id_type; /* Key identifier type [PKEY_ID_PKCS7] */ 40 - u8 signer_len; /* Length of signer's name [0] */ 41 - u8 key_id_len; /* Length of key identifier [0] */ 42 - u8 __pad[3]; 43 - __be32 sig_len; /* Length of signature data */ 44 - }; 45 - 46 - #define PKEY_ID_PKCS7 2 47 - 48 26 int s390_verify_sig(const char *kernel, unsigned long kernel_len) 49 27 { 50 28 const unsigned long marker_len = sizeof(MODULE_SIG_STRING) - 1;
-3
include/linux/module.h
··· 26 26 #include <linux/percpu.h> 27 27 #include <asm/module.h> 28 28 29 - /* In stripped ARM and x86-64 modules, ~ is surprisingly rare. */ 30 - #define MODULE_SIG_STRING "~Module signature appended~\n" 31 - 32 29 /* Not Yet Implemented */ 33 30 #define MODULE_SUPPORTED_DEVICE(name) 34 31
+44
include/linux/module_signature.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0+ */ 2 + /* 3 + * Module signature handling. 4 + * 5 + * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved. 6 + * Written by David Howells (dhowells@redhat.com) 7 + */ 8 + 9 + #ifndef _LINUX_MODULE_SIGNATURE_H 10 + #define _LINUX_MODULE_SIGNATURE_H 11 + 12 + /* In stripped ARM and x86-64 modules, ~ is surprisingly rare. */ 13 + #define MODULE_SIG_STRING "~Module signature appended~\n" 14 + 15 + enum pkey_id_type { 16 + PKEY_ID_PGP, /* OpenPGP generated key ID */ 17 + PKEY_ID_X509, /* X.509 arbitrary subjectKeyIdentifier */ 18 + PKEY_ID_PKCS7, /* Signature in PKCS#7 message */ 19 + }; 20 + 21 + /* 22 + * Module signature information block. 23 + * 24 + * The constituents of the signature section are, in order: 25 + * 26 + * - Signer's name 27 + * - Key identifier 28 + * - Signature data 29 + * - Information block 30 + */ 31 + struct module_signature { 32 + u8 algo; /* Public-key crypto algorithm [0] */ 33 + u8 hash; /* Digest algorithm [0] */ 34 + u8 id_type; /* Key identifier type [PKEY_ID_PKCS7] */ 35 + u8 signer_len; /* Length of signer's name [0] */ 36 + u8 key_id_len; /* Length of key identifier [0] */ 37 + u8 __pad[3]; 38 + __be32 sig_len; /* Length of signature data */ 39 + }; 40 + 41 + int mod_check_sig(const struct module_signature *ms, size_t file_len, 42 + const char *name); 43 + 44 + #endif /* _LINUX_MODULE_SIGNATURE_H */
+5 -1
init/Kconfig
··· 1930 1930 default 0 if BASE_FULL 1931 1931 default 1 if !BASE_FULL 1932 1932 1933 + config MODULE_SIG_FORMAT 1934 + def_bool n 1935 + select SYSTEM_DATA_VERIFICATION 1936 + 1933 1937 menuconfig MODULES 1934 1938 bool "Enable loadable module support" 1935 1939 option modules ··· 2011 2007 config MODULE_SIG 2012 2008 bool "Module signature verification" 2013 2009 depends on MODULES 2014 - select SYSTEM_DATA_VERIFICATION 2010 + select MODULE_SIG_FORMAT 2015 2011 help 2016 2012 Check modules for valid signatures upon load: the signature 2017 2013 is simply appended to the module. For more information see
+1
kernel/Makefile
··· 58 58 obj-$(CONFIG_UID16) += uid16.o 59 59 obj-$(CONFIG_MODULES) += module.o 60 60 obj-$(CONFIG_MODULE_SIG) += module_signing.o 61 + obj-$(CONFIG_MODULE_SIG_FORMAT) += module_signature.o 61 62 obj-$(CONFIG_KALLSYMS) += kallsyms.o 62 63 obj-$(CONFIG_BSD_PROCESS_ACCT) += acct.o 63 64 obj-$(CONFIG_CRASH_CORE) += crash_core.o
+1
kernel/module.c
··· 7 7 #include <linux/export.h> 8 8 #include <linux/extable.h> 9 9 #include <linux/moduleloader.h> 10 + #include <linux/module_signature.h> 10 11 #include <linux/trace_events.h> 11 12 #include <linux/init.h> 12 13 #include <linux/kallsyms.h>
+46
kernel/module_signature.c
··· 1 + // SPDX-License-Identifier: GPL-2.0+ 2 + /* 3 + * Module signature checker 4 + * 5 + * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved. 6 + * Written by David Howells (dhowells@redhat.com) 7 + */ 8 + 9 + #include <linux/errno.h> 10 + #include <linux/printk.h> 11 + #include <linux/module_signature.h> 12 + #include <asm/byteorder.h> 13 + 14 + /** 15 + * mod_check_sig - check that the given signature is sane 16 + * 17 + * @ms: Signature to check. 18 + * @file_len: Size of the file to which @ms is appended. 19 + * @name: What is being checked. Used for error messages. 20 + */ 21 + int mod_check_sig(const struct module_signature *ms, size_t file_len, 22 + const char *name) 23 + { 24 + if (be32_to_cpu(ms->sig_len) >= file_len - sizeof(*ms)) 25 + return -EBADMSG; 26 + 27 + if (ms->id_type != PKEY_ID_PKCS7) { 28 + pr_err("%s: Module is not signed with expected PKCS#7 message\n", 29 + name); 30 + return -ENOPKG; 31 + } 32 + 33 + if (ms->algo != 0 || 34 + ms->hash != 0 || 35 + ms->signer_len != 0 || 36 + ms->key_id_len != 0 || 37 + ms->__pad[0] != 0 || 38 + ms->__pad[1] != 0 || 39 + ms->__pad[2] != 0) { 40 + pr_err("%s: PKCS#7 signature info has unexpected non-zero params\n", 41 + name); 42 + return -EBADMSG; 43 + } 44 + 45 + return 0; 46 + }
+8 -48
kernel/module_signing.c
··· 7 7 8 8 #include <linux/kernel.h> 9 9 #include <linux/errno.h> 10 + #include <linux/module.h> 11 + #include <linux/module_signature.h> 10 12 #include <linux/string.h> 11 13 #include <linux/verification.h> 12 14 #include <crypto/public_key.h> 13 15 #include "module-internal.h" 14 - 15 - enum pkey_id_type { 16 - PKEY_ID_PGP, /* OpenPGP generated key ID */ 17 - PKEY_ID_X509, /* X.509 arbitrary subjectKeyIdentifier */ 18 - PKEY_ID_PKCS7, /* Signature in PKCS#7 message */ 19 - }; 20 - 21 - /* 22 - * Module signature information block. 23 - * 24 - * The constituents of the signature section are, in order: 25 - * 26 - * - Signer's name 27 - * - Key identifier 28 - * - Signature data 29 - * - Information block 30 - */ 31 - struct module_signature { 32 - u8 algo; /* Public-key crypto algorithm [0] */ 33 - u8 hash; /* Digest algorithm [0] */ 34 - u8 id_type; /* Key identifier type [PKEY_ID_PKCS7] */ 35 - u8 signer_len; /* Length of signer's name [0] */ 36 - u8 key_id_len; /* Length of key identifier [0] */ 37 - u8 __pad[3]; 38 - __be32 sig_len; /* Length of signature data */ 39 - }; 40 16 41 17 /* 42 18 * Verify the signature on a module. ··· 21 45 { 22 46 struct module_signature ms; 23 47 size_t sig_len, modlen = info->len; 48 + int ret; 24 49 25 50 pr_devel("==>%s(,%zu)\n", __func__, modlen); 26 51 ··· 29 52 return -EBADMSG; 30 53 31 54 memcpy(&ms, mod + (modlen - sizeof(ms)), sizeof(ms)); 32 - modlen -= sizeof(ms); 55 + 56 + ret = mod_check_sig(&ms, modlen, info->name); 57 + if (ret) 58 + return ret; 33 59 34 60 sig_len = be32_to_cpu(ms.sig_len); 35 - if (sig_len >= modlen) 36 - return -EBADMSG; 37 - modlen -= sig_len; 61 + modlen -= sig_len + sizeof(ms); 38 62 info->len = modlen; 39 - 40 - if (ms.id_type != PKEY_ID_PKCS7) { 41 - pr_err("%s: Module is not signed with expected PKCS#7 message\n", 42 - info->name); 43 - return -ENOPKG; 44 - } 45 - 46 - if (ms.algo != 0 || 47 - ms.hash != 0 || 48 - ms.signer_len != 0 || 49 - ms.key_id_len != 0 || 50 - ms.__pad[0] != 0 || 51 - ms.__pad[1] != 0 || 52 - ms.__pad[2] != 0) { 53 - pr_err("%s: PKCS#7 signature info has unexpected non-zero params\n", 54 - info->name); 55 - return -EBADMSG; 56 - } 57 63 58 64 return verify_pkcs7_signature(mod, modlen, mod + modlen, sig_len, 59 65 VERIFY_USE_SECONDARY_KEYRING,
+1 -1
scripts/Makefile
··· 17 17 hostprogs-$(BUILD_C_RECORDMCOUNT) += recordmcount 18 18 hostprogs-$(CONFIG_BUILDTIME_EXTABLE_SORT) += sortextable 19 19 hostprogs-$(CONFIG_ASN1) += asn1_compiler 20 - hostprogs-$(CONFIG_MODULE_SIG) += sign-file 20 + hostprogs-$(CONFIG_MODULE_SIG_FORMAT) += sign-file 21 21 hostprogs-$(CONFIG_SYSTEM_TRUSTED_KEYRING) += extract-cert 22 22 hostprogs-$(CONFIG_SYSTEM_EXTRA_CERTIFICATE) += insert-sys-cert 23 23