drm/panfrost: Remove set but not used variable 'bo'
[linux/fpc-iii.git] / security / tomoyo / tomoyo.c
blob716c92ec941adc595c1887995afd897a77d8ab26
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_set_creds - Target for security_bprm_set_creds().
68 * @bprm: Pointer to "struct linux_binprm".
70 * Returns 0.
72 static int tomoyo_bprm_set_creds(struct linux_binprm *bprm)
75 * Do only if this function is called for the first time of an execve
76 * operation.
78 if (bprm->called_set_creds)
79 return 0;
81 * Load policy if /sbin/tomoyo-init exists and /sbin/init is requested
82 * for the first time.
84 if (!tomoyo_policy_loaded)
85 tomoyo_load_policy(bprm->filename);
86 return 0;
88 #endif
90 /**
91 * tomoyo_bprm_check_security - Target for security_bprm_check().
93 * @bprm: Pointer to "struct linux_binprm".
95 * Returns 0 on success, negative value otherwise.
97 static int tomoyo_bprm_check_security(struct linux_binprm *bprm)
99 struct tomoyo_task *s = tomoyo_task(current);
102 * Execute permission is checked against pathname passed to do_execve()
103 * using current domain.
105 if (!s->old_domain_info) {
106 const int idx = tomoyo_read_lock();
107 const int err = tomoyo_find_next_domain(bprm);
109 tomoyo_read_unlock(idx);
110 return err;
113 * Read permission is checked against interpreters using next domain.
115 return tomoyo_check_open_permission(s->domain_info,
116 &bprm->file->f_path, O_RDONLY);
120 * tomoyo_inode_getattr - Target for security_inode_getattr().
122 * @mnt: Pointer to "struct vfsmount".
123 * @dentry: Pointer to "struct dentry".
125 * Returns 0 on success, negative value otherwise.
127 static int tomoyo_inode_getattr(const struct path *path)
129 return tomoyo_path_perm(TOMOYO_TYPE_GETATTR, path, NULL);
133 * tomoyo_path_truncate - Target for security_path_truncate().
135 * @path: Pointer to "struct path".
137 * Returns 0 on success, negative value otherwise.
139 static int tomoyo_path_truncate(const struct path *path)
141 return tomoyo_path_perm(TOMOYO_TYPE_TRUNCATE, path, NULL);
145 * tomoyo_path_unlink - Target for security_path_unlink().
147 * @parent: Pointer to "struct path".
148 * @dentry: Pointer to "struct dentry".
150 * Returns 0 on success, negative value otherwise.
152 static int tomoyo_path_unlink(const struct path *parent, struct dentry *dentry)
154 struct path path = { .mnt = parent->mnt, .dentry = dentry };
156 return tomoyo_path_perm(TOMOYO_TYPE_UNLINK, &path, NULL);
160 * tomoyo_path_mkdir - Target for security_path_mkdir().
162 * @parent: Pointer to "struct path".
163 * @dentry: Pointer to "struct dentry".
164 * @mode: DAC permission mode.
166 * Returns 0 on success, negative value otherwise.
168 static int tomoyo_path_mkdir(const struct path *parent, struct dentry *dentry,
169 umode_t mode)
171 struct path path = { .mnt = parent->mnt, .dentry = dentry };
173 return tomoyo_path_number_perm(TOMOYO_TYPE_MKDIR, &path,
174 mode & S_IALLUGO);
178 * tomoyo_path_rmdir - Target for security_path_rmdir().
180 * @parent: Pointer to "struct path".
181 * @dentry: Pointer to "struct dentry".
183 * Returns 0 on success, negative value otherwise.
185 static int tomoyo_path_rmdir(const struct path *parent, struct dentry *dentry)
187 struct path path = { .mnt = parent->mnt, .dentry = dentry };
189 return tomoyo_path_perm(TOMOYO_TYPE_RMDIR, &path, NULL);
193 * tomoyo_path_symlink - Target for security_path_symlink().
195 * @parent: Pointer to "struct path".
196 * @dentry: Pointer to "struct dentry".
197 * @old_name: Symlink's content.
199 * Returns 0 on success, negative value otherwise.
201 static int tomoyo_path_symlink(const struct path *parent, struct dentry *dentry,
202 const char *old_name)
204 struct path path = { .mnt = parent->mnt, .dentry = dentry };
206 return tomoyo_path_perm(TOMOYO_TYPE_SYMLINK, &path, old_name);
210 * tomoyo_path_mknod - Target for security_path_mknod().
212 * @parent: Pointer to "struct path".
213 * @dentry: Pointer to "struct dentry".
214 * @mode: DAC permission mode.
215 * @dev: Device attributes.
217 * Returns 0 on success, negative value otherwise.
219 static int tomoyo_path_mknod(const struct path *parent, struct dentry *dentry,
220 umode_t mode, unsigned int dev)
222 struct path path = { .mnt = parent->mnt, .dentry = dentry };
223 int type = TOMOYO_TYPE_CREATE;
224 const unsigned int perm = mode & S_IALLUGO;
226 switch (mode & S_IFMT) {
227 case S_IFCHR:
228 type = TOMOYO_TYPE_MKCHAR;
229 break;
230 case S_IFBLK:
231 type = TOMOYO_TYPE_MKBLOCK;
232 break;
233 default:
234 goto no_dev;
236 return tomoyo_mkdev_perm(type, &path, perm, dev);
237 no_dev:
238 switch (mode & S_IFMT) {
239 case S_IFIFO:
240 type = TOMOYO_TYPE_MKFIFO;
241 break;
242 case S_IFSOCK:
243 type = TOMOYO_TYPE_MKSOCK;
244 break;
246 return tomoyo_path_number_perm(type, &path, perm);
250 * tomoyo_path_link - Target for security_path_link().
252 * @old_dentry: Pointer to "struct dentry".
253 * @new_dir: Pointer to "struct path".
254 * @new_dentry: Pointer to "struct dentry".
256 * Returns 0 on success, negative value otherwise.
258 static int tomoyo_path_link(struct dentry *old_dentry, const struct path *new_dir,
259 struct dentry *new_dentry)
261 struct path path1 = { .mnt = new_dir->mnt, .dentry = old_dentry };
262 struct path path2 = { .mnt = new_dir->mnt, .dentry = new_dentry };
264 return tomoyo_path2_perm(TOMOYO_TYPE_LINK, &path1, &path2);
268 * tomoyo_path_rename - Target for security_path_rename().
270 * @old_parent: Pointer to "struct path".
271 * @old_dentry: Pointer to "struct dentry".
272 * @new_parent: Pointer to "struct path".
273 * @new_dentry: Pointer to "struct dentry".
275 * Returns 0 on success, negative value otherwise.
277 static int tomoyo_path_rename(const struct path *old_parent,
278 struct dentry *old_dentry,
279 const struct path *new_parent,
280 struct dentry *new_dentry)
282 struct path path1 = { .mnt = old_parent->mnt, .dentry = old_dentry };
283 struct path path2 = { .mnt = new_parent->mnt, .dentry = new_dentry };
285 return tomoyo_path2_perm(TOMOYO_TYPE_RENAME, &path1, &path2);
289 * tomoyo_file_fcntl - Target for security_file_fcntl().
291 * @file: Pointer to "struct file".
292 * @cmd: Command for fcntl().
293 * @arg: Argument for @cmd.
295 * Returns 0 on success, negative value otherwise.
297 static int tomoyo_file_fcntl(struct file *file, unsigned int cmd,
298 unsigned long arg)
300 if (!(cmd == F_SETFL && ((arg ^ file->f_flags) & O_APPEND)))
301 return 0;
302 return tomoyo_check_open_permission(tomoyo_domain(), &file->f_path,
303 O_WRONLY | (arg & O_APPEND));
307 * tomoyo_file_open - Target for security_file_open().
309 * @f: Pointer to "struct file".
310 * @cred: Pointer to "struct cred".
312 * Returns 0 on success, negative value otherwise.
314 static int tomoyo_file_open(struct file *f)
316 /* Don't check read permission here if called from do_execve(). */
317 if (current->in_execve)
318 return 0;
319 return tomoyo_check_open_permission(tomoyo_domain(), &f->f_path,
320 f->f_flags);
324 * tomoyo_file_ioctl - Target for security_file_ioctl().
326 * @file: Pointer to "struct file".
327 * @cmd: Command for ioctl().
328 * @arg: Argument for @cmd.
330 * Returns 0 on success, negative value otherwise.
332 static int tomoyo_file_ioctl(struct file *file, unsigned int cmd,
333 unsigned long arg)
335 return tomoyo_path_number_perm(TOMOYO_TYPE_IOCTL, &file->f_path, cmd);
339 * tomoyo_path_chmod - Target for security_path_chmod().
341 * @path: Pointer to "struct path".
342 * @mode: DAC permission mode.
344 * Returns 0 on success, negative value otherwise.
346 static int tomoyo_path_chmod(const struct path *path, umode_t mode)
348 return tomoyo_path_number_perm(TOMOYO_TYPE_CHMOD, path,
349 mode & S_IALLUGO);
353 * tomoyo_path_chown - Target for security_path_chown().
355 * @path: Pointer to "struct path".
356 * @uid: Owner ID.
357 * @gid: Group ID.
359 * Returns 0 on success, negative value otherwise.
361 static int tomoyo_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
363 int error = 0;
365 if (uid_valid(uid))
366 error = tomoyo_path_number_perm(TOMOYO_TYPE_CHOWN, path,
367 from_kuid(&init_user_ns, uid));
368 if (!error && gid_valid(gid))
369 error = tomoyo_path_number_perm(TOMOYO_TYPE_CHGRP, path,
370 from_kgid(&init_user_ns, gid));
371 return error;
375 * tomoyo_path_chroot - Target for security_path_chroot().
377 * @path: Pointer to "struct path".
379 * Returns 0 on success, negative value otherwise.
381 static int tomoyo_path_chroot(const struct path *path)
383 return tomoyo_path_perm(TOMOYO_TYPE_CHROOT, path, NULL);
387 * tomoyo_sb_mount - Target for security_sb_mount().
389 * @dev_name: Name of device file. Maybe NULL.
390 * @path: Pointer to "struct path".
391 * @type: Name of filesystem type. Maybe NULL.
392 * @flags: Mount options.
393 * @data: Optional data. Maybe NULL.
395 * Returns 0 on success, negative value otherwise.
397 static int tomoyo_sb_mount(const char *dev_name, const struct path *path,
398 const char *type, unsigned long flags, void *data)
400 return tomoyo_mount_permission(dev_name, path, type, flags, data);
404 * tomoyo_sb_umount - Target for security_sb_umount().
406 * @mnt: Pointer to "struct vfsmount".
407 * @flags: Unmount options.
409 * Returns 0 on success, negative value otherwise.
411 static int tomoyo_sb_umount(struct vfsmount *mnt, int flags)
413 struct path path = { .mnt = mnt, .dentry = mnt->mnt_root };
415 return tomoyo_path_perm(TOMOYO_TYPE_UMOUNT, &path, NULL);
419 * tomoyo_sb_pivotroot - Target for security_sb_pivotroot().
421 * @old_path: Pointer to "struct path".
422 * @new_path: Pointer to "struct path".
424 * Returns 0 on success, negative value otherwise.
426 static int tomoyo_sb_pivotroot(const struct path *old_path, const struct path *new_path)
428 return tomoyo_path2_perm(TOMOYO_TYPE_PIVOT_ROOT, new_path, old_path);
432 * tomoyo_socket_listen - Check permission for listen().
434 * @sock: Pointer to "struct socket".
435 * @backlog: Backlog parameter.
437 * Returns 0 on success, negative value otherwise.
439 static int tomoyo_socket_listen(struct socket *sock, int backlog)
441 return tomoyo_socket_listen_permission(sock);
445 * tomoyo_socket_connect - Check permission for connect().
447 * @sock: Pointer to "struct socket".
448 * @addr: Pointer to "struct sockaddr".
449 * @addr_len: Size of @addr.
451 * Returns 0 on success, negative value otherwise.
453 static int tomoyo_socket_connect(struct socket *sock, struct sockaddr *addr,
454 int addr_len)
456 return tomoyo_socket_connect_permission(sock, addr, addr_len);
460 * tomoyo_socket_bind - Check permission for bind().
462 * @sock: Pointer to "struct socket".
463 * @addr: Pointer to "struct sockaddr".
464 * @addr_len: Size of @addr.
466 * Returns 0 on success, negative value otherwise.
468 static int tomoyo_socket_bind(struct socket *sock, struct sockaddr *addr,
469 int addr_len)
471 return tomoyo_socket_bind_permission(sock, addr, addr_len);
475 * tomoyo_socket_sendmsg - Check permission for sendmsg().
477 * @sock: Pointer to "struct socket".
478 * @msg: Pointer to "struct msghdr".
479 * @size: Size of message.
481 * Returns 0 on success, negative value otherwise.
483 static int tomoyo_socket_sendmsg(struct socket *sock, struct msghdr *msg,
484 int size)
486 return tomoyo_socket_sendmsg_permission(sock, msg, size);
489 struct lsm_blob_sizes tomoyo_blob_sizes __lsm_ro_after_init = {
490 .lbs_task = sizeof(struct tomoyo_task),
494 * tomoyo_task_alloc - Target for security_task_alloc().
496 * @task: Pointer to "struct task_struct".
497 * @flags: clone() flags.
499 * Returns 0.
501 static int tomoyo_task_alloc(struct task_struct *task,
502 unsigned long clone_flags)
504 struct tomoyo_task *old = tomoyo_task(current);
505 struct tomoyo_task *new = tomoyo_task(task);
507 new->domain_info = old->domain_info;
508 atomic_inc(&new->domain_info->users);
509 new->old_domain_info = NULL;
510 return 0;
514 * tomoyo_task_free - Target for security_task_free().
516 * @task: Pointer to "struct task_struct".
518 static void tomoyo_task_free(struct task_struct *task)
520 struct tomoyo_task *s = tomoyo_task(task);
522 if (s->domain_info) {
523 atomic_dec(&s->domain_info->users);
524 s->domain_info = NULL;
526 if (s->old_domain_info) {
527 atomic_dec(&s->old_domain_info->users);
528 s->old_domain_info = NULL;
533 * tomoyo_security_ops is a "struct security_operations" which is used for
534 * registering TOMOYO.
536 static struct security_hook_list tomoyo_hooks[] __lsm_ro_after_init = {
537 LSM_HOOK_INIT(cred_prepare, tomoyo_cred_prepare),
538 LSM_HOOK_INIT(bprm_committed_creds, tomoyo_bprm_committed_creds),
539 LSM_HOOK_INIT(task_alloc, tomoyo_task_alloc),
540 LSM_HOOK_INIT(task_free, tomoyo_task_free),
541 #ifndef CONFIG_SECURITY_TOMOYO_OMIT_USERSPACE_LOADER
542 LSM_HOOK_INIT(bprm_set_creds, tomoyo_bprm_set_creds),
543 #endif
544 LSM_HOOK_INIT(bprm_check_security, tomoyo_bprm_check_security),
545 LSM_HOOK_INIT(file_fcntl, tomoyo_file_fcntl),
546 LSM_HOOK_INIT(file_open, tomoyo_file_open),
547 LSM_HOOK_INIT(path_truncate, tomoyo_path_truncate),
548 LSM_HOOK_INIT(path_unlink, tomoyo_path_unlink),
549 LSM_HOOK_INIT(path_mkdir, tomoyo_path_mkdir),
550 LSM_HOOK_INIT(path_rmdir, tomoyo_path_rmdir),
551 LSM_HOOK_INIT(path_symlink, tomoyo_path_symlink),
552 LSM_HOOK_INIT(path_mknod, tomoyo_path_mknod),
553 LSM_HOOK_INIT(path_link, tomoyo_path_link),
554 LSM_HOOK_INIT(path_rename, tomoyo_path_rename),
555 LSM_HOOK_INIT(inode_getattr, tomoyo_inode_getattr),
556 LSM_HOOK_INIT(file_ioctl, tomoyo_file_ioctl),
557 LSM_HOOK_INIT(path_chmod, tomoyo_path_chmod),
558 LSM_HOOK_INIT(path_chown, tomoyo_path_chown),
559 LSM_HOOK_INIT(path_chroot, tomoyo_path_chroot),
560 LSM_HOOK_INIT(sb_mount, tomoyo_sb_mount),
561 LSM_HOOK_INIT(sb_umount, tomoyo_sb_umount),
562 LSM_HOOK_INIT(sb_pivotroot, tomoyo_sb_pivotroot),
563 LSM_HOOK_INIT(socket_bind, tomoyo_socket_bind),
564 LSM_HOOK_INIT(socket_connect, tomoyo_socket_connect),
565 LSM_HOOK_INIT(socket_listen, tomoyo_socket_listen),
566 LSM_HOOK_INIT(socket_sendmsg, tomoyo_socket_sendmsg),
569 /* Lock for GC. */
570 DEFINE_SRCU(tomoyo_ss);
572 int tomoyo_enabled __lsm_ro_after_init = 1;
575 * tomoyo_init - Register TOMOYO Linux as a LSM module.
577 * Returns 0.
579 static int __init tomoyo_init(void)
581 struct tomoyo_task *s = tomoyo_task(current);
583 /* register ourselves with the security framework */
584 security_add_hooks(tomoyo_hooks, ARRAY_SIZE(tomoyo_hooks), "tomoyo");
585 pr_info("TOMOYO Linux initialized\n");
586 s->domain_info = &tomoyo_kernel_domain;
587 atomic_inc(&tomoyo_kernel_domain.users);
588 s->old_domain_info = NULL;
589 tomoyo_mm_init();
591 return 0;
594 DEFINE_LSM(tomoyo) = {
595 .name = "tomoyo",
596 .enabled = &tomoyo_enabled,
597 .flags = LSM_FLAG_LEGACY_MAJOR,
598 .blobs = &tomoyo_blob_sizes,
599 .init = tomoyo_init,