loadpin.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * Module and Firmware Pinning Security Module
  3. *
  4. * Copyright 2011-2016 Google Inc.
  5. *
  6. * Author: Kees Cook <keescook@chromium.org>
  7. *
  8. * This software is licensed under the terms of the GNU General Public
  9. * License version 2, as published by the Free Software Foundation, and
  10. * may be copied, distributed, and modified under those terms.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. */
  17. #define pr_fmt(fmt) "LoadPin: " fmt
  18. #include <linux/module.h>
  19. #include <linux/fs.h>
  20. #include <linux/lsm_hooks.h>
  21. #include <linux/mount.h>
  22. #include <linux/path.h>
  23. #include <linux/sched.h> /* current */
  24. #include <linux/string_helpers.h>
  25. static void report_load(const char *origin, struct file *file, char *operation)
  26. {
  27. char *cmdline, *pathname;
  28. pathname = kstrdup_quotable_file(file, GFP_KERNEL);
  29. cmdline = kstrdup_quotable_cmdline(current, GFP_KERNEL);
  30. pr_notice("%s %s obj=%s%s%s pid=%d cmdline=%s%s%s\n",
  31. origin, operation,
  32. (pathname && pathname[0] != '<') ? "\"" : "",
  33. pathname,
  34. (pathname && pathname[0] != '<') ? "\"" : "",
  35. task_pid_nr(current),
  36. cmdline ? "\"" : "", cmdline, cmdline ? "\"" : "");
  37. kfree(cmdline);
  38. kfree(pathname);
  39. }
  40. static int enabled = IS_ENABLED(CONFIG_SECURITY_LOADPIN_ENABLED);
  41. static struct super_block *pinned_root;
  42. static DEFINE_SPINLOCK(pinned_root_spinlock);
  43. #ifdef CONFIG_SYSCTL
  44. static int zero;
  45. static int one = 1;
  46. static struct ctl_path loadpin_sysctl_path[] = {
  47. { .procname = "kernel", },
  48. { .procname = "loadpin", },
  49. { }
  50. };
  51. static struct ctl_table loadpin_sysctl_table[] = {
  52. {
  53. .procname = "enabled",
  54. .data = &enabled,
  55. .maxlen = sizeof(int),
  56. .mode = 0644,
  57. .proc_handler = proc_dointvec_minmax,
  58. .extra1 = &zero,
  59. .extra2 = &one,
  60. },
  61. { }
  62. };
  63. /*
  64. * This must be called after early kernel init, since then the rootdev
  65. * is available.
  66. */
  67. static void check_pinning_enforcement(struct super_block *mnt_sb)
  68. {
  69. bool ro = false;
  70. /*
  71. * If load pinning is not enforced via a read-only block
  72. * device, allow sysctl to change modes for testing.
  73. */
  74. if (mnt_sb->s_bdev) {
  75. ro = bdev_read_only(mnt_sb->s_bdev);
  76. pr_info("dev(%u,%u): %s\n",
  77. MAJOR(mnt_sb->s_bdev->bd_dev),
  78. MINOR(mnt_sb->s_bdev->bd_dev),
  79. ro ? "read-only" : "writable");
  80. } else
  81. pr_info("mnt_sb lacks block device, treating as: writable\n");
  82. if (!ro) {
  83. if (!register_sysctl_paths(loadpin_sysctl_path,
  84. loadpin_sysctl_table))
  85. pr_notice("sysctl registration failed!\n");
  86. else
  87. pr_info("load pinning can be disabled.\n");
  88. } else
  89. pr_info("load pinning engaged.\n");
  90. }
  91. #else
  92. static void check_pinning_enforcement(struct super_block *mnt_sb)
  93. {
  94. pr_info("load pinning engaged.\n");
  95. }
  96. #endif
  97. static void loadpin_sb_free_security(struct super_block *mnt_sb)
  98. {
  99. /*
  100. * When unmounting the filesystem we were using for load
  101. * pinning, we acknowledge the superblock release, but make sure
  102. * no other modules or firmware can be loaded.
  103. */
  104. if (!IS_ERR_OR_NULL(pinned_root) && mnt_sb == pinned_root) {
  105. pinned_root = ERR_PTR(-EIO);
  106. pr_info("umount pinned fs: refusing further loads\n");
  107. }
  108. }
  109. static int loadpin_read_file(struct file *file, enum kernel_read_file_id id)
  110. {
  111. struct super_block *load_root;
  112. const char *origin = kernel_read_file_id_str(id);
  113. /* This handles the older init_module API that has a NULL file. */
  114. if (!file) {
  115. if (!enabled) {
  116. report_load(origin, NULL, "old-api-pinning-ignored");
  117. return 0;
  118. }
  119. report_load(origin, NULL, "old-api-denied");
  120. return -EPERM;
  121. }
  122. load_root = file->f_path.mnt->mnt_sb;
  123. /* First loaded module/firmware defines the root for all others. */
  124. spin_lock(&pinned_root_spinlock);
  125. /*
  126. * pinned_root is only NULL at startup. Otherwise, it is either
  127. * a valid reference, or an ERR_PTR.
  128. */
  129. if (!pinned_root) {
  130. pinned_root = load_root;
  131. /*
  132. * Unlock now since it's only pinned_root we care about.
  133. * In the worst case, we will (correctly) report pinning
  134. * failures before we have announced that pinning is
  135. * enabled. This would be purely cosmetic.
  136. */
  137. spin_unlock(&pinned_root_spinlock);
  138. check_pinning_enforcement(pinned_root);
  139. report_load(origin, file, "pinned");
  140. } else {
  141. spin_unlock(&pinned_root_spinlock);
  142. }
  143. if (IS_ERR_OR_NULL(pinned_root) || load_root != pinned_root) {
  144. if (unlikely(!enabled)) {
  145. report_load(origin, file, "pinning-ignored");
  146. return 0;
  147. }
  148. report_load(origin, file, "denied");
  149. return -EPERM;
  150. }
  151. return 0;
  152. }
  153. static int loadpin_load_data(enum kernel_load_data_id id)
  154. {
  155. return loadpin_read_file(NULL, (enum kernel_read_file_id) id);
  156. }
  157. static struct security_hook_list loadpin_hooks[] __lsm_ro_after_init = {
  158. LSM_HOOK_INIT(sb_free_security, loadpin_sb_free_security),
  159. LSM_HOOK_INIT(kernel_read_file, loadpin_read_file),
  160. LSM_HOOK_INIT(kernel_load_data, loadpin_load_data),
  161. };
  162. void __init loadpin_add_hooks(void)
  163. {
  164. pr_info("ready to pin (currently %sabled)", enabled ? "en" : "dis");
  165. security_add_hooks(loadpin_hooks, ARRAY_SIZE(loadpin_hooks), "loadpin");
  166. }
  167. /* Should not be mutable after boot, so not listed in sysfs (perm == 0). */
  168. module_param(enabled, int, 0);
  169. MODULE_PARM_DESC(enabled, "Pin module/firmware loading (default: true)");