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