blob: 7dc7f59b7ddecabc7a70253f1a213a581f943b90 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Tetsuo Handa2106ccd2010-05-17 10:10:31 +09002/*
3 * security/tomoyo/mount.c
4 *
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +09005 * Copyright (C) 2005-2011 NTT DATA CORPORATION
Tetsuo Handa2106ccd2010-05-17 10:10:31 +09006 */
7
8#include <linux/slab.h>
David Howellse262e32d2018-11-01 23:07:23 +00009#include <uapi/linux/mount.h>
Tetsuo Handa2106ccd2010-05-17 10:10:31 +090010#include "common.h"
11
Tetsuo Handab5bc60b2011-06-26 23:16:03 +090012/* String table for special mount operations. */
13static const char * const tomoyo_mounts[TOMOYO_MAX_SPECIAL_MOUNT] = {
14 [TOMOYO_MOUNT_BIND] = "--bind",
15 [TOMOYO_MOUNT_MOVE] = "--move",
16 [TOMOYO_MOUNT_REMOUNT] = "--remount",
17 [TOMOYO_MOUNT_MAKE_UNBINDABLE] = "--make-unbindable",
18 [TOMOYO_MOUNT_MAKE_PRIVATE] = "--make-private",
19 [TOMOYO_MOUNT_MAKE_SLAVE] = "--make-slave",
20 [TOMOYO_MOUNT_MAKE_SHARED] = "--make-shared",
21};
Tetsuo Handa2106ccd2010-05-17 10:10:31 +090022
23/**
Tetsuo Handa99a85252010-06-16 16:22:51 +090024 * tomoyo_audit_mount_log - Audit mount log.
25 *
26 * @r: Pointer to "struct tomoyo_request_info".
27 *
28 * Returns 0 on success, negative value otherwise.
29 */
30static int tomoyo_audit_mount_log(struct tomoyo_request_info *r)
31{
Tetsuo Handaeadd99c2011-06-26 23:18:58 +090032 return tomoyo_supervisor(r, "file mount %s %s %s 0x%lX\n",
Tetsuo Handa7c759642011-06-26 23:15:31 +090033 r->param.mount.dev->name,
Tetsuo Handaeadd99c2011-06-26 23:18:58 +090034 r->param.mount.dir->name,
35 r->param.mount.type->name,
36 r->param.mount.flags);
Tetsuo Handa99a85252010-06-16 16:22:51 +090037}
38
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +090039/**
40 * tomoyo_check_mount_acl - Check permission for path path path number operation.
41 *
42 * @r: Pointer to "struct tomoyo_request_info".
43 * @ptr: Pointer to "struct tomoyo_acl_info".
44 *
45 * Returns true if granted, false otherwise.
46 */
Tetsuo Handa484ca792010-07-29 14:29:55 +090047static bool tomoyo_check_mount_acl(struct tomoyo_request_info *r,
Tetsuo Handa99a85252010-06-16 16:22:51 +090048 const struct tomoyo_acl_info *ptr)
49{
50 const struct tomoyo_mount_acl *acl =
51 container_of(ptr, typeof(*acl), head);
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +090052 return tomoyo_compare_number_union(r->param.mount.flags,
53 &acl->flags) &&
54 tomoyo_compare_name_union(r->param.mount.type,
55 &acl->fs_type) &&
56 tomoyo_compare_name_union(r->param.mount.dir,
57 &acl->dir_name) &&
Tetsuo Handa99a85252010-06-16 16:22:51 +090058 (!r->param.mount.need_dev ||
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +090059 tomoyo_compare_name_union(r->param.mount.dev,
60 &acl->dev_name));
Tetsuo Handa99a85252010-06-16 16:22:51 +090061}
62
63/**
Tetsuo Handad795ef9e2010-06-16 16:24:58 +090064 * tomoyo_mount_acl - Check permission for mount() operation.
Tetsuo Handa2106ccd2010-05-17 10:10:31 +090065 *
66 * @r: Pointer to "struct tomoyo_request_info".
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +090067 * @dev_name: Name of device file. Maybe NULL.
Tetsuo Handa2106ccd2010-05-17 10:10:31 +090068 * @dir: Pointer to "struct path".
69 * @type: Name of filesystem type.
70 * @flags: Mount options.
71 *
72 * Returns 0 on success, negative value otherwise.
73 *
74 * Caller holds tomoyo_read_lock().
75 */
Al Viro808d4e32012-10-11 11:42:01 -040076static int tomoyo_mount_acl(struct tomoyo_request_info *r,
77 const char *dev_name,
Al Viroe6641ed2016-03-25 14:41:28 -040078 const struct path *dir, const char *type,
Tetsuo Handab5bc60b2011-06-26 23:16:03 +090079 unsigned long flags)
Tetsuo Handa2106ccd2010-05-17 10:10:31 +090080{
Tetsuo Handa97fb35e2011-07-08 13:25:53 +090081 struct tomoyo_obj_info obj = { };
Tetsuo Handa2106ccd2010-05-17 10:10:31 +090082 struct path path;
Tetsuo Handa2106ccd2010-05-17 10:10:31 +090083 struct file_system_type *fstype = NULL;
84 const char *requested_type = NULL;
85 const char *requested_dir_name = NULL;
86 const char *requested_dev_name = NULL;
87 struct tomoyo_path_info rtype;
88 struct tomoyo_path_info rdev;
89 struct tomoyo_path_info rdir;
90 int need_dev = 0;
91 int error = -ENOMEM;
Tetsuo Handa97fb35e2011-07-08 13:25:53 +090092 r->obj = &obj;
Tetsuo Handa2106ccd2010-05-17 10:10:31 +090093
94 /* Get fstype. */
Tetsuo Handac8c57e82010-06-03 20:36:43 +090095 requested_type = tomoyo_encode(type);
Tetsuo Handa2106ccd2010-05-17 10:10:31 +090096 if (!requested_type)
97 goto out;
98 rtype.name = requested_type;
99 tomoyo_fill_path_info(&rtype);
100
101 /* Get mount point. */
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900102 obj.path2 = *dir;
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900103 requested_dir_name = tomoyo_realpath_from_path(dir);
104 if (!requested_dir_name) {
105 error = -ENOMEM;
106 goto out;
107 }
108 rdir.name = requested_dir_name;
109 tomoyo_fill_path_info(&rdir);
110
111 /* Compare fs name. */
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900112 if (type == tomoyo_mounts[TOMOYO_MOUNT_REMOUNT]) {
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900113 /* dev_name is ignored. */
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900114 } else if (type == tomoyo_mounts[TOMOYO_MOUNT_MAKE_UNBINDABLE] ||
115 type == tomoyo_mounts[TOMOYO_MOUNT_MAKE_PRIVATE] ||
116 type == tomoyo_mounts[TOMOYO_MOUNT_MAKE_SLAVE] ||
117 type == tomoyo_mounts[TOMOYO_MOUNT_MAKE_SHARED]) {
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900118 /* dev_name is ignored. */
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900119 } else if (type == tomoyo_mounts[TOMOYO_MOUNT_BIND] ||
120 type == tomoyo_mounts[TOMOYO_MOUNT_MOVE]) {
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900121 need_dev = -1; /* dev_name is a directory */
122 } else {
123 fstype = get_fs_type(type);
124 if (!fstype) {
125 error = -ENODEV;
126 goto out;
127 }
128 if (fstype->fs_flags & FS_REQUIRES_DEV)
129 /* dev_name is a block device file. */
130 need_dev = 1;
131 }
132 if (need_dev) {
133 /* Get mount point or device file. */
Tetsuo Handa4e78c722011-06-13 13:49:11 +0900134 if (!dev_name || kern_path(dev_name, LOOKUP_FOLLOW, &path)) {
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900135 error = -ENOENT;
136 goto out;
137 }
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900138 obj.path1 = path;
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900139 requested_dev_name = tomoyo_realpath_from_path(&path);
140 if (!requested_dev_name) {
141 error = -ENOENT;
142 goto out;
143 }
144 } else {
145 /* Map dev_name to "<NULL>" if no dev_name given. */
146 if (!dev_name)
147 dev_name = "<NULL>";
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900148 requested_dev_name = tomoyo_encode(dev_name);
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900149 if (!requested_dev_name) {
150 error = -ENOMEM;
151 goto out;
152 }
153 }
154 rdev.name = requested_dev_name;
155 tomoyo_fill_path_info(&rdev);
Tetsuo Handacf6e9a62010-06-16 16:21:36 +0900156 r->param_type = TOMOYO_TYPE_MOUNT_ACL;
157 r->param.mount.need_dev = need_dev;
158 r->param.mount.dev = &rdev;
159 r->param.mount.dir = &rdir;
160 r->param.mount.type = &rtype;
161 r->param.mount.flags = flags;
Tetsuo Handa99a85252010-06-16 16:22:51 +0900162 do {
163 tomoyo_check_acl(r, tomoyo_check_mount_acl);
164 error = tomoyo_audit_mount_log(r);
165 } while (error == TOMOYO_RETRY_REQUEST);
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900166 out:
167 kfree(requested_dev_name);
168 kfree(requested_dir_name);
169 if (fstype)
170 put_filesystem(fstype);
171 kfree(requested_type);
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900172 /* Drop refcount obtained by kern_path(). */
173 if (obj.path1.dentry)
174 path_put(&obj.path1);
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900175 return error;
176}
177
178/**
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900179 * tomoyo_mount_permission - Check permission for mount() operation.
180 *
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900181 * @dev_name: Name of device file. Maybe NULL.
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900182 * @path: Pointer to "struct path".
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900183 * @type: Name of filesystem type. Maybe NULL.
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900184 * @flags: Mount options.
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900185 * @data_page: Optional data. Maybe NULL.
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900186 *
187 * Returns 0 on success, negative value otherwise.
188 */
Al Viroe6641ed2016-03-25 14:41:28 -0400189int tomoyo_mount_permission(const char *dev_name, const struct path *path,
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900190 const char *type, unsigned long flags,
191 void *data_page)
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900192{
193 struct tomoyo_request_info r;
194 int error;
195 int idx;
196
Tetsuo Handa57c25902010-06-03 20:38:44 +0900197 if (tomoyo_init_request_info(&r, NULL, TOMOYO_MAC_FILE_MOUNT)
198 == TOMOYO_CONFIG_DISABLED)
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900199 return 0;
Tetsuo Handad795ef9e2010-06-16 16:24:58 +0900200 if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
201 flags &= ~MS_MGC_MSK;
202 if (flags & MS_REMOUNT) {
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900203 type = tomoyo_mounts[TOMOYO_MOUNT_REMOUNT];
Tetsuo Handad795ef9e2010-06-16 16:24:58 +0900204 flags &= ~MS_REMOUNT;
Tetsuo Handadf91e492012-02-29 21:53:22 +0900205 } else if (flags & MS_BIND) {
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900206 type = tomoyo_mounts[TOMOYO_MOUNT_BIND];
Tetsuo Handad795ef9e2010-06-16 16:24:58 +0900207 flags &= ~MS_BIND;
Tetsuo Handadf91e492012-02-29 21:53:22 +0900208 } else if (flags & MS_SHARED) {
209 if (flags & (MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
210 return -EINVAL;
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900211 type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_SHARED];
Tetsuo Handad795ef9e2010-06-16 16:24:58 +0900212 flags &= ~MS_SHARED;
Tetsuo Handadf91e492012-02-29 21:53:22 +0900213 } else if (flags & MS_PRIVATE) {
214 if (flags & (MS_SHARED | MS_SLAVE | MS_UNBINDABLE))
215 return -EINVAL;
216 type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_PRIVATE];
217 flags &= ~MS_PRIVATE;
218 } else if (flags & MS_SLAVE) {
219 if (flags & (MS_SHARED | MS_PRIVATE | MS_UNBINDABLE))
220 return -EINVAL;
221 type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_SLAVE];
222 flags &= ~MS_SLAVE;
223 } else if (flags & MS_UNBINDABLE) {
224 if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE))
225 return -EINVAL;
226 type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_UNBINDABLE];
227 flags &= ~MS_UNBINDABLE;
228 } else if (flags & MS_MOVE) {
229 type = tomoyo_mounts[TOMOYO_MOUNT_MOVE];
230 flags &= ~MS_MOVE;
Tetsuo Handad795ef9e2010-06-16 16:24:58 +0900231 }
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900232 if (!type)
233 type = "<NULL>";
234 idx = tomoyo_read_lock();
235 error = tomoyo_mount_acl(&r, dev_name, path, type, flags);
236 tomoyo_read_unlock(idx);
237 return error;
238}