modsign_pubkey.c 2.9 KB

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