tomoyo.c 8.0 KB
Newer Older
K
Kentaro Takeda 已提交
1 2 3 4 5 6 7
/*
 * security/tomoyo/tomoyo.c
 *
 * LSM hooks for TOMOYO Linux.
 *
 * Copyright (C) 2005-2009  NTT DATA CORPORATION
 *
T
Tetsuo Handa 已提交
8
 * Version: 2.2.0   2009/04/01
K
Kentaro Takeda 已提交
9 10 11 12 13 14
 *
 */

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

15 16 17 18 19 20
static int tomoyo_cred_alloc_blank(struct cred *new, gfp_t gfp)
{
	new->security = NULL;
	return 0;
}

K
Kentaro Takeda 已提交
21 22 23
static int tomoyo_cred_prepare(struct cred *new, const struct cred *old,
			       gfp_t gfp)
{
24 25 26 27
	struct tomoyo_domain_info *domain = old->security;
	new->security = domain;
	if (domain)
		atomic_inc(&domain->users);
K
Kentaro Takeda 已提交
28 29 30
	return 0;
}

31 32
static void tomoyo_cred_transfer(struct cred *new, const struct cred *old)
{
33 34 35 36 37 38 39 40
	tomoyo_cred_prepare(new, old, 0);
}

static void tomoyo_cred_free(struct cred *cred)
{
	struct tomoyo_domain_info *domain = cred->security;
	if (domain)
		atomic_dec(&domain->users);
41 42
}

K
Kentaro Takeda 已提交
43 44
static int tomoyo_bprm_set_creds(struct linux_binprm *bprm)
{
45 46 47 48 49 50
	int rc;

	rc = cap_bprm_set_creds(bprm);
	if (rc)
		return rc;

K
Kentaro Takeda 已提交
51 52 53 54 55 56 57 58 59 60 61 62
	/*
	 * Do only if this function is called for the first time of an execve
	 * operation.
	 */
	if (bprm->cred_prepared)
		return 0;
	/*
	 * Load policy if /sbin/tomoyo-init exists and /sbin/init is requested
	 * for the first time.
	 */
	if (!tomoyo_policy_loaded)
		tomoyo_load_policy(bprm->filename);
63 64 65 66 67 68 69 70
	/*
	 * Release reference to "struct tomoyo_domain_info" stored inside
	 * "bprm->cred->security". New reference to "struct tomoyo_domain_info"
	 * stored inside "bprm->cred->security" will be acquired later inside
	 * tomoyo_find_next_domain().
	 */
	atomic_dec(&((struct tomoyo_domain_info *)
		     bprm->cred->security)->users);
K
Kentaro Takeda 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
	/*
	 * Tell tomoyo_bprm_check_security() is called for the first time of an
	 * execve operation.
	 */
	bprm->cred->security = NULL;
	return 0;
}

static int tomoyo_bprm_check_security(struct linux_binprm *bprm)
{
	struct tomoyo_domain_info *domain = bprm->cred->security;

	/*
	 * Execute permission is checked against pathname passed to do_execve()
	 * using current domain.
	 */
87 88 89 90 91 92
	if (!domain) {
		const int idx = tomoyo_read_lock();
		const int err = tomoyo_find_next_domain(bprm);
		tomoyo_read_unlock(idx);
		return err;
	}
K
Kentaro Takeda 已提交
93 94 95 96 97 98 99 100 101 102
	/*
	 * Read permission is checked against interpreters using next domain.
	 * '1' is the result of open_to_namei_flags(O_RDONLY).
	 */
	return tomoyo_check_open_permission(domain, &bprm->file->f_path, 1);
}

static int tomoyo_path_truncate(struct path *path, loff_t length,
				unsigned int time_attrs)
{
T
Tetsuo Handa 已提交
103
	return tomoyo_path_perm(tomoyo_domain(), TOMOYO_TYPE_TRUNCATE, path);
K
Kentaro Takeda 已提交
104 105 106 107 108
}

static int tomoyo_path_unlink(struct path *parent, struct dentry *dentry)
{
	struct path path = { parent->mnt, dentry };
T
Tetsuo Handa 已提交
109
	return tomoyo_path_perm(tomoyo_domain(), TOMOYO_TYPE_UNLINK, &path);
K
Kentaro Takeda 已提交
110 111 112 113 114 115
}

static int tomoyo_path_mkdir(struct path *parent, struct dentry *dentry,
			     int mode)
{
	struct path path = { parent->mnt, dentry };
T
Tetsuo Handa 已提交
116
	return tomoyo_path_perm(tomoyo_domain(), TOMOYO_TYPE_MKDIR, &path);
K
Kentaro Takeda 已提交
117 118 119 120 121
}

