loadpin.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 enforce = IS_ENABLED(CONFIG_SECURITY_LOADPIN_ENFORCE);
  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 = "enforce",
  54. .data = &enforce,
  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. char bdev[BDEVNAME_SIZE];
  76. ro = bdev_read_only(mnt_sb->s_bdev);
  77. bdevname(mnt_sb->s_bdev, bdev);
  78. pr_info("%s (%u:%u): %s\n", bdev,
  79. MAJOR(mnt_sb->s_bdev->bd_dev),
  80. MINOR(mnt_sb->s_bdev->bd_dev),
  81. ro ? "read-only" : "writable");
  82. } else
  83. pr_info("mnt_sb lacks block device, treating as: writable\n");
  84. if (!ro) {
  85. if (!register_sysctl_paths(loadpin_sysctl_path,
  86. loadpin_sysctl_table))
  87. pr_notice("sysctl registration failed!\n");
  88. else
  89. pr_info("enforcement can be disabled.\n");
  90. } else
  91. pr_info("load pinning engaged.\n");
  92. }
  93. #else
  94. static void check_pinning_enforcement(struct super_block *mnt_sb)
  95. {
  96. pr_info("load pinning engaged.\n");
  97. }
  98. #endif
  99. static void loadpin_sb_free_security(struct super_block *mnt_sb)
  100. {
  101. /*
  102. * When unmounting the filesystem we were using for load
  103. * pinning, we acknowledge the superblock release, but make sure
  104. * no other modules or firmware can be loaded.
  105. */
  106. if (!IS_ERR_OR_NULL(pinned_root) && mnt_sb == pinned_root) {
  107. pinned_root = ERR_PTR(-EIO);
  108. pr_info("umount pinned fs: refusing further loads\n");
  109. }
  110. }
  111. static int loadpin_read_file(struct file *file, enum kernel_read_file_id id)
  112. {
  113. struct super_block *load_root;
  114. const char *origin = kernel_read_file_id_str(id);
  115. /* This handles the older init_module API that has a NULL file. */
  116. if (!file) {
  117. if (!enforce) {
  118. report_load(origin, NULL, "old-api-pinning-ignored");
  119. return 0;
  120. }
  121. report_load(origin, NULL, "old-api-denied");
  122. return -EPERM;
  123. }
  124. load_root = file->f_path.mnt->mnt_sb;
  125. /* First loaded module/firmware defines the root for all others. */
  126. spin_lock(&pinned_root_spinlock);
  127. /*
  128. * pinned_root is only NULL at startup. Otherwise, it is either
  129. * a valid reference, or an ERR_PTR.
  130. */
  131. if (!pinned_root) {
  132. pinned_root = load_root;
  133. /*
  134. * Unlock now since it's only pinned_root we care about.
  135. * In the worst case, we will (correctly) report pinning
  136. * failures before we have announced that pinning is
  137. * enforcing. This would be purely cosmetic.
  138. */
  139. spin_unlock(&pinned_root_spinlock);
  140. check_pinning_enforcement(pinned_root);
  141. report_load(origin, file, "pinned");
  142. } else {
  143. spin_unlock(&pinned_root_spinlock);
  144. }
  145. if (IS_ERR_OR_NULL(pinned_root) || load_root != pinned_root) {
  146. if (unlikely(!enforce)) {
  147. report_load(origin, file, "pinning-ignored");
  148. return 0;
  149. }
  150. report_load(origin, file, "denied");
  151. return -EPERM;
  152. }
  153. return 0;
  154. }
  155. static int loadpin_load_data(enum kernel_load_data_id id)
  156. {
  157. return loadpin_read_file(NULL, (enum kernel_read_file_id) id);
  158. }
  159. static struct security_hook_list loadpin_hooks[] __lsm_ro_after_init = {
  160. LSM_HOOK_INIT(sb_free_security, loadpin_sb_free_security),
  161. LSM_HOOK_INIT(kernel_read_file, loadpin_read_file),
  162. LSM_HOOK_INIT(kernel_load_data, loadpin_load_data),
  163. };
  164. void __init loadpin_add_hooks(void)
  165. {
  166. pr_info("ready to pin (currently %senforcing)\n",
  167. enforce ? "" : "not ");
  168. security_add_hooks(loadpin_hooks, ARRAY_SIZE(loadpin_hooks), "loadpin");
  169. }
  170. /* Should not be mutable after boot, so not listed in sysfs (perm == 0). */
  171. module_param(enforce, int, 0);
  172. MODULE_PARM_DESC(enforce, "Enforce module/firmware pinning");