module_signature.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Module signature checker
  4. *
  5. * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
  6. * Written by David Howells (dhowells@redhat.com)
  7. */
  8. #include <linux/errno.h>
  9. #include <linux/printk.h>
  10. #include <linux/module_signature.h>
  11. #include <asm/byteorder.h>
  12. /**
  13. * mod_check_sig - check that the given signature is sane
  14. *
  15. * @ms: Signature to check.
  16. * @file_len: Size of the file to which @ms is appended.
  17. * @name: What is being checked. Used for error messages.
  18. */
  19. int mod_check_sig(const struct module_signature *ms, size_t file_len,
  20. const char *name)
  21. {
  22. if (be32_to_cpu(ms->sig_len) >= file_len - sizeof(*ms))
  23. return -EBADMSG;
  24. if (ms->id_type != PKEY_ID_PKCS7) {
  25. pr_err("%s: not signed with expected PKCS#7 message\n",
  26. name);
  27. return -ENOPKG;
  28. }
  29. if (ms->algo != 0 ||
  30. ms->hash != 0 ||
  31. ms->signer_len != 0 ||
  32. ms->key_id_len != 0 ||
  33. ms->__pad[0] != 0 ||
  34. ms->__pad[1] != 0 ||
  35. ms->__pad[2] != 0) {
  36. pr_err("%s: PKCS#7 signature info has unexpected non-zero params\n",
  37. name);
  38. return -EBADMSG;
  39. }
  40. return 0;
  41. }