GNU Linux-libre 5.4.241-gnu1
[releases.git] / security / lockdown / lockdown.c
1 // 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
17 static enum lockdown_reason kernel_locked_down;
18
19 static const char *const lockdown_reasons[LOCKDOWN_CONFIDENTIALITY_MAX+1] = {
20         [LOCKDOWN_NONE] = "none",
21         [LOCKDOWN_MODULE_SIGNATURE] = "unsigned module loading",
22         [LOCKDOWN_DEV_MEM] = "/dev/mem,kmem,port",
23         [LOCKDOWN_EFI_TEST] = "/dev/efi_test access",
24         [LOCKDOWN_KEXEC] = "kexec of unsigned images",
25         [LOCKDOWN_HIBERNATION] = "hibernation",
26         [LOCKDOWN_PCI_ACCESS] = "direct PCI access",
27         [LOCKDOWN_IOPORT] = "raw io port access",
28         [LOCKDOWN_MSR] = "raw MSR access",
29         [LOCKDOWN_ACPI_TABLES] = "modifying ACPI tables",
30         [LOCKDOWN_PCMCIA_CIS] = "direct PCMCIA CIS storage",
31         [LOCKDOWN_TIOCSSERIAL] = "reconfiguration of serial port IO",
32         [LOCKDOWN_MODULE_PARAMETERS] = "unsafe module parameters",
33         [LOCKDOWN_MMIOTRACE] = "unsafe mmio",
34         [LOCKDOWN_DEBUGFS] = "debugfs access",
35         [LOCKDOWN_XMON_WR] = "xmon write access",
36         [LOCKDOWN_DBG_WRITE_KERNEL] = "use of kgdb/kdb to write kernel RAM",
37         [LOCKDOWN_INTEGRITY_MAX] = "integrity",
38         [LOCKDOWN_KCORE] = "/proc/kcore access",
39         [LOCKDOWN_KPROBES] = "use of kprobes",
40         [LOCKDOWN_BPF_READ] = "use of bpf to read kernel RAM",
41         [LOCKDOWN_DBG_READ_KERNEL] = "use of kgdb/kdb to read kernel RAM",
42         [LOCKDOWN_PERF] = "unsafe use of perf",
43         [LOCKDOWN_TRACEFS] = "use of tracefs",
44         [LOCKDOWN_XMON_RW] = "xmon read and write access",
45         [LOCKDOWN_CONFIDENTIALITY_MAX] = "confidentiality",
46 };
47
48 static const enum lockdown_reason lockdown_levels[] = {LOCKDOWN_NONE,
49                                                  LOCKDOWN_INTEGRITY_MAX,
50                                                  LOCKDOWN_CONFIDENTIALITY_MAX};
51
52 /*
53  * Put the kernel into lock-down mode.
54  */
55 static int lock_kernel_down(const char *where, enum lockdown_reason level)
56 {
57         if (kernel_locked_down >= level)
58                 return -EPERM;
59
60         kernel_locked_down = level;
61         pr_notice("Kernel is locked down from %s; see man kernel_lockdown.7\n",
62                   where);
63         return 0;
64 }
65
66 static int __init lockdown_param(char *level)
67 {
68         if (!level)
69                 return -EINVAL;
70
71         if (strcmp(level, "integrity") == 0)
72                 lock_kernel_down("command line", LOCKDOWN_INTEGRITY_MAX);
73         else if (strcmp(level, "confidentiality") == 0)
74                 lock_kernel_down("command line", LOCKDOWN_CONFIDENTIALITY_MAX);
75         else
76                 return -EINVAL;
77
78         return 0;
79 }
80
81 early_param("lockdown", lockdown_param);
82
83 /**
84  * lockdown_is_locked_down - Find out if the kernel is locked down
85  * @what: Tag to use in notice generated if lockdown is in effect
86  */
87 static int lockdown_is_locked_down(enum lockdown_reason what)
88 {
89         if (WARN(what >= LOCKDOWN_CONFIDENTIALITY_MAX,
90                  "Invalid lockdown reason"))
91                 return -EPERM;
92
93         if (kernel_locked_down >= what) {
94                 if (lockdown_reasons[what])
95                         pr_notice("Lockdown: %s: %s is restricted; see man kernel_lockdown.7\n",
96                                   current->comm, lockdown_reasons[what]);
97                 return -EPERM;
98         }
99
100         return 0;
101 }
102
103 static struct security_hook_list lockdown_hooks[] __lsm_ro_after_init = {
104         LSM_HOOK_INIT(locked_down, lockdown_is_locked_down),
105 };
106
107 static int __init lockdown_lsm_init(void)
108 {
109 #if defined(CONFIG_LOCK_DOWN_KERNEL_FORCE_INTEGRITY)
110         lock_kernel_down("Kernel configuration", LOCKDOWN_INTEGRITY_MAX);
111 #elif defined(CONFIG_LOCK_DOWN_KERNEL_FORCE_CONFIDENTIALITY)
112         lock_kernel_down("Kernel configuration", LOCKDOWN_CONFIDENTIALITY_MAX);
113 #endif
114         security_add_hooks(lockdown_hooks, ARRAY_SIZE(lockdown_hooks),
115                            "lockdown");
116         return 0;
117 }
118
119 static ssize_t lockdown_read(struct file *filp, char __user *buf, size_t count,
120                              loff_t *ppos)
121 {
122         char temp[80];
123         int i, offset = 0;
124
125         for (i = 0; i < ARRAY_SIZE(lockdown_levels); i++) {
126                 enum lockdown_reason level = lockdown_levels[i];
127
128                 if (lockdown_reasons[level]) {
129                         const char *label = lockdown_reasons[level];
130
131                         if (kernel_locked_down == level)
132                                 offset += sprintf(temp+offset, "[%s] ", label);
133                         else
134                                 offset += sprintf(temp+offset, "%s ", label);
135                 }
136         }
137
138         /* Convert the last space to a newline if needed. */
139         if (offset > 0)
140                 temp[offset-1] = '\n';
141
142         return simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
143 }
144
145 static ssize_t lockdown_write(struct file *file, const char __user *buf,
146                               size_t n, loff_t *ppos)
147 {
148         char *state;
149         int i, len, err = -EINVAL;
150
151         state = memdup_user_nul(buf, n);
152         if (IS_ERR(state))
153                 return PTR_ERR(state);
154
155         len = strlen(state);
156         if (len && state[len-1] == '\n') {
157                 state[len-1] = '\0';
158                 len--;
159         }
160
161         for (i = 0; i < ARRAY_SIZE(lockdown_levels); i++) {
162                 enum lockdown_reason level = lockdown_levels[i];
163                 const char *label = lockdown_reasons[level];
164
165                 if (label && !strcmp(state, label))
166                         err = lock_kernel_down("securityfs", level);
167         }
168
169         kfree(state);
170         return err ? err : n;
171 }
172
173 static const struct file_operations lockdown_ops = {
174         .read  = lockdown_read,
175         .write = lockdown_write,
176 };
177
178 static int __init lockdown_secfs_init(void)
179 {
180         struct dentry *dentry;
181
182         dentry = securityfs_create_file("lockdown", 0644, NULL, NULL,
183                                         &lockdown_ops);
184         return PTR_ERR_OR_ZERO(dentry);
185 }
186
187 core_initcall(lockdown_secfs_init);
188
189 #ifdef CONFIG_SECURITY_LOCKDOWN_LSM_EARLY
190 DEFINE_EARLY_LSM(lockdown) = {
191 #else
192 DEFINE_LSM(lockdown) = {
193 #endif
194         .name = "lockdown",
195         .init = lockdown_lsm_init,
196 };