blob: 807fd91dbb540605b42bbf7d629586e2f87f684a [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>
9#include "common.h"
10
Tetsuo Handab5bc60b2011-06-26 23:16:03 +090011/* String table for special mount operations. */
12static const char * const tomoyo_mounts[TOMOYO_MAX_SPECIAL_MOUNT] = {
13 [TOMOYO_MOUNT_BIND] = "--bind",
14 [TOMOYO_MOUNT_MOVE] = "--move",
15 [TOMOYO_MOUNT_REMOUNT] = "--remount",
16 [TOMOYO_MOUNT_MAKE_UNBINDABLE] = "--make-unbindable",
17 [TOMOYO_MOUNT_MAKE_PRIVATE] = "--make-private",
18 [TOMOYO_MOUNT_MAKE_SLAVE] = "--make-slave",
19 [TOMOYO_MOUNT_MAKE_SHARED] = "--make-shared",
20};
Tetsuo Handa2106ccd2010-05-17 10:10:31 +090021
22/**
Tetsuo Handa99a85252010-06-16 16:22:51 +090023 * tomoyo_audit_mount_log - Audit mount log.
24 *
25 * @r: Pointer to "struct tomoyo_request_info".
26 *
27 * Returns 0 on success, negative value otherwise.
28 */
29static int tomoyo_audit_mount_log(struct tomoyo_request_info *r)
30{
Tetsuo Handaeadd99c2011-06-26 23:18:58 +090031 return tomoyo_supervisor(r, "file mount %s %s %s 0x%lX\n",
Tetsuo Handa7c759642011-06-26 23:15:31 +090032 r->param.mount.dev->name,
Tetsuo Handaeadd99c2011-06-26 23:18:58 +090033 r->param.mount.dir->name,
34 r->param.mount.type->name,
35 r->param.mount.flags);
Tetsuo Handa99a85252010-06-16 16:22:51 +090036}
37
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +090038/**
39 * tomoyo_check_mount_acl - Check permission for path path path number operation.
40 *
41 * @r: Pointer to "struct tomoyo_request_info".
42 * @ptr: Pointer to "struct tomoyo_acl_info".
43 *
44 * Returns true if granted, false otherwise.
45 */
Tetsuo Handa484ca792010-07-29 14:29:55 +090046static bool tomoyo_check_mount_acl(struct tomoyo_request_info *r,
Tetsuo Handa99a85252010-06-16 16:22:51 +090047 const struct tomoyo_acl_info *ptr)
48{
49 const struct tomoyo_mount_acl *acl =
50 container_of(ptr, typeof(*acl), head);
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +090051 return tomoyo_compare_number_union(r->param.mount.flags,
52 &acl->flags) &&
53 tomoyo_compare_name_union(r->param.mount.type,
54 &acl->fs_type) &&
55 tomoyo_compare_name_union(r->param.mount.dir,
56 &acl->dir_name) &&
Tetsuo Handa99a85252010-06-16 16:22:51 +090057 (!r->param.mount.need_dev ||
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +090058 tomoyo_compare_name_union(r->param.mount.dev,
59 &acl->dev_name));
Tetsuo Handa99a85252010-06-16 16:22:51 +090060}
61
62/**
Tetsuo Handad795ef9e2010-06-16 16:24:58 +090063 * tomoyo_mount_acl - Check permission for mount() operation.
Tetsuo Handa2106ccd2010-05-17 10:10:31 +090064 *
65 * @r: Pointer to "struct tomoyo_request_info".
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +090066 * @dev_name: Name of device file. Maybe NULL.
Tetsuo Handa2106ccd2010-05-17 10:10:31 +090067 * @dir: Pointer to "struct path".
68 * @type: Name of filesystem type.
69 * @flags: Mount options.
70 *
71 * Returns 0 on success, negative value otherwise.
72 *
73 * Caller holds tomoyo_read_lock().
74 */
Al Viro808d4e32012-10-11 11:42:01 -040075static int tomoyo_mount_acl(struct tomoyo_request_info *r,
76 const char *dev_name,
Al Viroe6641ed2016-03-25 14:41:28 -040077 const struct path *dir, const char *type,
Tetsuo Handab5bc60b2011-06-26 23:16:03 +090078 unsigned long flags)
Tetsuo Handa2106ccd2010-05-17 10:10:31 +090079{
Tetsuo Handa97fb35e2011-07-08 13:25:53 +090080 struct tomoyo_obj_info obj = { };
Tetsuo Handa2106ccd2010-05-17 10:10:31 +090081 struct path path;
Tetsuo Handa2106ccd2010-05-17 10:10:31 +090082 struct file_system_type *fstype = NULL;
83 const char *requested_type = NULL;
84 const char *requested_dir_name = NULL;
85 const char *requested_dev_name = NULL;
86 struct tomoyo_path_info rtype;
87 struct tomoyo_path_info rdev;
88 struct tomoyo_path_info rdir;
89 int need_dev = 0;
90 int error = -ENOMEM;
Tetsuo Handa97fb35e2011-07-08 13:25:53 +090091 r->obj = &obj;
Tetsuo Handa2106ccd2010-05-17 10:10:31 +090092
93 /* Get fstype. */
Tetsuo Handac8c57e82010-06-03 20:36:43 +090094 requested_type = tomoyo_encode(type);
Tetsuo Handa2106ccd2010-05-17 10:10:31 +090095 if (!requested_type)
96 goto out;
97 rtype.name = requested_type;
98 tomoyo_fill_path_info(&rtype);
99
100 /* Get mount point. */
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900101 obj.path2 = *dir;
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900102 requested_dir_name = tomoyo_realpath_from_path(dir);
103 if (!requested_dir_name) {
104 error = -ENOMEM;
105 goto out;
106 }
107 rdir.name = requested_dir_name;
108 tomoyo_fill_path_info(&rdir);
109
110 /* Compare fs name. */
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900111 if (type == tomoyo_mounts[TOMOYO_MOUNT_REMOUNT]) {
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900112 /* dev_name is ignored. */
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900113 } else if (type == tomoyo_mounts[TOMOYO_MOUNT_MAKE_UNBINDABLE] ||
114 type == tomoyo_mounts[TOMOYO_MOUNT_MAKE_PRIVATE] ||
115 type == tomoyo_mounts[TOMOYO_MOUNT_MAKE_SLAVE] ||
116 type == tomoyo_mounts[TOMOYO_MOUNT_MAKE_SHARED]) {
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900117 /* dev_name is ignored. */
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900118 } else if (type == tomoyo_mounts[TOMOYO_MOUNT_BIND] ||
119 type == tomoyo_mounts[TOMOYO_MOUNT_MOVE]) {
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900120 need_dev = -1; /* dev_name is a directory */
121 } else {
122 fstype = get_fs_type(type);
123 if (!fstype) {
124 error = -ENODEV;
125 goto out;
126 }
127 if (fstype->fs_flags & FS_REQUIRES_DEV)
128 /* dev_name is a block device file. */
129 need_dev = 1;
130 }
131 if (need_dev) {
132 /* Get mount point or device file. */
Tetsuo Handa4e78c722011-06-13 13:49:11 +0900133 if (!dev_name || kern_path(dev_name, LOOKUP_FOLLOW, &path)) {
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900134 error = -ENOENT;
135 goto out;
136 }
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900137 obj.path1 = path;
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900138 requested_dev_name = tomoyo_realpath_from_path(&path);
139 if (!requested_dev_name) {
140 error = -ENOENT;
141 goto out;
142 }
143 } else {
144 /* Map dev_name to "<NULL>" if no dev_name given. */
145 if (!dev_name)
146 dev_name = "<NULL>";
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900147 requested_dev_name = tomoyo_encode(dev_name);
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900148 if (!requested_dev_name) {
149 error = -ENOMEM;
150 goto out;
151 }
152 }
153 rdev.name = requested_dev_name;
154 tomoyo_fill_path_info(&rdev);
Tetsuo Handacf6e9a62010-06-16 16:21:36 +0900155 r->param_type = TOMOYO_TYPE_MOUNT_ACL;
156 r->param.mount.need_dev = need_dev;
157 r->param.mount.dev = &rdev;
158 r->param.mount.dir = &rdir;
159 r->param.mount.type = &rtype;
160 r->param.mount.flags = flags;
Tetsuo Handa99a85252010-06-16 16:22:51 +0900161 do {
162 tomoyo_check_acl(r, tomoyo_check_mount_acl);
163 error = tomoyo_audit_mount_log(r);
164 } while (error == TOMOYO_RETRY_REQUEST);
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900165 out:
166 kfree(requested_dev_name);
167 kfree(requested_dir_name);
168 if (fstype)
169 put_filesystem(fstype);
170 kfree(requested_type);
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900171 /* Drop refcount obtained by kern_path(). */
172 if (obj.path1.dentry)
173 path_put(&obj.path1);
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900174 return error;
175}
176
177/**
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900178 * tomoyo_mount_permission - Check permission for mount() operation.
179 *
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900180 * @dev_name: Name of device file. Maybe NULL.
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900181 * @path: Pointer to "struct path".
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900182 * @type: Name of filesystem type. Maybe NULL.
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900183 * @flags: Mount options.
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900184 * @data_page: Optional data. Maybe NULL.
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900185 *
186 * Returns 0 on success, negative value otherwise.
187 */
Al Viroe6641ed2016-03-25 14:41:28 -0400188int tomoyo_mount_permission(const char *dev_name, const struct path *path,
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900189 const char *type, unsigned long flags,
190 void *data_page)
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900191{
192 struct tomoyo_request_info r;
193 int error;
194 int idx;
195
Tetsuo Handa57c25902010-06-03 20:38:44 +0900196 if (tomoyo_init_request_info(&r, NULL, TOMOYO_MAC_FILE_MOUNT)
197 == TOMOYO_CONFIG_DISABLED)
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900198 return 0;
Tetsuo Handad795ef9e2010-06-16 16:24:58 +0900199 if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
200 flags &= ~MS_MGC_MSK;
201 if (flags & MS_REMOUNT) {
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900202 type = tomoyo_mounts[TOMOYO_MOUNT_REMOUNT];
Tetsuo Handad795ef9e2010-06-16 16:24:58 +0900203 flags &= ~MS_REMOUNT;
Tetsuo Handadf91e492012-02-29 21:53:22 +0900204 } else if (flags & MS_BIND) {
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900205 type = tomoyo_mounts[TOMOYO_MOUNT_BIND];
Tetsuo Handad795ef9e2010-06-16 16:24:58 +0900206 flags &= ~MS_BIND;
Tetsuo Handadf91e492012-02-29 21:53:22 +0900207 } else if (flags & MS_SHARED) {
208 if (flags & (MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
209 return -EINVAL;
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900210 type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_SHARED];
Tetsuo Handad795ef9e2010-06-16 16:24:58 +0900211 flags &= ~MS_SHARED;
Tetsuo Handadf91e492012-02-29 21:53:22 +0900212 } else if (flags & MS_PRIVATE) {
213 if (flags & (MS_SHARED | MS_SLAVE | MS_UNBINDABLE))
214 return -EINVAL;
215 type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_PRIVATE];
216 flags &= ~MS_PRIVATE;
217 } else if (flags & MS_SLAVE) {
218 if (flags & (MS_SHARED | MS_PRIVATE | MS_UNBINDABLE))
219 return -EINVAL;
220 type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_SLAVE];
221 flags &= ~MS_SLAVE;
222 } else if (flags & MS_UNBINDABLE) {
223 if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE))
224 return -EINVAL;
225 type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_UNBINDABLE];
226 flags &= ~MS_UNBINDABLE;
227 } else if (flags & MS_MOVE) {
228 type = tomoyo_mounts[TOMOYO_MOUNT_MOVE];
229 flags &= ~MS_MOVE;
Tetsuo Handad795ef9e2010-06-16 16:24:58 +0900230 }
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900231 if (!type)
232 type = "<NULL>";
233 idx = tomoyo_read_lock();
234 error = tomoyo_mount_acl(&r, dev_name, path, type, flags);
235 tomoyo_read_unlock(idx);
236 return error;
237}