2 * security/tomoyo/tomoyo.c
4 * Copyright (C) 2005-2011 NTT DATA CORPORATION
7 #include <linux/security.h>
11 * tomoyo_cred_alloc_blank - Target for security_cred_alloc_blank().
13 * @new: Pointer to "struct cred".
14 * @gfp: Memory allocation flags.
18 static int tomoyo_cred_alloc_blank(struct cred
*new, gfp_t gfp
)
25 * tomoyo_cred_prepare - Target for security_prepare_creds().
27 * @new: Pointer to "struct cred".
28 * @old: Pointer to "struct cred".
29 * @gfp: Memory allocation flags.
33 static int tomoyo_cred_prepare(struct cred
*new, const struct cred
*old
,
36 struct tomoyo_domain_info
*domain
= old
->security
;
37 new->security
= domain
;
39 atomic_inc(&domain
->users
);
44 * tomoyo_cred_transfer - Target for security_transfer_creds().
46 * @new: Pointer to "struct cred".
47 * @old: Pointer to "struct cred".
49 static void tomoyo_cred_transfer(struct cred
*new, const struct cred
*old
)
51 tomoyo_cred_prepare(new, old
, 0);
55 * tomoyo_cred_free - Target for security_cred_free().
57 * @cred: Pointer to "struct cred".
59 static void tomoyo_cred_free(struct cred
*cred
)
61 struct tomoyo_domain_info
*domain
= cred
->security
;
63 atomic_dec(&domain
->users
);
67 * tomoyo_bprm_set_creds - Target for security_bprm_set_creds().
69 * @bprm: Pointer to "struct linux_binprm".
71 * Returns 0 on success, negative value otherwise.
73 static int tomoyo_bprm_set_creds(struct linux_binprm
*bprm
)
77 rc
= cap_bprm_set_creds(bprm
);
82 * Do only if this function is called for the first time of an execve
85 if (bprm
->cred_prepared
)
87 #ifndef CONFIG_SECURITY_TOMOYO_OMIT_USERSPACE_LOADER
89 * Load policy if /sbin/tomoyo-init exists and /sbin/init is requested
92 if (!tomoyo_policy_loaded
)
93 tomoyo_load_policy(bprm
->filename
);
96 * Release reference to "struct tomoyo_domain_info" stored inside
97 * "bprm->cred->security". New reference to "struct tomoyo_domain_info"
98 * stored inside "bprm->cred->security" will be acquired later inside
99 * tomoyo_find_next_domain().
101 atomic_dec(&((struct tomoyo_domain_info
*)
102 bprm
->cred
->security
)->users
);
104 * Tell tomoyo_bprm_check_security() is called for the first time of an
107 bprm
->cred
->security
= NULL
;
112 * tomoyo_bprm_check_security - Target for security_bprm_check().
114 * @bprm: Pointer to "struct linux_binprm".
116 * Returns 0 on success, negative value otherwise.
118 static int tomoyo_bprm_check_security(struct linux_binprm
*bprm
)
120 struct tomoyo_domain_info
*domain
= bprm
->cred
->security
;
123 * Execute permission is checked against pathname passed to do_execve()
124 * using current domain.
127 const int idx
= tomoyo_read_lock();
128 const int err
= tomoyo_find_next_domain(bprm
);
129 tomoyo_read_unlock(idx
);
133 * Read permission is checked against interpreters using next domain.
135 return tomoyo_check_open_permission(domain
, &bprm
->file
->f_path
,
140 * tomoyo_inode_getattr - Target for security_inode_getattr().
142 * @mnt: Pointer to "struct vfsmount".
143 * @dentry: Pointer to "struct dentry".
145 * Returns 0 on success, negative value otherwise.
147 static int tomoyo_inode_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
)
149 struct path path
= { mnt
, dentry
};
150 return tomoyo_path_perm(TOMOYO_TYPE_GETATTR
, &path
, NULL
);
154 * tomoyo_path_truncate - Target for security_path_truncate().
156 * @path: Pointer to "struct path".
158 * Returns 0 on success, negative value otherwise.
160 static int tomoyo_path_truncate(struct path
*path
)
162 return tomoyo_path_perm(TOMOYO_TYPE_TRUNCATE
, path
, NULL
);
166 * tomoyo_path_unlink - Target for security_path_unlink().
168 * @parent: Pointer to "struct path".
169 * @dentry: Pointer to "struct dentry".
171 * Returns 0 on success, negative value otherwise.
173 static int tomoyo_path_unlink(struct path
*parent
, struct dentry
*dentry
)
175 struct path path
= { parent
->mnt
, dentry
};
176 return tomoyo_path_perm(TOMOYO_TYPE_UNLINK
, &path
, NULL
);
180 * tomoyo_path_mkdir - Target for security_path_mkdir().
182 * @parent: Pointer to "struct path".
183 * @dentry: Pointer to "struct dentry".
184 * @mode: DAC permission mode.
186 * Returns 0 on success, negative value otherwise.
188 static int tomoyo_path_mkdir(struct path
*parent
, struct dentry
*dentry
,
191 struct path path
= { parent
->mnt
, dentry
};
192 return tomoyo_path_number_perm(TOMOYO_TYPE_MKDIR
, &path
,
197 * tomoyo_path_rmdir - Target for security_path_rmdir().
199 * @parent: Pointer to "struct path".
200 * @dentry: Pointer to "struct dentry".
202 * Returns 0 on success, negative value otherwise.
204 static int tomoyo_path_rmdir(struct path
*parent
, struct dentry
*dentry
)
206 struct path path
= { parent
->mnt
, dentry
};
207 return tomoyo_path_perm(TOMOYO_TYPE_RMDIR
, &path
, NULL
);
211 * tomoyo_path_symlink - Target for security_path_symlink().
213 * @parent: Pointer to "struct path".
214 * @dentry: Pointer to "struct dentry".
215 * @old_name: Symlink's content.
217 * Returns 0 on success, negative value otherwise.
219 static int tomoyo_path_symlink(struct path
*parent
, struct dentry
*dentry
,
220 const char *old_name
)
222 struct path path
= { parent
->mnt
, dentry
};
223 return tomoyo_path_perm(TOMOYO_TYPE_SYMLINK
, &path
, old_name
);
227 * tomoyo_path_mknod - Target for security_path_mknod().
229 * @parent: Pointer to "struct path".
230 * @dentry: Pointer to "struct dentry".
231 * @mode: DAC permission mode.
232 * @dev: Device attributes.
234 * Returns 0 on success, negative value otherwise.
236 static int tomoyo_path_mknod(struct path
*parent
, struct dentry
*dentry
,
237 int mode
, unsigned int dev
)
239 struct path path
= { parent
->mnt
, dentry
};
240 int type
= TOMOYO_TYPE_CREATE
;
241 const unsigned int perm
= mode
& S_IALLUGO
;
243 switch (mode
& S_IFMT
) {
245 type
= TOMOYO_TYPE_MKCHAR
;
248 type
= TOMOYO_TYPE_MKBLOCK
;
253 return tomoyo_mkdev_perm(type
, &path
, perm
, dev
);
255 switch (mode
& S_IFMT
) {
257 type
= TOMOYO_TYPE_MKFIFO
;
260 type
= TOMOYO_TYPE_MKSOCK
;
263 return tomoyo_path_number_perm(type
, &path
, perm
);
267 * tomoyo_path_link - Target for security_path_link().
269 * @old_dentry: Pointer to "struct dentry".
270 * @new_dir: Pointer to "struct path".
271 * @new_dentry: Pointer to "struct dentry".
273 * Returns 0 on success, negative value otherwise.
275 static int tomoyo_path_link(struct dentry
*old_dentry
, struct path
*new_dir
,
276 struct dentry
*new_dentry
)
278 struct path path1
= { new_dir
->mnt
, old_dentry
};
279 struct path path2
= { new_dir
->mnt
, new_dentry
};
280 return tomoyo_path2_perm(TOMOYO_TYPE_LINK
, &path1
, &path2
);
284 * tomoyo_path_rename - Target for security_path_rename().
286 * @old_parent: Pointer to "struct path".
287 * @old_dentry: Pointer to "struct dentry".
288 * @new_parent: Pointer to "struct path".
289 * @new_dentry: Pointer to "struct dentry".
291 * Returns 0 on success, negative value otherwise.
293 static int tomoyo_path_rename(struct path
*old_parent
,
294 struct dentry
*old_dentry
,
295 struct path
*new_parent
,
296 struct dentry
*new_dentry
)
298 struct path path1
= { old_parent
->mnt
, old_dentry
};
299 struct path path2
= { new_parent
->mnt
, new_dentry
};
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_dentry_open - Target for security_dentry_open().
324 * @f: Pointer to "struct file".
325 * @cred: Pointer to "struct cred".
327 * Returns 0 on success, negative value otherwise.
329 static int tomoyo_dentry_open(struct file
*f
, const struct cred
*cred
)
331 int flags
= f
->f_flags
;
332 /* Don't check read permission here if called from do_execve(). */
333 if (current
->in_execve
)
335 return tomoyo_check_open_permission(tomoyo_domain(), &f
->f_path
, flags
);
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 * @dentry: Pointer to "struct dentry".
357 * @mnt: Pointer to "struct vfsmount".
358 * @mode: DAC permission mode.
360 * Returns 0 on success, negative value otherwise.
362 static int tomoyo_path_chmod(struct dentry
*dentry
, struct vfsmount
*mnt
,
365 struct path path
= { mnt
, dentry
};
366 return tomoyo_path_number_perm(TOMOYO_TYPE_CHMOD
, &path
,
371 * tomoyo_path_chown - Target for security_path_chown().
373 * @path: Pointer to "struct path".
377 * Returns 0 on success, negative value otherwise.
379 static int tomoyo_path_chown(struct path
*path
, uid_t uid
, gid_t gid
)
382 if (uid
!= (uid_t
) -1)
383 error
= tomoyo_path_number_perm(TOMOYO_TYPE_CHOWN
, path
, uid
);
384 if (!error
&& gid
!= (gid_t
) -1)
385 error
= tomoyo_path_number_perm(TOMOYO_TYPE_CHGRP
, path
, 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(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(char *dev_name
, struct path
*path
,
413 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
->mnt_root
};
429 return tomoyo_path_perm(TOMOYO_TYPE_UMOUNT
, &path
, NULL
);
433 * tomoyo_sb_pivotroot - Target for security_sb_pivotroot().
435 * @old_path: Pointer to "struct path".
436 * @new_path: Pointer to "struct path".
438 * Returns 0 on success, negative value otherwise.
440 static int tomoyo_sb_pivotroot(struct path
*old_path
, struct path
*new_path
)
442 return tomoyo_path2_perm(TOMOYO_TYPE_PIVOT_ROOT
, new_path
, old_path
);
446 * tomoyo_socket_listen - Check permission for listen().
448 * @sock: Pointer to "struct socket".
449 * @backlog: Backlog parameter.
451 * Returns 0 on success, negative value otherwise.
453 static int tomoyo_socket_listen(struct socket
*sock
, int backlog
)
455 return tomoyo_socket_listen_permission(sock
);
459 * tomoyo_socket_connect - Check permission for connect().
461 * @sock: Pointer to "struct socket".
462 * @addr: Pointer to "struct sockaddr".
463 * @addr_len: Size of @addr.
465 * Returns 0 on success, negative value otherwise.
467 static int tomoyo_socket_connect(struct socket
*sock
, struct sockaddr
*addr
,
470 return tomoyo_socket_connect_permission(sock
, addr
, addr_len
);
474 * tomoyo_socket_bind - Check permission for bind().
476 * @sock: Pointer to "struct socket".
477 * @addr: Pointer to "struct sockaddr".
478 * @addr_len: Size of @addr.
480 * Returns 0 on success, negative value otherwise.
482 static int tomoyo_socket_bind(struct socket
*sock
, struct sockaddr
*addr
,
485 return tomoyo_socket_bind_permission(sock
, addr
, addr_len
);
489 * tomoyo_socket_sendmsg - Check permission for sendmsg().
491 * @sock: Pointer to "struct socket".
492 * @msg: Pointer to "struct msghdr".
493 * @size: Size of message.
495 * Returns 0 on success, negative value otherwise.
497 static int tomoyo_socket_sendmsg(struct socket
*sock
, struct msghdr
*msg
,
500 return tomoyo_socket_sendmsg_permission(sock
, msg
, size
);
504 * tomoyo_security_ops is a "struct security_operations" which is used for
505 * registering TOMOYO.
507 static struct security_operations tomoyo_security_ops
= {
509 .cred_alloc_blank
= tomoyo_cred_alloc_blank
,
510 .cred_prepare
= tomoyo_cred_prepare
,
511 .cred_transfer
= tomoyo_cred_transfer
,
512 .cred_free
= tomoyo_cred_free
,
513 .bprm_set_creds
= tomoyo_bprm_set_creds
,
514 .bprm_check_security
= tomoyo_bprm_check_security
,
515 .file_fcntl
= tomoyo_file_fcntl
,
516 .dentry_open
= tomoyo_dentry_open
,
517 .path_truncate
= tomoyo_path_truncate
,
518 .path_unlink
= tomoyo_path_unlink
,
519 .path_mkdir
= tomoyo_path_mkdir
,
520 .path_rmdir
= tomoyo_path_rmdir
,
521 .path_symlink
= tomoyo_path_symlink
,
522 .path_mknod
= tomoyo_path_mknod
,
523 .path_link
= tomoyo_path_link
,
524 .path_rename
= tomoyo_path_rename
,
525 .inode_getattr
= tomoyo_inode_getattr
,
526 .file_ioctl
= tomoyo_file_ioctl
,
527 .path_chmod
= tomoyo_path_chmod
,
528 .path_chown
= tomoyo_path_chown
,
529 .path_chroot
= tomoyo_path_chroot
,
530 .sb_mount
= tomoyo_sb_mount
,
531 .sb_umount
= tomoyo_sb_umount
,
532 .sb_pivotroot
= tomoyo_sb_pivotroot
,
533 .socket_bind
= tomoyo_socket_bind
,
534 .socket_connect
= tomoyo_socket_connect
,
535 .socket_listen
= tomoyo_socket_listen
,
536 .socket_sendmsg
= tomoyo_socket_sendmsg
,
540 struct srcu_struct tomoyo_ss
;
543 * tomoyo_init - Register TOMOYO Linux as a LSM module.
547 static int __init
tomoyo_init(void)
549 struct cred
*cred
= (struct cred
*) current_cred();
551 if (!security_module_enable(&tomoyo_security_ops
))
553 /* register ourselves with the security framework */
554 if (register_security(&tomoyo_security_ops
) ||
555 init_srcu_struct(&tomoyo_ss
))
556 panic("Failure registering TOMOYO Linux");
557 printk(KERN_INFO
"TOMOYO Linux initialized\n");
558 cred
->security
= &tomoyo_kernel_domain
;
563 security_initcall(tomoyo_init
);