1
2
3
4
5
6
7
8
9
10
11
12#include <linux/kernel.h>
13#include <linux/errno.h>
14#include <linux/string.h>
15#include <linux/verification.h>
16#include <crypto/public_key.h>
17#include "module-internal.h"
18
19enum pkey_id_type {
20 PKEY_ID_PGP,
21 PKEY_ID_X509,
22 PKEY_ID_PKCS7,
23};
24
25
26
27
28
29
30
31
32
33
34
35struct module_signature {
36 u8 algo;
37 u8 hash;
38 u8 id_type;
39 u8 signer_len;
40 u8 key_id_len;
41 u8 __pad[3];
42 __be32 sig_len;
43};
44
45
46
47
48int mod_verify_sig(const void *mod, unsigned long *_modlen)
49{
50 struct module_signature ms;
51 size_t modlen = *_modlen, sig_len;
52
53 pr_devel("==>%s(,%zu)\n", __func__, modlen);
54
55 if (modlen <= sizeof(ms))
56 return -EBADMSG;
57
58 memcpy(&ms, mod + (modlen - sizeof(ms)), sizeof(ms));
59 modlen -= sizeof(ms);
60
61 sig_len = be32_to_cpu(ms.sig_len);
62 if (sig_len >= modlen)
63 return -EBADMSG;
64 modlen -= sig_len;
65 *_modlen = modlen;
66
67 if (ms.id_type != PKEY_ID_PKCS7) {
68 pr_err("Module is not signed with expected PKCS#7 message\n");
69 return -ENOPKG;
70 }
71
72 if (ms.algo != 0 ||
73 ms.hash != 0 ||
74 ms.signer_len != 0 ||
75 ms.key_id_len != 0 ||
76 ms.__pad[0] != 0 ||
77 ms.__pad[1] != 0 ||
78 ms.__pad[2] != 0) {
79 pr_err("PKCS#7 signature info has unexpected non-zero params\n");
80 return -EBADMSG;
81 }
82
83 return verify_pkcs7_signature(mod, modlen, mod + modlen, sig_len,
84 NULL, VERIFYING_MODULE_SIGNATURE,
85 NULL, NULL);
86}
87