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