Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 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 | |
Tetsuo Handa | b5bc60b | 2011-06-26 23:16:03 +0900 | [diff] [blame^] | 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 | }; |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 20 | |
| 21 | /** |
Tetsuo Handa | 99a8525 | 2010-06-16 16:22:51 +0900 | [diff] [blame] | 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; |
Tetsuo Handa | b5bc60b | 2011-06-26 23:16:03 +0900 | [diff] [blame^] | 36 | if (type == tomoyo_mounts[TOMOYO_MOUNT_REMOUNT]) |
Tetsuo Handa | 99a8525 | 2010-06-16 16:22:51 +0900 | [diff] [blame] | 37 | tomoyo_warn_log(r, "mount -o remount %s 0x%lX", dir, flags); |
Tetsuo Handa | b5bc60b | 2011-06-26 23:16:03 +0900 | [diff] [blame^] | 38 | else if (type == tomoyo_mounts[TOMOYO_MOUNT_BIND] |
| 39 | || type == tomoyo_mounts[TOMOYO_MOUNT_MOVE]) |
Tetsuo Handa | 99a8525 | 2010-06-16 16:22:51 +0900 | [diff] [blame] | 40 | tomoyo_warn_log(r, "mount %s %s %s 0x%lX", type, dev, dir, |
| 41 | flags); |
Tetsuo Handa | b5bc60b | 2011-06-26 23:16:03 +0900 | [diff] [blame^] | 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]) |
Tetsuo Handa | 99a8525 | 2010-06-16 16:22:51 +0900 | [diff] [blame] | 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); |
Tetsuo Handa | b5bc60b | 2011-06-26 23:16:03 +0900 | [diff] [blame^] | 50 | return tomoyo_supervisor(r, "allow_mount %s %s %s 0x%lX\n", |
Tetsuo Handa | 7c75964 | 2011-06-26 23:15:31 +0900 | [diff] [blame] | 51 | r->param.mount.dev->name, |
| 52 | r->param.mount.dir->name, type, flags); |
Tetsuo Handa | 99a8525 | 2010-06-16 16:22:51 +0900 | [diff] [blame] | 53 | } |
| 54 | |
Tetsuo Handa | 484ca79 | 2010-07-29 14:29:55 +0900 | [diff] [blame] | 55 | static bool tomoyo_check_mount_acl(struct tomoyo_request_info *r, |
Tetsuo Handa | 99a8525 | 2010-06-16 16:22:51 +0900 | [diff] [blame] | 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 | |
| 67 | /** |
Tetsuo Handa | d795ef9e | 2010-06-16 16:24:58 +0900 | [diff] [blame] | 68 | * tomoyo_mount_acl - Check permission for mount() operation. |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 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 | */ |
Tetsuo Handa | d795ef9e | 2010-06-16 16:24:58 +0900 | [diff] [blame] | 80 | static int tomoyo_mount_acl(struct tomoyo_request_info *r, char *dev_name, |
Tetsuo Handa | b5bc60b | 2011-06-26 23:16:03 +0900 | [diff] [blame^] | 81 | struct path *dir, const char *type, |
| 82 | unsigned long flags) |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 83 | { |
| 84 | struct path path; |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 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. */ |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 96 | requested_type = tomoyo_encode(type); |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 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. */ |
Tetsuo Handa | b5bc60b | 2011-06-26 23:16:03 +0900 | [diff] [blame^] | 112 | if (type == tomoyo_mounts[TOMOYO_MOUNT_REMOUNT]) { |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 113 | /* dev_name is ignored. */ |
Tetsuo Handa | b5bc60b | 2011-06-26 23:16:03 +0900 | [diff] [blame^] | 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]) { |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 118 | /* dev_name is ignored. */ |
Tetsuo Handa | b5bc60b | 2011-06-26 23:16:03 +0900 | [diff] [blame^] | 119 | } else if (type == tomoyo_mounts[TOMOYO_MOUNT_BIND] || |
| 120 | type == tomoyo_mounts[TOMOYO_MOUNT_MOVE]) { |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 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); |
Tetsuo Handa | db5ca35 | 2011-04-20 06:49:15 +0900 | [diff] [blame] | 139 | path_put(&path); |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 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>"; |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 148 | requested_dev_name = tomoyo_encode(dev_name); |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 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); |
Tetsuo Handa | cf6e9a6 | 2010-06-16 16:21:36 +0900 | [diff] [blame] | 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; |
Tetsuo Handa | 99a8525 | 2010-06-16 16:22:51 +0900 | [diff] [blame] | 162 | do { |
| 163 | tomoyo_check_acl(r, tomoyo_check_mount_acl); |
| 164 | error = tomoyo_audit_mount_log(r); |
| 165 | } while (error == TOMOYO_RETRY_REQUEST); |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 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 | |
| 175 | /** |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 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 | */ |
Tetsuo Handa | b5bc60b | 2011-06-26 23:16:03 +0900 | [diff] [blame^] | 186 | int tomoyo_mount_permission(char *dev_name, struct path *path, |
| 187 | const char *type, unsigned long flags, |
| 188 | void *data_page) |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 189 | { |
| 190 | struct tomoyo_request_info r; |
| 191 | int error; |
| 192 | int idx; |
| 193 | |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 194 | if (tomoyo_init_request_info(&r, NULL, TOMOYO_MAC_FILE_MOUNT) |
| 195 | == TOMOYO_CONFIG_DISABLED) |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 196 | return 0; |
Tetsuo Handa | d795ef9e | 2010-06-16 16:24:58 +0900 | [diff] [blame] | 197 | if ((flags & MS_MGC_MSK) == MS_MGC_VAL) |
| 198 | flags &= ~MS_MGC_MSK; |
| 199 | if (flags & MS_REMOUNT) { |
Tetsuo Handa | b5bc60b | 2011-06-26 23:16:03 +0900 | [diff] [blame^] | 200 | type = tomoyo_mounts[TOMOYO_MOUNT_REMOUNT]; |
Tetsuo Handa | d795ef9e | 2010-06-16 16:24:58 +0900 | [diff] [blame] | 201 | flags &= ~MS_REMOUNT; |
| 202 | } |
| 203 | if (flags & MS_MOVE) { |
Tetsuo Handa | b5bc60b | 2011-06-26 23:16:03 +0900 | [diff] [blame^] | 204 | type = tomoyo_mounts[TOMOYO_MOUNT_MOVE]; |
Tetsuo Handa | d795ef9e | 2010-06-16 16:24:58 +0900 | [diff] [blame] | 205 | flags &= ~MS_MOVE; |
| 206 | } |
| 207 | if (flags & MS_BIND) { |
Tetsuo Handa | b5bc60b | 2011-06-26 23:16:03 +0900 | [diff] [blame^] | 208 | type = tomoyo_mounts[TOMOYO_MOUNT_BIND]; |
Tetsuo Handa | d795ef9e | 2010-06-16 16:24:58 +0900 | [diff] [blame] | 209 | flags &= ~MS_BIND; |
| 210 | } |
| 211 | if (flags & MS_UNBINDABLE) { |
Tetsuo Handa | b5bc60b | 2011-06-26 23:16:03 +0900 | [diff] [blame^] | 212 | type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_UNBINDABLE]; |
Tetsuo Handa | d795ef9e | 2010-06-16 16:24:58 +0900 | [diff] [blame] | 213 | flags &= ~MS_UNBINDABLE; |
| 214 | } |
| 215 | if (flags & MS_PRIVATE) { |
Tetsuo Handa | b5bc60b | 2011-06-26 23:16:03 +0900 | [diff] [blame^] | 216 | type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_PRIVATE]; |
Tetsuo Handa | d795ef9e | 2010-06-16 16:24:58 +0900 | [diff] [blame] | 217 | flags &= ~MS_PRIVATE; |
| 218 | } |
| 219 | if (flags & MS_SLAVE) { |
Tetsuo Handa | b5bc60b | 2011-06-26 23:16:03 +0900 | [diff] [blame^] | 220 | type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_SLAVE]; |
Tetsuo Handa | d795ef9e | 2010-06-16 16:24:58 +0900 | [diff] [blame] | 221 | flags &= ~MS_SLAVE; |
| 222 | } |
| 223 | if (flags & MS_SHARED) { |
Tetsuo Handa | b5bc60b | 2011-06-26 23:16:03 +0900 | [diff] [blame^] | 224 | type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_SHARED]; |
Tetsuo Handa | d795ef9e | 2010-06-16 16:24:58 +0900 | [diff] [blame] | 225 | flags &= ~MS_SHARED; |
| 226 | } |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 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 | |
Tetsuo Handa | 237ab45 | 2010-06-12 20:46:22 +0900 | [diff] [blame] | 235 | static 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); |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 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); |
Tetsuo Handa | 237ab45 | 2010-06-12 20:46:22 +0900 | [diff] [blame] | 245 | } |
| 246 | |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 247 | /** |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 248 | * tomoyo_write_mount - Write "struct tomoyo_mount_acl" list. |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 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. |
Tetsuo Handa | 237ab45 | 2010-06-12 20:46:22 +0900 | [diff] [blame] | 255 | * |
| 256 | * Caller holds tomoyo_read_lock(). |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 257 | */ |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 258 | int tomoyo_write_mount(char *data, struct tomoyo_domain_info *domain, |
| 259 | const bool is_delete) |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 260 | { |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 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; |
Tetsuo Handa | 237ab45 | 2010-06-12 20:46:22 +0900 | [diff] [blame] | 271 | error = tomoyo_update_domain(&e.head, sizeof(e), is_delete, domain, |
| 272 | tomoyo_same_mount_acl, NULL); |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 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 | } |