mount.c 6.1 KB
Newer Older
T
Tetsuo Handa 已提交
1 2 3 4 5 6 7 8 9
/*
 * security/tomoyo/mount.c
 *
 * Copyright (C) 2005-2010  NTT DATA CORPORATION
 */

#include <linux/slab.h>
#include "common.h"

T
Tetsuo Handa 已提交
10 11 12 13 14 15 16 17 18 19
/* String table for special mount operations. */
static const char * const tomoyo_mounts[TOMOYO_MAX_SPECIAL_MOUNT] = {
	[TOMOYO_MOUNT_BIND]            = "--bind",
	[TOMOYO_MOUNT_MOVE]            = "--move",
	[TOMOYO_MOUNT_REMOUNT]         = "--remount",
	[TOMOYO_MOUNT_MAKE_UNBINDABLE] = "--make-unbindable",
	[TOMOYO_MOUNT_MAKE_PRIVATE]    = "--make-private",
	[TOMOYO_MOUNT_MAKE_SLAVE]      = "--make-slave",
	[TOMOYO_MOUNT_MAKE_SHARED]     = "--make-shared",
};
T
Tetsuo Handa 已提交
20

21 22 23 24 25 26 27 28 29
/**
 * tomoyo_audit_mount_log - Audit mount log.
 *
 * @r: Pointer to "struct tomoyo_request_info".
 *
 * Returns 0 on success, negative value otherwise.
 */
static int tomoyo_audit_mount_log(struct tomoyo_request_info *r)
{
T
Tetsuo Handa 已提交
30
	return tomoyo_supervisor(r, "file mount %s %s %s 0x%lX\n",
T
Tetsuo Handa 已提交
31
				 r->param.mount.dev->name,
T
Tetsuo Handa 已提交
32 33 34
				 r->param.mount.dir->name,
				 r->param.mount.type->name,
				 r->param.mount.flags);
35 36
}

T
Tetsuo Handa 已提交
37 38 39 40 41 42 43 44
/**
 * tomoyo_check_mount_acl - Check permission for path path path number operation.
 *
 * @r:   Pointer to "struct tomoyo_request_info".
 * @ptr: Pointer to "struct tomoyo_acl_info".
 *
 * Returns true if granted, false otherwise.
 */
45
static bool tomoyo_check_mount_acl(struct tomoyo_request_info *r,
46 47 48 49
				   const struct tomoyo_acl_info *ptr)
{
	const struct tomoyo_mount_acl *acl =
		container_of(ptr, typeof(*acl), head);
T
Tetsuo Handa 已提交
50 51 52 53 54 55
	return tomoyo_compare_number_union(r->param.mount.flags,
					   &acl->flags) &&
		tomoyo_compare_name_union(r->param.mount.type,
					  &acl->fs_type) &&
		tomoyo_compare_name_union(r->param.mount.dir,
					  &acl->dir_name) &&
56
		(!r->param.mount.need_dev ||
T
Tetsuo Handa 已提交
57 58
		 tomoyo_compare_name_union(r->param.mount.dev,
					   &acl->dev_name));
59 60
}

T
Tetsuo Handa 已提交
61
/**
62
 * tomoyo_mount_acl - Check permission for mount() operation.
T
Tetsuo Handa 已提交
63 64 65 66 67 68 69 70 71 72 73
 *
 * @r:        Pointer to "struct tomoyo_request_info".
 * @dev_name: Name of device file.
 * @dir:      Pointer to "struct path".
 * @type:     Name of filesystem type.
 * @flags:    Mount options.
 *
 * Returns 0 on success, negative value otherwise.
 *
 * Caller holds tomoyo_read_lock().
 */
74
static int tomoyo_mount_acl(struct tomoyo_request_info *r, char *dev_name,
T
Tetsuo Handa 已提交
75 76
			    struct path *dir, const char *type,
			    unsigned long flags)
