module_signing.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 <keys/system_keyring.h>
  14. #include <crypto/public_key.h>
  15. #include <crypto/pkcs7.h>
  16. #include "module-internal.h"
  17. /*
  18. * Module signature information block.
  19. *
  20. * The constituents of the signature section are, in order:
  21. *
  22. * - Signer's name
  23. * - Key identifier
  24. * - Signature data
  25. * - Information block
  26. */
  27. struct module_signature {
  28. u8 algo; /* Public-key crypto algorithm [0] */
  29. u8 hash; /* Digest algorithm [0] */
  30. u8 id_type; /* Key identifier type [PKEY_ID_PKCS7] */
  31. u8 signer_len; /* Length of signer's name [0] */
  32. u8 key_id_len; /* Length of key identifier [0] */
  33. u8 __pad[3];
  34. __be32 sig_len; /* Length of signature data */
  35. };
  36. /*
  37. * Verify a PKCS#7-based signature on a module.
  38. */
  39. static int mod_verify_pkcs7(const void *mod, unsigned long modlen,
  40. const void *raw_pkcs7, size_t pkcs7_len)
  41. {
  42. struct pkcs7_message *pkcs7;
  43. bool trusted;
  44. int ret;
  45. pkcs7 = pkcs7_parse_message(raw_pkcs7, pkcs7_len);
  46. if (IS_ERR(pkcs7))
  47. return PTR_ERR(pkcs7);
  48. /* The data should be detached - so we need to supply it. */
  49. if (pkcs7_supply_detached_data(pkcs7, mod, modlen) < 0) {
  50. pr_err("PKCS#7 signature with non-detached data\n");
  51. ret = -EBADMSG;
  52. goto error;
  53. }
  54. ret = pkcs7_verify(pkcs7);
  55. if (ret < 0)
  56. goto error;
  57. ret = pkcs7_validate_trust(pkcs7, system_trusted_keyring, &trusted);
  58. if (ret < 0)
  59. goto error;
  60. if (!trusted) {
  61. pr_err("PKCS#7 signature not signed with a trusted key\n");
  62. ret = -ENOKEY;
  63. }
  64. error:
  65. pkcs7_free_message(pkcs7);
  66. pr_devel("<==%s() = %d\n", __func__, ret);
  67. return ret;
  68. }
  69. /*
  70. * Verify the signature on a module.
  71. */
  72. int mod_verify_sig(const void *mod, unsigned long *_modlen)
  73. {
  74. struct module_signature ms;
  75. size_t modlen = *_modlen, sig_len;
  76. pr_devel("==>%s(,%zu)\n", __func__, modlen);
  77. if (modlen <= sizeof(ms))
  78. return -EBADMSG;
  79. memcpy(&ms, mod + (modlen - sizeof(ms)), sizeof(ms));
  80. modlen -= sizeof(ms);
  81. sig_len = be32_to_cpu(ms.sig_len);
  82. if (sig_len >= modlen)
  83. return -EBADMSG;
  84. modlen -= sig_len;
  85. *_modlen = modlen;
  86. if (ms.id_type != PKEY_ID_PKCS7) {
  87. pr_err("Module is not signed with expected PKCS#7 message\n");
  88. return -ENOPKG;
  89. }
  90. if (ms.algo != 0 ||
  91. ms.hash != 0 ||
  92. ms.signer_len != 0 ||
  93. ms.key_id_len != 0 ||
  94. ms.__pad[0] != 0 ||
  95. ms.__pad[1] != 0 ||
  96. ms.__pad[2] != 0) {
  97. pr_err("PKCS#7 signature info has unexpected non-zero params\n");
  98. return -EBADMSG;
  99. }
  100. return mod_verify_pkcs7(mod, modlen, mod + modlen, sig_len);
  101. }