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/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> 23 24 static void report_load(const char *origin, struct file *file, char *operation) 25 { 26 char *cmdline, *pathname; 27 28 pathname = kstrdup_quotable_file(file, GFP_KERNEL); 29 cmdline = kstrdup_quotable_cmdline(current, GFP_KERNEL); 30 31 pr_notice("%s %s obj=%s%s%s pid=%d cmdline=%s%s%s\n", 32 origin, operation, 33 (pathname && pathname[0] != '<') ? "\"" : "", 34 pathname, 35 (pathname && pathname[0] != '<') ? "\"" : "", 36 task_pid_nr(current), 37 cmdline ? "\"" : "", cmdline, cmdline ? "\"" : ""); 38 39 kfree(cmdline); 40 kfree(pathname); 41 } 42 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; 50 #endif 51 52 #ifdef CONFIG_SYSCTL 53 54 static struct ctl_path loadpin_sysctl_path[] = { 55 { .procname = "kernel", }, 56 { .procname = "loadpin", }, 57 { } 58 }; 59 60 static struct ctl_table loadpin_sysctl_table[] = { 61 { 62 .procname = "enforce", 63 .data = &enforce, 64 .maxlen = sizeof(int), 65 .mode = 0644, 66 .proc_handler = proc_dointvec_minmax, 67 .extra1 = SYSCTL_ZERO, 68 .extra2 = SYSCTL_ONE, 69 }, 70 { } 71 }; 72 73 /* 74 * This must be called after early kernel init, since then the rootdev 75 * is available. 76 */ 77 static void check_pinning_enforcement(struct super_block *mnt_sb) 78 { 79 bool ro = false; 80 81 /* 82 * If load pinning is not enforced via a read-only block 83 * device, allow sysctl to change modes for testing. 84 */ 85 if (mnt_sb->s_bdev) { 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"); 91 } else 92 pr_info("mnt_sb lacks block device, treating as: writable\n"); 93 94 if (!ro) { 95 if (!register_sysctl_paths(loadpin_sysctl_path, 96 loadpin_sysctl_table)) 97 pr_notice("sysctl registration failed!\n"); 98 else 99 pr_info("enforcement can be disabled.\n"); 100 } else 101 pr_info("load pinning engaged.\n"); 102 } 103 #else 104 static void check_pinning_enforcement(struct super_block *mnt_sb) 105 { 106 pr_info("load pinning engaged.\n"); 107 } 108 #endif 109 110 static void loadpin_sb_free_security(struct super_block *mnt_sb) 111 { 112 /* 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. 116 */ 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"); 120 } 121 } 122 123 static int loadpin_read_file(struct file *file, enum kernel_read_file_id id, 124 bool contents) 125 { 126 struct super_block *load_root; 127 const char *origin = kernel_read_file_id_str(id); 128 129 /* 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. 134 */ 135 if (!contents) 136 file = NULL; 137 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"); 142 return 0; 143 } 144 145 /* This handles the older init_module API that has a NULL file. */ 146 if (!file) { 147 if (!enforce) { 148 report_load(origin, NULL, "old-api-pinning-ignored"); 149 return 0; 150 } 151 152 report_load(origin, NULL, "old-api-denied"); 153 return -EPERM; 154 } 155 156 load_root = file->f_path.mnt->mnt_sb; 157 158 /* First loaded module/firmware defines the root for all others. */ 159 spin_lock(&pinned_root_spinlock); 160 /* 161 * pinned_root is only NULL at startup. Otherwise, it is either 162 * a valid reference, or an ERR_PTR. 163 */ 164 if (!pinned_root) { 165 pinned_root = load_root; 166 /* 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. 171 */ 172 spin_unlock(&pinned_root_spinlock); 173 check_pinning_enforcement(pinned_root); 174 report_load(origin, file, "pinned"); 175 } else { 176 spin_unlock(&pinned_root_spinlock); 177 } 178 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"); 183 return 0; 184 } 185 186 report_load(origin, file, "denied"); 187 return -EPERM; 188 } 189 190 return 0; 191 } 192 193 static int loadpin_load_data(enum kernel_load_data_id id, bool contents) 194 { 195 return loadpin_read_file(NULL, (enum kernel_read_file_id) id, contents); 196 } 197 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), 202 }; 203 204 static void __init parse_exclude(void) 205 { 206 int i, j; 207 char *cur; 208 209 /* 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. 213 */ 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)); 218 219 for (i = 0; i < ARRAY_SIZE(exclude_read_files); i++) { 220 cur = exclude_read_files[i]; 221 if (!cur) 222 break; 223 if (*cur == '\0') 224 continue; 225 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; 231 /* 232 * Can not break, because one read_file_str 233 * may map to more than on read_file_id. 234 */ 235 } 236 } 237 } 238 } 239 240 static int __init loadpin_init(void) 241 { 242 pr_info("ready to pin (currently %senforcing)\n", 243 enforce ? "" : "not "); 244 parse_exclude(); 245 security_add_hooks(loadpin_hooks, ARRAY_SIZE(loadpin_hooks), "loadpin"); 246 247 return 0; 248 } 249 250 DEFINE_LSM(loadpin) = { 251 .name = "loadpin", 252 .init = loadpin_init, 253 }; 254 255 #ifdef CONFIG_SECURITY_LOADPIN_VERITY 256 257 enum loadpin_securityfs_interface_index { 258 LOADPIN_DM_VERITY, 259 }; 260 261 static int read_trusted_verity_root_digests(unsigned int fd) 262 { 263 struct fd f; 264 void *data; 265 int rc; 266 char *p, *d; 267 268 if (deny_reading_verity_digests) 269 return -EPERM; 270 271 /* The list of trusted root digests can only be set up once */ 272 if (!list_empty(&dm_verity_loadpin_trusted_root_digests)) 273 return -EPERM; 274 275 f = fdget(fd); 276 if (!f.file) 277 return -EINVAL; 278 279 data = kzalloc(SZ_4K, GFP_KERNEL); 280 if (!data) { 281 rc = -ENOMEM; 282 goto err; 283 } 284 285 rc = kernel_read_file(f.file, 0, (void **)&data, SZ_4K - 1, NULL, READING_POLICY); 286 if (rc < 0) 287 goto err; 288 289 p = data; 290 p[rc] = '\0'; 291 p = strim(p); 292 293 p = strim(data); 294 while ((d = strsep(&p, "\n")) != NULL) { 295 int len = strlen(d); 296 struct dm_verity_loadpin_trusted_root_digest *trd; 297 298 if (len % 2) { 299 rc = -EPROTO; 300 goto err; 301 } 302 303 len /= 2; 304 305 trd = kzalloc(struct_size(trd, data, len), GFP_KERNEL); 306 if (!trd) { 307 rc = -ENOMEM; 308 goto err; 309 } 310 311 if (hex2bin(trd->data, d, len)) { 312 kfree(trd); 313 rc = -EPROTO; 314 goto err; 315 } 316 317 trd->len = len; 318 319 list_add_tail(&trd->node, &dm_verity_loadpin_trusted_root_digests); 320 } 321 322 if (list_empty(&dm_verity_loadpin_trusted_root_digests)) { 323 rc = -EPROTO; 324 goto err; 325 } 326 327 kfree(data); 328 fdput(f); 329 330 return 0; 331 332 err: 333 kfree(data); 334 335 /* any failure in loading/parsing invalidates the entire list */ 336 { 337 struct dm_verity_loadpin_trusted_root_digest *trd, *tmp; 338 339 list_for_each_entry_safe(trd, tmp, &dm_verity_loadpin_trusted_root_digests, node) { 340 list_del(&trd->node); 341 kfree(trd); 342 } 343 } 344 345 /* disallow further attempts after reading a corrupt/invalid file */ 346 deny_reading_verity_digests = true; 347 348 fdput(f); 349 350 return rc; 351 } 352 353 /******************************** securityfs ********************************/ 354 355 static long dm_verity_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) 356 { 357 void __user *uarg = (void __user *)arg; 358 unsigned int fd; 359 int rc; 360 361 switch (cmd) { 362 case LOADPIN_IOC_SET_TRUSTED_VERITY_DIGESTS: 363 rc = copy_from_user(&fd, uarg, sizeof(fd)); 364 if (rc) 365 return rc; 366 367 return read_trusted_verity_root_digests(fd); 368 369 default: 370 return -EINVAL; 371 } 372 } 373 374 static const struct file_operations loadpin_dm_verity_ops = { 375 .unlocked_ioctl = dm_verity_ioctl, 376 .compat_ioctl = compat_ptr_ioctl, 377 }; 378 379 /** 380 * init_loadpin_securityfs - create the securityfs directory for LoadPin 381 * 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. 384 * 385 * Returns 0 if the securityfs directory creation was successful. 386 */ 387 static int __init init_loadpin_securityfs(void) 388 { 389 struct dentry *loadpin_dir, *dentry; 390 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); 396 } 397 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", 402 PTR_ERR(dentry)); 403 return PTR_ERR(dentry); 404 } 405 406 return 0; 407 } 408 409 fs_initcall(init_loadpin_securityfs); 410 411 #endif /* CONFIG_SECURITY_LOADPIN_VERITY */ 412 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"); 418