T
Tetsuo Handa 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89
{
	struct path path;
	struct file_system_type *fstype = NULL;
	const char *requested_type = NULL;
	const char *requested_dir_name = NULL;
	const char *requested_dev_name = NULL;
	struct tomoyo_path_info rtype;
	struct tomoyo_path_info rdev;
	struct tomoyo_path_info rdir;
	int need_dev = 0;
	int error = -ENOMEM;

	/* Get fstype. */
T
Tetsuo Handa 已提交
90
	requested_type = tomoyo_encode(type);
T
Tetsuo Handa 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
	if (!requested_type)
		goto out;
	rtype.name = requested_type;
	tomoyo_fill_path_info(&rtype);

	/* Get mount point. */
	requested_dir_name = tomoyo_realpath_from_path(dir);
	if (!requested_dir_name) {
		error = -ENOMEM;
		goto out;
	}
	rdir.name = requested_dir_name;
	tomoyo_fill_path_info(&rdir);

	/* Compare fs name. */
T
Tetsuo Handa 已提交
106
	if (type == tomoyo_mounts[TOMOYO_MOUNT_REMOUNT]) {
T
Tetsuo Handa 已提交
107
		/* dev_name is ignored. */
T
Tetsuo Handa 已提交
108 109 110 111
	} else if (type == tomoyo_mounts[TOMOYO_MOUNT_MAKE_UNBINDABLE] ||
		   type == tomoyo_mounts[TOMOYO_MOUNT_MAKE_PRIVATE] ||
		   type == tomoyo_mounts[TOMOYO_MOUNT_MAKE_SLAVE] ||
		   type == tomoyo_mounts[TOMOYO_MOUNT_MAKE_SHARED]) {
T
Tetsuo Handa 已提交
112
		/* dev_name is ignored. */
T
Tetsuo Handa 已提交
113 114
	} else if (type == tomoyo_mounts[TOMOYO_MOUNT_BIND] ||
		   type == tomoyo_mounts[TOMOYO_MOUNT_MOVE]) {
T
Tetsuo Handa 已提交
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
		need_dev = -1; /* dev_name is a directory */
	} else {
		fstype = get_fs_type(type);
		if (!fstype) {
			error = -ENODEV;
			goto out;
		}
		if (fstype->fs_flags & FS_REQUIRES_DEV)
			/* dev_name is a block device file. */
			need_dev = 1;
	}
	if (need_dev) {
		/* Get mount point or device file. */
		if (kern_path(dev_name, LOOKUP_FOLLOW, &path)) {
			error = -ENOENT;
			goto out;
		}
		requested_dev_name = tomoyo_realpath_from_path(&path);
133
		path_put(&path);
T
Tetsuo Handa 已提交
134 135 136 137 138 139 140 141
		if (!requested_dev_name) {
			error = -ENOENT;
			goto out;
		}
	} else {
		/* Map dev_name to "<NULL>" if no dev_name given. */
		if (!dev_name)
			dev_name = "<NULL>";
T
Tetsuo Handa 已提交
142
		requested_dev_name = tomoyo_encode(dev_name);
T
Tetsuo Handa 已提交
143 144 145 146 147 148 149
		if (!requested_dev_name) {
			error = -ENOMEM;
			goto out;
		}
	}
	rdev.name = requested_dev_name;
	tomoyo_fill_path_info(&rdev);
150 151 152 153 154 155
	r->param_type = TOMOYO_TYPE_MOUNT_ACL;
	r->param.mount.need_dev = need_dev;
	r->param.mount.dev = &rdev;
	r->param.mount.dir = &rdir;
	r->param.mount.type = &rtype;
	r->param.mount.flags = flags;
156 157 158 159
	do {
		tomoyo_check_acl(r, tomoyo_check_mount_acl);
		error = tomoyo_audit_mount_log(r);
	} while (error == TOMOYO_RETRY_REQUEST);
T
Tetsuo Handa 已提交
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
 out:
	kfree(requested_dev_name);
	kfree(requested_dir_name);
	if (fstype)
		put_filesystem(fstype);
	kfree(requested_type);
	return error;
}

/**
 * tomoyo_mount_permission - Check permission for mount() operation.
 *
 * @dev_name:  Name of device file.
 * @path:      Pointer to "struct path".
 * @type:      Name of filesystem type. May be NULL.
 * @flags:     Mount options.
 * @data_page: Optional data. May be NULL.
 *
 * Returns 0 on success, negative value otherwise.
 */
T
Tetsuo Handa 已提交
180 181 182
int tomoyo_mount_permission(char *dev_name, struct path *path,
			    const char *type, unsigned long flags,
			    void *data_page)
T
Tetsuo Handa 已提交
183 184 185 186 187
{
	struct tomoyo_request_info r;
	int error;
	int idx;

T
Tetsuo Handa 已提交
188 189
	if (tomoyo_init_request_info(&r, NULL, TOMOYO_MAC_FILE_MOUNT)
	    == TOMOYO_CONFIG_DISABLED)
T
Tetsuo Handa 已提交
190
		return 0;
191 192 193
	if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
		flags &= ~MS_MGC_MSK;
	if (flags & MS_REMOUNT) {
T
Tetsuo Handa 已提交
194
		type = tomoyo_mounts[TOMOYO_MOUNT_REMOUNT];
195 196 197
		flags &= ~MS_REMOUNT;
	}
	if (flags & MS_MOVE) {
T
Tetsuo Handa 已提交
198
		type = tomoyo_mounts[TOMOYO_MOUNT_MOVE];
199 200 201
		flags &= ~MS_MOVE;
	}
	if (flags & MS_BIND) {
T
Tetsuo Handa 已提交
202
		type = tomoyo_mounts[TOMOYO_MOUNT_BIND];
203 204 205
		flags &= ~MS_BIND;
	}
	if (flags & MS_UNBINDABLE) {
T
Tetsuo Handa 已提交
206
		type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_UNBINDABLE];
207 208 209
		flags &= ~MS_UNBINDABLE;
	}
	if (flags & MS_PRIVATE) {
T
Tetsuo Handa 已提交
210
		type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_PRIVATE];
211 212 213
		flags &= ~MS_PRIVATE;
	}
	if (flags & MS_SLAVE) {
T
Tetsuo Handa 已提交
214
		type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_SLAVE];
215 216 217
		flags &= ~MS_SLAVE;
	}
	if (flags & MS_SHARED) {
T
Tetsuo Handa 已提交
218
		type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_SHARED];
219 220
		flags &= ~MS_SHARED;
	}
T
Tetsuo Handa 已提交
221 222 223 224 225 226 227
	if (!type)
		type = "<NULL>";
	idx = tomoyo_read_lock();
	error = tomoyo_mount_acl(&r, dev_name, path, type, flags);
	tomoyo_read_unlock(idx);
	return error;
}