Matthew Garrett | 000d388 | 2019-08-19 17:17:39 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* Lock down the kernel |
| 3 | * |
| 4 | * Copyright (C) 2016 Red Hat, Inc. All Rights Reserved. |
| 5 | * Written by David Howells (dhowells@redhat.com) |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public Licence |
| 9 | * as published by the Free Software Foundation; either version |
| 10 | * 2 of the Licence, or (at your option) any later version. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/security.h> |
| 14 | #include <linux/export.h> |
| 15 | #include <linux/lsm_hooks.h> |
| 16 | |
| 17 | static enum lockdown_reason kernel_locked_down; |
| 18 | |
Matthew Garrett | f8a9bc6 | 2019-09-10 03:03:17 -0700 | [diff] [blame] | 19 | static const char *const lockdown_reasons[LOCKDOWN_CONFIDENTIALITY_MAX+1] = { |
Matthew Garrett | 000d388 | 2019-08-19 17:17:39 -0700 | [diff] [blame] | 20 | [LOCKDOWN_NONE] = "none", |
David Howells | 49fcf73 | 2019-08-19 17:17:40 -0700 | [diff] [blame] | 21 | [LOCKDOWN_MODULE_SIGNATURE] = "unsigned module loading", |
Matthew Garrett | 9b9d8dd | 2019-08-19 17:17:41 -0700 | [diff] [blame] | 22 | [LOCKDOWN_DEV_MEM] = "/dev/mem,kmem,port", |
Matthew Garrett | 7d31f46 | 2019-08-19 17:17:42 -0700 | [diff] [blame] | 23 | [LOCKDOWN_KEXEC] = "kexec of unsigned images", |
Josh Boyer | 38bd94b | 2019-08-19 17:17:46 -0700 | [diff] [blame] | 24 | [LOCKDOWN_HIBERNATION] = "hibernation", |
Matthew Garrett | eb627e1 | 2019-08-19 17:17:47 -0700 | [diff] [blame] | 25 | [LOCKDOWN_PCI_ACCESS] = "direct PCI access", |
Matthew Garrett | 96c4f67 | 2019-08-19 17:17:48 -0700 | [diff] [blame] | 26 | [LOCKDOWN_IOPORT] = "raw io port access", |
Matthew Garrett | 95f5e95 | 2019-08-19 17:17:49 -0700 | [diff] [blame] | 27 | [LOCKDOWN_MSR] = "raw MSR access", |
Matthew Garrett | f474e14 | 2019-08-19 17:17:50 -0700 | [diff] [blame] | 28 | [LOCKDOWN_ACPI_TABLES] = "modifying ACPI tables", |
David Howells | 3f19cad | 2019-08-19 17:17:53 -0700 | [diff] [blame] | 29 | [LOCKDOWN_PCMCIA_CIS] = "direct PCMCIA CIS storage", |
David Howells | 794edf3 | 2019-08-19 17:17:54 -0700 | [diff] [blame] | 30 | [LOCKDOWN_TIOCSSERIAL] = "reconfiguration of serial port IO", |
David Howells | 20657f6 | 2019-08-19 17:17:55 -0700 | [diff] [blame] | 31 | [LOCKDOWN_MODULE_PARAMETERS] = "unsafe module parameters", |
David Howells | 906357f | 2019-08-19 17:17:56 -0700 | [diff] [blame] | 32 | [LOCKDOWN_MMIOTRACE] = "unsafe mmio", |
David Howells | 5496197 | 2019-08-19 17:18:02 -0700 | [diff] [blame] | 33 | [LOCKDOWN_DEBUGFS] = "debugfs access", |
Matthew Garrett | 000d388 | 2019-08-19 17:17:39 -0700 | [diff] [blame] | 34 | [LOCKDOWN_INTEGRITY_MAX] = "integrity", |
David Howells | 02e935b | 2019-08-19 17:17:57 -0700 | [diff] [blame] | 35 | [LOCKDOWN_KCORE] = "/proc/kcore access", |
David Howells | a94549d | 2019-08-19 17:17:58 -0700 | [diff] [blame] | 36 | [LOCKDOWN_KPROBES] = "use of kprobes", |
David Howells | 9d1f8be5 | 2019-08-19 17:17:59 -0700 | [diff] [blame] | 37 | [LOCKDOWN_BPF_READ] = "use of bpf to read kernel RAM", |
David Howells | b0c8fdc | 2019-08-19 17:18:00 -0700 | [diff] [blame] | 38 | [LOCKDOWN_PERF] = "unsafe use of perf", |
Matthew Garrett | ccbd54f | 2019-08-19 17:18:03 -0700 | [diff] [blame] | 39 | [LOCKDOWN_TRACEFS] = "use of tracefs", |
Matthew Garrett | 000d388 | 2019-08-19 17:17:39 -0700 | [diff] [blame] | 40 | [LOCKDOWN_CONFIDENTIALITY_MAX] = "confidentiality", |
| 41 | }; |
| 42 | |
Matthew Garrett | f8a9bc6 | 2019-09-10 03:03:17 -0700 | [diff] [blame] | 43 | static const enum lockdown_reason lockdown_levels[] = {LOCKDOWN_NONE, |
Matthew Garrett | 000d388 | 2019-08-19 17:17:39 -0700 | [diff] [blame] | 44 | LOCKDOWN_INTEGRITY_MAX, |
| 45 | LOCKDOWN_CONFIDENTIALITY_MAX}; |
| 46 | |
| 47 | /* |
| 48 | * Put the kernel into lock-down mode. |
| 49 | */ |
| 50 | static int lock_kernel_down(const char *where, enum lockdown_reason level) |
| 51 | { |
| 52 | if (kernel_locked_down >= level) |
| 53 | return -EPERM; |
| 54 | |
| 55 | kernel_locked_down = level; |
| 56 | pr_notice("Kernel is locked down from %s; see man kernel_lockdown.7\n", |
| 57 | where); |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | static int __init lockdown_param(char *level) |
| 62 | { |
| 63 | if (!level) |
| 64 | return -EINVAL; |
| 65 | |
| 66 | if (strcmp(level, "integrity") == 0) |
| 67 | lock_kernel_down("command line", LOCKDOWN_INTEGRITY_MAX); |
| 68 | else if (strcmp(level, "confidentiality") == 0) |
| 69 | lock_kernel_down("command line", LOCKDOWN_CONFIDENTIALITY_MAX); |
| 70 | else |
| 71 | return -EINVAL; |
| 72 | |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | early_param("lockdown", lockdown_param); |
| 77 | |
| 78 | /** |
| 79 | * lockdown_is_locked_down - Find out if the kernel is locked down |
| 80 | * @what: Tag to use in notice generated if lockdown is in effect |
| 81 | */ |
| 82 | static int lockdown_is_locked_down(enum lockdown_reason what) |
| 83 | { |
Matthew Garrett | b602614 | 2019-08-19 17:18:05 -0700 | [diff] [blame] | 84 | if (WARN(what >= LOCKDOWN_CONFIDENTIALITY_MAX, |
| 85 | "Invalid lockdown reason")) |
| 86 | return -EPERM; |
| 87 | |
Matthew Garrett | 000d388 | 2019-08-19 17:17:39 -0700 | [diff] [blame] | 88 | if (kernel_locked_down >= what) { |
| 89 | if (lockdown_reasons[what]) |
Matthew Garrett | b602614 | 2019-08-19 17:18:05 -0700 | [diff] [blame] | 90 | pr_notice("Lockdown: %s: %s is restricted; see man kernel_lockdown.7\n", |
| 91 | current->comm, lockdown_reasons[what]); |
Matthew Garrett | 000d388 | 2019-08-19 17:17:39 -0700 | [diff] [blame] | 92 | return -EPERM; |
| 93 | } |
| 94 | |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | static struct security_hook_list lockdown_hooks[] __lsm_ro_after_init = { |
| 99 | LSM_HOOK_INIT(locked_down, lockdown_is_locked_down), |
| 100 | }; |
| 101 | |
| 102 | static int __init lockdown_lsm_init(void) |
| 103 | { |
| 104 | #if defined(CONFIG_LOCK_DOWN_KERNEL_FORCE_INTEGRITY) |
| 105 | lock_kernel_down("Kernel configuration", LOCKDOWN_INTEGRITY_MAX); |
| 106 | #elif defined(CONFIG_LOCK_DOWN_KERNEL_FORCE_CONFIDENTIALITY) |
| 107 | lock_kernel_down("Kernel configuration", LOCKDOWN_CONFIDENTIALITY_MAX); |
| 108 | #endif |
| 109 | security_add_hooks(lockdown_hooks, ARRAY_SIZE(lockdown_hooks), |
| 110 | "lockdown"); |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | static ssize_t lockdown_read(struct file *filp, char __user *buf, size_t count, |
| 115 | loff_t *ppos) |
| 116 | { |
| 117 | char temp[80]; |
| 118 | int i, offset = 0; |
| 119 | |
| 120 | for (i = 0; i < ARRAY_SIZE(lockdown_levels); i++) { |
| 121 | enum lockdown_reason level = lockdown_levels[i]; |
| 122 | |
| 123 | if (lockdown_reasons[level]) { |
| 124 | const char *label = lockdown_reasons[level]; |
| 125 | |
| 126 | if (kernel_locked_down == level) |
| 127 | offset += sprintf(temp+offset, "[%s] ", label); |
| 128 | else |
| 129 | offset += sprintf(temp+offset, "%s ", label); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | /* Convert the last space to a newline if needed. */ |
| 134 | if (offset > 0) |
| 135 | temp[offset-1] = '\n'; |
| 136 | |
| 137 | return simple_read_from_buffer(buf, count, ppos, temp, strlen(temp)); |
| 138 | } |
| 139 | |
| 140 | static ssize_t lockdown_write(struct file *file, const char __user *buf, |
| 141 | size_t n, loff_t *ppos) |
| 142 | { |
| 143 | char *state; |
| 144 | int i, len, err = -EINVAL; |
| 145 | |
| 146 | state = memdup_user_nul(buf, n); |
| 147 | if (IS_ERR(state)) |
| 148 | return PTR_ERR(state); |
| 149 | |
| 150 | len = strlen(state); |
| 151 | if (len && state[len-1] == '\n') { |
| 152 | state[len-1] = '\0'; |
| 153 | len--; |
| 154 | } |
| 155 | |
| 156 | for (i = 0; i < ARRAY_SIZE(lockdown_levels); i++) { |
| 157 | enum lockdown_reason level = lockdown_levels[i]; |
| 158 | const char *label = lockdown_reasons[level]; |
| 159 | |
| 160 | if (label && !strcmp(state, label)) |
| 161 | err = lock_kernel_down("securityfs", level); |
| 162 | } |
| 163 | |
| 164 | kfree(state); |
| 165 | return err ? err : n; |
| 166 | } |
| 167 | |
| 168 | static const struct file_operations lockdown_ops = { |
| 169 | .read = lockdown_read, |
| 170 | .write = lockdown_write, |
| 171 | }; |
| 172 | |
| 173 | static int __init lockdown_secfs_init(void) |
| 174 | { |
| 175 | struct dentry *dentry; |
| 176 | |
| 177 | dentry = securityfs_create_file("lockdown", 0600, NULL, NULL, |
| 178 | &lockdown_ops); |
| 179 | return PTR_ERR_OR_ZERO(dentry); |
| 180 | } |
| 181 | |
| 182 | core_initcall(lockdown_secfs_init); |
| 183 | |
| 184 | #ifdef CONFIG_SECURITY_LOCKDOWN_LSM_EARLY |
| 185 | DEFINE_EARLY_LSM(lockdown) = { |
| 186 | #else |
| 187 | DEFINE_LSM(lockdown) = { |
| 188 | #endif |
| 189 | .name = "lockdown", |
| 190 | .init = lockdown_lsm_init, |
| 191 | }; |