loadpin.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Module and Firmware Pinning Security Module
  4. *
  5. * Copyright 2011-2016 Google Inc.
  6. *
  7. * Author: Kees Cook <keescook@chromium.org>
  8. */
  9. #define pr_fmt(fmt) "LoadPin: " fmt
  10. #include <linux/module.h>
  11. #include <linux/fs.h>
  12. #include <linux/kernel_read_file.h>
  13. #include <linux/lsm_hooks.h>
  14. #include <linux/mount.h>
  15. #include <linux/blkdev.h>
  16. #include <linux/path.h>
  17. #include <linux/sched.h> /* current */
  18. #include <linux/string_helpers.h>
  19. static void report_load(const char *origin, struct file *file, char *operation)
  20. {
  21. char *cmdline, *pathname;
  22. pathname = kstrdup_quotable_file(file, GFP_KERNEL);
  23. cmdline = kstrdup_quotable_cmdline(current, GFP_KERNEL);
  24. pr_notice("%s %s obj=%s%s%s pid=%d cmdline=%s%s%s\n",
  25. origin, operation,
  26. (pathname && pathname[0] != '<') ? "\"" : "",
  27. pathname,
  28. (pathname && pathname[0] != '<') ? "\"" : "",
  29. task_pid_nr(current),
  30. cmdline ? "\"" : "", cmdline, cmdline ? "\"" : "");
  31. kfree(cmdline);
  32. kfree(pathname);
  33. }
  34. static int enforce = IS_ENABLED(CONFIG_SECURITY_LOADPIN_ENFORCE);
  35. static char *exclude_read_files[READING_MAX_ID];
  36. static int ignore_read_file_id[READING_MAX_ID] __ro_after_init;
  37. static struct super_block *pinned_root;
  38. static DEFINE_SPINLOCK(pinned_root_spinlock);
  39. #ifdef CONFIG_SYSCTL
  40. static struct ctl_path loadpin_sysctl_path[] = {
  41. { .procname = "kernel", },
  42. { .procname = "loadpin", },
  43. { }
  44. };
  45. static struct ctl_table loadpin_sysctl_table[] = {
  46. {
  47. .procname = "enforce",
  48. .data = &enforce,
  49. .maxlen = sizeof(int),
  50. .mode = 0644,
  51. .proc_handler = proc_dointvec_minmax,
  52. .extra1 = SYSCTL_ZERO,
  53. .extra2 = SYSCTL_ONE,
  54. },
  55. { }
  56. };
  57. /*
  58. * This must be called after early kernel init, since then the rootdev
  59. * is available.
  60. */
  61. static void check_pinning_enforcement(struct super_block *mnt_sb)
  62. {
  63. bool ro = false;
  64. /*
  65. * If load pinning is not enforced via a read-only block
  66. * device, allow sysctl to change modes for testing.
  67. */
  68. if (mnt_sb->s_bdev) {
  69. char bdev[BDEVNAME_SIZE];
  70. ro = bdev_read_only(mnt_sb->s_bdev);
  71. bdevname(mnt_sb->s_bdev, bdev);
  72. pr_info("%s (%u:%u): %s\n", bdev,
  73. MAJOR(mnt_sb->s_bdev->bd_dev),
  74. MINOR(mnt_sb->s_bdev->bd_dev),
  75. ro ? "read-only" : "writable");
  76. } else
  77. pr_info("mnt_sb lacks block device, treating as: writable\n");
  78. if (!ro) {
  79. if (!register_sysctl_paths(loadpin_sysctl_path,
  80. loadpin_sysctl_table))
  81. pr_notice("sysctl registration failed!\n");
  82. else
  83. pr_info("enforcement can be disabled.\n");
  84. } else
  85. pr_info("load pinning engaged.\n");
  86. }
  87. #else
  88. static void check_pinning_enforcement(struct super_block *mnt_sb)
  89. {
  90. pr_info("load pinning engaged.\n");
  91. }
  92. #endif
  93. static void loadpin_sb_free_security(struct super_block *mnt_sb)
  94. {
  95. /*
  96. * When unmounting the filesystem we were using for load
  97. * pinning, we acknowledge the superblock release, but make sure
  98. * no other modules or firmware can be loaded.
  99. */
  100. if (!IS_ERR_OR_NULL(pinned_root) && mnt_sb == pinned_root) {
  101. pinned_root = ERR_PTR(-EIO);
  102. pr_info("umount pinned fs: refusing further loads\n");
  103. }
  104. }
  105. static int loadpin_read_file(struct file *file, enum kernel_read_file_id id,
  106. bool contents)
  107. {
  108. struct super_block *load_root;
  109. const char *origin = kernel_read_file_id_str(id);
  110. /*
  111. * If we will not know that we'll be seeing the full contents
  112. * then we cannot trust a load will be complete and unchanged
  113. * off disk. Treat all contents=false hooks as if there were
  114. * no associated file struct.
  115. */
  116. if (!contents)
  117. file = NULL;
  118. /* If the file id is excluded, ignore the pinning. */
  119. if ((unsigned int)id < ARRAY_SIZE(ignore_read_file_id) &&
  120. ignore_read_file_id[id]) {
  121. report_load(origin, file, "pinning-excluded");
  122. return 0;
  123. }
  124. /* This handles the older init_module API that has a NULL file. */
  125. if (!file) {
  126. if (!enforce) {
  127. report_load(origin, NULL, "old-api-pinning-ignored");
  128. return 0;
  129. }
  130. report_load(origin, NULL, "old-api-denied");
  131. return -EPERM;
  132. }
  133. load_root = file->f_path.mnt->mnt_sb;
  134. /* First loaded module/firmware defines the root for all others. */
  135. spin_lock(&pinned_root_spinlock);
  136. /*
  137. * pinned_root is only NULL at startup. Otherwise, it is either
  138. * a valid reference, or an ERR_PTR.
  139. */
  140. if (!pinned_root) {
  141. pinned_root = load_root;
  142. /*
  143. * Unlock now since it's only pinned_root we care about.
  144. * In the worst case, we will (correctly) report pinning
  145. * failures before we have announced that pinning is
  146. * enforcing. This would be purely cosmetic.
  147. */
  148. spin_unlock(&pinned_root_spinlock);
  149. check_pinning_enforcement(pinned_root);
  150. report_load(origin, file, "pinned");
  151. } else {
  152. spin_unlock(&pinned_root_spinlock);
  153. }
  154. if (IS_ERR_OR_NULL(pinned_root) || load_root != pinned_root) {
  155. if (unlikely(!enforce)) {
  156. report_load(origin, file, "pinning-ignored");
  157. return 0;
  158. }
  159. report_load(origin, file, "denied");
  160. return -EPERM;
  161. }
  162. return 0;
  163. }
  164. static int loadpin_load_data(enum kernel_load_data_id id, bool contents)
  165. {
  166. return loadpin_read_file(NULL, (enum kernel_read_file_id) id, contents);
  167. }
  168. static struct security_hook_list loadpin_hooks[] __lsm_ro_after_init = {
  169. LSM_HOOK_INIT(sb_free_security, loadpin_sb_free_security),
  170. LSM_HOOK_INIT(kernel_read_file, loadpin_read_file),
  171. LSM_HOOK_INIT(kernel_load_data, loadpin_load_data),
  172. };
  173. static void __init parse_exclude(void)
  174. {
  175. int i, j;
  176. char *cur;
  177. /*
  178. * Make sure all the arrays stay within expected sizes. This
  179. * is slightly weird because kernel_read_file_str[] includes
  180. * READING_MAX_ID, which isn't actually meaningful here.
  181. */
  182. BUILD_BUG_ON(ARRAY_SIZE(exclude_read_files) !=
  183. ARRAY_SIZE(ignore_read_file_id));
  184. BUILD_BUG_ON(ARRAY_SIZE(kernel_read_file_str) <
  185. ARRAY_SIZE(ignore_read_file_id));
  186. for (i = 0; i < ARRAY_SIZE(exclude_read_files); i++) {
  187. cur = exclude_read_files[i];
  188. if (!cur)
  189. break;
  190. if (*cur == '\0')
  191. continue;
  192. for (j = 0; j < ARRAY_SIZE(ignore_read_file_id); j++) {
  193. if (strcmp(cur, kernel_read_file_str[j]) == 0) {
  194. pr_info("excluding: %s\n",
  195. kernel_read_file_str[j]);
  196. ignore_read_file_id[j] = 1;
  197. /*
  198. * Can not break, because one read_file_str
  199. * may map to more than on read_file_id.
  200. */
  201. }
  202. }
  203. }
  204. }
  205. static int __init loadpin_init(void)
  206. {
  207. pr_info("ready to pin (currently %senforcing)\n",
  208. enforce ? "" : "not ");
  209. parse_exclude();
  210. security_add_hooks(loadpin_hooks, ARRAY_SIZE(loadpin_hooks), "loadpin");
  211. return 0;
  212. }
  213. DEFINE_LSM(loadpin) = {
  214. .name = "loadpin",
  215. .init = loadpin_init,
  216. };
  217. /* Should not be mutable after boot, so not listed in sysfs (perm == 0). */
  218. module_param(enforce, int, 0);
  219. MODULE_PARM_DESC(enforce, "Enforce module/firmware pinning");
  220. module_param_array_named(exclude, exclude_read_files, charp, NULL, 0);
  221. MODULE_PARM_DESC(exclude, "Exclude pinning specific read file types");