system_keyring.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /* System trusted keyring for trusted public keys
  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/export.h>
  12. #include <linux/kernel.h>
  13. #include <linux/sched.h>
  14. #include <linux/cred.h>
  15. #include <linux/err.h>
  16. #include <keys/asymmetric-type.h>
  17. #include <keys/system_keyring.h>
  18. #include "module-internal.h"
  19. struct key *system_trusted_keyring;
  20. EXPORT_SYMBOL_GPL(system_trusted_keyring);
  21. extern __initconst const u8 system_certificate_list[];
  22. extern __initconst const unsigned long system_certificate_list_size;
  23. /*
  24. * Load the compiled-in keys
  25. */
  26. static __init int system_trusted_keyring_init(void)
  27. {
  28. pr_notice("Initialise system trusted keyring\n");
  29. system_trusted_keyring =
  30. keyring_alloc(".system_keyring",
  31. KUIDT_INIT(0), KGIDT_INIT(0), current_cred(),
  32. ((KEY_POS_ALL & ~KEY_POS_SETATTR) |
  33. KEY_USR_VIEW | KEY_USR_READ | KEY_USR_SEARCH),
  34. KEY_ALLOC_NOT_IN_QUOTA, NULL);
  35. if (IS_ERR(system_trusted_keyring))
  36. panic("Can't allocate system trusted keyring\n");
  37. set_bit(KEY_FLAG_TRUSTED_ONLY, &system_trusted_keyring->flags);
  38. return 0;
  39. }
  40. /*
  41. * Must be initialised before we try and load the keys into the keyring.
  42. */
  43. device_initcall(system_trusted_keyring_init);
  44. /*
  45. * Load the compiled-in list of X.509 certificates.
  46. */
  47. static __init int load_system_certificate_list(void)
  48. {
  49. key_ref_t key;
  50. const u8 *p, *end;
  51. size_t plen;
  52. pr_notice("Loading compiled-in X.509 certificates\n");
  53. p = system_certificate_list;
  54. end = p + system_certificate_list_size;
  55. while (p < end) {
  56. /* Each cert begins with an ASN.1 SEQUENCE tag and must be more
  57. * than 256 bytes in size.
  58. */
  59. if (end - p < 4)
  60. goto dodgy_cert;
  61. if (p[0] != 0x30 &&
  62. p[1] != 0x82)
  63. goto dodgy_cert;
  64. plen = (p[2] << 8) | p[3];
  65. plen += 4;
  66. if (plen > end - p)
  67. goto dodgy_cert;
  68. key = key_create_or_update(make_key_ref(system_trusted_keyring, 1),
  69. "asymmetric",
  70. NULL,
  71. p,
  72. plen,
  73. ((KEY_POS_ALL & ~KEY_POS_SETATTR) |
  74. KEY_USR_VIEW | KEY_USR_READ),
  75. KEY_ALLOC_NOT_IN_QUOTA |
  76. KEY_ALLOC_TRUSTED);
  77. if (IS_ERR(key)) {
  78. pr_err("Problem loading in-kernel X.509 certificate (%ld)\n",
  79. PTR_ERR(key));
  80. } else {
  81. set_bit(KEY_FLAG_BUILTIN, &key_ref_to_ptr(key)->flags);
  82. pr_notice("Loaded X.509 cert '%s'\n",
  83. key_ref_to_ptr(key)->description);
  84. key_ref_put(key);
  85. }
  86. p += plen;
  87. }
  88. return 0;
  89. dodgy_cert:
  90. pr_err("Problem parsing in-kernel X.509 certificate list\n");
  91. return 0;
  92. }
  93. late_initcall(load_system_certificate_list);