static int tomoyo_path_rmdir(struct path *parent, struct dentry *dentry)
{
	struct path path = { parent->mnt, dentry };
T
Tetsuo Handa 已提交
122
	return tomoyo_path_perm(tomoyo_domain(), TOMOYO_TYPE_RMDIR, &path);
K
Kentaro Takeda 已提交
123 124 125 126 127 128
}

static int tomoyo_path_symlink(struct path *parent, struct dentry *dentry,
			       const char *old_name)
{
	struct path path = { parent->mnt, dentry };
T
Tetsuo Handa 已提交
129
	return tomoyo_path_perm(tomoyo_domain(), TOMOYO_TYPE_SYMLINK, &path);
K
Kentaro Takeda 已提交
130 131 132 133 134 135
}

static int tomoyo_path_mknod(struct path *parent, struct dentry *dentry,
			     int mode, unsigned int dev)
{
	struct path path = { parent->mnt, dentry };
T
Tetsuo Handa 已提交
136
	int type = TOMOYO_TYPE_CREATE;
K
Kentaro Takeda 已提交
137 138 139

	switch (mode & S_IFMT) {
	case S_IFCHR:
T
Tetsuo Handa 已提交
140
		type = TOMOYO_TYPE_MKCHAR;
K
Kentaro Takeda 已提交
141 142
		break;
	case S_IFBLK:
T
Tetsuo Handa 已提交
143
		type = TOMOYO_TYPE_MKBLOCK;
K
Kentaro Takeda 已提交
144 145
		break;
	case S_IFIFO:
T
Tetsuo Handa 已提交
146
		type = TOMOYO_TYPE_MKFIFO;
K
Kentaro Takeda 已提交
147 148
		break;
	case S_IFSOCK:
T
Tetsuo Handa 已提交
149
		type = TOMOYO_TYPE_MKSOCK;
K
Kentaro Takeda 已提交
150 151
		break;
	}
T
Tetsuo Handa 已提交
152
	return tomoyo_path_perm(tomoyo_domain(), type, &path);
K
Kentaro Takeda 已提交
153 154 155 156 157 158 159
}

static int tomoyo_path_link(struct dentry *old_dentry, struct path *new_dir,
			    struct dentry *new_dentry)
{
	struct path path1 = { new_dir->mnt, old_dentry };
	struct path path2 = { new_dir->mnt, new_dentry };
T
Tetsuo Handa 已提交
160 161
	return tomoyo_path2_perm(tomoyo_domain(), TOMOYO_TYPE_LINK, &path1,
				 &path2);
K
Kentaro Takeda 已提交
162 163 164 165 166 167 168 169 170
}

static int tomoyo_path_rename(struct path *old_parent,
			      struct dentry *old_dentry,
			      struct path *new_parent,
			      struct dentry *new_dentry)
{
	struct path path1 = { old_parent->mnt, old_dentry };
	struct path path2 = { new_parent->mnt, new_dentry };
T
Tetsuo Handa 已提交
171 172
	return tomoyo_path2_perm(tomoyo_domain(), TOMOYO_TYPE_RENAME, &path1,
				 &path2);
K
Kentaro Takeda 已提交
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
}

static int tomoyo_file_fcntl(struct file *file, unsigned int cmd,
			     unsigned long arg)
{
	if (cmd == F_SETFL && ((arg ^ file->f_flags) & O_APPEND))
		return tomoyo_check_rewrite_permission(tomoyo_domain(), file);
	return 0;
}

static int tomoyo_dentry_open(struct file *f, const struct cred *cred)
{
	int flags = f->f_flags;

	if ((flags + 1) & O_ACCMODE)
		flags++;
	flags |= f->f_flags & (O_APPEND | O_TRUNC);
	/* Don't check read permission here if called from do_execve(). */
	if (current->in_execve)
		return 0;
	return tomoyo_check_open_permission(tomoyo_domain(), &f->f_path, flags);
}

196 197 198
static int tomoyo_file_ioctl(struct file *file, unsigned int cmd,
			     unsigned long arg)
{
T
Tetsuo Handa 已提交
199 200
	return tomoyo_path_perm(tomoyo_domain(), TOMOYO_TYPE_IOCTL,
				&file->f_path);
201 202 203 204 205 206
}

static int tomoyo_path_chmod(struct dentry *dentry, struct vfsmount *mnt,
			     mode_t mode)
{
	struct path path = { mnt, dentry };
T
Tetsuo Handa 已提交
207
	return tomoyo_path_perm(tomoyo_domain(), TOMOYO_TYPE_CHMOD, &path);
208 209 210 211 212 213
}

