Skip to content

Commit 106a4ee

Browse files
committed
module: signature checking hook
We do a very simple search for a particular string appended to the module (which is cache-hot and about to be SHA'd anyway). There's both a config option and a boot parameter which control whether we accept or fail with unsigned modules and modules that are signed with an unknown key. If module signing is enabled, the kernel will be tainted if a module is loaded that is unsigned or has a signature for which we don't have the key. (Useful feedback and tweaks by David Howells <dhowells@redhat.com>) Signed-off-by: Rusty Russell <rusty@rustcorp.com.au> Signed-off-by: David Howells <dhowells@redhat.com> Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
1 parent c26fd69 commit 106a4ee

7 files changed

Lines changed: 157 additions & 1 deletion

File tree

‎Documentation/kernel-parameters.txt‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1582,6 +1582,12 @@ bytes respectively. Such letter suffixes can also be entirely omitted.
15821582
log everything. Information is printed at KERN_DEBUG
15831583
so loglevel=8 may also need to be specified.
15841584

1585+
module.sig_enforce
1586+
[KNL] When CONFIG_MODULE_SIG is set, this means that
1587+
modules without (valid) signatures will fail to load.
1588+
Note that if CONFIG_MODULE_SIG_ENFORCE is set, that
1589+
is always true, so this option does nothing.
1590+
15851591
mousedev.tap_time=
15861592
[MOUSE] Maximum time between finger touching and
15871593
leaving touchpad surface for touch to be considered

‎include/linux/module.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
#include <linux/percpu.h>
2222
#include <asm/module.h>
2323

24+
/* In stripped ARM and x86-64 modules, ~ is surprisingly rare. */
25+
#define MODULE_SIG_STRING "~Module signature appended~\n"
26+
2427
/* Not Yet Implemented */
2528
#define MODULE_SUPPORTED_DEVICE(name)
2629

@@ -260,6 +263,11 @@ struct module
260263
const unsigned long *unused_gpl_crcs;
261264
#endif
262265

266+
#ifdef CONFIG_MODULE_SIG
267+
/* Signature was verified. */
268+
bool sig_ok;
269+
#endif
270+
263271
/* symbols that will be GPL-only in the near future. */
264272
const struct kernel_symbol *gpl_future_syms;
265273
const unsigned long *gpl_future_crcs;

‎init/Kconfig‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1585,6 +1585,20 @@ config MODULE_SRCVERSION_ALL
15851585
the version). With this option, such a "srcversion" field
15861586
will be created for all modules. If unsure, say N.
15871587

1588+
config MODULE_SIG
1589+
bool "Module signature verification"
1590+
depends on MODULES
1591+
help
1592+
Check modules for valid signatures upon load: the signature
1593+
is simply appended to the module. For more information see
1594+
Documentation/module-signing.txt.
1595+
1596+
config MODULE_SIG_FORCE
1597+
bool "Require modules to be validly signed"
1598+
depends on MODULE_SIG
1599+
help
1600+
Reject unsigned modules or signed modules for which we don't have a
1601+
key. Without this, such modules will simply taint the kernel.
15881602
endif # MODULES
15891603

15901604
config INIT_ALL_POSSIBLE

‎kernel/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ obj-$(CONFIG_DEBUG_SPINLOCK) += spinlock.o
5555
obj-$(CONFIG_PROVE_LOCKING) += spinlock.o
5656
obj-$(CONFIG_UID16) += uid16.o
5757
obj-$(CONFIG_MODULES) += module.o
58+
obj-$(CONFIG_MODULE_SIG) += module_signing.o
5859
obj-$(CONFIG_KALLSYMS) += kallsyms.o
5960
obj-$(CONFIG_BSD_PROCESS_ACCT) += acct.o
6061
obj-$(CONFIG_KEXEC) += kexec.o

‎kernel/module-internal.h‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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 int mod_verify_sig(const void *mod, unsigned long modlen,
13+
const void *sig, unsigned long siglen);

‎kernel/module.c‎

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
#include <linux/jump_label.h>
5959
#include <linux/pfn.h>
6060
#include <linux/bsearch.h>
61+
#include "module-internal.h"
6162

6263
#define CREATE_TRACE_POINTS
6364
#include <trace/events/module.h>
@@ -102,6 +103,43 @@ static LIST_HEAD(modules);
102103
struct list_head *kdb_modules = &modules; /* kdb needs the list of modules */
103104
#endif /* CONFIG_KGDB_KDB */
104105

