loadpin.c 5.1 KB

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