blob: 14b53fb2a0cfef9158ee1eb814349288684ff256 [file] [log] [blame]
Tetsuo Handa2106ccd2010-05-17 10:10:31 +09001/*
2 * security/tomoyo/mount.c
3 *
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +09004 * Copyright (C) 2005-2011 NTT DATA CORPORATION
Tetsuo Handa2106ccd2010-05-17 10:10:31 +09005 */
6
7#include <linux/slab.h>
8#include "common.h"
9
Tetsuo Handab5bc60b2011-06-26 23:16:03 +090010/* String table for special mount operations. */
11static const char * const tomoyo_mounts[TOMOYO_MAX_SPECIAL_MOUNT] = {
12 [TOMOYO_MOUNT_BIND] = "--bind",
13 [TOMOYO_MOUNT_MOVE] = "--move",
14 [TOMOYO_MOUNT_REMOUNT] = "--remount",
15 [TOMOYO_MOUNT_MAKE_UNBINDABLE] = "--make-unbindable",
16 [TOMOYO_MOUNT_MAKE_PRIVATE] = "--make-private",
17 [TOMOYO_MOUNT_MAKE_SLAVE] = "--make-slave",
18 [TOMOYO_MOUNT_MAKE_SHARED] = "--make-shared",
19};
Tetsuo Handa2106ccd2010-05-17 10:10:31 +090020
21/**
Tetsuo Handa99a85252010-06-16 16:22:51 +090022 * tomoyo_audit_mount_log - Audit mount log.
23 *
24 * @r: Pointer to "struct tomoyo_request_info".
25 *
26 * Returns 0 on success, negative value otherwise.
27 */
28static int tomoyo_audit_mount_log(struct tomoyo_request_info *r)
29{
Tetsuo Handaeadd99c2011-06-26 23:18:58 +090030 return tomoyo_supervisor(r, "file mount %s %s %s 0x%lX\n",
Tetsuo Handa7c759642011-06-26 23:15:31 +090031 r->param.mount.dev->name,
Tetsuo Handaeadd99c2011-06-26 23:18:58 +090032 r->param.mount.dir->name,
33 r->param.mount.type->name,
34 r->param.mount.flags);
Tetsuo Handa99a85252010-06-16 16:22:51 +090035}
36
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +090037/**
38 * tomoyo_check_mount_acl - Check permission for path path path number operation.
39 *
40 * @r: Pointer to "struct tomoyo_request_info".
41 * @ptr: Pointer to "struct tomoyo_acl_info".
42 *
43 * Returns true if granted, false otherwise.
44 */
Tetsuo Handa484ca792010-07-29 14:29:55 +090045static bool tomoyo_check_mount_acl(struct tomoyo_request_info *r,
Tetsuo Handa99a85252010-06-16 16:22:51 +090046 const struct tomoyo_acl_info *ptr)
47{
48 const struct tomoyo_mount_acl *acl =
49 container_of(ptr, typeof(*acl), head);
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +090050 return tomoyo_compare_number_union(r->param.mount.flags,
51 &acl->flags) &&
52 tomoyo_compare_name_union(r->param.mount.type,
53 &acl->fs_type) &&
54 tomoyo_compare_name_union(r->param.mount.dir,
55 &acl->dir_name) &&
Tetsuo Handa99a85252010-06-16 16:22:51 +090056 (!r->param.mount.need_dev ||
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +090057 tomoyo_compare_name_union(r->param.mount.dev,
58 &acl->dev_name));
Tetsuo Handa99a85252010-06-16 16:22:51 +090059}
60
61/**
Tetsuo Handad795ef9e2010-06-16 16:24:58 +090062 * tomoyo_mount_acl - Check permission for mount() operation.
Tetsuo Handa2106ccd2010-05-17 10:10:31 +090063 *
64 * @r: Pointer to "struct tomoyo_request_info".
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +090065 * @dev_name: Name of device file. Maybe NULL.
Tetsuo Handa2106ccd2010-05-17 10:10:31 +090066 * @dir: Pointer to "struct path".
67 * @type: Name of filesystem type.
68 * @flags: Mount options.
69 *
70 * Returns 0 on success, negative value otherwise.
71 *
72 * Caller holds tomoyo_read_lock().
73 */
Al Viro808d4e32012-10-11 11:42:01 -040074static int tomoyo_mount_acl(struct tomoyo_request_info *r,
75 const char *dev_name,
Al Viroe6641ed2016-03-25 14:41:28 -040076 const struct path *dir, const char *type,
Tetsuo Handab5bc60b2011-06-26 23:16:03 +090077 unsigned long flags)
Tetsuo Handa2106ccd2010-05-17 10:10:31 +090078{
Tetsuo Handa97fb35e2011-07-08 13:25:53 +090079 struct tomoyo_obj_info obj = { };
Tetsuo Handa2106ccd2010-05-17 10:10:31 +090080 struct path path;
Tetsuo Handa2106ccd2010-05-17 10:10:31 +090081 struct file_system_type *fstype = NULL;
82 const char *requested_type = NULL;
83 const char *requested_dir_name = NULL;
84 const char *requested_dev_name = NULL;
85 struct tomoyo_path_info rtype;
86 struct tomoyo_path_info rdev;
87 struct tomoyo_path_info rdir;
88 int need_dev = 0;
89 int error = -ENOMEM;
Tetsuo Handa97fb35e2011-07-08 13:25:53 +090090 r->obj = &obj;
Tetsuo Handa2106ccd2010-05-17 10:10:31 +090091
92 /* Get fstype. */
Tetsuo Handac8c57e82010-06-03 20:36:43 +090093 requested_type = tomoyo_encode(type);
Tetsuo Handa2106ccd2010-05-17 10:10:31 +090094 if (!requested_type)
95 goto out;
96 rtype.name = requested_type;
97 tomoyo_fill_path_info(&rtype);
98
99 /* Get mount point. */
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900100 obj.path2 = *dir;
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900101 requested_dir_name = tomoyo_realpath_from_path(dir);
102 if (!requested_dir_name) {
103 error = -ENOMEM;
104 goto out;
105 }
106 rdir.name = requested_dir_name;
107 tomoyo_fill_path_info(&rdir);
108
109 /* Compare fs name. */
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900110 if (type == tomoyo_mounts[TOMOYO_MOUNT_REMOUNT]) {
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900111 /* dev_name is ignored. */
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900112 } else if (type == tomoyo_mounts[TOMOYO_MOUNT_MAKE_UNBINDABLE] ||
113 type == tomoyo_mounts[TOMOYO_MOUNT_MAKE_PRIVATE] ||
114 type == tomoyo_mounts[TOMOYO_MOUNT_MAKE_SLAVE] ||
115 type == tomoyo_mounts[TOMOYO_MOUNT_MAKE_SHARED]) {
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900116 /* dev_name is ignored. */
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900117 } else if (type == tomoyo_mounts[TOMOYO_MOUNT_BIND] ||
118 type == tomoyo_mounts[TOMOYO_MOUNT_MOVE]) {
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900119 need_dev = -1; /* dev_name is a directory */
120 } else {
121 fstype = get_fs_type(type);
122 if (!fstype) {
123 error = -ENODEV;
124 goto out;
125 }
126 if (fstype->fs_flags & FS_REQUIRES_DEV)
127 /* dev_name is a block device file. */
128 need_dev = 1;
129 }
130 if (need_dev) {
131 /* Get mount point or device file. */
Tetsuo Handa4e78c722011-06-13 13:49:11 +0900132 if (!dev_name || kern_path(dev_name, LOOKUP_FOLLOW, &path)) {
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900133 error = -ENOENT;
134 goto out;
135 }
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900136 obj.path1 = path;
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900137 requested_dev_name = tomoyo_realpath_from_path(&path);
138 if (!requested_dev_name) {
139 error = -ENOENT;
140 goto out;
141 }
142 } else {
143 /* Map dev_name to "<NULL>" if no dev_name given. */
144 if (!dev_name)
145 dev_name = "<NULL>";
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900146 requested_dev_name = tomoyo_encode(dev_name);
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900147 if (!requested_dev_name) {
148 error = -ENOMEM;
149 goto out;
150 }
151 }
152 rdev.name = requested_dev_name;
153 tomoyo_fill_path_info(&rdev);
Tetsuo Handacf6e9a62010-06-16 16:21:36 +0900154 r->param_type = TOMOYO_TYPE_MOUNT_ACL;
155 r->param.mount.need_dev = need_dev;
156 r->param.mount.dev = &rdev;
157 r->param.mount.dir = &rdir;
158 r->param.mount.type = &rtype;
159 r->param.mount.flags = flags;
Tetsuo Handa99a85252010-06-16 16:22:51 +0900160 do {
161 tomoyo_check_acl(r, tomoyo_check_mount_acl);
162 error = tomoyo_audit_mount_log(r);
163 } while (error == TOMOYO_RETRY_REQUEST);
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900164 out:
165 kfree(requested_dev_name);
166 kfree(requested_dir_name);
167 if (fstype)
168 put_filesystem(fstype);
169 kfree(requested_type);
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900170 /* Drop refcount obtained by kern_path(). */
171 if (obj.path1.dentry)
172 path_put(&obj.path1);
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900173 return error;
174}
175
176/**
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900177 * tomoyo_mount_permission - Check permission for mount() operation.
178 *
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900179 * @dev_name: Name of device file. Maybe NULL.
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900180 * @path: Pointer to "struct path".
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900181 * @type: Name of filesystem type. Maybe NULL.
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900182 * @flags: Mount options.
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900183 * @data_page: Optional data. Maybe NULL.
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900184 *
185 * Returns 0 on success, negative value otherwise.
186 */
Al Viroe6641ed2016-03-25 14:41:28 -0400187int tomoyo_mount_permission(const char *dev_name, const struct path *path,
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900188 const char *type, unsigned long flags,
189 void *data_page)
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900190{
191 struct tomoyo_request_info r;
192 int error;
193 int idx;
194
Tetsuo Handa57c25902010-06-03 20:38:44 +0900195 if (tomoyo_init_request_info(&r, NULL, TOMOYO_MAC_FILE_MOUNT)
196 == TOMOYO_CONFIG_DISABLED)
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900197 return 0;
Tetsuo Handad795ef9e2010-06-16 16:24:58 +0900198 if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
199 flags &= ~MS_MGC_MSK;
200 if (flags & MS_REMOUNT) {
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900201 type = tomoyo_mounts[TOMOYO_MOUNT_REMOUNT];
Tetsuo Handad795ef9e2010-06-16 16:24:58 +0900202 flags &= ~MS_REMOUNT;
Tetsuo Handadf91e492012-02-29 21:53:22 +0900203 } else if (flags & MS_BIND) {
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900204 type = tomoyo_mounts[TOMOYO_MOUNT_BIND];
Tetsuo Handad795ef9e2010-06-16 16:24:58 +0900205 flags &= ~MS_BIND;
Tetsuo Handadf91e492012-02-29 21:53:22 +0900206 } else if (flags & MS_SHARED) {
207 if (flags & (MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
208 return -EINVAL;
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900209 type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_SHARED];
Tetsuo Handad795ef9e2010-06-16 16:24:58 +0900210 flags &= ~MS_SHARED;
Tetsuo Handadf91e492012-02-29 21:53:22 +0900211 } else if (flags & MS_PRIVATE) {
212 if (flags & (MS_SHARED | MS_SLAVE | MS_UNBINDABLE))
213 return -EINVAL;
214 type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_PRIVATE];
215 flags &= ~MS_PRIVATE;
216 } else if (flags & MS_SLAVE) {
217 if (flags & (MS_SHARED | MS_PRIVATE | MS_UNBINDABLE))
218 return -EINVAL;
219 type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_SLAVE];
220 flags &= ~MS_SLAVE;
221 } else if (flags & MS_UNBINDABLE) {
222 if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE))
223 return -EINVAL;
224 type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_UNBINDABLE];
225 flags &= ~MS_UNBINDABLE;
226 } else if (flags & MS_MOVE) {
227 type = tomoyo_mounts[TOMOYO_MOUNT_MOVE];
228 flags &= ~MS_MOVE;
Tetsuo Handad795ef9e2010-06-16 16:24:58 +0900229 }
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900230 if (!type)
231 type = "<NULL>";
232 idx = tomoyo_read_lock();
233 error = tomoyo_mount_acl(&r, dev_name, path, type, flags);
234 tomoyo_read_unlock(idx);
235 return error;
236}