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>
15 static int tomoyo_cred_alloc_blank(struct cred
*new, gfp_t gfp
)
21 static int tomoyo_cred_prepare(struct cred
*new, const struct cred
*old
,
24 struct tomoyo_domain_info
*domain
= old
->security
;
25 new->security
= domain
;
27 atomic_inc(&domain
->users
);
31 static void tomoyo_cred_transfer(struct cred
*new, const struct cred
*old
)
33 tomoyo_cred_prepare(new, old
, 0);
36 static void tomoyo_cred_free(struct cred
*cred
)
38 struct tomoyo_domain_info
*domain
= cred
->security
;
40 atomic_dec(&domain
->users
);
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 * Release reference to "struct tomoyo_domain_info" stored inside
65 * "bprm->cred->security". New reference to "struct tomoyo_domain_info"
66 * stored inside "bprm->cred->security" will be acquired later inside
67 * tomoyo_find_next_domain().
69 atomic_dec(&((struct tomoyo_domain_info
*)
70 bprm
->cred
->security
)->users
);
72 * Tell tomoyo_bprm_check_security() is called for the first time of an
75 bprm
->cred
->security
= NULL
;
79 static int tomoyo_bprm_check_security(struct linux_binprm
*bprm
)
81 struct tomoyo_domain_info
*domain
= bprm
->cred
->security
;
84 * Execute permission is checked against pathname passed to do_execve()
85 * using current domain.
88 const int idx
= tomoyo_read_lock();
89 const int err
= tomoyo_find_next_domain(bprm
);
90 tomoyo_read_unlock(idx
);
94 * Read permission is checked against interpreters using next domain.
96 return tomoyo_check_open_permission(domain
, &bprm
->file
->f_path
, O_RDONLY
);
99 static int tomoyo_path_truncate(struct path
*path
, loff_t length
,
100 unsigned int time_attrs
)
102 return tomoyo_path_perm(TOMOYO_TYPE_TRUNCATE
, path
);
105 static int tomoyo_path_unlink(struct path
*parent
, struct dentry
*dentry
)
107 struct path path
= { parent
->mnt
, dentry
};
108 return tomoyo_path_perm(TOMOYO_TYPE_UNLINK
, &path
);
111 static int tomoyo_path_mkdir(struct path
*parent
, struct dentry
*dentry
,
114 struct path path
= { parent
->mnt
, dentry
};
115 return tomoyo_path_perm(TOMOYO_TYPE_MKDIR
, &path
);
118 static int tomoyo_path_rmdir(struct path
*parent
, struct dentry
*dentry
)
120 struct path path
= { parent
->mnt
, dentry
};
121 return tomoyo_path_perm(TOMOYO_TYPE_RMDIR
, &path
);
124 static int tomoyo_path_symlink(struct path
*parent
, struct dentry
*dentry
,
125 const char *old_name
)
127 struct path path
= { parent
->mnt
, dentry
};
128 return tomoyo_path_perm(TOMOYO_TYPE_SYMLINK
, &path
);
131 static int tomoyo_path_mknod(struct path
*parent
, struct dentry
*dentry
,
132 int mode
, unsigned int dev
)
134 struct path path
= { parent
->mnt
, dentry
};
135 int type
= TOMOYO_TYPE_CREATE
;
137 switch (mode
& S_IFMT
) {
139 type
= TOMOYO_TYPE_MKCHAR
;
142 type
= TOMOYO_TYPE_MKBLOCK
;
145 type
= TOMOYO_TYPE_MKFIFO
;
148 type
= TOMOYO_TYPE_MKSOCK
;
151 return tomoyo_path_perm(type
, &path
);
154 static int tomoyo_path_link(struct dentry
*old_dentry
, struct path
*new_dir
,
155 struct dentry
*new_dentry
)
157 struct path path1
= { new_dir
->mnt
, old_dentry
};
158 struct path path2
= { new_dir
->mnt
, new_dentry
};
159 return tomoyo_path2_perm(TOMOYO_TYPE_LINK
, &path1
, &path2
);
162 static int tomoyo_path_rename(struct path
*old_parent
,
163 struct dentry
*old_dentry
,
164 struct path
*new_parent
,
165 struct dentry
*new_dentry
)
167 struct path path1
= { old_parent
->mnt
, old_dentry
};
168 struct path path2
= { new_parent
->mnt
, new_dentry
};
169 return tomoyo_path2_perm(TOMOYO_TYPE_RENAME
, &path1
, &path2
);
172 static int tomoyo_file_fcntl(struct file
*file
, unsigned int cmd
,
175 if (cmd
== F_SETFL
&& ((arg
^ file
->f_flags
) & O_APPEND
))
176 return tomoyo_check_rewrite_permission(file
);
180 static int tomoyo_dentry_open(struct file
*f
, const struct cred
*cred
)
182 int flags
= f
->f_flags
;
183 /* Don't check read permission here if called from do_execve(). */
184 if (current
->in_execve
)
186 return tomoyo_check_open_permission(tomoyo_domain(), &f
->f_path
, flags
);
189 static int tomoyo_file_ioctl(struct file
*file
, unsigned int cmd
,
192 return tomoyo_path_perm(TOMOYO_TYPE_IOCTL
, &file
->f_path
);
195 static int tomoyo_path_chmod(struct dentry
*dentry
, struct vfsmount
*mnt
,
198 struct path path
= { mnt
, dentry
};
199 return tomoyo_path_perm(TOMOYO_TYPE_CHMOD
, &path
);
202 static int tomoyo_path_chown(struct path
*path
, uid_t uid
, gid_t gid
)
205 if (uid
!= (uid_t
) -1)
206 error
= tomoyo_path_perm(TOMOYO_TYPE_CHOWN
, path
);
207 if (!error
&& gid
!= (gid_t
) -1)
208 error
= tomoyo_path_perm(TOMOYO_TYPE_CHGRP
, path
);
212 static int tomoyo_path_chroot(struct path
*path
)
214 return tomoyo_path_perm(TOMOYO_TYPE_CHROOT
, path
);
217 static int tomoyo_sb_mount(char *dev_name
, struct path
*path
,
218 char *type
, unsigned long flags
, void *data
)
220 return tomoyo_path_perm(TOMOYO_TYPE_MOUNT
, path
);
223 static int tomoyo_sb_umount(struct vfsmount
*mnt
, int flags
)
225 struct path path
= { mnt
, mnt
->mnt_root
};
226 return tomoyo_path_perm(TOMOYO_TYPE_UMOUNT
, &path
);
229 static int tomoyo_sb_pivotroot(struct path
*old_path
, struct path
*new_path
)
231 return tomoyo_path2_perm(TOMOYO_TYPE_PIVOT_ROOT
, new_path
, old_path
);
235 * tomoyo_security_ops is a "struct security_operations" which is used for
236 * registering TOMOYO.
238 static struct security_operations tomoyo_security_ops
= {
240 .cred_alloc_blank
= tomoyo_cred_alloc_blank
,
241 .cred_prepare
= tomoyo_cred_prepare
,
242 .cred_transfer
= tomoyo_cred_transfer
,
243 .cred_free
= tomoyo_cred_free
,
244 .bprm_set_creds
= tomoyo_bprm_set_creds
,
245 .bprm_check_security
= tomoyo_bprm_check_security
,
246 .file_fcntl
= tomoyo_file_fcntl
,
247 .dentry_open
= tomoyo_dentry_open
,
248 .path_truncate
= tomoyo_path_truncate
,
249 .path_unlink
= tomoyo_path_unlink
,
250 .path_mkdir
= tomoyo_path_mkdir
,
251 .path_rmdir
= tomoyo_path_rmdir
,
252 .path_symlink
= tomoyo_path_symlink
,
253 .path_mknod
= tomoyo_path_mknod
,
254 .path_link
= tomoyo_path_link
,
255 .path_rename
= tomoyo_path_rename
,
256 .file_ioctl
= tomoyo_file_ioctl
,
257 .path_chmod
= tomoyo_path_chmod
,
258 .path_chown
= tomoyo_path_chown
,
259 .path_chroot
= tomoyo_path_chroot
,
260 .sb_mount
= tomoyo_sb_mount
,
261 .sb_umount
= tomoyo_sb_umount
,
262 .sb_pivotroot
= tomoyo_sb_pivotroot
,
266 struct srcu_struct tomoyo_ss
;
268 static int __init
tomoyo_init(void)
270 struct cred
*cred
= (struct cred
*) current_cred();
272 if (!security_module_enable(&tomoyo_security_ops
))
274 /* register ourselves with the security framework */
275 if (register_security(&tomoyo_security_ops
) ||
276 init_srcu_struct(&tomoyo_ss
))
277 panic("Failure registering TOMOYO Linux");
278 printk(KERN_INFO
"TOMOYO Linux initialized\n");
279 cred
->security
= &tomoyo_kernel_domain
;
280 tomoyo_realpath_init();
284 security_initcall(tomoyo_init
);