CHROMIUM: LSM: add module loading restrictions
[cascardo/linux.git] / security / chromiumos / lsm.c
1 /*
2  * Linux Security Module for Chromium OS
3  *
4  * Copyright 2011 Google Inc. All Rights Reserved
5  *
6  * Authors:
7  *      Stephan Uphoff  <ups@google.com>
8  *      Kees Cook       <keescook@chromium.org>
9  *
10  * This software is licensed under the terms of the GNU General Public
11  * License version 2, as published by the Free Software Foundation, and
12  * may be copied, distributed, and modified under those terms.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  */
19
20 #define pr_fmt(fmt) "Chromium OS LSM: " fmt
21
22 #include <linux/module.h>
23 #include <linux/security.h>
24 #include <linux/sched.h>        /* current and other task related stuff */
25 #include <linux/fs.h>
26 #include <linux/fs_struct.h>
27 #include <linux/mount.h>
28 #include <linux/path.h>
29 #include <linux/root_dev.h>
30
31 #include "utils.h"
32
33 static int chromiumos_security_sb_mount(char *dev_name, struct path *path,
34                          char *type, unsigned long flags, void *data)
35 {
36         int error = current->total_link_count ? -ELOOP : 0;
37
38         if (error) {
39                 char *cmdline;
40
41                 cmdline = printable_cmdline(current);
42                 pr_notice("Mount path with symlinks prohibited - "
43                         "pid=%d cmdline=%s\n",
44                         task_pid_nr(current), cmdline);
45                 kfree(cmdline);
46         }
47
48         return error;
49 }
50
51 static void report_load_module(struct path *path, char *operation)
52 {
53         char *alloced = NULL, *cmdline;
54         char *pathname; /* Pointer to either static string or "alloced". */
55
56         if (!path)
57                 pathname = "<unknown>";
58         else {
59                 /* We will allow 11 spaces for ' (deleted)' to be appended */
60                 alloced = pathname = kmalloc(PATH_MAX+11, GFP_KERNEL);
61                 if (!pathname)
62                         pathname = "<no_memory>";
63                 else {
64                         pathname = d_path(path, pathname, PATH_MAX+11);
65                         if (IS_ERR(pathname))
66                                 pathname = "<too_long>";
67                         else {
68                                 pathname = printable(pathname);
69                                 kfree(alloced);
70                                 alloced = pathname;
71                         }
72                 }
73         }
74
75         cmdline = printable_cmdline(current);
76
77         pr_notice("init_module %s module=%s pid=%d cmdline=%s\n",
78                   operation, pathname, task_pid_nr(current), cmdline);
79
80         kfree(cmdline);
81         kfree(alloced);
82 }
83
84 static int module_locking = 1;
85 static struct dentry *locked_root;
86
87 #ifdef CONFIG_SYSCTL
88 static int zero;
89 static int one = 1;
90
91 static struct ctl_path chromiumos_sysctl_path[] = {
92         { .procname = "kernel", },
93         { .procname = "chromiumos", },
94         { }
95 };
96
97 static struct ctl_table chromiumos_sysctl_table[] = {
98         {
99                 .procname       = "module_locking",
100                 .data           = &module_locking,
101                 .maxlen         = sizeof(int),
102                 .mode           = 0644,
103                 .proc_handler   = proc_dointvec_minmax,
104                 .extra1         = &zero,
105                 .extra2         = &one,
106         },
107         { }
108 };
109
110 /* Check if the root device is read-only (e.g. dm-verity is enabled).
111  * This must be called after early kernel init, since then the rootdev
112  * is available.
113  */
114 static bool rootdev_readonly(void)
115 {
116         bool rc;
117         struct block_device *bdev;
118         const fmode_t mode = FMODE_WRITE;
119
120         bdev = blkdev_get_by_dev(ROOT_DEV, mode, NULL);
121         if (IS_ERR(bdev)) {
122                 /* In this weird case, assume it is read-only. */
123                 pr_info("dev(%u,%u): FMODE_WRITE disallowed?!\n",
124                         MAJOR(ROOT_DEV), MINOR(ROOT_DEV));
125                 return true;
126         }
127
128         rc = bdev_read_only(bdev);
129         blkdev_put(bdev, mode);
130
131         pr_info("dev(%u,%u): %s\n", MAJOR(ROOT_DEV), MINOR(ROOT_DEV),
132                 rc ? "read-only" : "writable");
133
134         return rc;
135 }
136
137 static void check_locking_enforcement(void)
138 {
139         /* If module locking is not being enforced, allow sysctl to change
140          * modes for testing.
141          */
142         if (!rootdev_readonly()) {
143                 if (!register_sysctl_paths(chromiumos_sysctl_path,
144                                            chromiumos_sysctl_table))
145                         pr_notice("sysctl registration failed!\n");
146                 else
147                         pr_info("module locking can be disabled.\n");
148         } else
149                 pr_info("module locking engaged.\n");
150 }
151 #else
152 static void check_locking_enforcement(void) { }
153 #endif
154
155
156 static int chromiumos_security_load_module(struct file *file)
157 {
158         struct dentry *module_root;
159
160         if (!file) {
161                 report_load_module(NULL, "old-api-denied");
162                 return -EPERM;
163         }
164
165         module_root = file->f_vfsmnt->mnt_root;
166
167         /* First loaded module defines the root for all others. */
168         if (!locked_root) {
169                 locked_root = dget(module_root);
170                 report_load_module(&file->f_path, "locked");
171                 check_locking_enforcement();
172         }
173
174         if (module_root != locked_root) {
175                 if (unlikely(!module_locking)) {
176                         report_load_module(&file->f_path, "locking-ignored");
177                         return 0;
178                 }
179
180                 report_load_module(&file->f_path, "denied");
181                 return -EPERM;
182         }
183
184         return 0;
185 }
186
187 static struct security_operations chromiumos_security_ops = {
188         .name   = "chromiumos",
189         .sb_mount = chromiumos_security_sb_mount,
190         .kernel_module_from_file = chromiumos_security_load_module,
191 };
192
193
194 static int __init chromiumos_security_init(void)
195 {
196         int error;
197
198         error = register_security(&chromiumos_security_ops);
199
200         if (error)
201                 panic("Could not register Chromium OS security module");
202
203         return error;
204 }
205 security_initcall(chromiumos_security_init);
206
207 module_param(module_locking, int, S_IRUSR);
208 MODULE_PARM_DESC(module_locking, "Module loading restrictions (default: true)");