2 * security/tomoyo/tomoyo.c
4 * LSM hooks for TOMOYO Linux.
6 * Copyright (C) 2005-2009 NTT DATA CORPORATION
8 * Version: 2.2.0 2009/04/01
12 #include <linux/security.h>
17 static int tomoyo_cred_alloc_blank(struct cred *new, gfp_t gfp)
23 static int tomoyo_cred_prepare(struct cred *new, const struct cred *old,
27 * Since "struct tomoyo_domain_info *" is a sharable pointer,
28 * we don't need to duplicate.
30 new->security = old->security;
34 static void tomoyo_cred_transfer(struct cred *new, const struct cred *old)
37 * Since "struct tomoyo_domain_info *" is a sharable pointer,
38 * we don't need to duplicate.
40 new->security = old->security;
43 static int tomoyo_bprm_set_creds(struct linux_binprm *bprm)
47 rc = cap_bprm_set_creds(bprm);
52 * Do only if this function is called for the first time of an execve
55 if (bprm->cred_prepared)
58 * Load policy if /sbin/tomoyo-init exists and /sbin/init is requested
61 if (!tomoyo_policy_loaded)
62 tomoyo_load_policy(bprm->filename);
64 * Tell tomoyo_bprm_check_security() is called for the first time of an
67 bprm->cred->security = NULL;
71 static int tomoyo_bprm_check_security(struct linux_binprm *bprm)
73 struct tomoyo_domain_info *domain = bprm->cred->security;
76 * Execute permission is checked against pathname passed to do_execve()
77 * using current domain.
81 * We will need to protect whole execve() operation when GC
82 * starts kfree()ing "struct tomoyo_domain_info" because
83 * bprm->cred->security points to "struct tomoyo_domain_info"
84 * but "struct tomoyo_domain_info" does not have a refcounter.
86 const int idx = tomoyo_read_lock();
87 const int err = tomoyo_find_next_domain(bprm);
88 tomoyo_read_unlock(idx);
92 * Read permission is checked against interpreters using next domain.
93 * '1' is the result of open_to_namei_flags(O_RDONLY).
95 return tomoyo_check_open_permission(domain, &bprm->file->f_path, 1);
98 static int tomoyo_path_truncate(struct path *path, loff_t length,
99 unsigned int time_attrs)
101 return tomoyo_check_1path_perm(tomoyo_domain(),
102 TOMOYO_TYPE_TRUNCATE_ACL,
106 static int tomoyo_path_unlink(struct path *parent, struct dentry *dentry)
108 struct path path = { parent->mnt, dentry };
109 return tomoyo_check_1path_perm(tomoyo_domain(),
110 TOMOYO_TYPE_UNLINK_ACL,
114 static int tomoyo_path_mkdir(struct path *parent, struct dentry *dentry,
117 struct path path = { parent->mnt, dentry };
118 return tomoyo_check_1path_perm(tomoyo_domain(),
119 TOMOYO_TYPE_MKDIR_ACL,
123 static int tomoyo_path_rmdir(struct path *parent, struct dentry *dentry)
125 struct path path = { parent->mnt, dentry };
126 return tomoyo_check_1path_perm(tomoyo_domain(),
127 TOMOYO_TYPE_RMDIR_ACL,
131 static int tomoyo_path_symlink(struct path *parent, struct dentry *dentry,
132 const char *old_name)
134 struct path path = { parent->mnt, dentry };
135 return tomoyo_check_1path_perm(tomoyo_domain(),
136 TOMOYO_TYPE_SYMLINK_ACL,
140 static int tomoyo_path_mknod(struct path *parent, struct dentry *dentry,
141 int mode, unsigned int dev)
143 struct path path = { parent->mnt, dentry };
144 int type = TOMOYO_TYPE_CREATE_ACL;
146 switch (mode & S_IFMT) {
148 type = TOMOYO_TYPE_MKCHAR_ACL;
151 type = TOMOYO_TYPE_MKBLOCK_ACL;
154 type = TOMOYO_TYPE_MKFIFO_ACL;
157 type = TOMOYO_TYPE_MKSOCK_ACL;
160 return tomoyo_check_1path_perm(tomoyo_domain(),
164 static int tomoyo_path_link(struct dentry *old_dentry, struct path *new_dir,
165 struct dentry *new_dentry)
167 struct path path1 = { new_dir->mnt, old_dentry };
168 struct path path2 = { new_dir->mnt, new_dentry };
169 return tomoyo_check_2path_perm(tomoyo_domain(),
170 TOMOYO_TYPE_LINK_ACL,
174 static int tomoyo_path_rename(struct path *old_parent,
175 struct dentry *old_dentry,
176 struct path *new_parent,
177 struct dentry *new_dentry)
179 struct path path1 = { old_parent->mnt, old_dentry };
180 struct path path2 = { new_parent->mnt, new_dentry };
181 return tomoyo_check_2path_perm(tomoyo_domain(),
182 TOMOYO_TYPE_RENAME_ACL,
186 static int tomoyo_file_fcntl(struct file *file, unsigned int cmd,
189 if (cmd == F_SETFL && ((arg ^ file->f_flags) & O_APPEND))
190 return tomoyo_check_rewrite_permission(tomoyo_domain(), file);
194 static int tomoyo_dentry_open(struct file *f, const struct cred *cred)
196 int flags = f->f_flags;
198 if ((flags + 1) & O_ACCMODE)
200 flags |= f->f_flags & (O_APPEND | O_TRUNC);
201 /* Don't check read permission here if called from do_execve(). */
202 if (current->in_execve)
204 return tomoyo_check_open_permission(tomoyo_domain(), &f->f_path, flags);
207 static int tomoyo_file_ioctl(struct file *file, unsigned int cmd,
210 return tomoyo_check_1path_perm(tomoyo_domain(), TOMOYO_TYPE_IOCTL_ACL,
214 static int tomoyo_path_chmod(struct dentry *dentry, struct vfsmount *mnt,
217 struct path path = { mnt, dentry };
218 return tomoyo_check_1path_perm(tomoyo_domain(), TOMOYO_TYPE_CHMOD_ACL,
222 static int tomoyo_path_chown(struct path *path, uid_t uid, gid_t gid)
225 if (uid != (uid_t) -1)
226 error = tomoyo_check_1path_perm(tomoyo_domain(),
227 TOMOYO_TYPE_CHOWN_ACL, path);
228 if (!error && gid != (gid_t) -1)
229 error = tomoyo_check_1path_perm(tomoyo_domain(),
230 TOMOYO_TYPE_CHGRP_ACL, path);
234 static int tomoyo_path_chroot(struct path *path)
236 return tomoyo_check_1path_perm(tomoyo_domain(), TOMOYO_TYPE_CHROOT_ACL,
240 static int tomoyo_sb_mount(char *dev_name, struct path *path,
241 char *type, unsigned long flags, void *data)
243 return tomoyo_check_1path_perm(tomoyo_domain(), TOMOYO_TYPE_MOUNT_ACL,
247 static int tomoyo_sb_umount(struct vfsmount *mnt, int flags)
249 struct path path = { mnt, mnt->mnt_root };
250 return tomoyo_check_1path_perm(tomoyo_domain(), TOMOYO_TYPE_UMOUNT_ACL,
254 static int tomoyo_sb_pivotroot(struct path *old_path, struct path *new_path)
256 return tomoyo_check_2path_perm(tomoyo_domain(),
257 TOMOYO_TYPE_PIVOT_ROOT_ACL,
262 * tomoyo_security_ops is a "struct security_operations" which is used for
263 * registering TOMOYO.
265 static struct security_operations tomoyo_security_ops = {
267 .cred_alloc_blank = tomoyo_cred_alloc_blank,
268 .cred_prepare = tomoyo_cred_prepare,
269 .cred_transfer = tomoyo_cred_transfer,
270 .bprm_set_creds = tomoyo_bprm_set_creds,
271 .bprm_check_security = tomoyo_bprm_check_security,
272 .file_fcntl = tomoyo_file_fcntl,
273 .dentry_open = tomoyo_dentry_open,
274 .path_truncate = tomoyo_path_truncate,
275 .path_unlink = tomoyo_path_unlink,
276 .path_mkdir = tomoyo_path_mkdir,
277 .path_rmdir = tomoyo_path_rmdir,
278 .path_symlink = tomoyo_path_symlink,
279 .path_mknod = tomoyo_path_mknod,
280 .path_link = tomoyo_path_link,
281 .path_rename = tomoyo_path_rename,
282 .file_ioctl = tomoyo_file_ioctl,
283 .path_chmod = tomoyo_path_chmod,
284 .path_chown = tomoyo_path_chown,
285 .path_chroot = tomoyo_path_chroot,
286 .sb_mount = tomoyo_sb_mount,
287 .sb_umount = tomoyo_sb_umount,
288 .sb_pivotroot = tomoyo_sb_pivotroot,
292 struct srcu_struct tomoyo_ss;
294 static int __init tomoyo_init(void)
296 struct cred *cred = (struct cred *) current_cred();
298 if (!security_module_enable(&tomoyo_security_ops))
300 /* register ourselves with the security framework */
301 if (register_security(&tomoyo_security_ops) ||
302 init_srcu_struct(&tomoyo_ss))
303 panic("Failure registering TOMOYO Linux");
304 printk(KERN_INFO "TOMOYO Linux initialized\n");
305 cred->security = &tomoyo_kernel_domain;
306 tomoyo_realpath_init();
310 security_initcall(tomoyo_init);