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_path_unlink - Target for security_path_unlink().
140 * @parent: Pointer to "struct path".
141 * @dentry: Pointer to "struct dentry".
143 * Returns 0 on success, negative value otherwise.
145 static int tomoyo_path_unlink(const struct path *parent, struct dentry *dentry)
147 struct path path = { .mnt = parent->mnt, .dentry = dentry };
149 return tomoyo_path_perm(TOMOYO_TYPE_UNLINK, &path, NULL);
153 * tomoyo_path_mkdir - Target for security_path_mkdir().
155 * @parent: Pointer to "struct path".
156 * @dentry: Pointer to "struct dentry".
157 * @mode: DAC permission mode.
159 * Returns 0 on success, negative value otherwise.
161 static int tomoyo_path_mkdir(const struct path *parent, struct dentry *dentry,
164 struct path path = { .mnt = parent->mnt, .dentry = dentry };
166 return tomoyo_path_number_perm(TOMOYO_TYPE_MKDIR, &path,
171 * tomoyo_path_rmdir - Target for security_path_rmdir().
173 * @parent: Pointer to "struct path".
174 * @dentry: Pointer to "struct dentry".
176 * Returns 0 on success, negative value otherwise.
178 static int tomoyo_path_rmdir(const struct path *parent, struct dentry *dentry)
180 struct path path = { .mnt = parent->mnt, .dentry = dentry };
182 return tomoyo_path_perm(TOMOYO_TYPE_RMDIR, &path, NULL);
186 * tomoyo_path_symlink - Target for security_path_symlink().
188 * @parent: Pointer to "struct path".
189 * @dentry: Pointer to "struct dentry".
190 * @old_name: Symlink's content.
192 * Returns 0 on success, negative value otherwise.
194 static int tomoyo_path_symlink(const struct path *parent, struct dentry *dentry,
195 const char *old_name)
197 struct path path = { .mnt = parent->mnt, .dentry = dentry };
199 return tomoyo_path_perm(TOMOYO_TYPE_SYMLINK, &path, old_name);
203 * tomoyo_path_mknod - Target for security_path_mknod().
205 * @parent: Pointer to "struct path".
206 * @dentry: Pointer to "struct dentry".
207 * @mode: DAC permission mode.
208 * @dev: Device attributes.
210 * Returns 0 on success, negative value otherwise.
212 static int tomoyo_path_mknod(const struct path *parent, struct dentry *dentry,
213 umode_t mode, unsigned int dev)
215 struct path path = { .mnt = parent->mnt, .dentry = dentry };
216 int type = TOMOYO_TYPE_CREATE;
217 const unsigned int perm = mode & S_IALLUGO;
219 switch (mode & S_IFMT) {
221 type = TOMOYO_TYPE_MKCHAR;
224 type = TOMOYO_TYPE_MKBLOCK;
229 return tomoyo_mkdev_perm(type, &path, perm, dev);
231 switch (mode & S_IFMT) {
233 type = TOMOYO_TYPE_MKFIFO;
236 type = TOMOYO_TYPE_MKSOCK;
239 return tomoyo_path_number_perm(type, &path, perm);
243 * tomoyo_path_link - Target for security_path_link().
245 * @old_dentry: Pointer to "struct dentry".
246 * @new_dir: Pointer to "struct path".
247 * @new_dentry: Pointer to "struct dentry".
249 * Returns 0 on success, negative value otherwise.
251 static int tomoyo_path_link(struct dentry *old_dentry, const struct path *new_dir,
252 struct dentry *new_dentry)
254 struct path path1 = { .mnt = new_dir->mnt, .dentry = old_dentry };
255 struct path path2 = { .mnt = new_dir->mnt, .dentry = new_dentry };
257 return tomoyo_path2_perm(TOMOYO_TYPE_LINK, &path1, &path2);
261 * tomoyo_path_rename - Target for security_path_rename().
263 * @old_parent: Pointer to "struct path".
264 * @old_dentry: Pointer to "struct dentry".
265 * @new_parent: Pointer to "struct path".
266 * @new_dentry: Pointer to "struct dentry".
267 * @flags: Rename options.
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,
275 const unsigned int flags)
277 struct path path1 = { .mnt = old_parent->mnt, .dentry = old_dentry };
278 struct path path2 = { .mnt = new_parent->mnt, .dentry = new_dentry };
280 if (flags & RENAME_EXCHANGE) {
281 const int err = tomoyo_path2_perm(TOMOYO_TYPE_RENAME, &path2,
287 return tomoyo_path2_perm(TOMOYO_TYPE_RENAME, &path1, &path2);
291 * tomoyo_file_fcntl - Target for security_file_fcntl().
293 * @file: Pointer to "struct file".
294 * @cmd: Command for fcntl().
295 * @arg: Argument for @cmd.
297 * Returns 0 on success, negative value otherwise.
299 static int tomoyo_file_fcntl(struct file *file, unsigned int cmd,
302 if (!(cmd == F_SETFL && ((arg ^ file->f_flags) & O_APPEND)))
304 return tomoyo_check_open_permission(tomoyo_domain(), &file->f_path,
305 O_WRONLY | (arg & O_APPEND));
309 * tomoyo_file_open - Target for security_file_open().
311 * @f: Pointer to "struct file".
313 * Returns 0 on success, negative value otherwise.
315 static int tomoyo_file_open(struct file *f)
317 /* Don't check read permission here if called from execve(). */
318 if (current->in_execve)
320 return tomoyo_check_open_permission(tomoyo_domain(), &f->f_path,
325 * tomoyo_file_ioctl - Target for security_file_ioctl().
327 * @file: Pointer to "struct file".
328 * @cmd: Command for ioctl().
329 * @arg: Argument for @cmd.
331 * Returns 0 on success, negative value otherwise.
333 static int tomoyo_file_ioctl(struct file *file, unsigned int cmd,
336 return tomoyo_path_number_perm(TOMOYO_TYPE_IOCTL, &file->f_path, cmd);
340 * tomoyo_path_chmod - Target for security_path_chmod().
342 * @path: Pointer to "struct path".
343 * @mode: DAC permission mode.
345 * Returns 0 on success, negative value otherwise.
347 static int tomoyo_path_chmod(const struct path *path, umode_t mode)
349 return tomoyo_path_number_perm(TOMOYO_TYPE_CHMOD, path,
354 * tomoyo_path_chown - Target for security_path_chown().
356 * @path: Pointer to "struct path".
360 * Returns 0 on success, negative value otherwise.
362 static int tomoyo_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
367 error = tomoyo_path_number_perm(TOMOYO_TYPE_CHOWN, path,
368 from_kuid(&init_user_ns, uid));
369 if (!error && gid_valid(gid))
370 error = tomoyo_path_number_perm(TOMOYO_TYPE_CHGRP, path,
371 from_kgid(&init_user_ns, gid));
376 * tomoyo_path_chroot - Target for security_path_chroot().
378 * @path: Pointer to "struct path".
380 * Returns 0 on success, negative value otherwise.
382 static int tomoyo_path_chroot(const struct path *path)
384 return tomoyo_path_perm(TOMOYO_TYPE_CHROOT, path, NULL);
388 * tomoyo_sb_mount - Target for security_sb_mount().
390 * @dev_name: Name of device file. Maybe NULL.
391 * @path: Pointer to "struct path".
392 * @type: Name of filesystem type. Maybe NULL.
393 * @flags: Mount options.
394 * @data: Optional data. Maybe NULL.
396 * Returns 0 on success, negative value otherwise.
398 static int tomoyo_sb_mount(const char *dev_name, const struct path *path,
399 const char *type, unsigned long flags, void *data)
401 return tomoyo_mount_permission(dev_name, path, type, flags, data);
405 * tomoyo_sb_umount - Target for security_sb_umount().
407 * @mnt: Pointer to "struct vfsmount".
408 * @flags: Unmount options.
410 * Returns 0 on success, negative value otherwise.
412 static int tomoyo_sb_umount(struct vfsmount *mnt, int flags)
414 struct path path = { .mnt = mnt, .dentry = mnt->mnt_root };
416 return tomoyo_path_perm(TOMOYO_TYPE_UMOUNT, &path, NULL);
420 * tomoyo_sb_pivotroot - Target for security_sb_pivotroot().
422 * @old_path: Pointer to "struct path".
423 * @new_path: Pointer to "struct path".
425 * Returns 0 on success, negative value otherwise.
427 static int tomoyo_sb_pivotroot(const struct path *old_path, const struct path *new_path)
429 return tomoyo_path2_perm(TOMOYO_TYPE_PIVOT_ROOT, new_path, old_path);
433 * tomoyo_socket_listen - Check permission for listen().
435 * @sock: Pointer to "struct socket".
436 * @backlog: Backlog parameter.
438 * Returns 0 on success, negative value otherwise.
440 static int tomoyo_socket_listen(struct socket *sock, int backlog)
442 return tomoyo_socket_listen_permission(sock);
446 * tomoyo_socket_connect - Check permission for connect().
448 * @sock: Pointer to "struct socket".
449 * @addr: Pointer to "struct sockaddr".
450 * @addr_len: Size of @addr.
452 * Returns 0 on success, negative value otherwise.
454 static int tomoyo_socket_connect(struct socket *sock, struct sockaddr *addr,
457 return tomoyo_socket_connect_permission(sock, addr, addr_len);
461 * tomoyo_socket_bind - Check permission for bind().
463 * @sock: Pointer to "struct socket".
464 * @addr: Pointer to "struct sockaddr".
465 * @addr_len: Size of @addr.
467 * Returns 0 on success, negative value otherwise.
469 static int tomoyo_socket_bind(struct socket *sock, struct sockaddr *addr,
472 return tomoyo_socket_bind_permission(sock, addr, addr_len);
476 * tomoyo_socket_sendmsg - Check permission for sendmsg().
478 * @sock: Pointer to "struct socket".
479 * @msg: Pointer to "struct msghdr".
480 * @size: Size of message.
482 * Returns 0 on success, negative value otherwise.
484 static int tomoyo_socket_sendmsg(struct socket *sock, struct msghdr *msg,
487 return tomoyo_socket_sendmsg_permission(sock, msg, size);
490 struct lsm_blob_sizes tomoyo_blob_sizes __lsm_ro_after_init = {
491 .lbs_task = sizeof(struct tomoyo_task),
495 * tomoyo_task_alloc - Target for security_task_alloc().
497 * @task: Pointer to "struct task_struct".
498 * @clone_flags: clone() flags.
502 static int tomoyo_task_alloc(struct task_struct *task,
503 unsigned long clone_flags)
505 struct tomoyo_task *old = tomoyo_task(current);
506 struct tomoyo_task *new = tomoyo_task(task);
508 new->domain_info = old->domain_info;
509 atomic_inc(&new->domain_info->users);
510 new->old_domain_info = NULL;
515 * tomoyo_task_free - Target for security_task_free().
517 * @task: Pointer to "struct task_struct".
519 static void tomoyo_task_free(struct task_struct *task)
521 struct tomoyo_task *s = tomoyo_task(task);
523 if (s->domain_info) {
524 atomic_dec(&s->domain_info->users);
525 s->domain_info = NULL;
527 if (s->old_domain_info) {
528 atomic_dec(&s->old_domain_info->users);
529 s->old_domain_info = NULL;
534 * tomoyo_security_ops is a "struct security_operations" which is used for
535 * registering TOMOYO.
537 static struct security_hook_list tomoyo_hooks[] __lsm_ro_after_init = {
538 LSM_HOOK_INIT(cred_prepare, tomoyo_cred_prepare),
539 LSM_HOOK_INIT(bprm_committed_creds, tomoyo_bprm_committed_creds),
540 LSM_HOOK_INIT(task_alloc, tomoyo_task_alloc),
541 LSM_HOOK_INIT(task_free, tomoyo_task_free),
542 #ifndef CONFIG_SECURITY_TOMOYO_OMIT_USERSPACE_LOADER
543 LSM_HOOK_INIT(bprm_creds_for_exec, tomoyo_bprm_creds_for_exec),
545 LSM_HOOK_INIT(bprm_check_security, tomoyo_bprm_check_security),
546 LSM_HOOK_INIT(file_fcntl, tomoyo_file_fcntl),
547 LSM_HOOK_INIT(file_open, tomoyo_file_open),
548 LSM_HOOK_INIT(path_truncate, tomoyo_path_truncate),
549 LSM_HOOK_INIT(path_unlink, tomoyo_path_unlink),
550 LSM_HOOK_INIT(path_mkdir, tomoyo_path_mkdir),
551 LSM_HOOK_INIT(path_rmdir, tomoyo_path_rmdir),
552 LSM_HOOK_INIT(path_symlink, tomoyo_path_symlink),
553 LSM_HOOK_INIT(path_mknod, tomoyo_path_mknod),
554 LSM_HOOK_INIT(path_link, tomoyo_path_link),
555 LSM_HOOK_INIT(path_rename, tomoyo_path_rename),
556 LSM_HOOK_INIT(inode_getattr, tomoyo_inode_getattr),
557 LSM_HOOK_INIT(file_ioctl, tomoyo_file_ioctl),
558 LSM_HOOK_INIT(path_chmod, tomoyo_path_chmod),
559 LSM_HOOK_INIT(path_chown, tomoyo_path_chown),
560 LSM_HOOK_INIT(path_chroot, tomoyo_path_chroot),
561 LSM_HOOK_INIT(sb_mount, tomoyo_sb_mount),
562 LSM_HOOK_INIT(sb_umount, tomoyo_sb_umount),
563 LSM_HOOK_INIT(sb_pivotroot, tomoyo_sb_pivotroot),
564 LSM_HOOK_INIT(socket_bind, tomoyo_socket_bind),
565 LSM_HOOK_INIT(socket_connect, tomoyo_socket_connect),
566 LSM_HOOK_INIT(socket_listen, tomoyo_socket_listen),
567 LSM_HOOK_INIT(socket_sendmsg, tomoyo_socket_sendmsg),
571 DEFINE_SRCU(tomoyo_ss);
573 int tomoyo_enabled __lsm_ro_after_init = 1;
576 * tomoyo_init - Register TOMOYO Linux as a LSM module.
580 static int __init tomoyo_init(void)
582 struct tomoyo_task *s = tomoyo_task(current);
584 /* register ourselves with the security framework */
585 security_add_hooks(tomoyo_hooks, ARRAY_SIZE(tomoyo_hooks), "tomoyo");
586 pr_info("TOMOYO Linux initialized\n");
587 s->domain_info = &tomoyo_kernel_domain;
588 atomic_inc(&tomoyo_kernel_domain.users);
589 s->old_domain_info = NULL;
595 DEFINE_LSM(tomoyo) = {
597 .enabled = &tomoyo_enabled,
598 .flags = LSM_FLAG_LEGACY_MAJOR,
599 .blobs = &tomoyo_blob_sizes,