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