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
)
26 * tomoyo_cred_prepare - Target for security_prepare_creds().
28 * @new: Pointer to "struct cred".
29 * @old: Pointer to "struct cred".
30 * @gfp: Memory allocation flags.
34 static int tomoyo_cred_prepare(struct cred
*new, const struct cred
*old
,
37 struct tomoyo_domain_info
*domain
= old
->security
;
38 new->security
= domain
;
40 atomic_inc(&domain
->users
);
45 * tomoyo_cred_transfer - Target for security_transfer_creds().
47 * @new: Pointer to "struct cred".
48 * @old: Pointer to "struct cred".
50 static void tomoyo_cred_transfer(struct cred
*new, const struct cred
*old
)
52 tomoyo_cred_prepare(new, old
, 0);
56 * tomoyo_cred_free - Target for security_cred_free().
58 * @cred: Pointer to "struct cred".
60 static void tomoyo_cred_free(struct cred
*cred
)
62 struct tomoyo_domain_info
*domain
= cred
->security
;
64 atomic_dec(&domain
->users
);
68 * tomoyo_bprm_set_creds - Target for security_bprm_set_creds().
70 * @bprm: Pointer to "struct linux_binprm".
72 * Returns 0 on success, negative value otherwise.
74 static int tomoyo_bprm_set_creds(struct linux_binprm
*bprm
)
77 * Do only if this function is called for the first time of an execve
80 if (bprm
->called_set_creds
)
82 #ifndef CONFIG_SECURITY_TOMOYO_OMIT_USERSPACE_LOADER
84 * Load policy if /sbin/tomoyo-init exists and /sbin/init is requested
87 if (!tomoyo_policy_loaded
)
88 tomoyo_load_policy(bprm
->filename
);
91 * Release reference to "struct tomoyo_domain_info" stored inside
92 * "bprm->cred->security". New reference to "struct tomoyo_domain_info"
93 * stored inside "bprm->cred->security" will be acquired later inside
94 * tomoyo_find_next_domain().
96 atomic_dec(&((struct tomoyo_domain_info
*)
97 bprm
->cred
->security
)->users
);
99 * Tell tomoyo_bprm_check_security() is called for the first time of an
102 bprm
->cred
->security
= NULL
;
107 * tomoyo_bprm_check_security - Target for security_bprm_check().
109 * @bprm: Pointer to "struct linux_binprm".
111 * Returns 0 on success, negative value otherwise.
113 static int tomoyo_bprm_check_security(struct linux_binprm
*bprm
)
115 struct tomoyo_domain_info
*domain
= bprm
->cred
->security
;
118 * Execute permission is checked against pathname passed to do_execve()
119 * using current domain.
122 const int idx
= tomoyo_read_lock();
123 const int err
= tomoyo_find_next_domain(bprm
);
124 tomoyo_read_unlock(idx
);
128 * Read permission is checked against interpreters using next domain.
130 return tomoyo_check_open_permission(domain
, &bprm
->file
->f_path
,
135 * tomoyo_inode_getattr - Target for security_inode_getattr().
137 * @mnt: Pointer to "struct vfsmount".
138 * @dentry: Pointer to "struct dentry".
140 * Returns 0 on success, negative value otherwise.
142 static int tomoyo_inode_getattr(const struct path
*path
)
144 return tomoyo_path_perm(TOMOYO_TYPE_GETATTR
, path
, NULL
);
148 * tomoyo_path_truncate - Target for security_path_truncate().
150 * @path: Pointer to "struct path".
152 * Returns 0 on success, negative value otherwise.
154 static int tomoyo_path_truncate(const struct path
*path
)
156 return tomoyo_path_perm(TOMOYO_TYPE_TRUNCATE
, path
, NULL
);
160 * tomoyo_path_unlink - Target for security_path_unlink().
162 * @parent: Pointer to "struct path".
163 * @dentry: Pointer to "struct dentry".
165 * Returns 0 on success, negative value otherwise.
167 static int tomoyo_path_unlink(const struct path
*parent
, struct dentry
*dentry
)
169 struct path path
= { .mnt
= parent
->mnt
, .dentry
= dentry
};
170 return tomoyo_path_perm(TOMOYO_TYPE_UNLINK
, &path
, NULL
);
174 * tomoyo_path_mkdir - Target for security_path_mkdir().
176 * @parent: Pointer to "struct path".
177 * @dentry: Pointer to "struct dentry".
178 * @mode: DAC permission mode.
180 * Returns 0 on success, negative value otherwise.
182 static int tomoyo_path_mkdir(const struct path
*parent
, struct dentry
*dentry
,
185 struct path path
= { .mnt
= parent
->mnt
, .dentry
= dentry
};
186 return tomoyo_path_number_perm(TOMOYO_TYPE_MKDIR
, &path
,
191 * tomoyo_path_rmdir - Target for security_path_rmdir().
193 * @parent: Pointer to "struct path".
194 * @dentry: Pointer to "struct dentry".
196 * Returns 0 on success, negative value otherwise.
198 static int tomoyo_path_rmdir(const struct path
*parent
, struct dentry
*dentry
)
200 struct path path
= { .mnt
= parent
->mnt
, .dentry
= dentry
};
201 return tomoyo_path_perm(TOMOYO_TYPE_RMDIR
, &path
, NULL
);
205 * tomoyo_path_symlink - Target for security_path_symlink().
207 * @parent: Pointer to "struct path".
208 * @dentry: Pointer to "struct dentry".
209 * @old_name: Symlink's content.
211 * Returns 0 on success, negative value otherwise.
213 static int tomoyo_path_symlink(const struct path
*parent
, struct dentry
*dentry
,
214 const char *old_name
)
216 struct path path
= { .mnt
= parent
->mnt
, .dentry
= dentry
};
217 return tomoyo_path_perm(TOMOYO_TYPE_SYMLINK
, &path
, old_name
);
221 * tomoyo_path_mknod - Target for security_path_mknod().
223 * @parent: Pointer to "struct path".
224 * @dentry: Pointer to "struct dentry".
225 * @mode: DAC permission mode.
226 * @dev: Device attributes.
228 * Returns 0 on success, negative value otherwise.
230 static int tomoyo_path_mknod(const struct path
*parent
, struct dentry
*dentry
,
231 umode_t mode
, unsigned int dev
)
233 struct path path
= { .mnt
= parent
->mnt
, .dentry
= dentry
};
234 int type
= TOMOYO_TYPE_CREATE
;
235 const unsigned int perm
= mode
& S_IALLUGO
;
237 switch (mode
& S_IFMT
) {
239 type
= TOMOYO_TYPE_MKCHAR
;
242 type
= TOMOYO_TYPE_MKBLOCK
;
247 return tomoyo_mkdev_perm(type
, &path
, perm
, dev
);
249 switch (mode
& S_IFMT
) {
251 type
= TOMOYO_TYPE_MKFIFO
;
254 type
= TOMOYO_TYPE_MKSOCK
;
257 return tomoyo_path_number_perm(type
, &path
, perm
);
261 * tomoyo_path_link - Target for security_path_link().
263 * @old_dentry: Pointer to "struct dentry".
264 * @new_dir: Pointer to "struct path".
265 * @new_dentry: Pointer to "struct dentry".
267 * Returns 0 on success, negative value otherwise.
269 static int tomoyo_path_link(struct dentry
*old_dentry
, const struct path
*new_dir
,
270 struct dentry
*new_dentry
)
272 struct path path1
= { .mnt
= new_dir
->mnt
, .dentry
= old_dentry
};
273 struct path path2
= { .mnt
= new_dir
->mnt
, .dentry
= new_dentry
};
274 return tomoyo_path2_perm(TOMOYO_TYPE_LINK
, &path1
, &path2
);
278 * tomoyo_path_rename - Target for security_path_rename().
280 * @old_parent: Pointer to "struct path".
281 * @old_dentry: Pointer to "struct dentry".
282 * @new_parent: Pointer to "struct path".
283 * @new_dentry: Pointer to "struct dentry".
285 * Returns 0 on success, negative value otherwise.
287 static int tomoyo_path_rename(const struct path
*old_parent
,
288 struct dentry
*old_dentry
,
289 const struct path
*new_parent
,
290 struct dentry
*new_dentry
)
292 struct path path1
= { .mnt
= old_parent
->mnt
, .dentry
= old_dentry
};
293 struct path path2
= { .mnt
= new_parent
->mnt
, .dentry
= new_dentry
};
294 return tomoyo_path2_perm(TOMOYO_TYPE_RENAME
, &path1
, &path2
);
298 * tomoyo_file_fcntl - Target for security_file_fcntl().
300 * @file: Pointer to "struct file".
301 * @cmd: Command for fcntl().
302 * @arg: Argument for @cmd.
304 * Returns 0 on success, negative value otherwise.
306 static int tomoyo_file_fcntl(struct file
*file
, unsigned int cmd
,
309 if (!(cmd
== F_SETFL
&& ((arg
^ file
->f_flags
) & O_APPEND
)))
311 return tomoyo_check_open_permission(tomoyo_domain(), &file
->f_path
,
312 O_WRONLY
| (arg
& O_APPEND
));
316 * tomoyo_file_open - Target for security_file_open().
318 * @f: Pointer to "struct file".
319 * @cred: Pointer to "struct cred".
321 * Returns 0 on success, negative value otherwise.
323 static int tomoyo_file_open(struct file
*f
, const struct cred
*cred
)
325 int flags
= f
->f_flags
;
326 /* Don't check read permission here if called from do_execve(). */
327 if (current
->in_execve
)
329 return tomoyo_check_open_permission(tomoyo_domain(), &f
->f_path
, flags
);
333 * tomoyo_file_ioctl - Target for security_file_ioctl().
335 * @file: Pointer to "struct file".
336 * @cmd: Command for ioctl().
337 * @arg: Argument for @cmd.
339 * Returns 0 on success, negative value otherwise.
341 static int tomoyo_file_ioctl(struct file
*file
, unsigned int cmd
,
344 return tomoyo_path_number_perm(TOMOYO_TYPE_IOCTL
, &file
->f_path
, cmd
);
348 * tomoyo_path_chmod - Target for security_path_chmod().
350 * @path: Pointer to "struct path".
351 * @mode: DAC permission mode.
353 * Returns 0 on success, negative value otherwise.
355 static int tomoyo_path_chmod(const struct path
*path
, umode_t mode
)
357 return tomoyo_path_number_perm(TOMOYO_TYPE_CHMOD
, path
,
362 * tomoyo_path_chown - Target for security_path_chown().
364 * @path: Pointer to "struct path".
368 * Returns 0 on success, negative value otherwise.
370 static int tomoyo_path_chown(const struct path
*path
, kuid_t uid
, kgid_t gid
)
374 error
= tomoyo_path_number_perm(TOMOYO_TYPE_CHOWN
, path
,
375 from_kuid(&init_user_ns
, uid
));
376 if (!error
&& gid_valid(gid
))
377 error
= tomoyo_path_number_perm(TOMOYO_TYPE_CHGRP
, path
,
378 from_kgid(&init_user_ns
, gid
));
383 * tomoyo_path_chroot - Target for security_path_chroot().
385 * @path: Pointer to "struct path".
387 * Returns 0 on success, negative value otherwise.
389 static int tomoyo_path_chroot(const struct path
*path
)
391 return tomoyo_path_perm(TOMOYO_TYPE_CHROOT
, path
, NULL
);
395 * tomoyo_sb_mount - Target for security_sb_mount().
397 * @dev_name: Name of device file. Maybe NULL.
398 * @path: Pointer to "struct path".
399 * @type: Name of filesystem type. Maybe NULL.
400 * @flags: Mount options.
401 * @data: Optional data. Maybe NULL.
403 * Returns 0 on success, negative value otherwise.
405 static int tomoyo_sb_mount(const char *dev_name
, const struct path
*path
,
406 const char *type
, unsigned long flags
, void *data
)
408 return tomoyo_mount_permission(dev_name
, path
, type
, flags
, data
);
412 * tomoyo_sb_umount - Target for security_sb_umount().
414 * @mnt: Pointer to "struct vfsmount".
415 * @flags: Unmount options.
417 * Returns 0 on success, negative value otherwise.
419 static int tomoyo_sb_umount(struct vfsmount
*mnt
, int flags
)
421 struct path path
= { .mnt
= mnt
, .dentry
= mnt
->mnt_root
};
422 return tomoyo_path_perm(TOMOYO_TYPE_UMOUNT
, &path
, NULL
);
426 * tomoyo_sb_pivotroot - Target for security_sb_pivotroot().
428 * @old_path: Pointer to "struct path".
429 * @new_path: Pointer to "struct path".
431 * Returns 0 on success, negative value otherwise.
433 static int tomoyo_sb_pivotroot(const struct path
*old_path
, const struct path
*new_path
)
435 return tomoyo_path2_perm(TOMOYO_TYPE_PIVOT_ROOT
, new_path
, old_path
);
439 * tomoyo_socket_listen - Check permission for listen().
441 * @sock: Pointer to "struct socket".
442 * @backlog: Backlog parameter.
444 * Returns 0 on success, negative value otherwise.
446 static int tomoyo_socket_listen(struct socket
*sock
, int backlog
)
448 return tomoyo_socket_listen_permission(sock
);
452 * tomoyo_socket_connect - Check permission for connect().
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_connect(struct socket
*sock
, struct sockaddr
*addr
,
463 return tomoyo_socket_connect_permission(sock
, addr
, addr_len
);
467 * tomoyo_socket_bind - Check permission for bind().
469 * @sock: Pointer to "struct socket".
470 * @addr: Pointer to "struct sockaddr".
471 * @addr_len: Size of @addr.
473 * Returns 0 on success, negative value otherwise.
475 static int tomoyo_socket_bind(struct socket
*sock
, struct sockaddr
*addr
,
478 return tomoyo_socket_bind_permission(sock
, addr
, addr_len
);
482 * tomoyo_socket_sendmsg - Check permission for sendmsg().
484 * @sock: Pointer to "struct socket".
485 * @msg: Pointer to "struct msghdr".
486 * @size: Size of message.
488 * Returns 0 on success, negative value otherwise.
490 static int tomoyo_socket_sendmsg(struct socket
*sock
, struct msghdr
*msg
,
493 return tomoyo_socket_sendmsg_permission(sock
, msg
, size
);
497 * tomoyo_security_ops is a "struct security_operations" which is used for
498 * registering TOMOYO.
500 static struct security_hook_list tomoyo_hooks
[] __lsm_ro_after_init
= {
501 LSM_HOOK_INIT(cred_alloc_blank
, tomoyo_cred_alloc_blank
),
502 LSM_HOOK_INIT(cred_prepare
, tomoyo_cred_prepare
),
503 LSM_HOOK_INIT(cred_transfer
, tomoyo_cred_transfer
),
504 LSM_HOOK_INIT(cred_free
, tomoyo_cred_free
),
505 LSM_HOOK_INIT(bprm_set_creds
, tomoyo_bprm_set_creds
),
506 LSM_HOOK_INIT(bprm_check_security
, tomoyo_bprm_check_security
),
507 LSM_HOOK_INIT(file_fcntl
, tomoyo_file_fcntl
),
508 LSM_HOOK_INIT(file_open
, tomoyo_file_open
),
509 LSM_HOOK_INIT(path_truncate
, tomoyo_path_truncate
),
510 LSM_HOOK_INIT(path_unlink
, tomoyo_path_unlink
),
511 LSM_HOOK_INIT(path_mkdir
, tomoyo_path_mkdir
),
512 LSM_HOOK_INIT(path_rmdir
, tomoyo_path_rmdir
),
513 LSM_HOOK_INIT(path_symlink
, tomoyo_path_symlink
),
514 LSM_HOOK_INIT(path_mknod
, tomoyo_path_mknod
),
515 LSM_HOOK_INIT(path_link
, tomoyo_path_link
),
516 LSM_HOOK_INIT(path_rename
, tomoyo_path_rename
),
517 LSM_HOOK_INIT(inode_getattr
, tomoyo_inode_getattr
),
518 LSM_HOOK_INIT(file_ioctl
, tomoyo_file_ioctl
),
519 LSM_HOOK_INIT(path_chmod
, tomoyo_path_chmod
),
520 LSM_HOOK_INIT(path_chown
, tomoyo_path_chown
),
521 LSM_HOOK_INIT(path_chroot
, tomoyo_path_chroot
),
522 LSM_HOOK_INIT(sb_mount
, tomoyo_sb_mount
),
523 LSM_HOOK_INIT(sb_umount
, tomoyo_sb_umount
),
524 LSM_HOOK_INIT(sb_pivotroot
, tomoyo_sb_pivotroot
),
525 LSM_HOOK_INIT(socket_bind
, tomoyo_socket_bind
),
526 LSM_HOOK_INIT(socket_connect
, tomoyo_socket_connect
),
527 LSM_HOOK_INIT(socket_listen
, tomoyo_socket_listen
),
528 LSM_HOOK_INIT(socket_sendmsg
, tomoyo_socket_sendmsg
),
532 DEFINE_SRCU(tomoyo_ss
);
535 * tomoyo_init - Register TOMOYO Linux as a LSM module.
539 static int __init
tomoyo_init(void)
541 struct cred
*cred
= (struct cred
*) current_cred();
543 if (!security_module_enable("tomoyo"))
545 /* register ourselves with the security framework */
546 security_add_hooks(tomoyo_hooks
, ARRAY_SIZE(tomoyo_hooks
), "tomoyo");
547 printk(KERN_INFO
"TOMOYO Linux initialized\n");
548 cred
->security
= &tomoyo_kernel_domain
;
553 security_initcall(tomoyo_init
);