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