diff options
author | Linus Torvalds <torvalds@linux-foundation.org> | 2012-10-14 16:39:34 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2012-10-14 16:39:34 -0400 |
commit | d25282d1c9b9bc4cda7f9d3c0205108e99aa7a9d (patch) | |
tree | f414482d768b015a609924293b779b4ad0b8f764 /kernel | |
parent | b6eea87fc6850d3531a64a27d2323a4498cd4e43 (diff) | |
parent | dbadc17683e6c673a69b236c0f041b931cc55c42 (diff) |
Merge branch 'modules-next' of git://git.kernel.org/pub/scm/linux/kernel/git/rusty/linux
Pull module signing support from Rusty Russell:
"module signing is the highlight, but it's an all-over David Howells frenzy..."
Hmm "Magrathea: Glacier signing key". Somebody has been reading too much HHGTTG.
* 'modules-next' of git://git.kernel.org/pub/scm/linux/kernel/git/rusty/linux: (37 commits)
X.509: Fix indefinite length element skip error handling
X.509: Convert some printk calls to pr_devel
asymmetric keys: fix printk format warning
MODSIGN: Fix 32-bit overflow in X.509 certificate validity date checking
MODSIGN: Make mrproper should remove generated files.
MODSIGN: Use utf8 strings in signer's name in autogenerated X.509 certs
MODSIGN: Use the same digest for the autogen key sig as for the module sig
MODSIGN: Sign modules during the build process
MODSIGN: Provide a script for generating a key ID from an X.509 cert
MODSIGN: Implement module signature checking
MODSIGN: Provide module signing public keys to the kernel
MODSIGN: Automatically generate module signing keys if missing
MODSIGN: Provide Kconfig options
MODSIGN: Provide gitignore and make clean rules for extra files
MODSIGN: Add FIPS policy
module: signature checking hook
X.509: Add a crypto key parser for binary (DER) X.509 certificates
MPILIB: Provide a function to read raw data into an MPI
X.509: Add an ASN.1 decoder
X.509: Add simple ASN.1 grammar compiler
...
Diffstat (limited to 'kernel')
-rw-r--r-- | kernel/Makefile | 77 | ||||
-rw-r--r-- | kernel/modsign_pubkey.c | 113 | ||||
-rw-r--r-- | kernel/module-internal.h | 15 | ||||
-rw-r--r-- | kernel/module.c | 157 | ||||
-rw-r--r-- | kernel/module_signing.c | 243 |
5 files changed, 578 insertions, 27 deletions
diff --git a/kernel/Makefile b/kernel/Makefile index 5404911eaee9..0dfeca4324ee 100644 --- a/kernel/Makefile +++ b/kernel/Makefile | |||
@@ -54,6 +54,7 @@ obj-$(CONFIG_DEBUG_SPINLOCK) += spinlock.o | |||
54 | obj-$(CONFIG_PROVE_LOCKING) += spinlock.o | 54 | obj-$(CONFIG_PROVE_LOCKING) += spinlock.o |
55 | obj-$(CONFIG_UID16) += uid16.o | 55 | obj-$(CONFIG_UID16) += uid16.o |
56 | obj-$(CONFIG_MODULES) += module.o | 56 | obj-$(CONFIG_MODULES) += module.o |
57 | obj-$(CONFIG_MODULE_SIG) += module_signing.o modsign_pubkey.o | ||
57 | obj-$(CONFIG_KALLSYMS) += kallsyms.o | 58 | obj-$(CONFIG_KALLSYMS) += kallsyms.o |
58 | obj-$(CONFIG_BSD_PROCESS_ACCT) += acct.o | 59 | obj-$(CONFIG_BSD_PROCESS_ACCT) += acct.o |
59 | obj-$(CONFIG_KEXEC) += kexec.o | 60 | obj-$(CONFIG_KEXEC) += kexec.o |
@@ -130,3 +131,79 @@ quiet_cmd_timeconst = TIMEC $@ | |||
130 | targets += timeconst.h | 131 | targets += timeconst.h |
131 | $(obj)/timeconst.h: $(src)/timeconst.pl FORCE | 132 | $(obj)/timeconst.h: $(src)/timeconst.pl FORCE |
132 | $(call if_changed,timeconst) | 133 | $(call if_changed,timeconst) |
134 | |||
135 | ifeq ($(CONFIG_MODULE_SIG),y) | ||
136 | # | ||
137 | # Pull the signing certificate and any extra certificates into the kernel | ||
138 | # | ||
139 | extra_certificates: | ||
140 | touch $@ | ||
141 | |||
142 | kernel/modsign_pubkey.o: signing_key.x509 extra_certificates | ||
143 | |||
144 | ############################################################################### | ||
145 | # | ||
146 | # If module signing is requested, say by allyesconfig, but a key has not been | ||
147 | # supplied, then one will need to be generated to make sure the build does not | ||
148 | # fail and that the kernel may be used afterwards. | ||
149 | # | ||
150 | ############################################################################### | ||
151 | sign_key_with_hash := | ||
152 | ifeq ($(CONFIG_MODULE_SIG_SHA1),y) | ||
153 | sign_key_with_hash := -sha1 | ||
154 | endif | ||
155 | ifeq ($(CONFIG_MODULE_SIG_SHA224),y) | ||
156 | sign_key_with_hash := -sha224 | ||
157 | endif | ||
158 | ifeq ($(CONFIG_MODULE_SIG_SHA256),y) | ||
159 | sign_key_with_hash := -sha256 | ||
160 | endif | ||
161 | ifeq ($(CONFIG_MODULE_SIG_SHA384),y) | ||
162 | sign_key_with_hash := -sha384 | ||
163 | endif | ||
164 | ifeq ($(CONFIG_MODULE_SIG_SHA512),y) | ||
165 | sign_key_with_hash := -sha512 | ||
166 | endif | ||
167 | ifeq ($(sign_key_with_hash),) | ||
168 | $(error Could not determine digest type to use from kernel config) | ||
169 | endif | ||
170 | |||
171 | signing_key.priv signing_key.x509: x509.genkey | ||
172 | @echo "###" | ||
173 | @echo "### Now generating an X.509 key pair to be used for signing modules." | ||
174 | @echo "###" | ||
175 | @echo "### If this takes a long time, you might wish to run rngd in the" | ||
176 | @echo "### background to keep the supply of entropy topped up. It" | ||
177 | @echo "### needs to be run as root, and should use a hardware random" | ||
178 | @echo "### number generator if one is available, eg:" | ||
179 | @echo "###" | ||
180 | @echo "### rngd -r /dev/hwrandom" | ||
181 | @echo "###" | ||
182 | openssl req -new -nodes -utf8 $(sign_key_with_hash) -days 36500 -batch \ | ||
183 | -x509 -config x509.genkey \ | ||
184 | -outform DER -out signing_key.x509 \ | ||
185 | -keyout signing_key.priv | ||
186 | @echo "###" | ||
187 | @echo "### Key pair generated." | ||
188 | @echo "###" | ||
189 | |||
190 | x509.genkey: | ||
191 | @echo Generating X.509 key generation config | ||
192 | @echo >x509.genkey "[ req ]" | ||
193 | @echo >>x509.genkey "default_bits = 4096" | ||
194 | @echo >>x509.genkey "distinguished_name = req_distinguished_name" | ||
195 | @echo >>x509.genkey "prompt = no" | ||
196 | @echo >>x509.genkey "string_mask = utf8only" | ||
197 | @echo >>x509.genkey "x509_extensions = myexts" | ||
198 | @echo >>x509.genkey | ||
199 | @echo >>x509.genkey "[ req_distinguished_name ]" | ||
200 | @echo >>x509.genkey "O = Magrathea" | ||
201 | @echo >>x509.genkey "CN = Glacier signing key" | ||
202 | @echo >>x509.genkey "emailAddress = slartibartfast@magrathea.h2g2" | ||
203 | @echo >>x509.genkey | ||
204 | @echo >>x509.genkey "[ myexts ]" | ||
205 | @echo >>x509.genkey "basicConstraints=critical,CA:FALSE" | ||
206 | @echo >>x509.genkey "keyUsage=digitalSignature" | ||
207 | @echo >>x509.genkey "subjectKeyIdentifier=hash" | ||
208 | @echo >>x509.genkey "authorityKeyIdentifier=keyid" | ||
209 | endif | ||
diff --git a/kernel/modsign_pubkey.c b/kernel/modsign_pubkey.c new file mode 100644 index 000000000000..4646eb2c3820 --- /dev/null +++ b/kernel/modsign_pubkey.c | |||
@@ -0,0 +1,113 @@ | |||
1 | /* Public keys for module signature verification | ||
2 | * | ||
3 | * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved. | ||
4 | * Written by David Howells (dhowells@redhat.com) | ||
5 | * | ||
6 | * This program is free software; you can redistribute it and/or | ||
7 | * modify it under the terms of the GNU General Public Licence | ||
8 | * as published by the Free Software Foundation; either version | ||
9 | * 2 of the Licence, or (at your option) any later version. | ||
10 | */ | ||
11 | |||
12 | #include <linux/kernel.h> | ||
13 | #include <linux/sched.h> | ||
14 | #include <linux/cred.h> | ||
15 | #include <linux/err.h> | ||
16 | #include <keys/asymmetric-type.h> | ||
17 | #include "module-internal.h" | ||
18 | |||
19 | struct key *modsign_keyring; | ||
20 | |||
21 | extern __initdata const u8 modsign_certificate_list[]; | ||
22 | extern __initdata const u8 modsign_certificate_list_end[]; | ||
23 | asm(".section .init.data,\"aw\"\n" | ||
24 | "modsign_certificate_list:\n" | ||
25 | ".incbin \"signing_key.x509\"\n" | ||
26 | ".incbin \"extra_certificates\"\n" | ||
27 | "modsign_certificate_list_end:" | ||
28 | ); | ||
29 | |||
30 | /* | ||
31 | * We need to make sure ccache doesn't cache the .o file as it doesn't notice | ||
32 | * if modsign.pub changes. | ||
33 | */ | ||
34 | static __initdata const char annoy_ccache[] = __TIME__ "foo"; | ||
35 | |||
36 | /* | ||
37 | * Load the compiled-in keys | ||
38 | */ | ||
39 | static __init int module_verify_init(void) | ||
40 | { | ||
41 | pr_notice("Initialise module verification\n"); | ||
42 | |||
43 | modsign_keyring = key_alloc(&key_type_keyring, ".module_sign", | ||
44 | KUIDT_INIT(0), KGIDT_INIT(0), | ||
45 | current_cred(), | ||
46 | (KEY_POS_ALL & ~KEY_POS_SETATTR) | | ||
47 | KEY_USR_VIEW | KEY_USR_READ, | ||
48 | KEY_ALLOC_NOT_IN_QUOTA); | ||
49 | if (IS_ERR(modsign_keyring)) | ||
50 | panic("Can't allocate module signing keyring\n"); | ||
51 | |||
52 | if (key_instantiate_and_link(modsign_keyring, NULL, 0, NULL, NULL) < 0) | ||
53 | panic("Can't instantiate module signing keyring\n"); | ||
54 | |||
55 | return 0; | ||
56 | } | ||
57 | |||
58 | /* | ||
59 | * Must be initialised before we try and load the keys into the keyring. | ||
60 | */ | ||
61 | device_initcall(module_verify_init); | ||
62 | |||
63 | /* | ||
64 | * Load the compiled-in keys | ||
65 | */ | ||
66 | static __init int load_module_signing_keys(void) | ||
67 | { | ||
68 | key_ref_t key; | ||
69 | const u8 *p, *end; | ||
70 | size_t plen; | ||
71 | |||
72 | pr_notice("Loading module verification certificates\n"); | ||
73 | |||
74 | end = modsign_certificate_list_end; | ||
75 | p = modsign_certificate_list; | ||
76 | while (p < end) { | ||
77 | /* Each cert begins with an ASN.1 SEQUENCE tag and must be more | ||
78 | * than 256 bytes in size. | ||
79 | */ | ||
80 | if (end - p < 4) | ||
81 | goto dodgy_cert; | ||
82 | if (p[0] != 0x30 && | ||
83 | p[1] != 0x82) | ||
84 | goto dodgy_cert; | ||
85 | plen = (p[2] << 8) | p[3]; | ||
86 | plen += 4; | ||
87 | if (plen > end - p) | ||
88 | goto dodgy_cert; | ||
89 | |||
90 | key = key_create_or_update(make_key_ref(modsign_keyring, 1), | ||
91 | "asymmetric", | ||
92 | NULL, | ||
93 | p, | ||
94 | plen, | ||
95 | (KEY_POS_ALL & ~KEY_POS_SETATTR) | | ||
96 | KEY_USR_VIEW, | ||
97 | KEY_ALLOC_NOT_IN_QUOTA); | ||
98 | if (IS_ERR(key)) | ||
99 | pr_err("MODSIGN: Problem loading in-kernel X.509 certificate (%ld)\n", | ||
100 | PTR_ERR(key)); | ||
101 | else | ||
102 | pr_notice("MODSIGN: Loaded cert '%s'\n", | ||
103 | key_ref_to_ptr(key)->description); | ||
104 | p += plen; | ||
105 | } | ||
106 | |||
107 | return 0; | ||
108 | |||
109 | dodgy_cert: | ||
110 | pr_err("MODSIGN: Problem parsing in-kernel X.509 certificate list\n"); | ||
111 | return 0; | ||
112 | } | ||
113 | late_initcall(load_module_signing_keys); | ||
diff --git a/kernel/module-internal.h b/kernel/module-internal.h new file mode 100644 index 000000000000..6114a13419bd --- /dev/null +++ b/kernel/module-internal.h | |||
@@ -0,0 +1,15 @@ | |||
1 | /* Module internals | ||
2 | * | ||
3 | * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved. | ||
4 | * Written by David Howells (dhowells@redhat.com) | ||
5 | * | ||
6 | * This program is free software; you can redistribute it and/or | ||
7 | * modify it under the terms of the GNU General Public Licence | ||
8 | * as published by the Free Software Foundation; either version | ||
9 | * 2 of the Licence, or (at your option) any later version. | ||
10 | */ | ||
11 | |||
12 | extern struct key *modsign_keyring; | ||
13 | |||
14 | extern int mod_verify_sig(const void *mod, unsigned long modlen, | ||
15 | const void *sig, unsigned long siglen); | ||
diff --git a/kernel/module.c b/kernel/module.c index 4edbd9c11aca..0e2da8695f8e 100644 --- a/kernel/module.c +++ b/kernel/module.c | |||
@@ -58,6 +58,8 @@ | |||
58 | #include <linux/jump_label.h> | 58 | #include <linux/jump_label.h> |
59 | #include <linux/pfn.h> | 59 | #include <linux/pfn.h> |
60 | #include <linux/bsearch.h> | 60 | #include <linux/bsearch.h> |
61 | #include <linux/fips.h> | ||
62 | #include "module-internal.h" | ||
61 | 63 | ||
62 | #define CREATE_TRACE_POINTS | 64 | #define CREATE_TRACE_POINTS |
63 | #include <trace/events/module.h> | 65 | #include <trace/events/module.h> |
@@ -102,6 +104,43 @@ static LIST_HEAD(modules); | |||
102 | struct list_head *kdb_modules = &modules; /* kdb needs the list of modules */ | 104 | struct list_head *kdb_modules = &modules; /* kdb needs the list of modules */ |
103 | #endif /* CONFIG_KGDB_KDB */ | 105 | #endif /* CONFIG_KGDB_KDB */ |
104 | 106 | ||
107 | #ifdef CONFIG_MODULE_SIG | ||
108 | #ifdef CONFIG_MODULE_SIG_FORCE | ||
109 | static bool sig_enforce = true; | ||
110 | #else | ||
111 | static bool sig_enforce = false; | ||
112 | |||
113 | static int param_set_bool_enable_only(const char *val, | ||
114 | const struct kernel_param *kp) | ||
115 | { | ||
116 | int err; | ||
117 | bool test; | ||
118 | struct kernel_param dummy_kp = *kp; | ||
119 | |||
120 | dummy_kp.arg = &test; | ||
121 | |||
122 | err = param_set_bool(val, &dummy_kp); | ||
123 | if (err) | ||
124 | return err; | ||
125 | |||
126 | /* Don't let them unset it once it's set! */ | ||
127 | if (!test && sig_enforce) | ||
128 | return -EROFS; | ||
129 | |||
130 | if (test) | ||
131 | sig_enforce = true; | ||
132 | return 0; | ||
133 | } | ||
134 | |||
135 | static const struct kernel_param_ops param_ops_bool_enable_only = { | ||
136 | .set = param_set_bool_enable_only, | ||
137 | .get = param_get_bool, | ||
138 | }; | ||
139 | #define param_check_bool_enable_only param_check_bool | ||
140 | |||
141 | module_param(sig_enforce, bool_enable_only, 0644); | ||
142 | #endif /* !CONFIG_MODULE_SIG_FORCE */ | ||
143 | #endif /* CONFIG_MODULE_SIG */ | ||
105 | 144 | ||
106 | /* Block module loading/unloading? */ | 145 | /* Block module loading/unloading? */ |
107 | int modules_disabled = 0; | 146 | int modules_disabled = 0; |
@@ -136,6 +175,7 @@ struct load_info { | |||
136 | unsigned long symoffs, stroffs; | 175 | unsigned long symoffs, stroffs; |
137 | struct _ddebug *debug; | 176 | struct _ddebug *debug; |
138 | unsigned int num_debug; | 177 | unsigned int num_debug; |
178 | bool sig_ok; | ||
139 | struct { | 179 | struct { |
140 | unsigned int sym, str, mod, vers, info, pcpu; | 180 | unsigned int sym, str, mod, vers, info, pcpu; |
141 | } index; | 181 | } index; |
@@ -1949,26 +1989,6 @@ static int simplify_symbols(struct module *mod, const struct load_info *info) | |||
1949 | return ret; | 1989 | return ret; |
1950 | } | 1990 | } |
1951 | 1991 | ||
1952 | int __weak apply_relocate(Elf_Shdr *sechdrs, | ||
1953 | const char *strtab, | ||
1954 | unsigned int symindex, | ||
1955 | unsigned int relsec, | ||
1956 | struct module *me) | ||
1957 | { | ||
1958 | pr_err("module %s: REL relocation unsupported\n", me->name); | ||
1959 | return -ENOEXEC; | ||
1960 | } | ||
1961 | |||
1962 | int __weak apply_relocate_add(Elf_Shdr *sechdrs, | ||
1963 | const char *strtab, | ||
1964 | unsigned int symindex, | ||
1965 | unsigned int relsec, | ||
1966 | struct module *me) | ||
1967 | { | ||
1968 | pr_err("module %s: RELA relocation unsupported\n", me->name); | ||
1969 | return -ENOEXEC; | ||
1970 | } | ||
1971 | |||
1972 | static int apply_relocations(struct module *mod, const struct load_info *info) | 1992 | static int apply_relocations(struct module *mod, const struct load_info *info) |
1973 | { | 1993 | { |
1974 | unsigned int i; | 1994 | unsigned int i; |
@@ -2399,7 +2419,52 @@ static inline void kmemleak_load_module(const struct module *mod, | |||
2399 | } | 2419 | } |
2400 | #endif | 2420 | #endif |
2401 | 2421 | ||
2402 | /* Sets info->hdr and info->len. */ | 2422 | #ifdef CONFIG_MODULE_SIG |
2423 | static int module_sig_check(struct load_info *info, | ||
2424 | const void *mod, unsigned long *len) | ||
2425 | { | ||
2426 | int err = -ENOKEY; | ||
2427 | const unsigned long markerlen = sizeof(MODULE_SIG_STRING) - 1; | ||
2428 | const void *p = mod, *end = mod + *len; | ||
2429 | |||
2430 | /* Poor man's memmem. */ | ||
2431 | while ((p = memchr(p, MODULE_SIG_STRING[0], end - p))) { | ||
2432 | if (p + markerlen > end) | ||
2433 | break; | ||
2434 | |||
2435 | if (memcmp(p, MODULE_SIG_STRING, markerlen) == 0) { | ||
2436 | const void *sig = p + markerlen; | ||
2437 | /* Truncate module up to signature. */ | ||
2438 | *len = p - mod; | ||
2439 | err = mod_verify_sig(mod, *len, sig, end - sig); | ||
2440 | break; | ||
2441 | } | ||
2442 | p++; | ||
2443 | } | ||
2444 | |||
2445 | if (!err) { | ||
2446 | info->sig_ok = true; | ||
2447 | return 0; | ||
2448 | } | ||
2449 | |||
2450 | /* Not having a signature is only an error if we're strict. */ | ||
2451 | if (err < 0 && fips_enabled) | ||
2452 | panic("Module verification failed with error %d in FIPS mode\n", | ||
2453 | err); | ||
2454 | if (err == -ENOKEY && !sig_enforce) | ||
2455 | err = 0; | ||
2456 | |||
2457 | return err; | ||
2458 | } | ||
2459 | #else /* !CONFIG_MODULE_SIG */ | ||
2460 | static int module_sig_check(struct load_info *info, | ||
2461 | void *mod, unsigned long *len) | ||
2462 | { | ||
2463 | return 0; | ||
2464 | } | ||
2465 | #endif /* !CONFIG_MODULE_SIG */ | ||
2466 | |||
2467 | /* Sets info->hdr, info->len and info->sig_ok. */ | ||
2403 | static int copy_and_check(struct load_info *info, | 2468 | static int copy_and_check(struct load_info *info, |
2404 | const void __user *umod, unsigned long len, | 2469 | const void __user *umod, unsigned long len, |
2405 | const char __user *uargs) | 2470 | const char __user *uargs) |
@@ -2419,6 +2484,10 @@ static int copy_and_check(struct load_info *info, | |||
2419 | goto free_hdr; | 2484 | goto free_hdr; |
2420 | } | 2485 | } |
2421 | 2486 | ||
2487 | err = module_sig_check(info, hdr, &len); | ||
2488 | if (err) | ||
2489 | goto free_hdr; | ||
2490 | |||
2422 | /* Sanity checks against insmoding binaries or wrong arch, | 2491 | /* Sanity checks against insmoding binaries or wrong arch, |
2423 | weird elf version */ | 2492 | weird elf version */ |
2424 | if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0 | 2493 | if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0 |
@@ -2730,6 +2799,10 @@ static int check_module_license_and_versions(struct module *mod) | |||
2730 | if (strcmp(mod->name, "driverloader") == 0) | 2799 | if (strcmp(mod->name, "driverloader") == 0) |
2731 | add_taint_module(mod, TAINT_PROPRIETARY_MODULE); | 2800 | add_taint_module(mod, TAINT_PROPRIETARY_MODULE); |
2732 | 2801 | ||
2802 | /* lve claims to be GPL but upstream won't provide source */ | ||
2803 | if (strcmp(mod->name, "lve") == 0) | ||
2804 | add_taint_module(mod, TAINT_PROPRIETARY_MODULE); | ||
2805 | |||
2733 | #ifdef CONFIG_MODVERSIONS | 2806 | #ifdef CONFIG_MODVERSIONS |
2734 | if ((mod->num_syms && !mod->crcs) | 2807 | if ((mod->num_syms && !mod->crcs) |
2735 | || (mod->num_gpl_syms && !mod->gpl_crcs) | 2808 | || (mod->num_gpl_syms && !mod->gpl_crcs) |
@@ -2861,6 +2934,20 @@ static int post_relocation(struct module *mod, const struct load_info *info) | |||
2861 | return module_finalize(info->hdr, info->sechdrs, mod); | 2934 | return module_finalize(info->hdr, info->sechdrs, mod); |
2862 | } | 2935 | } |
2863 | 2936 | ||
2937 | /* Is this module of this name done loading? No locks held. */ | ||
2938 | static bool finished_loading(const char *name) | ||
2939 | { | ||
2940 | struct module *mod; | ||
2941 | bool ret; | ||
2942 | |||
2943 | mutex_lock(&module_mutex); | ||
2944 | mod = find_module(name); | ||
2945 | ret = !mod || mod->state != MODULE_STATE_COMING; | ||
2946 | mutex_unlock(&module_mutex); | ||
2947 | |||
2948 | return ret; | ||
2949 | } | ||
2950 | |||
2864 | /* Allocate and load the module: note that size of section 0 is always | 2951 | /* Allocate and load the module: note that size of section 0 is always |
2865 | zero, and we rely on this for optional sections. */ | 2952 | zero, and we rely on this for optional sections. */ |
2866 | static struct module *load_module(void __user *umod, | 2953 | static struct module *load_module(void __user *umod, |
@@ -2868,7 +2955,7 @@ static struct module *load_module(void __user *umod, | |||
2868 | const char __user *uargs) | 2955 | const char __user *uargs) |
2869 | { | 2956 | { |
2870 | struct load_info info = { NULL, }; | 2957 | struct load_info info = { NULL, }; |
2871 | struct module *mod; | 2958 | struct module *mod, *old; |
2872 | long err; | 2959 | long err; |
2873 | 2960 | ||
2874 | pr_debug("load_module: umod=%p, len=%lu, uargs=%p\n", | 2961 | pr_debug("load_module: umod=%p, len=%lu, uargs=%p\n", |
@@ -2886,6 +2973,12 @@ static struct module *load_module(void __user *umod, | |||
2886 | goto free_copy; | 2973 | goto free_copy; |
2887 | } | 2974 | } |
2888 | 2975 | ||
2976 | #ifdef CONFIG_MODULE_SIG | ||
2977 | mod->sig_ok = info.sig_ok; | ||
2978 | if (!mod->sig_ok) | ||
2979 | add_taint_module(mod, TAINT_FORCED_MODULE); | ||
2980 | #endif | ||
2981 | |||
2889 | /* Now module is in final location, initialize linked lists, etc. */ | 2982 | /* Now module is in final location, initialize linked lists, etc. */ |
2890 | err = module_unload_init(mod); | 2983 | err = module_unload_init(mod); |
2891 | if (err) | 2984 | if (err) |
@@ -2934,8 +3027,18 @@ static struct module *load_module(void __user *umod, | |||
2934 | * function to insert in a way safe to concurrent readers. | 3027 | * function to insert in a way safe to concurrent readers. |
2935 | * The mutex protects against concurrent writers. | 3028 | * The mutex protects against concurrent writers. |
2936 | */ | 3029 | */ |
3030 | again: | ||
2937 | mutex_lock(&module_mutex); | 3031 | mutex_lock(&module_mutex); |
2938 | if (find_module(mod->name)) { | 3032 | if ((old = find_module(mod->name)) != NULL) { |
3033 | if (old->state == MODULE_STATE_COMING) { | ||
3034 | /* Wait in case it fails to load. */ | ||
3035 | mutex_unlock(&module_mutex); | ||
3036 | err = wait_event_interruptible(module_wq, | ||
3037 | finished_loading(mod->name)); | ||
3038 | if (err) | ||
3039 | goto free_arch_cleanup; | ||
3040 | goto again; | ||
3041 | } | ||
2939 | err = -EEXIST; | 3042 | err = -EEXIST; |
2940 | goto unlock; | 3043 | goto unlock; |
2941 | } | 3044 | } |
@@ -2975,7 +3078,7 @@ static struct module *load_module(void __user *umod, | |||
2975 | /* Unlink carefully: kallsyms could be walking list. */ | 3078 | /* Unlink carefully: kallsyms could be walking list. */ |
2976 | list_del_rcu(&mod->list); | 3079 | list_del_rcu(&mod->list); |
2977 | module_bug_cleanup(mod); | 3080 | module_bug_cleanup(mod); |
2978 | 3081 | wake_up_all(&module_wq); | |
2979 | ddebug: | 3082 | ddebug: |
2980 | dynamic_debug_remove(info.debug); | 3083 | dynamic_debug_remove(info.debug); |
2981 | unlock: | 3084 | unlock: |
@@ -3050,7 +3153,7 @@ SYSCALL_DEFINE3(init_module, void __user *, umod, | |||
3050 | blocking_notifier_call_chain(&module_notify_list, | 3153 | blocking_notifier_call_chain(&module_notify_list, |
3051 | MODULE_STATE_GOING, mod); | 3154 | MODULE_STATE_GOING, mod); |
3052 | free_module(mod); | 3155 | free_module(mod); |
3053 | wake_up(&module_wq); | 3156 | wake_up_all(&module_wq); |
3054 | return ret; | 3157 | return ret; |
3055 | } | 3158 | } |
3056 | if (ret > 0) { | 3159 | if (ret > 0) { |
@@ -3062,9 +3165,8 @@ SYSCALL_DEFINE3(init_module, void __user *, umod, | |||
3062 | dump_stack(); | 3165 | dump_stack(); |
3063 | } | 3166 | } |
3064 | 3167 | ||
3065 | /* Now it's a first class citizen! Wake up anyone waiting for it. */ | 3168 | /* Now it's a first class citizen! */ |
3066 | mod->state = MODULE_STATE_LIVE; | 3169 | mod->state = MODULE_STATE_LIVE; |
3067 | wake_up(&module_wq); | ||
3068 | blocking_notifier_call_chain(&module_notify_list, | 3170 | blocking_notifier_call_chain(&module_notify_list, |
3069 | MODULE_STATE_LIVE, mod); | 3171 | MODULE_STATE_LIVE, mod); |
3070 | 3172 | ||
@@ -3087,6 +3189,7 @@ SYSCALL_DEFINE3(init_module, void __user *, umod, | |||
3087 | mod->init_ro_size = 0; | 3189 | mod->init_ro_size = 0; |
3088 | mod->init_text_size = 0; | 3190 | mod->init_text_size = 0; |
3089 | mutex_unlock(&module_mutex); | 3191 | mutex_unlock(&module_mutex); |
3192 | wake_up_all(&module_wq); | ||
3090 | 3193 | ||
3091 | return 0; | 3194 | return 0; |
3092 | } | 3195 | } |
diff --git a/kernel/module_signing.c b/kernel/module_signing.c new file mode 100644 index 000000000000..6b09f6983ac0 --- /dev/null +++ b/kernel/module_signing.c | |||
@@ -0,0 +1,243 @@ | |||
1 | /* Module signature checker | ||
2 | * | ||
3 | * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved. | ||
4 | * Written by David Howells (dhowells@redhat.com) | ||
5 | * | ||
6 | * This program is free software; you can redistribute it and/or | ||
7 | * modify it under the terms of the GNU General Public Licence | ||
8 | * as published by the Free Software Foundation; either version | ||
9 | * 2 of the Licence, or (at your option) any later version. | ||
10 | */ | ||
11 | |||
12 | #include <linux/kernel.h> | ||
13 | #include <linux/err.h> | ||
14 | #include <crypto/public_key.h> | ||
15 | #include <crypto/hash.h> | ||
16 | #include <keys/asymmetric-type.h> | ||
17 | #include "module-internal.h" | ||
18 | |||
19 | /* | ||
20 | * Module signature information block. | ||
21 | * | ||
22 | * The constituents of the signature section are, in order: | ||
23 | * | ||
24 | * - Signer's name | ||
25 | * - Key identifier | ||
26 | * - Signature data | ||
27 | * - Information block | ||
28 | */ | ||
29 | struct module_signature { | ||
30 | enum pkey_algo algo : 8; /* Public-key crypto algorithm */ | ||
31 | enum pkey_hash_algo hash : 8; /* Digest algorithm */ | ||
32 | enum pkey_id_type id_type : 8; /* Key identifier type */ | ||
33 | u8 signer_len; /* Length of signer's name */ | ||
34 | u8 key_id_len; /* Length of key identifier */ | ||
35 | u8 __pad[3]; | ||
36 | __be32 sig_len; /* Length of signature data */ | ||
37 | }; | ||
38 | |||
39 | /* | ||
40 | * Digest the module contents. | ||
41 | */ | ||
42 | static struct public_key_signature *mod_make_digest(enum pkey_hash_algo hash, | ||
43 | const void *mod, | ||
44 | unsigned long modlen) | ||
45 | { | ||
46 | struct public_key_signature *pks; | ||
47 | struct crypto_shash *tfm; | ||
48 | struct shash_desc *desc; | ||
49 | size_t digest_size, desc_size; | ||
50 | int ret; | ||
51 | |||
52 | pr_devel("==>%s()\n", __func__); | ||
53 | |||
54 | /* Allocate the hashing algorithm we're going to need and find out how | ||
55 | * big the hash operational data will be. | ||
56 | */ | ||
57 | tfm = crypto_alloc_shash(pkey_hash_algo[hash], 0, 0); | ||
58 | if (IS_ERR(tfm)) | ||
59 | return (PTR_ERR(tfm) == -ENOENT) ? ERR_PTR(-ENOPKG) : ERR_CAST(tfm); | ||
60 | |||
61 | desc_size = crypto_shash_descsize(tfm) + sizeof(*desc); | ||
62 | digest_size = crypto_shash_digestsize(tfm); | ||
63 | |||
64 | /* We allocate the hash operational data storage on the end of our | ||
65 | * context data and the digest output buffer on the end of that. | ||
66 | */ | ||
67 | ret = -ENOMEM; | ||
68 | pks = kzalloc(digest_size + sizeof(*pks) + desc_size, GFP_KERNEL); | ||
69 | if (!pks) | ||
70 | goto error_no_pks; | ||
71 | |||
72 | pks->pkey_hash_algo = hash; | ||
73 | pks->digest = (u8 *)pks + sizeof(*pks) + desc_size; | ||
74 | pks->digest_size = digest_size; | ||
75 | |||
76 | desc = (void *)pks + sizeof(*pks); | ||
77 | desc->tfm = tfm; | ||
78 | desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP; | ||
79 | |||
80 | ret = crypto_shash_init(desc); | ||
81 | if (ret < 0) | ||
82 | goto error; | ||
83 | |||
84 | ret = crypto_shash_finup(desc, mod, modlen, pks->digest); | ||
85 | if (ret < 0) | ||
86 | goto error; | ||
87 | |||
88 | crypto_free_shash(tfm); | ||
89 | pr_devel("<==%s() = ok\n", __func__); | ||
90 | return pks; | ||
91 | |||
92 | error: | ||
93 | kfree(pks); | ||
94 | error_no_pks: | ||
95 | crypto_free_shash(tfm); | ||
96 | pr_devel("<==%s() = %d\n", __func__, ret); | ||
97 | return ERR_PTR(ret); | ||
98 | } | ||
99 | |||
100 | /* | ||
101 | * Extract an MPI array from the signature data. This represents the actual | ||
102 | * signature. Each raw MPI is prefaced by a BE 2-byte value indicating the | ||
103 | * size of the MPI in bytes. | ||
104 | * | ||
105 | * RSA signatures only have one MPI, so currently we only read one. | ||
106 | */ | ||
107 | static int mod_extract_mpi_array(struct public_key_signature *pks, | ||
108 | const void *data, size_t len) | ||
109 | { | ||
110 | size_t nbytes; | ||
111 | MPI mpi; | ||
112 | |||
113 | if (len < 3) | ||
114 | return -EBADMSG; | ||
115 | nbytes = ((const u8 *)data)[0] << 8 | ((const u8 *)data)[1]; | ||
116 | data += 2; | ||
117 | len -= 2; | ||
118 | if (len != nbytes) | ||
119 | return -EBADMSG; | ||
120 | |||
121 | mpi = mpi_read_raw_data(data, nbytes); | ||
122 | if (!mpi) | ||
123 | return -ENOMEM; | ||
124 | pks->mpi[0] = mpi; | ||
125 | pks->nr_mpi = 1; | ||
126 | return 0; | ||
127 | } | ||
128 | |||
129 | /* | ||
130 | * Request an asymmetric key. | ||
131 | */ | ||
132 | static struct key *request_asymmetric_key(const char *signer, size_t signer_len, | ||
133 | const u8 *key_id, size_t key_id_len) | ||
134 | { | ||
135 | key_ref_t key; | ||
136 | size_t i; | ||
137 | char *id, *q; | ||
138 | |||
139 | pr_devel("==>%s(,%zu,,%zu)\n", __func__, signer_len, key_id_len); | ||
140 | |||
141 | /* Construct an identifier. */ | ||
142 | id = kmalloc(signer_len + 2 + key_id_len * 2 + 1, GFP_KERNEL); | ||
143 | if (!id) | ||
144 | return ERR_PTR(-ENOKEY); | ||
145 | |||
146 | memcpy(id, signer, signer_len); | ||
147 | |||
148 | q = id + signer_len; | ||
149 | *q++ = ':'; | ||
150 | *q++ = ' '; | ||
151 | for (i = 0; i < key_id_len; i++) { | ||
152 | *q++ = hex_asc[*key_id >> 4]; | ||
153 | *q++ = hex_asc[*key_id++ & 0x0f]; | ||
154 | } | ||
155 | |||
156 | *q = 0; | ||
157 | |||
158 | pr_debug("Look up: \"%s\"\n", id); | ||
159 | |||
160 | key = keyring_search(make_key_ref(modsign_keyring, 1), | ||
161 | &key_type_asymmetric, id); | ||
162 | if (IS_ERR(key)) | ||
163 | pr_warn("Request for unknown module key '%s' err %ld\n", | ||
164 | id, PTR_ERR(key)); | ||
165 | kfree(id); | ||
166 | |||
167 | if (IS_ERR(key)) { | ||
168 | switch (PTR_ERR(key)) { | ||
169 | /* Hide some search errors */ | ||
170 | case -EACCES: | ||
171 | case -ENOTDIR: | ||
172 | case -EAGAIN: | ||
173 | return ERR_PTR(-ENOKEY); | ||
174 | default: | ||
175 | return ERR_CAST(key); | ||
176 | } | ||
177 | } | ||
178 | |||
179 | pr_devel("<==%s() = 0 [%x]\n", __func__, key_serial(key_ref_to_ptr(key))); | ||
180 | return key_ref_to_ptr(key); | ||
181 | } | ||
182 | |||
183 | /* | ||
184 | * Verify the signature on a module. | ||
185 | */ | ||
186 | int mod_verify_sig(const void *mod, unsigned long modlen, | ||
187 | const void *sig, unsigned long siglen) | ||
188 | { | ||
189 | struct public_key_signature *pks; | ||
190 | struct module_signature ms; | ||
191 | struct key *key; | ||
192 | size_t sig_len; | ||
193 | int ret; | ||
194 | |||
195 | pr_devel("==>%s(,%lu,,%lu,)\n", __func__, modlen, siglen); | ||
196 | |||
197 | if (siglen <= sizeof(ms)) | ||
198 | return -EBADMSG; | ||
199 | |||
200 | memcpy(&ms, sig + (siglen - sizeof(ms)), sizeof(ms)); | ||
201 | siglen -= sizeof(ms); | ||
202 | |||
203 | sig_len = be32_to_cpu(ms.sig_len); | ||
204 | if (sig_len >= siglen || | ||
205 | siglen - sig_len != (size_t)ms.signer_len + ms.key_id_len) | ||
206 | return -EBADMSG; | ||
207 | |||
208 | /* For the moment, only support RSA and X.509 identifiers */ | ||
209 | if (ms.algo != PKEY_ALGO_RSA || | ||
210 | ms.id_type != PKEY_ID_X509) | ||
211 | return -ENOPKG; | ||
212 | |||
213 | if (ms.hash >= PKEY_HASH__LAST || | ||
214 | !pkey_hash_algo[ms.hash]) | ||
215 | return -ENOPKG; | ||
216 | |||
217 | key = request_asymmetric_key(sig, ms.signer_len, | ||
218 | sig + ms.signer_len, ms.key_id_len); | ||
219 | if (IS_ERR(key)) | ||
220 | return PTR_ERR(key); | ||
221 | |||
222 | pks = mod_make_digest(ms.hash, mod, modlen); | ||
223 | if (IS_ERR(pks)) { | ||
224 | ret = PTR_ERR(pks); | ||
225 | goto error_put_key; | ||
226 | } | ||
227 | |||
228 | ret = mod_extract_mpi_array(pks, sig + ms.signer_len + ms.key_id_len, | ||
229 | sig_len); | ||
230 | if (ret < 0) | ||
231 | goto error_free_pks; | ||
232 | |||
233 | ret = verify_signature(key, pks); | ||
234 | pr_devel("verify_signature() = %d\n", ret); | ||
235 | |||
236 | error_free_pks: | ||
237 | mpi_free(pks->rsa.s); | ||
238 | kfree(pks); | ||
239 | error_put_key: | ||
240 | key_put(key); | ||
241 | pr_devel("<==%s() = %d\n", __func__, ret); | ||
242 | return ret; | ||
243 | } | ||