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 | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame^] | 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 | */ |
Tetsuo Handa | 484ca79 | 2010-07-29 14:29:55 +0900 | [diff] [blame] | 63 | static bool tomoyo_check_mount_acl(struct tomoyo_request_info *r, |
Tetsuo Handa | 99a8525 | 2010-06-16 16:22:51 +0900 | [diff] [blame] | 64 | const struct tomoyo_acl_info *ptr) |
| 65 | { |
| 66 | const struct tomoyo_mount_acl *acl = |
| 67 | container_of(ptr, typeof(*acl), head); |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame^] | 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) && |
Tetsuo Handa | 99a8525 | 2010-06-16 16:22:51 +0900 | [diff] [blame] | 74 | (!r->param.mount.need_dev || |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame^] | 75 | tomoyo_compare_name_union(r->param.mount.dev, |
| 76 | &acl->dev_name)); |
Tetsuo Handa | 99a8525 | 2010-06-16 16:22:51 +0900 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | /** |
Tetsuo Handa | d795ef9e | 2010-06-16 16:24:58 +0900 | [diff] [blame] | 80 | * tomoyo_mount_acl - Check permission for mount() operation. |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 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 | */ |
Tetsuo Handa | d795ef9e | 2010-06-16 16:24:58 +0900 | [diff] [blame] | 92 | 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] | 93 | struct path *dir, const char *type, |
| 94 | unsigned long flags) |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 95 | { |
| 96 | struct path path; |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 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. */ |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 108 | requested_type = tomoyo_encode(type); |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 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. */ |
Tetsuo Handa | b5bc60b | 2011-06-26 23:16:03 +0900 | [diff] [blame] | 124 | if (type == tomoyo_mounts[TOMOYO_MOUNT_REMOUNT]) { |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 125 | /* dev_name is ignored. */ |
Tetsuo Handa | b5bc60b | 2011-06-26 23:16:03 +0900 | [diff] [blame] | 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]) { |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 130 | /* dev_name is ignored. */ |
Tetsuo Handa | b5bc60b | 2011-06-26 23:16:03 +0900 | [diff] [blame] | 131 | } else if (type == tomoyo_mounts[TOMOYO_MOUNT_BIND] || |
| 132 | type == tomoyo_mounts[TOMOYO_MOUNT_MOVE]) { |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 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); |
Tetsuo Handa | db5ca35 | 2011-04-20 06:49:15 +0900 | [diff] [blame] | 151 | path_put(&path); |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 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>"; |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 160 | requested_dev_name = tomoyo_encode(dev_name); |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 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); |
Tetsuo Handa | cf6e9a6 | 2010-06-16 16:21:36 +0900 | [diff] [blame] | 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; |
Tetsuo Handa | 99a8525 | 2010-06-16 16:22:51 +0900 | [diff] [blame] | 174 | do { |
| 175 | tomoyo_check_acl(r, tomoyo_check_mount_acl); |
| 176 | error = tomoyo_audit_mount_log(r); |
| 177 | } while (error == TOMOYO_RETRY_REQUEST); |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 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 | /** |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 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 | */ |
Tetsuo Handa | b5bc60b | 2011-06-26 23:16:03 +0900 | [diff] [blame] | 198 | int tomoyo_mount_permission(char *dev_name, struct path *path, |
| 199 | const char *type, unsigned long flags, |
| 200 | void *data_page) |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 201 | { |
| 202 | struct tomoyo_request_info r; |
| 203 | int error; |
| 204 | int idx; |
| 205 | |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 206 | if (tomoyo_init_request_info(&r, NULL, TOMOYO_MAC_FILE_MOUNT) |
| 207 | == TOMOYO_CONFIG_DISABLED) |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 208 | return 0; |
Tetsuo Handa | d795ef9e | 2010-06-16 16:24:58 +0900 | [diff] [blame] | 209 | if ((flags & MS_MGC_MSK) == MS_MGC_VAL) |
| 210 | flags &= ~MS_MGC_MSK; |
| 211 | if (flags & MS_REMOUNT) { |
Tetsuo Handa | b5bc60b | 2011-06-26 23:16:03 +0900 | [diff] [blame] | 212 | type = tomoyo_mounts[TOMOYO_MOUNT_REMOUNT]; |
Tetsuo Handa | d795ef9e | 2010-06-16 16:24:58 +0900 | [diff] [blame] | 213 | flags &= ~MS_REMOUNT; |
| 214 | } |
| 215 | if (flags & MS_MOVE) { |
Tetsuo Handa | b5bc60b | 2011-06-26 23:16:03 +0900 | [diff] [blame] | 216 | type = tomoyo_mounts[TOMOYO_MOUNT_MOVE]; |
Tetsuo Handa | d795ef9e | 2010-06-16 16:24:58 +0900 | [diff] [blame] | 217 | flags &= ~MS_MOVE; |
| 218 | } |
| 219 | if (flags & MS_BIND) { |
Tetsuo Handa | b5bc60b | 2011-06-26 23:16:03 +0900 | [diff] [blame] | 220 | type = tomoyo_mounts[TOMOYO_MOUNT_BIND]; |
Tetsuo Handa | d795ef9e | 2010-06-16 16:24:58 +0900 | [diff] [blame] | 221 | flags &= ~MS_BIND; |
| 222 | } |
| 223 | if (flags & MS_UNBINDABLE) { |
Tetsuo Handa | b5bc60b | 2011-06-26 23:16:03 +0900 | [diff] [blame] | 224 | type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_UNBINDABLE]; |
Tetsuo Handa | d795ef9e | 2010-06-16 16:24:58 +0900 | [diff] [blame] | 225 | flags &= ~MS_UNBINDABLE; |
| 226 | } |
| 227 | if (flags & MS_PRIVATE) { |
Tetsuo Handa | b5bc60b | 2011-06-26 23:16:03 +0900 | [diff] [blame] | 228 | type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_PRIVATE]; |
Tetsuo Handa | d795ef9e | 2010-06-16 16:24:58 +0900 | [diff] [blame] | 229 | flags &= ~MS_PRIVATE; |
| 230 | } |
| 231 | if (flags & MS_SLAVE) { |
Tetsuo Handa | b5bc60b | 2011-06-26 23:16:03 +0900 | [diff] [blame] | 232 | type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_SLAVE]; |
Tetsuo Handa | d795ef9e | 2010-06-16 16:24:58 +0900 | [diff] [blame] | 233 | flags &= ~MS_SLAVE; |
| 234 | } |
| 235 | if (flags & MS_SHARED) { |
Tetsuo Handa | b5bc60b | 2011-06-26 23:16:03 +0900 | [diff] [blame] | 236 | type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_SHARED]; |
Tetsuo Handa | d795ef9e | 2010-06-16 16:24:58 +0900 | [diff] [blame] | 237 | flags &= ~MS_SHARED; |
| 238 | } |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 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 | } |
| 246 | |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame^] | 247 | /** |
| 248 | * tomoyo_same_mount_acl - Check for duplicated "struct tomoyo_mount_acl" entry. |
| 249 | * |
| 250 | * @a: Pointer to "struct tomoyo_acl_info". |
| 251 | * @b: Pointer to "struct tomoyo_acl_info". |
| 252 | * |
| 253 | * Returns true if @a == @b, false otherwise. |
| 254 | */ |
Tetsuo Handa | 237ab45 | 2010-06-12 20:46:22 +0900 | [diff] [blame] | 255 | static bool tomoyo_same_mount_acl(const struct tomoyo_acl_info *a, |
| 256 | const struct tomoyo_acl_info *b) |
| 257 | { |
| 258 | const struct tomoyo_mount_acl *p1 = container_of(a, typeof(*p1), head); |
| 259 | const struct tomoyo_mount_acl *p2 = container_of(b, typeof(*p2), head); |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame^] | 260 | return tomoyo_same_name_union(&p1->dev_name, &p2->dev_name) && |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 261 | tomoyo_same_name_union(&p1->dir_name, &p2->dir_name) && |
| 262 | tomoyo_same_name_union(&p1->fs_type, &p2->fs_type) && |
| 263 | tomoyo_same_number_union(&p1->flags, &p2->flags); |
Tetsuo Handa | 237ab45 | 2010-06-12 20:46:22 +0900 | [diff] [blame] | 264 | } |
| 265 | |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 266 | /** |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 267 | * tomoyo_write_mount - Write "struct tomoyo_mount_acl" list. |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 268 | * |
| 269 | * @data: String to parse. |
| 270 | * @domain: Pointer to "struct tomoyo_domain_info". |
| 271 | * @is_delete: True if it is a delete request. |
| 272 | * |
| 273 | * Returns 0 on success, negative value otherwise. |
Tetsuo Handa | 237ab45 | 2010-06-12 20:46:22 +0900 | [diff] [blame] | 274 | * |
| 275 | * Caller holds tomoyo_read_lock(). |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 276 | */ |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 277 | int tomoyo_write_mount(char *data, struct tomoyo_domain_info *domain, |
| 278 | const bool is_delete) |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 279 | { |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 280 | struct tomoyo_mount_acl e = { .head.type = TOMOYO_TYPE_MOUNT_ACL }; |
| 281 | int error = is_delete ? -ENOENT : -ENOMEM; |
| 282 | char *w[4]; |
| 283 | if (!tomoyo_tokenize(data, w, sizeof(w)) || !w[3][0]) |
| 284 | return -EINVAL; |
| 285 | if (!tomoyo_parse_name_union(w[0], &e.dev_name) || |
| 286 | !tomoyo_parse_name_union(w[1], &e.dir_name) || |
| 287 | !tomoyo_parse_name_union(w[2], &e.fs_type) || |
| 288 | !tomoyo_parse_number_union(w[3], &e.flags)) |
| 289 | goto out; |
Tetsuo Handa | 237ab45 | 2010-06-12 20:46:22 +0900 | [diff] [blame] | 290 | error = tomoyo_update_domain(&e.head, sizeof(e), is_delete, domain, |
| 291 | tomoyo_same_mount_acl, NULL); |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 292 | out: |
| 293 | tomoyo_put_name_union(&e.dev_name); |
| 294 | tomoyo_put_name_union(&e.dir_name); |
| 295 | tomoyo_put_name_union(&e.fs_type); |
| 296 | tomoyo_put_number_union(&e.flags); |
| 297 | return error; |
| 298 | } |