blob: 5cfc720787428e24a7395bf221bab589510e90a6 [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 Handa484ca792010-07-29 14:29:55 +090055static bool tomoyo_check_mount_acl(struct tomoyo_request_info *r,
Tetsuo Handa99a85252010-06-16 16:22:51 +090056 const struct tomoyo_acl_info *ptr)
57{
58 const struct tomoyo_mount_acl *acl =
59 container_of(ptr, typeof(*acl), head);
60 return tomoyo_compare_number_union(r->param.mount.flags, &acl->flags) &&
61 tomoyo_compare_name_union(r->param.mount.type, &acl->fs_type) &&
62 tomoyo_compare_name_union(r->param.mount.dir, &acl->dir_name) &&
63 (!r->param.mount.need_dev ||
64 tomoyo_compare_name_union(r->param.mount.dev, &acl->dev_name));
65}
66
67/**
Tetsuo Handad795ef9e2010-06-16 16:24:58 +090068 * tomoyo_mount_acl - Check permission for mount() operation.
Tetsuo Handa2106ccd2010-05-17 10:10:31 +090069 *
70 * @r: Pointer to "struct tomoyo_request_info".
71 * @dev_name: Name of device file.
72 * @dir: Pointer to "struct path".
73 * @type: Name of filesystem type.
74 * @flags: Mount options.
75 *
76 * Returns 0 on success, negative value otherwise.
77 *
78 * Caller holds tomoyo_read_lock().
79 */
Tetsuo Handad795ef9e2010-06-16 16:24:58 +090080static int tomoyo_mount_acl(struct tomoyo_request_info *r, char *dev_name,
Tetsuo Handab5bc60b2011-06-26 23:16:03 +090081 struct path *dir, const char *type,
82 unsigned long flags)
Tetsuo Handa2106ccd2010-05-17 10:10:31 +090083{
84 struct path path;
Tetsuo Handa2106ccd2010-05-17 10:10:31 +090085 struct file_system_type *fstype = NULL;
86 const char *requested_type = NULL;
87 const char *requested_dir_name = NULL;
88 const char *requested_dev_name = NULL;
89 struct tomoyo_path_info rtype;
90 struct tomoyo_path_info rdev;
91 struct tomoyo_path_info rdir;
92 int need_dev = 0;
93 int error = -ENOMEM;
94
95 /* Get fstype. */
Tetsuo Handac8c57e82010-06-03 20:36:43 +090096 requested_type = tomoyo_encode(type);
Tetsuo Handa2106ccd2010-05-17 10:10:31 +090097 if (!requested_type)
98 goto out;
99 rtype.name = requested_type;
100 tomoyo_fill_path_info(&rtype);
101
102 /* Get mount point. */
103 requested_dir_name = tomoyo_realpath_from_path(dir);
104 if (!requested_dir_name) {
105 error = -ENOMEM;
106 goto out;
107 }
108 rdir.name = requested_dir_name;
109 tomoyo_fill_path_info(&rdir);
110
111 /* Compare fs name. */
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900112 if (type == tomoyo_mounts[TOMOYO_MOUNT_REMOUNT]) {
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900113 /* dev_name is ignored. */
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900114 } else if (type == tomoyo_mounts[TOMOYO_MOUNT_MAKE_UNBINDABLE] ||
115 type == tomoyo_mounts[TOMOYO_MOUNT_MAKE_PRIVATE] ||
116 type == tomoyo_mounts[TOMOYO_MOUNT_MAKE_SLAVE] ||
117 type == tomoyo_mounts[TOMOYO_MOUNT_MAKE_SHARED]) {
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900118 /* dev_name is ignored. */
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900119 } else if (type == tomoyo_mounts[TOMOYO_MOUNT_BIND] ||
120 type == tomoyo_mounts[TOMOYO_MOUNT_MOVE]) {
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900121 need_dev = -1; /* dev_name is a directory */
122 } else {
123 fstype = get_fs_type(type);
124 if (!fstype) {
125 error = -ENODEV;
126 goto out;
127 }
128 if (fstype->fs_flags & FS_REQUIRES_DEV)
129 /* dev_name is a block device file. */
130 need_dev = 1;
131 }
132 if (need_dev) {
133 /* Get mount point or device file. */
134 if (kern_path(dev_name, LOOKUP_FOLLOW, &path)) {
135 error = -ENOENT;
136 goto out;
137 }
138 requested_dev_name = tomoyo_realpath_from_path(&path);
Tetsuo Handadb5ca352011-04-20 06:49:15 +0900139 path_put(&path);
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900140 if (!requested_dev_name) {
141 error = -ENOENT;
142 goto out;
143 }
144 } else {
145 /* Map dev_name to "<NULL>" if no dev_name given. */
146 if (!dev_name)
147 dev_name = "<NULL>";
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900148 requested_dev_name = tomoyo_encode(dev_name);
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900149 if (!requested_dev_name) {
150 error = -ENOMEM;
151 goto out;
152 }
153 }
154 rdev.name = requested_dev_name;
155 tomoyo_fill_path_info(&rdev);
Tetsuo Handacf6e9a62010-06-16 16:21:36 +0900156 r->param_type = TOMOYO_TYPE_MOUNT_ACL;
157 r->param.mount.need_dev = need_dev;
158 r->param.mount.dev = &rdev;
159 r->param.mount.dir = &rdir;
160 r->param.mount.type = &rtype;
161 r->param.mount.flags = flags;
Tetsuo Handa99a85252010-06-16 16:22:51 +0900162 do {
163 tomoyo_check_acl(r, tomoyo_check_mount_acl);
164 error = tomoyo_audit_mount_log(r);
165 } while (error == TOMOYO_RETRY_REQUEST);
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900166 out:
167 kfree(requested_dev_name);
168 kfree(requested_dir_name);
169 if (fstype)
170 put_filesystem(fstype);
171 kfree(requested_type);
172 return error;
173}
174
175/**
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900176 * tomoyo_mount_permission - Check permission for mount() operation.
177 *
178 * @dev_name: Name of device file.
179 * @path: Pointer to "struct path".
180 * @type: Name of filesystem type. May be NULL.
181 * @flags: Mount options.
182 * @data_page: Optional data. May be NULL.
183 *
184 * Returns 0 on success, negative value otherwise.
185 */
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900186int tomoyo_mount_permission(char *dev_name, struct path *path,
187 const char *type, unsigned long flags,
188 void *data_page)
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900189{
190 struct tomoyo_request_info r;
191 int error;
192 int idx;
193
Tetsuo Handa57c25902010-06-03 20:38:44 +0900194 if (tomoyo_init_request_info(&r, NULL, TOMOYO_MAC_FILE_MOUNT)
195 == TOMOYO_CONFIG_DISABLED)
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900196 return 0;
Tetsuo Handad795ef9e2010-06-16 16:24:58 +0900197 if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
198 flags &= ~MS_MGC_MSK;
199 if (flags & MS_REMOUNT) {
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900200 type = tomoyo_mounts[TOMOYO_MOUNT_REMOUNT];
Tetsuo Handad795ef9e2010-06-16 16:24:58 +0900201 flags &= ~MS_REMOUNT;
202 }
203 if (flags & MS_MOVE) {
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900204 type = tomoyo_mounts[TOMOYO_MOUNT_MOVE];
Tetsuo Handad795ef9e2010-06-16 16:24:58 +0900205 flags &= ~MS_MOVE;
206 }
207 if (flags & MS_BIND) {
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900208 type = tomoyo_mounts[TOMOYO_MOUNT_BIND];
Tetsuo Handad795ef9e2010-06-16 16:24:58 +0900209 flags &= ~MS_BIND;
210 }
211 if (flags & MS_UNBINDABLE) {
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900212 type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_UNBINDABLE];
Tetsuo Handad795ef9e2010-06-16 16:24:58 +0900213 flags &= ~MS_UNBINDABLE;
214 }
215 if (flags & MS_PRIVATE) {
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900216 type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_PRIVATE];
Tetsuo Handad795ef9e2010-06-16 16:24:58 +0900217 flags &= ~MS_PRIVATE;
218 }
219 if (flags & MS_SLAVE) {
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900220 type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_SLAVE];
Tetsuo Handad795ef9e2010-06-16 16:24:58 +0900221 flags &= ~MS_SLAVE;
222 }
223 if (flags & MS_SHARED) {
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900224 type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_SHARED];
Tetsuo Handad795ef9e2010-06-16 16:24:58 +0900225 flags &= ~MS_SHARED;
226 }
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900227 if (!type)
228 type = "<NULL>";
229 idx = tomoyo_read_lock();
230 error = tomoyo_mount_acl(&r, dev_name, path, type, flags);
231 tomoyo_read_unlock(idx);
232 return error;
233}
234
Tetsuo Handa237ab452010-06-12 20:46:22 +0900235static bool tomoyo_same_mount_acl(const struct tomoyo_acl_info *a,
236 const struct tomoyo_acl_info *b)
237{
238 const struct tomoyo_mount_acl *p1 = container_of(a, typeof(*p1), head);
239 const struct tomoyo_mount_acl *p2 = container_of(b, typeof(*p2), head);
Tetsuo Handa75093152010-06-16 16:23:55 +0900240 return tomoyo_same_acl_head(&p1->head, &p2->head) &&
241 tomoyo_same_name_union(&p1->dev_name, &p2->dev_name) &&
242 tomoyo_same_name_union(&p1->dir_name, &p2->dir_name) &&
243 tomoyo_same_name_union(&p1->fs_type, &p2->fs_type) &&
244 tomoyo_same_number_union(&p1->flags, &p2->flags);
Tetsuo Handa237ab452010-06-12 20:46:22 +0900245}
246
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900247/**
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900248 * tomoyo_write_mount - Write "struct tomoyo_mount_acl" list.
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900249 *
250 * @data: String to parse.
251 * @domain: Pointer to "struct tomoyo_domain_info".
252 * @is_delete: True if it is a delete request.
253 *
254 * Returns 0 on success, negative value otherwise.
Tetsuo Handa237ab452010-06-12 20:46:22 +0900255 *
256 * Caller holds tomoyo_read_lock().
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900257 */
Tetsuo Handae2bf6902010-06-25 11:16:00 +0900258int tomoyo_write_mount(char *data, struct tomoyo_domain_info *domain,
259 const bool is_delete)
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900260{
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900261 struct tomoyo_mount_acl e = { .head.type = TOMOYO_TYPE_MOUNT_ACL };
262 int error = is_delete ? -ENOENT : -ENOMEM;
263 char *w[4];
264 if (!tomoyo_tokenize(data, w, sizeof(w)) || !w[3][0])
265 return -EINVAL;
266 if (!tomoyo_parse_name_union(w[0], &e.dev_name) ||
267 !tomoyo_parse_name_union(w[1], &e.dir_name) ||
268 !tomoyo_parse_name_union(w[2], &e.fs_type) ||
269 !tomoyo_parse_number_union(w[3], &e.flags))
270 goto out;
Tetsuo Handa237ab452010-06-12 20:46:22 +0900271 error = tomoyo_update_domain(&e.head, sizeof(e), is_delete, domain,
272 tomoyo_same_mount_acl, NULL);
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900273 out:
274 tomoyo_put_name_union(&e.dev_name);
275 tomoyo_put_name_union(&e.dir_name);
276 tomoyo_put_name_union(&e.fs_type);
277 tomoyo_put_number_union(&e.flags);
278 return error;
279}