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);
90 static int tomoyo_prepend(char **buffer
, int *buflen
, const char *str
)
92 int namelen
= strlen(str
);
94 if (*buflen
< namelen
)
98 memcpy(*buffer
, str
, namelen
);
103 * tomoyo_sysctl_path - return the realpath of a ctl_table.
104 * @table: pointer to "struct ctl_table".
106 * Returns realpath(3) of the @table on success.
107 * Returns NULL on failure.
109 * This function uses tomoyo_alloc(), so the caller must call tomoyo_free()
110 * if this function didn't return NULL.
112 static char *tomoyo_sysctl_path(struct ctl_table
*table
)
114 int buflen
= TOMOYO_MAX_PATHNAME_LEN
;
115 char *buf
= tomoyo_alloc(buflen
);
116 char *end
= buf
+ buflen
;
126 const char *sp
= table
->procname
;
129 memset(num
, 0, sizeof(num
));
130 snprintf(num
, sizeof(num
) - 1, "=%d=", table
->ctl_name
);
133 if (tomoyo_prepend(&end
, &buflen
, sp
) ||
134 tomoyo_prepend(&end
, &buflen
, "/"))
136 table
= table
->parent
;
138 if (tomoyo_prepend(&end
, &buflen
, "/proc/sys"))
140 error
= tomoyo_encode(buf
, end
- buf
, end
);
148 static int tomoyo_sysctl(struct ctl_table
*table
, int op
)
153 op
&= MAY_READ
| MAY_WRITE
;
156 name
= tomoyo_sysctl_path(table
);
159 error
= tomoyo_check_file_perm(tomoyo_domain(), name
, op
);
165 static int tomoyo_path_truncate(struct path
*path
, loff_t length
,
166 unsigned int time_attrs
)
168 return tomoyo_check_1path_perm(tomoyo_domain(),
169 TOMOYO_TYPE_TRUNCATE_ACL
,
173 static int tomoyo_path_unlink(struct path
*parent
, struct dentry
*dentry
)
175 struct path path
= { parent
->mnt
, dentry
};
176 return tomoyo_check_1path_perm(tomoyo_domain(),
177 TOMOYO_TYPE_UNLINK_ACL
,
181 static int tomoyo_path_mkdir(struct path
*parent
, struct dentry
*dentry
,
184 struct path path
= { parent
->mnt
, dentry
};
185 return tomoyo_check_1path_perm(tomoyo_domain(),
186 TOMOYO_TYPE_MKDIR_ACL
,
190 static int tomoyo_path_rmdir(struct path
*parent
, struct dentry
*dentry
)
192 struct path path
= { parent
->mnt
, dentry
};
193 return tomoyo_check_1path_perm(tomoyo_domain(),
194 TOMOYO_TYPE_RMDIR_ACL
,
198 static int tomoyo_path_symlink(struct path
*parent
, struct dentry
*dentry
,
199 const char *old_name
)
201 struct path path
= { parent
->mnt
, dentry
};
202 return tomoyo_check_1path_perm(tomoyo_domain(),
203 TOMOYO_TYPE_SYMLINK_ACL
,
207 static int tomoyo_path_mknod(struct path
*parent
, struct dentry
*dentry
,
208 int mode
, unsigned int dev
)
210 struct path path
= { parent
->mnt
, dentry
};
211 int type
= TOMOYO_TYPE_CREATE_ACL
;
213 switch (mode
& S_IFMT
) {
215 type
= TOMOYO_TYPE_MKCHAR_ACL
;
218 type
= TOMOYO_TYPE_MKBLOCK_ACL
;
221 type
= TOMOYO_TYPE_MKFIFO_ACL
;
224 type
= TOMOYO_TYPE_MKSOCK_ACL
;
227 return tomoyo_check_1path_perm(tomoyo_domain(),
231 static int tomoyo_path_link(struct dentry
*old_dentry
, struct path
*new_dir
,
232 struct dentry
*new_dentry
)
234 struct path path1
= { new_dir
->mnt
, old_dentry
};
235 struct path path2
= { new_dir
->mnt
, new_dentry
};
236 return tomoyo_check_2path_perm(tomoyo_domain(),
237 TOMOYO_TYPE_LINK_ACL
,
241 static int tomoyo_path_rename(struct path
*old_parent
,
242 struct dentry
*old_dentry
,
243 struct path
*new_parent
,
244 struct dentry
*new_dentry
)
246 struct path path1
= { old_parent
->mnt
, old_dentry
};
247 struct path path2
= { new_parent
->mnt
, new_dentry
};
248 return tomoyo_check_2path_perm(tomoyo_domain(),
249 TOMOYO_TYPE_RENAME_ACL
,
253 static int tomoyo_file_fcntl(struct file
*file
, unsigned int cmd
,
256 if (cmd
== F_SETFL
&& ((arg
^ file
->f_flags
) & O_APPEND
))
257 return tomoyo_check_rewrite_permission(tomoyo_domain(), file
);
261 static int tomoyo_dentry_open(struct file
*f
, const struct cred
*cred
)
263 int flags
= f
->f_flags
;
265 if ((flags
+ 1) & O_ACCMODE
)
267 flags
|= f
->f_flags
& (O_APPEND
| O_TRUNC
);
268 /* Don't check read permission here if called from do_execve(). */
269 if (current
->in_execve
)
271 return tomoyo_check_open_permission(tomoyo_domain(), &f
->f_path
, flags
);
275 * tomoyo_security_ops is a "struct security_operations" which is used for
276 * registering TOMOYO.
278 static struct security_operations tomoyo_security_ops
= {
280 .cred_alloc_blank
= tomoyo_cred_alloc_blank
,
281 .cred_prepare
= tomoyo_cred_prepare
,
282 .cred_transfer
= tomoyo_cred_transfer
,
283 .bprm_set_creds
= tomoyo_bprm_set_creds
,
284 .bprm_check_security
= tomoyo_bprm_check_security
,
286 .sysctl
= tomoyo_sysctl
,
288 .file_fcntl
= tomoyo_file_fcntl
,
289 .dentry_open
= tomoyo_dentry_open
,
290 .path_truncate
= tomoyo_path_truncate
,
291 .path_unlink
= tomoyo_path_unlink
,
292 .path_mkdir
= tomoyo_path_mkdir
,
293 .path_rmdir
= tomoyo_path_rmdir
,
294 .path_symlink
= tomoyo_path_symlink
,
295 .path_mknod
= tomoyo_path_mknod
,
296 .path_link
= tomoyo_path_link
,
297 .path_rename
= tomoyo_path_rename
,
300 static int __init
tomoyo_init(void)
302 struct cred
*cred
= (struct cred
*) current_cred();
304 if (!security_module_enable(&tomoyo_security_ops
))
306 /* register ourselves with the security framework */
307 if (register_security(&tomoyo_security_ops
))
308 panic("Failure registering TOMOYO Linux");
309 printk(KERN_INFO
"TOMOYO Linux initialized\n");
310 cred
->security
= &tomoyo_kernel_domain
;
311 tomoyo_realpath_init();
315 security_initcall(tomoyo_init
);