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".
268 * Returns 0 on success, negative value otherwise.
270 static int tomoyo_path_rename(const struct path
*old_parent
,
271 struct dentry
*old_dentry
,
272 const struct path
*new_parent
,
273 struct dentry
*new_dentry
)
275 struct path path1
= { .mnt
= old_parent
->mnt
, .dentry
= old_dentry
};
276 struct path path2
= { .mnt
= new_parent
->mnt
, .dentry
= new_dentry
};
278 return tomoyo_path2_perm(TOMOYO_TYPE_RENAME
, &path1
, &path2
);
282 * tomoyo_file_fcntl - Target for security_file_fcntl().
284 * @file: Pointer to "struct file".
285 * @cmd: Command for fcntl().
286 * @arg: Argument for @cmd.
288 * Returns 0 on success, negative value otherwise.
290 static int tomoyo_file_fcntl(struct file
*file
, unsigned int cmd
,
293 if (!(cmd
== F_SETFL
&& ((arg
^ file
->f_flags
) & O_APPEND
)))
295 return tomoyo_check_open_permission(tomoyo_domain(), &file
->f_path
,
296 O_WRONLY
| (arg
& O_APPEND
));
300 * tomoyo_file_open - Target for security_file_open().
302 * @f: Pointer to "struct file".
304 * Returns 0 on success, negative value otherwise.
306 static int tomoyo_file_open(struct file
*f
)
308 /* Don't check read permission here if called from execve(). */
309 if (current
->in_execve
)
311 return tomoyo_check_open_permission(tomoyo_domain(), &f
->f_path
,
316 * tomoyo_file_ioctl - Target for security_file_ioctl().
318 * @file: Pointer to "struct file".
319 * @cmd: Command for ioctl().
320 * @arg: Argument for @cmd.
322 * Returns 0 on success, negative value otherwise.
324 static int tomoyo_file_ioctl(struct file
*file
, unsigned int cmd
,
327 return tomoyo_path_number_perm(TOMOYO_TYPE_IOCTL
, &file
->f_path
, cmd
);
331 * tomoyo_path_chmod - Target for security_path_chmod().
333 * @path: Pointer to "struct path".
334 * @mode: DAC permission mode.
336 * Returns 0 on success, negative value otherwise.
338 static int tomoyo_path_chmod(const struct path
*path
, umode_t mode
)
340 return tomoyo_path_number_perm(TOMOYO_TYPE_CHMOD
, path
,
345 * tomoyo_path_chown - Target for security_path_chown().
347 * @path: Pointer to "struct path".
351 * Returns 0 on success, negative value otherwise.
353 static int tomoyo_path_chown(const struct path
*path
, kuid_t uid
, kgid_t gid
)
358 error
= tomoyo_path_number_perm(TOMOYO_TYPE_CHOWN
, path
,
359 from_kuid(&init_user_ns
, uid
));
360 if (!error
&& gid_valid(gid
))
361 error
= tomoyo_path_number_perm(TOMOYO_TYPE_CHGRP
, path
,
362 from_kgid(&init_user_ns
, gid
));
367 * tomoyo_path_chroot - Target for security_path_chroot().
369 * @path: Pointer to "struct path".
371 * Returns 0 on success, negative value otherwise.
373 static int tomoyo_path_chroot(const struct path
*path
)
375 return tomoyo_path_perm(TOMOYO_TYPE_CHROOT
, path
, NULL
);
379 * tomoyo_sb_mount - Target for security_sb_mount().
381 * @dev_name: Name of device file. Maybe NULL.
382 * @path: Pointer to "struct path".
383 * @type: Name of filesystem type. Maybe NULL.
384 * @flags: Mount options.
385 * @data: Optional data. Maybe NULL.
387 * Returns 0 on success, negative value otherwise.
389 static int tomoyo_sb_mount(const char *dev_name
, const struct path
*path
,
390 const char *type
, unsigned long flags
, void *data
)
392 return tomoyo_mount_permission(dev_name
, path
, type
, flags
, data
);
396 * tomoyo_sb_umount - Target for security_sb_umount().
398 * @mnt: Pointer to "struct vfsmount".
399 * @flags: Unmount options.
401 * Returns 0 on success, negative value otherwise.
403 static int tomoyo_sb_umount(struct vfsmount
*mnt
, int flags
)
405 struct path path
= { .mnt
= mnt
, .dentry
= mnt
->mnt_root
};
407 return tomoyo_path_perm(TOMOYO_TYPE_UMOUNT
, &path
, NULL
);
411 * tomoyo_sb_pivotroot - Target for security_sb_pivotroot().
413 * @old_path: Pointer to "struct path".
414 * @new_path: Pointer to "struct path".
416 * Returns 0 on success, negative value otherwise.
418 static int tomoyo_sb_pivotroot(const struct path
*old_path
, const struct path
*new_path
)
420 return tomoyo_path2_perm(TOMOYO_TYPE_PIVOT_ROOT
, new_path
, old_path
);
424 * tomoyo_socket_listen - Check permission for listen().
426 * @sock: Pointer to "struct socket".
427 * @backlog: Backlog parameter.
429 * Returns 0 on success, negative value otherwise.
431 static int tomoyo_socket_listen(struct socket
*sock
, int backlog
)
433 return tomoyo_socket_listen_permission(sock
);
437 * tomoyo_socket_connect - Check permission for connect().
439 * @sock: Pointer to "struct socket".
440 * @addr: Pointer to "struct sockaddr".
441 * @addr_len: Size of @addr.
443 * Returns 0 on success, negative value otherwise.
445 static int tomoyo_socket_connect(struct socket
*sock
, struct sockaddr
*addr
,
448 return tomoyo_socket_connect_permission(sock
, addr
, addr_len
);
452 * tomoyo_socket_bind - Check permission for bind().
454 * @sock: Pointer to "struct socket".
455 * @addr: Pointer to "struct sockaddr".
456 * @addr_len: Size of @addr.
458 * Returns 0 on success, negative value otherwise.
460 static int tomoyo_socket_bind(struct socket
*sock
, struct sockaddr
*addr
,
463 return tomoyo_socket_bind_permission(sock
, addr
, addr_len
);
467 * tomoyo_socket_sendmsg - Check permission for sendmsg().
469 * @sock: Pointer to "struct socket".
470 * @msg: Pointer to "struct msghdr".
471 * @size: Size of message.
473 * Returns 0 on success, negative value otherwise.
475 static int tomoyo_socket_sendmsg(struct socket
*sock
, struct msghdr
*msg
,
478 return tomoyo_socket_sendmsg_permission(sock
, msg
, size
);
481 struct lsm_blob_sizes tomoyo_blob_sizes __lsm_ro_after_init
= {
482 .lbs_task
= sizeof(struct tomoyo_task
),
486 * tomoyo_task_alloc - Target for security_task_alloc().
488 * @task: Pointer to "struct task_struct".
489 * @clone_flags: clone() flags.
493 static int tomoyo_task_alloc(struct task_struct
*task
,
494 unsigned long clone_flags
)
496 struct tomoyo_task
*old
= tomoyo_task(current
);
497 struct tomoyo_task
*new = tomoyo_task(task
);
499 new->domain_info
= old
->domain_info
;
500 atomic_inc(&new->domain_info
->users
);
501 new->old_domain_info
= NULL
;
506 * tomoyo_task_free - Target for security_task_free().
508 * @task: Pointer to "struct task_struct".
510 static void tomoyo_task_free(struct task_struct
*task
)
512 struct tomoyo_task
*s
= tomoyo_task(task
);
514 if (s
->domain_info
) {
515 atomic_dec(&s
->domain_info
->users
);
516 s
->domain_info
= NULL
;
518 if (s
->old_domain_info
) {
519 atomic_dec(&s
->old_domain_info
->users
);
520 s
->old_domain_info
= NULL
;
524 static struct lsm_id tomoyo_lsmid __lsm_ro_after_init
= {
526 .slot
= LSMBLOB_NOT_NEEDED
530 * tomoyo_security_ops is a "struct security_operations" which is used for
531 * registering TOMOYO.
533 static struct security_hook_list tomoyo_hooks
[] __lsm_ro_after_init
= {
534 LSM_HOOK_INIT(cred_prepare
, tomoyo_cred_prepare
),
535 LSM_HOOK_INIT(bprm_committed_creds
, tomoyo_bprm_committed_creds
),
536 LSM_HOOK_INIT(task_alloc
, tomoyo_task_alloc
),
537 LSM_HOOK_INIT(task_free
, tomoyo_task_free
),
538 #ifndef CONFIG_SECURITY_TOMOYO_OMIT_USERSPACE_LOADER
539 LSM_HOOK_INIT(bprm_creds_for_exec
, tomoyo_bprm_creds_for_exec
),
541 LSM_HOOK_INIT(bprm_check_security
, tomoyo_bprm_check_security
),
542 LSM_HOOK_INIT(file_fcntl
, tomoyo_file_fcntl
),
543 LSM_HOOK_INIT(file_open
, tomoyo_file_open
),
544 LSM_HOOK_INIT(path_truncate
, tomoyo_path_truncate
),
545 LSM_HOOK_INIT(path_unlink
, tomoyo_path_unlink
),
546 LSM_HOOK_INIT(path_mkdir
, tomoyo_path_mkdir
),
547 LSM_HOOK_INIT(path_rmdir
, tomoyo_path_rmdir
),
548 LSM_HOOK_INIT(path_symlink
, tomoyo_path_symlink
),
549 LSM_HOOK_INIT(path_mknod
, tomoyo_path_mknod
),
550 LSM_HOOK_INIT(path_link
, tomoyo_path_link
),
551 LSM_HOOK_INIT(path_rename
, tomoyo_path_rename
),
552 LSM_HOOK_INIT(inode_getattr
, tomoyo_inode_getattr
),
553 LSM_HOOK_INIT(file_ioctl
, tomoyo_file_ioctl
),
554 LSM_HOOK_INIT(path_chmod
, tomoyo_path_chmod
),
555 LSM_HOOK_INIT(path_chown
, tomoyo_path_chown
),
556 LSM_HOOK_INIT(path_chroot
, tomoyo_path_chroot
),
557 LSM_HOOK_INIT(sb_mount
, tomoyo_sb_mount
),
558 LSM_HOOK_INIT(sb_umount
, tomoyo_sb_umount
),
559 LSM_HOOK_INIT(sb_pivotroot
, tomoyo_sb_pivotroot
),
560 LSM_HOOK_INIT(socket_bind
, tomoyo_socket_bind
),
561 LSM_HOOK_INIT(socket_connect
, tomoyo_socket_connect
),
562 LSM_HOOK_INIT(socket_listen
, tomoyo_socket_listen
),
563 LSM_HOOK_INIT(socket_sendmsg
, tomoyo_socket_sendmsg
),
567 DEFINE_SRCU(tomoyo_ss
);
569 int tomoyo_enabled __lsm_ro_after_init
= 1;
572 * tomoyo_init - Register TOMOYO Linux as a LSM module.
576 static int __init
tomoyo_init(void)
578 struct tomoyo_task
*s
= tomoyo_task(current
);
580 /* register ourselves with the security framework */
581 security_add_hooks(tomoyo_hooks
, ARRAY_SIZE(tomoyo_hooks
),
583 pr_info("TOMOYO Linux initialized\n");
584 s
->domain_info
= &tomoyo_kernel_domain
;
585 atomic_inc(&tomoyo_kernel_domain
.users
);
586 s
->old_domain_info
= NULL
;
592 DEFINE_LSM(tomoyo
) = {
594 .enabled
= &tomoyo_enabled
,
595 .flags
= LSM_FLAG_LEGACY_MAJOR
,
596 .blobs
= &tomoyo_blob_sizes
,