Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 2 | /* |
| 3 | * security/tomoyo/mount.c |
| 4 | * |
Tetsuo Handa | 0f2a55d | 2011-07-14 14:46:51 +0900 | [diff] [blame] | 5 | * Copyright (C) 2005-2011 NTT DATA CORPORATION |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <linux/slab.h> |
David Howells | e262e32d | 2018-11-01 23:07:23 +0000 | [diff] [blame] | 9 | #include <uapi/linux/mount.h> |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 10 | #include "common.h" |
| 11 | |
Tetsuo Handa | b5bc60b | 2011-06-26 23:16:03 +0900 | [diff] [blame] | 12 | /* String table for special mount operations. */ |
| 13 | static const char * const tomoyo_mounts[TOMOYO_MAX_SPECIAL_MOUNT] = { |
| 14 | [TOMOYO_MOUNT_BIND] = "--bind", |
| 15 | [TOMOYO_MOUNT_MOVE] = "--move", |
| 16 | [TOMOYO_MOUNT_REMOUNT] = "--remount", |
| 17 | [TOMOYO_MOUNT_MAKE_UNBINDABLE] = "--make-unbindable", |
| 18 | [TOMOYO_MOUNT_MAKE_PRIVATE] = "--make-private", |
| 19 | [TOMOYO_MOUNT_MAKE_SLAVE] = "--make-slave", |
| 20 | [TOMOYO_MOUNT_MAKE_SHARED] = "--make-shared", |
| 21 | }; |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 22 | |
| 23 | /** |
Tetsuo Handa | 99a8525 | 2010-06-16 16:22:51 +0900 | [diff] [blame] | 24 | * tomoyo_audit_mount_log - Audit mount log. |
| 25 | * |
| 26 | * @r: Pointer to "struct tomoyo_request_info". |
| 27 | * |
| 28 | * Returns 0 on success, negative value otherwise. |
| 29 | */ |
| 30 | static int tomoyo_audit_mount_log(struct tomoyo_request_info *r) |
| 31 | { |
Tetsuo Handa | eadd99c | 2011-06-26 23:18:58 +0900 | [diff] [blame] | 32 | return tomoyo_supervisor(r, "file mount %s %s %s 0x%lX\n", |
Tetsuo Handa | 7c75964 | 2011-06-26 23:15:31 +0900 | [diff] [blame] | 33 | r->param.mount.dev->name, |
Tetsuo Handa | eadd99c | 2011-06-26 23:18:58 +0900 | [diff] [blame] | 34 | r->param.mount.dir->name, |
| 35 | r->param.mount.type->name, |
| 36 | r->param.mount.flags); |
Tetsuo Handa | 99a8525 | 2010-06-16 16:22:51 +0900 | [diff] [blame] | 37 | } |
| 38 | |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame] | 39 | /** |
| 40 | * tomoyo_check_mount_acl - Check permission for path path path number operation. |
| 41 | * |
| 42 | * @r: Pointer to "struct tomoyo_request_info". |
| 43 | * @ptr: Pointer to "struct tomoyo_acl_info". |
| 44 | * |
| 45 | * Returns true if granted, false otherwise. |
| 46 | */ |
Tetsuo Handa | 484ca79 | 2010-07-29 14:29:55 +0900 | [diff] [blame] | 47 | static bool tomoyo_check_mount_acl(struct tomoyo_request_info *r, |
Tetsuo Handa | 99a8525 | 2010-06-16 16:22:51 +0900 | [diff] [blame] | 48 | const struct tomoyo_acl_info *ptr) |
| 49 | { |
| 50 | const struct tomoyo_mount_acl *acl = |
| 51 | container_of(ptr, typeof(*acl), head); |
Tetsuo Handa | cdcf672 | 2019-01-24 18:37:35 +0900 | [diff] [blame] | 52 | |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame] | 53 | return tomoyo_compare_number_union(r->param.mount.flags, |
| 54 | &acl->flags) && |
| 55 | tomoyo_compare_name_union(r->param.mount.type, |
| 56 | &acl->fs_type) && |
| 57 | tomoyo_compare_name_union(r->param.mount.dir, |
| 58 | &acl->dir_name) && |
Tetsuo Handa | 99a8525 | 2010-06-16 16:22:51 +0900 | [diff] [blame] | 59 | (!r->param.mount.need_dev || |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame] | 60 | tomoyo_compare_name_union(r->param.mount.dev, |
| 61 | &acl->dev_name)); |
Tetsuo Handa | 99a8525 | 2010-06-16 16:22:51 +0900 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | /** |
Tetsuo Handa | d795ef9e | 2010-06-16 16:24:58 +0900 | [diff] [blame] | 65 | * tomoyo_mount_acl - Check permission for mount() operation. |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 66 | * |
| 67 | * @r: Pointer to "struct tomoyo_request_info". |
Tetsuo Handa | 0f2a55d | 2011-07-14 14:46:51 +0900 | [diff] [blame] | 68 | * @dev_name: Name of device file. Maybe NULL. |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 69 | * @dir: Pointer to "struct path". |
| 70 | * @type: Name of filesystem type. |
| 71 | * @flags: Mount options. |
| 72 | * |
| 73 | * Returns 0 on success, negative value otherwise. |
| 74 | * |
| 75 | * Caller holds tomoyo_read_lock(). |
| 76 | */ |
Al Viro | 808d4e3 | 2012-10-11 11:42:01 -0400 | [diff] [blame] | 77 | static int tomoyo_mount_acl(struct tomoyo_request_info *r, |
| 78 | const char *dev_name, |
Al Viro | e6641ed | 2016-03-25 14:41:28 -0400 | [diff] [blame] | 79 | const struct path *dir, const char *type, |
Tetsuo Handa | b5bc60b | 2011-06-26 23:16:03 +0900 | [diff] [blame] | 80 | unsigned long flags) |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 81 | { |
Tetsuo Handa | 97fb35e | 2011-07-08 13:25:53 +0900 | [diff] [blame] | 82 | struct tomoyo_obj_info obj = { }; |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 83 | struct path path; |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 84 | struct file_system_type *fstype = NULL; |
| 85 | const char *requested_type = NULL; |
| 86 | const char *requested_dir_name = NULL; |
| 87 | const char *requested_dev_name = NULL; |
| 88 | struct tomoyo_path_info rtype; |
| 89 | struct tomoyo_path_info rdev; |
| 90 | struct tomoyo_path_info rdir; |
| 91 | int need_dev = 0; |
| 92 | int error = -ENOMEM; |
Tetsuo Handa | cdcf672 | 2019-01-24 18:37:35 +0900 | [diff] [blame] | 93 | |
Tetsuo Handa | 97fb35e | 2011-07-08 13:25:53 +0900 | [diff] [blame] | 94 | r->obj = &obj; |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 95 | |
| 96 | /* Get fstype. */ |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 97 | requested_type = tomoyo_encode(type); |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 98 | if (!requested_type) |
| 99 | goto out; |
| 100 | rtype.name = requested_type; |
| 101 | tomoyo_fill_path_info(&rtype); |
| 102 | |
| 103 | /* Get mount point. */ |
Tetsuo Handa | 97fb35e | 2011-07-08 13:25:53 +0900 | [diff] [blame] | 104 | obj.path2 = *dir; |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 105 | requested_dir_name = tomoyo_realpath_from_path(dir); |
| 106 | if (!requested_dir_name) { |
| 107 | error = -ENOMEM; |
| 108 | goto out; |
| 109 | } |
| 110 | rdir.name = requested_dir_name; |
| 111 | tomoyo_fill_path_info(&rdir); |
| 112 | |
| 113 | /* Compare fs name. */ |
Tetsuo Handa | b5bc60b | 2011-06-26 23:16:03 +0900 | [diff] [blame] | 114 | if (type == tomoyo_mounts[TOMOYO_MOUNT_REMOUNT]) { |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 115 | /* dev_name is ignored. */ |
Tetsuo Handa | b5bc60b | 2011-06-26 23:16:03 +0900 | [diff] [blame] | 116 | } else if (type == tomoyo_mounts[TOMOYO_MOUNT_MAKE_UNBINDABLE] || |
| 117 | type == tomoyo_mounts[TOMOYO_MOUNT_MAKE_PRIVATE] || |
| 118 | type == tomoyo_mounts[TOMOYO_MOUNT_MAKE_SLAVE] || |
| 119 | type == tomoyo_mounts[TOMOYO_MOUNT_MAKE_SHARED]) { |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 120 | /* dev_name is ignored. */ |
Tetsuo Handa | b5bc60b | 2011-06-26 23:16:03 +0900 | [diff] [blame] | 121 | } else if (type == tomoyo_mounts[TOMOYO_MOUNT_BIND] || |
| 122 | type == tomoyo_mounts[TOMOYO_MOUNT_MOVE]) { |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 123 | need_dev = -1; /* dev_name is a directory */ |
| 124 | } else { |
| 125 | fstype = get_fs_type(type); |
| 126 | if (!fstype) { |
| 127 | error = -ENODEV; |
| 128 | goto out; |
| 129 | } |
| 130 | if (fstype->fs_flags & FS_REQUIRES_DEV) |
| 131 | /* dev_name is a block device file. */ |
| 132 | need_dev = 1; |
| 133 | } |
| 134 | if (need_dev) { |
| 135 | /* Get mount point or device file. */ |
Tetsuo Handa | 4e78c72 | 2011-06-13 13:49:11 +0900 | [diff] [blame] | 136 | if (!dev_name || kern_path(dev_name, LOOKUP_FOLLOW, &path)) { |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 137 | error = -ENOENT; |
| 138 | goto out; |
| 139 | } |
Tetsuo Handa | 97fb35e | 2011-07-08 13:25:53 +0900 | [diff] [blame] | 140 | obj.path1 = path; |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 141 | requested_dev_name = tomoyo_realpath_from_path(&path); |
| 142 | if (!requested_dev_name) { |
| 143 | error = -ENOENT; |
| 144 | goto out; |
| 145 | } |
| 146 | } else { |
| 147 | /* Map dev_name to "<NULL>" if no dev_name given. */ |
| 148 | if (!dev_name) |
| 149 | dev_name = "<NULL>"; |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 150 | requested_dev_name = tomoyo_encode(dev_name); |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 151 | if (!requested_dev_name) { |
| 152 | error = -ENOMEM; |
| 153 | goto out; |
| 154 | } |
| 155 | } |
| 156 | rdev.name = requested_dev_name; |
| 157 | tomoyo_fill_path_info(&rdev); |
Tetsuo Handa | cf6e9a6 | 2010-06-16 16:21:36 +0900 | [diff] [blame] | 158 | r->param_type = TOMOYO_TYPE_MOUNT_ACL; |
| 159 | r->param.mount.need_dev = need_dev; |
| 160 | r->param.mount.dev = &rdev; |
| 161 | r->param.mount.dir = &rdir; |
| 162 | r->param.mount.type = &rtype; |
| 163 | r->param.mount.flags = flags; |
Tetsuo Handa | 99a8525 | 2010-06-16 16:22:51 +0900 | [diff] [blame] | 164 | do { |
| 165 | tomoyo_check_acl(r, tomoyo_check_mount_acl); |
| 166 | error = tomoyo_audit_mount_log(r); |
| 167 | } while (error == TOMOYO_RETRY_REQUEST); |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 168 | out: |
| 169 | kfree(requested_dev_name); |
| 170 | kfree(requested_dir_name); |
| 171 | if (fstype) |
| 172 | put_filesystem(fstype); |
| 173 | kfree(requested_type); |
Tetsuo Handa | 97fb35e | 2011-07-08 13:25:53 +0900 | [diff] [blame] | 174 | /* Drop refcount obtained by kern_path(). */ |
| 175 | if (obj.path1.dentry) |
| 176 | path_put(&obj.path1); |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 177 | return error; |
| 178 | } |
| 179 | |
| 180 | /** |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 181 | * tomoyo_mount_permission - Check permission for mount() operation. |
| 182 | * |
Tetsuo Handa | 0f2a55d | 2011-07-14 14:46:51 +0900 | [diff] [blame] | 183 | * @dev_name: Name of device file. Maybe NULL. |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 184 | * @path: Pointer to "struct path". |
Tetsuo Handa | 0f2a55d | 2011-07-14 14:46:51 +0900 | [diff] [blame] | 185 | * @type: Name of filesystem type. Maybe NULL. |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 186 | * @flags: Mount options. |
Tetsuo Handa | 0f2a55d | 2011-07-14 14:46:51 +0900 | [diff] [blame] | 187 | * @data_page: Optional data. Maybe NULL. |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 188 | * |
| 189 | * Returns 0 on success, negative value otherwise. |
| 190 | */ |
Al Viro | e6641ed | 2016-03-25 14:41:28 -0400 | [diff] [blame] | 191 | int tomoyo_mount_permission(const char *dev_name, const struct path *path, |
Tetsuo Handa | b5bc60b | 2011-06-26 23:16:03 +0900 | [diff] [blame] | 192 | const char *type, unsigned long flags, |
| 193 | void *data_page) |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 194 | { |
| 195 | struct tomoyo_request_info r; |
| 196 | int error; |
| 197 | int idx; |
| 198 | |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 199 | if (tomoyo_init_request_info(&r, NULL, TOMOYO_MAC_FILE_MOUNT) |
| 200 | == TOMOYO_CONFIG_DISABLED) |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 201 | return 0; |
Tetsuo Handa | d795ef9e | 2010-06-16 16:24:58 +0900 | [diff] [blame] | 202 | if ((flags & MS_MGC_MSK) == MS_MGC_VAL) |
| 203 | flags &= ~MS_MGC_MSK; |
| 204 | if (flags & MS_REMOUNT) { |
Tetsuo Handa | b5bc60b | 2011-06-26 23:16:03 +0900 | [diff] [blame] | 205 | type = tomoyo_mounts[TOMOYO_MOUNT_REMOUNT]; |
Tetsuo Handa | d795ef9e | 2010-06-16 16:24:58 +0900 | [diff] [blame] | 206 | flags &= ~MS_REMOUNT; |
Tetsuo Handa | df91e49 | 2012-02-29 21:53:22 +0900 | [diff] [blame] | 207 | } else 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; |
Tetsuo Handa | df91e49 | 2012-02-29 21:53:22 +0900 | [diff] [blame] | 210 | } else if (flags & MS_SHARED) { |
| 211 | if (flags & (MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE)) |
| 212 | return -EINVAL; |
Tetsuo Handa | b5bc60b | 2011-06-26 23:16:03 +0900 | [diff] [blame] | 213 | type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_SHARED]; |
Tetsuo Handa | d795ef9e | 2010-06-16 16:24:58 +0900 | [diff] [blame] | 214 | flags &= ~MS_SHARED; |
Tetsuo Handa | df91e49 | 2012-02-29 21:53:22 +0900 | [diff] [blame] | 215 | } else if (flags & MS_PRIVATE) { |
| 216 | if (flags & (MS_SHARED | MS_SLAVE | MS_UNBINDABLE)) |
| 217 | return -EINVAL; |
| 218 | type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_PRIVATE]; |
| 219 | flags &= ~MS_PRIVATE; |
| 220 | } else if (flags & MS_SLAVE) { |
| 221 | if (flags & (MS_SHARED | MS_PRIVATE | MS_UNBINDABLE)) |
| 222 | return -EINVAL; |
| 223 | type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_SLAVE]; |
| 224 | flags &= ~MS_SLAVE; |
| 225 | } else if (flags & MS_UNBINDABLE) { |
| 226 | if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE)) |
| 227 | return -EINVAL; |
| 228 | type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_UNBINDABLE]; |
| 229 | flags &= ~MS_UNBINDABLE; |
| 230 | } else if (flags & MS_MOVE) { |
| 231 | type = tomoyo_mounts[TOMOYO_MOUNT_MOVE]; |
| 232 | flags &= ~MS_MOVE; |
Tetsuo Handa | d795ef9e | 2010-06-16 16:24:58 +0900 | [diff] [blame] | 233 | } |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 234 | if (!type) |
| 235 | type = "<NULL>"; |
| 236 | idx = tomoyo_read_lock(); |
| 237 | error = tomoyo_mount_acl(&r, dev_name, path, type, flags); |
| 238 | tomoyo_read_unlock(idx); |
| 239 | return error; |
| 240 | } |