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.
80 return tomoyo_find_next_domain(bprm
);
82 * Read permission is checked against interpreters using next domain.
83 * '1' is the result of open_to_namei_flags(O_RDONLY).
85 return tomoyo_check_open_permission(domain
, &bprm
->file
->f_path
, 1);
88 static int tomoyo_path_truncate(struct path
*path
, loff_t length
,
89 unsigned int time_attrs
)
91 return tomoyo_check_1path_perm(tomoyo_domain(),
92 TOMOYO_TYPE_TRUNCATE_ACL
,
96 static int tomoyo_path_unlink(struct path
*parent
, struct dentry
*dentry
)
98 struct path path
= { parent
->mnt
, dentry
};
99 return tomoyo_check_1path_perm(tomoyo_domain(),
100 TOMOYO_TYPE_UNLINK_ACL
,
104 static int tomoyo_path_mkdir(struct path
*parent
, struct dentry
*dentry
,
107 struct path path
= { parent
->mnt
, dentry
};
108 return tomoyo_check_1path_perm(tomoyo_domain(),
109 TOMOYO_TYPE_MKDIR_ACL
,
113 static int tomoyo_path_rmdir(struct path
*parent
, struct dentry
*dentry
)
115 struct path path
= { parent
->mnt
, dentry
};
116 return tomoyo_check_1path_perm(tomoyo_domain(),
117 TOMOYO_TYPE_RMDIR_ACL
,
121 static int tomoyo_path_symlink(struct path
*parent
, struct dentry
*dentry
,
122 const char *old_name
)
124 struct path path
= { parent
->mnt
, dentry
};
125 return tomoyo_check_1path_perm(tomoyo_domain(),
126 TOMOYO_TYPE_SYMLINK_ACL
,
130 static int tomoyo_path_mknod(struct path
*parent
, struct dentry
*dentry
,
131 int mode
, unsigned int dev
)
133 struct path path
= { parent
->mnt
, dentry
};
134 int type
= TOMOYO_TYPE_CREATE_ACL
;
136 switch (mode
& S_IFMT
) {
138 type
= TOMOYO_TYPE_MKCHAR_ACL
;
141 type
= TOMOYO_TYPE_MKBLOCK_ACL
;
144 type
= TOMOYO_TYPE_MKFIFO_ACL
;
147 type
= TOMOYO_TYPE_MKSOCK_ACL
;
150 return tomoyo_check_1path_perm(tomoyo_domain(),
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_check_2path_perm(tomoyo_domain(),
160 TOMOYO_TYPE_LINK_ACL
,
164 static int tomoyo_path_rename(struct path
*old_parent
,
165 struct dentry
*old_dentry
,
166 struct path
*new_parent
,
167 struct dentry
*new_dentry
)
169 struct path path1
= { old_parent
->mnt
, old_dentry
};
170 struct path path2
= { new_parent
->mnt
, new_dentry
};
171 return tomoyo_check_2path_perm(tomoyo_domain(),
172 TOMOYO_TYPE_RENAME_ACL
,
176 static int tomoyo_file_fcntl(struct file
*file
, unsigned int cmd
,
179 if (cmd
== F_SETFL
&& ((arg
^ file
->f_flags
) & O_APPEND
))
180 return tomoyo_check_rewrite_permission(tomoyo_domain(), file
);
184 static int tomoyo_dentry_open(struct file
*f
, const struct cred
*cred
)
186 int flags
= f
->f_flags
;
188 if ((flags
+ 1) & O_ACCMODE
)
190 flags
|= f
->f_flags
& (O_APPEND
| O_TRUNC
);
191 /* Don't check read permission here if called from do_execve(). */
192 if (current
->in_execve
)
194 return tomoyo_check_open_permission(tomoyo_domain(), &f
->f_path
, flags
);
198 * tomoyo_security_ops is a "struct security_operations" which is used for
199 * registering TOMOYO.
201 static struct security_operations tomoyo_security_ops
= {
203 .cred_alloc_blank
= tomoyo_cred_alloc_blank
,
204 .cred_prepare
= tomoyo_cred_prepare
,
205 .cred_transfer
= tomoyo_cred_transfer
,
206 .bprm_set_creds
= tomoyo_bprm_set_creds
,
207 .bprm_check_security
= tomoyo_bprm_check_security
,
208 .file_fcntl
= tomoyo_file_fcntl
,
209 .dentry_open
= tomoyo_dentry_open
,
210 .path_truncate
= tomoyo_path_truncate
,
211 .path_unlink
= tomoyo_path_unlink
,
212 .path_mkdir
= tomoyo_path_mkdir
,
213 .path_rmdir
= tomoyo_path_rmdir
,
214 .path_symlink
= tomoyo_path_symlink
,
215 .path_mknod
= tomoyo_path_mknod
,
216 .path_link
= tomoyo_path_link
,
217 .path_rename
= tomoyo_path_rename
,
220 static int __init
tomoyo_init(void)
222 struct cred
*cred
= (struct cred
*) current_cred();
224 if (!security_module_enable(&tomoyo_security_ops
))
226 /* register ourselves with the security framework */
227 if (register_security(&tomoyo_security_ops
))
228 panic("Failure registering TOMOYO Linux");
229 printk(KERN_INFO
"TOMOYO Linux initialized\n");
230 cred
->security
= &tomoyo_kernel_domain
;
231 tomoyo_realpath_init();
235 security_initcall(tomoyo_init
);