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