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