Merge tag 'regmap-fix-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux/fpc-iii.git] / security / tomoyo / tomoyo.c
blob1f3cd432d830875d47e30d83d96ba4f2cd07b3af
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * security/tomoyo/tomoyo.c
5 * Copyright (C) 2005-2011 NTT DATA CORPORATION
6 */
8 #include <linux/lsm_hooks.h>
9 #include "common.h"
11 /**
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;
27 /**
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.
34 * Returns 0.
36 static int tomoyo_cred_prepare(struct cred *new, const struct cred *old,
37 gfp_t gfp)
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;
47 return 0;
50 /**
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
65 /**
66 * tomoyo_bprm_for_exec - Target for security_bprm_creds_for_exec().
68 * @bprm: Pointer to "struct linux_binprm".
70 * Returns 0.
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
76 * for the first time.
78 if (!tomoyo_policy_loaded)
79 tomoyo_load_policy(bprm->filename);
80 return 0;
82 #endif
84 /**
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);
104 return err;
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 * @mnt: Pointer to "struct vfsmount".
117 * @dentry: Pointer to "struct dentry".
119 * Returns 0 on success, negative value otherwise.
121 static int tomoyo_inode_getattr(const struct path *path)
123 return tomoyo_path_perm(TOMOYO_TYPE_GETATTR, path, NULL);
127 * tomoyo_path_truncate - Target for security_path_truncate().
129 * @path: Pointer to "struct path".
131 * Returns 0 on success, negative value otherwise.
133 static int tomoyo_path_truncate(const struct path *path)
135 return tomoyo_path_perm(TOMOYO_TYPE_TRUNCATE, path, NULL);
139 * tomoyo_path_unlink - Target for security_path_unlink().
141 * @parent: Pointer to "struct path".
142 * @dentry: Pointer to "struct dentry".
144 * Returns 0 on success, negative value otherwise.
146 static int tomoyo_path_unlink(const struct path *parent, struct dentry *dentry)
148 struct path path = { .mnt = parent->mnt, .dentry = dentry };
150 return tomoyo_path_perm(TOMOYO_TYPE_UNLINK, &path, NULL);
154 * tomoyo_path_mkdir - Target for security_path_mkdir().
156 * @parent: Pointer to "struct path".
157 * @dentry: Pointer to "struct dentry".
158 * @mode: DAC permission mode.
160 * Returns 0 on success, negative value otherwise.
162 static int tomoyo_path_mkdir(const struct path *parent, struct dentry *dentry,
163 umode_t mode)
165 struct path path = { .mnt = parent->mnt, .dentry = dentry };
167 return tomoyo_path_number_perm(TOMOYO_TYPE_MKDIR, &path,
168 mode & S_IALLUGO);
172 * tomoyo_path_rmdir - Target for security_path_rmdir().
174 * @parent: Pointer to "struct path".
175 * @dentry: Pointer to "struct dentry".
177 * Returns 0 on success, negative value otherwise.
179 static int tomoyo_path_rmdir(const struct path *parent, struct dentry *dentry)
181 struct path path = { .mnt = parent->mnt, .dentry = dentry };
183 return tomoyo_path_perm(TOMOYO_TYPE_RMDIR, &path, NULL);
187 * tomoyo_path_symlink - Target for security_path_symlink().
189 * @parent: Pointer to "struct path".
190 * @dentry: Pointer to "struct dentry".
191 * @old_name: Symlink's content.
193 * Returns 0 on success, negative value otherwise.
195 static int tomoyo_path_symlink(const struct path *parent, struct dentry *dentry,
196 const char *old_name)
198 struct path path = { .mnt = parent->mnt, .dentry = dentry };
200 return tomoyo_path_perm(TOMOYO_TYPE_SYMLINK, &path, old_name);
204 * tomoyo_path_mknod - Target for security_path_mknod().
206 * @parent: Pointer to "struct path".
207 * @dentry: Pointer to "struct dentry".
208 * @mode: DAC permission mode.
209 * @dev: Device attributes.
211 * Returns 0 on success, negative value otherwise.
213 static int tomoyo_path_mknod(const struct path *parent, struct dentry *dentry,
214 umode_t mode, unsigned int dev)
216 struct path path = { .mnt = parent->mnt, .dentry = dentry };
217 int type = TOMOYO_TYPE_CREATE;
218 const unsigned int perm = mode & S_IALLUGO;
220 switch (mode & S_IFMT) {
221 case S_IFCHR:
222 type = TOMOYO_TYPE_MKCHAR;
223 break;
224 case S_IFBLK:
225 type = TOMOYO_TYPE_MKBLOCK;
226 break;
227 default:
228 goto no_dev;
230 return tomoyo_mkdev_perm(type, &path, perm, dev);
231 no_dev:
232 switch (mode & S_IFMT) {
233 case S_IFIFO:
234 type = TOMOYO_TYPE_MKFIFO;
235 break;
236 case S_IFSOCK:
237 type = TOMOYO_TYPE_MKSOCK;
238 break;
240 return tomoyo_path_number_perm(type, &path, perm);
244 * tomoyo_path_link - Target for security_path_link().
246 * @old_dentry: Pointer to "struct dentry".
247 * @new_dir: Pointer to "struct path".
248 * @new_dentry: Pointer to "struct dentry".
250 * Returns 0 on success, negative value otherwise.
252 static int tomoyo_path_link(struct dentry *old_dentry, const struct path *new_dir,
253 struct dentry *new_dentry)
255 struct path path1 = { .mnt = new_dir->mnt, .dentry = old_dentry };
256 struct path path2 = { .mnt = new_dir->mnt, .dentry = new_dentry };
258 return tomoyo_path2_perm(TOMOYO_TYPE_LINK, &path1, &path2);
262 * tomoyo_path_rename - Target for security_path_rename().
264 * @old_parent: Pointer to "struct path".
265 * @old_dentry: Pointer to "struct dentry".
266 * @new_parent: Pointer to "struct path".
267 * @new_dentry: Pointer to "struct dentry".
269 * Returns 0 on success, negative value otherwise.
271 static int tomoyo_path_rename(const struct path *old_parent,
272 struct dentry *old_dentry,
273 const struct path *new_parent,
274 struct dentry *new_dentry)
276 struct path path1 = { .mnt = old_parent->mnt, .dentry = old_dentry };
277 struct path path2 = { .mnt = new_parent->mnt, .dentry = new_dentry };
279 return tomoyo_path2_perm(TOMOYO_TYPE_RENAME, &path1, &path2);
283 * tomoyo_file_fcntl - Target for security_file_fcntl().
285 * @file: Pointer to "struct file".
286 * @cmd: Command for fcntl().
287 * @arg: Argument for @cmd.
289 * Returns 0 on success, negative value otherwise.
291 static int tomoyo_file_fcntl(struct file *file, unsigned int cmd,
292 unsigned long arg)
294 if (!(cmd == F_SETFL && ((arg ^ file->f_flags) & O_APPEND)))
295 return 0;
296 return tomoyo_check_open_permission(tomoyo_domain(), &file->f_path,
297 O_WRONLY | (arg & O_APPEND));
301 * tomoyo_file_open - Target for security_file_open().
303 * @f: Pointer to "struct file".
304 * @cred: Pointer to "struct cred".
306 * Returns 0 on success, negative value otherwise.
308 static int tomoyo_file_open(struct file *f)
310 /* Don't check read permission here if called from execve(). */
311 if (current->in_execve)
312 return 0;
313 return tomoyo_check_open_permission(tomoyo_domain(), &f->f_path,
314 f->f_flags);
318 * tomoyo_file_ioctl - Target for security_file_ioctl().
320 * @file: Pointer to "struct file".
321 * @cmd: Command for ioctl().
322 * @arg: Argument for @cmd.
324 * Returns 0 on success, negative value otherwise.
326 static int tomoyo_file_ioctl(struct file *file, unsigned int cmd,
327 unsigned long arg)
329 return tomoyo_path_number_perm(TOMOYO_TYPE_IOCTL, &file->f_path, cmd);
333 * tomoyo_path_chmod - Target for security_path_chmod().
335 * @path: Pointer to "struct path".
336 * @mode: DAC permission mode.
338 * Returns 0 on success, negative value otherwise.
340 static int tomoyo_path_chmod(const struct path *path, umode_t mode)
342 return tomoyo_path_number_perm(TOMOYO_TYPE_CHMOD, path,
343 mode & S_IALLUGO);
347 * tomoyo_path_chown - Target for security_path_chown().
349 * @path: Pointer to "struct path".
350 * @uid: Owner ID.
351 * @gid: Group ID.
353 * Returns 0 on success, negative value otherwise.
355 static int tomoyo_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
357 int error = 0;
359 if (uid_valid(uid))
360 error = tomoyo_path_number_perm(TOMOYO_TYPE_CHOWN, path,
361 from_kuid(&init_user_ns, uid));
362 if (!error && gid_valid(gid))
363 error = tomoyo_path_number_perm(TOMOYO_TYPE_CHGRP, path,
364 from_kgid(&init_user_ns, gid));
365 return error;
369 * tomoyo_path_chroot - Target for security_path_chroot().
371 * @path: Pointer to "struct path".
373 * Returns 0 on success, negative value otherwise.
375 static int tomoyo_path_chroot(const struct path *path)
377 return tomoyo_path_perm(TOMOYO_TYPE_CHROOT, path, NULL);
381 * tomoyo_sb_mount - Target for security_sb_mount().
383 * @dev_name: Name of device file. Maybe NULL.
384 * @path: Pointer to "struct path".
385 * @type: Name of filesystem type. Maybe NULL.
386 * @flags: Mount options.
387 * @data: Optional data. Maybe NULL.
389 * Returns 0 on success, negative value otherwise.
391 static int tomoyo_sb_mount(const char *dev_name, const struct path *path,
392 const char *type, unsigned long flags, void *data)
394 return tomoyo_mount_permission(dev_name, path, type, flags, data);
398 * tomoyo_sb_umount - Target for security_sb_umount().
400 * @mnt: Pointer to "struct vfsmount".
401 * @flags: Unmount options.
403 * Returns 0 on success, negative value otherwise.
405 static int tomoyo_sb_umount(struct vfsmount *mnt, int flags)
407 struct path path = { .mnt = mnt, .dentry = mnt->mnt_root };
409 return tomoyo_path_perm(TOMOYO_TYPE_UMOUNT, &path, NULL);
413 * tomoyo_sb_pivotroot - Target for security_sb_pivotroot().
415 * @old_path: Pointer to "struct path".
416 * @new_path: Pointer to "struct path".
418 * Returns 0 on success, negative value otherwise.
420 static int tomoyo_sb_pivotroot(const struct path *old_path, const struct path *new_path)
422 return tomoyo_path2_perm(TOMOYO_TYPE_PIVOT_ROOT, new_path, old_path);
426 * tomoyo_socket_listen - Check permission for listen().
428 * @sock: Pointer to "struct socket".
429 * @backlog: Backlog parameter.
431 * Returns 0 on success, negative value otherwise.
433 static int tomoyo_socket_listen(struct socket *sock, int backlog)
435 return tomoyo_socket_listen_permission(sock);
439 * tomoyo_socket_connect - Check permission for connect().
441 * @sock: Pointer to "struct socket".
442 * @addr: Pointer to "struct sockaddr".
443 * @addr_len: Size of @addr.
445 * Returns 0 on success, negative value otherwise.
447 static int tomoyo_socket_connect(struct socket *sock, struct sockaddr *addr,
448 int addr_len)
450 return tomoyo_socket_connect_permission(sock, addr, addr_len);
454 * tomoyo_socket_bind - Check permission for bind().
456 * @sock: Pointer to "struct socket".
457 * @addr: Pointer to "struct sockaddr".
458 * @addr_len: Size of @addr.
460 * Returns 0 on success, negative value otherwise.
462 static int tomoyo_socket_bind(struct socket *sock, struct sockaddr *addr,
463 int addr_len)
465 return tomoyo_socket_bind_permission(sock, addr, addr_len);
469 * tomoyo_socket_sendmsg - Check permission for sendmsg().
471 * @sock: Pointer to "struct socket".
472 * @msg: Pointer to "struct msghdr".
473 * @size: Size of message.
475 * Returns 0 on success, negative value otherwise.
477 static int tomoyo_socket_sendmsg(struct socket *sock, struct msghdr *msg,
478 int size)
480 return tomoyo_socket_sendmsg_permission(sock, msg, size);
483 struct lsm_blob_sizes tomoyo_blob_sizes __lsm_ro_after_init = {
484 .lbs_task = sizeof(struct tomoyo_task),
488 * tomoyo_task_alloc - Target for security_task_alloc().
490 * @task: Pointer to "struct task_struct".
491 * @flags: clone() flags.
493 * Returns 0.
495 static int tomoyo_task_alloc(struct task_struct *task,
496 unsigned long clone_flags)
498 struct tomoyo_task *old = tomoyo_task(current);
499 struct tomoyo_task *new = tomoyo_task(task);
501 new->domain_info = old->domain_info;
502 atomic_inc(&new->domain_info->users);
503 new->old_domain_info = NULL;
504 return 0;
508 * tomoyo_task_free - Target for security_task_free().
510 * @task: Pointer to "struct task_struct".
512 static void tomoyo_task_free(struct task_struct *task)
514 struct tomoyo_task *s = tomoyo_task(task);
516 if (s->domain_info) {
517 atomic_dec(&s->domain_info->users);
518 s->domain_info = NULL;
520 if (s->old_domain_info) {
521 atomic_dec(&s->old_domain_info->users);
522 s->old_domain_info = NULL;
527 * tomoyo_security_ops is a "struct security_operations" which is used for
528 * registering TOMOYO.
530 static struct security_hook_list tomoyo_hooks[] __lsm_ro_after_init = {
531 LSM_HOOK_INIT(cred_prepare, tomoyo_cred_prepare),
532 LSM_HOOK_INIT(bprm_committed_creds, tomoyo_bprm_committed_creds),
533 LSM_HOOK_INIT(task_alloc, tomoyo_task_alloc),
534 LSM_HOOK_INIT(task_free, tomoyo_task_free),
535 #ifndef CONFIG_SECURITY_TOMOYO_OMIT_USERSPACE_LOADER
536 LSM_HOOK_INIT(bprm_creds_for_exec, tomoyo_bprm_creds_for_exec),
537 #endif
538 LSM_HOOK_INIT(bprm_check_security, tomoyo_bprm_check_security),
539 LSM_HOOK_INIT(file_fcntl, tomoyo_file_fcntl),
540 LSM_HOOK_INIT(file_open, tomoyo_file_open),
541 LSM_HOOK_INIT(path_truncate, tomoyo_path_truncate),
542 LSM_HOOK_INIT(path_unlink, tomoyo_path_unlink),
543 LSM_HOOK_INIT(path_mkdir, tomoyo_path_mkdir),
544 LSM_HOOK_INIT(path_rmdir, tomoyo_path_rmdir),
545 LSM_HOOK_INIT(path_symlink, tomoyo_path_symlink),
546 LSM_HOOK_INIT(path_mknod, tomoyo_path_mknod),
547 LSM_HOOK_INIT(path_link, tomoyo_path_link),
548 LSM_HOOK_INIT(path_rename, tomoyo_path_rename),
549 LSM_HOOK_INIT(inode_getattr, tomoyo_inode_getattr),
550 LSM_HOOK_INIT(file_ioctl, tomoyo_file_ioctl),
551 LSM_HOOK_INIT(path_chmod, tomoyo_path_chmod),
552 LSM_HOOK_INIT(path_chown, tomoyo_path_chown),
553 LSM_HOOK_INIT(path_chroot, tomoyo_path_chroot),
554 LSM_HOOK_INIT(sb_mount, tomoyo_sb_mount),
555 LSM_HOOK_INIT(sb_umount, tomoyo_sb_umount),
556 LSM_HOOK_INIT(sb_pivotroot, tomoyo_sb_pivotroot),
557 LSM_HOOK_INIT(socket_bind, tomoyo_socket_bind),
558 LSM_HOOK_INIT(socket_connect, tomoyo_socket_connect),
559 LSM_HOOK_INIT(socket_listen, tomoyo_socket_listen),
560 LSM_HOOK_INIT(socket_sendmsg, tomoyo_socket_sendmsg),
563 /* Lock for GC. */
564 DEFINE_SRCU(tomoyo_ss);
566 int tomoyo_enabled __lsm_ro_after_init = 1;
569 * tomoyo_init - Register TOMOYO Linux as a LSM module.
571 * Returns 0.
573 static int __init tomoyo_init(void)
575 struct tomoyo_task *s = tomoyo_task(current);
577 /* register ourselves with the security framework */
578 security_add_hooks(tomoyo_hooks, ARRAY_SIZE(tomoyo_hooks), "tomoyo");
579 pr_info("TOMOYO Linux initialized\n");
580 s->domain_info = &tomoyo_kernel_domain;
581 atomic_inc(&tomoyo_kernel_domain.users);
582 s->old_domain_info = NULL;
583 tomoyo_mm_init();
585 return 0;
588 DEFINE_LSM(tomoyo) = {
589 .name = "tomoyo",
590 .enabled = &tomoyo_enabled,
591 .flags = LSM_FLAG_LEGACY_MAJOR,
592 .blobs = &tomoyo_blob_sizes,
593 .init = tomoyo_init,