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