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)
55 * Load policy if /sbin/tomoyo-init exists and /sbin/init is requested
58 if (!tomoyo_policy_loaded)
59 tomoyo_load_policy(bprm->filename);
61 * Release reference to "struct tomoyo_domain_info" stored inside
62 * "bprm->cred->security". New reference to "struct tomoyo_domain_info"
63 * stored inside "bprm->cred->security" will be acquired later inside
64 * tomoyo_find_next_domain().
66 atomic_dec(&((struct tomoyo_domain_info *)
67 bprm->cred->security)->users);
69 * Tell tomoyo_bprm_check_security() is called for the first time of an
72 bprm->cred->security = NULL;
76 static int tomoyo_bprm_check_security(struct linux_binprm *bprm)
78 struct tomoyo_domain_info *domain = bprm->cred->security;
81 * Execute permission is checked against pathname passed to do_execve()
82 * using current domain.
85 const int idx = tomoyo_read_lock();
86 const int err = tomoyo_find_next_domain(bprm);
87 tomoyo_read_unlock(idx);
91 * Read permission is checked against interpreters using next domain.
93 return tomoyo_check_open_permission(domain, &bprm->file->f_path, O_RDONLY);
96 static int tomoyo_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
98 struct path path = { mnt, dentry };
99 return tomoyo_path_perm(TOMOYO_TYPE_GETATTR, &path);
102 static int tomoyo_path_truncate(struct path *path)
104 return tomoyo_path_perm(TOMOYO_TYPE_TRUNCATE, path);
107 static int tomoyo_path_unlink(struct path *parent, struct dentry *dentry)
109 struct path path = { parent->mnt, dentry };
110 return tomoyo_path_perm(TOMOYO_TYPE_UNLINK, &path);
113 static int tomoyo_path_mkdir(struct path *parent, struct dentry *dentry,
116 struct path path = { parent->mnt, dentry };
117 return tomoyo_path_number_perm(TOMOYO_TYPE_MKDIR, &path,
121 static int tomoyo_path_rmdir(struct path *parent, struct dentry *dentry)
123 struct path path = { parent->mnt, dentry };
124 return tomoyo_path_perm(TOMOYO_TYPE_RMDIR, &path);
127 static int tomoyo_path_symlink(struct path *parent, struct dentry *dentry,
128 const char *old_name)
130 struct path path = { parent->mnt, dentry };
131 return tomoyo_path_perm(TOMOYO_TYPE_SYMLINK, &path);
134 static int tomoyo_path_mknod(struct path *parent, struct dentry *dentry,
135 int mode, unsigned int dev)
137 struct path path = { parent->mnt, dentry };
138 int type = TOMOYO_TYPE_CREATE;
139 const unsigned int perm = mode & S_IALLUGO;
141 switch (mode & S_IFMT) {
143 type = TOMOYO_TYPE_MKCHAR;
146 type = TOMOYO_TYPE_MKBLOCK;
151 return tomoyo_mkdev_perm(type, &path, perm, dev);
153 switch (mode & S_IFMT) {
155 type = TOMOYO_TYPE_MKFIFO;
158 type = TOMOYO_TYPE_MKSOCK;
161 return tomoyo_path_number_perm(type, &path, perm);
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_path2_perm(TOMOYO_TYPE_LINK, &path1, &path2);
172 static int tomoyo_path_rename(struct path *old_parent,
173 struct dentry *old_dentry,
174 struct path *new_parent,
175 struct dentry *new_dentry)
177 struct path path1 = { old_parent->mnt, old_dentry };
178 struct path path2 = { new_parent->mnt, new_dentry };
179 return tomoyo_path2_perm(TOMOYO_TYPE_RENAME, &path1, &path2);
182 static int tomoyo_file_fcntl(struct file *file, unsigned int cmd,
185 if (!(cmd == F_SETFL && ((arg ^ file->f_flags) & O_APPEND)))
187 return tomoyo_check_open_permission(tomoyo_domain(), &file->f_path,
188 O_WRONLY | (arg & O_APPEND));
191 static int tomoyo_dentry_open(struct file *f, const struct cred *cred)
193 int flags = f->f_flags;
194 /* Don't check read permission here if called from do_execve(). */
195 if (current->in_execve)
197 return tomoyo_check_open_permission(tomoyo_domain(), &f->f_path, flags);
200 static int tomoyo_file_ioctl(struct file *file, unsigned int cmd,
203 return tomoyo_path_number_perm(TOMOYO_TYPE_IOCTL, &file->f_path, cmd);
206 static int tomoyo_path_chmod(struct dentry *dentry, struct vfsmount *mnt,
209 struct path path = { mnt, dentry };
210 return tomoyo_path_number_perm(TOMOYO_TYPE_CHMOD, &path,
214 static int tomoyo_path_chown(struct path *path, uid_t uid, gid_t gid)
217 if (uid != (uid_t) -1)
218 error = tomoyo_path_number_perm(TOMOYO_TYPE_CHOWN, path, uid);
219 if (!error && gid != (gid_t) -1)
220 error = tomoyo_path_number_perm(TOMOYO_TYPE_CHGRP, path, gid);
224 static int tomoyo_path_chroot(struct path *path)
226 return tomoyo_path_perm(TOMOYO_TYPE_CHROOT, path);
229 static int tomoyo_sb_mount(char *dev_name, struct path *path,
230 char *type, unsigned long flags, void *data)
232 return tomoyo_mount_permission(dev_name, path, type, flags, data);
235 static int tomoyo_sb_umount(struct vfsmount *mnt, int flags)
237 struct path path = { mnt, mnt->mnt_root };
238 return tomoyo_path_perm(TOMOYO_TYPE_UMOUNT, &path);
241 static int tomoyo_sb_pivotroot(struct path *old_path, struct path *new_path)
243 return tomoyo_path2_perm(TOMOYO_TYPE_PIVOT_ROOT, new_path, old_path);
247 * tomoyo_security_ops is a "struct security_operations" which is used for
248 * registering TOMOYO.
250 static struct security_operations tomoyo_security_ops = {
252 .cred_alloc_blank = tomoyo_cred_alloc_blank,
253 .cred_prepare = tomoyo_cred_prepare,
254 .cred_transfer = tomoyo_cred_transfer,
255 .cred_free = tomoyo_cred_free,
256 .bprm_set_creds = tomoyo_bprm_set_creds,
257 .bprm_check_security = tomoyo_bprm_check_security,
258 .file_fcntl = tomoyo_file_fcntl,
259 .dentry_open = tomoyo_dentry_open,
260 .path_truncate = tomoyo_path_truncate,
261 .path_unlink = tomoyo_path_unlink,
262 .path_mkdir = tomoyo_path_mkdir,
263 .path_rmdir = tomoyo_path_rmdir,
264 .path_symlink = tomoyo_path_symlink,
265 .path_mknod = tomoyo_path_mknod,
266 .path_link = tomoyo_path_link,
267 .path_rename = tomoyo_path_rename,
268 .inode_getattr = tomoyo_inode_getattr,
269 .file_ioctl = tomoyo_file_ioctl,
270 .path_chmod = tomoyo_path_chmod,
271 .path_chown = tomoyo_path_chown,
272 .path_chroot = tomoyo_path_chroot,
273 .sb_mount = tomoyo_sb_mount,
274 .sb_umount = tomoyo_sb_umount,
275 .sb_pivotroot = tomoyo_sb_pivotroot,
279 struct srcu_struct tomoyo_ss;
281 static int __init tomoyo_init(void)
283 struct cred *cred = (struct cred *) current_cred();
285 if (!security_module_enable(&tomoyo_security_ops))
287 /* register ourselves with the security framework */
288 if (register_security(&tomoyo_security_ops) ||
289 init_srcu_struct(&tomoyo_ss))
290 panic("Failure registering TOMOYO Linux");
291 printk(KERN_INFO "TOMOYO Linux initialized\n");
292 cred->security = &tomoyo_kernel_domain;
297 security_initcall(tomoyo_init);