modsign_pubkey.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* Public keys for module signature verification
  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/sched.h>
  13. #include <linux/cred.h>
  14. #include <linux/err.h>
  15. #include <keys/asymmetric-type.h>
  16. #include "module-internal.h"
  17. struct key *modsign_keyring;
  18. extern __initdata const u8 modsign_certificate_list[];
  19. extern __initdata const u8 modsign_certificate_list_end[];
  20. /*
  21. * We need to make sure ccache doesn't cache the .o file as it doesn't notice
  22. * if modsign.pub changes.
  23. */
  24. static __initdata const char annoy_ccache[] = __TIME__ "foo";
  25. /*
  26. * Load the compiled-in keys
  27. */
  28. static __init int module_verify_init(void)
  29. {
  30. pr_notice("Initialise module verification\n");
  31. modsign_keyring = keyring_alloc(".module_sign",
  32. KUIDT_INIT(0), KGIDT_INIT(0),
  33. current_cred(),
  34. ((KEY_POS_ALL & ~KEY_POS_SETATTR) |
  35. KEY_USR_VIEW | KEY_USR_READ),
  36. KEY_ALLOC_NOT_IN_QUOTA, NULL);
  37. if (IS_ERR(modsign_keyring))
  38. panic("Can't allocate module signing keyring\n");
  39. return 0;
  40. }
  41. /*
  42. * Must be initialised before we try and load the keys into the keyring.
  43. */
  44. device_initcall(module_verify_init);
  45. /*
  46. * Load the compiled-in keys
  47. */
  48. static __init int load_module_signing_keys(void)
  49. {
  50. key_ref_t key;
  51. const u8 *p, *end;
  52. size_t plen;
  53. pr_notice("Loading module verification certificates\n");
  54. end = modsign_certificate_list_end;
  55. p = modsign_certificate_list;
  56. while (p < end) {
  57. /* Each cert begins with an ASN.1 SEQUENCE tag and must be more
  58. * than 256 bytes in size.
  59. */
  60. if (end - p < 4)
  61. goto dodgy_cert;
  62. if (p[0] != 0x30 &&
  63. p[1] != 0x82)
  64. goto dodgy_cert;
  65. plen = (p[2] << 8) | p[3];
  66. plen += 4;
  67. if (plen > end - p)
  68. goto dodgy_cert;
  69. key = key_create_or_update(make_key_ref(modsign_keyring, 1),
  70. "asymmetric",
  71. NULL,
  72. p,
  73. plen,
  74. (KEY_POS_ALL & ~KEY_POS_SETATTR) |
  75. KEY_USR_VIEW,
  76. KEY_ALLOC_NOT_IN_QUOTA);
  77. if (IS_ERR(key))
  78. pr_err("MODSIGN: Problem loading in-kernel X.509 certificate (%ld)\n",
  79. PTR_ERR(key));
  80. else
  81. pr_notice("MODSIGN: Loaded cert '%s'\n",
  82. key_ref_to_ptr(key)->description);
  83. p += plen;
  84. }
  85. return 0;
  86. dodgy_cert:
  87. pr_err("MODSIGN: Problem parsing in-kernel X.509 certificate list\n");
  88. return 0;
  89. }
  90. late_initcall(load_module_signing_keys);