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