loadpin.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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 <[email protected]>
  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. #include <linux/dm-verity-loadpin.h>
  20. #include <uapi/linux/loadpin.h>
  21. #define VERITY_DIGEST_FILE_HEADER "# LOADPIN_TRUSTED_VERITY_ROOT_DIGESTS"
  22. static void report_load(const char *origin, struct file *file, char *operation)
  23. {
  24. char *cmdline, *pathname;
  25. pathname = kstrdup_quotable_file(file, GFP_KERNEL);
  26. cmdline = kstrdup_quotable_cmdline(current, GFP_KERNEL);
  27. pr_notice("%s %s obj=%s%s%s pid=%d cmdline=%s%s%s\n",
  28. origin, operation,
  29. (pathname && pathname[0] != '<') ? "\"" : "",
  30. pathname,
  31. (pathname && pathname[0] != '<') ? "\"" : "",
  32. task_pid_nr(current),
  33. cmdline ? "\"" : "", cmdline, cmdline ? "\"" : "");
  34. kfree(cmdline);
  35. kfree(pathname);
  36. }
  37. static int enforce = IS_ENABLED(CONFIG_SECURITY_LOADPIN_ENFORCE);
  38. static char *exclude_read_files[READING_MAX_ID];
  39. static int ignore_read_file_id[READING_MAX_ID] __ro_after_init;
  40. static struct super_block *pinned_root;
  41. static DEFINE_SPINLOCK(pinned_root_spinlock);
  42. #ifdef CONFIG_SECURITY_LOADPIN_VERITY
  43. static bool deny_reading_verity_digests;
  44. #endif
  45. #ifdef CONFIG_SYSCTL
  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 = SYSCTL_ZERO,
  59. .extra2 = SYSCTL_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("%pg (%u:%u): %s\n", mnt_sb->s_bdev,
  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("enforcement 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_check(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. /* If the file id is excluded, ignore the pinning. */
  114. if ((unsigned int)id < ARRAY_SIZE(ignore_read_file_id) &&
  115. ignore_read_file_id[id]) {
  116. report_load(origin, file, "pinning-excluded");
  117. return 0;
  118. }
  119. /* This handles the older init_module API that has a NULL file. */
  120. if (!file) {
  121. if (!enforce) {
  122. report_load(origin, NULL, "old-api-pinning-ignored");
  123. return 0;
  124. }
  125. report_load(origin, NULL, "old-api-denied");
  126. return -EPERM;
  127. }
  128. load_root = file->f_path.mnt->mnt_sb;
  129. /* First loaded module/firmware defines the root for all others. */
  130. spin_lock(&pinned_root_spinlock);
  131. /*
  132. * pinned_root is only NULL at startup. Otherwise, it is either
  133. * a valid reference, or an ERR_PTR.
  134. */
  135. if (!pinned_root) {
  136. pinned_root = load_root;
  137. /*
  138. * Unlock now since it's only pinned_root we care about.
  139. * In the worst case, we will (correctly) report pinning
  140. * failures before we have announced that pinning is
  141. * enforcing. This would be purely cosmetic.
  142. */
  143. spin_unlock(&pinned_root_spinlock);
  144. check_pinning_enforcement(pinned_root);
  145. report_load(origin, file, "pinned");
  146. } else {
  147. spin_unlock(&pinned_root_spinlock);
  148. }
  149. if (IS_ERR_OR_NULL(pinned_root) ||
  150. ((load_root != pinned_root) && !dm_verity_loadpin_is_bdev_trusted(load_root->s_bdev))) {
  151. if (unlikely(!enforce)) {
  152. report_load(origin, file, "pinning-ignored");
  153. return 0;
  154. }
  155. report_load(origin, file, "denied");
  156. return -EPERM;
  157. }
  158. return 0;
  159. }
  160. static int loadpin_read_file(struct file *file, enum kernel_read_file_id id,
  161. bool contents)
  162. {
  163. /*
  164. * LoadPin only cares about the _origin_ of a file, not its
  165. * contents, so we can ignore the "are full contents available"
  166. * argument here.
  167. */
  168. return loadpin_check(file, id);
  169. }
  170. static int loadpin_load_data(enum kernel_load_data_id id, bool contents)
  171. {
  172. /*
  173. * LoadPin only cares about the _origin_ of a file, not its
  174. * contents, so a NULL file is passed, and we can ignore the
  175. * state of "contents".
  176. */
  177. return loadpin_check(NULL, (enum kernel_read_file_id) id);
  178. }
  179. static struct security_hook_list loadpin_hooks[] __lsm_ro_after_init = {
  180. LSM_HOOK_INIT(sb_free_security, loadpin_sb_free_security),
  181. LSM_HOOK_INIT(kernel_read_file, loadpin_read_file),
  182. LSM_HOOK_INIT(kernel_load_data, loadpin_load_data),
  183. };
  184. static void __init parse_exclude(void)
  185. {
  186. int i, j;
  187. char *cur;
  188. /*
  189. * Make sure all the arrays stay within expected sizes. This
  190. * is slightly weird because kernel_read_file_str[] includes
  191. * READING_MAX_ID, which isn't actually meaningful here.
  192. */
  193. BUILD_BUG_ON(ARRAY_SIZE(exclude_read_files) !=
  194. ARRAY_SIZE(ignore_read_file_id));
  195. BUILD_BUG_ON(ARRAY_SIZE(kernel_read_file_str) <
  196. ARRAY_SIZE(ignore_read_file_id));
  197. for (i = 0; i < ARRAY_SIZE(exclude_read_files); i++) {
  198. cur = exclude_read_files[i];
  199. if (!cur)
  200. break;
  201. if (*cur == '\0')
  202. continue;
  203. for (j = 0; j < ARRAY_SIZE(ignore_read_file_id); j++) {
  204. if (strcmp(cur, kernel_read_file_str[j]) == 0) {
  205. pr_info("excluding: %s\n",
  206. kernel_read_file_str[j]);
  207. ignore_read_file_id[j] = 1;
  208. /*
  209. * Can not break, because one read_file_str
  210. * may map to more than on read_file_id.
  211. */
  212. }
  213. }
  214. }
  215. }
  216. static int __init loadpin_init(void)
  217. {
  218. pr_info("ready to pin (currently %senforcing)\n",
  219. enforce ? "" : "not ");
  220. parse_exclude();
  221. security_add_hooks(loadpin_hooks, ARRAY_SIZE(loadpin_hooks), "loadpin");
  222. return 0;
  223. }
  224. DEFINE_LSM(loadpin) = {
  225. .name = "loadpin",
  226. .init = loadpin_init,
  227. };
  228. #ifdef CONFIG_SECURITY_LOADPIN_VERITY
  229. enum loadpin_securityfs_interface_index {
  230. LOADPIN_DM_VERITY,
  231. };
  232. static int read_trusted_verity_root_digests(unsigned int fd)
  233. {
  234. struct fd f;
  235. void *data;
  236. int rc;
  237. char *p, *d;
  238. if (deny_reading_verity_digests)
  239. return -EPERM;
  240. /* The list of trusted root digests can only be set up once */
  241. if (!list_empty(&dm_verity_loadpin_trusted_root_digests))
  242. return -EPERM;
  243. f = fdget(fd);
  244. if (!f.file)
  245. return -EINVAL;
  246. data = kzalloc(SZ_4K, GFP_KERNEL);
  247. if (!data) {
  248. rc = -ENOMEM;
  249. goto err;
  250. }
  251. rc = kernel_read_file(f.file, 0, (void **)&data, SZ_4K - 1, NULL, READING_POLICY);
  252. if (rc < 0)
  253. goto err;
  254. p = data;
  255. p[rc] = '\0';
  256. p = strim(p);
  257. p = strim(data);
  258. while ((d = strsep(&p, "\n")) != NULL) {
  259. int len;
  260. struct dm_verity_loadpin_trusted_root_digest *trd;
  261. if (d == data) {
  262. /* first line, validate header */
  263. if (strcmp(d, VERITY_DIGEST_FILE_HEADER)) {
  264. rc = -EPROTO;
  265. goto err;
  266. }
  267. continue;
  268. }
  269. len = strlen(d);
  270. if (len % 2) {
  271. rc = -EPROTO;
  272. goto err;
  273. }
  274. len /= 2;
  275. trd = kzalloc(struct_size(trd, data, len), GFP_KERNEL);
  276. if (!trd) {
  277. rc = -ENOMEM;
  278. goto err;
  279. }
  280. if (hex2bin(trd->data, d, len)) {
  281. kfree(trd);
  282. rc = -EPROTO;
  283. goto err;
  284. }
  285. trd->len = len;
  286. list_add_tail(&trd->node, &dm_verity_loadpin_trusted_root_digests);
  287. }
  288. if (list_empty(&dm_verity_loadpin_trusted_root_digests)) {
  289. rc = -EPROTO;
  290. goto err;
  291. }
  292. kfree(data);
  293. fdput(f);
  294. return 0;
  295. err:
  296. kfree(data);
  297. /* any failure in loading/parsing invalidates the entire list */
  298. {
  299. struct dm_verity_loadpin_trusted_root_digest *trd, *tmp;
  300. list_for_each_entry_safe(trd, tmp, &dm_verity_loadpin_trusted_root_digests, node) {
  301. list_del(&trd->node);
  302. kfree(trd);
  303. }
  304. }
  305. /* disallow further attempts after reading a corrupt/invalid file */
  306. deny_reading_verity_digests = true;
  307. fdput(f);
  308. return rc;
  309. }
  310. /******************************** securityfs ********************************/
  311. static long dm_verity_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  312. {
  313. void __user *uarg = (void __user *)arg;
  314. unsigned int fd;
  315. switch (cmd) {
  316. case LOADPIN_IOC_SET_TRUSTED_VERITY_DIGESTS:
  317. if (copy_from_user(&fd, uarg, sizeof(fd)))
  318. return -EFAULT;
  319. return read_trusted_verity_root_digests(fd);
  320. default:
  321. return -EINVAL;
  322. }
  323. }
  324. static const struct file_operations loadpin_dm_verity_ops = {
  325. .unlocked_ioctl = dm_verity_ioctl,
  326. .compat_ioctl = compat_ptr_ioctl,
  327. };
  328. /**
  329. * init_loadpin_securityfs - create the securityfs directory for LoadPin
  330. *
  331. * We can not put this method normally under the loadpin_init() code path since
  332. * the security subsystem gets initialized before the vfs caches.
  333. *
  334. * Returns 0 if the securityfs directory creation was successful.
  335. */
  336. static int __init init_loadpin_securityfs(void)
  337. {
  338. struct dentry *loadpin_dir, *dentry;
  339. loadpin_dir = securityfs_create_dir("loadpin", NULL);
  340. if (IS_ERR(loadpin_dir)) {
  341. pr_err("LoadPin: could not create securityfs dir: %ld\n",
  342. PTR_ERR(loadpin_dir));
  343. return PTR_ERR(loadpin_dir);
  344. }
  345. dentry = securityfs_create_file("dm-verity", 0600, loadpin_dir,
  346. (void *)LOADPIN_DM_VERITY, &loadpin_dm_verity_ops);
  347. if (IS_ERR(dentry)) {
  348. pr_err("LoadPin: could not create securityfs entry 'dm-verity': %ld\n",
  349. PTR_ERR(dentry));
  350. return PTR_ERR(dentry);
  351. }
  352. return 0;
  353. }
  354. fs_initcall(init_loadpin_securityfs);
  355. #endif /* CONFIG_SECURITY_LOADPIN_VERITY */
  356. /* Should not be mutable after boot, so not listed in sysfs (perm == 0). */
  357. module_param(enforce, int, 0);
  358. MODULE_PARM_DESC(enforce, "Enforce module/firmware pinning");
  359. module_param_array_named(exclude, exclude_read_files, charp, NULL, 0);
  360. MODULE_PARM_DESC(exclude, "Exclude pinning specific read file types");