1 // SPDX-License-Identifier: GPL-2.0
2 /* Lock down the kernel
4 * Copyright (C) 2016 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
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.
13 #include <linux/security.h>
14 #include <linux/export.h>
15 #include <linux/lsm_hooks.h>
16 #include <uapi/linux/lsm.h>
18 static enum lockdown_reason kernel_locked_down;
20 static const enum lockdown_reason lockdown_levels[] = {LOCKDOWN_NONE,
21 LOCKDOWN_INTEGRITY_MAX,
22 LOCKDOWN_CONFIDENTIALITY_MAX};
25 * Put the kernel into lock-down mode.
27 static int lock_kernel_down(const char *where, enum lockdown_reason level)
29 if (kernel_locked_down >= level)
32 kernel_locked_down = level;
33 pr_notice("Kernel is locked down from %s; see man kernel_lockdown.7\n",
38 static int __init lockdown_param(char *level)
43 if (strcmp(level, "integrity") == 0)
44 lock_kernel_down("command line", LOCKDOWN_INTEGRITY_MAX);
45 else if (strcmp(level, "confidentiality") == 0)
46 lock_kernel_down("command line", LOCKDOWN_CONFIDENTIALITY_MAX);
53 early_param("lockdown", lockdown_param);
56 * lockdown_is_locked_down - Find out if the kernel is locked down
57 * @what: Tag to use in notice generated if lockdown is in effect
59 static int lockdown_is_locked_down(enum lockdown_reason what)
61 if (WARN(what >= LOCKDOWN_CONFIDENTIALITY_MAX,
62 "Invalid lockdown reason"))
65 if (kernel_locked_down >= what) {
66 if (lockdown_reasons[what])
67 pr_notice_ratelimited("Lockdown: %s: %s is restricted; see man kernel_lockdown.7\n",
68 current->comm, lockdown_reasons[what]);
75 static struct security_hook_list lockdown_hooks[] __ro_after_init = {
76 LSM_HOOK_INIT(locked_down, lockdown_is_locked_down),
79 const struct lsm_id lockdown_lsmid = {
81 .id = LSM_ID_LOCKDOWN,
84 static int __init lockdown_lsm_init(void)
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);
91 security_add_hooks(lockdown_hooks, ARRAY_SIZE(lockdown_hooks),
96 static ssize_t lockdown_read(struct file *filp, char __user *buf, size_t count,
102 for (i = 0; i < ARRAY_SIZE(lockdown_levels); i++) {
103 enum lockdown_reason level = lockdown_levels[i];
105 if (lockdown_reasons[level]) {
106 const char *label = lockdown_reasons[level];
108 if (kernel_locked_down == level)
109 offset += sprintf(temp+offset, "[%s] ", label);
111 offset += sprintf(temp+offset, "%s ", label);
115 /* Convert the last space to a newline if needed. */
117 temp[offset-1] = '\n';
119 return simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
122 static ssize_t lockdown_write(struct file *file, const char __user *buf,
123 size_t n, loff_t *ppos)
126 int i, len, err = -EINVAL;
128 state = memdup_user_nul(buf, n);
130 return PTR_ERR(state);
133 if (len && state[len-1] == '\n') {
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];
142 if (label && !strcmp(state, label))
143 err = lock_kernel_down("securityfs", level);
147 return err ? err : n;
150 static const struct file_operations lockdown_ops = {
151 .read = lockdown_read,
152 .write = lockdown_write,
155 static int __init lockdown_secfs_init(void)
157 struct dentry *dentry;
159 dentry = securityfs_create_file("lockdown", 0644, NULL, NULL,
161 return PTR_ERR_OR_ZERO(dentry);
164 core_initcall(lockdown_secfs_init);
166 #ifdef CONFIG_SECURITY_LOCKDOWN_LSM_EARLY
167 DEFINE_EARLY_LSM(lockdown) = {
169 DEFINE_LSM(lockdown) = {
172 .init = lockdown_lsm_init,