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