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