1 // SPDX-License-Identifier: GPL-2.0
3 * security/tomoyo/tomoyo.c
5 * Copyright (C) 2005-2011 NTT DATA CORPORATION
8 #include <linux/lsm_hooks.h>
12 * tomoyo_domain - Get "struct tomoyo_domain_info" for current thread.
14 * Returns pointer to "struct tomoyo_domain_info" for current thread.
16 struct tomoyo_domain_info *tomoyo_domain(void)
18 struct tomoyo_task *s = tomoyo_task(current);
20 if (s->old_domain_info && !current->in_execve) {
21 atomic_dec(&s->old_domain_info->users);
22 s->old_domain_info = NULL;
24 return s->domain_info;
28 * tomoyo_cred_prepare - Target for security_prepare_creds().
30 * @new: Pointer to "struct cred".
31 * @old: Pointer to "struct cred".
32 * @gfp: Memory allocation flags.
36 static int tomoyo_cred_prepare(struct cred *new, const struct cred *old,
39 /* Restore old_domain_info saved by previous execve() request. */
40 struct tomoyo_task *s = tomoyo_task(current);
42 if (s->old_domain_info && !current->in_execve) {
43 atomic_dec(&s->domain_info->users);
44 s->domain_info = s->old_domain_info;
45 s->old_domain_info = NULL;
51 * tomoyo_bprm_committed_creds - Target for security_bprm_committed_creds().
53 * @bprm: Pointer to "struct linux_binprm".
55 static void tomoyo_bprm_committed_creds(struct linux_binprm *bprm)
57 /* Clear old_domain_info saved by execve() request. */
58 struct tomoyo_task *s = tomoyo_task(current);
60 atomic_dec(&s->old_domain_info->users);
61 s->old_domain_info = NULL;
64 #ifndef CONFIG_SECURITY_TOMOYO_OMIT_USERSPACE_LOADER
66 * tomoyo_bprm_creds_for_exec - Target for security_bprm_creds_for_exec().
68 * @bprm: Pointer to "struct linux_binprm".
72 static int tomoyo_bprm_creds_for_exec(struct linux_binprm *bprm)
75 * Load policy if /sbin/tomoyo-init exists and /sbin/init is requested
78 if (!tomoyo_policy_loaded)
79 tomoyo_load_policy(bprm->filename);
85 * tomoyo_bprm_check_security - Target for security_bprm_check().
87 * @bprm: Pointer to "struct linux_binprm".
89 * Returns 0 on success, negative value otherwise.
91 static int tomoyo_bprm_check_security(struct linux_binprm *bprm)
93 struct tomoyo_task *s = tomoyo_task(current);
96 * Execute permission is checked against pathname passed to execve()
97 * using current domain.
99 if (!s->old_domain_info) {
100 const int idx = tomoyo_read_lock();
101 const int err = tomoyo_find_next_domain(bprm);
103 tomoyo_read_unlock(idx);
107 * Read permission is checked against interpreters using next domain.
109 return tomoyo_check_open_permission(s->domain_info,
110 &bprm->file->f_path, O_RDONLY);
114 * tomoyo_inode_getattr - Target for security_inode_getattr().
116 * @path: Pointer to "struct path".
118 * Returns 0 on success, negative value otherwise.
120 static int tomoyo_inode_getattr(const struct path *path)
122 return tomoyo_path_perm(TOMOYO_TYPE_GETATTR, path, NULL);
126 * tomoyo_path_truncate - Target for security_path_truncate().
128 * @path: Pointer to "struct path".
130 * Returns 0 on success, negative value otherwise.
132 static int tomoyo_path_truncate(const struct path *path)
134 return tomoyo_path_perm(TOMOYO_TYPE_TRUNCATE, path, NULL);
138 * tomoyo_file_truncate - Target for security_file_truncate().
140 * @file: Pointer to "struct file".
142 * Returns 0 on success, negative value otherwise.
144 static int tomoyo_file_truncate(struct file *file)
146 return tomoyo_path_truncate(&file->f_path);
150 * tomoyo_path_unlink - Target for security_path_unlink().
152 * @parent: Pointer to "struct path".
153 * @dentry: Pointer to "struct dentry".
155 * Returns 0 on success, negative value otherwise.
157 static int tomoyo_path_unlink(const struct path *parent, struct dentry *dentry)
159 struct path path = { .mnt = parent->mnt, .dentry = dentry };
161 return tomoyo_path_perm(TOMOYO_TYPE_UNLINK, &path, NULL);
165 * tomoyo_path_mkdir - Target for security_path_mkdir().
167 * @parent: Pointer to "struct path".
168 * @dentry: Pointer to "struct dentry".
169 * @mode: DAC permission mode.
171 * Returns 0 on success, negative value otherwise.
173 static int tomoyo_path_mkdir(const struct path *parent, struct dentry *dentry,
176 struct path path = { .mnt = parent->mnt, .dentry = dentry };
178 return tomoyo_path_number_perm(TOMOYO_TYPE_MKDIR, &path,
183 * tomoyo_path_rmdir - Target for security_path_rmdir().
185 * @parent: Pointer to "struct path".
186 * @dentry: Pointer to "struct dentry".
188 * Returns 0 on success, negative value otherwise.
190 static int tomoyo_path_rmdir(const struct path *parent, struct dentry *dentry)
192 struct path path = { .mnt = parent->mnt, .dentry = dentry };
194 return tomoyo_path_perm(TOMOYO_TYPE_RMDIR, &path, NULL);
198 * tomoyo_path_symlink - Target for security_path_symlink().
200 * @parent: Pointer to "struct path".
201 * @dentry: Pointer to "struct dentry".
202 * @old_name: Symlink's content.
204 * Returns 0 on success, negative value otherwise.
206 static int tomoyo_path_symlink(const struct path *parent, struct dentry *dentry,
207 const char *old_name)
209 struct path path = { .mnt = parent->mnt, .dentry = dentry };
211 return tomoyo_path_perm(TOMOYO_TYPE_SYMLINK, &path, old_name);
215 * tomoyo_path_mknod - Target for security_path_mknod().
217 * @parent: Pointer to "struct path".
218 * @dentry: Pointer to "struct dentry".
219 * @mode: DAC permission mode.
220 * @dev: Device attributes.
222 * Returns 0 on success, negative value otherwise.
224 static int tomoyo_path_mknod(const struct path *parent, struct dentry *dentry,
225 umode_t mode, unsigned int dev)
227 struct path path = { .mnt = parent->mnt, .dentry = dentry };
228 int type = TOMOYO_TYPE_CREATE;
229 const unsigned int perm = mode & S_IALLUGO;
231 switch (mode & S_IFMT) {
233 type = TOMOYO_TYPE_MKCHAR;
236 type = TOMOYO_TYPE_MKBLOCK;
241 return tomoyo_mkdev_perm(type, &path, perm, dev);
243 switch (mode & S_IFMT) {
245 type = TOMOYO_TYPE_MKFIFO;
248 type = TOMOYO_TYPE_MKSOCK;
251 return tomoyo_path_number_perm(type, &path, perm);
255 * tomoyo_path_link - Target for security_path_link().
257 * @old_dentry: Pointer to "struct dentry".
258 * @new_dir: Pointer to "struct path".
259 * @new_dentry: Pointer to "struct dentry".
261 * Returns 0 on success, negative value otherwise.
263 static int tomoyo_path_link(struct dentry *old_dentry, const struct path *new_dir,
264 struct dentry *new_dentry)
266 struct path path1 = { .mnt = new_dir->mnt, .dentry = old_dentry };
267 struct path path2 = { .mnt = new_dir->mnt, .dentry = new_dentry };
269 return tomoyo_path2_perm(TOMOYO_TYPE_LINK, &path1, &path2);
273 * tomoyo_path_rename - Target for security_path_rename().
275 * @old_parent: Pointer to "struct path".
276 * @old_dentry: Pointer to "struct dentry".
277 * @new_parent: Pointer to "struct path".
278 * @new_dentry: Pointer to "struct dentry".
279 * @flags: Rename options.
281 * Returns 0 on success, negative value otherwise.
283 static int tomoyo_path_rename(const struct path *old_parent,
284 struct dentry *old_dentry,
285 const struct path *new_parent,
286 struct dentry *new_dentry,
287 const unsigned int flags)
289 struct path path1 = { .mnt = old_parent->mnt, .dentry = old_dentry };
290 struct path path2 = { .mnt = new_parent->mnt, .dentry = new_dentry };
292 if (flags & RENAME_EXCHANGE) {
293 const int err = tomoyo_path2_perm(TOMOYO_TYPE_RENAME, &path2,
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".
325 * Returns 0 on success, negative value otherwise.
327 static int tomoyo_file_open(struct file *f)
329 /* Don't check read permission here if called from execve(). */
330 if (current->in_execve)
332 return tomoyo_check_open_permission(tomoyo_domain(), &f->f_path,
337 * tomoyo_file_ioctl - Target for security_file_ioctl().
339 * @file: Pointer to "struct file".
340 * @cmd: Command for ioctl().
341 * @arg: Argument for @cmd.
343 * Returns 0 on success, negative value otherwise.
345 static int tomoyo_file_ioctl(struct file *file, unsigned int cmd,
348 return tomoyo_path_number_perm(TOMOYO_TYPE_IOCTL, &file->f_path, cmd);
352 * tomoyo_path_chmod - Target for security_path_chmod().
354 * @path: Pointer to "struct path".
355 * @mode: DAC permission mode.
357 * Returns 0 on success, negative value otherwise.
359 static int tomoyo_path_chmod(const struct path *path, umode_t mode)
361 return tomoyo_path_number_perm(TOMOYO_TYPE_CHMOD, path,
366 * tomoyo_path_chown - Target for security_path_chown().
368 * @path: Pointer to "struct path".
372 * Returns 0 on success, negative value otherwise.
374 static int tomoyo_path_chown(const 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(const 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, const 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, .dentry = mnt->mnt_root };
428 return tomoyo_path_perm(TOMOYO_TYPE_UMOUNT, &path, NULL);
432 * tomoyo_sb_pivotroot - Target for security_sb_pivotroot().
434 * @old_path: Pointer to "struct path".
435 * @new_path: Pointer to "struct path".
437 * Returns 0 on success, negative value otherwise.
439 static int tomoyo_sb_pivotroot(const struct path *old_path, const struct path *new_path)
441 return tomoyo_path2_perm(TOMOYO_TYPE_PIVOT_ROOT, new_path, old_path);
445 * tomoyo_socket_listen - Check permission for listen().
447 * @sock: Pointer to "struct socket".
448 * @backlog: Backlog parameter.
450 * Returns 0 on success, negative value otherwise.
452 static int tomoyo_socket_listen(struct socket *sock, int backlog)
454 return tomoyo_socket_listen_permission(sock);
458 * tomoyo_socket_connect - Check permission for connect().
460 * @sock: Pointer to "struct socket".
461 * @addr: Pointer to "struct sockaddr".
462 * @addr_len: Size of @addr.
464 * Returns 0 on success, negative value otherwise.
466 static int tomoyo_socket_connect(struct socket *sock, struct sockaddr *addr,
469 return tomoyo_socket_connect_permission(sock, addr, addr_len);
473 * tomoyo_socket_bind - Check permission for bind().
475 * @sock: Pointer to "struct socket".
476 * @addr: Pointer to "struct sockaddr".
477 * @addr_len: Size of @addr.
479 * Returns 0 on success, negative value otherwise.
481 static int tomoyo_socket_bind(struct socket *sock, struct sockaddr *addr,
484 return tomoyo_socket_bind_permission(sock, addr, addr_len);
488 * tomoyo_socket_sendmsg - Check permission for sendmsg().
490 * @sock: Pointer to "struct socket".
491 * @msg: Pointer to "struct msghdr".
492 * @size: Size of message.
494 * Returns 0 on success, negative value otherwise.
496 static int tomoyo_socket_sendmsg(struct socket *sock, struct msghdr *msg,
499 return tomoyo_socket_sendmsg_permission(sock, msg, size);
502 struct lsm_blob_sizes tomoyo_blob_sizes __ro_after_init = {
503 .lbs_task = sizeof(struct tomoyo_task),
507 * tomoyo_task_alloc - Target for security_task_alloc().
509 * @task: Pointer to "struct task_struct".
510 * @clone_flags: clone() flags.
514 static int tomoyo_task_alloc(struct task_struct *task,
515 unsigned long clone_flags)
517 struct tomoyo_task *old = tomoyo_task(current);
518 struct tomoyo_task *new = tomoyo_task(task);
520 new->domain_info = old->domain_info;
521 atomic_inc(&new->domain_info->users);
522 new->old_domain_info = NULL;
527 * tomoyo_task_free - Target for security_task_free().
529 * @task: Pointer to "struct task_struct".
531 static void tomoyo_task_free(struct task_struct *task)
533 struct tomoyo_task *s = tomoyo_task(task);
535 if (s->domain_info) {
536 atomic_dec(&s->domain_info->users);
537 s->domain_info = NULL;
539 if (s->old_domain_info) {
540 atomic_dec(&s->old_domain_info->users);
541 s->old_domain_info = NULL;
546 * tomoyo_security_ops is a "struct security_operations" which is used for
547 * registering TOMOYO.
549 static struct security_hook_list tomoyo_hooks[] __ro_after_init = {
550 LSM_HOOK_INIT(cred_prepare, tomoyo_cred_prepare),
551 LSM_HOOK_INIT(bprm_committed_creds, tomoyo_bprm_committed_creds),
552 LSM_HOOK_INIT(task_alloc, tomoyo_task_alloc),
553 LSM_HOOK_INIT(task_free, tomoyo_task_free),
554 #ifndef CONFIG_SECURITY_TOMOYO_OMIT_USERSPACE_LOADER
555 LSM_HOOK_INIT(bprm_creds_for_exec, tomoyo_bprm_creds_for_exec),
557 LSM_HOOK_INIT(bprm_check_security, tomoyo_bprm_check_security),
558 LSM_HOOK_INIT(file_fcntl, tomoyo_file_fcntl),
559 LSM_HOOK_INIT(file_open, tomoyo_file_open),
560 LSM_HOOK_INIT(file_truncate, tomoyo_file_truncate),
561 LSM_HOOK_INIT(path_truncate, tomoyo_path_truncate),
562 LSM_HOOK_INIT(path_unlink, tomoyo_path_unlink),
563 LSM_HOOK_INIT(path_mkdir, tomoyo_path_mkdir),
564 LSM_HOOK_INIT(path_rmdir, tomoyo_path_rmdir),
565 LSM_HOOK_INIT(path_symlink, tomoyo_path_symlink),
566 LSM_HOOK_INIT(path_mknod, tomoyo_path_mknod),
567 LSM_HOOK_INIT(path_link, tomoyo_path_link),
568 LSM_HOOK_INIT(path_rename, tomoyo_path_rename),
569 LSM_HOOK_INIT(inode_getattr, tomoyo_inode_getattr),
570 LSM_HOOK_INIT(file_ioctl, tomoyo_file_ioctl),
571 LSM_HOOK_INIT(path_chmod, tomoyo_path_chmod),
572 LSM_HOOK_INIT(path_chown, tomoyo_path_chown),
573 LSM_HOOK_INIT(path_chroot, tomoyo_path_chroot),
574 LSM_HOOK_INIT(sb_mount, tomoyo_sb_mount),
575 LSM_HOOK_INIT(sb_umount, tomoyo_sb_umount),
576 LSM_HOOK_INIT(sb_pivotroot, tomoyo_sb_pivotroot),
577 LSM_HOOK_INIT(socket_bind, tomoyo_socket_bind),
578 LSM_HOOK_INIT(socket_connect, tomoyo_socket_connect),
579 LSM_HOOK_INIT(socket_listen, tomoyo_socket_listen),
580 LSM_HOOK_INIT(socket_sendmsg, tomoyo_socket_sendmsg),
584 DEFINE_SRCU(tomoyo_ss);
586 int tomoyo_enabled __ro_after_init = 1;
589 * tomoyo_init - Register TOMOYO Linux as a LSM module.
593 static int __init tomoyo_init(void)
595 struct tomoyo_task *s = tomoyo_task(current);
597 /* register ourselves with the security framework */
598 security_add_hooks(tomoyo_hooks, ARRAY_SIZE(tomoyo_hooks), "tomoyo");
599 pr_info("TOMOYO Linux initialized\n");
600 s->domain_info = &tomoyo_kernel_domain;
601 atomic_inc(&tomoyo_kernel_domain.users);
602 s->old_domain_info = NULL;
608 DEFINE_LSM(tomoyo) = {
610 .enabled = &tomoyo_enabled,
611 .flags = LSM_FLAG_LEGACY_MAJOR,
612 .blobs = &tomoyo_blob_sizes,