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