106+
#ifdef CONFIG_MODULE_SIG
107+
#ifdef CONFIG_MODULE_SIG_FORCE
108+
static bool sig_enforce = true;
109+
#else
110+
static bool sig_enforce = false;
111+
112+
static int param_set_bool_enable_only(const char *val,
113+
const struct kernel_param *kp)
114+
{
115+
int err;
116+
bool test;
117+
struct kernel_param dummy_kp = *kp;
118+
119+
dummy_kp.arg = &test;
120+
121+
err = param_set_bool(val, &dummy_kp);
122+
if (err)
123+
return err;
124+
125+
/* Don't let them unset it once it's set! */
126+
if (!test && sig_enforce)
127+
return -EROFS;
128+
129+
if (test)
130+
sig_enforce = true;
131+
return 0;
132+
}
133+
134+
static const struct kernel_param_ops param_ops_bool_enable_only = {
135+
.set = param_set_bool_enable_only,
136+
.get = param_get_bool,
137+
};
138+
#define param_check_bool_enable_only param_check_bool
139+
140+
module_param(sig_enforce, bool_enable_only, 0644);
141+
#endif /* !CONFIG_MODULE_SIG_FORCE */
142+
#endif /* CONFIG_MODULE_SIG */
105143

106144
/* Block module loading/unloading? */
107145
int modules_disabled = 0;
@@ -136,6 +174,7 @@ struct load_info {
136174
unsigned long symoffs, stroffs;
137175
struct _ddebug *debug;
138176
unsigned int num_debug;
177+
bool sig_ok;
139178
struct {
140179
unsigned int sym, str, mod, vers, info, pcpu;
141180
} index;
@@ -2379,7 +2418,49 @@ static inline void kmemleak_load_module(const struct module *mod,
23792418
}
23802419
#endif
23812420

2382-
/* Sets info->hdr and info->len. */
2421+
#ifdef CONFIG_MODULE_SIG
2422+
static int module_sig_check(struct load_info *info,
2423+
const void *mod, unsigned long *len)
2424+
{
2425+
int err = -ENOKEY;
2426+
const unsigned long markerlen = sizeof(MODULE_SIG_STRING) - 1;
2427+
const void *p = mod, *end = mod + *len;
2428+
2429+
/* Poor man's memmem. */
2430+
while ((p = memchr(p, MODULE_SIG_STRING[0], end - p))) {
2431+
if (p + markerlen > end)
2432+
break;
2433+
2434+
if (memcmp(p, MODULE_SIG_STRING, markerlen) == 0) {
2435+
const void *sig = p + markerlen;
2436+
/* Truncate module up to signature. */
2437+
*len = p - mod;
2438+
err = mod_verify_sig(mod, *len, sig, end - sig);
2439+
break;
2440+
}
2441+
p++;
2442+
}
2443+
2444+
if (!err) {
2445+
info->sig_ok = true;
2446+
return 0;
2447+
}
2448+
2449+
/* Not having a signature is only an error if we're strict. */
2450+
if (err == -ENOKEY && !sig_enforce)
2451+
err = 0;
2452+
2453+
return err;
2454+
}
2455+
#else /* !CONFIG_MODULE_SIG */
2456+
static int module_sig_check(struct load_info *info,
2457+
void *mod, unsigned long *len)
2458+
{
2459+
return 0;
2460+
}
2461+
#endif /* !CONFIG_MODULE_SIG */
2462+
2463+
/* Sets info->hdr, info->len and info->sig_ok. */
23832464
static int copy_and_check(struct load_info *info,
23842465
const void __user *umod, unsigned long len,
23852466
const char __user *uargs)
@@ -2399,6 +2480,10 @@ static int copy_and_check(struct load_info *info,
23992480
goto free_hdr;
24002481
}
24012482

2483+
err = module_sig_check(info, hdr, &len);
2484+
if (err)
2485+
goto free_hdr;
2486+
24022487
/* Sanity checks against insmoding binaries or wrong arch,
24032488
weird elf version */
24042489
if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0
@@ -2884,6 +2969,12 @@ static struct module *load_module(void __user *umod,
28842969
goto free_copy;
28852970
}
28862971

2972+
#ifdef CONFIG_MODULE_SIG
2973+
mod->sig_ok = info.sig_ok;
2974+
if (!mod->sig_ok)
2975+
add_taint_module(mod, TAINT_FORCED_MODULE);
2976+
#endif
2977+
28872978
/* Now module is in final location, initialize linked lists, etc. */
28882979
err = module_unload_init(mod);
28892980
if (err)

‎kernel/module_signing.c‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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 "module-internal.h"
15+
16+
/*
17+
* Verify the signature on a module.
18+
*/
19+
int mod_verify_sig(const void *mod, unsigned long modlen,
20+
const void *sig, unsigned long siglen)
21+
{
22+
return -ENOKEY;
23+
}

0 commit comments

Comments
 (0)