xref: /openbmc/linux/security/loadpin/loadpin.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
19c92ab61SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
29b091556SKees Cook /*
39b091556SKees Cook  * Module and Firmware Pinning Security Module
49b091556SKees Cook  *
59b091556SKees Cook  * Copyright 2011-2016 Google Inc.
69b091556SKees Cook  *
79b091556SKees Cook  * Author: Kees Cook <keescook@chromium.org>
89b091556SKees Cook  */
99b091556SKees Cook 
109b091556SKees Cook #define pr_fmt(fmt) "LoadPin: " fmt
119b091556SKees Cook 
129b091556SKees Cook #include <linux/module.h>
139b091556SKees Cook #include <linux/fs.h>
14b89999d0SScott Branden #include <linux/kernel_read_file.h>
159b091556SKees Cook #include <linux/lsm_hooks.h>
169b091556SKees Cook #include <linux/mount.h>
173f1266f1SChristoph Hellwig #include <linux/blkdev.h>
189b091556SKees Cook #include <linux/path.h>
199b091556SKees Cook #include <linux/sched.h>	/* current */
209b091556SKees Cook #include <linux/string_helpers.h>
213f805f8cSMatthias Kaehlcke #include <linux/dm-verity-loadpin.h>
223f805f8cSMatthias Kaehlcke #include <uapi/linux/loadpin.h>
239b091556SKees Cook 
246e42aec7SMatthias Kaehlcke #define VERITY_DIGEST_FILE_HEADER "# LOADPIN_TRUSTED_VERITY_ROOT_DIGESTS"
256e42aec7SMatthias Kaehlcke 
report_load(const char * origin,struct file * file,char * operation)269b091556SKees Cook static void report_load(const char *origin, struct file *file, char *operation)
279b091556SKees Cook {
289b091556SKees Cook 	char *cmdline, *pathname;
299b091556SKees Cook 
309b091556SKees Cook 	pathname = kstrdup_quotable_file(file, GFP_KERNEL);
319b091556SKees Cook 	cmdline = kstrdup_quotable_cmdline(current, GFP_KERNEL);
329b091556SKees Cook 
339b091556SKees Cook 	pr_notice("%s %s obj=%s%s%s pid=%d cmdline=%s%s%s\n",
349b091556SKees Cook 		  origin, operation,
359b091556SKees Cook 		  (pathname && pathname[0] != '<') ? "\"" : "",
369b091556SKees Cook 		  pathname,
379b091556SKees Cook 		  (pathname && pathname[0] != '<') ? "\"" : "",
389b091556SKees Cook 		  task_pid_nr(current),
399b091556SKees Cook 		  cmdline ? "\"" : "", cmdline, cmdline ? "\"" : "");
409b091556SKees Cook 
419b091556SKees Cook 	kfree(cmdline);
429b091556SKees Cook 	kfree(pathname);
439b091556SKees Cook }
449b091556SKees Cook 
4513523befSKees Cook static int enforce = IS_ENABLED(CONFIG_SECURITY_LOADPIN_ENFORCE);
460ff98480SKe Wu static char *exclude_read_files[READING_MAX_ID];
470ff98480SKe Wu static int ignore_read_file_id[READING_MAX_ID] __ro_after_init;
489b091556SKees Cook static struct super_block *pinned_root;
499b091556SKees Cook static DEFINE_SPINLOCK(pinned_root_spinlock);
503f805f8cSMatthias Kaehlcke #ifdef CONFIG_SECURITY_LOADPIN_VERITY
513f805f8cSMatthias Kaehlcke static bool deny_reading_verity_digests;
523f805f8cSMatthias Kaehlcke #endif
539b091556SKees Cook 
549b091556SKees Cook #ifdef CONFIG_SYSCTL
559b091556SKees Cook static struct ctl_table loadpin_sysctl_table[] = {
569b091556SKees Cook 	{
5713523befSKees Cook 		.procname       = "enforce",
5813523befSKees Cook 		.data           = &enforce,
599b091556SKees Cook 		.maxlen         = sizeof(int),
609b091556SKees Cook 		.mode           = 0644,
619b091556SKees Cook 		.proc_handler   = proc_dointvec_minmax,
6260ba1028SKees Cook 		.extra1         = SYSCTL_ONE,
63eec4844fSMatteo Croce 		.extra2         = SYSCTL_ONE,
649b091556SKees Cook 	},
659b091556SKees Cook 	{ }
669b091556SKees Cook };
679b091556SKees Cook 
set_sysctl(bool is_writable)6860ba1028SKees Cook static void set_sysctl(bool is_writable)
699b091556SKees Cook {
709b091556SKees Cook 	/*
719b091556SKees Cook 	 * If load pinning is not enforced via a read-only block
729b091556SKees Cook 	 * device, allow sysctl to change modes for testing.
739b091556SKees Cook 	 */
7460ba1028SKees Cook 	if (is_writable)
7560ba1028SKees Cook 		loadpin_sysctl_table[0].extra1 = SYSCTL_ZERO;
7660ba1028SKees Cook 	else
7760ba1028SKees Cook 		loadpin_sysctl_table[0].extra1 = SYSCTL_ONE;
7860ba1028SKees Cook }
7960ba1028SKees Cook #else
set_sysctl(bool is_writable)8060ba1028SKees Cook static inline void set_sysctl(bool is_writable) { }
8160ba1028SKees Cook #endif
8260ba1028SKees Cook 
report_writable(struct super_block * mnt_sb,bool writable)8360ba1028SKees Cook static void report_writable(struct super_block *mnt_sb, bool writable)
8460ba1028SKees Cook {
859b091556SKees Cook 	if (mnt_sb->s_bdev) {
86ed5edd5aSChristoph Hellwig 		pr_info("%pg (%u:%u): %s\n", mnt_sb->s_bdev,
879b091556SKees Cook 			MAJOR(mnt_sb->s_bdev->bd_dev),
889b091556SKees Cook 			MINOR(mnt_sb->s_bdev->bd_dev),
89b76ded21SKees Cook 			writable ? "writable" : "read-only");
909b091556SKees Cook 	} else
919b091556SKees Cook 		pr_info("mnt_sb lacks block device, treating as: writable\n");
929b091556SKees Cook 
9360ba1028SKees Cook 	if (!writable)
949b091556SKees Cook 		pr_info("load pinning engaged.\n");
959b091556SKees Cook }
969b091556SKees Cook 
97b76ded21SKees Cook /*
98b76ded21SKees Cook  * This must be called after early kernel init, since then the rootdev
99b76ded21SKees Cook  * is available.
100b76ded21SKees Cook  */
sb_is_writable(struct super_block * mnt_sb)101b76ded21SKees Cook static bool sb_is_writable(struct super_block *mnt_sb)
102b76ded21SKees Cook {
103b76ded21SKees Cook 	bool writable = true;
104b76ded21SKees Cook 
105b76ded21SKees Cook 	if (mnt_sb->s_bdev)
106b76ded21SKees Cook 		writable = !bdev_read_only(mnt_sb->s_bdev);
107b76ded21SKees Cook 
108b76ded21SKees Cook 	return writable;
109b76ded21SKees Cook }
110b76ded21SKees Cook 
loadpin_sb_free_security(struct super_block * mnt_sb)1119b091556SKees Cook static void loadpin_sb_free_security(struct super_block *mnt_sb)
1129b091556SKees Cook {
1139b091556SKees Cook 	/*
1149b091556SKees Cook 	 * When unmounting the filesystem we were using for load
1159b091556SKees Cook 	 * pinning, we acknowledge the superblock release, but make sure
116eba77359SKees Cook 	 * no other modules or firmware can be loaded when we are in
117eba77359SKees Cook 	 * enforcing mode. Otherwise, allow the root to be reestablished.
1189b091556SKees Cook 	 */
1199b091556SKees Cook 	if (!IS_ERR_OR_NULL(pinned_root) && mnt_sb == pinned_root) {
120eba77359SKees Cook 		if (enforce) {
1219b091556SKees Cook 			pinned_root = ERR_PTR(-EIO);
1229b091556SKees Cook 			pr_info("umount pinned fs: refusing further loads\n");
123eba77359SKees Cook 		} else {
124eba77359SKees Cook 			pinned_root = NULL;
125eba77359SKees Cook 		}
1269b091556SKees Cook 	}
1279b091556SKees Cook }
1289b091556SKees Cook 
loadpin_check(struct file * file,enum kernel_read_file_id id)1291a17e5b5SKees Cook static int loadpin_check(struct file *file, enum kernel_read_file_id id)
1309b091556SKees Cook {
1319b091556SKees Cook 	struct super_block *load_root;
1329b091556SKees Cook 	const char *origin = kernel_read_file_id_str(id);
1332cfaa84eSKees Cook 	bool first_root_pin = false;
134b76ded21SKees Cook 	bool load_root_writable;
1359b091556SKees Cook 
1360ff98480SKe Wu 	/* If the file id is excluded, ignore the pinning. */
1370ff98480SKe Wu 	if ((unsigned int)id < ARRAY_SIZE(ignore_read_file_id) &&
1380ff98480SKe Wu 	    ignore_read_file_id[id]) {
1390ff98480SKe Wu 		report_load(origin, file, "pinning-excluded");
1400ff98480SKe Wu 		return 0;
1410ff98480SKe Wu 	}
1420ff98480SKe Wu 
1439b091556SKees Cook 	/* This handles the older init_module API that has a NULL file. */
1449b091556SKees Cook 	if (!file) {
14513523befSKees Cook 		if (!enforce) {
1469b091556SKees Cook 			report_load(origin, NULL, "old-api-pinning-ignored");
1479b091556SKees Cook 			return 0;
1489b091556SKees Cook 		}
1499b091556SKees Cook 
1509b091556SKees Cook 		report_load(origin, NULL, "old-api-denied");
1519b091556SKees Cook 		return -EPERM;
1529b091556SKees Cook 	}
1539b091556SKees Cook 
1549b091556SKees Cook 	load_root = file->f_path.mnt->mnt_sb;
155b76ded21SKees Cook 	load_root_writable = sb_is_writable(load_root);
1569b091556SKees Cook 
1579b091556SKees Cook 	/* First loaded module/firmware defines the root for all others. */
1589b091556SKees Cook 	spin_lock(&pinned_root_spinlock);
1599b091556SKees Cook 	/*
160eba77359SKees Cook 	 * pinned_root is only NULL at startup or when the pinned root has
161eba77359SKees Cook 	 * been unmounted while we are not in enforcing mode. Otherwise, it
162eba77359SKees Cook 	 * is either a valid reference, or an ERR_PTR.
1639b091556SKees Cook 	 */
1649b091556SKees Cook 	if (!pinned_root) {
1659b091556SKees Cook 		pinned_root = load_root;
1662cfaa84eSKees Cook 		first_root_pin = true;
1672cfaa84eSKees Cook 	}
1689b091556SKees Cook 	spin_unlock(&pinned_root_spinlock);
1692cfaa84eSKees Cook 
1702cfaa84eSKees Cook 	if (first_root_pin) {
171b76ded21SKees Cook 		report_writable(pinned_root, load_root_writable);
17260ba1028SKees Cook 		set_sysctl(load_root_writable);
1739b091556SKees Cook 		report_load(origin, file, "pinned");
1749b091556SKees Cook 	}
1759b091556SKees Cook 
1763f805f8cSMatthias Kaehlcke 	if (IS_ERR_OR_NULL(pinned_root) ||
1773f805f8cSMatthias Kaehlcke 	    ((load_root != pinned_root) && !dm_verity_loadpin_is_bdev_trusted(load_root->s_bdev))) {
17813523befSKees Cook 		if (unlikely(!enforce)) {
1799b091556SKees Cook 			report_load(origin, file, "pinning-ignored");
1809b091556SKees Cook 			return 0;
1819b091556SKees Cook 		}
1829b091556SKees Cook 
1839b091556SKees Cook 		report_load(origin, file, "denied");
1849b091556SKees Cook 		return -EPERM;
1859b091556SKees Cook 	}
1869b091556SKees Cook 
1879b091556SKees Cook 	return 0;
1889b091556SKees Cook }
1899b091556SKees Cook 
loadpin_read_file(struct file * file,enum kernel_read_file_id id,bool contents)1901a17e5b5SKees Cook static int loadpin_read_file(struct file *file, enum kernel_read_file_id id,
1911a17e5b5SKees Cook 			     bool contents)
1921a17e5b5SKees Cook {
1931a17e5b5SKees Cook 	/*
1941a17e5b5SKees Cook 	 * LoadPin only cares about the _origin_ of a file, not its
1951a17e5b5SKees Cook 	 * contents, so we can ignore the "are full contents available"
1961a17e5b5SKees Cook 	 * argument here.
1971a17e5b5SKees Cook 	 */
1981a17e5b5SKees Cook 	return loadpin_check(file, id);
1991a17e5b5SKees Cook }
2001a17e5b5SKees Cook 
loadpin_load_data(enum kernel_load_data_id id,bool contents)201b64fcae7SKees Cook static int loadpin_load_data(enum kernel_load_data_id id, bool contents)
202c77b8cdfSMimi Zohar {
2031a17e5b5SKees Cook 	/*
2041a17e5b5SKees Cook 	 * LoadPin only cares about the _origin_ of a file, not its
2051a17e5b5SKees Cook 	 * contents, so a NULL file is passed, and we can ignore the
2061a17e5b5SKees Cook 	 * state of "contents".
2071a17e5b5SKees Cook 	 */
2081a17e5b5SKees Cook 	return loadpin_check(NULL, (enum kernel_read_file_id) id);
209c77b8cdfSMimi Zohar }
210c77b8cdfSMimi Zohar 
211f22f9aafSPaul Moore static struct security_hook_list loadpin_hooks[] __ro_after_init = {
2129b091556SKees Cook 	LSM_HOOK_INIT(sb_free_security, loadpin_sb_free_security),
2139b091556SKees Cook 	LSM_HOOK_INIT(kernel_read_file, loadpin_read_file),
214c77b8cdfSMimi Zohar 	LSM_HOOK_INIT(kernel_load_data, loadpin_load_data),
2159b091556SKees Cook };
2169b091556SKees Cook 
parse_exclude(void)2170ff98480SKe Wu static void __init parse_exclude(void)
2180ff98480SKe Wu {
2190ff98480SKe Wu 	int i, j;
2200ff98480SKe Wu 	char *cur;
2210ff98480SKe Wu 
2220ff98480SKe Wu 	/*
2230ff98480SKe Wu 	 * Make sure all the arrays stay within expected sizes. This
2240ff98480SKe Wu 	 * is slightly weird because kernel_read_file_str[] includes
2250ff98480SKe Wu 	 * READING_MAX_ID, which isn't actually meaningful here.
2260ff98480SKe Wu 	 */
2270ff98480SKe Wu 	BUILD_BUG_ON(ARRAY_SIZE(exclude_read_files) !=
2280ff98480SKe Wu 		     ARRAY_SIZE(ignore_read_file_id));
2290ff98480SKe Wu 	BUILD_BUG_ON(ARRAY_SIZE(kernel_read_file_str) <
2300ff98480SKe Wu 		     ARRAY_SIZE(ignore_read_file_id));
2310ff98480SKe Wu 
2320ff98480SKe Wu 	for (i = 0; i < ARRAY_SIZE(exclude_read_files); i++) {
2330ff98480SKe Wu 		cur = exclude_read_files[i];
2340ff98480SKe Wu 		if (!cur)
2350ff98480SKe Wu 			break;
2360ff98480SKe Wu 		if (*cur == '\0')
2370ff98480SKe Wu 			continue;
2380ff98480SKe Wu 
2390ff98480SKe Wu 		for (j = 0; j < ARRAY_SIZE(ignore_read_file_id); j++) {
2400ff98480SKe Wu 			if (strcmp(cur, kernel_read_file_str[j]) == 0) {
2410ff98480SKe Wu 				pr_info("excluding: %s\n",
2420ff98480SKe Wu 					kernel_read_file_str[j]);
2430ff98480SKe Wu 				ignore_read_file_id[j] = 1;
2440ff98480SKe Wu 				/*
2450ff98480SKe Wu 				 * Can not break, because one read_file_str
2460ff98480SKe Wu 				 * may map to more than on read_file_id.
2470ff98480SKe Wu 				 */
2480ff98480SKe Wu 			}
2490ff98480SKe Wu 		}
2500ff98480SKe Wu 	}
2510ff98480SKe Wu }
2520ff98480SKe Wu 
loadpin_init(void)25370b62c25SKees Cook static int __init loadpin_init(void)
2549b091556SKees Cook {
25513523befSKees Cook 	pr_info("ready to pin (currently %senforcing)\n",
25613523befSKees Cook 		enforce ? "" : "not ");
2570ff98480SKe Wu 	parse_exclude();
25860ba1028SKees Cook #ifdef CONFIG_SYSCTL
2595df5bdc3SLuis Chamberlain 	if (!register_sysctl("kernel/loadpin", loadpin_sysctl_table))
26060ba1028SKees Cook 		pr_notice("sysctl registration failed!\n");
26160ba1028SKees Cook #endif
262d69dece5SCasey Schaufler 	security_add_hooks(loadpin_hooks, ARRAY_SIZE(loadpin_hooks), "loadpin");
2633f805f8cSMatthias Kaehlcke 
26470b62c25SKees Cook 	return 0;
2659b091556SKees Cook }
2669b091556SKees Cook 
26770b62c25SKees Cook DEFINE_LSM(loadpin) = {
26870b62c25SKees Cook 	.name = "loadpin",
26970b62c25SKees Cook 	.init = loadpin_init,
27070b62c25SKees Cook };
27170b62c25SKees Cook 
2723f805f8cSMatthias Kaehlcke #ifdef CONFIG_SECURITY_LOADPIN_VERITY
2733f805f8cSMatthias Kaehlcke 
2743f805f8cSMatthias Kaehlcke enum loadpin_securityfs_interface_index {
2753f805f8cSMatthias Kaehlcke 	LOADPIN_DM_VERITY,
2763f805f8cSMatthias Kaehlcke };
2773f805f8cSMatthias Kaehlcke 
read_trusted_verity_root_digests(unsigned int fd)2783f805f8cSMatthias Kaehlcke static int read_trusted_verity_root_digests(unsigned int fd)
2793f805f8cSMatthias Kaehlcke {
2803f805f8cSMatthias Kaehlcke 	struct fd f;
2813f805f8cSMatthias Kaehlcke 	void *data;
2823f805f8cSMatthias Kaehlcke 	int rc;
2833f805f8cSMatthias Kaehlcke 	char *p, *d;
2843f805f8cSMatthias Kaehlcke 
2853f805f8cSMatthias Kaehlcke 	if (deny_reading_verity_digests)
2863f805f8cSMatthias Kaehlcke 		return -EPERM;
2873f805f8cSMatthias Kaehlcke 
2883f805f8cSMatthias Kaehlcke 	/* The list of trusted root digests can only be set up once */
2893f805f8cSMatthias Kaehlcke 	if (!list_empty(&dm_verity_loadpin_trusted_root_digests))
2903f805f8cSMatthias Kaehlcke 		return -EPERM;
2913f805f8cSMatthias Kaehlcke 
2923f805f8cSMatthias Kaehlcke 	f = fdget(fd);
2933f805f8cSMatthias Kaehlcke 	if (!f.file)
2943f805f8cSMatthias Kaehlcke 		return -EINVAL;
2953f805f8cSMatthias Kaehlcke 
2963f805f8cSMatthias Kaehlcke 	data = kzalloc(SZ_4K, GFP_KERNEL);
2973f805f8cSMatthias Kaehlcke 	if (!data) {
2983f805f8cSMatthias Kaehlcke 		rc = -ENOMEM;
2993f805f8cSMatthias Kaehlcke 		goto err;
3003f805f8cSMatthias Kaehlcke 	}
3013f805f8cSMatthias Kaehlcke 
3023f805f8cSMatthias Kaehlcke 	rc = kernel_read_file(f.file, 0, (void **)&data, SZ_4K - 1, NULL, READING_POLICY);
3033f805f8cSMatthias Kaehlcke 	if (rc < 0)
3043f805f8cSMatthias Kaehlcke 		goto err;
3053f805f8cSMatthias Kaehlcke 
3063f805f8cSMatthias Kaehlcke 	p = data;
3073f805f8cSMatthias Kaehlcke 	p[rc] = '\0';
3083f805f8cSMatthias Kaehlcke 	p = strim(p);
3093f805f8cSMatthias Kaehlcke 
3103f805f8cSMatthias Kaehlcke 	p = strim(data);
3113f805f8cSMatthias Kaehlcke 	while ((d = strsep(&p, "\n")) != NULL) {
3126e42aec7SMatthias Kaehlcke 		int len;
3133f805f8cSMatthias Kaehlcke 		struct dm_verity_loadpin_trusted_root_digest *trd;
3143f805f8cSMatthias Kaehlcke 
3156e42aec7SMatthias Kaehlcke 		if (d == data) {
3166e42aec7SMatthias Kaehlcke 			/* first line, validate header */
3176e42aec7SMatthias Kaehlcke 			if (strcmp(d, VERITY_DIGEST_FILE_HEADER)) {
3186e42aec7SMatthias Kaehlcke 				rc = -EPROTO;
3196e42aec7SMatthias Kaehlcke 				goto err;
3206e42aec7SMatthias Kaehlcke 			}
3216e42aec7SMatthias Kaehlcke 
3226e42aec7SMatthias Kaehlcke 			continue;
3236e42aec7SMatthias Kaehlcke 		}
3246e42aec7SMatthias Kaehlcke 
3256e42aec7SMatthias Kaehlcke 		len = strlen(d);
3266e42aec7SMatthias Kaehlcke 
3273f805f8cSMatthias Kaehlcke 		if (len % 2) {
3283f805f8cSMatthias Kaehlcke 			rc = -EPROTO;
3293f805f8cSMatthias Kaehlcke 			goto err;
3303f805f8cSMatthias Kaehlcke 		}
3313f805f8cSMatthias Kaehlcke 
3323f805f8cSMatthias Kaehlcke 		len /= 2;
3333f805f8cSMatthias Kaehlcke 
3343f805f8cSMatthias Kaehlcke 		trd = kzalloc(struct_size(trd, data, len), GFP_KERNEL);
3353f805f8cSMatthias Kaehlcke 		if (!trd) {
3363f805f8cSMatthias Kaehlcke 			rc = -ENOMEM;
3373f805f8cSMatthias Kaehlcke 			goto err;
3383f805f8cSMatthias Kaehlcke 		}
339*5f536ac6SKees Cook 		trd->len = len;
3403f805f8cSMatthias Kaehlcke 
3413f805f8cSMatthias Kaehlcke 		if (hex2bin(trd->data, d, len)) {
3423f805f8cSMatthias Kaehlcke 			kfree(trd);
3433f805f8cSMatthias Kaehlcke 			rc = -EPROTO;
3443f805f8cSMatthias Kaehlcke 			goto err;
3453f805f8cSMatthias Kaehlcke 		}
3463f805f8cSMatthias Kaehlcke 
3473f805f8cSMatthias Kaehlcke 		list_add_tail(&trd->node, &dm_verity_loadpin_trusted_root_digests);
3483f805f8cSMatthias Kaehlcke 	}
3493f805f8cSMatthias Kaehlcke 
3503f805f8cSMatthias Kaehlcke 	if (list_empty(&dm_verity_loadpin_trusted_root_digests)) {
3513f805f8cSMatthias Kaehlcke 		rc = -EPROTO;
3523f805f8cSMatthias Kaehlcke 		goto err;
3533f805f8cSMatthias Kaehlcke 	}
3543f805f8cSMatthias Kaehlcke 
3553f805f8cSMatthias Kaehlcke 	kfree(data);
3563f805f8cSMatthias Kaehlcke 	fdput(f);
3573f805f8cSMatthias Kaehlcke 
3583f805f8cSMatthias Kaehlcke 	return 0;
3593f805f8cSMatthias Kaehlcke 
3603f805f8cSMatthias Kaehlcke err:
3613f805f8cSMatthias Kaehlcke 	kfree(data);
3623f805f8cSMatthias Kaehlcke 
3633f805f8cSMatthias Kaehlcke 	/* any failure in loading/parsing invalidates the entire list */
3643f805f8cSMatthias Kaehlcke 	{
3653f805f8cSMatthias Kaehlcke 		struct dm_verity_loadpin_trusted_root_digest *trd, *tmp;
3663f805f8cSMatthias Kaehlcke 
3673f805f8cSMatthias Kaehlcke 		list_for_each_entry_safe(trd, tmp, &dm_verity_loadpin_trusted_root_digests, node) {
3683f805f8cSMatthias Kaehlcke 			list_del(&trd->node);
3693f805f8cSMatthias Kaehlcke 			kfree(trd);
3703f805f8cSMatthias Kaehlcke 		}
3713f805f8cSMatthias Kaehlcke 	}
3723f805f8cSMatthias Kaehlcke 
3733f805f8cSMatthias Kaehlcke 	/* disallow further attempts after reading a corrupt/invalid file */
3743f805f8cSMatthias Kaehlcke 	deny_reading_verity_digests = true;
3753f805f8cSMatthias Kaehlcke 
3763f805f8cSMatthias Kaehlcke 	fdput(f);
3773f805f8cSMatthias Kaehlcke 
3783f805f8cSMatthias Kaehlcke 	return rc;
3793f805f8cSMatthias Kaehlcke }
3803f805f8cSMatthias Kaehlcke 
3813f805f8cSMatthias Kaehlcke /******************************** securityfs ********************************/
3823f805f8cSMatthias Kaehlcke 
dm_verity_ioctl(struct file * filp,unsigned int cmd,unsigned long arg)3833f805f8cSMatthias Kaehlcke static long dm_verity_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
3843f805f8cSMatthias Kaehlcke {
3853f805f8cSMatthias Kaehlcke 	void __user *uarg = (void __user *)arg;
3863f805f8cSMatthias Kaehlcke 	unsigned int fd;
3873f805f8cSMatthias Kaehlcke 
3883f805f8cSMatthias Kaehlcke 	switch (cmd) {
3893f805f8cSMatthias Kaehlcke 	case LOADPIN_IOC_SET_TRUSTED_VERITY_DIGESTS:
3906a3981afSKees Cook 		if (copy_from_user(&fd, uarg, sizeof(fd)))
3916a3981afSKees Cook 			return -EFAULT;
3923f805f8cSMatthias Kaehlcke 
3933f805f8cSMatthias Kaehlcke 		return read_trusted_verity_root_digests(fd);
3943f805f8cSMatthias Kaehlcke 
3953f805f8cSMatthias Kaehlcke 	default:
3963f805f8cSMatthias Kaehlcke 		return -EINVAL;
3973f805f8cSMatthias Kaehlcke 	}
3983f805f8cSMatthias Kaehlcke }
3993f805f8cSMatthias Kaehlcke 
4003f805f8cSMatthias Kaehlcke static const struct file_operations loadpin_dm_verity_ops = {
4013f805f8cSMatthias Kaehlcke 	.unlocked_ioctl = dm_verity_ioctl,
4023f805f8cSMatthias Kaehlcke 	.compat_ioctl = compat_ptr_ioctl,
4033f805f8cSMatthias Kaehlcke };
4043f805f8cSMatthias Kaehlcke 
4053f805f8cSMatthias Kaehlcke /**
4063f805f8cSMatthias Kaehlcke  * init_loadpin_securityfs - create the securityfs directory for LoadPin
4073f805f8cSMatthias Kaehlcke  *
4083f805f8cSMatthias Kaehlcke  * We can not put this method normally under the loadpin_init() code path since
4093f805f8cSMatthias Kaehlcke  * the security subsystem gets initialized before the vfs caches.
4103f805f8cSMatthias Kaehlcke  *
4113f805f8cSMatthias Kaehlcke  * Returns 0 if the securityfs directory creation was successful.
4123f805f8cSMatthias Kaehlcke  */
init_loadpin_securityfs(void)4133f805f8cSMatthias Kaehlcke static int __init init_loadpin_securityfs(void)
4143f805f8cSMatthias Kaehlcke {
4153f805f8cSMatthias Kaehlcke 	struct dentry *loadpin_dir, *dentry;
4163f805f8cSMatthias Kaehlcke 
4173f805f8cSMatthias Kaehlcke 	loadpin_dir = securityfs_create_dir("loadpin", NULL);
4183f805f8cSMatthias Kaehlcke 	if (IS_ERR(loadpin_dir)) {
4193f805f8cSMatthias Kaehlcke 		pr_err("LoadPin: could not create securityfs dir: %ld\n",
4203f805f8cSMatthias Kaehlcke 		       PTR_ERR(loadpin_dir));
4213f805f8cSMatthias Kaehlcke 		return PTR_ERR(loadpin_dir);
4223f805f8cSMatthias Kaehlcke 	}
4233f805f8cSMatthias Kaehlcke 
4243f805f8cSMatthias Kaehlcke 	dentry = securityfs_create_file("dm-verity", 0600, loadpin_dir,
4253f805f8cSMatthias Kaehlcke 					(void *)LOADPIN_DM_VERITY, &loadpin_dm_verity_ops);
4263f805f8cSMatthias Kaehlcke 	if (IS_ERR(dentry)) {
4273f805f8cSMatthias Kaehlcke 		pr_err("LoadPin: could not create securityfs entry 'dm-verity': %ld\n",
4283f805f8cSMatthias Kaehlcke 		       PTR_ERR(dentry));
4293f805f8cSMatthias Kaehlcke 		return PTR_ERR(dentry);
4303f805f8cSMatthias Kaehlcke 	}
4313f805f8cSMatthias Kaehlcke 
4323f805f8cSMatthias Kaehlcke 	return 0;
4333f805f8cSMatthias Kaehlcke }
4343f805f8cSMatthias Kaehlcke 
4353f805f8cSMatthias Kaehlcke fs_initcall(init_loadpin_securityfs);
4363f805f8cSMatthias Kaehlcke 
4373f805f8cSMatthias Kaehlcke #endif /* CONFIG_SECURITY_LOADPIN_VERITY */
4383f805f8cSMatthias Kaehlcke 
4399b091556SKees Cook /* Should not be mutable after boot, so not listed in sysfs (perm == 0). */
44013523befSKees Cook module_param(enforce, int, 0);
44113523befSKees Cook MODULE_PARM_DESC(enforce, "Enforce module/firmware pinning");
4420ff98480SKe Wu module_param_array_named(exclude, exclude_read_files, charp, NULL, 0);
4430ff98480SKe Wu MODULE_PARM_DESC(exclude, "Exclude pinning specific read file types");
444