tomoyo.c 14.5 KB
Newer Older
K
Kentaro Takeda 已提交
1 2 3
/*
 * security/tomoyo/tomoyo.c
 *
T
Tetsuo Handa 已提交
4
 * Copyright (C) 2005-2011  NTT DATA CORPORATION
K
Kentaro Takeda 已提交
5 6
 */

C
Casey Schaufler 已提交
7
#include <linux/lsm_hooks.h>
K
Kentaro Takeda 已提交
8 9
#include "common.h"

T
Tetsuo Handa 已提交
10 11 12 13 14 15 16 17
/**
 * tomoyo_cred_alloc_blank - Target for security_cred_alloc_blank().
 *
 * @new: Pointer to "struct cred".
 * @gfp: Memory allocation flags.
 *
 * Returns 0.
 */
18 19 20 21 22 23
static int tomoyo_cred_alloc_blank(struct cred *new, gfp_t gfp)
{
	new->security = NULL;
	return 0;
}

T
Tetsuo Handa 已提交
24 25 26 27 28 29 30 31 32
/**
 * tomoyo_cred_prepare - Target for security_prepare_creds().
 *
 * @new: Pointer to "struct cred".
 * @old: Pointer to "struct cred".
 * @gfp: Memory allocation flags.
 *
 * Returns 0.
 */
K
Kentaro Takeda 已提交
33 34 35
static int tomoyo_cred_prepare(struct cred *new, const struct cred *old,
			       gfp_t gfp)
{
36 37 38 39
	struct tomoyo_domain_info *domain = old->security;
	new->security = domain;
	if (domain)
		atomic_inc(&domain->users);
K
Kentaro Takeda 已提交
40 41 42
	return 0;
}

T
Tetsuo Handa 已提交
43 44 45 46 47 48
/**
 * tomoyo_cred_transfer - Target for security_transfer_creds().
 *
 * @new: Pointer to "struct cred".
 * @old: Pointer to "struct cred".
 */
49 50
static void tomoyo_cred_transfer(struct cred *new, const struct cred *old)
{
51 52 53
	tomoyo_cred_prepare(new, old, 0);
}

T
Tetsuo Handa 已提交
54 55 56 57 58
/**
 * tomoyo_cred_free - Target for security_cred_free().
 *
 * @cred: Pointer to "struct cred".
 */
59 60 61 62 63
static void tomoyo_cred_free(struct cred *cred)
{
	struct tomoyo_domain_info *domain = cred->security;
	if (domain)
		atomic_dec(&domain->users);
64 65
}

T
Tetsuo Handa 已提交
66 67 68 69 70 71 72
/**
 * tomoyo_bprm_set_creds - Target for security_bprm_set_creds().
 *
 * @bprm: Pointer to "struct linux_binprm".
 *
 * Returns 0 on success, negative value otherwise.
 */
K
Kentaro Takeda 已提交
73 74
static int tomoyo_bprm_set_creds(struct linux_binprm *bprm)
{
75 76 77 78 79 80
	int rc;

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

K
Kentaro Takeda 已提交
81 82 83 84 85 86
	/*
	 * Do only if this function is called for the first time of an execve
	 * operation.
	 */
	if (bprm->cred_prepared)
		return 0;
87
#ifndef CONFIG_SECURITY_TOMOYO_OMIT_USERSPACE_LOADER
K
Kentaro Takeda 已提交
88 89 90 91 92 93
	/*
	 * 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);
94
#endif
95 96 97 98 99 100 101 102
	/*
	 * 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 已提交
103 104 105 106 107 108 109 110
	/*
	 * Tell tomoyo_bprm_check_security() is called for the first time of an
	 * execve operation.
	 */
	bprm->cred->security = NULL;
	return 0;
}

T
Tetsuo Handa 已提交
111 112 113 114 115 116 117
/**
 * tomoyo_bprm_check_security - Target for security_bprm_check().
 *
 * @bprm: Pointer to "struct linux_binprm".
 *
 * Returns 0 on success, negative value otherwise.
 */
K
Kentaro Takeda 已提交
118 119 120 121 122 123 124 125
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.
	 */
126 127 128 129 130 131
	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 已提交
132 133 134
	/*
	 * Read permission is checked against interpreters using next domain.
	 */
T
Tetsuo Handa 已提交
135 136
	return tomoyo_check_open_permission(domain, &bprm->file->f_path,
					    O_RDONLY);
K
Kentaro Takeda 已提交
137 138
}

