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