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/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>
20 static void report_load(const char *origin
, struct file
*file
, char *operation
)
22 char *cmdline
, *pathname
;
24 pathname
= kstrdup_quotable_file(file
, GFP_KERNEL
);
25 cmdline
= kstrdup_quotable_cmdline(current
, GFP_KERNEL
);
27 pr_notice("%s %s obj=%s%s%s pid=%d cmdline=%s%s%s\n",
29 (pathname
&& pathname
[0] != '<') ? "\"" : "",
31 (pathname
&& pathname
[0] != '<') ? "\"" : "",
33 cmdline
? "\"" : "", cmdline
, cmdline
? "\"" : "");
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
);
47 static struct ctl_path loadpin_sysctl_path
[] = {
48 { .procname
= "kernel", },
49 { .procname
= "loadpin", },
53 static struct ctl_table loadpin_sysctl_table
[] = {
55 .procname
= "enforce",
57 .maxlen
= sizeof(int),
59 .proc_handler
= proc_dointvec_minmax
,
60 .extra1
= SYSCTL_ZERO
,
67 * This must be called after early kernel init, since then the rootdev
70 static void check_pinning_enforcement(struct super_block
*mnt_sb
)
75 * If load pinning is not enforced via a read-only block
76 * device, allow sysctl to change modes for testing.
79 char bdev
[BDEVNAME_SIZE
];
81 ro
= bdev_read_only(mnt_sb
->s_bdev
);
82 bdevname(mnt_sb
->s_bdev
, bdev
);
83 pr_info("%s (%u:%u): %s\n", bdev
,
84 MAJOR(mnt_sb
->s_bdev
->bd_dev
),
85 MINOR(mnt_sb
->s_bdev
->bd_dev
),
86 ro
? "read-only" : "writable");
88 pr_info("mnt_sb lacks block device, treating as: writable\n");
91 if (!register_sysctl_paths(loadpin_sysctl_path
,
92 loadpin_sysctl_table
))
93 pr_notice("sysctl registration failed!\n");
95 pr_info("enforcement can be disabled.\n");
97 pr_info("load pinning engaged.\n");
100 static void check_pinning_enforcement(struct super_block
*mnt_sb
)
102 pr_info("load pinning engaged.\n");
106 static void loadpin_sb_free_security(struct super_block
*mnt_sb
)
109 * When unmounting the filesystem we were using for load
110 * pinning, we acknowledge the superblock release, but make sure
111 * no other modules or firmware can be loaded.
113 if (!IS_ERR_OR_NULL(pinned_root
) && mnt_sb
== pinned_root
) {
114 pinned_root
= ERR_PTR(-EIO
);
115 pr_info("umount pinned fs: refusing further loads\n");
119 static int loadpin_read_file(struct file
*file
, enum kernel_read_file_id id
)
121 struct super_block
*load_root
;
122 const char *origin
= kernel_read_file_id_str(id
);
124 /* If the file id is excluded, ignore the pinning. */
125 if ((unsigned int)id
< ARRAY_SIZE(ignore_read_file_id
) &&
126 ignore_read_file_id
[id
]) {
127 report_load(origin
, file
, "pinning-excluded");
131 /* This handles the older init_module API that has a NULL file. */
134 report_load(origin
, NULL
, "old-api-pinning-ignored");
138 report_load(origin
, NULL
, "old-api-denied");
142 load_root
= file
->f_path
.mnt
->mnt_sb
;
144 /* First loaded module/firmware defines the root for all others. */
145 spin_lock(&pinned_root_spinlock
);
147 * pinned_root is only NULL at startup. Otherwise, it is either
148 * a valid reference, or an ERR_PTR.
151 pinned_root
= load_root
;
153 * Unlock now since it's only pinned_root we care about.
154 * In the worst case, we will (correctly) report pinning
155 * failures before we have announced that pinning is
156 * enforcing. This would be purely cosmetic.
158 spin_unlock(&pinned_root_spinlock
);
159 check_pinning_enforcement(pinned_root
);
160 report_load(origin
, file
, "pinned");
162 spin_unlock(&pinned_root_spinlock
);
165 if (IS_ERR_OR_NULL(pinned_root
) || load_root
!= pinned_root
) {
166 if (unlikely(!enforce
)) {
167 report_load(origin
, file
, "pinning-ignored");
171 report_load(origin
, file
, "denied");
178 static int loadpin_load_data(enum kernel_load_data_id id
)
180 return loadpin_read_file(NULL
, (enum kernel_read_file_id
) id
);
183 static struct security_hook_list loadpin_hooks
[] __lsm_ro_after_init
= {
184 LSM_HOOK_INIT(sb_free_security
, loadpin_sb_free_security
),
185 LSM_HOOK_INIT(kernel_read_file
, loadpin_read_file
),
186 LSM_HOOK_INIT(kernel_load_data
, loadpin_load_data
),
189 static void __init
parse_exclude(void)
195 * Make sure all the arrays stay within expected sizes. This
196 * is slightly weird because kernel_read_file_str[] includes
197 * READING_MAX_ID, which isn't actually meaningful here.
199 BUILD_BUG_ON(ARRAY_SIZE(exclude_read_files
) !=
200 ARRAY_SIZE(ignore_read_file_id
));
201 BUILD_BUG_ON(ARRAY_SIZE(kernel_read_file_str
) <
202 ARRAY_SIZE(ignore_read_file_id
));
204 for (i
= 0; i
< ARRAY_SIZE(exclude_read_files
); i
++) {
205 cur
= exclude_read_files
[i
];
211 for (j
= 0; j
< ARRAY_SIZE(ignore_read_file_id
); j
++) {
212 if (strcmp(cur
, kernel_read_file_str
[j
]) == 0) {
213 pr_info("excluding: %s\n",
214 kernel_read_file_str
[j
]);
215 ignore_read_file_id
[j
] = 1;
217 * Can not break, because one read_file_str
218 * may map to more than on read_file_id.
225 static int __init
loadpin_init(void)
227 pr_info("ready to pin (currently %senforcing)\n",
228 enforce
? "" : "not ");
230 security_add_hooks(loadpin_hooks
, ARRAY_SIZE(loadpin_hooks
), "loadpin");
234 DEFINE_LSM(loadpin
) = {
236 .init
= loadpin_init
,
239 /* Should not be mutable after boot, so not listed in sysfs (perm == 0). */
240 module_param(enforce
, int, 0);
241 MODULE_PARM_DESC(enforce
, "Enforce module/firmware pinning");
242 module_param_array_named(exclude
, exclude_read_files
, charp
, NULL
, 0);
243 MODULE_PARM_DESC(exclude
, "Exclude pinning specific read file types");