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 19 static char *lockdown_reasons[LOCKDOWN_CONFIDENTIALITY_MAX+1] = { 20 [LOCKDOWN_NONE] = "none", 21 [LOCKDOWN_MODULE_SIGNATURE] = "unsigned module loading", 22 [LOCKDOWN_DEV_MEM] = "/dev/mem,kmem,port", 23 [LOCKDOWN_KEXEC] = "kexec of unsigned images", 24 [LOCKDOWN_HIBERNATION] = "hibernation", 25 [LOCKDOWN_PCI_ACCESS] = "direct PCI access", 26 [LOCKDOWN_IOPORT] = "raw io port access", 27 [LOCKDOWN_MSR] = "raw MSR access", 28 [LOCKDOWN_ACPI_TABLES] = "modifying ACPI tables", 29 [LOCKDOWN_PCMCIA_CIS] = "direct PCMCIA CIS storage", 30 [LOCKDOWN_TIOCSSERIAL] = "reconfiguration of serial port IO", 31 [LOCKDOWN_MODULE_PARAMETERS] = "unsafe module parameters", 32 [LOCKDOWN_MMIOTRACE] = "unsafe mmio", 33 [LOCKDOWN_INTEGRITY_MAX] = "integrity", 34 [LOCKDOWN_KCORE] = "/proc/kcore access", 35 [LOCKDOWN_KPROBES] = "use of kprobes", 36 [LOCKDOWN_BPF_READ] = "use of bpf to read kernel RAM", 37 [LOCKDOWN_PERF] = "unsafe use of perf", 38 [LOCKDOWN_CONFIDENTIALITY_MAX] = "confidentiality", 39 }; 40 41 static enum lockdown_reason lockdown_levels[] = {LOCKDOWN_NONE, 42 LOCKDOWN_INTEGRITY_MAX, 43 LOCKDOWN_CONFIDENTIALITY_MAX}; 44 45 /* 46 * Put the kernel into lock-down mode. 47 */ 48 static int lock_kernel_down(const char *where, enum lockdown_reason level) 49 { 50 if (kernel_locked_down >= level) 51 return -EPERM; 52 53 kernel_locked_down = level; 54 pr_notice("Kernel is locked down from %s; see man kernel_lockdown.7\n", 55 where); 56 return 0; 57 } 58 59 static int __init lockdown_param(char *level) 60 { 61 if (!level) 62 return -EINVAL; 63 64 if (strcmp(level, "integrity") == 0) 65 lock_kernel_down("command line", LOCKDOWN_INTEGRITY_MAX); 66 else if (strcmp(level, "confidentiality") == 0) 67 lock_kernel_down("command line", LOCKDOWN_CONFIDENTIALITY_MAX); 68 else 69 return -EINVAL; 70 71 return 0; 72 } 73 74 early_param("lockdown", lockdown_param); 75 76 /** 77 * lockdown_is_locked_down - Find out if the kernel is locked down 78 * @what: Tag to use in notice generated if lockdown is in effect 79 */ 80 static int lockdown_is_locked_down(enum lockdown_reason what) 81 { 82 if (kernel_locked_down >= what) { 83 if (lockdown_reasons[what]) 84 pr_notice("Lockdown: %s is restricted; see man kernel_lockdown.7\n", 85 lockdown_reasons[what]); 86 return -EPERM; 87 } 88 89 return 0; 90 } 91 92 static struct security_hook_list lockdown_hooks[] __lsm_ro_after_init = { 93 LSM_HOOK_INIT(locked_down, lockdown_is_locked_down), 94 }; 95 96 static int __init lockdown_lsm_init(void) 97 { 98 #if defined(CONFIG_LOCK_DOWN_KERNEL_FORCE_INTEGRITY) 99 lock_kernel_down("Kernel configuration", LOCKDOWN_INTEGRITY_MAX); 100 #elif defined(CONFIG_LOCK_DOWN_KERNEL_FORCE_CONFIDENTIALITY) 101 lock_kernel_down("Kernel configuration", LOCKDOWN_CONFIDENTIALITY_MAX); 102 #endif 103 security_add_hooks(lockdown_hooks, ARRAY_SIZE(lockdown_hooks), 104 "lockdown"); 105 return 0; 106 } 107 108 static ssize_t lockdown_read(struct file *filp, char __user *buf, size_t count, 109 loff_t *ppos) 110 { 111 char temp[80]; 112 int i, offset = 0; 113 114 for (i = 0; i < ARRAY_SIZE(lockdown_levels); i++) { 115 enum lockdown_reason level = lockdown_levels[i]; 116 117 if (lockdown_reasons[level]) { 118 const char *label = lockdown_reasons[level]; 119 120 if (kernel_locked_down == level) 121 offset += sprintf(temp+offset, "[%s] ", label); 122 else 123 offset += sprintf(temp+offset, "%s ", label); 124 } 125 } 126 127 /* Convert the last space to a newline if needed. */ 128 if (offset > 0) 129 temp[offset-1] = '\n'; 130 131 return simple_read_from_buffer(buf, count, ppos, temp, strlen(temp)); 132 } 133 134 static ssize_t lockdown_write(struct file *file, const char __user *buf, 135 size_t n, loff_t *ppos) 136 { 137 char *state; 138 int i, len, err = -EINVAL; 139 140 state = memdup_user_nul(buf, n); 141 if (IS_ERR(state)) 142 return PTR_ERR(state); 143 144 len = strlen(state); 145 if (len && state[len-1] == '\n') { 146 state[len-1] = '\0'; 147 len--; 148 } 149 150 for (i = 0; i < ARRAY_SIZE(lockdown_levels); i++) { 151 enum lockdown_reason level = lockdown_levels[i]; 152 const char *label = lockdown_reasons[level]; 153 154 if (label && !strcmp(state, label)) 155 err = lock_kernel_down("securityfs", level); 156 } 157 158 kfree(state); 159 return err ? err : n; 160 } 161 162 static const struct file_operations lockdown_ops = { 163 .read = lockdown_read, 164 .write = lockdown_write, 165 }; 166 167 static int __init lockdown_secfs_init(void) 168 { 169 struct dentry *dentry; 170 171 dentry = securityfs_create_file("lockdown", 0600, NULL, NULL, 172 &lockdown_ops); 173 return PTR_ERR_OR_ZERO(dentry); 174 } 175 176 core_initcall(lockdown_secfs_init); 177 178 #ifdef CONFIG_SECURITY_LOCKDOWN_LSM_EARLY 179 DEFINE_EARLY_LSM(lockdown) = { 180 #else 181 DEFINE_LSM(lockdown) = { 182 #endif 183 .name = "lockdown", 184 .init = lockdown_lsm_init, 185 }; 186