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