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>
9 #include <uapi/linux/lsm.h>
13 * tomoyo_domain - Get "struct tomoyo_domain_info" for current thread.
15 * Returns pointer to "struct tomoyo_domain_info" for current thread.
17 struct tomoyo_domain_info
*tomoyo_domain(void)
19 struct tomoyo_task
*s
= tomoyo_task(current
);
21 if (s
->old_domain_info
&& !current
->in_execve
) {
22 atomic_dec(&s
->old_domain_info
->users
);
23 s
->old_domain_info
= NULL
;
25 return s
->domain_info
;
29 * tomoyo_cred_prepare - Target for security_prepare_creds().
31 * @new: Pointer to "struct cred".
32 * @old: Pointer to "struct cred".
33 * @gfp: Memory allocation flags.
37 static int tomoyo_cred_prepare(struct cred
*new, const struct cred
*old
,
40 /* Restore old_domain_info saved by previous execve() request. */
41 struct tomoyo_task
*s
= tomoyo_task(current
);
43 if (s
->old_domain_info
&& !current
->in_execve
) {
44 atomic_dec(&s
->domain_info
->users
);
45 s
->domain_info
= s
->old_domain_info
;
46 s
->old_domain_info
= NULL
;
52 * tomoyo_bprm_committed_creds - Target for security_bprm_committed_creds().
54 * @bprm: Pointer to "struct linux_binprm".
56 static void tomoyo_bprm_committed_creds(const struct linux_binprm
*bprm
)
58 /* Clear old_domain_info saved by execve() request. */
59 struct tomoyo_task
*s
= tomoyo_task(current
);
61 atomic_dec(&s
->old_domain_info
->users
);
62 s
->old_domain_info
= NULL
;
65 #ifndef CONFIG_SECURITY_TOMOYO_OMIT_USERSPACE_LOADER
67 * tomoyo_bprm_creds_for_exec - Target for security_bprm_creds_for_exec().
69 * @bprm: Pointer to "struct linux_binprm".
73 static int tomoyo_bprm_creds_for_exec(struct linux_binprm
*bprm
)
76 * Load policy if /sbin/tomoyo-init exists and /sbin/init is requested
79 if (!tomoyo_policy_loaded
)
80 tomoyo_load_policy(bprm
->filename
);
86 * tomoyo_bprm_check_security - Target for security_bprm_check().
88 * @bprm: Pointer to "struct linux_binprm".
90 * Returns 0 on success, negative value otherwise.
92 static int tomoyo_bprm_check_security(struct linux_binprm
*bprm
)
94 struct tomoyo_task
*s
= tomoyo_task(current
);
97 * Execute permission is checked against pathname passed to execve()
98 * using current domain.
100 if (!s
->old_domain_info
) {
101 const int idx
= tomoyo_read_lock();
102 const int err
= tomoyo_find_next_domain(bprm
);
104 tomoyo_read_unlock(idx
);
108 * Read permission is checked against interpreters using next domain.
110 return tomoyo_check_open_permission(s
->domain_info
,
111 &bprm
->file
->f_path
, O_RDONLY
);
115 * tomoyo_inode_getattr - Target for security_inode_getattr().
117 * @path: Pointer to "struct path".
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_file_truncate - Target for security_file_truncate().
141 * @file: Pointer to "struct file".
143 * Returns 0 on success, negative value otherwise.
145 static int tomoyo_file_truncate(struct file
*file
)
147 return tomoyo_path_truncate(&file
->f_path
);
151 * tomoyo_path_unlink - Target for security_path_unlink().
153 * @parent: Pointer to "struct path".
154 * @dentry: Pointer to "struct dentry".
156 * Returns 0 on success, negative value otherwise.
158 static int tomoyo_path_unlink(const struct path
*parent
, struct dentry
*dentry
)
160 struct path path
= { .mnt
= parent
->mnt
, .dentry
= dentry
};
162 return tomoyo_path_perm(TOMOYO_TYPE_UNLINK
, &path
, NULL
);
166 * tomoyo_path_mkdir - Target for security_path_mkdir().
168 * @parent: Pointer to "struct path".
169 * @dentry: Pointer to "struct dentry".
170 * @mode: DAC permission mode.
172 * Returns 0 on success, negative value otherwise.
174 static int tomoyo_path_mkdir(const struct path
*parent
, struct dentry
*dentry
,
177 struct path path
= { .mnt
= parent
->mnt
, .dentry
= dentry
};
179 return tomoyo_path_number_perm(TOMOYO_TYPE_MKDIR
, &path
,
184 * tomoyo_path_rmdir - Target for security_path_rmdir().
186 * @parent: Pointer to "struct path".
187 * @dentry: Pointer to "struct dentry".
189 * Returns 0 on success, negative value otherwise.
191 static int tomoyo_path_rmdir(const struct path
*parent
, struct dentry
*dentry
)
193 struct path path
= { .mnt
= parent
->mnt
, .dentry
= dentry
};
195 return tomoyo_path_perm(TOMOYO_TYPE_RMDIR
, &path
, NULL
);
199 * tomoyo_path_symlink - Target for security_path_symlink().
201 * @parent: Pointer to "struct path".
202 * @dentry: Pointer to "struct dentry".
203 * @old_name: Symlink's content.
205 * Returns 0 on success, negative value otherwise.
207 static int tomoyo_path_symlink(const struct path
*parent
, struct dentry
*dentry
,
208 const char *old_name
)
210 struct path path
= { .mnt
= parent
->mnt
, .dentry
= dentry
};
212 return tomoyo_path_perm(TOMOYO_TYPE_SYMLINK
, &path
, old_name
);
216 * tomoyo_path_mknod - Target for security_path_mknod().
218 * @parent: Pointer to "struct path".
219 * @dentry: Pointer to "struct dentry".
220 * @mode: DAC permission mode.
221 * @dev: Device attributes.
223 * Returns 0 on success, negative value otherwise.
225 static int tomoyo_path_mknod(const struct path
*parent
, struct dentry
*dentry
,
226 umode_t mode
, unsigned int dev
)
228 struct path path
= { .mnt
= parent
->mnt
, .dentry
= dentry
};
229 int type
= TOMOYO_TYPE_CREATE
;
230 const unsigned int perm
= mode
& S_IALLUGO
;
232 switch (mode
& S_IFMT
) {
234 type
= TOMOYO_TYPE_MKCHAR
;
237 type
= TOMOYO_TYPE_MKBLOCK
;
242 return tomoyo_mkdev_perm(type
, &path
, perm
, dev
);
244 switch (mode
& S_IFMT
) {
246 type
= TOMOYO_TYPE_MKFIFO
;
249 type
= TOMOYO_TYPE_MKSOCK
;
252 return tomoyo_path_number_perm(type
, &path
, perm
);
256 * tomoyo_path_link - Target for security_path_link().
258 * @old_dentry: Pointer to "struct dentry".
259 * @new_dir: Pointer to "struct path".
260 * @new_dentry: Pointer to "struct dentry".
262 * Returns 0 on success, negative value otherwise.
264 static int tomoyo_path_link(struct dentry
*old_dentry
, const struct path
*new_dir
,
265 struct dentry
*new_dentry
)
267 struct path path1
= { .mnt
= new_dir
->mnt
, .dentry
= old_dentry
};
268 struct path path2
= { .mnt
= new_dir
->mnt
, .dentry
= new_dentry
};
270 return tomoyo_path2_perm(TOMOYO_TYPE_LINK
, &path1
, &path2
);
274 * tomoyo_path_rename - Target for security_path_rename().
276 * @old_parent: Pointer to "struct path".
277 * @old_dentry: Pointer to "struct dentry".
278 * @new_parent: Pointer to "struct path".
279 * @new_dentry: Pointer to "struct dentry".
280 * @flags: Rename options.
282 * Returns 0 on success, negative value otherwise.
284 static int tomoyo_path_rename(const struct path
*old_parent
,
285 struct dentry
*old_dentry
,
286 const struct path
*new_parent
,
287 struct dentry
*new_dentry
,
288 const unsigned int flags
)
290 struct path path1
= { .mnt
= old_parent
->mnt
, .dentry
= old_dentry
};
291 struct path path2
= { .mnt
= new_parent
->mnt
, .dentry
= new_dentry
};
293 if (flags
& RENAME_EXCHANGE
) {
294 const int err
= tomoyo_path2_perm(TOMOYO_TYPE_RENAME
, &path2
,
300 return tomoyo_path2_perm(TOMOYO_TYPE_RENAME
, &path1
, &path2
);
304 * tomoyo_file_fcntl - Target for security_file_fcntl().
306 * @file: Pointer to "struct file".
307 * @cmd: Command for fcntl().
308 * @arg: Argument for @cmd.
310 * Returns 0 on success, negative value otherwise.
312 static int tomoyo_file_fcntl(struct file
*file
, unsigned int cmd
,
315 if (!(cmd
== F_SETFL
&& ((arg
^ file
->f_flags
) & O_APPEND
)))
317 return tomoyo_check_open_permission(tomoyo_domain(), &file
->f_path
,
318 O_WRONLY
| (arg
& O_APPEND
));
322 * tomoyo_file_open - Target for security_file_open().
324 * @f: Pointer to "struct file".
326 * Returns 0 on success, negative value otherwise.
328 static int tomoyo_file_open(struct file
*f
)
330 /* Don't check read permission here if called from execve(). */
331 /* Illogically, FMODE_EXEC is in f_flags, not f_mode. */
332 if (f
->f_flags
& __FMODE_EXEC
)
334 return tomoyo_check_open_permission(tomoyo_domain(), &f
->f_path
,
339 * tomoyo_file_ioctl - Target for security_file_ioctl().
341 * @file: Pointer to "struct file".
342 * @cmd: Command for ioctl().
343 * @arg: Argument for @cmd.
345 * Returns 0 on success, negative value otherwise.
347 static int tomoyo_file_ioctl(struct file
*file
, unsigned int cmd
,
350 return tomoyo_path_number_perm(TOMOYO_TYPE_IOCTL
, &file
->f_path
, cmd
);
354 * tomoyo_path_chmod - Target for security_path_chmod().
356 * @path: Pointer to "struct path".
357 * @mode: DAC permission mode.
359 * Returns 0 on success, negative value otherwise.
361 static int tomoyo_path_chmod(const struct path
*path
, umode_t mode
)
363 return tomoyo_path_number_perm(TOMOYO_TYPE_CHMOD
, path
,
368 * tomoyo_path_chown - Target for security_path_chown().
370 * @path: Pointer to "struct path".
374 * Returns 0 on success, negative value otherwise.
376 static int tomoyo_path_chown(const struct path
*path
, kuid_t uid
, kgid_t gid
)
381 error
= tomoyo_path_number_perm(TOMOYO_TYPE_CHOWN
, path
,
382 from_kuid(&init_user_ns
, uid
));
383 if (!error
&& gid_valid(gid
))
384 error
= tomoyo_path_number_perm(TOMOYO_TYPE_CHGRP
, path
,
385 from_kgid(&init_user_ns
, gid
));
390 * tomoyo_path_chroot - Target for security_path_chroot().
392 * @path: Pointer to "struct path".
394 * Returns 0 on success, negative value otherwise.
396 static int tomoyo_path_chroot(const struct path
*path
)
398 return tomoyo_path_perm(TOMOYO_TYPE_CHROOT
, path
, NULL
);
402 * tomoyo_sb_mount - Target for security_sb_mount().
404 * @dev_name: Name of device file. Maybe NULL.
405 * @path: Pointer to "struct path".
406 * @type: Name of filesystem type. Maybe NULL.
407 * @flags: Mount options.
408 * @data: Optional data. Maybe NULL.
410 * Returns 0 on success, negative value otherwise.
412 static int tomoyo_sb_mount(const char *dev_name
, const struct path
*path
,
413 const char *type
, unsigned long flags
, void *data
)
415 return tomoyo_mount_permission(dev_name
, path
, type
, flags
, data
);
419 * tomoyo_sb_umount - Target for security_sb_umount().
421 * @mnt: Pointer to "struct vfsmount".
422 * @flags: Unmount options.
424 * Returns 0 on success, negative value otherwise.
426 static int tomoyo_sb_umount(struct vfsmount
*mnt
, int flags
)
428 struct path path
= { .mnt
= mnt
, .dentry
= mnt
->mnt_root
};
430 return tomoyo_path_perm(TOMOYO_TYPE_UMOUNT
, &path
, NULL
);
434 * tomoyo_sb_pivotroot - Target for security_sb_pivotroot().
436 * @old_path: Pointer to "struct path".
437 * @new_path: Pointer to "struct path".
439 * Returns 0 on success, negative value otherwise.
441 static int tomoyo_sb_pivotroot(const struct path
*old_path
, const struct path
*new_path
)
443 return tomoyo_path2_perm(TOMOYO_TYPE_PIVOT_ROOT
, new_path
, old_path
);
447 * tomoyo_socket_listen - Check permission for listen().
449 * @sock: Pointer to "struct socket".
450 * @backlog: Backlog parameter.
452 * Returns 0 on success, negative value otherwise.
454 static int tomoyo_socket_listen(struct socket
*sock
, int backlog
)
456 return tomoyo_socket_listen_permission(sock
);
460 * tomoyo_socket_connect - Check permission for connect().
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_connect(struct socket
*sock
, struct sockaddr
*addr
,
471 return tomoyo_socket_connect_permission(sock
, addr
, addr_len
);
475 * tomoyo_socket_bind - Check permission for bind().
477 * @sock: Pointer to "struct socket".
478 * @addr: Pointer to "struct sockaddr".
479 * @addr_len: Size of @addr.
481 * Returns 0 on success, negative value otherwise.
483 static int tomoyo_socket_bind(struct socket
*sock
, struct sockaddr
*addr
,
486 return tomoyo_socket_bind_permission(sock
, addr
, addr_len
);
490 * tomoyo_socket_sendmsg - Check permission for sendmsg().
492 * @sock: Pointer to "struct socket".
493 * @msg: Pointer to "struct msghdr".
494 * @size: Size of message.
496 * Returns 0 on success, negative value otherwise.
498 static int tomoyo_socket_sendmsg(struct socket
*sock
, struct msghdr
*msg
,
501 return tomoyo_socket_sendmsg_permission(sock
, msg
, size
);
504 struct lsm_blob_sizes tomoyo_blob_sizes __ro_after_init
= {
505 .lbs_task
= sizeof(struct tomoyo_task
),
509 * tomoyo_task_alloc - Target for security_task_alloc().
511 * @task: Pointer to "struct task_struct".
512 * @clone_flags: clone() flags.
516 static int tomoyo_task_alloc(struct task_struct
*task
,
517 unsigned long clone_flags
)
519 struct tomoyo_task
*old
= tomoyo_task(current
);
520 struct tomoyo_task
*new = tomoyo_task(task
);
522 new->domain_info
= old
->domain_info
;
523 atomic_inc(&new->domain_info
->users
);
524 new->old_domain_info
= NULL
;
529 * tomoyo_task_free - Target for security_task_free().
531 * @task: Pointer to "struct task_struct".
533 static void tomoyo_task_free(struct task_struct
*task
)
535 struct tomoyo_task
*s
= tomoyo_task(task
);
537 if (s
->domain_info
) {
538 atomic_dec(&s
->domain_info
->users
);
539 s
->domain_info
= NULL
;
541 if (s
->old_domain_info
) {
542 atomic_dec(&s
->old_domain_info
->users
);
543 s
->old_domain_info
= NULL
;
547 static const struct lsm_id tomoyo_lsmid
= {
553 * tomoyo_security_ops is a "struct security_operations" which is used for
554 * registering TOMOYO.
556 static struct security_hook_list tomoyo_hooks
[] __ro_after_init
= {
557 LSM_HOOK_INIT(cred_prepare
, tomoyo_cred_prepare
),
558 LSM_HOOK_INIT(bprm_committed_creds
, tomoyo_bprm_committed_creds
),
559 LSM_HOOK_INIT(task_alloc
, tomoyo_task_alloc
),
560 LSM_HOOK_INIT(task_free
, tomoyo_task_free
),
561 #ifndef CONFIG_SECURITY_TOMOYO_OMIT_USERSPACE_LOADER
562 LSM_HOOK_INIT(bprm_creds_for_exec
, tomoyo_bprm_creds_for_exec
),
564 LSM_HOOK_INIT(bprm_check_security
, tomoyo_bprm_check_security
),
565 LSM_HOOK_INIT(file_fcntl
, tomoyo_file_fcntl
),
566 LSM_HOOK_INIT(file_open
, tomoyo_file_open
),
567 LSM_HOOK_INIT(file_truncate
, tomoyo_file_truncate
),
568 LSM_HOOK_INIT(path_truncate
, tomoyo_path_truncate
),
569 LSM_HOOK_INIT(path_unlink
, tomoyo_path_unlink
),
570 LSM_HOOK_INIT(path_mkdir
, tomoyo_path_mkdir
),
571 LSM_HOOK_INIT(path_rmdir
, tomoyo_path_rmdir
),
572 LSM_HOOK_INIT(path_symlink
, tomoyo_path_symlink
),
573 LSM_HOOK_INIT(path_mknod
, tomoyo_path_mknod
),
574 LSM_HOOK_INIT(path_link
, tomoyo_path_link
),
575 LSM_HOOK_INIT(path_rename
, tomoyo_path_rename
),
576 LSM_HOOK_INIT(inode_getattr
, tomoyo_inode_getattr
),
577 LSM_HOOK_INIT(file_ioctl
, tomoyo_file_ioctl
),
578 LSM_HOOK_INIT(file_ioctl_compat
, tomoyo_file_ioctl
),
579 LSM_HOOK_INIT(path_chmod
, tomoyo_path_chmod
),
580 LSM_HOOK_INIT(path_chown
, tomoyo_path_chown
),
581 LSM_HOOK_INIT(path_chroot
, tomoyo_path_chroot
),
582 LSM_HOOK_INIT(sb_mount
, tomoyo_sb_mount
),
583 LSM_HOOK_INIT(sb_umount
, tomoyo_sb_umount
),
584 LSM_HOOK_INIT(sb_pivotroot
, tomoyo_sb_pivotroot
),
585 LSM_HOOK_INIT(socket_bind
, tomoyo_socket_bind
),
586 LSM_HOOK_INIT(socket_connect
, tomoyo_socket_connect
),
587 LSM_HOOK_INIT(socket_listen
, tomoyo_socket_listen
),
588 LSM_HOOK_INIT(socket_sendmsg
, tomoyo_socket_sendmsg
),
592 DEFINE_SRCU(tomoyo_ss
);
594 int tomoyo_enabled __ro_after_init
= 1;
597 * tomoyo_init - Register TOMOYO Linux as a LSM module.
601 static int __init
tomoyo_init(void)
603 struct tomoyo_task
*s
= tomoyo_task(current
);
605 /* register ourselves with the security framework */
606 security_add_hooks(tomoyo_hooks
, ARRAY_SIZE(tomoyo_hooks
),
608 pr_info("TOMOYO Linux initialized\n");
609 s
->domain_info
= &tomoyo_kernel_domain
;
610 atomic_inc(&tomoyo_kernel_domain
.users
);
611 s
->old_domain_info
= NULL
;
617 DEFINE_LSM(tomoyo
) = {
619 .enabled
= &tomoyo_enabled
,
620 .flags
= LSM_FLAG_LEGACY_MAJOR
,
621 .blobs
= &tomoyo_blob_sizes
,