modsign_pubkey.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 = key_alloc(&key_type_keyring, ".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);
  37. if (IS_ERR(modsign_keyring))
  38. panic("Can't allocate module signing keyring\n");
  39. if (key_instantiate_and_link(modsign_keyring, NULL, 0, NULL, NULL) < 0)
  40. panic("Can't instantiate module signing keyring\n");
  41. return 0;
  42. }
  43. /*
  44. * Must be initialised before we try and load the keys into the keyring.
  45. */
  46. device_initcall(module_verify_init);
  47. /*
  48. * Load the compiled-in keys
  49. */
  50. static __init int load_module_signing_keys(void)
  51. {
  52. key_ref_t key;
  53. const u8 *p, *end;
  54. size_t plen;
  55. pr_notice("Loading module verification certificates\n");
  56. end = modsign_certificate_list_end;
  57. p = modsign_certificate_list;
  58. while (p < end) {
  59. /* Each cert begins with an ASN.1 SEQUENCE tag and must be more
  60. * than 256 bytes in size.
  61. */
  62. if (end - p < 4)
  63. goto dodgy_cert;
  64. if (p[0] != 0x30 &&
  65. p[1] != 0x82)
  66. goto dodgy_cert;
  67. plen = (p[2] << 8) | p[3];
  68. plen += 4;
  69. if (plen > end - p)
  70. goto dodgy_cert;
  71. key = key_create_or_update(make_key_ref(modsign_keyring, 1),
  72. "asymmetric",
  73. NULL,
  74. p,
  75. plen,
  76. (KEY_POS_ALL & ~KEY_POS_SETATTR) |
  77. KEY_USR_VIEW,
  78. KEY_ALLOC_NOT_IN_QUOTA);
  79. if (IS_ERR(key))
  80. pr_err("MODSIGN: Problem loading in-kernel X.509 certificate (%ld)\n",
  81. PTR_ERR(key));
  82. else
  83. pr_notice("MODSIGN: Loaded cert '%s'\n",
  84. key_ref_to_ptr(key)->description);
  85. p += plen;
  86. }
  87. return 0;
  88. dodgy_cert:
  89. pr_err("MODSIGN: Problem parsing in-kernel X.509 certificate list\n");
  90. return 0;
  91. }
  92. late_initcall(load_module_signing_keys);