module_signing.c 2.0 KB

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