static int tomoyo_path_chown(struct path *path, uid_t uid, gid_t gid)
{
	int error = 0;
	if (uid != (uid_t) -1)
T
Tetsuo Handa 已提交
214 215
		error = tomoyo_path_perm(tomoyo_domain(), TOMOYO_TYPE_CHOWN,
					 path);
216
	if (!error && gid != (gid_t) -1)
T
Tetsuo Handa 已提交
217 218
		error = tomoyo_path_perm(tomoyo_domain(), TOMOYO_TYPE_CHGRP,
					 path);
219 220 221 222 223
	return error;
}

static int tomoyo_path_chroot(struct path *path)
{
T
Tetsuo Handa 已提交
224
	return tomoyo_path_perm(tomoyo_domain(), TOMOYO_TYPE_CHROOT, path);
225 226 227 228 229
}

static int tomoyo_sb_mount(char *dev_name, struct path *path,
			   char *type, unsigned long flags, void *data)
{
T
Tetsuo Handa 已提交
230
	return tomoyo_path_perm(tomoyo_domain(), TOMOYO_TYPE_MOUNT, path);
231 232 233 234 235
}

static int tomoyo_sb_umount(struct vfsmount *mnt, int flags)
{
	struct path path = { mnt, mnt->mnt_root };
T
Tetsuo Handa 已提交
236
	return tomoyo_path_perm(tomoyo_domain(), TOMOYO_TYPE_UMOUNT, &path);
237 238 239 240
}

static int tomoyo_sb_pivotroot(struct path *old_path, struct path *new_path)
{
T
Tetsuo Handa 已提交
241 242
	return tomoyo_path2_perm(tomoyo_domain(), TOMOYO_TYPE_PIVOT_ROOT,
				 new_path, old_path);
243 244
}

245 246 247 248
/*
 * tomoyo_security_ops is a "struct security_operations" which is used for
 * registering TOMOYO.
 */
K
Kentaro Takeda 已提交
249 250
static struct security_operations tomoyo_security_ops = {
	.name                = "tomoyo",
251
	.cred_alloc_blank    = tomoyo_cred_alloc_blank,
K
Kentaro Takeda 已提交
252
	.cred_prepare        = tomoyo_cred_prepare,
253
	.cred_transfer	     = tomoyo_cred_transfer,
254
	.cred_free           = tomoyo_cred_free,
K
Kentaro Takeda 已提交
255 256 257 258 259 260 261 262 263 264 265 266
	.bprm_set_creds      = tomoyo_bprm_set_creds,
	.bprm_check_security = tomoyo_bprm_check_security,
	.file_fcntl          = tomoyo_file_fcntl,
	.dentry_open         = tomoyo_dentry_open,
	.path_truncate       = tomoyo_path_truncate,
	.path_unlink         = tomoyo_path_unlink,
	.path_mkdir          = tomoyo_path_mkdir,
	.path_rmdir          = tomoyo_path_rmdir,
	.path_symlink        = tomoyo_path_symlink,
	.path_mknod          = tomoyo_path_mknod,
	.path_link           = tomoyo_path_link,
	.path_rename         = tomoyo_path_rename,
267 268 269 270 271 272 273
	.file_ioctl          = tomoyo_file_ioctl,
	.path_chmod          = tomoyo_path_chmod,
	.path_chown          = tomoyo_path_chown,
	.path_chroot         = tomoyo_path_chroot,
	.sb_mount            = tomoyo_sb_mount,
	.sb_umount           = tomoyo_sb_umount,
	.sb_pivotroot        = tomoyo_sb_pivotroot,
K
Kentaro Takeda 已提交
274 275
};

276 277 278
/* Lock for GC. */
struct srcu_struct tomoyo_ss;

K
Kentaro Takeda 已提交
279 280 281 282 283 284 285
static int __init tomoyo_init(void)
{
	struct cred *cred = (struct cred *) current_cred();

	if (!security_module_enable(&tomoyo_security_ops))
		return 0;
	/* register ourselves with the security framework */
286 287
	if (register_security(&tomoyo_security_ops) ||
	    init_srcu_struct(&tomoyo_ss))
K
Kentaro Takeda 已提交
288 289 290
		panic("Failure registering TOMOYO Linux");
	printk(KERN_INFO "TOMOYO Linux initialized\n");
	cred->security = &tomoyo_kernel_domain;
291
	tomoyo_realpath_init();
K
Kentaro Takeda 已提交
292 293 294 295
	return 0;
}

security_initcall(tomoyo_init);