T
Tetsuo Handa 已提交
139 140 141 142 143 144 145 146
/**
 * tomoyo_inode_getattr - Target for security_inode_getattr().
 *
 * @mnt:    Pointer to "struct vfsmount".
 * @dentry: Pointer to "struct dentry".
 *
 * Returns 0 on success, negative value otherwise.
 */
147
static int tomoyo_inode_getattr(const struct path *path)
T
Tetsuo Handa 已提交
148
{
149
	return tomoyo_path_perm(TOMOYO_TYPE_GETATTR, path, NULL);
T
Tetsuo Handa 已提交
150 151
}

T
Tetsuo Handa 已提交
152 153 154 155 156 157 158
/**
 * tomoyo_path_truncate - Target for security_path_truncate().
 *
 * @path: Pointer to "struct path".
 *
 * Returns 0 on success, negative value otherwise.
 */
159
static int tomoyo_path_truncate(struct path *path)
K
Kentaro Takeda 已提交
160
{
T
Tetsuo Handa 已提交
161
	return tomoyo_path_perm(TOMOYO_TYPE_TRUNCATE, path, NULL);
K
Kentaro Takeda 已提交
162 163
}

T
Tetsuo Handa 已提交
164 165 166 167 168 169 170 171
/**
 * tomoyo_path_unlink - Target for security_path_unlink().
 *
 * @parent: Pointer to "struct path".
 * @dentry: Pointer to "struct dentry".
 *
 * Returns 0 on success, negative value otherwise.
 */
K
Kentaro Takeda 已提交
172 173 174
static int tomoyo_path_unlink(struct path *parent, struct dentry *dentry)
{
	struct path path = { parent->mnt, dentry };
T
Tetsuo Handa 已提交
175
	return tomoyo_path_perm(TOMOYO_TYPE_UNLINK, &path, NULL);
K
Kentaro Takeda 已提交
176 177
}

T
Tetsuo Handa 已提交
178 179 180 181 182 183 184 185 186
/**
 * tomoyo_path_mkdir - Target for security_path_mkdir().
 *
 * @parent: Pointer to "struct path".
 * @dentry: Pointer to "struct dentry".
 * @mode:   DAC permission mode.
 *
 * Returns 0 on success, negative value otherwise.
 */
K
Kentaro Takeda 已提交
187
static int tomoyo_path_mkdir(struct path *parent, struct dentry *dentry,
A
Al Viro 已提交
188
			     umode_t mode)
K
Kentaro Takeda 已提交
189 190
{
	struct path path = { parent->mnt, dentry };
191 192
	return tomoyo_path_number_perm(TOMOYO_TYPE_MKDIR, &path,
				       mode & S_IALLUGO);
K
Kentaro Takeda 已提交
193 194
}

T
Tetsuo Handa 已提交
195 196 197 198 199 200 201 202
/**
 * tomoyo_path_rmdir - Target for security_path_rmdir().
 *
 * @parent: Pointer to "struct path".
 * @dentry: Pointer to "struct dentry".
 *
 * Returns 0 on success, negative value otherwise.
 */
K
Kentaro Takeda 已提交
203 204 205
static int tomoyo_path_rmdir(struct path *parent, struct dentry *dentry)
{
	struct path path = { parent->mnt, dentry };
T
Tetsuo Handa 已提交
206
	return tomoyo_path_perm(TOMOYO_TYPE_RMDIR, &path, NULL);
K
Kentaro Takeda 已提交
207 208
}

T
Tetsuo Handa 已提交
209 210 211 212 213 214 215 216 217
/**
 * tomoyo_path_symlink - Target for security_path_symlink().
 *
 * @parent:   Pointer to "struct path".
 * @dentry:   Pointer to "struct dentry".
 * @old_name: Symlink's content.
 *
 * Returns 0 on success, negative value otherwise.
 */
K
Kentaro Takeda 已提交
218 219 220 221
static int tomoyo_path_symlink(struct path *parent, struct dentry *dentry,
			       const char *old_name)
{
	struct path path = { parent->mnt, dentry };
T
Tetsuo Handa 已提交
222
	return tomoyo_path_perm(TOMOYO_TYPE_SYMLINK, &path, old_name);
K
Kentaro Takeda 已提交
223 224
}

