2 * Module and Firmware Pinning Security Module
4 * Copyright 2011-2016 Google Inc.
6 * Author: Kees Cook <keescook@chromium.org>
8 * This software is licensed under the terms of the GNU General Public
9 * License version 2, as published by the Free Software Foundation, and
10 * may be copied, distributed, and modified under those terms.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
18 #define pr_fmt(fmt) "LoadPin: " fmt
20 #include <linux/module.h>
22 #include <linux/fs_struct.h>
23 #include <linux/lsm_hooks.h>
24 #include <linux/mount.h>
25 #include <linux/path.h>
26 #include <linux/sched.h> /* current */
27 #include <linux/string_helpers.h>
29 static void report_load(const char *origin
, struct file
*file
, char *operation
)
31 char *cmdline
, *pathname
;
33 pathname
= kstrdup_quotable_file(file
, GFP_KERNEL
);
34 cmdline
= kstrdup_quotable_cmdline(current
, GFP_KERNEL
);
36 pr_notice("%s %s obj=%s%s%s pid=%d cmdline=%s%s%s\n",
38 (pathname
&& pathname
[0] != '<') ? "\"" : "",
40 (pathname
&& pathname
[0] != '<') ? "\"" : "",
42 cmdline
? "\"" : "", cmdline
, cmdline
? "\"" : "");
48 static int enabled
= IS_ENABLED(CONFIG_SECURITY_LOADPIN_ENABLED
);
49 static struct super_block
*pinned_root
;
50 static DEFINE_SPINLOCK(pinned_root_spinlock
);
56 static struct ctl_path loadpin_sysctl_path
[] = {
57 { .procname
= "kernel", },
58 { .procname
= "loadpin", },
62 static struct ctl_table loadpin_sysctl_table
[] = {
64 .procname
= "enabled",
66 .maxlen
= sizeof(int),
68 .proc_handler
= proc_dointvec_minmax
,
76 * This must be called after early kernel init, since then the rootdev
79 static void check_pinning_enforcement(struct super_block
*mnt_sb
)
84 * If load pinning is not enforced via a read-only block
85 * device, allow sysctl to change modes for testing.
88 ro
= bdev_read_only(mnt_sb
->s_bdev
);
89 pr_info("dev(%u,%u): %s\n",
90 MAJOR(mnt_sb
->s_bdev
->bd_dev
),
91 MINOR(mnt_sb
->s_bdev
->bd_dev
),
92 ro
? "read-only" : "writable");
94 pr_info("mnt_sb lacks block device, treating as: writable\n");
97 if (!register_sysctl_paths(loadpin_sysctl_path
,
98 loadpin_sysctl_table
))
99 pr_notice("sysctl registration failed!\n");
101 pr_info("load pinning can be disabled.\n");
103 pr_info("load pinning engaged.\n");
106 static void check_pinning_enforcement(struct super_block
*mnt_sb
)
108 pr_info("load pinning engaged.\n");
112 static void loadpin_sb_free_security(struct super_block
*mnt_sb
)
115 * When unmounting the filesystem we were using for load
116 * pinning, we acknowledge the superblock release, but make sure
117 * no other modules or firmware can be loaded.
119 if (!IS_ERR_OR_NULL(pinned_root
) && mnt_sb
== pinned_root
) {
120 pinned_root
= ERR_PTR(-EIO
);
121 pr_info("umount pinned fs: refusing further loads\n");
125 static int loadpin_read_file(struct file
*file
, enum kernel_read_file_id id
)
127 struct super_block
*load_root
;
128 const char *origin
= kernel_read_file_id_str(id
);
130 /* This handles the older init_module API that has a NULL file. */
133 report_load(origin
, NULL
, "old-api-pinning-ignored");
137 report_load(origin
, NULL
, "old-api-denied");
141 load_root
= file
->f_path
.mnt
->mnt_sb
;
143 /* First loaded module/firmware defines the root for all others. */
144 spin_lock(&pinned_root_spinlock
);
146 * pinned_root is only NULL at startup. Otherwise, it is either
147 * a valid reference, or an ERR_PTR.
150 pinned_root
= load_root
;
152 * Unlock now since it's only pinned_root we care about.
153 * In the worst case, we will (correctly) report pinning
154 * failures before we have announced that pinning is
155 * enabled. This would be purely cosmetic.
157 spin_unlock(&pinned_root_spinlock
);
158 check_pinning_enforcement(pinned_root
);
159 report_load(origin
, file
, "pinned");
161 spin_unlock(&pinned_root_spinlock
);
164 if (IS_ERR_OR_NULL(pinned_root
) || load_root
!= pinned_root
) {
165 if (unlikely(!enabled
)) {
166 report_load(origin
, file
, "pinning-ignored");
170 report_load(origin
, file
, "denied");
177 static struct security_hook_list loadpin_hooks
[] = {
178 LSM_HOOK_INIT(sb_free_security
, loadpin_sb_free_security
),
179 LSM_HOOK_INIT(kernel_read_file
, loadpin_read_file
),
182 void __init
loadpin_add_hooks(void)
184 pr_info("ready to pin (currently %sabled)", enabled
? "en" : "dis");
185 security_add_hooks(loadpin_hooks
, ARRAY_SIZE(loadpin_hooks
));
188 /* Should not be mutable after boot, so not listed in sysfs (perm == 0). */
189 module_param(enabled
, int, 0);
190 MODULE_PARM_DESC(enabled
, "Pin module/firmware loading (default: true)");