2 * security/tomoyo/tomoyo.c
4 * LSM hooks for TOMOYO Linux.
6 * Copyright (C) 2005-2010 NTT DATA CORPORATION
9 #include <linux/security.h>
12 static int tomoyo_cred_alloc_blank(struct cred *new, gfp_t gfp)
18 static int tomoyo_cred_prepare(struct cred *new, const struct cred *old,
21 struct tomoyo_domain_info *domain = old->security;
22 new->security = domain;
24 atomic_inc(&domain->users);
28 static void tomoyo_cred_transfer(struct cred *new, const struct cred *old)
30 tomoyo_cred_prepare(new, old, 0);
33 static void tomoyo_cred_free(struct cred *cred)
35 struct tomoyo_domain_info *domain = cred->security;
37 atomic_dec(&domain->users);
40 static int tomoyo_bprm_set_creds(struct linux_binprm *bprm)
44 rc = cap_bprm_set_creds(bprm);
49 * Do only if this function is called for the first time of an execve
52 if (bprm->cred_prepared)
54 #ifndef CONFIG_SECURITY_TOMOYO_OMIT_USERSPACE_LOADER
56 * Load policy if /sbin/tomoyo-init exists and /sbin/init is requested
59 if (!tomoyo_policy_loaded)
60 tomoyo_load_policy(bprm->filename);
63 * Release reference to "struct tomoyo_domain_info" stored inside
64 * "bprm->cred->security". New reference to "struct tomoyo_domain_info"
65 * stored inside "bprm->cred->security" will be acquired later inside
66 * tomoyo_find_next_domain().
68 atomic_dec(&((struct tomoyo_domain_info *)
69 bprm->cred->security)->users);
71 * Tell tomoyo_bprm_check_security() is called for the first time of an
74 bprm->cred->security = NULL;
78 static int tomoyo_bprm_check_security(struct linux_binprm *bprm)
80 struct tomoyo_domain_info *domain = bprm->cred->security;
83 * Execute permission is checked against pathname passed to do_execve()
84 * using current domain.
87 const int idx = tomoyo_read_lock();
88 const int err = tomoyo_find_next_domain(bprm);
89 tomoyo_read_unlock(idx);
93 * Read permission is checked against interpreters using next domain.
95 return tomoyo_check_open_permission(domain, &bprm->file->f_path, O_RDONLY);
98 static int tomoyo_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
100 struct path path = { mnt, dentry };
101 return tomoyo_path_perm(TOMOYO_TYPE_GETATTR, &path, NULL);
104 static int tomoyo_path_truncate(struct path *path)
106 return tomoyo_path_perm(TOMOYO_TYPE_TRUNCATE, path, NULL);
109 static int tomoyo_path_unlink(struct path *parent, struct dentry *dentry)
111 struct path path = { parent->mnt, dentry };
112 return tomoyo_path_perm(TOMOYO_TYPE_UNLINK, &path, NULL);
115 static int tomoyo_path_mkdir(struct path *parent, struct dentry *dentry,
118 struct path path = { parent->mnt, dentry };
119 return tomoyo_path_number_perm(TOMOYO_TYPE_MKDIR, &path,
123 static int tomoyo_path_rmdir(struct path *parent, struct dentry *dentry)
125 struct path path = { parent->mnt, dentry };
126 return tomoyo_path_perm(TOMOYO_TYPE_RMDIR, &path, NULL);
129 static int tomoyo_path_symlink(struct path *parent, struct dentry *dentry,
130 const char *old_name)
132 struct path path = { parent->mnt, dentry };
133 return tomoyo_path_perm(TOMOYO_TYPE_SYMLINK, &path, old_name);
136 static int tomoyo_path_mknod(struct path *parent, struct dentry *dentry,
137 int mode, unsigned int dev)
139 struct path path = { parent->mnt, dentry };
140 int type = TOMOYO_TYPE_CREATE;
141 const unsigned int perm = mode & S_IALLUGO;
143 switch (mode & S_IFMT) {
145 type = TOMOYO_TYPE_MKCHAR;
148 type = TOMOYO_TYPE_MKBLOCK;
153 return tomoyo_mkdev_perm(type, &path, perm, dev);
155 switch (mode & S_IFMT) {
157 type = TOMOYO_TYPE_MKFIFO;
160 type = TOMOYO_TYPE_MKSOCK;
163 return tomoyo_path_number_perm(type, &path, perm);
166 static int tomoyo_path_link(struct dentry *old_dentry, struct path *new_dir,
167 struct dentry *new_dentry)
169 struct path path1 = { new_dir->mnt, old_dentry };
170 struct path path2 = { new_dir->mnt, new_dentry };
171 return tomoyo_path2_perm(TOMOYO_TYPE_LINK, &path1, &path2);
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_path2_perm(TOMOYO_TYPE_RENAME, &path1, &path2);
184 static int tomoyo_file_fcntl(struct file *file, unsigned int cmd,
187 if (!(cmd == F_SETFL && ((arg ^ file->f_flags) & O_APPEND)))
189 return tomoyo_check_open_permission(tomoyo_domain(), &file->f_path,
190 O_WRONLY | (arg & O_APPEND));
193 static int tomoyo_dentry_open(struct file *f, const struct cred *cred)
195 int flags = f->f_flags;
196 /* Don't check read permission here if called from do_execve(). */
197 if (current->in_execve)
199 return tomoyo_check_open_permission(tomoyo_domain(), &f->f_path, flags);
202 static int tomoyo_file_ioctl(struct file *file, unsigned int cmd,
205 return tomoyo_path_number_perm(TOMOYO_TYPE_IOCTL, &file->f_path, cmd);
208 static int tomoyo_path_chmod(struct dentry *dentry, struct vfsmount *mnt,
211 struct path path = { mnt, dentry };
212 return tomoyo_path_number_perm(TOMOYO_TYPE_CHMOD, &path,
216 static int tomoyo_path_chown(struct path *path, uid_t uid, gid_t gid)
219 if (uid != (uid_t) -1)
220 error = tomoyo_path_number_perm(TOMOYO_TYPE_CHOWN, path, uid);
221 if (!error && gid != (gid_t) -1)
222 error = tomoyo_path_number_perm(TOMOYO_TYPE_CHGRP, path, gid);
226 static int tomoyo_path_chroot(struct path *path)
228 return tomoyo_path_perm(TOMOYO_TYPE_CHROOT, path, NULL);
231 static int tomoyo_sb_mount(char *dev_name, struct path *path,
232 char *type, unsigned long flags, void *data)
234 return tomoyo_mount_permission(dev_name, path, type, flags, data);
237 static int tomoyo_sb_umount(struct vfsmount *mnt, int flags)
239 struct path path = { mnt, mnt->mnt_root };
240 return tomoyo_path_perm(TOMOYO_TYPE_UMOUNT, &path, NULL);
243 static int tomoyo_sb_pivotroot(struct path *old_path, struct path *new_path)
245 return tomoyo_path2_perm(TOMOYO_TYPE_PIVOT_ROOT, new_path, old_path);
249 * tomoyo_security_ops is a "struct security_operations" which is used for
250 * registering TOMOYO.
252 static struct security_operations tomoyo_security_ops = {
254 .cred_alloc_blank = tomoyo_cred_alloc_blank,
255 .cred_prepare = tomoyo_cred_prepare,
256 .cred_transfer = tomoyo_cred_transfer,
257 .cred_free = tomoyo_cred_free,
258 .bprm_set_creds = tomoyo_bprm_set_creds,
259 .bprm_check_security = tomoyo_bprm_check_security,
260 .file_fcntl = tomoyo_file_fcntl,
261 .dentry_open = tomoyo_dentry_open,
262 .path_truncate = tomoyo_path_truncate,
263 .path_unlink = tomoyo_path_unlink,
264 .path_mkdir = tomoyo_path_mkdir,
265 .path_rmdir = tomoyo_path_rmdir,
266 .path_symlink = tomoyo_path_symlink,
267 .path_mknod = tomoyo_path_mknod,
268 .path_link = tomoyo_path_link,
269 .path_rename = tomoyo_path_rename,
270 .inode_getattr = tomoyo_inode_getattr,
271 .file_ioctl = tomoyo_file_ioctl,
272 .path_chmod = tomoyo_path_chmod,
273 .path_chown = tomoyo_path_chown,
274 .path_chroot = tomoyo_path_chroot,
275 .sb_mount = tomoyo_sb_mount,
276 .sb_umount = tomoyo_sb_umount,
277 .sb_pivotroot = tomoyo_sb_pivotroot,
281 struct srcu_struct tomoyo_ss;
283 static int __init tomoyo_init(void)
285 struct cred *cred = (struct cred *) current_cred();
287 if (!security_module_enable(&tomoyo_security_ops))
289 /* register ourselves with the security framework */
290 if (register_security(&tomoyo_security_ops) ||
291 init_srcu_struct(&tomoyo_ss))
292 panic("Failure registering TOMOYO Linux");
293 printk(KERN_INFO "TOMOYO Linux initialized\n");
294 cred->security = &tomoyo_kernel_domain;
299 security_initcall(tomoyo_init);