1 // SPDX-License-Identifier: GPL-2.0
3 * security/tomoyo/mount.c
5 * Copyright (C) 2005-2011 NTT DATA CORPORATION
8 #include <linux/slab.h>
11 /* String table for special mount operations. */
12 static 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",
23 * tomoyo_audit_mount_log - Audit mount log.
25 * @r: Pointer to "struct tomoyo_request_info".
27 * Returns 0 on success, negative value otherwise.
29 static int tomoyo_audit_mount_log(struct tomoyo_request_info *r)
31 return tomoyo_supervisor(r, "file mount %s %s %s 0x%lX\n",
32 r->param.mount.dev->name,
33 r->param.mount.dir->name,
34 r->param.mount.type->name,
35 r->param.mount.flags);
39 * tomoyo_check_mount_acl - Check permission for path path path number operation.
41 * @r: Pointer to "struct tomoyo_request_info".
42 * @ptr: Pointer to "struct tomoyo_acl_info".
44 * Returns true if granted, false otherwise.
46 static bool tomoyo_check_mount_acl(struct tomoyo_request_info *r,
47 const struct tomoyo_acl_info *ptr)
49 const struct tomoyo_mount_acl *acl =
50 container_of(ptr, typeof(*acl), head);
51 return tomoyo_compare_number_union(r->param.mount.flags,
53 tomoyo_compare_name_union(r->param.mount.type,
55 tomoyo_compare_name_union(r->param.mount.dir,
57 (!r->param.mount.need_dev ||
58 tomoyo_compare_name_union(r->param.mount.dev,
63 * tomoyo_mount_acl - Check permission for mount() operation.
65 * @r: Pointer to "struct tomoyo_request_info".
66 * @dev_name: Name of device file. Maybe NULL.
67 * @dir: Pointer to "struct path".
68 * @type: Name of filesystem type.
69 * @flags: Mount options.
71 * Returns 0 on success, negative value otherwise.
73 * Caller holds tomoyo_read_lock().
75 static int tomoyo_mount_acl(struct tomoyo_request_info *r,
77 const struct path *dir, const char *type,
80 struct tomoyo_obj_info obj = { };
82 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;
94 requested_type = tomoyo_encode(type);
97 rtype.name = requested_type;
98 tomoyo_fill_path_info(&rtype);
100 /* Get mount point. */
102 requested_dir_name = tomoyo_realpath_from_path(dir);
103 if (!requested_dir_name) {
107 rdir.name = requested_dir_name;
108 tomoyo_fill_path_info(&rdir);
110 /* Compare fs name. */
111 if (type == tomoyo_mounts[TOMOYO_MOUNT_REMOUNT]) {
112 /* dev_name is ignored. */
113 } 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]) {
117 /* dev_name is ignored. */
118 } else if (type == tomoyo_mounts[TOMOYO_MOUNT_BIND] ||
119 type == tomoyo_mounts[TOMOYO_MOUNT_MOVE]) {
120 need_dev = -1; /* dev_name is a directory */
122 fstype = get_fs_type(type);
127 if (fstype->fs_flags & FS_REQUIRES_DEV)
128 /* dev_name is a block device file. */
132 /* Get mount point or device file. */
133 if (!dev_name || kern_path(dev_name, LOOKUP_FOLLOW, &path)) {
138 requested_dev_name = tomoyo_realpath_from_path(&path);
139 if (!requested_dev_name) {
144 /* Map dev_name to "<NULL>" if no dev_name given. */
147 requested_dev_name = tomoyo_encode(dev_name);
148 if (!requested_dev_name) {
153 rdev.name = requested_dev_name;
154 tomoyo_fill_path_info(&rdev);
155 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;
162 tomoyo_check_acl(r, tomoyo_check_mount_acl);
163 error = tomoyo_audit_mount_log(r);
164 } while (error == TOMOYO_RETRY_REQUEST);
166 kfree(requested_dev_name);
167 kfree(requested_dir_name);
169 put_filesystem(fstype);
170 kfree(requested_type);
171 /* Drop refcount obtained by kern_path(). */
172 if (obj.path1.dentry)
173 path_put(&obj.path1);
178 * tomoyo_mount_permission - Check permission for mount() operation.
180 * @dev_name: Name of device file. Maybe NULL.
181 * @path: Pointer to "struct path".
182 * @type: Name of filesystem type. Maybe NULL.
183 * @flags: Mount options.
184 * @data_page: Optional data. Maybe NULL.
186 * Returns 0 on success, negative value otherwise.
188 int tomoyo_mount_permission(const char *dev_name, const struct path *path,
189 const char *type, unsigned long flags,
192 struct tomoyo_request_info r;
196 if (tomoyo_init_request_info(&r, NULL, TOMOYO_MAC_FILE_MOUNT)
197 == TOMOYO_CONFIG_DISABLED)
199 if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
200 flags &= ~MS_MGC_MSK;
201 if (flags & MS_REMOUNT) {
202 type = tomoyo_mounts[TOMOYO_MOUNT_REMOUNT];
203 flags &= ~MS_REMOUNT;
204 } else if (flags & MS_BIND) {
205 type = tomoyo_mounts[TOMOYO_MOUNT_BIND];
207 } else if (flags & MS_SHARED) {
208 if (flags & (MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
210 type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_SHARED];
212 } else if (flags & MS_PRIVATE) {
213 if (flags & (MS_SHARED | MS_SLAVE | MS_UNBINDABLE))
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))
220 type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_SLAVE];
222 } else if (flags & MS_UNBINDABLE) {
223 if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE))
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];
233 idx = tomoyo_read_lock();
234 error = tomoyo_mount_acl(&r, dev_name, path, type, flags);
235 tomoyo_read_unlock(idx);