TOMOYO: Cleanup part 2.
[linux-block.git] / security / tomoyo / mount.c
CommitLineData
2106ccd9
TH
1/*
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
b5bc60b4
TH
10/* 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};
2106ccd9 20
99a85259
TH
21/**
22 * 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;
b5bc60b4 36 if (type == tomoyo_mounts[TOMOYO_MOUNT_REMOUNT])
99a85259 37 tomoyo_warn_log(r, "mount -o remount %s 0x%lX", dir, flags);
b5bc60b4
TH
38 else if (type == tomoyo_mounts[TOMOYO_MOUNT_BIND]
39 || type == tomoyo_mounts[TOMOYO_MOUNT_MOVE])
99a85259
TH
40 tomoyo_warn_log(r, "mount %s %s %s 0x%lX", type, dev, dir,
41 flags);
b5bc60b4
TH
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])
99a85259
TH
46 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);
b5bc60b4 50 return tomoyo_supervisor(r, "allow_mount %s %s %s 0x%lX\n",
7c75964f
TH
51 r->param.mount.dev->name,
52 r->param.mount.dir->name, type, flags);
99a85259
TH
53}
54
484ca79c 55static bool tomoyo_check_mount_acl(struct tomoyo_request_info *r,
99a85259
TH
56 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
2106ccd9 67/**
d795ef9e 68 * tomoyo_mount_acl - Check permission for mount() operation.
2106ccd9
TH
69 *
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 */
d795ef9e 80static int tomoyo_mount_acl(struct tomoyo_request_info *r, char *dev_name,
b5bc60b4
TH
81 struct path *dir, const char *type,
82 unsigned long flags)
2106ccd9
TH
83{
84 struct path path;
2106ccd9
TH
85 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. */
c8c57e84 96 requested_type = tomoyo_encode(type);
2106ccd9
TH
97 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. */
b5bc60b4 112 if (type == tomoyo_mounts[TOMOYO_MOUNT_REMOUNT]) {
2106ccd9 113 /* dev_name is ignored. */
b5bc60b4
TH
114 } 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]) {
2106ccd9 118 /* dev_name is ignored. */
b5bc60b4
TH
119 } else if (type == tomoyo_mounts[TOMOYO_MOUNT_BIND] ||
120 type == tomoyo_mounts[TOMOYO_MOUNT_MOVE]) {
2106ccd9
TH
121 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);
db5ca356 139 path_put(&path);
2106ccd9
TH
140 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>";
c8c57e84 148 requested_dev_name = tomoyo_encode(dev_name);
2106ccd9
TH
149 if (!requested_dev_name) {
150 error = -ENOMEM;
151 goto out;
152 }
153 }
154 rdev.name = requested_dev_name;
155 tomoyo_fill_path_info(&rdev);
cf6e9a64
TH
156 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;
99a85259
TH
162 do {
163 tomoyo_check_acl(r, tomoyo_check_mount_acl);
164 error = tomoyo_audit_mount_log(r);
165 } while (error == TOMOYO_RETRY_REQUEST);
2106ccd9
TH
166 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
2106ccd9
TH
175/**
176 * 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 */
b5bc60b4
TH
186int tomoyo_mount_permission(char *dev_name, struct path *path,
187 const char *type, unsigned long flags,
188 void *data_page)
2106ccd9
TH
189{
190 struct tomoyo_request_info r;
191 int error;
192 int idx;
193
57c2590f
TH
194 if (tomoyo_init_request_info(&r, NULL, TOMOYO_MAC_FILE_MOUNT)
195 == TOMOYO_CONFIG_DISABLED)
2106ccd9 196 return 0;
d795ef9e
TH
197 if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
198 flags &= ~MS_MGC_MSK;
199 if (flags & MS_REMOUNT) {
b5bc60b4 200 type = tomoyo_mounts[TOMOYO_MOUNT_REMOUNT];
d795ef9e
TH
201 flags &= ~MS_REMOUNT;
202 }
203 if (flags & MS_MOVE) {
b5bc60b4 204 type = tomoyo_mounts[TOMOYO_MOUNT_MOVE];
d795ef9e
TH
205 flags &= ~MS_MOVE;
206 }
207 if (flags & MS_BIND) {
b5bc60b4 208 type = tomoyo_mounts[TOMOYO_MOUNT_BIND];
d795ef9e
TH
209 flags &= ~MS_BIND;
210 }
211 if (flags & MS_UNBINDABLE) {
b5bc60b4 212 type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_UNBINDABLE];
d795ef9e
TH
213 flags &= ~MS_UNBINDABLE;
214 }
215 if (flags & MS_PRIVATE) {
b5bc60b4 216 type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_PRIVATE];
d795ef9e
TH
217 flags &= ~MS_PRIVATE;
218 }
219 if (flags & MS_SLAVE) {
b5bc60b4 220 type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_SLAVE];
d795ef9e
TH
221 flags &= ~MS_SLAVE;
222 }
223 if (flags & MS_SHARED) {
b5bc60b4 224 type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_SHARED];
d795ef9e
TH
225 flags &= ~MS_SHARED;
226 }
2106ccd9
TH
227 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
237ab459
TH
235static 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);
75093152
TH
240 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);
237ab459
TH
245}
246
2106ccd9 247/**
e2bf6907 248 * tomoyo_write_mount - Write "struct tomoyo_mount_acl" list.
2106ccd9
TH
249 *
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.
237ab459
TH
255 *
256 * Caller holds tomoyo_read_lock().
2106ccd9 257 */
e2bf6907
TH
258int tomoyo_write_mount(char *data, struct tomoyo_domain_info *domain,
259 const bool is_delete)
2106ccd9 260{
2106ccd9
TH
261 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;
237ab459
TH
271 error = tomoyo_update_domain(&e.head, sizeof(e), is_delete, domain,
272 tomoyo_same_mount_acl, NULL);
2106ccd9
TH
273 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}