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 | { |
Tetsuo Handa | eadd99c | 2011-06-26 23:18:58 +0900 | [diff] [blame] | 30 | return tomoyo_supervisor(r, "file mount %s %s %s 0x%lX\n", |
Tetsuo Handa | 7c75964 | 2011-06-26 23:15:31 +0900 | [diff] [blame] | 31 | r->param.mount.dev->name, |
Tetsuo Handa | eadd99c | 2011-06-26 23:18:58 +0900 | [diff] [blame] | 32 | r->param.mount.dir->name, |
| 33 | r->param.mount.type->name, |
| 34 | r->param.mount.flags); |
Tetsuo Handa | 99a8525 | 2010-06-16 16:22:51 +0900 | [diff] [blame] | 35 | } |
| 36 | |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame] | 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 | */ |
Tetsuo Handa | 484ca79 | 2010-07-29 14:29:55 +0900 | [diff] [blame] | 45 | static bool tomoyo_check_mount_acl(struct tomoyo_request_info *r, |
Tetsuo Handa | 99a8525 | 2010-06-16 16:22:51 +0900 | [diff] [blame] | 46 | const struct tomoyo_acl_info *ptr) |
| 47 | { |
| 48 | const struct tomoyo_mount_acl *acl = |
| 49 | container_of(ptr, typeof(*acl), head); |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame] | 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) && |
Tetsuo Handa | 99a8525 | 2010-06-16 16:22:51 +0900 | [diff] [blame] | 56 | (!r->param.mount.need_dev || |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame] | 57 | tomoyo_compare_name_union(r->param.mount.dev, |
| 58 | &acl->dev_name)); |
Tetsuo Handa | 99a8525 | 2010-06-16 16:22:51 +0900 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | /** |
Tetsuo Handa | d795ef9e | 2010-06-16 16:24:58 +0900 | [diff] [blame] | 62 | * tomoyo_mount_acl - Check permission for mount() operation. |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 63 | * |
| 64 | * @r: Pointer to "struct tomoyo_request_info". |
| 65 | * @dev_name: Name of device file. |
| 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 | */ |
Tetsuo Handa | d795ef9e | 2010-06-16 16:24:58 +0900 | [diff] [blame] | 74 | 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] | 75 | struct path *dir, const char *type, |
| 76 | unsigned long flags) |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 77 | { |
| 78 | struct path path; |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 79 | struct file_system_type *fstype = NULL; |
| 80 | const char *requested_type = NULL; |
| 81 | const char *requested_dir_name = NULL; |
| 82 | const char *requested_dev_name = NULL; |
| 83 | struct tomoyo_path_info rtype; |
| 84 | struct tomoyo_path_info rdev; |
| 85 | struct tomoyo_path_info rdir; |
| 86 | int need_dev = 0; |
| 87 | int error = -ENOMEM; |
| 88 | |
| 89 | /* Get fstype. */ |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 90 | requested_type = tomoyo_encode(type); |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 91 | if (!requested_type) |
| 92 | goto out; |
| 93 | rtype.name = requested_type; |
| 94 | tomoyo_fill_path_info(&rtype); |
| 95 | |
| 96 | /* Get mount point. */ |
| 97 | requested_dir_name = tomoyo_realpath_from_path(dir); |
| 98 | if (!requested_dir_name) { |
| 99 | error = -ENOMEM; |
| 100 | goto out; |
| 101 | } |
| 102 | rdir.name = requested_dir_name; |
| 103 | tomoyo_fill_path_info(&rdir); |
| 104 | |
| 105 | /* Compare fs name. */ |
Tetsuo Handa | b5bc60b | 2011-06-26 23:16:03 +0900 | [diff] [blame] | 106 | if (type == tomoyo_mounts[TOMOYO_MOUNT_REMOUNT]) { |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 107 | /* dev_name is ignored. */ |
Tetsuo Handa | b5bc60b | 2011-06-26 23:16:03 +0900 | [diff] [blame] | 108 | } else if (type == tomoyo_mounts[TOMOYO_MOUNT_MAKE_UNBINDABLE] || |
| 109 | type == tomoyo_mounts[TOMOYO_MOUNT_MAKE_PRIVATE] || |
| 110 | type == tomoyo_mounts[TOMOYO_MOUNT_MAKE_SLAVE] || |
| 111 | type == tomoyo_mounts[TOMOYO_MOUNT_MAKE_SHARED]) { |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 112 | /* dev_name is ignored. */ |
Tetsuo Handa | b5bc60b | 2011-06-26 23:16:03 +0900 | [diff] [blame] | 113 | } else if (type == tomoyo_mounts[TOMOYO_MOUNT_BIND] || |
| 114 | type == tomoyo_mounts[TOMOYO_MOUNT_MOVE]) { |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 115 | need_dev = -1; /* dev_name is a directory */ |
| 116 | } else { |
| 117 | fstype = get_fs_type(type); |
| 118 | if (!fstype) { |
| 119 | error = -ENODEV; |
| 120 | goto out; |
| 121 | } |
| 122 | if (fstype->fs_flags & FS_REQUIRES_DEV) |
| 123 | /* dev_name is a block device file. */ |
| 124 | need_dev = 1; |
| 125 | } |
| 126 | if (need_dev) { |
| 127 | /* Get mount point or device file. */ |
Tetsuo Handa | 4e78c72 | 2011-06-13 13:49:11 +0900 | [diff] [blame] | 128 | if (!dev_name || kern_path(dev_name, LOOKUP_FOLLOW, &path)) { |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 129 | error = -ENOENT; |
| 130 | goto out; |
| 131 | } |
| 132 | requested_dev_name = tomoyo_realpath_from_path(&path); |
Tetsuo Handa | db5ca35 | 2011-04-20 06:49:15 +0900 | [diff] [blame] | 133 | path_put(&path); |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 134 | if (!requested_dev_name) { |
| 135 | error = -ENOENT; |
| 136 | goto out; |
| 137 | } |
| 138 | } else { |
| 139 | /* Map dev_name to "<NULL>" if no dev_name given. */ |
| 140 | if (!dev_name) |
| 141 | dev_name = "<NULL>"; |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 142 | requested_dev_name = tomoyo_encode(dev_name); |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 143 | if (!requested_dev_name) { |
| 144 | error = -ENOMEM; |
| 145 | goto out; |
| 146 | } |
| 147 | } |
| 148 | rdev.name = requested_dev_name; |
| 149 | tomoyo_fill_path_info(&rdev); |
Tetsuo Handa | cf6e9a6 | 2010-06-16 16:21:36 +0900 | [diff] [blame] | 150 | r->param_type = TOMOYO_TYPE_MOUNT_ACL; |
| 151 | r->param.mount.need_dev = need_dev; |
| 152 | r->param.mount.dev = &rdev; |
| 153 | r->param.mount.dir = &rdir; |
| 154 | r->param.mount.type = &rtype; |
| 155 | r->param.mount.flags = flags; |
Tetsuo Handa | 99a8525 | 2010-06-16 16:22:51 +0900 | [diff] [blame] | 156 | do { |
| 157 | tomoyo_check_acl(r, tomoyo_check_mount_acl); |
| 158 | error = tomoyo_audit_mount_log(r); |
| 159 | } while (error == TOMOYO_RETRY_REQUEST); |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 160 | out: |
| 161 | kfree(requested_dev_name); |
| 162 | kfree(requested_dir_name); |
| 163 | if (fstype) |
| 164 | put_filesystem(fstype); |
| 165 | kfree(requested_type); |
| 166 | return error; |
| 167 | } |
| 168 | |
| 169 | /** |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 170 | * tomoyo_mount_permission - Check permission for mount() operation. |
| 171 | * |
| 172 | * @dev_name: Name of device file. |
| 173 | * @path: Pointer to "struct path". |
| 174 | * @type: Name of filesystem type. May be NULL. |
| 175 | * @flags: Mount options. |
| 176 | * @data_page: Optional data. May be NULL. |
| 177 | * |
| 178 | * Returns 0 on success, negative value otherwise. |
| 179 | */ |
Tetsuo Handa | b5bc60b | 2011-06-26 23:16:03 +0900 | [diff] [blame] | 180 | int tomoyo_mount_permission(char *dev_name, struct path *path, |
| 181 | const char *type, unsigned long flags, |
| 182 | void *data_page) |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 183 | { |
| 184 | struct tomoyo_request_info r; |
| 185 | int error; |
| 186 | int idx; |
| 187 | |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 188 | if (tomoyo_init_request_info(&r, NULL, TOMOYO_MAC_FILE_MOUNT) |
| 189 | == TOMOYO_CONFIG_DISABLED) |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 190 | return 0; |
Tetsuo Handa | d795ef9e | 2010-06-16 16:24:58 +0900 | [diff] [blame] | 191 | if ((flags & MS_MGC_MSK) == MS_MGC_VAL) |
| 192 | flags &= ~MS_MGC_MSK; |
| 193 | if (flags & MS_REMOUNT) { |
Tetsuo Handa | b5bc60b | 2011-06-26 23:16:03 +0900 | [diff] [blame] | 194 | type = tomoyo_mounts[TOMOYO_MOUNT_REMOUNT]; |
Tetsuo Handa | d795ef9e | 2010-06-16 16:24:58 +0900 | [diff] [blame] | 195 | flags &= ~MS_REMOUNT; |
| 196 | } |
| 197 | if (flags & MS_MOVE) { |
Tetsuo Handa | b5bc60b | 2011-06-26 23:16:03 +0900 | [diff] [blame] | 198 | type = tomoyo_mounts[TOMOYO_MOUNT_MOVE]; |
Tetsuo Handa | d795ef9e | 2010-06-16 16:24:58 +0900 | [diff] [blame] | 199 | flags &= ~MS_MOVE; |
| 200 | } |
| 201 | if (flags & MS_BIND) { |
Tetsuo Handa | b5bc60b | 2011-06-26 23:16:03 +0900 | [diff] [blame] | 202 | type = tomoyo_mounts[TOMOYO_MOUNT_BIND]; |
Tetsuo Handa | d795ef9e | 2010-06-16 16:24:58 +0900 | [diff] [blame] | 203 | flags &= ~MS_BIND; |
| 204 | } |
| 205 | if (flags & MS_UNBINDABLE) { |
Tetsuo Handa | b5bc60b | 2011-06-26 23:16:03 +0900 | [diff] [blame] | 206 | type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_UNBINDABLE]; |
Tetsuo Handa | d795ef9e | 2010-06-16 16:24:58 +0900 | [diff] [blame] | 207 | flags &= ~MS_UNBINDABLE; |
| 208 | } |
| 209 | if (flags & MS_PRIVATE) { |
Tetsuo Handa | b5bc60b | 2011-06-26 23:16:03 +0900 | [diff] [blame] | 210 | type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_PRIVATE]; |
Tetsuo Handa | d795ef9e | 2010-06-16 16:24:58 +0900 | [diff] [blame] | 211 | flags &= ~MS_PRIVATE; |
| 212 | } |
| 213 | if (flags & MS_SLAVE) { |
Tetsuo Handa | b5bc60b | 2011-06-26 23:16:03 +0900 | [diff] [blame] | 214 | type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_SLAVE]; |
Tetsuo Handa | d795ef9e | 2010-06-16 16:24:58 +0900 | [diff] [blame] | 215 | flags &= ~MS_SLAVE; |
| 216 | } |
| 217 | if (flags & MS_SHARED) { |
Tetsuo Handa | b5bc60b | 2011-06-26 23:16:03 +0900 | [diff] [blame] | 218 | type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_SHARED]; |
Tetsuo Handa | d795ef9e | 2010-06-16 16:24:58 +0900 | [diff] [blame] | 219 | flags &= ~MS_SHARED; |
| 220 | } |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 221 | if (!type) |
| 222 | type = "<NULL>"; |
| 223 | idx = tomoyo_read_lock(); |
| 224 | error = tomoyo_mount_acl(&r, dev_name, path, type, flags); |
| 225 | tomoyo_read_unlock(idx); |
| 226 | return error; |
| 227 | } |