blob: 1e610f96c99db6d6904bb0214c0b2a6bb731c387 [file] [log] [blame]
Tetsuo Handa2106ccd2010-05-17 10:10:31 +09001/*
2 * security/tomoyo/mount.c
3 *
4 * Copyright (C) 2005-2010 NTT DATA CORPORATION
5 */
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{
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;
34 if (r->granted)
35 return 0;
Tetsuo Handab5bc60b2011-06-26 23:16:03 +090036 if (type == tomoyo_mounts[TOMOYO_MOUNT_REMOUNT])
Tetsuo Handa99a85252010-06-16 16:22:51 +090037 tomoyo_warn_log(r, "mount -o remount %s 0x%lX", dir, flags);
Tetsuo Handab5bc60b2011-06-26 23:16:03 +090038 else if (type == tomoyo_mounts[TOMOYO_MOUNT_BIND]
39 || type == tomoyo_mounts[TOMOYO_MOUNT_MOVE])
Tetsuo Handa99a85252010-06-16 16:22:51 +090040 tomoyo_warn_log(r, "mount %s %s %s 0x%lX", type, dev, dir,
41 flags);
Tetsuo Handab5bc60b2011-06-26 23:16:03 +090042 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])
Tetsuo Handa99a85252010-06-16 16:22:51 +090046 tomoyo_warn_log(r, "mount %s %s 0x%lX", type, dir, flags);
47 else
48 tomoyo_warn_log(r, "mount -t %s %s %s 0x%lX", type, dev, dir,
49 flags);
Tetsuo Handab5bc60b2011-06-26 23:16:03 +090050 return tomoyo_supervisor(r, "allow_mount %s %s %s 0x%lX\n",
Tetsuo Handa7c759642011-06-26 23:15:31 +090051 r->param.mount.dev->name,
52 r->param.mount.dir->name, type, flags);
Tetsuo Handa99a85252010-06-16 16:22:51 +090053}
54
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +090055/**
56 * tomoyo_check_mount_acl - Check permission for path path path number operation.
57 *
58 * @r: Pointer to "struct tomoyo_request_info".
59 * @ptr: Pointer to "struct tomoyo_acl_info".
60 *
61 * Returns true if granted, false otherwise.
62 */
Tetsuo Handa484ca792010-07-29 14:29:55 +090063static bool tomoyo_check_mount_acl(struct tomoyo_request_info *r,
Tetsuo Handa99a85252010-06-16 16:22:51 +090064 const struct tomoyo_acl_info *ptr)
65{
66 const struct tomoyo_mount_acl *acl =
67 container_of(ptr, typeof(*acl), head);
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +090068 return tomoyo_compare_number_union(r->param.mount.flags,
69 &acl->flags) &&
70 tomoyo_compare_name_union(r->param.mount.type,
71 &acl->fs_type) &&
72 tomoyo_compare_name_union(r->param.mount.dir,
73 &acl->dir_name) &&
Tetsuo Handa99a85252010-06-16 16:22:51 +090074 (!r->param.mount.need_dev ||
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +090075 tomoyo_compare_name_union(r->param.mount.dev,
76 &acl->dev_name));
Tetsuo Handa99a85252010-06-16 16:22:51 +090077}
78
79/**
Tetsuo Handad795ef9e2010-06-16 16:24:58 +090080 * tomoyo_mount_acl - Check permission for mount() operation.
Tetsuo Handa2106ccd2010-05-17 10:10:31 +090081 *
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.
87 *
88 * Returns 0 on success, negative value otherwise.
89 *
90 * Caller holds tomoyo_read_lock().
91 */
Tetsuo Handad795ef9e2010-06-16 16:24:58 +090092static int tomoyo_mount_acl(struct tomoyo_request_info *r, char *dev_name,
Tetsuo Handab5bc60b2011-06-26 23:16:03 +090093 struct path *dir, const char *type,
94 unsigned long flags)
Tetsuo Handa2106ccd2010-05-17 10:10:31 +090095{
96 struct path path;
Tetsuo Handa2106ccd2010-05-17 10:10:31 +090097 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;
104 int need_dev = 0;
105 int error = -ENOMEM;
106
107 /* Get fstype. */
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900108 requested_type = tomoyo_encode(type);
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900109 if (!requested_type)
110 goto out;
111 rtype.name = requested_type;
112 tomoyo_fill_path_info(&rtype);
113
114 /* Get mount point. */
115 requested_dir_name = tomoyo_realpath_from_path(dir);
116 if (!requested_dir_name) {
117 error = -ENOMEM;
118 goto out;
119 }
120 rdir.name = requested_dir_name;
121 tomoyo_fill_path_info(&rdir);
122
123 /* Compare fs name. */
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900124 if (type == tomoyo_mounts[TOMOYO_MOUNT_REMOUNT]) {
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900125 /* dev_name is ignored. */
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900126 } 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]) {
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900130 /* dev_name is ignored. */
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900131 } else if (type == tomoyo_mounts[TOMOYO_MOUNT_BIND] ||
132 type == tomoyo_mounts[TOMOYO_MOUNT_MOVE]) {
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900133 need_dev = -1; /* dev_name is a directory */
134 } else {
135 fstype = get_fs_type(type);
136 if (!fstype) {
137 error = -ENODEV;
138 goto out;
139 }
140 if (fstype->fs_flags & FS_REQUIRES_DEV)
141 /* dev_name is a block device file. */
142 need_dev = 1;
143 }
144 if (need_dev) {
145 /* Get mount point or device file. */
146 if (kern_path(dev_name, LOOKUP_FOLLOW, &path)) {
147 error = -ENOENT;
148 goto out;
149 }
150 requested_dev_name = tomoyo_realpath_from_path(&path);
Tetsuo Handadb5ca352011-04-20 06:49:15 +0900151 path_put(&path);
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900152 if (!requested_dev_name) {
153 error = -ENOENT;
154 goto out;
155 }
156 } else {
157 /* Map dev_name to "<NULL>" if no dev_name given. */
158 if (!dev_name)
159 dev_name = "<NULL>";
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900160 requested_dev_name = tomoyo_encode(dev_name);
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900161 if (!requested_dev_name) {
162 error = -ENOMEM;
163 goto out;
164 }
165 }
166 rdev.name = requested_dev_name;
167 tomoyo_fill_path_info(&rdev);
Tetsuo Handacf6e9a62010-06-16 16:21:36 +0900168 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;
Tetsuo Handa99a85252010-06-16 16:22:51 +0900174 do {
175 tomoyo_check_acl(r, tomoyo_check_mount_acl);
176 error = tomoyo_audit_mount_log(r);
177 } while (error == TOMOYO_RETRY_REQUEST);
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900178 out:
179 kfree(requested_dev_name);
180 kfree(requested_dir_name);
181 if (fstype)
182 put_filesystem(fstype);
183 kfree(requested_type);
184 return error;
185}
186
187/**
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900188 * tomoyo_mount_permission - Check permission for mount() operation.
189 *
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.
195 *
196 * Returns 0 on success, negative value otherwise.
197 */
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900198int tomoyo_mount_permission(char *dev_name, struct path *path,
199 const char *type, unsigned long flags,
200 void *data_page)
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900201{
202 struct tomoyo_request_info r;
203 int error;
204 int idx;
205
Tetsuo Handa57c25902010-06-03 20:38:44 +0900206 if (tomoyo_init_request_info(&r, NULL, TOMOYO_MAC_FILE_MOUNT)
207 == TOMOYO_CONFIG_DISABLED)
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900208 return 0;
Tetsuo Handad795ef9e2010-06-16 16:24:58 +0900209 if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
210 flags &= ~MS_MGC_MSK;
211 if (flags & MS_REMOUNT) {
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900212 type = tomoyo_mounts[TOMOYO_MOUNT_REMOUNT];
Tetsuo Handad795ef9e2010-06-16 16:24:58 +0900213 flags &= ~MS_REMOUNT;
214 }
215 if (flags & MS_MOVE) {
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900216 type = tomoyo_mounts[TOMOYO_MOUNT_MOVE];
Tetsuo Handad795ef9e2010-06-16 16:24:58 +0900217 flags &= ~MS_MOVE;
218 }
219 if (flags & MS_BIND) {
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900220 type = tomoyo_mounts[TOMOYO_MOUNT_BIND];
Tetsuo Handad795ef9e2010-06-16 16:24:58 +0900221 flags &= ~MS_BIND;
222 }
223 if (flags & MS_UNBINDABLE) {
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900224 type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_UNBINDABLE];
Tetsuo Handad795ef9e2010-06-16 16:24:58 +0900225 flags &= ~MS_UNBINDABLE;
226 }
227 if (flags & MS_PRIVATE) {
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900228 type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_PRIVATE];
Tetsuo Handad795ef9e2010-06-16 16:24:58 +0900229 flags &= ~MS_PRIVATE;
230 }
231 if (flags & MS_SLAVE) {
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900232 type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_SLAVE];
Tetsuo Handad795ef9e2010-06-16 16:24:58 +0900233 flags &= ~MS_SLAVE;
234 }
235 if (flags & MS_SHARED) {
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900236 type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_SHARED];
Tetsuo Handad795ef9e2010-06-16 16:24:58 +0900237 flags &= ~MS_SHARED;
238 }
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900239 if (!type)
240 type = "<NULL>";
241 idx = tomoyo_read_lock();
242 error = tomoyo_mount_acl(&r, dev_name, path, type, flags);
243 tomoyo_read_unlock(idx);
244 return error;
245}