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>
22 static void report_load(const char *origin, struct file *file, char *operation)
24 char *cmdline, *pathname;
26 pathname = kstrdup_quotable_file(file, GFP_KERNEL);
27 cmdline = kstrdup_quotable_cmdline(current, GFP_KERNEL);
29 pr_notice("%s %s obj=%s%s%s pid=%d cmdline=%s%s%s\n",
31 (pathname && pathname[0] != '<') ? "\"" : "",
33 (pathname && pathname[0] != '<') ? "\"" : "",
35 cmdline ? "\"" : "", cmdline, cmdline ? "\"" : "");
41 static int enforce = IS_ENABLED(CONFIG_SECURITY_LOADPIN_ENFORCE);
42 static char *exclude_read_files[READING_MAX_ID];
43 static int ignore_read_file_id[READING_MAX_ID] __ro_after_init;
44 static struct super_block *pinned_root;
45 static DEFINE_SPINLOCK(pinned_root_spinlock);
49 static struct ctl_path loadpin_sysctl_path[] = {
50 { .procname = "kernel", },
51 { .procname = "loadpin", },
55 static struct ctl_table loadpin_sysctl_table[] = {
57 .procname = "enforce",
59 .maxlen = sizeof(int),
61 .proc_handler = proc_dointvec_minmax,
62 .extra1 = SYSCTL_ZERO,
69 * This must be called after early kernel init, since then the rootdev
72 static void check_pinning_enforcement(struct super_block *mnt_sb)
77 * If load pinning is not enforced via a read-only block
78 * device, allow sysctl to change modes for testing.
81 char bdev[BDEVNAME_SIZE];
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");
90 pr_info("mnt_sb lacks block device, treating as: writable\n");
93 if (!register_sysctl_paths(loadpin_sysctl_path,
94 loadpin_sysctl_table))
95 pr_notice("sysctl registration failed!\n");
97 pr_info("enforcement can be disabled.\n");
99 pr_info("load pinning engaged.\n");
102 static void check_pinning_enforcement(struct super_block *mnt_sb)
104 pr_info("load pinning engaged.\n");
108 static void loadpin_sb_free_security(struct super_block *mnt_sb)
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.
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");
121 static int loadpin_check(struct file *file, enum kernel_read_file_id id)
123 struct super_block *load_root;
124 const char *origin = kernel_read_file_id_str(id);
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");
133 /* This handles the older init_module API that has a NULL file. */
136 report_load(origin, NULL, "old-api-pinning-ignored");
140 report_load(origin, NULL, "old-api-denied");
144 load_root = file->f_path.mnt->mnt_sb;
146 /* First loaded module/firmware defines the root for all others. */
147 spin_lock(&pinned_root_spinlock);
149 * pinned_root is only NULL at startup. Otherwise, it is either
150 * a valid reference, or an ERR_PTR.
153 pinned_root = load_root;
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.
160 spin_unlock(&pinned_root_spinlock);
161 check_pinning_enforcement(pinned_root);
162 report_load(origin, file, "pinned");
164 spin_unlock(&pinned_root_spinlock);
167 if (IS_ERR_OR_NULL(pinned_root) || load_root != pinned_root) {
168 if (unlikely(!enforce)) {
169 report_load(origin, file, "pinning-ignored");
173 report_load(origin, file, "denied");
180 static int loadpin_read_file(struct file *file, enum kernel_read_file_id id,
184 * LoadPin only cares about the _origin_ of a file, not its
185 * contents, so we can ignore the "are full contents available"
188 return loadpin_check(file, id);
191 static int loadpin_load_data(enum kernel_load_data_id id, bool contents)
194 * LoadPin only cares about the _origin_ of a file, not its
195 * contents, so a NULL file is passed, and we can ignore the
196 * state of "contents".
198 return loadpin_check(NULL, (enum kernel_read_file_id) id);
201 static struct security_hook_list loadpin_hooks[] __lsm_ro_after_init = {
202 LSM_HOOK_INIT(sb_free_security, loadpin_sb_free_security),
203 LSM_HOOK_INIT(kernel_read_file, loadpin_read_file),
204 LSM_HOOK_INIT(kernel_load_data, loadpin_load_data),
207 static void __init parse_exclude(void)
213 * Make sure all the arrays stay within expected sizes. This
214 * is slightly weird because kernel_read_file_str[] includes
215 * READING_MAX_ID, which isn't actually meaningful here.
217 BUILD_BUG_ON(ARRAY_SIZE(exclude_read_files) !=
218 ARRAY_SIZE(ignore_read_file_id));
219 BUILD_BUG_ON(ARRAY_SIZE(kernel_read_file_str) <
220 ARRAY_SIZE(ignore_read_file_id));
222 for (i = 0; i < ARRAY_SIZE(exclude_read_files); i++) {
223 cur = exclude_read_files[i];
229 for (j = 0; j < ARRAY_SIZE(ignore_read_file_id); j++) {
230 if (strcmp(cur, kernel_read_file_str[j]) == 0) {
231 pr_info("excluding: %s\n",
232 kernel_read_file_str[j]);
233 ignore_read_file_id[j] = 1;
235 * Can not break, because one read_file_str
236 * may map to more than on read_file_id.
243 static int __init loadpin_init(void)
245 pr_info("ready to pin (currently %senforcing)\n",
246 enforce ? "" : "not ");
248 security_add_hooks(loadpin_hooks, ARRAY_SIZE(loadpin_hooks), "loadpin");
252 DEFINE_LSM(loadpin) = {
254 .init = loadpin_init,
257 /* Should not be mutable after boot, so not listed in sysfs (perm == 0). */
258 module_param(enforce, int, 0);
259 MODULE_PARM_DESC(enforce, "Enforce module/firmware pinning");
260 module_param_array_named(exclude, exclude_read_files, charp, NULL, 0);
261 MODULE_PARM_DESC(exclude, "Exclude pinning specific read file types");