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