Merge git://git.kernel.org/pub/scm/linux/kernel/git/cmetcalf/linux-tile
[platform/adaptation/renesas_rcar/renesas_kernel.git] / security / tomoyo / mount.c
1 /*
2  * security/tomoyo/mount.c
3  *
4  * Copyright (C) 2005-2011  NTT DATA CORPORATION
5  */
6
7 #include <linux/slab.h>
8 #include "common.h"
9
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",
19 };
20
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  */
28 static int tomoyo_audit_mount_log(struct tomoyo_request_info *r)
29 {
30         return tomoyo_supervisor(r, "file mount %s %s %s 0x%lX\n",
31                                  r->param.mount.dev->name,
32                                  r->param.mount.dir->name,
33                                  r->param.mount.type->name,
34                                  r->param.mount.flags);
35 }
36
37 /**
38  * tomoyo_check_mount_acl - Check permission for path path path number operation.
39  *
40  * @r:   Pointer to "struct tomoyo_request_info".
41  * @ptr: Pointer to "struct tomoyo_acl_info".
42  *
43  * Returns true if granted, false otherwise.
44  */
45 static bool tomoyo_check_mount_acl(struct tomoyo_request_info *r,
46                                    const struct tomoyo_acl_info *ptr)
47 {
48         const struct tomoyo_mount_acl *acl =
49                 container_of(ptr, typeof(*acl), head);
50         return tomoyo_compare_number_union(r->param.mount.flags,
51                                            &acl->flags) &&
52                 tomoyo_compare_name_union(r->param.mount.type,
53                                           &acl->fs_type) &&
54                 tomoyo_compare_name_union(r->param.mount.dir,
55                                           &acl->dir_name) &&
56                 (!r->param.mount.need_dev ||
57                  tomoyo_compare_name_union(r->param.mount.dev,
58                                            &acl->dev_name));
59 }
60
61 /**
62  * tomoyo_mount_acl - Check permission for mount() operation.
63  *
64  * @r:        Pointer to "struct tomoyo_request_info".
65  * @dev_name: Name of device file. Maybe NULL.
66  * @dir:      Pointer to "struct path".
67  * @type:     Name of filesystem type.
68  * @flags:    Mount options.
69  *
70  * Returns 0 on success, negative value otherwise.
71  *
72  * Caller holds tomoyo_read_lock().
73  */
74 static int tomoyo_mount_acl(struct tomoyo_request_info *r, char *dev_name,
75                             struct path *dir, const char *type,
76                             unsigned long flags)
77 {
78         struct tomoyo_obj_info obj = { };
79         struct path path;
80         struct file_system_type *fstype = NULL;
81         const char *requested_type = NULL;
82         const char *requested_dir_name = NULL;
83         const char *requested_dev_name = NULL;
84         struct tomoyo_path_info rtype;
85         struct tomoyo_path_info rdev;
86         struct tomoyo_path_info rdir;
87         int need_dev = 0;
88         int error = -ENOMEM;
89         r->obj = &obj;
90
91         /* Get fstype. */
92         requested_type = tomoyo_encode(type);
93         if (!requested_type)
94                 goto out;
95         rtype.name = requested_type;
96         tomoyo_fill_path_info(&rtype);
97
98         /* Get mount point. */
99         obj.path2 = *dir;
100         requested_dir_name = tomoyo_realpath_from_path(dir);
101         if (!requested_dir_name) {
102                 error = -ENOMEM;
103                 goto out;
104         }
105         rdir.name = requested_dir_name;
106         tomoyo_fill_path_info(&rdir);
107
108         /* Compare fs name. */
109         if (type == tomoyo_mounts[TOMOYO_MOUNT_REMOUNT]) {
110                 /* dev_name is ignored. */
111         } else if (type == tomoyo_mounts[TOMOYO_MOUNT_MAKE_UNBINDABLE] ||
112                    type == tomoyo_mounts[TOMOYO_MOUNT_MAKE_PRIVATE] ||
113                    type == tomoyo_mounts[TOMOYO_MOUNT_MAKE_SLAVE] ||
114                    type == tomoyo_mounts[TOMOYO_MOUNT_MAKE_SHARED]) {
115                 /* dev_name is ignored. */
116         } else if (type == tomoyo_mounts[TOMOYO_MOUNT_BIND] ||
117                    type == tomoyo_mounts[TOMOYO_MOUNT_MOVE]) {
118                 need_dev = -1; /* dev_name is a directory */
119         } else {
120                 fstype = get_fs_type(type);
121                 if (!fstype) {
122                         error = -ENODEV;
123                         goto out;
124                 }
125                 if (fstype->fs_flags & FS_REQUIRES_DEV)
126                         /* dev_name is a block device file. */
127                         need_dev = 1;
128         }
129         if (need_dev) {
130                 /* Get mount point or device file. */
131                 if (!dev_name || kern_path(dev_name, LOOKUP_FOLLOW, &path)) {
132                         error = -ENOENT;
133                         goto out;
134                 }
135                 obj.path1 = path;
136                 requested_dev_name = tomoyo_realpath_from_path(&path);
137                 if (!requested_dev_name) {
138                         error = -ENOENT;
139                         goto out;
140                 }
141         } else {
142                 /* Map dev_name to "<NULL>" if no dev_name given. */
143                 if (!dev_name)
144                         dev_name = "<NULL>";
145                 requested_dev_name = tomoyo_encode(dev_name);
146                 if (!requested_dev_name) {
147                         error = -ENOMEM;
148                         goto out;
149                 }
150         }
151         rdev.name = requested_dev_name;
152         tomoyo_fill_path_info(&rdev);
153         r->param_type = TOMOYO_TYPE_MOUNT_ACL;
154         r->param.mount.need_dev = need_dev;
155         r->param.mount.dev = &rdev;
156         r->param.mount.dir = &rdir;
157         r->param.mount.type = &rtype;
158         r->param.mount.flags = flags;
159         do {
160                 tomoyo_check_acl(r, tomoyo_check_mount_acl);
161                 error = tomoyo_audit_mount_log(r);
162         } while (error == TOMOYO_RETRY_REQUEST);
163  out:
164         kfree(requested_dev_name);
165         kfree(requested_dir_name);
166         if (fstype)
167                 put_filesystem(fstype);
168         kfree(requested_type);
169         /* Drop refcount obtained by kern_path(). */
170         if (obj.path1.dentry)
171                 path_put(&obj.path1);
172         return error;
173 }
174
175 /**
176  * tomoyo_mount_permission - Check permission for mount() operation.
177  *
178  * @dev_name:  Name of device file. Maybe NULL.
179  * @path:      Pointer to "struct path".
180  * @type:      Name of filesystem type. Maybe NULL.
181  * @flags:     Mount options.
182  * @data_page: Optional data. Maybe NULL.
183  *
184  * Returns 0 on success, negative value otherwise.
185  */
186 int tomoyo_mount_permission(char *dev_name, struct path *path,
187                             const char *type, unsigned long flags,
188                             void *data_page)
189 {
190         struct tomoyo_request_info r;
191         int error;
192         int idx;
193
194         if (tomoyo_init_request_info(&r, NULL, TOMOYO_MAC_FILE_MOUNT)
195             == TOMOYO_CONFIG_DISABLED)
196                 return 0;
197         if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
198                 flags &= ~MS_MGC_MSK;
199         if (flags & MS_REMOUNT) {
200                 type = tomoyo_mounts[TOMOYO_MOUNT_REMOUNT];
201                 flags &= ~MS_REMOUNT;
202         } else if (flags & MS_BIND) {
203                 type = tomoyo_mounts[TOMOYO_MOUNT_BIND];
204                 flags &= ~MS_BIND;
205         } else if (flags & MS_SHARED) {
206                 if (flags & (MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
207                         return -EINVAL;
208                 type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_SHARED];
209                 flags &= ~MS_SHARED;
210         } else if (flags & MS_PRIVATE) {
211                 if (flags & (MS_SHARED | MS_SLAVE | MS_UNBINDABLE))
212                         return -EINVAL;
213                 type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_PRIVATE];
214                 flags &= ~MS_PRIVATE;
215         } else if (flags & MS_SLAVE) {
216                 if (flags & (MS_SHARED | MS_PRIVATE | MS_UNBINDABLE))
217                         return -EINVAL;
218                 type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_SLAVE];
219                 flags &= ~MS_SLAVE;
220         } else if (flags & MS_UNBINDABLE) {
221                 if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE))
222                         return -EINVAL;
223                 type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_UNBINDABLE];
224                 flags &= ~MS_UNBINDABLE;
225         } else if (flags & MS_MOVE) {
226                 type = tomoyo_mounts[TOMOYO_MOUNT_MOVE];
227                 flags &= ~MS_MOVE;
228         }
229         if (!type)
230                 type = "<NULL>";
231         idx = tomoyo_read_lock();
232         error = tomoyo_mount_acl(&r, dev_name, path, type, flags);
233         tomoyo_read_unlock(idx);
234         return error;
235 }