2 * security/tomoyo/mount.c
4 * Copyright (C) 2005-2010 NTT DATA CORPORATION
7 #include <linux/slab.h>
10 /* Keywords for mount restrictions. */
12 /* Allow to call 'mount --bind /source_dir /dest_dir' */
13 #define TOMOYO_MOUNT_BIND_KEYWORD "--bind"
14 /* Allow to call 'mount --move /old_dir /new_dir ' */
15 #define TOMOYO_MOUNT_MOVE_KEYWORD "--move"
16 /* Allow to call 'mount -o remount /dir ' */
17 #define TOMOYO_MOUNT_REMOUNT_KEYWORD "--remount"
18 /* Allow to call 'mount --make-unbindable /dir' */
19 #define TOMOYO_MOUNT_MAKE_UNBINDABLE_KEYWORD "--make-unbindable"
20 /* Allow to call 'mount --make-private /dir' */
21 #define TOMOYO_MOUNT_MAKE_PRIVATE_KEYWORD "--make-private"
22 /* Allow to call 'mount --make-slave /dir' */
23 #define TOMOYO_MOUNT_MAKE_SLAVE_KEYWORD "--make-slave"
24 /* Allow to call 'mount --make-shared /dir' */
25 #define TOMOYO_MOUNT_MAKE_SHARED_KEYWORD "--make-shared"
28 * tomoyo_encode2: Encode binary string to ascii string.
30 * @str: String in binary format.
32 * Returns pointer to @str in ascii format on success, NULL otherwise.
34 * This function uses kzalloc(), so caller must kfree() if this function
37 static char *tomoyo_encode2(const char *str)
46 const unsigned char c = *p++;
49 else if (c > ' ' && c < 127)
55 /* Reserve space for appending "/". */
56 cp = kzalloc(len + 10, GFP_NOFS);
62 const unsigned char c = *p++;
66 } else if (c > ' ' && c < 127) {
70 *cp++ = (c >> 6) + '0';
71 *cp++ = ((c >> 3) & 7) + '0';
72 *cp++ = (c & 7) + '0';
79 * tomoyo_mount_acl2 - Check permission for mount() operation.
81 * @r: Pointer to "struct tomoyo_request_info".
82 * @dev_name: Name of device file.
83 * @dir: Pointer to "struct path".
84 * @type: Name of filesystem type.
85 * @flags: Mount options.
87 * Returns 0 on success, negative value otherwise.
89 * Caller holds tomoyo_read_lock().
91 static int tomoyo_mount_acl2(struct tomoyo_request_info *r, char *dev_name,
92 struct path *dir, char *type, unsigned long flags)
95 struct tomoyo_acl_info *ptr;
96 struct file_system_type *fstype = NULL;
97 const char *requested_type = NULL;
98 const char *requested_dir_name = NULL;
99 const char *requested_dev_name = NULL;
100 struct tomoyo_path_info rtype;
101 struct tomoyo_path_info rdev;
102 struct tomoyo_path_info rdir;
107 requested_type = tomoyo_encode2(type);
110 rtype.name = requested_type;
111 tomoyo_fill_path_info(&rtype);
113 /* Get mount point. */
114 requested_dir_name = tomoyo_realpath_from_path(dir);
115 if (!requested_dir_name) {
119 rdir.name = requested_dir_name;
120 tomoyo_fill_path_info(&rdir);
122 /* Compare fs name. */
123 if (!strcmp(type, TOMOYO_MOUNT_REMOUNT_KEYWORD)) {
124 /* dev_name is ignored. */
125 } else if (!strcmp(type, TOMOYO_MOUNT_MAKE_UNBINDABLE_KEYWORD) ||
126 !strcmp(type, TOMOYO_MOUNT_MAKE_PRIVATE_KEYWORD) ||
127 !strcmp(type, TOMOYO_MOUNT_MAKE_SLAVE_KEYWORD) ||
128 !strcmp(type, TOMOYO_MOUNT_MAKE_SHARED_KEYWORD)) {
129 /* dev_name is ignored. */
130 } else if (!strcmp(type, TOMOYO_MOUNT_BIND_KEYWORD) ||
131 !strcmp(type, TOMOYO_MOUNT_MOVE_KEYWORD)) {
132 need_dev = -1; /* dev_name is a directory */
134 fstype = get_fs_type(type);
139 if (fstype->fs_flags & FS_REQUIRES_DEV)
140 /* dev_name is a block device file. */
144 /* Get mount point or device file. */
145 if (kern_path(dev_name, LOOKUP_FOLLOW, &path)) {
149 requested_dev_name = tomoyo_realpath_from_path(&path);
150 if (!requested_dev_name) {
155 /* Map dev_name to "<NULL>" if no dev_name given. */
158 requested_dev_name = tomoyo_encode2(dev_name);
159 if (!requested_dev_name) {
164 rdev.name = requested_dev_name;
165 tomoyo_fill_path_info(&rdev);
166 list_for_each_entry_rcu(ptr, &r->domain->acl_info_list, list) {
167 struct tomoyo_mount_acl *acl;
168 if (ptr->type != TOMOYO_TYPE_MOUNT_ACL)
170 acl = container_of(ptr, struct tomoyo_mount_acl, head);
171 if (acl->is_deleted ||
172 !tomoyo_compare_number_union(flags, &acl->flags) ||
173 !tomoyo_compare_name_union(&rtype, &acl->fs_type) ||
174 !tomoyo_compare_name_union(&rdir, &acl->dir_name) ||
176 !tomoyo_compare_name_union(&rdev, &acl->dev_name)))
182 const char *dev = tomoyo_get_file_pattern(&rdev)->name;
183 const char *dir = tomoyo_get_file_pattern(&rdir)->name;
184 int len = strlen(dev) + strlen(dir) + strlen(requested_type)
186 char *buf = kzalloc(len, GFP_NOFS);
188 snprintf(buf, len - 1, "%s %s %s 0x%lX",
189 dev, dir, requested_type, flags);
190 tomoyo_write_mount_policy(buf, r->domain, false);
195 kfree(requested_dev_name);
196 kfree(requested_dir_name);
198 put_filesystem(fstype);
199 kfree(requested_type);
204 * tomoyo_mount_acl - Check permission for mount() operation.
206 * @r: Pointer to "struct tomoyo_request_info".
207 * @dev_name: Name of device file.
208 * @dir: Pointer to "struct path".
209 * @type: Name of filesystem type.
210 * @flags: Mount options.
212 * Returns 0 on success, negative value otherwise.
214 * Caller holds tomoyo_read_lock().
216 static int tomoyo_mount_acl(struct tomoyo_request_info *r, char *dev_name,
217 struct path *dir, char *type, unsigned long flags)
221 if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
222 flags &= ~MS_MGC_MSK;
223 switch (flags & (MS_REMOUNT | MS_MOVE | MS_BIND)) {
230 printk(KERN_WARNING "ERROR: "
231 "%s%s%sare given for single mount operation.\n",
232 flags & MS_REMOUNT ? "'remount' " : "",
233 flags & MS_MOVE ? "'move' " : "",
234 flags & MS_BIND ? "'bind' " : "");
237 switch (flags & (MS_UNBINDABLE | MS_PRIVATE | MS_SLAVE | MS_SHARED)) {
245 printk(KERN_WARNING "ERROR: "
246 "%s%s%s%sare given for single mount operation.\n",
247 flags & MS_UNBINDABLE ? "'unbindable' " : "",
248 flags & MS_PRIVATE ? "'private' " : "",
249 flags & MS_SLAVE ? "'slave' " : "",
250 flags & MS_SHARED ? "'shared' " : "");
253 if (flags & MS_REMOUNT)
254 error = tomoyo_mount_acl(r, dev_name, dir,
255 TOMOYO_MOUNT_REMOUNT_KEYWORD,
256 flags & ~MS_REMOUNT);
257 else if (flags & MS_MOVE)
258 error = tomoyo_mount_acl(r, dev_name, dir,
259 TOMOYO_MOUNT_MOVE_KEYWORD,
261 else if (flags & MS_BIND)
262 error = tomoyo_mount_acl(r, dev_name, dir,
263 TOMOYO_MOUNT_BIND_KEYWORD,
265 else if (flags & MS_UNBINDABLE)
266 error = tomoyo_mount_acl(r, dev_name, dir,
267 TOMOYO_MOUNT_MAKE_UNBINDABLE_KEYWORD,
268 flags & ~MS_UNBINDABLE);
269 else if (flags & MS_PRIVATE)
270 error = tomoyo_mount_acl(r, dev_name, dir,
271 TOMOYO_MOUNT_MAKE_PRIVATE_KEYWORD,
272 flags & ~MS_PRIVATE);
273 else if (flags & MS_SLAVE)
274 error = tomoyo_mount_acl(r, dev_name, dir,
275 TOMOYO_MOUNT_MAKE_SLAVE_KEYWORD,
277 else if (flags & MS_SHARED)
278 error = tomoyo_mount_acl(r, dev_name, dir,
279 TOMOYO_MOUNT_MAKE_SHARED_KEYWORD,
282 error = tomoyo_mount_acl2(r, dev_name, dir, type, flags);
283 if (r->mode != TOMOYO_CONFIG_ENFORCING)
289 * tomoyo_mount_permission - Check permission for mount() operation.
291 * @dev_name: Name of device file.
292 * @path: Pointer to "struct path".
293 * @type: Name of filesystem type. May be NULL.
294 * @flags: Mount options.
295 * @data_page: Optional data. May be NULL.
297 * Returns 0 on success, negative value otherwise.
299 int tomoyo_mount_permission(char *dev_name, struct path *path, char *type,
300 unsigned long flags, void *data_page)
302 struct tomoyo_request_info r;
306 if (tomoyo_init_request_info(&r, NULL) == TOMOYO_CONFIG_DISABLED)
310 idx = tomoyo_read_lock();
311 error = tomoyo_mount_acl(&r, dev_name, path, type, flags);
312 tomoyo_read_unlock(idx);
317 * tomoyo_write_mount_policy - Write "struct tomoyo_mount_acl" list.
319 * @data: String to parse.
320 * @domain: Pointer to "struct tomoyo_domain_info".
321 * @is_delete: True if it is a delete request.
323 * Returns 0 on success, negative value otherwise.
325 int tomoyo_write_mount_policy(char *data, struct tomoyo_domain_info *domain,
326 const bool is_delete)
328 struct tomoyo_acl_info *ptr;
329 struct tomoyo_mount_acl e = { .head.type = TOMOYO_TYPE_MOUNT_ACL };
330 int error = is_delete ? -ENOENT : -ENOMEM;
332 if (!tomoyo_tokenize(data, w, sizeof(w)) || !w[3][0])
334 if (!tomoyo_parse_name_union(w[0], &e.dev_name) ||
335 !tomoyo_parse_name_union(w[1], &e.dir_name) ||
336 !tomoyo_parse_name_union(w[2], &e.fs_type) ||
337 !tomoyo_parse_number_union(w[3], &e.flags))
339 if (mutex_lock_interruptible(&tomoyo_policy_lock))
341 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
342 struct tomoyo_mount_acl *acl =
343 container_of(ptr, struct tomoyo_mount_acl, head);
344 if (!tomoyo_is_same_mount_acl(acl, &e))
346 acl->is_deleted = is_delete;
350 if (!is_delete && error) {
351 struct tomoyo_mount_acl *entry =
352 tomoyo_commit_ok(&e, sizeof(e));
354 list_add_tail_rcu(&entry->head.list,
355 &domain->acl_info_list);
359 mutex_unlock(&tomoyo_policy_lock);
361 tomoyo_put_name_union(&e.dev_name);
362 tomoyo_put_name_union(&e.dir_name);
363 tomoyo_put_name_union(&e.fs_type);
364 tomoyo_put_number_union(&e.flags);