2 * security/tomoyo/mount.c
4 * Copyright (C) 2005-2010 NTT DATA CORPORATION
7 #include <linux/slab.h>
10 /* String table for special mount operations. */
11 static 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",
22 * tomoyo_audit_mount_log - Audit mount log.
24 * @r: Pointer to "struct tomoyo_request_info".
26 * Returns 0 on success, negative value otherwise.
28 static int tomoyo_audit_mount_log(struct tomoyo_request_info *r)
30 return tomoyo_supervisor(r, "file mount %s %s %s 0x%lX\n",
31 r->param.mount.dev->name,
32 r->param.mount.dir->name,
33 r->param.mount.type->name,
34 r->param.mount.flags);
38 * tomoyo_check_mount_acl - Check permission for path path path number operation.
40 * @r: Pointer to "struct tomoyo_request_info".
41 * @ptr: Pointer to "struct tomoyo_acl_info".
43 * Returns true if granted, false otherwise.
45 static bool tomoyo_check_mount_acl(struct tomoyo_request_info *r,
46 const struct tomoyo_acl_info *ptr)
48 const struct tomoyo_mount_acl *acl =
49 container_of(ptr, typeof(*acl), head);
50 return tomoyo_compare_number_union(r->param.mount.flags,
52 tomoyo_compare_name_union(r->param.mount.type,
54 tomoyo_compare_name_union(r->param.mount.dir,
56 (!r->param.mount.need_dev ||
57 tomoyo_compare_name_union(r->param.mount.dev,
62 * tomoyo_mount_acl - Check permission for mount() operation.
64 * @r: Pointer to "struct tomoyo_request_info".
65 * @dev_name: Name of device file.
66 * @dir: Pointer to "struct path".
67 * @type: Name of filesystem type.
68 * @flags: Mount options.
70 * Returns 0 on success, negative value otherwise.
72 * Caller holds tomoyo_read_lock().
74 static int tomoyo_mount_acl(struct tomoyo_request_info *r, char *dev_name,
75 struct path *dir, const char *type,
79 struct file_system_type *fstype = NULL;
80 const char *requested_type = NULL;
81 const char *requested_dir_name = NULL;
82 const char *requested_dev_name = NULL;
83 struct tomoyo_path_info rtype;
84 struct tomoyo_path_info rdev;
85 struct tomoyo_path_info rdir;
90 requested_type = tomoyo_encode(type);
93 rtype.name = requested_type;
94 tomoyo_fill_path_info(&rtype);
96 /* Get mount point. */
97 requested_dir_name = tomoyo_realpath_from_path(dir);
98 if (!requested_dir_name) {
102 rdir.name = requested_dir_name;
103 tomoyo_fill_path_info(&rdir);
105 /* Compare fs name. */
106 if (type == tomoyo_mounts[TOMOYO_MOUNT_REMOUNT]) {
107 /* dev_name is ignored. */
108 } else if (type == tomoyo_mounts[TOMOYO_MOUNT_MAKE_UNBINDABLE] ||
109 type == tomoyo_mounts[TOMOYO_MOUNT_MAKE_PRIVATE] ||
110 type == tomoyo_mounts[TOMOYO_MOUNT_MAKE_SLAVE] ||
111 type == tomoyo_mounts[TOMOYO_MOUNT_MAKE_SHARED]) {
112 /* dev_name is ignored. */
113 } else if (type == tomoyo_mounts[TOMOYO_MOUNT_BIND] ||
114 type == tomoyo_mounts[TOMOYO_MOUNT_MOVE]) {
115 need_dev = -1; /* dev_name is a directory */
117 fstype = get_fs_type(type);
122 if (fstype->fs_flags & FS_REQUIRES_DEV)
123 /* dev_name is a block device file. */
127 /* Get mount point or device file. */
128 if (!dev_name || kern_path(dev_name, LOOKUP_FOLLOW, &path)) {
132 requested_dev_name = tomoyo_realpath_from_path(&path);
134 if (!requested_dev_name) {
139 /* Map dev_name to "<NULL>" if no dev_name given. */
142 requested_dev_name = tomoyo_encode(dev_name);
143 if (!requested_dev_name) {
148 rdev.name = requested_dev_name;
149 tomoyo_fill_path_info(&rdev);
150 r->param_type = TOMOYO_TYPE_MOUNT_ACL;
151 r->param.mount.need_dev = need_dev;
152 r->param.mount.dev = &rdev;
153 r->param.mount.dir = &rdir;
154 r->param.mount.type = &rtype;
155 r->param.mount.flags = flags;
157 tomoyo_check_acl(r, tomoyo_check_mount_acl);
158 error = tomoyo_audit_mount_log(r);
159 } while (error == TOMOYO_RETRY_REQUEST);
161 kfree(requested_dev_name);
162 kfree(requested_dir_name);
164 put_filesystem(fstype);
165 kfree(requested_type);
170 * tomoyo_mount_permission - Check permission for mount() operation.
172 * @dev_name: Name of device file.
173 * @path: Pointer to "struct path".
174 * @type: Name of filesystem type. May be NULL.
175 * @flags: Mount options.
176 * @data_page: Optional data. May be NULL.
178 * Returns 0 on success, negative value otherwise.
180 int tomoyo_mount_permission(char *dev_name, struct path *path,
181 const char *type, unsigned long flags,
184 struct tomoyo_request_info r;
188 if (tomoyo_init_request_info(&r, NULL, TOMOYO_MAC_FILE_MOUNT)
189 == TOMOYO_CONFIG_DISABLED)
191 if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
192 flags &= ~MS_MGC_MSK;
193 if (flags & MS_REMOUNT) {
194 type = tomoyo_mounts[TOMOYO_MOUNT_REMOUNT];
195 flags &= ~MS_REMOUNT;
197 if (flags & MS_MOVE) {
198 type = tomoyo_mounts[TOMOYO_MOUNT_MOVE];
201 if (flags & MS_BIND) {
202 type = tomoyo_mounts[TOMOYO_MOUNT_BIND];
205 if (flags & MS_UNBINDABLE) {
206 type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_UNBINDABLE];
207 flags &= ~MS_UNBINDABLE;
209 if (flags & MS_PRIVATE) {
210 type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_PRIVATE];
211 flags &= ~MS_PRIVATE;
213 if (flags & MS_SLAVE) {
214 type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_SLAVE];
217 if (flags & MS_SHARED) {
218 type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_SHARED];
223 idx = tomoyo_read_lock();
224 error = tomoyo_mount_acl(&r, dev_name, path, type, flags);
225 tomoyo_read_unlock(idx);