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