module_signing.c 2.0 KB

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