module_signing.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. #include <linux/kernel.h>
  12. #include <linux/err.h>
  13. #include <crypto/public_key.h>
  14. #include <crypto/hash.h>
  15. #include <keys/asymmetric-type.h>
  16. #include <keys/system_keyring.h>
  17. #include "module-internal.h"
  18. /*
  19. * Module signature information block.
  20. *
  21. * The constituents of the signature section are, in order:
  22. *
  23. * - Signer's name
  24. * - Key identifier
  25. * - Signature data
  26. * - Information block
  27. */
  28. struct module_signature {
  29. u8 algo; /* Public-key crypto algorithm [enum pkey_algo] */
  30. u8 hash; /* Digest algorithm [enum hash_algo] */
  31. u8 id_type; /* Key identifier type [enum pkey_id_type] */
  32. u8 signer_len; /* Length of signer's name */
  33. u8 key_id_len; /* Length of key identifier */
  34. u8 __pad[3];
  35. __be32 sig_len; /* Length of signature data */
  36. };
  37. /*
  38. * Digest the module contents.
  39. */
  40. static struct public_key_signature *mod_make_digest(enum hash_algo hash,
  41. const void *mod,
  42. unsigned long modlen)
  43. {
  44. struct public_key_signature *pks;
  45. struct crypto_shash *tfm;
  46. struct shash_desc *desc;
  47. size_t digest_size, desc_size;
  48. int ret;
  49. pr_devel("==>%s()\n", __func__);
  50. /* Allocate the hashing algorithm we're going to need and find out how
  51. * big the hash operational data will be.
  52. */
  53. tfm = crypto_alloc_shash(hash_algo_name[hash], 0, 0);
  54. if (IS_ERR(tfm))
  55. return (PTR_ERR(tfm) == -ENOENT) ? ERR_PTR(-ENOPKG) : ERR_CAST(tfm);
  56. desc_size = crypto_shash_descsize(tfm) + sizeof(*desc);
  57. digest_size = crypto_shash_digestsize(tfm);
  58. /* We allocate the hash operational data storage on the end of our
  59. * context data and the digest output buffer on the end of that.
  60. */
  61. ret = -ENOMEM;
  62. pks = kzalloc(digest_size + sizeof(*pks) + desc_size, GFP_KERNEL);
  63. if (!pks)
  64. goto error_no_pks;
  65. pks->pkey_hash_algo = hash;
  66. pks->digest = (u8 *)pks + sizeof(*pks) + desc_size;
  67. pks->digest_size = digest_size;
  68. desc = (void *)pks + sizeof(*pks);
  69. desc->tfm = tfm;
  70. desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  71. ret = crypto_shash_init(desc);
  72. if (ret < 0)
  73. goto error;
  74. ret = crypto_shash_finup(desc, mod, modlen, pks->digest);
  75. if (ret < 0)
  76. goto error;
  77. crypto_free_shash(tfm);
  78. pr_devel("<==%s() = ok\n", __func__);
  79. return pks;
  80. error:
  81. kfree(pks);
  82. error_no_pks:
  83. crypto_free_shash(tfm);
  84. pr_devel("<==%s() = %d\n", __func__, ret);
  85. return ERR_PTR(ret);
  86. }
  87. /*
  88. * Extract an MPI array from the signature data. This represents the actual
  89. * signature. Each raw MPI is prefaced by a BE 2-byte value indicating the
  90. * size of the MPI in bytes.
  91. *
  92. * RSA signatures only have one MPI, so currently we only read one.
  93. */
  94. static int mod_extract_mpi_array(struct public_key_signature *pks,
  95. const void *data, size_t len)
  96. {
  97. size_t nbytes;
  98. MPI mpi;
  99. if (len < 3)
  100. return -EBADMSG;
  101. nbytes = ((const u8 *)data)[0] << 8 | ((const u8 *)data)[1];
  102. data += 2;
  103. len -= 2;
  104. if (len != nbytes)
  105. return -EBADMSG;
  106. mpi = mpi_read_raw_data(data, nbytes);
  107. if (!mpi)
  108. return -ENOMEM;
  109. pks->mpi[0] = mpi;
  110. pks->nr_mpi = 1;
  111. return 0;
  112. }
  113. /*
  114. * Request an asymmetric key.
  115. */
  116. static struct key *request_asymmetric_key(const char *signer, size_t signer_len,
  117. const u8 *key_id, size_t key_id_len)
  118. {
  119. key_ref_t key;
  120. size_t i;
  121. char *id, *q;
  122. pr_devel("==>%s(,%zu,,%zu)\n", __func__, signer_len, key_id_len);
  123. /* Construct an identifier. */
  124. id = kmalloc(signer_len + 2 + key_id_len * 2 + 1, GFP_KERNEL);
  125. if (!id)
  126. return ERR_PTR(-ENOKEY);
  127. memcpy(id, signer, signer_len);
  128. q = id + signer_len;
  129. *q++ = ':';
  130. *q++ = ' ';
  131. for (i = 0; i < key_id_len; i++) {
  132. *q++ = hex_asc[*key_id >> 4];
  133. *q++ = hex_asc[*key_id++ & 0x0f];
  134. }
  135. *q = 0;
  136. pr_debug("Look up: \"%s\"\n", id);
  137. key = keyring_search(make_key_ref(system_trusted_keyring, 1),
  138. &key_type_asymmetric, id);
  139. if (IS_ERR(key))
  140. pr_warn("Request for unknown module key '%s' err %ld\n",
  141. id, PTR_ERR(key));
  142. kfree(id);
  143. if (IS_ERR(key)) {
  144. switch (PTR_ERR(key)) {
  145. /* Hide some search errors */
  146. case -EACCES:
  147. case -ENOTDIR:
  148. case -EAGAIN:
  149. return ERR_PTR(-ENOKEY);
  150. default:
  151. return ERR_CAST(key);
  152. }
  153. }
  154. pr_devel("<==%s() = 0 [%x]\n", __func__, key_serial(key_ref_to_ptr(key)));
  155. return key_ref_to_ptr(key);
  156. }
  157. /*
  158. * Verify the signature on a module.
  159. */
  160. int mod_verify_sig(const void *mod, unsigned long *_modlen)
  161. {
  162. struct public_key_signature *pks;
  163. struct module_signature ms;
  164. struct key *key;
  165. const void *sig;
  166. size_t modlen = *_modlen, sig_len;
  167. int ret;
  168. pr_devel("==>%s(,%zu)\n", __func__, modlen);
  169. if (modlen <= sizeof(ms))
  170. return -EBADMSG;
  171. memcpy(&ms, mod + (modlen - sizeof(ms)), sizeof(ms));
  172. modlen -= sizeof(ms);
  173. sig_len = be32_to_cpu(ms.sig_len);
  174. if (sig_len >= modlen)
  175. return -EBADMSG;
  176. modlen -= sig_len;
  177. if ((size_t)ms.signer_len + ms.key_id_len >= modlen)
  178. return -EBADMSG;
  179. modlen -= (size_t)ms.signer_len + ms.key_id_len;
  180. *_modlen = modlen;
  181. sig = mod + modlen;
  182. /* For the moment, only support RSA and X.509 identifiers */
  183. if (ms.algo != PKEY_ALGO_RSA ||
  184. ms.id_type != PKEY_ID_X509)
  185. return -ENOPKG;
  186. if (ms.hash >= PKEY_HASH__LAST ||
  187. !hash_algo_name[ms.hash])
  188. return -ENOPKG;
  189. key = request_asymmetric_key(sig, ms.signer_len,
  190. sig + ms.signer_len, ms.key_id_len);
  191. if (IS_ERR(key))
  192. return PTR_ERR(key);
  193. pks = mod_make_digest(ms.hash, mod, modlen);
  194. if (IS_ERR(pks)) {
  195. ret = PTR_ERR(pks);
  196. goto error_put_key;
  197. }
  198. ret = mod_extract_mpi_array(pks, sig + ms.signer_len + ms.key_id_len,
  199. sig_len);
  200. if (ret < 0)
  201. goto error_free_pks;
  202. ret = verify_signature(key, pks);
  203. pr_devel("verify_signature() = %d\n", ret);
  204. error_free_pks:
  205. mpi_free(pks->rsa.s);
  206. kfree(pks);
  207. error_put_key:
  208. key_put(key);
  209. pr_devel("<==%s() = %d\n", __func__, ret);
  210. return ret;
  211. }