1 // SPDX-License-Identifier: GPL-2.0-only
3 * Module and Firmware Pinning Security Module
5 * Copyright 2011-2016 Google Inc.
7 * Author: Kees Cook <keescook@chromium.org>
10 #define pr_fmt(fmt) "LoadPin: " fmt
12 #include <linux/module.h>
14 #include <linux/kernel_read_file.h>
15 #include <linux/lsm_hooks.h>
16 #include <linux/mount.h>
17 #include <linux/blkdev.h>
18 #include <linux/path.h>
19 #include <linux/sched.h> /* current */
20 #include <linux/string_helpers.h>
21 #include <linux/dm-verity-loadpin.h>
22 #include <uapi/linux/loadpin.h>
24 static void report_load(const char *origin, struct file *file, char *operation)
26 char *cmdline, *pathname;
28 pathname = kstrdup_quotable_file(file, GFP_KERNEL);
29 cmdline = kstrdup_quotable_cmdline(current, GFP_KERNEL);
31 pr_notice("%s %s obj=%s%s%s pid=%d cmdline=%s%s%s\n",
33 (pathname && pathname[0] != '<') ? "\"" : "",
35 (pathname && pathname[0] != '<') ? "\"" : "",
37 cmdline ? "\"" : "", cmdline, cmdline ? "\"" : "");
43 static int enforce = IS_ENABLED(CONFIG_SECURITY_LOADPIN_ENFORCE);
44 static char *exclude_read_files[READING_MAX_ID];
45 static int ignore_read_file_id[READING_MAX_ID] __ro_after_init;
46 static struct super_block *pinned_root;
47 static DEFINE_SPINLOCK(pinned_root_spinlock);
48 #ifdef CONFIG_SECURITY_LOADPIN_VERITY
49 static bool deny_reading_verity_digests;
54 static struct ctl_path loadpin_sysctl_path[] = {
55 { .procname = "kernel", },
56 { .procname = "loadpin", },
60 static struct ctl_table loadpin_sysctl_table[] = {
62 .procname = "enforce",
64 .maxlen = sizeof(int),
66 .proc_handler = proc_dointvec_minmax,
67 .extra1 = SYSCTL_ZERO,
74 * This must be called after early kernel init, since then the rootdev
77 static void check_pinning_enforcement(struct super_block *mnt_sb)
82 * If load pinning is not enforced via a read-only block
83 * device, allow sysctl to change modes for testing.
86 ro = bdev_read_only(mnt_sb->s_bdev);
87 pr_info("%pg (%u:%u): %s\n", mnt_sb->s_bdev,
88 MAJOR(mnt_sb->s_bdev->bd_dev),
89 MINOR(mnt_sb->s_bdev->bd_dev),
90 ro ? "read-only" : "writable");
92 pr_info("mnt_sb lacks block device, treating as: writable\n");
95 if (!register_sysctl_paths(loadpin_sysctl_path,
96 loadpin_sysctl_table))
97 pr_notice("sysctl registration failed!\n");
99 pr_info("enforcement can be disabled.\n");
101 pr_info("load pinning engaged.\n");
104 static void check_pinning_enforcement(struct super_block *mnt_sb)
106 pr_info("load pinning engaged.\n");
110 static void loadpin_sb_free_security(struct super_block *mnt_sb)
113 * When unmounting the filesystem we were using for load
114 * pinning, we acknowledge the superblock release, but make sure
115 * no other modules or firmware can be loaded.
117 if (!IS_ERR_OR_NULL(pinned_root) && mnt_sb == pinned_root) {
118 pinned_root = ERR_PTR(-EIO);
119 pr_info("umount pinned fs: refusing further loads\n");
123 static int loadpin_read_file(struct file *file, enum kernel_read_file_id id,
126 struct super_block *load_root;
127 const char *origin = kernel_read_file_id_str(id);
130 * If we will not know that we'll be seeing the full contents
131 * then we cannot trust a load will be complete and unchanged
132 * off disk. Treat all contents=false hooks as if there were
133 * no associated file struct.
138 /* If the file id is excluded, ignore the pinning. */
139 if ((unsigned int)id < ARRAY_SIZE(ignore_read_file_id) &&
140 ignore_read_file_id[id]) {
141 report_load(origin, file, "pinning-excluded");
145 /* This handles the older init_module API that has a NULL file. */
148 report_load(origin, NULL, "old-api-pinning-ignored");
152 report_load(origin, NULL, "old-api-denied");
156 load_root = file->f_path.mnt->mnt_sb;
158 /* First loaded module/firmware defines the root for all others. */
159 spin_lock(&pinned_root_spinlock);
161 * pinned_root is only NULL at startup. Otherwise, it is either
162 * a valid reference, or an ERR_PTR.
165 pinned_root = load_root;
167 * Unlock now since it's only pinned_root we care about.
168 * In the worst case, we will (correctly) report pinning
169 * failures before we have announced that pinning is
170 * enforcing. This would be purely cosmetic.
172 spin_unlock(&pinned_root_spinlock);
173 check_pinning_enforcement(pinned_root);
174 report_load(origin, file, "pinned");
176 spin_unlock(&pinned_root_spinlock);
179 if (IS_ERR_OR_NULL(pinned_root) ||
180 ((load_root != pinned_root) && !dm_verity_loadpin_is_bdev_trusted(load_root->s_bdev))) {
181 if (unlikely(!enforce)) {
182 report_load(origin, file, "pinning-ignored");
186 report_load(origin, file, "denied");
193 static int loadpin_load_data(enum kernel_load_data_id id, bool contents)
195 return loadpin_read_file(NULL, (enum kernel_read_file_id) id, contents);
198 static struct security_hook_list loadpin_hooks[] __lsm_ro_after_init = {
199 LSM_HOOK_INIT(sb_free_security, loadpin_sb_free_security),
200 LSM_HOOK_INIT(kernel_read_file, loadpin_read_file),
201 LSM_HOOK_INIT(kernel_load_data, loadpin_load_data),
204 static void __init parse_exclude(void)
210 * Make sure all the arrays stay within expected sizes. This
211 * is slightly weird because kernel_read_file_str[] includes
212 * READING_MAX_ID, which isn't actually meaningful here.
214 BUILD_BUG_ON(ARRAY_SIZE(exclude_read_files) !=
215 ARRAY_SIZE(ignore_read_file_id));
216 BUILD_BUG_ON(ARRAY_SIZE(kernel_read_file_str) <
217 ARRAY_SIZE(ignore_read_file_id));
219 for (i = 0; i < ARRAY_SIZE(exclude_read_files); i++) {
220 cur = exclude_read_files[i];
226 for (j = 0; j < ARRAY_SIZE(ignore_read_file_id); j++) {
227 if (strcmp(cur, kernel_read_file_str[j]) == 0) {
228 pr_info("excluding: %s\n",
229 kernel_read_file_str[j]);
230 ignore_read_file_id[j] = 1;
232 * Can not break, because one read_file_str
233 * may map to more than on read_file_id.
240 static int __init loadpin_init(void)
242 pr_info("ready to pin (currently %senforcing)\n",
243 enforce ? "" : "not ");
245 security_add_hooks(loadpin_hooks, ARRAY_SIZE(loadpin_hooks), "loadpin");
250 DEFINE_LSM(loadpin) = {
252 .init = loadpin_init,
255 #ifdef CONFIG_SECURITY_LOADPIN_VERITY
257 enum loadpin_securityfs_interface_index {
261 static int read_trusted_verity_root_digests(unsigned int fd)
268 if (deny_reading_verity_digests)
271 /* The list of trusted root digests can only be set up once */
272 if (!list_empty(&dm_verity_loadpin_trusted_root_digests))
279 data = kzalloc(SZ_4K, GFP_KERNEL);
285 rc = kernel_read_file(f.file, 0, (void **)&data, SZ_4K - 1, NULL, READING_POLICY);
294 while ((d = strsep(&p, "\n")) != NULL) {
296 struct dm_verity_loadpin_trusted_root_digest *trd;
305 trd = kzalloc(struct_size(trd, data, len), GFP_KERNEL);
311 if (hex2bin(trd->data, d, len)) {
319 list_add_tail(&trd->node, &dm_verity_loadpin_trusted_root_digests);
322 if (list_empty(&dm_verity_loadpin_trusted_root_digests)) {
335 /* any failure in loading/parsing invalidates the entire list */
337 struct dm_verity_loadpin_trusted_root_digest *trd, *tmp;
339 list_for_each_entry_safe(trd, tmp, &dm_verity_loadpin_trusted_root_digests, node) {
340 list_del(&trd->node);
345 /* disallow further attempts after reading a corrupt/invalid file */
346 deny_reading_verity_digests = true;
353 /******************************** securityfs ********************************/
355 static long dm_verity_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
357 void __user *uarg = (void __user *)arg;
362 case LOADPIN_IOC_SET_TRUSTED_VERITY_DIGESTS:
363 rc = copy_from_user(&fd, uarg, sizeof(fd));
367 return read_trusted_verity_root_digests(fd);
374 static const struct file_operations loadpin_dm_verity_ops = {
375 .unlocked_ioctl = dm_verity_ioctl,
376 .compat_ioctl = compat_ptr_ioctl,
380 * init_loadpin_securityfs - create the securityfs directory for LoadPin
382 * We can not put this method normally under the loadpin_init() code path since
383 * the security subsystem gets initialized before the vfs caches.
385 * Returns 0 if the securityfs directory creation was successful.
387 static int __init init_loadpin_securityfs(void)
389 struct dentry *loadpin_dir, *dentry;
391 loadpin_dir = securityfs_create_dir("loadpin", NULL);
392 if (IS_ERR(loadpin_dir)) {
393 pr_err("LoadPin: could not create securityfs dir: %ld\n",
394 PTR_ERR(loadpin_dir));
395 return PTR_ERR(loadpin_dir);
398 dentry = securityfs_create_file("dm-verity", 0600, loadpin_dir,
399 (void *)LOADPIN_DM_VERITY, &loadpin_dm_verity_ops);
400 if (IS_ERR(dentry)) {
401 pr_err("LoadPin: could not create securityfs entry 'dm-verity': %ld\n",
403 return PTR_ERR(dentry);
409 fs_initcall(init_loadpin_securityfs);
411 #endif /* CONFIG_SECURITY_LOADPIN_VERITY */
413 /* Should not be mutable after boot, so not listed in sysfs (perm == 0). */
414 module_param(enforce, int, 0);
415 MODULE_PARM_DESC(enforce, "Enforce module/firmware pinning");
416 module_param_array_named(exclude, exclude_read_files, charp, NULL, 0);
417 MODULE_PARM_DESC(exclude, "Exclude pinning specific read file types");