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