module_signing.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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/errno.h>
  13. #include <linux/string.h>
  14. #include <linux/verification.h>
  15. #include <crypto/public_key.h>
  16. #include "module-internal.h"
  17. enum pkey_id_type {
  18. PKEY_ID_PGP, /* OpenPGP generated key ID */
  19. PKEY_ID_X509, /* X.509 arbitrary subjectKeyIdentifier */
  20. PKEY_ID_PKCS7, /* Signature in PKCS#7 message */
  21. };
  22. /*
  23. * Module signature information block.
  24. *
  25. * The constituents of the signature section are, in order:
  26. *
  27. * - Signer's name
  28. * - Key identifier
  29. * - Signature data
  30. * - Information block
  31. */
  32. struct module_signature {
  33. u8 algo; /* Public-key crypto algorithm [0] */
  34. u8 hash; /* Digest algorithm [0] */
  35. u8 id_type; /* Key identifier type [PKEY_ID_PKCS7] */
  36. u8 signer_len; /* Length of signer's name [0] */
  37. u8 key_id_len; /* Length of key identifier [0] */
  38. u8 __pad[3];
  39. __be32 sig_len; /* Length of signature data */
  40. };
  41. /*
  42. * Verify the signature on a module.
  43. */
  44. int mod_verify_sig(const void *mod, unsigned long *_modlen)
  45. {
  46. struct module_signature ms;
  47. size_t modlen = *_modlen, sig_len;
  48. pr_devel("==>%s(,%zu)\n", __func__, modlen);
  49. if (modlen <= sizeof(ms))
  50. return -EBADMSG;
  51. memcpy(&ms, mod + (modlen - sizeof(ms)), sizeof(ms));
  52. modlen -= sizeof(ms);
  53. sig_len = be32_to_cpu(ms.sig_len);
  54. if (sig_len >= modlen)
  55. return -EBADMSG;
  56. modlen -= sig_len;
  57. *_modlen = modlen;
  58. if (ms.id_type != PKEY_ID_PKCS7) {
  59. pr_err("Module is not signed with expected PKCS#7 message\n");
  60. return -ENOPKG;
  61. }
  62. if (ms.algo != 0 ||
  63. ms.hash != 0 ||
  64. ms.signer_len != 0 ||
  65. ms.key_id_len != 0 ||
  66. ms.__pad[0] != 0 ||
  67. ms.__pad[1] != 0 ||
  68. ms.__pad[2] != 0) {
  69. pr_err("PKCS#7 signature info has unexpected non-zero params\n");
  70. return -EBADMSG;
  71. }
  72. return verify_pkcs7_signature(mod, modlen, mod + modlen, sig_len,
  73. NULL, VERIFYING_MODULE_SIGNATURE,
  74. NULL, NULL);
  75. }