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