T
Tetsuo Handa 已提交
225 226 227 228 229 230 231 232 233 234
/**
 * tomoyo_path_mknod - Target for security_path_mknod().
 *
 * @parent: Pointer to "struct path".
 * @dentry: Pointer to "struct dentry".
 * @mode:   DAC permission mode.
 * @dev:    Device attributes.
 *
 * Returns 0 on success, negative value otherwise.
 */
K
Kentaro Takeda 已提交
235
static int tomoyo_path_mknod(struct path *parent, struct dentry *dentry,
A
Al Viro 已提交
236
			     umode_t mode, unsigned int dev)
K
Kentaro Takeda 已提交
237 238
{
	struct path path = { parent->mnt, dentry };
T
Tetsuo Handa 已提交
239
	int type = TOMOYO_TYPE_CREATE;
240
	const unsigned int perm = mode & S_IALLUGO;
K
Kentaro Takeda 已提交
241 242 243

	switch (mode & S_IFMT) {
	case S_IFCHR:
T
Tetsuo Handa 已提交
244
		type = TOMOYO_TYPE_MKCHAR;
K
Kentaro Takeda 已提交
245 246
		break;
	case S_IFBLK:
T
Tetsuo Handa 已提交
247
		type = TOMOYO_TYPE_MKBLOCK;
K
Kentaro Takeda 已提交
248
		break;
249 250 251
	default:
		goto no_dev;
	}
T
Tetsuo Handa 已提交
252
	return tomoyo_mkdev_perm(type, &path, perm, dev);
253 254
 no_dev:
	switch (mode & S_IFMT) {
K
Kentaro Takeda 已提交
255
	case S_IFIFO:
T
Tetsuo Handa 已提交
256
		type = TOMOYO_TYPE_MKFIFO;
K
Kentaro Takeda 已提交
257 258
		break;
	case S_IFSOCK:
T
Tetsuo Handa 已提交
259
		type = TOMOYO_TYPE_MKSOCK;
K
Kentaro Takeda 已提交
260 261
		break;
	}
262
	return tomoyo_path_number_perm(type, &path, perm);
K
Kentaro Takeda 已提交
263 264
}

T
Tetsuo Handa 已提交
265 266 267 268 269 270 271 272 273
/**
 * tomoyo_path_link - Target for security_path_link().
 *
 * @old_dentry: Pointer to "struct dentry".
 * @new_dir:    Pointer to "struct path".
 * @new_dentry: Pointer to "struct dentry".
 *
 * Returns 0 on success, negative value otherwise.
 */
K
Kentaro Takeda 已提交
274 275 276 277 278
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 };
279
	return tomoyo_path2_perm(TOMOYO_TYPE_LINK, &path1, &path2);
K
Kentaro Takeda 已提交
280 281
}

T
Tetsuo Handa 已提交
282 283 284 285 286 287 288 289 290 291
/**
 * tomoyo_path_rename - Target for security_path_rename().
 *
 * @old_parent: Pointer to "struct path".
 * @old_dentry: Pointer to "struct dentry".
 * @new_parent: Pointer to "struct path".
 * @new_dentry: Pointer to "struct dentry".
 *
 * Returns 0 on success, negative value otherwise.
 */
K
Kentaro Takeda 已提交
292 293 294 295 296 297 298
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 };
299
	return tomoyo_path2_perm(TOMOYO_TYPE_RENAME, &path1, &path2);
K
Kentaro Takeda 已提交
300 301
}

T
Tetsuo Handa 已提交
302 303 304 305 306 307 308 309 310
/**
 * tomoyo_file_fcntl - Target for security_file_fcntl().
 *
 * @file: Pointer to "struct file".
 * @cmd:  Command for fcntl().
 * @arg:  Argument for @cmd.
 *
 * Returns 0 on success, negative value otherwise.
 */
K
Kentaro Takeda 已提交
311 312 313
static int tomoyo_file_fcntl(struct file *file, unsigned int cmd,
			     unsigned long arg)
{
T
Tetsuo Handa 已提交
314 315 316 317
	if (!(cmd == F_SETFL && ((arg ^ file->f_flags) & O_APPEND)))
		return 0;
	return tomoyo_check_open_permission(tomoyo_domain(), &file->f_path,
					    O_WRONLY | (arg & O_APPEND));
K
Kentaro Takeda 已提交
318 319
}

T
Tetsuo Handa 已提交
320
/**
321
 * tomoyo_file_open - Target for security_file_open().
T
Tetsuo Handa 已提交
322 323 324 325 326 327
 *
 * @f:    Pointer to "struct file".
 * @cred: Pointer to "struct cred".
 *
 * Returns 0 on success, negative value otherwise.
 */
