TOMOYO: Use struct for passing ACL line.
[linux-flexiantxendom0.git] / security / tomoyo / mount.c
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
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         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;
36         if (type == tomoyo_mounts[TOMOYO_MOUNT_REMOUNT])
37                 tomoyo_warn_log(r, "mount -o remount %s 0x%lX", dir, flags);
38         else if (type == tomoyo_mounts[TOMOYO_MOUNT_BIND]
39                  || type == tomoyo_mounts[TOMOYO_MOUNT_MOVE])
40                 tomoyo_warn_log(r, "mount %s %s %s 0x%lX", type, dev, dir,
41                                 flags);
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])
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);
50         return tomoyo_supervisor(r, "allow_mount %s %s %s 0x%lX\n",
51                                  r->param.mount.dev->name,
52                                  r->param.mount.dir->name, type, flags);
53 }
54
55 /**
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  */
63 static bool tomoyo_check_mount_acl(struct tomoyo_request_info *r,
64                                    const struct tomoyo_acl_info *ptr)
65 {
66         const struct tomoyo_mount_acl *acl =
67                 container_of(ptr, typeof(*acl), head);
68         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) &&
74                 (!r->param.mount.need_dev ||
75                  tomoyo_compare_name_union(r->param.mount.dev,
76                                            &acl->dev_name));
77 }
78
79 /**
80  * tomoyo_mount_acl - Check permission for mount() operation.
81  *
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  */
92 static int tomoyo_mount_acl(struct tomoyo_request_info *r, char *dev_name,
93                             struct path *dir, const char *type,
94                             unsigned long flags)
95 {
96         struct path path;
97         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. */
108         requested_type = tomoyo_encode(type);
109         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. */
124         if (type == tomoyo_mounts[TOMOYO_MOUNT_REMOUNT]) {
125                 /* dev_name is ignored. */
126         } 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]) {
130                 /* dev_name is ignored. */
131         } else if (type == tomoyo_mounts[TOMOYO_MOUNT_BIND] ||
132                    type == tomoyo_mounts[TOMOYO_MOUNT_MOVE]) {
133                 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);
151                 path_put(&path);
152                 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>";
160                 requested_dev_name = tomoyo_encode(dev_name);
161                 if (!requested_dev_name) {
162                         error = -ENOMEM;
163                         goto out;
164                 }
165         }
166         rdev.name = requested_dev_name;
167         tomoyo_fill_path_info(&rdev);
168         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;
174         do {
175                 tomoyo_check_acl(r, tomoyo_check_mount_acl);
176                 error = tomoyo_audit_mount_log(r);
177         } while (error == TOMOYO_RETRY_REQUEST);
178  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 /**
188  * 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  */
198 int tomoyo_mount_permission(char *dev_name, struct path *path,
199                             const char *type, unsigned long flags,
200                             void *data_page)
201 {
202         struct tomoyo_request_info r;
203         int error;
204         int idx;
205
206         if (tomoyo_init_request_info(&r, NULL, TOMOYO_MAC_FILE_MOUNT)
207             == TOMOYO_CONFIG_DISABLED)
208                 return 0;
209         if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
210                 flags &= ~MS_MGC_MSK;
211         if (flags & MS_REMOUNT) {
212                 type = tomoyo_mounts[TOMOYO_MOUNT_REMOUNT];
213                 flags &= ~MS_REMOUNT;
214         }
215         if (flags & MS_MOVE) {
216                 type = tomoyo_mounts[TOMOYO_MOUNT_MOVE];
217                 flags &= ~MS_MOVE;
218         }
219         if (flags & MS_BIND) {
220                 type = tomoyo_mounts[TOMOYO_MOUNT_BIND];
221                 flags &= ~MS_BIND;
222         }
223         if (flags & MS_UNBINDABLE) {
224                 type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_UNBINDABLE];
225                 flags &= ~MS_UNBINDABLE;
226         }
227         if (flags & MS_PRIVATE) {
228                 type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_PRIVATE];
229                 flags &= ~MS_PRIVATE;
230         }
231         if (flags & MS_SLAVE) {
232                 type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_SLAVE];
233                 flags &= ~MS_SLAVE;
234         }
235         if (flags & MS_SHARED) {
236                 type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_SHARED];
237                 flags &= ~MS_SHARED;
238         }
239         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 }