2 * security/tomoyo/tomoyo.c
4 * Copyright (C) 2005-2011 NTT DATA CORPORATION
7 #include <linux/lsm_hooks.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
)
76 * Do only if this function is called for the first time of an execve
79 if (bprm
->cred_prepared
)
81 #ifndef CONFIG_SECURITY_TOMOYO_OMIT_USERSPACE_LOADER
83 * Load policy if /sbin/tomoyo-init exists and /sbin/init is requested
86 if (!tomoyo_policy_loaded
)
87 tomoyo_load_policy(bprm
->filename
);
90 * Release reference to "struct tomoyo_domain_info" stored inside
91 * "bprm->cred->security". New reference to "struct tomoyo_domain_info"
92 * stored inside "bprm->cred->security" will be acquired later inside
93 * tomoyo_find_next_domain().
95 atomic_dec(&((struct tomoyo_domain_info
*)
96 bprm
->cred
->security
)->users
);
98 * Tell tomoyo_bprm_check_security() is called for the first time of an
101 bprm
->cred
->security
= NULL
;
106 * tomoyo_bprm_check_security - Target for security_bprm_check().
108 * @bprm: Pointer to "struct linux_binprm".
110 * Returns 0 on success, negative value otherwise.
112 static int tomoyo_bprm_check_security(struct linux_binprm
*bprm
)
114 struct tomoyo_domain_info
*domain
= bprm
->cred
->security
;
117 * Execute permission is checked against pathname passed to do_execve()
118 * using current domain.
121 const int idx
= tomoyo_read_lock();
122 const int err
= tomoyo_find_next_domain(bprm
);
123 tomoyo_read_unlock(idx
);
127 * Read permission is checked against interpreters using next domain.
129 return tomoyo_check_open_permission(domain
, &bprm
->file
->f_path
,
134 * tomoyo_inode_getattr - Target for security_inode_getattr().
136 * @mnt: Pointer to "struct vfsmount".
137 * @dentry: Pointer to "struct dentry".
139 * Returns 0 on success, negative value otherwise.
141 static int tomoyo_inode_getattr(const struct path
*path
)
143 return tomoyo_path_perm(TOMOYO_TYPE_GETATTR
, path
, NULL
);
147 * tomoyo_path_truncate - Target for security_path_truncate().
149 * @path: Pointer to "struct path".
151 * Returns 0 on success, negative value otherwise.
153 static int tomoyo_path_truncate(const struct path
*path
)
155 return tomoyo_path_perm(TOMOYO_TYPE_TRUNCATE
, path
, NULL
);
159 * tomoyo_path_unlink - Target for security_path_unlink().
161 * @parent: Pointer to "struct path".
162 * @dentry: Pointer to "struct dentry".
164 * Returns 0 on success, negative value otherwise.
166 static int tomoyo_path_unlink(const struct path
*parent
, struct dentry
*dentry
)
168 struct path path
= { .mnt
= parent
->mnt
, .dentry
= dentry
};
169 return tomoyo_path_perm(TOMOYO_TYPE_UNLINK
, &path
, NULL
);
173 * tomoyo_path_mkdir - Target for security_path_mkdir().
175 * @parent: Pointer to "struct path".
176 * @dentry: Pointer to "struct dentry".
177 * @mode: DAC permission mode.
179 * Returns 0 on success, negative value otherwise.
181 static int tomoyo_path_mkdir(const struct path
*parent
, struct dentry
*dentry
,
184 struct path path
= { .mnt
= parent
->mnt
, .dentry
= dentry
};
185 return tomoyo_path_number_perm(TOMOYO_TYPE_MKDIR
, &path
,
190 * tomoyo_path_rmdir - Target for security_path_rmdir().
192 * @parent: Pointer to "struct path".
193 * @dentry: Pointer to "struct dentry".
195 * Returns 0 on success, negative value otherwise.
197 static int tomoyo_path_rmdir(const struct path
*parent
, struct dentry
*dentry
)
199 struct path path
= { .mnt
= parent
->mnt
, .dentry
= dentry
};
200 return tomoyo_path_perm(TOMOYO_TYPE_RMDIR
, &path
, NULL
);
204 * tomoyo_path_symlink - Target for security_path_symlink().
206 * @parent: Pointer to "struct path".
207 * @dentry: Pointer to "struct dentry".
208 * @old_name: Symlink's content.
210 * Returns 0 on success, negative value otherwise.
212 static int tomoyo_path_symlink(const struct path
*parent
, struct dentry
*dentry
,
213 const char *old_name
)
215 struct path path
= { .mnt
= parent
->mnt
, .dentry
= dentry
};
216 return tomoyo_path_perm(TOMOYO_TYPE_SYMLINK
, &path
, old_name
);
220 * tomoyo_path_mknod - Target for security_path_mknod().
222 * @parent: Pointer to "struct path".
223 * @dentry: Pointer to "struct dentry".
224 * @mode: DAC permission mode.
225 * @dev: Device attributes.
227 * Returns 0 on success, negative value otherwise.
229 static int tomoyo_path_mknod(const struct path
*parent
, struct dentry
*dentry
,
230 umode_t mode
, unsigned int dev
)
232 struct path path
= { .mnt
= parent
->mnt
, .dentry
= dentry
};
233 int type
= TOMOYO_TYPE_CREATE
;
234 const unsigned int perm
= mode
& S_IALLUGO
;
236 switch (mode
& S_IFMT
) {
238 type
= TOMOYO_TYPE_MKCHAR
;
241 type
= TOMOYO_TYPE_MKBLOCK
;
246 return tomoyo_mkdev_perm(type
, &path
, perm
, dev
);
248 switch (mode
& S_IFMT
) {
250 type
= TOMOYO_TYPE_MKFIFO
;
253 type
= TOMOYO_TYPE_MKSOCK
;
256 return tomoyo_path_number_perm(type
, &path
, perm
);
260 * tomoyo_path_link - Target for security_path_link().
262 * @old_dentry: Pointer to "struct dentry".
263 * @new_dir: Pointer to "struct path".
264 * @new_dentry: Pointer to "struct dentry".
266 * Returns 0 on success, negative value otherwise.
268 static int tomoyo_path_link(struct dentry
*old_dentry
, const struct path
*new_dir
,
269 struct dentry
*new_dentry
)
271 struct path path1
= { .mnt
= new_dir
->mnt
, .dentry
= old_dentry
};
272 struct path path2
= { .mnt
= new_dir
->mnt
, .dentry
= new_dentry
};
273 return tomoyo_path2_perm(TOMOYO_TYPE_LINK
, &path1
, &path2
);
277 * tomoyo_path_rename - Target for security_path_rename().
279 * @old_parent: Pointer to "struct path".
280 * @old_dentry: Pointer to "struct dentry".
281 * @new_parent: Pointer to "struct path".
282 * @new_dentry: Pointer to "struct dentry".
284 * Returns 0 on success, negative value otherwise.
286 static int tomoyo_path_rename(const struct path
*old_parent
,
287 struct dentry
*old_dentry
,
288 const struct path
*new_parent
,
289 struct dentry
*new_dentry
)
291 struct path path1
= { .mnt
= old_parent
->mnt
, .dentry
= old_dentry
};
292 struct path path2
= { .mnt
= new_parent
->mnt
, .dentry
= new_dentry
};
293 return tomoyo_path2_perm(TOMOYO_TYPE_RENAME
, &path1
, &path2
);
297 * tomoyo_file_fcntl - Target for security_file_fcntl().
299 * @file: Pointer to "struct file".
300 * @cmd: Command for fcntl().
301 * @arg: Argument for @cmd.
303 * Returns 0 on success, negative value otherwise.
305 static int tomoyo_file_fcntl(struct file
*file
, unsigned int cmd
,
308 if (!(cmd
== F_SETFL
&& ((arg
^ file
->f_flags
) & O_APPEND
)))
310 return tomoyo_check_open_permission(tomoyo_domain(), &file
->f_path
,
311 O_WRONLY
| (arg
& O_APPEND
));
315 * tomoyo_file_open - Target for security_file_open().
317 * @f: Pointer to "struct file".
318 * @cred: Pointer to "struct cred".
320 * Returns 0 on success, negative value otherwise.
322 static int tomoyo_file_open(struct file
*f
, const struct cred
*cred
)
324 int flags
= f
->f_flags
;
325 /* Don't check read permission here if called from do_execve(). */
326 if (current
->in_execve
)
328 return tomoyo_check_open_permission(tomoyo_domain(), &f
->f_path
, flags
);
332 * tomoyo_file_ioctl - Target for security_file_ioctl().
334 * @file: Pointer to "struct file".
335 * @cmd: Command for ioctl().
336 * @arg: Argument for @cmd.
338 * Returns 0 on success, negative value otherwise.
340 static int tomoyo_file_ioctl(struct file
*file
, unsigned int cmd
,
343 return tomoyo_path_number_perm(TOMOYO_TYPE_IOCTL
, &file
->f_path
, cmd
);
347 * tomoyo_path_chmod - Target for security_path_chmod().
349 * @path: Pointer to "struct path".
350 * @mode: DAC permission mode.
352 * Returns 0 on success, negative value otherwise.
354 static int tomoyo_path_chmod(const struct path
*path
, umode_t mode
)
356 return tomoyo_path_number_perm(TOMOYO_TYPE_CHMOD
, path
,
361 * tomoyo_path_chown - Target for security_path_chown().
363 * @path: Pointer to "struct path".
367 * Returns 0 on success, negative value otherwise.
369 static int tomoyo_path_chown(const struct path
*path
, kuid_t uid
, kgid_t gid
)
373 error
= tomoyo_path_number_perm(TOMOYO_TYPE_CHOWN
, path
,
374 from_kuid(&init_user_ns
, uid
));
375 if (!error
&& gid_valid(gid
))
376 error
= tomoyo_path_number_perm(TOMOYO_TYPE_CHGRP
, path
,
377 from_kgid(&init_user_ns
, gid
));
382 * tomoyo_path_chroot - Target for security_path_chroot().
384 * @path: Pointer to "struct path".
386 * Returns 0 on success, negative value otherwise.
388 static int tomoyo_path_chroot(const struct path
*path
)
390 return tomoyo_path_perm(TOMOYO_TYPE_CHROOT
, path
, NULL
);
394 * tomoyo_sb_mount - Target for security_sb_mount().
396 * @dev_name: Name of device file. Maybe NULL.
397 * @path: Pointer to "struct path".
398 * @type: Name of filesystem type. Maybe NULL.
399 * @flags: Mount options.
400 * @data: Optional data. Maybe NULL.
402 * Returns 0 on success, negative value otherwise.
404 static int tomoyo_sb_mount(const char *dev_name
, const struct path
*path
,
405 const char *type
, unsigned long flags
, void *data
)
407 return tomoyo_mount_permission(dev_name
, path
, type
, flags
, data
);
411 * tomoyo_sb_umount - Target for security_sb_umount().
413 * @mnt: Pointer to "struct vfsmount".
414 * @flags: Unmount options.
416 * Returns 0 on success, negative value otherwise.
418 static int tomoyo_sb_umount(struct vfsmount
*mnt
, int flags
)
420 struct path path
= { .mnt
= mnt
, .dentry
= mnt
->mnt_root
};
421 return tomoyo_path_perm(TOMOYO_TYPE_UMOUNT
, &path
, NULL
);
425 * tomoyo_sb_pivotroot - Target for security_sb_pivotroot().
427 * @old_path: Pointer to "struct path".
428 * @new_path: Pointer to "struct path".
430 * Returns 0 on success, negative value otherwise.
432 static int tomoyo_sb_pivotroot(const struct path
*old_path
, const struct path
*new_path
)
434 return tomoyo_path2_perm(TOMOYO_TYPE_PIVOT_ROOT
, new_path
, old_path
);
438 * tomoyo_socket_listen - Check permission for listen().
440 * @sock: Pointer to "struct socket".
441 * @backlog: Backlog parameter.
443 * Returns 0 on success, negative value otherwise.
445 static int tomoyo_socket_listen(struct socket
*sock
, int backlog
)
447 return tomoyo_socket_listen_permission(sock
);
451 * tomoyo_socket_connect - Check permission for connect().
453 * @sock: Pointer to "struct socket".
454 * @addr: Pointer to "struct sockaddr".
455 * @addr_len: Size of @addr.
457 * Returns 0 on success, negative value otherwise.
459 static int tomoyo_socket_connect(struct socket
*sock
, struct sockaddr
*addr
,
462 return tomoyo_socket_connect_permission(sock
, addr
, addr_len
);
466 * tomoyo_socket_bind - Check permission for bind().
468 * @sock: Pointer to "struct socket".
469 * @addr: Pointer to "struct sockaddr".
470 * @addr_len: Size of @addr.
472 * Returns 0 on success, negative value otherwise.
474 static int tomoyo_socket_bind(struct socket
*sock
, struct sockaddr
*addr
,
477 return tomoyo_socket_bind_permission(sock
, addr
, addr_len
);
481 * tomoyo_socket_sendmsg - Check permission for sendmsg().
483 * @sock: Pointer to "struct socket".
484 * @msg: Pointer to "struct msghdr".
485 * @size: Size of message.
487 * Returns 0 on success, negative value otherwise.
489 static int tomoyo_socket_sendmsg(struct socket
*sock
, struct msghdr
*msg
,
492 return tomoyo_socket_sendmsg_permission(sock
, msg
, size
);
496 * tomoyo_security_ops is a "struct security_operations" which is used for
497 * registering TOMOYO.
499 static struct security_hook_list tomoyo_hooks
[] __lsm_ro_after_init
= {
500 LSM_HOOK_INIT(cred_alloc_blank
, tomoyo_cred_alloc_blank
),
501 LSM_HOOK_INIT(cred_prepare
, tomoyo_cred_prepare
),
502 LSM_HOOK_INIT(cred_transfer
, tomoyo_cred_transfer
),
503 LSM_HOOK_INIT(cred_free
, tomoyo_cred_free
),
504 LSM_HOOK_INIT(bprm_set_creds
, tomoyo_bprm_set_creds
),
505 LSM_HOOK_INIT(bprm_check_security
, tomoyo_bprm_check_security
),
506 LSM_HOOK_INIT(file_fcntl
, tomoyo_file_fcntl
),
507 LSM_HOOK_INIT(file_open
, tomoyo_file_open
),
508 LSM_HOOK_INIT(path_truncate
, tomoyo_path_truncate
),
509 LSM_HOOK_INIT(path_unlink
, tomoyo_path_unlink
),
510 LSM_HOOK_INIT(path_mkdir
, tomoyo_path_mkdir
),
511 LSM_HOOK_INIT(path_rmdir
, tomoyo_path_rmdir
),
512 LSM_HOOK_INIT(path_symlink
, tomoyo_path_symlink
),
513 LSM_HOOK_INIT(path_mknod
, tomoyo_path_mknod
),
514 LSM_HOOK_INIT(path_link
, tomoyo_path_link
),
515 LSM_HOOK_INIT(path_rename
, tomoyo_path_rename
),
516 LSM_HOOK_INIT(inode_getattr
, tomoyo_inode_getattr
),
517 LSM_HOOK_INIT(file_ioctl
, tomoyo_file_ioctl
),
518 LSM_HOOK_INIT(path_chmod
, tomoyo_path_chmod
),
519 LSM_HOOK_INIT(path_chown
, tomoyo_path_chown
),
520 LSM_HOOK_INIT(path_chroot
, tomoyo_path_chroot
),
521 LSM_HOOK_INIT(sb_mount
, tomoyo_sb_mount
),
522 LSM_HOOK_INIT(sb_umount
, tomoyo_sb_umount
),
523 LSM_HOOK_INIT(sb_pivotroot
, tomoyo_sb_pivotroot
),
524 LSM_HOOK_INIT(socket_bind
, tomoyo_socket_bind
),
525 LSM_HOOK_INIT(socket_connect
, tomoyo_socket_connect
),
526 LSM_HOOK_INIT(socket_listen
, tomoyo_socket_listen
),
527 LSM_HOOK_INIT(socket_sendmsg
, tomoyo_socket_sendmsg
),
531 DEFINE_SRCU(tomoyo_ss
);
534 * tomoyo_init - Register TOMOYO Linux as a LSM module.
538 static int __init
tomoyo_init(void)
540 struct cred
*cred
= (struct cred
*) current_cred();
542 if (!security_module_enable("tomoyo"))
544 /* register ourselves with the security framework */
545 security_add_hooks(tomoyo_hooks
, ARRAY_SIZE(tomoyo_hooks
), "tomoyo");
546 printk(KERN_INFO
"TOMOYO Linux initialized\n");
547 cred
->security
= &tomoyo_kernel_domain
;
552 security_initcall(tomoyo_init
);