1 // SPDX-License-Identifier: GPL-2.0+
3 * IMA support for appraising module-style appended signatures.
5 * Copyright (C) 2019 IBM Corporation
8 * Thiago Jung Bauermann <bauerman@linux.ibm.com>
11 #include <linux/types.h>
12 #include <linux/module_signature.h>
13 #include <keys/asymmetric-type.h>
14 #include <crypto/pkcs7.h>
19 struct pkcs7_message *pkcs7_msg;
21 enum hash_algo hash_algo;
23 /* This digest will go in the 'd-modsig' field of the IMA template. */
28 * This is what will go to the measurement list if the template requires
29 * storing the signature.
36 * ima_read_modsig - Read modsig from buf.
38 * Return: 0 on success, error code otherwise.
40 int ima_read_modsig(enum ima_hooks func, const void *buf, loff_t buf_len,
41 struct modsig **modsig)
43 const size_t marker_len = strlen(MODULE_SIG_STRING);
44 const struct module_signature *sig;
50 if (buf_len <= marker_len + sizeof(*sig))
53 p = buf + buf_len - marker_len;
54 if (memcmp(p, MODULE_SIG_STRING, marker_len))
57 buf_len -= marker_len;
58 sig = (const struct module_signature *)(p - sizeof(*sig));
60 rc = mod_check_sig(sig, buf_len, func_tokens[func]);
64 sig_len = be32_to_cpu(sig->sig_len);
65 buf_len -= sig_len + sizeof(*sig);
67 /* Allocate sig_len additional bytes to hold the raw PKCS#7 data. */
68 hdr = kzalloc(sizeof(*hdr) + sig_len, GFP_KERNEL);
72 hdr->pkcs7_msg = pkcs7_parse_message(buf + buf_len, sig_len);
73 if (IS_ERR(hdr->pkcs7_msg)) {
74 rc = PTR_ERR(hdr->pkcs7_msg);
79 memcpy(hdr->raw_pkcs7, buf + buf_len, sig_len);
80 hdr->raw_pkcs7_len = sig_len;
82 /* We don't know the hash algorithm yet. */
83 hdr->hash_algo = HASH_ALGO__LAST;
91 * ima_collect_modsig - Calculate the file hash without the appended signature.
93 * Since the modsig is part of the file contents, the hash used in its signature
94 * isn't the same one ordinarily calculated by IMA. Therefore PKCS7 code
95 * calculates a separate one for signature verification.
97 void ima_collect_modsig(struct modsig *modsig, const void *buf, loff_t size)
102 * Provide the file contents (minus the appended sig) so that the PKCS7
103 * code can calculate the file hash.
105 size -= modsig->raw_pkcs7_len + strlen(MODULE_SIG_STRING) +
106 sizeof(struct module_signature);
107 rc = pkcs7_supply_detached_data(modsig->pkcs7_msg, buf, size);
111 /* Ask the PKCS7 code to calculate the file hash. */
112 rc = pkcs7_get_digest(modsig->pkcs7_msg, &modsig->digest,
113 &modsig->digest_size, &modsig->hash_algo);
116 int ima_modsig_verify(struct key *keyring, const struct modsig *modsig)
118 return verify_pkcs7_message_sig(NULL, 0, modsig->pkcs7_msg, keyring,
119 VERIFYING_MODULE_SIGNATURE, NULL, NULL);
122 int ima_get_modsig_digest(const struct modsig *modsig, enum hash_algo *algo,
123 const u8 **digest, u32 *digest_size)
125 *algo = modsig->hash_algo;
126 *digest = modsig->digest;
127 *digest_size = modsig->digest_size;
132 int ima_get_raw_modsig(const struct modsig *modsig, const void **data,
135 *data = &modsig->raw_pkcs7;
136 *data_len = modsig->raw_pkcs7_len;
141 void ima_free_modsig(struct modsig *modsig)
146 pkcs7_free_message(modsig->pkcs7_msg);