blob: 40b790536defaf8dcdad0655d4423f33c94dcd06 [file] [log] [blame]
Matthew Garrett000d3882019-08-19 17:17:39 -07001// 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
17static enum lockdown_reason kernel_locked_down;
18
Matthew Garrettf8a9bc62019-09-10 03:03:17 -070019static const char *const lockdown_reasons[LOCKDOWN_CONFIDENTIALITY_MAX+1] = {
Matthew Garrett000d3882019-08-19 17:17:39 -070020 [LOCKDOWN_NONE] = "none",
David Howells49fcf732019-08-19 17:17:40 -070021 [LOCKDOWN_MODULE_SIGNATURE] = "unsigned module loading",
Matthew Garrett9b9d8dd2019-08-19 17:17:41 -070022 [LOCKDOWN_DEV_MEM] = "/dev/mem,kmem,port",
Javier Martinez Canillas359efcc2019-10-29 18:37:55 +010023 [LOCKDOWN_EFI_TEST] = "/dev/efi_test access",
Matthew Garrett7d31f462019-08-19 17:17:42 -070024 [LOCKDOWN_KEXEC] = "kexec of unsigned images",
Josh Boyer38bd94b2019-08-19 17:17:46 -070025 [LOCKDOWN_HIBERNATION] = "hibernation",
Matthew Garretteb627e12019-08-19 17:17:47 -070026 [LOCKDOWN_PCI_ACCESS] = "direct PCI access",
Matthew Garrett96c4f672019-08-19 17:17:48 -070027 [LOCKDOWN_IOPORT] = "raw io port access",
Matthew Garrett95f5e952019-08-19 17:17:49 -070028 [LOCKDOWN_MSR] = "raw MSR access",
Matthew Garrettf474e142019-08-19 17:17:50 -070029 [LOCKDOWN_ACPI_TABLES] = "modifying ACPI tables",
David Howells3f19cad2019-08-19 17:17:53 -070030 [LOCKDOWN_PCMCIA_CIS] = "direct PCMCIA CIS storage",
David Howells794edf32019-08-19 17:17:54 -070031 [LOCKDOWN_TIOCSSERIAL] = "reconfiguration of serial port IO",
David Howells20657f62019-08-19 17:17:55 -070032 [LOCKDOWN_MODULE_PARAMETERS] = "unsafe module parameters",
David Howells906357f2019-08-19 17:17:56 -070033 [LOCKDOWN_MMIOTRACE] = "unsafe mmio",
David Howells54961972019-08-19 17:18:02 -070034 [LOCKDOWN_DEBUGFS] = "debugfs access",
Matthew Garrett000d3882019-08-19 17:17:39 -070035 [LOCKDOWN_INTEGRITY_MAX] = "integrity",
David Howells02e935b2019-08-19 17:17:57 -070036 [LOCKDOWN_KCORE] = "/proc/kcore access",
David Howellsa94549d2019-08-19 17:17:58 -070037 [LOCKDOWN_KPROBES] = "use of kprobes",
David Howells9d1f8be52019-08-19 17:17:59 -070038 [LOCKDOWN_BPF_READ] = "use of bpf to read kernel RAM",
David Howellsb0c8fdc2019-08-19 17:18:00 -070039 [LOCKDOWN_PERF] = "unsafe use of perf",
Matthew Garrettccbd54f2019-08-19 17:18:03 -070040 [LOCKDOWN_TRACEFS] = "use of tracefs",
Matthew Garrett000d3882019-08-19 17:17:39 -070041 [LOCKDOWN_CONFIDENTIALITY_MAX] = "confidentiality",
42};
43
Matthew Garrettf8a9bc62019-09-10 03:03:17 -070044static const enum lockdown_reason lockdown_levels[] = {LOCKDOWN_NONE,
Matthew Garrett000d3882019-08-19 17:17:39 -070045 LOCKDOWN_INTEGRITY_MAX,
46 LOCKDOWN_CONFIDENTIALITY_MAX};
47
48/*
49 * Put the kernel into lock-down mode.
50 */
51static int lock_kernel_down(const char *where, enum lockdown_reason level)
52{
53 if (kernel_locked_down >= level)
54 return -EPERM;
55
56 kernel_locked_down = level;
57 pr_notice("Kernel is locked down from %s; see man kernel_lockdown.7\n",
58 where);
59 return 0;
60}
61
62static int __init lockdown_param(char *level)
63{
64 if (!level)
65 return -EINVAL;
66
67 if (strcmp(level, "integrity") == 0)
68 lock_kernel_down("command line", LOCKDOWN_INTEGRITY_MAX);
69 else if (strcmp(level, "confidentiality") == 0)
70 lock_kernel_down("command line", LOCKDOWN_CONFIDENTIALITY_MAX);
71 else
72 return -EINVAL;
73
74 return 0;
75}
76
77early_param("lockdown", lockdown_param);
78
79/**
80 * lockdown_is_locked_down - Find out if the kernel is locked down
81 * @what: Tag to use in notice generated if lockdown is in effect
82 */
83static int lockdown_is_locked_down(enum lockdown_reason what)
84{
Matthew Garrettb6026142019-08-19 17:18:05 -070085 if (WARN(what >= LOCKDOWN_CONFIDENTIALITY_MAX,
86 "Invalid lockdown reason"))
87 return -EPERM;
88
Matthew Garrett000d3882019-08-19 17:17:39 -070089 if (kernel_locked_down >= what) {
90 if (lockdown_reasons[what])
Matthew Garrettb6026142019-08-19 17:18:05 -070091 pr_notice("Lockdown: %s: %s is restricted; see man kernel_lockdown.7\n",
92 current->comm, lockdown_reasons[what]);
Matthew Garrett000d3882019-08-19 17:17:39 -070093 return -EPERM;
94 }
95
96 return 0;
97}
98
99static struct security_hook_list lockdown_hooks[] __lsm_ro_after_init = {
100 LSM_HOOK_INIT(locked_down, lockdown_is_locked_down),
101};
102
103static int __init lockdown_lsm_init(void)
104{
105#if defined(CONFIG_LOCK_DOWN_KERNEL_FORCE_INTEGRITY)
106 lock_kernel_down("Kernel configuration", LOCKDOWN_INTEGRITY_MAX);
107#elif defined(CONFIG_LOCK_DOWN_KERNEL_FORCE_CONFIDENTIALITY)
108 lock_kernel_down("Kernel configuration", LOCKDOWN_CONFIDENTIALITY_MAX);
109#endif
110 security_add_hooks(lockdown_hooks, ARRAY_SIZE(lockdown_hooks),
111 "lockdown");
112 return 0;
113}
114
115static ssize_t lockdown_read(struct file *filp, char __user *buf, size_t count,
116 loff_t *ppos)
117{
118 char temp[80];
119 int i, offset = 0;
120
121 for (i = 0; i < ARRAY_SIZE(lockdown_levels); i++) {
122 enum lockdown_reason level = lockdown_levels[i];
123
124 if (lockdown_reasons[level]) {
125 const char *label = lockdown_reasons[level];
126
127 if (kernel_locked_down == level)
128 offset += sprintf(temp+offset, "[%s] ", label);
129 else
130 offset += sprintf(temp+offset, "%s ", label);
131 }
132 }
133
134 /* Convert the last space to a newline if needed. */
135 if (offset > 0)
136 temp[offset-1] = '\n';
137
138 return simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
139}
140
141static ssize_t lockdown_write(struct file *file, const char __user *buf,
142 size_t n, loff_t *ppos)
143{
144 char *state;
145 int i, len, err = -EINVAL;
146
147 state = memdup_user_nul(buf, n);
148 if (IS_ERR(state))
149 return PTR_ERR(state);
150
151 len = strlen(state);
152 if (len && state[len-1] == '\n') {
153 state[len-1] = '\0';
154 len--;
155 }
156
157 for (i = 0; i < ARRAY_SIZE(lockdown_levels); i++) {
158 enum lockdown_reason level = lockdown_levels[i];
159 const char *label = lockdown_reasons[level];
160
161 if (label && !strcmp(state, label))
162 err = lock_kernel_down("securityfs", level);
163 }
164
165 kfree(state);
166 return err ? err : n;
167}
168
169static const struct file_operations lockdown_ops = {
170 .read = lockdown_read,
171 .write = lockdown_write,
172};
173
174static int __init lockdown_secfs_init(void)
175{
176 struct dentry *dentry;
177
178 dentry = securityfs_create_file("lockdown", 0600, NULL, NULL,
179 &lockdown_ops);
180 return PTR_ERR_OR_ZERO(dentry);
181}
182
183core_initcall(lockdown_secfs_init);
184
185#ifdef CONFIG_SECURITY_LOCKDOWN_LSM_EARLY
186DEFINE_EARLY_LSM(lockdown) = {
187#else
188DEFINE_LSM(lockdown) = {
189#endif
190 .name = "lockdown",
191 .init = lockdown_lsm_init,
192};