MODSIGN: Use PKCS#7 messages as module signatures
[firefly-linux-kernel-4.4.55.git] / kernel / module_signing.c
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 <keys/system_keyring.h>
15 #include <crypto/public_key.h>
16 #include <crypto/pkcs7.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         u8      algo;           /* Public-key crypto algorithm [0] */
31         u8      hash;           /* Digest algorithm [0] */
32         u8      id_type;        /* Key identifier type [PKEY_ID_PKCS7] */
33         u8      signer_len;     /* Length of signer's name [0] */
34         u8      key_id_len;     /* Length of key identifier [0] */
35         u8      __pad[3];
36         __be32  sig_len;        /* Length of signature data */
37 };
38
39 /*
40  * Verify a PKCS#7-based signature on a module.
41  */
42 static int mod_verify_pkcs7(const void *mod, unsigned long modlen,
43                             const void *raw_pkcs7, size_t pkcs7_len)
44 {
45         struct pkcs7_message *pkcs7;
46         bool trusted;
47         int ret;
48
49         pkcs7 = pkcs7_parse_message(raw_pkcs7, pkcs7_len);
50         if (IS_ERR(pkcs7))
51                 return PTR_ERR(pkcs7);
52
53         /* The data should be detached - so we need to supply it. */
54         if (pkcs7_supply_detached_data(pkcs7, mod, modlen) < 0) {
55                 pr_err("PKCS#7 signature with non-detached data\n");
56                 ret = -EBADMSG;
57                 goto error;
58         }
59
60         ret = pkcs7_verify(pkcs7);
61         if (ret < 0)
62                 goto error;
63
64         ret = pkcs7_validate_trust(pkcs7, system_trusted_keyring, &trusted);
65         if (ret < 0)
66                 goto error;
67
68         if (!trusted) {
69                 pr_err("PKCS#7 signature not signed with a trusted key\n");
70                 ret = -ENOKEY;
71         }
72
73 error:
74         pkcs7_free_message(pkcs7);
75         pr_devel("<==%s() = %d\n", __func__, ret);
76         return ret;
77 }
78
79 /*
80  * Verify the signature on a module.
81  */
82 int mod_verify_sig(const void *mod, unsigned long *_modlen)
83 {
84         struct module_signature ms;
85         size_t modlen = *_modlen, sig_len;
86
87         pr_devel("==>%s(,%zu)\n", __func__, modlen);
88
89         if (modlen <= sizeof(ms))
90                 return -EBADMSG;
91
92         memcpy(&ms, mod + (modlen - sizeof(ms)), sizeof(ms));
93         modlen -= sizeof(ms);
94
95         sig_len = be32_to_cpu(ms.sig_len);
96         if (sig_len >= modlen)
97                 return -EBADMSG;
98         modlen -= sig_len;
99         *_modlen = modlen;
100
101         if (ms.id_type != PKEY_ID_PKCS7) {
102                 pr_err("Module is not signed with expected PKCS#7 message\n");
103                 return -ENOPKG;
104         }
105
106         if (ms.algo != 0 ||
107             ms.hash != 0 ||
108             ms.signer_len != 0 ||
109             ms.key_id_len != 0 ||
110             ms.__pad[0] != 0 ||
111             ms.__pad[1] != 0 ||
112             ms.__pad[2] != 0) {
113                 pr_err("PKCS#7 signature info has unexpected non-zero params\n");
114                 return -EBADMSG;
115         }
116
117         return mod_verify_pkcs7(mod, modlen, mod + modlen, sig_len);
118 }