| 1 | |
| 2 | |
| 3 | * Module and Firmware Pinning Security Module
|
| 4 | *
|
| 5 | * Copyright 2011-2016 Google Inc.
|
| 6 | *
|
| 7 | * Author: Kees Cook <[email protected]>
|
| 8 |
|
| 9 |
|
| 10 | #define pr_fmt(fmt) "LoadPin: " fmt
|
| 11 |
|
| 12 | #include <linux/module.h>
|
| 13 | #include <linux/fs.h>
|
| 14 | #include <linux/hex.h>
|
| 15 | #include <linux/kernel_read_file.h>
|
| 16 | #include <linux/lsm_hooks.h>
|
| 17 | #include <linux/mount.h>
|
| 18 | #include <linux/blkdev.h>
|
| 19 | #include <linux/path.h>
|
| 20 | #include <linux/sched.h>
|
| 21 | #include <linux/string_helpers.h>
|
| 22 | #include <linux/dm-verity-loadpin.h>
|
| 23 | #include <uapi/linux/loadpin.h>
|
| 24 | #include <uapi/linux/lsm.h>
|
| 25 |
|
| 26 | #define VERITY_DIGEST_FILE_HEADER "# LOADPIN_TRUSTED_VERITY_ROOT_DIGESTS"
|
| 27 |
|
| 28 | static void report_load(const char *origin, struct file *file, char *operation)
|
| 29 | {
|
| 30 | char *cmdline, *pathname;
|
| 31 |
|
| 32 | pathname = kstrdup_quotable_file(file, GFP_KERNEL);
|
| 33 | cmdline = kstrdup_quotable_cmdline(current, GFP_KERNEL);
|
| 34 |
|
| 35 | pr_notice("%s %s obj=%s%s%s pid=%d cmdline=%s%s%s\n",
|
| 36 | origin, operation,
|
| 37 | (pathname && pathname[0] != '<') ? "\"" : "",
|
| 38 | pathname,
|
| 39 | (pathname && pathname[0] != '<') ? "\"" : "",
|
| 40 | task_pid_nr(current),
|
| 41 | cmdline ? "\"" : "", cmdline, cmdline ? "\"" : "");
|
| 42 |
|
| 43 | kfree(cmdline);
|
| 44 | kfree(pathname);
|
| 45 | }
|
| 46 |
|
| 47 | static int enforce = IS_ENABLED(CONFIG_SECURITY_LOADPIN_ENFORCE);
|
| 48 | static char *exclude_read_files[READING_MAX_ID];
|
| 49 | static int ignore_read_file_id[READING_MAX_ID] __ro_after_init;
|
| 50 | static struct super_block *pinned_root;
|
| 51 | static DEFINE_SPINLOCK(pinned_root_spinlock);
|
| 52 | #ifdef CONFIG_SECURITY_LOADPIN_VERITY
|
| 53 | static bool deny_reading_verity_digests;
|
| 54 | #endif
|
| 55 |
|
| 56 | |
| 57 | static bool loadpin_root_writable;
|
| 58 | #ifdef CONFIG_SYSCTL
|
| 59 |
|
| 60 | static int proc_handler_loadpin(const struct ctl_table *table, int dir,
|
| 61 | void *buffer, size_t *lenp, loff_t *ppos)
|
| 62 | {
|
| 63 | if (!loadpin_root_writable && SYSCTL_USER_TO_KERN(dir))
|
| 64 | return -EINVAL;
|
| 65 | return proc_dointvec_minmax(table, dir, buffer, lenp, ppos);
|
| 66 | }
|
| 67 |
|
| 68 | static const struct ctl_table loadpin_sysctl_table[] = {
|
| 69 | {
|
| 70 | .procname = "enforce",
|
| 71 | .data = &enforce,
|
| 72 | .maxlen = sizeof(int),
|
| 73 | .mode = 0644,
|
| 74 | .proc_handler = proc_handler_loadpin,
|
| 75 | .extra1 = SYSCTL_ZERO,
|
| 76 | .extra2 = SYSCTL_ONE,
|
| 77 | },
|
| 78 | };
|
| 79 | #endif
|
| 80 |
|
| 81 | static void report_writable(struct super_block *mnt_sb, bool writable)
|
| 82 | {
|
| 83 | if (mnt_sb->s_bdev) {
|
| 84 | pr_info("%pg (%u:%u): %s\n", mnt_sb->s_bdev,
|
| 85 | MAJOR(mnt_sb->s_bdev->bd_dev),
|
| 86 | MINOR(mnt_sb->s_bdev->bd_dev),
|
| 87 | writable ? "writable" : "read-only");
|
| 88 | } else
|
| 89 | pr_info("mnt_sb lacks block device, treating as: writable\n");
|
| 90 |
|
| 91 | if (!writable)
|
| 92 | pr_info("load pinning engaged.\n");
|
| 93 | }
|
| 94 |
|
| 95 | |
| 96 | * This must be called after early kernel init, since then the rootdev
|
| 97 | * is available.
|
| 98 |
|
| 99 | static bool sb_is_writable(struct super_block *mnt_sb)
|
| 100 | {
|
| 101 | bool writable = true;
|
| 102 |
|
| 103 | if (mnt_sb->s_bdev)
|
| 104 | writable = !bdev_read_only(mnt_sb->s_bdev);
|
| 105 |
|
| 106 | return writable;
|
| 107 | }
|
| 108 |
|
| 109 | static void loadpin_sb_free_security(struct super_block *mnt_sb)
|
| 110 | {
|
| 111 | |
| 112 | * When unmounting the filesystem we were using for load
|
| 113 | * pinning, we acknowledge the superblock release, but make sure
|
| 114 | * no other modules or firmware can be loaded when we are in
|
| 115 | * enforcing mode. Otherwise, allow the root to be reestablished.
|
| 116 |
|
| 117 | if (!IS_ERR_OR_NULL(pinned_root) && mnt_sb == pinned_root) {
|
| 118 | if (enforce) {
|
| 119 | pinned_root = ERR_PTR(-EIO);
|
| 120 | pr_info("umount pinned fs: refusing further loads\n");
|
| 121 | } else {
|
| 122 | pinned_root = NULL;
|
| 123 | }
|
| 124 | }
|
| 125 | }
|
| 126 |
|
| 127 | static int loadpin_check(struct file *file, enum kernel_read_file_id id)
|
| 128 | {
|
| 129 | struct super_block *load_root;
|
| 130 | const char *origin = kernel_read_file_id_str(id);
|
| 131 | bool first_root_pin = false;
|
| 132 |
|
| 133 |
|
| 134 | if ((unsigned int)id < ARRAY_SIZE(ignore_read_file_id) &&
|
| 135 | ignore_read_file_id[id]) {
|
| 136 | report_load(origin, file, "pinning-excluded");
|
| 137 | return 0;
|
| 138 | }
|
| 139 |
|
| 140 |
|
| 141 | if (!file) {
|
| 142 | if (!enforce) {
|
| 143 | report_load(origin, NULL, "old-api-pinning-ignored");
|
| 144 | return 0;
|
| 145 | }
|
| 146 |
|
| 147 | report_load(origin, NULL, "old-api-denied");
|
| 148 | return -EPERM;
|
| 149 | }
|
| 150 |
|
| 151 | load_root = file->f_path.mnt->mnt_sb;
|
| 152 |
|
| 153 |
|
| 154 | spin_lock(&pinned_root_spinlock);
|
| 155 | |
| 156 | * pinned_root is only NULL at startup or when the pinned root has
|
| 157 | * been unmounted while we are not in enforcing mode. Otherwise, it
|
| 158 | * is either a valid reference, or an ERR_PTR.
|
| 159 |
|
| 160 | if (!pinned_root) {
|
| 161 | pinned_root = load_root;
|
| 162 | first_root_pin = true;
|
| 163 | }
|
| 164 | spin_unlock(&pinned_root_spinlock);
|
| 165 |
|
| 166 | if (first_root_pin) {
|
| 167 | loadpin_root_writable = sb_is_writable(pinned_root);
|
| 168 | report_writable(pinned_root, loadpin_root_writable);
|
| 169 | report_load(origin, file, "pinned");
|
| 170 | }
|
| 171 |
|
| 172 | if (IS_ERR_OR_NULL(pinned_root) ||
|
| 173 | ((load_root != pinned_root) && !dm_verity_loadpin_is_bdev_trusted(load_root->s_bdev))) {
|
| 174 | if (unlikely(!enforce)) {
|
| 175 | report_load(origin, file, "pinning-ignored");
|
| 176 | return 0;
|
| 177 | }
|
| 178 |
|
| 179 | report_load(origin, file, "denied");
|
| 180 | return -EPERM;
|
| 181 | }
|
| 182 |
|
| 183 | return 0;
|
| 184 | }
|
| 185 |
|
| 186 | static int loadpin_read_file(struct file *file, enum kernel_read_file_id id,
|
| 187 | bool contents)
|
| 188 | {
|
| 189 | |
| 190 | * LoadPin only cares about the _origin_ of a file, not its
|
| 191 | * contents, so we can ignore the "are full contents available"
|
| 192 | * argument here.
|
| 193 |
|
| 194 | return loadpin_check(file, id);
|
| 195 | }
|
| 196 |
|
| 197 | static int loadpin_load_data(enum kernel_load_data_id id, bool contents)
|
| 198 | {
|
| 199 | |
| 200 | * LoadPin only cares about the _origin_ of a file, not its
|
| 201 | * contents, so a NULL file is passed, and we can ignore the
|
| 202 | * state of "contents".
|
| 203 |
|
| 204 | return loadpin_check(NULL, (enum kernel_read_file_id) id);
|
| 205 | }
|
| 206 |
|
| 207 | static const struct lsm_id loadpin_lsmid = {
|
| 208 | .name = "loadpin",
|
| 209 | .id = LSM_ID_LOADPIN,
|
| 210 | };
|
| 211 |
|
| 212 | static struct security_hook_list loadpin_hooks[] __ro_after_init = {
|
| 213 | LSM_HOOK_INIT(sb_free_security, loadpin_sb_free_security),
|
| 214 | LSM_HOOK_INIT(kernel_read_file, loadpin_read_file),
|
| 215 | LSM_HOOK_INIT(kernel_load_data, loadpin_load_data),
|
| 216 | };
|
| 217 |
|
| 218 | static void __init parse_exclude(void)
|
| 219 | {
|
| 220 | int i, j;
|
| 221 | char *cur;
|
| 222 |
|
| 223 | |
| 224 | * Make sure all the arrays stay within expected sizes. This
|
| 225 | * is slightly weird because kernel_read_file_str[] includes
|
| 226 | * READING_MAX_ID, which isn't actually meaningful here.
|
| 227 |
|
| 228 | BUILD_BUG_ON(ARRAY_SIZE(exclude_read_files) !=
|
| 229 | ARRAY_SIZE(ignore_read_file_id));
|
| 230 | BUILD_BUG_ON(ARRAY_SIZE(kernel_read_file_str) <
|
| 231 | ARRAY_SIZE(ignore_read_file_id));
|
| 232 |
|
| 233 | for (i = 0; i < ARRAY_SIZE(exclude_read_files); i++) {
|
| 234 | cur = exclude_read_files[i];
|
| 235 | if (!cur)
|
| 236 | break;
|
| 237 | if (*cur == '\0')
|
| 238 | continue;
|
| 239 |
|
| 240 | for (j = 0; j < ARRAY_SIZE(ignore_read_file_id); j++) {
|
| 241 | if (strcmp(cur, kernel_read_file_str[j]) == 0) {
|
| 242 | pr_info("excluding: %s\n",
|
| 243 | kernel_read_file_str[j]);
|
| 244 | ignore_read_file_id[j] = 1;
|
| 245 | |
| 246 | * Can not break, because one read_file_str
|
| 247 | * may map to more than on read_file_id.
|
| 248 |
|
| 249 | }
|
| 250 | }
|
| 251 | }
|
| 252 | }
|
| 253 |
|
| 254 | static int __init loadpin_init(void)
|
| 255 | {
|
| 256 | pr_info("ready to pin (currently %senforcing)\n",
|
| 257 | enforce ? "" : "not ");
|
| 258 | parse_exclude();
|
| 259 | #ifdef CONFIG_SYSCTL
|
| 260 | if (!register_sysctl("kernel/loadpin", loadpin_sysctl_table))
|
| 261 | pr_notice("sysctl registration failed!\n");
|
| 262 | #endif
|
| 263 | security_add_hooks(loadpin_hooks, ARRAY_SIZE(loadpin_hooks),
|
| 264 | &loadpin_lsmid);
|
| 265 |
|
| 266 | return 0;
|
| 267 | }
|
| 268 |
|
| 269 | #ifdef CONFIG_SECURITY_LOADPIN_VERITY
|
| 270 |
|
| 271 | enum loadpin_securityfs_interface_index {
|
| 272 | LOADPIN_DM_VERITY,
|
| 273 | };
|
| 274 |
|
| 275 | static int read_trusted_verity_root_digests(unsigned int fd)
|
| 276 | {
|
| 277 | void *data;
|
| 278 | int rc;
|
| 279 | char *p, *d;
|
| 280 |
|
| 281 | if (deny_reading_verity_digests)
|
| 282 | return -EPERM;
|
| 283 |
|
| 284 |
|
| 285 | if (!list_empty(&dm_verity_loadpin_trusted_root_digests))
|
| 286 | return -EPERM;
|
| 287 |
|
| 288 | CLASS(fd, f)(fd);
|
| 289 | if (fd_empty(f))
|
| 290 | return -EINVAL;
|
| 291 |
|
| 292 | data = kzalloc(SZ_4K, GFP_KERNEL);
|
| 293 | if (!data) {
|
| 294 | rc = -ENOMEM;
|
| 295 | goto err;
|
| 296 | }
|
| 297 |
|
| 298 | rc = kernel_read_file(fd_file(f), 0, (void **)&data, SZ_4K - 1, NULL, READING_POLICY);
|
| 299 | if (rc < 0)
|
| 300 | goto err;
|
| 301 |
|
| 302 | p = data;
|
| 303 | p[rc] = '\0';
|
| 304 | p = strim(p);
|
| 305 |
|
| 306 | p = strim(data);
|
| 307 | while ((d = strsep(&p, "\n")) != NULL) {
|
| 308 | int len;
|
| 309 | struct dm_verity_loadpin_trusted_root_digest *trd;
|
| 310 |
|
| 311 | if (d == data) {
|
| 312 |
|
| 313 | if (strcmp(d, VERITY_DIGEST_FILE_HEADER)) {
|
| 314 | rc = -EPROTO;
|
| 315 | goto err;
|
| 316 | }
|
| 317 |
|
| 318 | continue;
|
| 319 | }
|
| 320 |
|
| 321 | len = strlen(d);
|
| 322 |
|
| 323 | if (len % 2) {
|
| 324 | rc = -EPROTO;
|
| 325 | goto err;
|
| 326 | }
|
| 327 |
|
| 328 | len /= 2;
|
| 329 |
|
| 330 | trd = kzalloc_flex(*trd, data, len);
|
| 331 | if (!trd) {
|
| 332 | rc = -ENOMEM;
|
| 333 | goto err;
|
| 334 | }
|
| 335 | trd->len = len;
|
| 336 |
|
| 337 | if (hex2bin(trd->data, d, len)) {
|
| 338 | kfree(trd);
|
| 339 | rc = -EPROTO;
|
| 340 | goto err;
|
| 341 | }
|
| 342 |
|
| 343 | list_add_tail(&trd->node, &dm_verity_loadpin_trusted_root_digests);
|
| 344 | }
|
| 345 |
|
| 346 | if (list_empty(&dm_verity_loadpin_trusted_root_digests)) {
|
| 347 | rc = -EPROTO;
|
| 348 | goto err;
|
| 349 | }
|
| 350 |
|
| 351 | kfree(data);
|
| 352 |
|
| 353 | return 0;
|
| 354 |
|
| 355 | err:
|
| 356 | kfree(data);
|
| 357 |
|
| 358 |
|
| 359 | {
|
| 360 | struct dm_verity_loadpin_trusted_root_digest *trd, *tmp;
|
| 361 |
|
| 362 | list_for_each_entry_safe(trd, tmp, &dm_verity_loadpin_trusted_root_digests, node) {
|
| 363 | list_del(&trd->node);
|
| 364 | kfree(trd);
|
| 365 | }
|
| 366 | }
|
| 367 |
|
| 368 |
|
| 369 | deny_reading_verity_digests = true;
|
| 370 |
|
| 371 | return rc;
|
| 372 | }
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 | static long dm_verity_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
|
| 377 | {
|
| 378 | void __user *uarg = (void __user *)arg;
|
| 379 | unsigned int fd;
|
| 380 |
|
| 381 | switch (cmd) {
|
| 382 | case LOADPIN_IOC_SET_TRUSTED_VERITY_DIGESTS:
|
| 383 | if (copy_from_user(&fd, uarg, sizeof(fd)))
|
| 384 | return -EFAULT;
|
| 385 |
|
| 386 | return read_trusted_verity_root_digests(fd);
|
| 387 |
|
| 388 | default:
|
| 389 | return -EINVAL;
|
| 390 | }
|
| 391 | }
|
| 392 |
|
| 393 | static const struct file_operations loadpin_dm_verity_ops = {
|
| 394 | .unlocked_ioctl = dm_verity_ioctl,
|
| 395 | .compat_ioctl = compat_ptr_ioctl,
|
| 396 | };
|
| 397 |
|
| 398 | |
| 399 | * init_loadpin_securityfs - create the securityfs directory for LoadPin
|
| 400 | *
|
| 401 | * We can not put this method normally under the loadpin_init() code path since
|
| 402 | * the security subsystem gets initialized before the vfs caches.
|
| 403 | *
|
| 404 | * Returns 0 if the securityfs directory creation was successful.
|
| 405 |
|
| 406 | static int __init init_loadpin_securityfs(void)
|
| 407 | {
|
| 408 | struct dentry *loadpin_dir, *dentry;
|
| 409 |
|
| 410 | loadpin_dir = securityfs_create_dir("loadpin", NULL);
|
| 411 | if (IS_ERR(loadpin_dir)) {
|
| 412 | pr_err("LoadPin: could not create securityfs dir: %ld\n",
|
| 413 | PTR_ERR(loadpin_dir));
|
| 414 | return PTR_ERR(loadpin_dir);
|
| 415 | }
|
| 416 |
|
| 417 | dentry = securityfs_create_file("dm-verity", 0600, loadpin_dir,
|
| 418 | (void *)LOADPIN_DM_VERITY, &loadpin_dm_verity_ops);
|
| 419 | if (IS_ERR(dentry)) {
|
| 420 | pr_err("LoadPin: could not create securityfs entry 'dm-verity': %ld\n",
|
| 421 | PTR_ERR(dentry));
|
| 422 | return PTR_ERR(dentry);
|
| 423 | }
|
| 424 |
|
| 425 | return 0;
|
| 426 | }
|
| 427 |
|
| 428 | #endif
|
| 429 |
|
| 430 | DEFINE_LSM(loadpin) = {
|
| 431 | .id = &loadpin_lsmid,
|
| 432 | .init = loadpin_init,
|
| 433 | #ifdef CONFIG_SECURITY_LOADPIN_VERITY
|
| 434 | .initcall_fs = init_loadpin_securityfs,
|
| 435 | #endif
|
| 436 | };
|
| 437 |
|
| 438 |
|
| 439 | module_param(enforce, int, 0);
|
| 440 | MODULE_PARM_DESC(enforce, "Enforce module/firmware pinning");
|
| 441 | module_param_array_named(exclude, exclude_read_files, charp, NULL, 0);
|
| 442 | MODULE_PARM_DESC(exclude, "Exclude pinning specific read file types");
|