Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/sparc
[pandora-kernel.git] / kernel / modsign_pubkey.c
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
24 /*
25  * We need to make sure ccache doesn't cache the .o file as it doesn't notice
26  * if modsign.pub changes.
27  */
28 static __initdata const char annoy_ccache[] = __TIME__ "foo";
29
30 /*
31  * Load the compiled-in keys
32  */
33 static __init int module_verify_init(void)
34 {
35         pr_notice("Initialise module verification\n");
36
37         modsign_keyring = key_alloc(&key_type_keyring, ".module_sign",
38                                     KUIDT_INIT(0), KGIDT_INIT(0),
39                                     current_cred(),
40                                     (KEY_POS_ALL & ~KEY_POS_SETATTR) |
41                                     KEY_USR_VIEW | KEY_USR_READ,
42                                     KEY_ALLOC_NOT_IN_QUOTA);
43         if (IS_ERR(modsign_keyring))
44                 panic("Can't allocate module signing keyring\n");
45
46         if (key_instantiate_and_link(modsign_keyring, NULL, 0, NULL, NULL) < 0)
47                 panic("Can't instantiate module signing keyring\n");
48
49         return 0;
50 }
51
52 /*
53  * Must be initialised before we try and load the keys into the keyring.
54  */
55 device_initcall(module_verify_init);
56
57 /*
58  * Load the compiled-in keys
59  */
60 static __init int load_module_signing_keys(void)
61 {
62         key_ref_t key;
63         const u8 *p, *end;
64         size_t plen;
65
66         pr_notice("Loading module verification certificates\n");
67
68         end = modsign_certificate_list_end;
69         p = modsign_certificate_list;
70         while (p < end) {
71                 /* Each cert begins with an ASN.1 SEQUENCE tag and must be more
72                  * than 256 bytes in size.
73                  */
74                 if (end - p < 4)
75                         goto dodgy_cert;
76                 if (p[0] != 0x30 &&
77                     p[1] != 0x82)
78                         goto dodgy_cert;
79                 plen = (p[2] << 8) | p[3];
80                 plen += 4;
81                 if (plen > end - p)
82                         goto dodgy_cert;
83
84                 key = key_create_or_update(make_key_ref(modsign_keyring, 1),
85                                            "asymmetric",
86                                            NULL,
87                                            p,
88                                            plen,
89                                            (KEY_POS_ALL & ~KEY_POS_SETATTR) |
90                                            KEY_USR_VIEW,
91                                            KEY_ALLOC_NOT_IN_QUOTA);
92                 if (IS_ERR(key))
93                         pr_err("MODSIGN: Problem loading in-kernel X.509 certificate (%ld)\n",
94                                PTR_ERR(key));
95                 else
96                         pr_notice("MODSIGN: Loaded cert '%s'\n",
97                                   key_ref_to_ptr(key)->description);
98                 p += plen;
99         }
100
101         return 0;
102
103 dodgy_cert:
104         pr_err("MODSIGN: Problem parsing in-kernel X.509 certificate list\n");
105         return 0;
106 }
107 late_initcall(load_module_signing_keys);