loadpin.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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. #include <linux/dm-verity-loadpin.h>
  20. #include <uapi/linux/loadpin.h>
  21. #include <uapi/linux/lsm.h>
  22. #define VERITY_DIGEST_FILE_HEADER "# LOADPIN_TRUSTED_VERITY_ROOT_DIGESTS"
  23. static void report_load(const char *origin, struct file *file, char *operation)
  24. {
  25. char *cmdline, *pathname;
  26. pathname = kstrdup_quotable_file(file, GFP_KERNEL);
  27. cmdline = kstrdup_quotable_cmdline(current, GFP_KERNEL);
  28. pr_notice("%s %s obj=%s%s%s pid=%d cmdline=%s%s%s\n",
  29. origin, operation,
  30. (pathname && pathname[0] != '<') ? "\"" : "",
  31. pathname,
  32. (pathname && pathname[0] != '<') ? "\"" : "",
  33. task_pid_nr(current),
  34. cmdline ? "\"" : "", cmdline, cmdline ? "\"" : "");
  35. kfree(cmdline);
  36. kfree(pathname);
  37. }
  38. static int enforce = IS_ENABLED(CONFIG_SECURITY_LOADPIN_ENFORCE);
  39. static char *exclude_read_files[READING_MAX_ID];
  40. static int ignore_read_file_id[READING_MAX_ID] __ro_after_init;
  41. static struct super_block *pinned_root;
  42. static DEFINE_SPINLOCK(pinned_root_spinlock);
  43. #ifdef CONFIG_SECURITY_LOADPIN_VERITY
  44. static bool deny_reading_verity_digests;
  45. #endif
  46. #ifdef CONFIG_SYSCTL
  47. static struct ctl_table loadpin_sysctl_table[] = {
  48. {
  49. .procname = "enforce",
  50. .data = &enforce,
  51. .maxlen = sizeof(int),
  52. .mode = 0644,
  53. .proc_handler = proc_dointvec_minmax,
  54. .extra1 = SYSCTL_ONE,
  55. .extra2 = SYSCTL_ONE,
  56. },
  57. };
  58. static void set_sysctl(bool is_writable)
  59. {
  60. /*
  61. * If load pinning is not enforced via a read-only block
  62. * device, allow sysctl to change modes for testing.
  63. */
  64. if (is_writable)
  65. loadpin_sysctl_table[0].extra1 = SYSCTL_ZERO;
  66. else
  67. loadpin_sysctl_table[0].extra1 = SYSCTL_ONE;
  68. }
  69. #else
  70. static inline void set_sysctl(bool is_writable) { }
  71. #endif
  72. static void report_writable(struct super_block *mnt_sb, bool writable)
  73. {
  74. if (mnt_sb->s_bdev) {
  75. pr_info("%pg (%u:%u): %s\n", mnt_sb->s_bdev,
  76. MAJOR(mnt_sb->s_bdev->bd_dev),
  77. MINOR(mnt_sb->s_bdev->bd_dev),
  78. writable ? "writable" : "read-only");
  79. } else
  80. pr_info("mnt_sb lacks block device, treating as: writable\n");
  81. if (!writable)
  82. pr_info("load pinning engaged.\n");
  83. }
  84. /*
  85. * This must be called after early kernel init, since then the rootdev
  86. * is available.
  87. */
  88. static bool sb_is_writable(struct super_block *mnt_sb)
  89. {
  90. bool writable = true;
  91. if (mnt_sb->s_bdev)
  92. writable = !bdev_read_only(mnt_sb->s_bdev);
  93. return writable;
  94. }
  95. static void loadpin_sb_free_security(struct super_block *mnt_sb)
  96. {
  97. /*
  98. * When unmounting the filesystem we were using for load
  99. * pinning, we acknowledge the superblock release, but make sure
  100. * no other modules or firmware can be loaded when we are in
  101. * enforcing mode. Otherwise, allow the root to be reestablished.
  102. */
  103. if (!IS_ERR_OR_NULL(pinned_root) && mnt_sb == pinned_root) {
  104. if (enforce) {
  105. pinned_root = ERR_PTR(-EIO);
  106. pr_info("umount pinned fs: refusing further loads\n");
  107. } else {
  108. pinned_root = NULL;
  109. }
  110. }
  111. }
  112. static int loadpin_check(struct file *file, enum kernel_read_file_id id)
  113. {
  114. struct super_block *load_root;
  115. const char *origin = kernel_read_file_id_str(id);
  116. bool first_root_pin = false;
  117. bool load_root_writable;
  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. load_root_writable = sb_is_writable(load_root);
  135. /* First loaded module/firmware defines the root for all others. */
  136. spin_lock(&pinned_root_spinlock);
  137. /*
  138. * pinned_root is only NULL at startup or when the pinned root has
  139. * been unmounted while we are not in enforcing mode. Otherwise, it
  140. * is either a valid reference, or an ERR_PTR.
  141. */
  142. if (!pinned_root) {
  143. pinned_root = load_root;
  144. first_root_pin = true;
  145. }
  146. spin_unlock(&pinned_root_spinlock);
  147. if (first_root_pin) {
  148. report_writable(pinned_root, load_root_writable);
  149. set_sysctl(load_root_writable);
  150. report_load(origin, file, "pinned");
  151. }
  152. if (IS_ERR_OR_NULL(pinned_root) ||
  153. ((load_root != pinned_root) && !dm_verity_loadpin_is_bdev_trusted(load_root->s_bdev))) {
  154. if (unlikely(!enforce)) {
  155. report_load(origin, file, "pinning-ignored");
  156. return 0;
  157. }
  158. report_load(origin, file, "denied");
  159. return -EPERM;
  160. }
  161. return 0;
  162. }
  163. static int loadpin_read_file(struct file *file, enum kernel_read_file_id id,
  164. bool contents)
  165. {
  166. /*
  167. * LoadPin only cares about the _origin_ of a file, not its
  168. * contents, so we can ignore the "are full contents available"
  169. * argument here.
  170. */
  171. return loadpin_check(file, id);
  172. }
  173. static int loadpin_load_data(enum kernel_load_data_id id, bool contents)
  174. {
  175. /*
  176. * LoadPin only cares about the _origin_ of a file, not its
  177. * contents, so a NULL file is passed, and we can ignore the
  178. * state of "contents".
  179. */
  180. return loadpin_check(NULL, (enum kernel_read_file_id) id);
  181. }
  182. static const struct lsm_id loadpin_lsmid = {
  183. .name = "loadpin",
  184. .id = LSM_ID_LOADPIN,
  185. };
  186. static struct security_hook_list loadpin_hooks[] __ro_after_init = {
  187. LSM_HOOK_INIT(sb_free_security, loadpin_sb_free_security),
  188. LSM_HOOK_INIT(kernel_read_file, loadpin_read_file),
  189. LSM_HOOK_INIT(kernel_load_data, loadpin_load_data),
  190. };
  191. static void __init parse_exclude(void)
  192. {
  193. int i, j;
  194. char *cur;
  195. /*
  196. * Make sure all the arrays stay within expected sizes. This
  197. * is slightly weird because kernel_read_file_str[] includes
  198. * READING_MAX_ID, which isn't actually meaningful here.
  199. */
  200. BUILD_BUG_ON(ARRAY_SIZE(exclude_read_files) !=
  201. ARRAY_SIZE(ignore_read_file_id));
  202. BUILD_BUG_ON(ARRAY_SIZE(kernel_read_file_str) <
  203. ARRAY_SIZE(ignore_read_file_id));
  204. for (i = 0; i < ARRAY_SIZE(exclude_read_files); i++) {
  205. cur = exclude_read_files[i];
  206. if (!cur)
  207. break;
  208. if (*cur == '\0')
  209. continue;
  210. for (j = 0; j < ARRAY_SIZE(ignore_read_file_id); j++) {
  211. if (strcmp(cur, kernel_read_file_str[j]) == 0) {
  212. pr_info("excluding: %s\n",
  213. kernel_read_file_str[j]);
  214. ignore_read_file_id[j] = 1;
  215. /*
  216. * Can not break, because one read_file_str
  217. * may map to more than on read_file_id.
  218. */
  219. }
  220. }
  221. }
  222. }
  223. static int __init loadpin_init(void)
  224. {
  225. pr_info("ready to pin (currently %senforcing)\n",
  226. enforce ? "" : "not ");
  227. parse_exclude();
  228. #ifdef CONFIG_SYSCTL
  229. if (!register_sysctl("kernel/loadpin", loadpin_sysctl_table))
  230. pr_notice("sysctl registration failed!\n");
  231. #endif
  232. security_add_hooks(loadpin_hooks, ARRAY_SIZE(loadpin_hooks),
  233. &loadpin_lsmid);
  234. return 0;
  235. }
  236. DEFINE_LSM(loadpin) = {
  237. .name = "loadpin",
  238. .init = loadpin_init,
  239. };
  240. #ifdef CONFIG_SECURITY_LOADPIN_VERITY
  241. enum loadpin_securityfs_interface_index {
  242. LOADPIN_DM_VERITY,
  243. };
  244. static int read_trusted_verity_root_digests(unsigned int fd)
  245. {
  246. struct fd f;
  247. void *data;
  248. int rc;
  249. char *p, *d;
  250. if (deny_reading_verity_digests)
  251. return -EPERM;
  252. /* The list of trusted root digests can only be set up once */
  253. if (!list_empty(&dm_verity_loadpin_trusted_root_digests))
  254. return -EPERM;
  255. f = fdget(fd);
  256. if (!fd_file(f))
  257. return -EINVAL;
  258. data = kzalloc(SZ_4K, GFP_KERNEL);
  259. if (!data) {
  260. rc = -ENOMEM;
  261. goto err;
  262. }
  263. rc = kernel_read_file(fd_file(f), 0, (void **)&data, SZ_4K - 1, NULL, READING_POLICY);
  264. if (rc < 0)
  265. goto err;
  266. p = data;
  267. p[rc] = '\0';
  268. p = strim(p);
  269. p = strim(data);
  270. while ((d = strsep(&p, "\n")) != NULL) {
  271. int len;
  272. struct dm_verity_loadpin_trusted_root_digest *trd;
  273. if (d == data) {
  274. /* first line, validate header */
  275. if (strcmp(d, VERITY_DIGEST_FILE_HEADER)) {
  276. rc = -EPROTO;
  277. goto err;
  278. }
  279. continue;
  280. }
  281. len = strlen(d);
  282. if (len % 2) {
  283. rc = -EPROTO;
  284. goto err;
  285. }
  286. len /= 2;
  287. trd = kzalloc(struct_size(trd, data, len), GFP_KERNEL);
  288. if (!trd) {
  289. rc = -ENOMEM;
  290. goto err;
  291. }
  292. trd->len = len;
  293. if (hex2bin(trd->data, d, len)) {
  294. kfree(trd);
  295. rc = -EPROTO;
  296. goto err;
  297. }
  298. list_add_tail(&trd->node, &dm_verity_loadpin_trusted_root_digests);
  299. }
  300. if (list_empty(&dm_verity_loadpin_trusted_root_digests)) {
  301. rc = -EPROTO;
  302. goto err;
  303. }
  304. kfree(data);
  305. fdput(f);
  306. return 0;
  307. err:
  308. kfree(data);
  309. /* any failure in loading/parsing invalidates the entire list */
  310. {
  311. struct dm_verity_loadpin_trusted_root_digest *trd, *tmp;
  312. list_for_each_entry_safe(trd, tmp, &dm_verity_loadpin_trusted_root_digests, node) {
  313. list_del(&trd->node);
  314. kfree(trd);
  315. }
  316. }
  317. /* disallow further attempts after reading a corrupt/invalid file */
  318. deny_reading_verity_digests = true;
  319. fdput(f);
  320. return rc;
  321. }
  322. /******************************** securityfs ********************************/
  323. static long dm_verity_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  324. {
  325. void __user *uarg = (void __user *)arg;
  326. unsigned int fd;
  327. switch (cmd) {
  328. case LOADPIN_IOC_SET_TRUSTED_VERITY_DIGESTS:
  329. if (copy_from_user(&fd, uarg, sizeof(fd)))
  330. return -EFAULT;
  331. return read_trusted_verity_root_digests(fd);
  332. default:
  333. return -EINVAL;
  334. }
  335. }
  336. static const struct file_operations loadpin_dm_verity_ops = {
  337. .unlocked_ioctl = dm_verity_ioctl,
  338. .compat_ioctl = compat_ptr_ioctl,
  339. };
  340. /**
  341. * init_loadpin_securityfs - create the securityfs directory for LoadPin
  342. *
  343. * We can not put this method normally under the loadpin_init() code path since
  344. * the security subsystem gets initialized before the vfs caches.
  345. *
  346. * Returns 0 if the securityfs directory creation was successful.
  347. */
  348. static int __init init_loadpin_securityfs(void)
  349. {
  350. struct dentry *loadpin_dir, *dentry;
  351. loadpin_dir = securityfs_create_dir("loadpin", NULL);
  352. if (IS_ERR(loadpin_dir)) {
  353. pr_err("LoadPin: could not create securityfs dir: %ld\n",
  354. PTR_ERR(loadpin_dir));
  355. return PTR_ERR(loadpin_dir);
  356. }
  357. dentry = securityfs_create_file("dm-verity", 0600, loadpin_dir,
  358. (void *)LOADPIN_DM_VERITY, &loadpin_dm_verity_ops);
  359. if (IS_ERR(dentry)) {
  360. pr_err("LoadPin: could not create securityfs entry 'dm-verity': %ld\n",
  361. PTR_ERR(dentry));
  362. return PTR_ERR(dentry);
  363. }
  364. return 0;
  365. }
  366. fs_initcall(init_loadpin_securityfs);
  367. #endif /* CONFIG_SECURITY_LOADPIN_VERITY */
  368. /* Should not be mutable after boot, so not listed in sysfs (perm == 0). */
  369. module_param(enforce, int, 0);
  370. MODULE_PARM_DESC(enforce, "Enforce module/firmware pinning");
  371. module_param_array_named(exclude, exclude_read_files, charp, NULL, 0);
  372. MODULE_PARM_DESC(exclude, "Exclude pinning specific read file types");