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