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 const char *dev = r->param.mount.dev->name;
31 const char *dir = r->param.mount.dir->name;
32 const char *type = r->param.mount.type->name;
33 const unsigned long flags = r->param.mount.flags;
36 if (type == tomoyo_mounts[TOMOYO_MOUNT_REMOUNT])
37 tomoyo_warn_log(r, "mount -o remount %s 0x%lX", dir, flags);
38 else if (type == tomoyo_mounts[TOMOYO_MOUNT_BIND]
39 || type == tomoyo_mounts[TOMOYO_MOUNT_MOVE])
40 tomoyo_warn_log(r, "mount %s %s %s 0x%lX", type, dev, dir,
42 else if (type == tomoyo_mounts[TOMOYO_MOUNT_MAKE_UNBINDABLE] ||
43 type == tomoyo_mounts[TOMOYO_MOUNT_MAKE_PRIVATE] ||
44 type == tomoyo_mounts[TOMOYO_MOUNT_MAKE_SLAVE] ||
45 type == tomoyo_mounts[TOMOYO_MOUNT_MAKE_SHARED])
46 tomoyo_warn_log(r, "mount %s %s 0x%lX", type, dir, flags);
48 tomoyo_warn_log(r, "mount -t %s %s %s 0x%lX", type, dev, dir,
50 return tomoyo_supervisor(r, "allow_mount %s %s %s 0x%lX\n",
51 r->param.mount.dev->name,
52 r->param.mount.dir->name, type, flags);
56 * tomoyo_check_mount_acl - Check permission for path path path number operation.
58 * @r: Pointer to "struct tomoyo_request_info".
59 * @ptr: Pointer to "struct tomoyo_acl_info".
61 * Returns true if granted, false otherwise.
63 static bool tomoyo_check_mount_acl(struct tomoyo_request_info *r,
64 const struct tomoyo_acl_info *ptr)
66 const struct tomoyo_mount_acl *acl =
67 container_of(ptr, typeof(*acl), head);
68 return tomoyo_compare_number_union(r->param.mount.flags,
70 tomoyo_compare_name_union(r->param.mount.type,
72 tomoyo_compare_name_union(r->param.mount.dir,
74 (!r->param.mount.need_dev ||
75 tomoyo_compare_name_union(r->param.mount.dev,
80 * tomoyo_mount_acl - Check permission for mount() operation.
82 * @r: Pointer to "struct tomoyo_request_info".
83 * @dev_name: Name of device file.
84 * @dir: Pointer to "struct path".
85 * @type: Name of filesystem type.
86 * @flags: Mount options.
88 * Returns 0 on success, negative value otherwise.
90 * Caller holds tomoyo_read_lock().
92 static int tomoyo_mount_acl(struct tomoyo_request_info *r, char *dev_name,
93 struct path *dir, const char *type,
97 struct file_system_type *fstype = NULL;
98 const char *requested_type = NULL;
99 const char *requested_dir_name = NULL;
100 const char *requested_dev_name = NULL;
101 struct tomoyo_path_info rtype;
102 struct tomoyo_path_info rdev;
103 struct tomoyo_path_info rdir;
108 requested_type = tomoyo_encode(type);
111 rtype.name = requested_type;
112 tomoyo_fill_path_info(&rtype);
114 /* Get mount point. */
115 requested_dir_name = tomoyo_realpath_from_path(dir);
116 if (!requested_dir_name) {
120 rdir.name = requested_dir_name;
121 tomoyo_fill_path_info(&rdir);
123 /* Compare fs name. */
124 if (type == tomoyo_mounts[TOMOYO_MOUNT_REMOUNT]) {
125 /* dev_name is ignored. */
126 } else if (type == tomoyo_mounts[TOMOYO_MOUNT_MAKE_UNBINDABLE] ||
127 type == tomoyo_mounts[TOMOYO_MOUNT_MAKE_PRIVATE] ||
128 type == tomoyo_mounts[TOMOYO_MOUNT_MAKE_SLAVE] ||
129 type == tomoyo_mounts[TOMOYO_MOUNT_MAKE_SHARED]) {
130 /* dev_name is ignored. */
131 } else if (type == tomoyo_mounts[TOMOYO_MOUNT_BIND] ||
132 type == tomoyo_mounts[TOMOYO_MOUNT_MOVE]) {
133 need_dev = -1; /* dev_name is a directory */
135 fstype = get_fs_type(type);
140 if (fstype->fs_flags & FS_REQUIRES_DEV)
141 /* dev_name is a block device file. */
145 /* Get mount point or device file. */
146 if (kern_path(dev_name, LOOKUP_FOLLOW, &path)) {
150 requested_dev_name = tomoyo_realpath_from_path(&path);
152 if (!requested_dev_name) {
157 /* Map dev_name to "<NULL>" if no dev_name given. */
160 requested_dev_name = tomoyo_encode(dev_name);
161 if (!requested_dev_name) {
166 rdev.name = requested_dev_name;
167 tomoyo_fill_path_info(&rdev);
168 r->param_type = TOMOYO_TYPE_MOUNT_ACL;
169 r->param.mount.need_dev = need_dev;
170 r->param.mount.dev = &rdev;
171 r->param.mount.dir = &rdir;
172 r->param.mount.type = &rtype;
173 r->param.mount.flags = flags;
175 tomoyo_check_acl(r, tomoyo_check_mount_acl);
176 error = tomoyo_audit_mount_log(r);
177 } while (error == TOMOYO_RETRY_REQUEST);
179 kfree(requested_dev_name);
180 kfree(requested_dir_name);
182 put_filesystem(fstype);
183 kfree(requested_type);
188 * tomoyo_mount_permission - Check permission for mount() operation.
190 * @dev_name: Name of device file.
191 * @path: Pointer to "struct path".
192 * @type: Name of filesystem type. May be NULL.
193 * @flags: Mount options.
194 * @data_page: Optional data. May be NULL.
196 * Returns 0 on success, negative value otherwise.
198 int tomoyo_mount_permission(char *dev_name, struct path *path,
199 const char *type, unsigned long flags,
202 struct tomoyo_request_info r;
206 if (tomoyo_init_request_info(&r, NULL, TOMOYO_MAC_FILE_MOUNT)
207 == TOMOYO_CONFIG_DISABLED)
209 if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
210 flags &= ~MS_MGC_MSK;
211 if (flags & MS_REMOUNT) {
212 type = tomoyo_mounts[TOMOYO_MOUNT_REMOUNT];
213 flags &= ~MS_REMOUNT;
215 if (flags & MS_MOVE) {
216 type = tomoyo_mounts[TOMOYO_MOUNT_MOVE];
219 if (flags & MS_BIND) {
220 type = tomoyo_mounts[TOMOYO_MOUNT_BIND];
223 if (flags & MS_UNBINDABLE) {
224 type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_UNBINDABLE];
225 flags &= ~MS_UNBINDABLE;
227 if (flags & MS_PRIVATE) {
228 type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_PRIVATE];
229 flags &= ~MS_PRIVATE;
231 if (flags & MS_SLAVE) {
232 type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_SLAVE];
235 if (flags & MS_SHARED) {
236 type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_SHARED];
241 idx = tomoyo_read_lock();
242 error = tomoyo_mount_acl(&r, dev_name, path, type, flags);
243 tomoyo_read_unlock(idx);