2 * security/tomoyo/tomoyo.c
4 * Copyright (C) 2005-2011 NTT DATA CORPORATION
7 #include <linux/security.h>
11 * tomoyo_cred_alloc_blank - Target for security_cred_alloc_blank().
13 * @new: Pointer to "struct cred".
14 * @gfp: Memory allocation flags.
18 static int tomoyo_cred_alloc_blank(struct cred *new, gfp_t gfp)
25 * tomoyo_cred_prepare - Target for security_prepare_creds().
27 * @new: Pointer to "struct cred".
28 * @old: Pointer to "struct cred".
29 * @gfp: Memory allocation flags.
33 static int tomoyo_cred_prepare(struct cred *new, const struct cred *old,
36 struct tomoyo_domain_info *domain = old->security;
37 new->security = domain;
39 atomic_inc(&domain->users);
44 * tomoyo_cred_transfer - Target for security_transfer_creds().
46 * @new: Pointer to "struct cred".
47 * @old: Pointer to "struct cred".
49 static void tomoyo_cred_transfer(struct cred *new, const struct cred *old)
51 tomoyo_cred_prepare(new, old, 0);
55 * tomoyo_cred_free - Target for security_cred_free().
57 * @cred: Pointer to "struct cred".
59 static void tomoyo_cred_free(struct cred *cred)
61 struct tomoyo_domain_info *domain = cred->security;
63 atomic_dec(&domain->users);
67 * tomoyo_bprm_set_creds - Target for security_bprm_set_creds().
69 * @bprm: Pointer to "struct linux_binprm".
71 * Returns 0 on success, negative value otherwise.
73 static int tomoyo_bprm_set_creds(struct linux_binprm *bprm)
77 rc = cap_bprm_set_creds(bprm);
82 * Do only if this function is called for the first time of an execve
85 if (bprm->cred_prepared)
87 #ifndef CONFIG_SECURITY_TOMOYO_OMIT_USERSPACE_LOADER
89 * Load policy if /sbin/tomoyo-init exists and /sbin/init is requested
92 if (!tomoyo_policy_loaded)
93 tomoyo_load_policy(bprm->filename);
96 * Release reference to "struct tomoyo_domain_info" stored inside
97 * "bprm->cred->security". New reference to "struct tomoyo_domain_info"
98 * stored inside "bprm->cred->security" will be acquired later inside
99 * tomoyo_find_next_domain().
101 atomic_dec(&((struct tomoyo_domain_info *)
102 bprm->cred->security)->users);
104 * Tell tomoyo_bprm_check_security() is called for the first time of an
107 bprm->cred->security = NULL;
112 * tomoyo_bprm_check_security - Target for security_bprm_check().
114 * @bprm: Pointer to "struct linux_binprm".
116 * Returns 0 on success, negative value otherwise.
118 static int tomoyo_bprm_check_security(struct linux_binprm *bprm)
120 struct tomoyo_domain_info *domain = bprm->cred->security;
123 * Execute permission is checked against pathname passed to do_execve()
124 * using current domain.
127 const int idx = tomoyo_read_lock();
128 const int err = tomoyo_find_next_domain(bprm);
129 tomoyo_read_unlock(idx);
133 * Read permission is checked against interpreters using next domain.
135 return tomoyo_check_open_permission(domain, &bprm->file->f_path,
140 * tomoyo_inode_getattr - Target for security_inode_getattr().
142 * @mnt: Pointer to "struct vfsmount".
143 * @dentry: Pointer to "struct dentry".
145 * Returns 0 on success, negative value otherwise.
147 static int tomoyo_inode_getattr(const struct path *path)
149 return tomoyo_path_perm(TOMOYO_TYPE_GETATTR, path, NULL);
153 * tomoyo_path_truncate - Target for security_path_truncate().
155 * @path: Pointer to "struct path".
157 * Returns 0 on success, negative value otherwise.
159 static int tomoyo_path_truncate(struct path *path)
161 return tomoyo_path_perm(TOMOYO_TYPE_TRUNCATE, path, NULL);
165 * tomoyo_path_unlink - Target for security_path_unlink().
167 * @parent: Pointer to "struct path".
168 * @dentry: Pointer to "struct dentry".
170 * Returns 0 on success, negative value otherwise.
172 static int tomoyo_path_unlink(struct path *parent, struct dentry *dentry)
174 struct path path = { parent->mnt, dentry };
175 return tomoyo_path_perm(TOMOYO_TYPE_UNLINK, &path, NULL);
179 * tomoyo_path_mkdir - Target for security_path_mkdir().
181 * @parent: Pointer to "struct path".
182 * @dentry: Pointer to "struct dentry".
183 * @mode: DAC permission mode.
185 * Returns 0 on success, negative value otherwise.
187 static int tomoyo_path_mkdir(struct path *parent, struct dentry *dentry,
190 struct path path = { parent->mnt, dentry };
191 return tomoyo_path_number_perm(TOMOYO_TYPE_MKDIR, &path,
196 * tomoyo_path_rmdir - Target for security_path_rmdir().
198 * @parent: Pointer to "struct path".
199 * @dentry: Pointer to "struct dentry".
201 * Returns 0 on success, negative value otherwise.
203 static int tomoyo_path_rmdir(struct path *parent, struct dentry *dentry)
205 struct path path = { parent->mnt, dentry };
206 return tomoyo_path_perm(TOMOYO_TYPE_RMDIR, &path, NULL);
210 * tomoyo_path_symlink - Target for security_path_symlink().
212 * @parent: Pointer to "struct path".
213 * @dentry: Pointer to "struct dentry".
214 * @old_name: Symlink's content.
216 * Returns 0 on success, negative value otherwise.
218 static int tomoyo_path_symlink(struct path *parent, struct dentry *dentry,
219 const char *old_name)
221 struct path path = { parent->mnt, dentry };
222 return tomoyo_path_perm(TOMOYO_TYPE_SYMLINK, &path, old_name);
226 * tomoyo_path_mknod - Target for security_path_mknod().
228 * @parent: Pointer to "struct path".
229 * @dentry: Pointer to "struct dentry".
230 * @mode: DAC permission mode.
231 * @dev: Device attributes.
233 * Returns 0 on success, negative value otherwise.
235 static int tomoyo_path_mknod(struct path *parent, struct dentry *dentry,
236 umode_t mode, unsigned int dev)
238 struct path path = { parent->mnt, dentry };
239 int type = TOMOYO_TYPE_CREATE;
240 const unsigned int perm = mode & S_IALLUGO;
242 switch (mode & S_IFMT) {
244 type = TOMOYO_TYPE_MKCHAR;
247 type = TOMOYO_TYPE_MKBLOCK;
252 return tomoyo_mkdev_perm(type, &path, perm, dev);
254 switch (mode & S_IFMT) {
256 type = TOMOYO_TYPE_MKFIFO;
259 type = TOMOYO_TYPE_MKSOCK;
262 return tomoyo_path_number_perm(type, &path, perm);
266 * tomoyo_path_link - Target for security_path_link().
268 * @old_dentry: Pointer to "struct dentry".
269 * @new_dir: Pointer to "struct path".
270 * @new_dentry: Pointer to "struct dentry".
272 * Returns 0 on success, negative value otherwise.
274 static int tomoyo_path_link(struct dentry *old_dentry, struct path *new_dir,
275 struct dentry *new_dentry)
277 struct path path1 = { new_dir->mnt, old_dentry };
278 struct path path2 = { new_dir->mnt, new_dentry };
279 return tomoyo_path2_perm(TOMOYO_TYPE_LINK, &path1, &path2);
283 * tomoyo_path_rename - Target for security_path_rename().
285 * @old_parent: Pointer to "struct path".
286 * @old_dentry: Pointer to "struct dentry".
287 * @new_parent: Pointer to "struct path".
288 * @new_dentry: Pointer to "struct dentry".
290 * Returns 0 on success, negative value otherwise.
292 static int tomoyo_path_rename(struct path *old_parent,
293 struct dentry *old_dentry,
294 struct path *new_parent,
295 struct dentry *new_dentry)
297 struct path path1 = { old_parent->mnt, old_dentry };
298 struct path path2 = { new_parent->mnt, new_dentry };
299 return tomoyo_path2_perm(TOMOYO_TYPE_RENAME, &path1, &path2);
303 * tomoyo_file_fcntl - Target for security_file_fcntl().
305 * @file: Pointer to "struct file".
306 * @cmd: Command for fcntl().
307 * @arg: Argument for @cmd.
309 * Returns 0 on success, negative value otherwise.
311 static int tomoyo_file_fcntl(struct file *file, unsigned int cmd,
314 if (!(cmd == F_SETFL && ((arg ^ file->f_flags) & O_APPEND)))
316 return tomoyo_check_open_permission(tomoyo_domain(), &file->f_path,
317 O_WRONLY | (arg & O_APPEND));
321 * tomoyo_file_open - Target for security_file_open().
323 * @f: Pointer to "struct file".
324 * @cred: Pointer to "struct cred".
326 * Returns 0 on success, negative value otherwise.
328 static int tomoyo_file_open(struct file *f, const struct cred *cred)
330 int flags = f->f_flags;
331 /* Don't check read permission here if called from do_execve(). */
332 if (current->in_execve)
334 return tomoyo_check_open_permission(tomoyo_domain(), &f->f_path, flags);
338 * tomoyo_file_ioctl - Target for security_file_ioctl().
340 * @file: Pointer to "struct file".
341 * @cmd: Command for ioctl().
342 * @arg: Argument for @cmd.
344 * Returns 0 on success, negative value otherwise.
346 static int tomoyo_file_ioctl(struct file *file, unsigned int cmd,
349 return tomoyo_path_number_perm(TOMOYO_TYPE_IOCTL, &file->f_path, cmd);
353 * tomoyo_path_chmod - Target for security_path_chmod().
355 * @path: Pointer to "struct path".
356 * @mode: DAC permission mode.
358 * Returns 0 on success, negative value otherwise.
360 static int tomoyo_path_chmod(struct path *path, umode_t mode)
362 return tomoyo_path_number_perm(TOMOYO_TYPE_CHMOD, path,
367 * tomoyo_path_chown - Target for security_path_chown().
369 * @path: Pointer to "struct path".
373 * Returns 0 on success, negative value otherwise.
375 static int tomoyo_path_chown(struct path *path, kuid_t uid, kgid_t gid)
379 error = tomoyo_path_number_perm(TOMOYO_TYPE_CHOWN, path,
380 from_kuid(&init_user_ns, uid));
381 if (!error && gid_valid(gid))
382 error = tomoyo_path_number_perm(TOMOYO_TYPE_CHGRP, path,
383 from_kgid(&init_user_ns, gid));
388 * tomoyo_path_chroot - Target for security_path_chroot().
390 * @path: Pointer to "struct path".
392 * Returns 0 on success, negative value otherwise.
394 static int tomoyo_path_chroot(struct path *path)
396 return tomoyo_path_perm(TOMOYO_TYPE_CHROOT, path, NULL);
400 * tomoyo_sb_mount - Target for security_sb_mount().
402 * @dev_name: Name of device file. Maybe NULL.
403 * @path: Pointer to "struct path".
404 * @type: Name of filesystem type. Maybe NULL.
405 * @flags: Mount options.
406 * @data: Optional data. Maybe NULL.
408 * Returns 0 on success, negative value otherwise.
410 static int tomoyo_sb_mount(const char *dev_name, struct path *path,
411 const char *type, unsigned long flags, void *data)
413 return tomoyo_mount_permission(dev_name, path, type, flags, data);
417 * tomoyo_sb_umount - Target for security_sb_umount().
419 * @mnt: Pointer to "struct vfsmount".
420 * @flags: Unmount options.
422 * Returns 0 on success, negative value otherwise.
424 static int tomoyo_sb_umount(struct vfsmount *mnt, int flags)
426 struct path path = { mnt, mnt->mnt_root };
427 return tomoyo_path_perm(TOMOYO_TYPE_UMOUNT, &path, NULL);
431 * tomoyo_sb_pivotroot - Target for security_sb_pivotroot().
433 * @old_path: Pointer to "struct path".
434 * @new_path: Pointer to "struct path".
436 * Returns 0 on success, negative value otherwise.
438 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);
444 * tomoyo_socket_listen - Check permission for listen().
446 * @sock: Pointer to "struct socket".
447 * @backlog: Backlog parameter.
449 * Returns 0 on success, negative value otherwise.
451 static int tomoyo_socket_listen(struct socket *sock, int backlog)
453 return tomoyo_socket_listen_permission(sock);
457 * tomoyo_socket_connect - Check permission for connect().
459 * @sock: Pointer to "struct socket".
460 * @addr: Pointer to "struct sockaddr".
461 * @addr_len: Size of @addr.
463 * Returns 0 on success, negative value otherwise.
465 static int tomoyo_socket_connect(struct socket *sock, struct sockaddr *addr,
468 return tomoyo_socket_connect_permission(sock, addr, addr_len);
472 * tomoyo_socket_bind - Check permission for bind().
474 * @sock: Pointer to "struct socket".
475 * @addr: Pointer to "struct sockaddr".
476 * @addr_len: Size of @addr.
478 * Returns 0 on success, negative value otherwise.
480 static int tomoyo_socket_bind(struct socket *sock, struct sockaddr *addr,
483 return tomoyo_socket_bind_permission(sock, addr, addr_len);
487 * tomoyo_socket_sendmsg - Check permission for sendmsg().
489 * @sock: Pointer to "struct socket".
490 * @msg: Pointer to "struct msghdr".
491 * @size: Size of message.
493 * Returns 0 on success, negative value otherwise.
495 static int tomoyo_socket_sendmsg(struct socket *sock, struct msghdr *msg,
498 return tomoyo_socket_sendmsg_permission(sock, msg, size);
502 * tomoyo_security_ops is a "struct security_operations" which is used for
503 * registering TOMOYO.
505 static struct security_operations tomoyo_security_ops = {
507 .cred_alloc_blank = tomoyo_cred_alloc_blank,
508 .cred_prepare = tomoyo_cred_prepare,
509 .cred_transfer = tomoyo_cred_transfer,
510 .cred_free = tomoyo_cred_free,
511 .bprm_set_creds = tomoyo_bprm_set_creds,
512 .bprm_check_security = tomoyo_bprm_check_security,
513 .file_fcntl = tomoyo_file_fcntl,
514 .file_open = tomoyo_file_open,
515 .path_truncate = tomoyo_path_truncate,
516 .path_unlink = tomoyo_path_unlink,
517 .path_mkdir = tomoyo_path_mkdir,
518 .path_rmdir = tomoyo_path_rmdir,
519 .path_symlink = tomoyo_path_symlink,
520 .path_mknod = tomoyo_path_mknod,
521 .path_link = tomoyo_path_link,
522 .path_rename = tomoyo_path_rename,
523 .inode_getattr = tomoyo_inode_getattr,
524 .file_ioctl = tomoyo_file_ioctl,
525 .path_chmod = tomoyo_path_chmod,
526 .path_chown = tomoyo_path_chown,
527 .path_chroot = tomoyo_path_chroot,
528 .sb_mount = tomoyo_sb_mount,
529 .sb_umount = tomoyo_sb_umount,
530 .sb_pivotroot = tomoyo_sb_pivotroot,
531 .socket_bind = tomoyo_socket_bind,
532 .socket_connect = tomoyo_socket_connect,
533 .socket_listen = tomoyo_socket_listen,
534 .socket_sendmsg = tomoyo_socket_sendmsg,
538 DEFINE_SRCU(tomoyo_ss);
541 * tomoyo_init - Register TOMOYO Linux as a LSM module.
545 static int __init tomoyo_init(void)
547 struct cred *cred = (struct cred *) current_cred();
549 if (!security_module_enable(&tomoyo_security_ops))
551 /* register ourselves with the security framework */
552 if (register_security(&tomoyo_security_ops))
553 panic("Failure registering TOMOYO Linux");
554 printk(KERN_INFO "TOMOYO Linux initialized\n");
555 cred->security = &tomoyo_kernel_domain;
560 security_initcall(tomoyo_init);