328
static int tomoyo_file_open(struct file *f, const struct cred *cred)
K
Kentaro Takeda 已提交
329 330 331 332 333 334 335 336
{
	int flags = f->f_flags;
	/* 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);
}

T
Tetsuo Handa 已提交
337 338 339 340 341 342 343 344 345
/**
 * tomoyo_file_ioctl - Target for security_file_ioctl().
 *
 * @file: Pointer to "struct file".
 * @cmd:  Command for ioctl().
 * @arg:  Argument for @cmd.
 *
 * Returns 0 on success, negative value otherwise.
 */
346 347 348
static int tomoyo_file_ioctl(struct file *file, unsigned int cmd,
			     unsigned long arg)
{
349
	return tomoyo_path_number_perm(TOMOYO_TYPE_IOCTL, &file->f_path, cmd);
350 351
}

T
Tetsuo Handa 已提交
352 353 354
/**
 * tomoyo_path_chmod - Target for security_path_chmod().
 *
355 356
 * @path: Pointer to "struct path".
 * @mode: DAC permission mode.
T
Tetsuo Handa 已提交
357 358 359
 *
 * Returns 0 on success, negative value otherwise.
 */
360
static int tomoyo_path_chmod(struct path *path, umode_t mode)
361
{
362
	return tomoyo_path_number_perm(TOMOYO_TYPE_CHMOD, path,
363
				       mode & S_IALLUGO);
364 365
}

T
Tetsuo Handa 已提交
366 367 368 369 370 371 372 373 374
/**
 * tomoyo_path_chown - Target for security_path_chown().
 *
 * @path: Pointer to "struct path".
 * @uid:  Owner ID.
 * @gid:  Group ID.
 *
 * Returns 0 on success, negative value otherwise.
 */
375
static int tomoyo_path_chown(struct path *path, kuid_t uid, kgid_t gid)
376 377
{
	int error = 0;
378 379 380 381 382 383
	if (uid_valid(uid))
		error = tomoyo_path_number_perm(TOMOYO_TYPE_CHOWN, path,
						from_kuid(&init_user_ns, uid));
	if (!error && gid_valid(gid))
		error = tomoyo_path_number_perm(TOMOYO_TYPE_CHGRP, path,
						from_kgid(&init_user_ns, gid));
384 385 386
	return error;
}

T
Tetsuo Handa 已提交
387 388 389 390 391 392 393
/**
 * tomoyo_path_chroot - Target for security_path_chroot().
 *
 * @path: Pointer to "struct path".
 *
 * Returns 0 on success, negative value otherwise.
 */
394 395
static int tomoyo_path_chroot(struct path *path)
{
T
Tetsuo Handa 已提交
396
	return tomoyo_path_perm(TOMOYO_TYPE_CHROOT, path, NULL);
397 398
}

T
Tetsuo Handa 已提交
399 400 401 402 403 404 405 406 407 408 409
/**
 * tomoyo_sb_mount - Target for security_sb_mount().
 *
 * @dev_name: Name of device file. Maybe NULL.
 * @path:     Pointer to "struct path".
 * @type:     Name of filesystem type. Maybe NULL.
 * @flags:    Mount options.
 * @data:     Optional data. Maybe NULL.
 *
 * Returns 0 on success, negative value otherwise.
 */
A
Al Viro 已提交
410 411
static int tomoyo_sb_mount(const char *dev_name, struct path *path,
			   const char *type, unsigned long flags, void *data)
412
{
T
Tetsuo Handa 已提交
413
	return tomoyo_mount_permission(dev_name, path, type, flags, data);
414 415
}

T
Tetsuo Handa 已提交
416 417 418 419 420 421 422 423
/**
 * tomoyo_sb_umount - Target for security_sb_umount().
 *
 * @mnt:   Pointer to "struct vfsmount".
 * @flags: Unmount options.
 *
 * Returns 0 on success, negative value otherwise.
 */
424 425 426
static int tomoyo_sb_umount(struct vfsmount *mnt, int flags)
{
	struct path path = { mnt, mnt->mnt_root };
T
Tetsuo Handa 已提交
427
	return tomoyo_path_perm(TOMOYO_TYPE_UMOUNT, &path, NULL);
428 429
}

T
Tetsuo Handa 已提交
430 431 432 433 434 435 436 437
/**
 * tomoyo_sb_pivotroot - Target for security_sb_pivotroot().
 *
 * @old_path: Pointer to "struct path".
 * @new_path: Pointer to "struct path".
 *
 * Returns 0 on success, negative value otherwise.
 */
438 439
static int tomoyo_sb_pivotroot(struct path *old_path, struct path *new_path)
{
440
	return tomoyo_path2_perm(TOMOYO_TYPE_PIVOT_ROOT, new_path, old_path);
441 442
}

443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
/**
 * tomoyo_socket_listen - Check permission for listen().
 *
 * @sock:    Pointer to "struct socket".
 * @backlog: Backlog parameter.
 *
 * Returns 0 on success, negative value otherwise.
 */
static int tomoyo_socket_listen(struct socket *sock, int backlog)
{
	return tomoyo_socket_listen_permission(sock);
}

/**
 * tomoyo_socket_connect - Check permission for connect().
 *
 * @sock:     Pointer to "struct socket".
 * @addr:     Pointer to "struct sockaddr".
 * @addr_len: Size of @addr.
 *
 * Returns 0 on success, negative value otherwise.
 */
static int tomoyo_socket_connect(struct socket *sock, struct sockaddr *addr,
				 int addr_len)
{
	return tomoyo_socket_connect_permission(sock, addr, addr_len);
}

/**
 * tomoyo_socket_bind - Check permission for bind().
 *
 * @sock:     Pointer to "struct socket".
 * @addr:     Pointer to "struct sockaddr".
 * @addr_len: Size of @addr.
 *
 * Returns 0 on success, negative value otherwise.
 */
static int tomoyo_socket_bind(struct socket *sock, struct sockaddr *addr,
			      int addr_len)
{
	return tomoyo_socket_bind_permission(sock, addr, addr_len);
}

/**
 * tomoyo_socket_sendmsg - Check permission for sendmsg().
 *
 * @sock: Pointer to "struct socket".
 * @msg:  Pointer to "struct msghdr".
 * @size: Size of message.
 *
 * Returns 0 on success, negative value otherwise.
 */
static int tomoyo_socket_sendmsg(struct socket *sock, struct msghdr *msg,
				 int size)
{
	return tomoyo_socket_sendmsg_permission(sock, msg, size);
}

501 502 503 504
/*
 * tomoyo_security_ops is a "struct security_operations" which is used for
 * registering TOMOYO.
 */
K
Kentaro Takeda 已提交
505 506
static struct security_operations tomoyo_security_ops = {
	.name                = "tomoyo",
507
	.cred_alloc_blank    = tomoyo_cred_alloc_blank,
K
Kentaro Takeda 已提交
508
	.cred_prepare        = tomoyo_cred_prepare,
509
	.cred_transfer	     = tomoyo_cred_transfer,
510
	.cred_free           = tomoyo_cred_free,
K
Kentaro Takeda 已提交
511 512 513
	.bprm_set_creds      = tomoyo_bprm_set_creds,
	.bprm_check_security = tomoyo_bprm_check_security,
	.file_fcntl          = tomoyo_file_fcntl,
514
	.file_open           = tomoyo_file_open,
K
Kentaro Takeda 已提交
515 516 517 518 519 520 521 522
	.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,
T
Tetsuo Handa 已提交
523
	.inode_getattr       = tomoyo_inode_getattr,
524 525 526 527 528 529 530
	.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,
531 532 533 534
	.socket_bind         = tomoyo_socket_bind,
	.socket_connect      = tomoyo_socket_connect,
	.socket_listen       = tomoyo_socket_listen,
	.socket_sendmsg      = tomoyo_socket_sendmsg,
K
Kentaro Takeda 已提交
535 536
};

537
/* Lock for GC. */
538
DEFINE_SRCU(tomoyo_ss);
539

T
Tetsuo Handa 已提交
540 541 542 543 544
/**
 * tomoyo_init - Register TOMOYO Linux as a LSM module.
 *
 * Returns 0.
 */
K
Kentaro Takeda 已提交
545 546 547 548 549 550 551
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 */
552
	if (register_security(&tomoyo_security_ops))
K
Kentaro Takeda 已提交
553 554 555
		panic("Failure registering TOMOYO Linux");
	printk(KERN_INFO "TOMOYO Linux initialized\n");
	cred->security = &tomoyo_kernel_domain;
556
	tomoyo_mm_init();
K
Kentaro Takeda 已提交
557 558 559 560
	return 0;
}

security_initcall(tomoyo_init);