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_cred_alloc_blank - Target for security_cred_alloc_blank().
14 * @new: Pointer to "struct cred".
15 * @gfp: Memory allocation flags.
19 static int tomoyo_cred_alloc_blank(struct cred
*new, gfp_t gfp
)
21 struct tomoyo_domain_info
**blob
= tomoyo_cred(new);
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 struct tomoyo_domain_info
**old_blob
= tomoyo_cred(old
);
40 struct tomoyo_domain_info
**new_blob
= tomoyo_cred(new);
41 struct tomoyo_domain_info
*domain
;
47 atomic_inc(&domain
->users
);
52 * tomoyo_cred_transfer - Target for security_transfer_creds().
54 * @new: Pointer to "struct cred".
55 * @old: Pointer to "struct cred".
57 static void tomoyo_cred_transfer(struct cred
*new, const struct cred
*old
)
59 tomoyo_cred_prepare(new, old
, 0);
63 * tomoyo_cred_free - Target for security_cred_free().
65 * @cred: Pointer to "struct cred".
67 static void tomoyo_cred_free(struct cred
*cred
)
69 struct tomoyo_domain_info
**blob
= tomoyo_cred(cred
);
70 struct tomoyo_domain_info
*domain
= *blob
;
73 atomic_dec(&domain
->users
);
77 * tomoyo_bprm_set_creds - Target for security_bprm_set_creds().
79 * @bprm: Pointer to "struct linux_binprm".
81 * Returns 0 on success, negative value otherwise.
83 static int tomoyo_bprm_set_creds(struct linux_binprm
*bprm
)
85 struct tomoyo_domain_info
**blob
;
86 struct tomoyo_domain_info
*domain
;
89 * Do only if this function is called for the first time of an execve
92 if (bprm
->called_set_creds
)
94 #ifndef CONFIG_SECURITY_TOMOYO_OMIT_USERSPACE_LOADER
96 * Load policy if /sbin/tomoyo-init exists and /sbin/init is requested
99 if (!tomoyo_policy_loaded
)
100 tomoyo_load_policy(bprm
->filename
);
103 * Release reference to "struct tomoyo_domain_info" stored inside
104 * "bprm->cred->security". New reference to "struct tomoyo_domain_info"
105 * stored inside "bprm->cred->security" will be acquired later inside
106 * tomoyo_find_next_domain().
108 blob
= tomoyo_cred(bprm
->cred
);
110 atomic_dec(&domain
->users
);
112 * Tell tomoyo_bprm_check_security() is called for the first time of an
120 * tomoyo_bprm_check_security - Target for security_bprm_check().
122 * @bprm: Pointer to "struct linux_binprm".
124 * Returns 0 on success, negative value otherwise.
126 static int tomoyo_bprm_check_security(struct linux_binprm
*bprm
)
128 struct tomoyo_domain_info
**blob
;
129 struct tomoyo_domain_info
*domain
;
131 blob
= tomoyo_cred(bprm
->cred
);
134 * Execute permission is checked against pathname passed to do_execve()
135 * using current domain.
138 const int idx
= tomoyo_read_lock();
139 const int err
= tomoyo_find_next_domain(bprm
);
140 tomoyo_read_unlock(idx
);
144 * Read permission is checked against interpreters using next domain.
146 return tomoyo_check_open_permission(domain
, &bprm
->file
->f_path
,
151 * tomoyo_inode_getattr - Target for security_inode_getattr().
153 * @mnt: Pointer to "struct vfsmount".
154 * @dentry: Pointer to "struct dentry".
156 * Returns 0 on success, negative value otherwise.
158 static int tomoyo_inode_getattr(const struct path
*path
)
160 return tomoyo_path_perm(TOMOYO_TYPE_GETATTR
, path
, NULL
);
164 * tomoyo_path_truncate - Target for security_path_truncate().
166 * @path: Pointer to "struct path".
168 * Returns 0 on success, negative value otherwise.
170 static int tomoyo_path_truncate(const struct path
*path
)
172 return tomoyo_path_perm(TOMOYO_TYPE_TRUNCATE
, path
, NULL
);
176 * tomoyo_path_unlink - Target for security_path_unlink().
178 * @parent: Pointer to "struct path".
179 * @dentry: Pointer to "struct dentry".
181 * Returns 0 on success, negative value otherwise.
183 static int tomoyo_path_unlink(const struct path
*parent
, struct dentry
*dentry
)
185 struct path path
= { .mnt
= parent
->mnt
, .dentry
= dentry
};
186 return tomoyo_path_perm(TOMOYO_TYPE_UNLINK
, &path
, NULL
);
190 * tomoyo_path_mkdir - Target for security_path_mkdir().
192 * @parent: Pointer to "struct path".
193 * @dentry: Pointer to "struct dentry".
194 * @mode: DAC permission mode.
196 * Returns 0 on success, negative value otherwise.
198 static int tomoyo_path_mkdir(const struct path
*parent
, struct dentry
*dentry
,
201 struct path path
= { .mnt
= parent
->mnt
, .dentry
= dentry
};
202 return tomoyo_path_number_perm(TOMOYO_TYPE_MKDIR
, &path
,
207 * tomoyo_path_rmdir - Target for security_path_rmdir().
209 * @parent: Pointer to "struct path".
210 * @dentry: Pointer to "struct dentry".
212 * Returns 0 on success, negative value otherwise.
214 static int tomoyo_path_rmdir(const struct path
*parent
, struct dentry
*dentry
)
216 struct path path
= { .mnt
= parent
->mnt
, .dentry
= dentry
};
217 return tomoyo_path_perm(TOMOYO_TYPE_RMDIR
, &path
, NULL
);
221 * tomoyo_path_symlink - Target for security_path_symlink().
223 * @parent: Pointer to "struct path".
224 * @dentry: Pointer to "struct dentry".
225 * @old_name: Symlink's content.
227 * Returns 0 on success, negative value otherwise.
229 static int tomoyo_path_symlink(const struct path
*parent
, struct dentry
*dentry
,
230 const char *old_name
)
232 struct path path
= { .mnt
= parent
->mnt
, .dentry
= dentry
};
233 return tomoyo_path_perm(TOMOYO_TYPE_SYMLINK
, &path
, old_name
);
237 * tomoyo_path_mknod - Target for security_path_mknod().
239 * @parent: Pointer to "struct path".
240 * @dentry: Pointer to "struct dentry".
241 * @mode: DAC permission mode.
242 * @dev: Device attributes.
244 * Returns 0 on success, negative value otherwise.
246 static int tomoyo_path_mknod(const struct path
*parent
, struct dentry
*dentry
,
247 umode_t mode
, unsigned int dev
)
249 struct path path
= { .mnt
= parent
->mnt
, .dentry
= dentry
};
250 int type
= TOMOYO_TYPE_CREATE
;
251 const unsigned int perm
= mode
& S_IALLUGO
;
253 switch (mode
& S_IFMT
) {
255 type
= TOMOYO_TYPE_MKCHAR
;
258 type
= TOMOYO_TYPE_MKBLOCK
;
263 return tomoyo_mkdev_perm(type
, &path
, perm
, dev
);
265 switch (mode
& S_IFMT
) {
267 type
= TOMOYO_TYPE_MKFIFO
;
270 type
= TOMOYO_TYPE_MKSOCK
;
273 return tomoyo_path_number_perm(type
, &path
, perm
);
277 * tomoyo_path_link - Target for security_path_link().
279 * @old_dentry: Pointer to "struct dentry".
280 * @new_dir: Pointer to "struct path".
281 * @new_dentry: Pointer to "struct dentry".
283 * Returns 0 on success, negative value otherwise.
285 static int tomoyo_path_link(struct dentry
*old_dentry
, const struct path
*new_dir
,
286 struct dentry
*new_dentry
)
288 struct path path1
= { .mnt
= new_dir
->mnt
, .dentry
= old_dentry
};
289 struct path path2
= { .mnt
= new_dir
->mnt
, .dentry
= new_dentry
};
290 return tomoyo_path2_perm(TOMOYO_TYPE_LINK
, &path1
, &path2
);
294 * tomoyo_path_rename - Target for security_path_rename().
296 * @old_parent: Pointer to "struct path".
297 * @old_dentry: Pointer to "struct dentry".
298 * @new_parent: Pointer to "struct path".
299 * @new_dentry: Pointer to "struct dentry".
301 * Returns 0 on success, negative value otherwise.
303 static int tomoyo_path_rename(const struct path
*old_parent
,
304 struct dentry
*old_dentry
,
305 const struct path
*new_parent
,
306 struct dentry
*new_dentry
)
308 struct path path1
= { .mnt
= old_parent
->mnt
, .dentry
= old_dentry
};
309 struct path path2
= { .mnt
= new_parent
->mnt
, .dentry
= new_dentry
};
310 return tomoyo_path2_perm(TOMOYO_TYPE_RENAME
, &path1
, &path2
);
314 * tomoyo_file_fcntl - Target for security_file_fcntl().
316 * @file: Pointer to "struct file".
317 * @cmd: Command for fcntl().
318 * @arg: Argument for @cmd.
320 * Returns 0 on success, negative value otherwise.
322 static int tomoyo_file_fcntl(struct file
*file
, unsigned int cmd
,
325 if (!(cmd
== F_SETFL
&& ((arg
^ file
->f_flags
) & O_APPEND
)))
327 return tomoyo_check_open_permission(tomoyo_domain(), &file
->f_path
,
328 O_WRONLY
| (arg
& O_APPEND
));
332 * tomoyo_file_open - Target for security_file_open().
334 * @f: Pointer to "struct file".
335 * @cred: Pointer to "struct cred".
337 * Returns 0 on success, negative value otherwise.
339 static int tomoyo_file_open(struct file
*f
, const struct cred
*cred
)
341 int flags
= f
->f_flags
;
342 /* Don't check read permission here if called from do_execve(). */
343 if (current
->in_execve
)
345 return tomoyo_check_open_permission(tomoyo_domain(), &f
->f_path
, flags
);
349 * tomoyo_file_ioctl - Target for security_file_ioctl().
351 * @file: Pointer to "struct file".
352 * @cmd: Command for ioctl().
353 * @arg: Argument for @cmd.
355 * Returns 0 on success, negative value otherwise.
357 static int tomoyo_file_ioctl(struct file
*file
, unsigned int cmd
,
360 return tomoyo_path_number_perm(TOMOYO_TYPE_IOCTL
, &file
->f_path
, cmd
);
364 * tomoyo_path_chmod - Target for security_path_chmod().
366 * @path: Pointer to "struct path".
367 * @mode: DAC permission mode.
369 * Returns 0 on success, negative value otherwise.
371 static int tomoyo_path_chmod(const struct path
*path
, umode_t mode
)
373 return tomoyo_path_number_perm(TOMOYO_TYPE_CHMOD
, path
,
378 * tomoyo_path_chown - Target for security_path_chown().
380 * @path: Pointer to "struct path".
384 * Returns 0 on success, negative value otherwise.
386 static int tomoyo_path_chown(const struct path
*path
, kuid_t uid
, kgid_t gid
)
390 error
= tomoyo_path_number_perm(TOMOYO_TYPE_CHOWN
, path
,
391 from_kuid(&init_user_ns
, uid
));
392 if (!error
&& gid_valid(gid
))
393 error
= tomoyo_path_number_perm(TOMOYO_TYPE_CHGRP
, path
,
394 from_kgid(&init_user_ns
, gid
));
399 * tomoyo_path_chroot - Target for security_path_chroot().
401 * @path: Pointer to "struct path".
403 * Returns 0 on success, negative value otherwise.
405 static int tomoyo_path_chroot(const struct path
*path
)
407 return tomoyo_path_perm(TOMOYO_TYPE_CHROOT
, path
, NULL
);
411 * tomoyo_sb_mount - Target for security_sb_mount().
413 * @dev_name: Name of device file. Maybe NULL.
414 * @path: Pointer to "struct path".
415 * @type: Name of filesystem type. Maybe NULL.
416 * @flags: Mount options.
417 * @data: Optional data. Maybe NULL.
419 * Returns 0 on success, negative value otherwise.
421 static int tomoyo_sb_mount(const char *dev_name
, const struct path
*path
,
422 const char *type
, unsigned long flags
, void *data
)
424 return tomoyo_mount_permission(dev_name
, path
, type
, flags
, data
);
428 * tomoyo_sb_umount - Target for security_sb_umount().
430 * @mnt: Pointer to "struct vfsmount".
431 * @flags: Unmount options.
433 * Returns 0 on success, negative value otherwise.
435 static int tomoyo_sb_umount(struct vfsmount
*mnt
, int flags
)
437 struct path path
= { .mnt
= mnt
, .dentry
= mnt
->mnt_root
};
438 return tomoyo_path_perm(TOMOYO_TYPE_UMOUNT
, &path
, NULL
);
442 * tomoyo_sb_pivotroot - Target for security_sb_pivotroot().
444 * @old_path: Pointer to "struct path".
445 * @new_path: Pointer to "struct path".
447 * Returns 0 on success, negative value otherwise.
449 static int tomoyo_sb_pivotroot(const struct path
*old_path
, const struct path
*new_path
)
451 return tomoyo_path2_perm(TOMOYO_TYPE_PIVOT_ROOT
, new_path
, old_path
);
455 * tomoyo_socket_listen - Check permission for listen().
457 * @sock: Pointer to "struct socket".
458 * @backlog: Backlog parameter.
460 * Returns 0 on success, negative value otherwise.
462 static int tomoyo_socket_listen(struct socket
*sock
, int backlog
)
464 return tomoyo_socket_listen_permission(sock
);
468 * tomoyo_socket_connect - Check permission for connect().
470 * @sock: Pointer to "struct socket".
471 * @addr: Pointer to "struct sockaddr".
472 * @addr_len: Size of @addr.
474 * Returns 0 on success, negative value otherwise.
476 static int tomoyo_socket_connect(struct socket
*sock
, struct sockaddr
*addr
,
479 return tomoyo_socket_connect_permission(sock
, addr
, addr_len
);
483 * tomoyo_socket_bind - Check permission for bind().
485 * @sock: Pointer to "struct socket".
486 * @addr: Pointer to "struct sockaddr".
487 * @addr_len: Size of @addr.
489 * Returns 0 on success, negative value otherwise.
491 static int tomoyo_socket_bind(struct socket
*sock
, struct sockaddr
*addr
,
494 return tomoyo_socket_bind_permission(sock
, addr
, addr_len
);
498 * tomoyo_socket_sendmsg - Check permission for sendmsg().
500 * @sock: Pointer to "struct socket".
501 * @msg: Pointer to "struct msghdr".
502 * @size: Size of message.
504 * Returns 0 on success, negative value otherwise.
506 static int tomoyo_socket_sendmsg(struct socket
*sock
, struct msghdr
*msg
,
509 return tomoyo_socket_sendmsg_permission(sock
, msg
, size
);
512 struct lsm_blob_sizes tomoyo_blob_sizes
= {
513 .lbs_cred
= sizeof(struct tomoyo_domain_info
*),
517 * tomoyo_security_ops is a "struct security_operations" which is used for
518 * registering TOMOYO.
520 static struct security_hook_list tomoyo_hooks
[] __lsm_ro_after_init
= {
521 LSM_HOOK_INIT(cred_alloc_blank
, tomoyo_cred_alloc_blank
),
522 LSM_HOOK_INIT(cred_prepare
, tomoyo_cred_prepare
),
523 LSM_HOOK_INIT(cred_transfer
, tomoyo_cred_transfer
),
524 LSM_HOOK_INIT(cred_free
, tomoyo_cred_free
),
525 LSM_HOOK_INIT(bprm_set_creds
, tomoyo_bprm_set_creds
),
526 LSM_HOOK_INIT(bprm_check_security
, tomoyo_bprm_check_security
),
527 LSM_HOOK_INIT(file_fcntl
, tomoyo_file_fcntl
),
528 LSM_HOOK_INIT(file_open
, tomoyo_file_open
),
529 LSM_HOOK_INIT(path_truncate
, tomoyo_path_truncate
),
530 LSM_HOOK_INIT(path_unlink
, tomoyo_path_unlink
),
531 LSM_HOOK_INIT(path_mkdir
, tomoyo_path_mkdir
),
532 LSM_HOOK_INIT(path_rmdir
, tomoyo_path_rmdir
),
533 LSM_HOOK_INIT(path_symlink
, tomoyo_path_symlink
),
534 LSM_HOOK_INIT(path_mknod
, tomoyo_path_mknod
),
535 LSM_HOOK_INIT(path_link
, tomoyo_path_link
),
536 LSM_HOOK_INIT(path_rename
, tomoyo_path_rename
),
537 LSM_HOOK_INIT(inode_getattr
, tomoyo_inode_getattr
),
538 LSM_HOOK_INIT(file_ioctl
, tomoyo_file_ioctl
),
539 LSM_HOOK_INIT(path_chmod
, tomoyo_path_chmod
),
540 LSM_HOOK_INIT(path_chown
, tomoyo_path_chown
),
541 LSM_HOOK_INIT(path_chroot
, tomoyo_path_chroot
),
542 LSM_HOOK_INIT(sb_mount
, tomoyo_sb_mount
),
543 LSM_HOOK_INIT(sb_umount
, tomoyo_sb_umount
),
544 LSM_HOOK_INIT(sb_pivotroot
, tomoyo_sb_pivotroot
),
545 LSM_HOOK_INIT(socket_bind
, tomoyo_socket_bind
),
546 LSM_HOOK_INIT(socket_connect
, tomoyo_socket_connect
),
547 LSM_HOOK_INIT(socket_listen
, tomoyo_socket_listen
),
548 LSM_HOOK_INIT(socket_sendmsg
, tomoyo_socket_sendmsg
),
552 DEFINE_SRCU(tomoyo_ss
);
557 * tomoyo_init - Register TOMOYO Linux as a LSM module.
561 static int __init
tomoyo_init(void)
564 struct cred
*cred
= (struct cred
*) current_cred();
565 struct tomoyo_domain_info
**blob
;
567 if (!security_module_enable("tomoyo",
568 IS_ENABLED(CONFIG_SECURITY_TOMOYO_STACKED
))) {
569 tomoyo_enabled
= false;
572 tomoyo_enabled
= true;
575 security_add_blobs(&tomoyo_blob_sizes
);
580 /* register ourselves with the security framework */
581 security_add_hooks(tomoyo_hooks
, ARRAY_SIZE(tomoyo_hooks
), "tomoyo");
582 printk(KERN_INFO
"TOMOYO Linux initialized\n");
583 lsm_early_cred(cred
);
584 blob
= tomoyo_cred(cred
);
585 *blob
= &tomoyo_kernel_domain
;
590 security_initcall(tomoyo_init
);