blob: 714daa34d4936b95ee123a295d6e38eebcdeb677 [file] [log] [blame]
Kentaro Takedaf7433242009-02-05 17:18:16 +09001/*
2 * security/tomoyo/tomoyo.c
3 *
4 * LSM hooks for TOMOYO Linux.
5 *
6 * Copyright (C) 2005-2009 NTT DATA CORPORATION
7 *
Tetsuo Handa39826a12009-04-08 22:31:28 +09008 * Version: 2.2.0 2009/04/01
Kentaro Takedaf7433242009-02-05 17:18:16 +09009 *
10 */
11
12#include <linux/security.h>
13#include "common.h"
14#include "tomoyo.h"
15#include "realpath.h"
16
David Howellsee18d642009-09-02 09:14:21 +010017static int tomoyo_cred_alloc_blank(struct cred *new, gfp_t gfp)
18{
19 new->security = NULL;
20 return 0;
21}
22
Kentaro Takedaf7433242009-02-05 17:18:16 +090023static int tomoyo_cred_prepare(struct cred *new, const struct cred *old,
24 gfp_t gfp)
25{
26 /*
27 * Since "struct tomoyo_domain_info *" is a sharable pointer,
28 * we don't need to duplicate.
29 */
30 new->security = old->security;
31 return 0;
32}
33
David Howellsee18d642009-09-02 09:14:21 +010034static void tomoyo_cred_transfer(struct cred *new, const struct cred *old)
35{
36 /*
37 * Since "struct tomoyo_domain_info *" is a sharable pointer,
38 * we don't need to duplicate.
39 */
40 new->security = old->security;
41}
42
Kentaro Takedaf7433242009-02-05 17:18:16 +090043static int tomoyo_bprm_set_creds(struct linux_binprm *bprm)
44{
Herton Ronaldo Krzesinskib1338d12009-05-26 12:15:53 +090045 int rc;
46
47 rc = cap_bprm_set_creds(bprm);
48 if (rc)
49 return rc;
50
Kentaro Takedaf7433242009-02-05 17:18:16 +090051 /*
52 * Do only if this function is called for the first time of an execve
53 * operation.
54 */
55 if (bprm->cred_prepared)
56 return 0;
57 /*
58 * Load policy if /sbin/tomoyo-init exists and /sbin/init is requested
59 * for the first time.
60 */
61 if (!tomoyo_policy_loaded)
62 tomoyo_load_policy(bprm->filename);
63 /*
64 * Tell tomoyo_bprm_check_security() is called for the first time of an
65 * execve operation.
66 */
67 bprm->cred->security = NULL;
68 return 0;
69}
70
71static int tomoyo_bprm_check_security(struct linux_binprm *bprm)
72{
73 struct tomoyo_domain_info *domain = bprm->cred->security;
74
75 /*
76 * Execute permission is checked against pathname passed to do_execve()
77 * using current domain.
78 */
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +090079 if (!domain) {
80 /*
81 * We will need to protect whole execve() operation when GC
82 * starts kfree()ing "struct tomoyo_domain_info" because
83 * bprm->cred->security points to "struct tomoyo_domain_info"
84 * but "struct tomoyo_domain_info" does not have a refcounter.
85 */
86 const int idx = tomoyo_read_lock();
87 const int err = tomoyo_find_next_domain(bprm);
88 tomoyo_read_unlock(idx);
89 return err;
90 }
Kentaro Takedaf7433242009-02-05 17:18:16 +090091 /*
92 * Read permission is checked against interpreters using next domain.
93 * '1' is the result of open_to_namei_flags(O_RDONLY).
94 */
95 return tomoyo_check_open_permission(domain, &bprm->file->f_path, 1);
96}
97
Kentaro Takedaf7433242009-02-05 17:18:16 +090098static int tomoyo_path_truncate(struct path *path, loff_t length,
99 unsigned int time_attrs)
100{
101 return tomoyo_check_1path_perm(tomoyo_domain(),
102 TOMOYO_TYPE_TRUNCATE_ACL,
103 path);
104}
105
106static int tomoyo_path_unlink(struct path *parent, struct dentry *dentry)
107{
108 struct path path = { parent->mnt, dentry };
109 return tomoyo_check_1path_perm(tomoyo_domain(),
110 TOMOYO_TYPE_UNLINK_ACL,
111 &path);
112}
113
114static int tomoyo_path_mkdir(struct path *parent, struct dentry *dentry,
115 int mode)
116{
117 struct path path = { parent->mnt, dentry };
118 return tomoyo_check_1path_perm(tomoyo_domain(),
119 TOMOYO_TYPE_MKDIR_ACL,
120 &path);
121}
122
123static int tomoyo_path_rmdir(struct path *parent, struct dentry *dentry)
124{
125 struct path path = { parent->mnt, dentry };
126 return tomoyo_check_1path_perm(tomoyo_domain(),
127 TOMOYO_TYPE_RMDIR_ACL,
128 &path);
129}
130
131static int tomoyo_path_symlink(struct path *parent, struct dentry *dentry,
132 const char *old_name)
133{
134 struct path path = { parent->mnt, dentry };
135 return tomoyo_check_1path_perm(tomoyo_domain(),
136 TOMOYO_TYPE_SYMLINK_ACL,
137 &path);
138}
139
140static int tomoyo_path_mknod(struct path *parent, struct dentry *dentry,
141 int mode, unsigned int dev)
142{
143 struct path path = { parent->mnt, dentry };
144 int type = TOMOYO_TYPE_CREATE_ACL;
145
146 switch (mode & S_IFMT) {
147 case S_IFCHR:
148 type = TOMOYO_TYPE_MKCHAR_ACL;
149 break;
150 case S_IFBLK:
151 type = TOMOYO_TYPE_MKBLOCK_ACL;
152 break;
153 case S_IFIFO:
154 type = TOMOYO_TYPE_MKFIFO_ACL;
155 break;
156 case S_IFSOCK:
157 type = TOMOYO_TYPE_MKSOCK_ACL;
158 break;
159 }
160 return tomoyo_check_1path_perm(tomoyo_domain(),
161 type, &path);
162}
163
164static int tomoyo_path_link(struct dentry *old_dentry, struct path *new_dir,
165 struct dentry *new_dentry)
166{
167 struct path path1 = { new_dir->mnt, old_dentry };
168 struct path path2 = { new_dir->mnt, new_dentry };
169 return tomoyo_check_2path_perm(tomoyo_domain(),
170 TOMOYO_TYPE_LINK_ACL,
171 &path1, &path2);
172}
173
174static int tomoyo_path_rename(struct path *old_parent,
175 struct dentry *old_dentry,
176 struct path *new_parent,
177 struct dentry *new_dentry)
178{
179 struct path path1 = { old_parent->mnt, old_dentry };
180 struct path path2 = { new_parent->mnt, new_dentry };
181 return tomoyo_check_2path_perm(tomoyo_domain(),
182 TOMOYO_TYPE_RENAME_ACL,
183 &path1, &path2);
184}
185
186static int tomoyo_file_fcntl(struct file *file, unsigned int cmd,
187 unsigned long arg)
188{
189 if (cmd == F_SETFL && ((arg ^ file->f_flags) & O_APPEND))
190 return tomoyo_check_rewrite_permission(tomoyo_domain(), file);
191 return 0;
192}
193
194static int tomoyo_dentry_open(struct file *f, const struct cred *cred)
195{
196 int flags = f->f_flags;
197
198 if ((flags + 1) & O_ACCMODE)
199 flags++;
200 flags |= f->f_flags & (O_APPEND | O_TRUNC);
201 /* Don't check read permission here if called from do_execve(). */
202 if (current->in_execve)
203 return 0;
204 return tomoyo_check_open_permission(tomoyo_domain(), &f->f_path, flags);
205}
206
Tetsuo Handa937bf612009-12-02 21:09:48 +0900207static int tomoyo_file_ioctl(struct file *file, unsigned int cmd,
208 unsigned long arg)
209{
210 return tomoyo_check_1path_perm(tomoyo_domain(), TOMOYO_TYPE_IOCTL_ACL,
211 &file->f_path);
212}
213
214static int tomoyo_path_chmod(struct dentry *dentry, struct vfsmount *mnt,
215 mode_t mode)
216{
217 struct path path = { mnt, dentry };
218 return tomoyo_check_1path_perm(tomoyo_domain(), TOMOYO_TYPE_CHMOD_ACL,
219 &path);
220}
221
222static int tomoyo_path_chown(struct path *path, uid_t uid, gid_t gid)
223{
224 int error = 0;
225 if (uid != (uid_t) -1)
226 error = tomoyo_check_1path_perm(tomoyo_domain(),
227 TOMOYO_TYPE_CHOWN_ACL, path);
228 if (!error && gid != (gid_t) -1)
229 error = tomoyo_check_1path_perm(tomoyo_domain(),
230 TOMOYO_TYPE_CHGRP_ACL, path);
231 return error;
232}
233
234static int tomoyo_path_chroot(struct path *path)
235{
236 return tomoyo_check_1path_perm(tomoyo_domain(), TOMOYO_TYPE_CHROOT_ACL,
237 path);
238}
239
240static int tomoyo_sb_mount(char *dev_name, struct path *path,
241 char *type, unsigned long flags, void *data)
242{
243 return tomoyo_check_1path_perm(tomoyo_domain(), TOMOYO_TYPE_MOUNT_ACL,
244 path);
245}
246
247static int tomoyo_sb_umount(struct vfsmount *mnt, int flags)
248{
249 struct path path = { mnt, mnt->mnt_root };
250 return tomoyo_check_1path_perm(tomoyo_domain(), TOMOYO_TYPE_UMOUNT_ACL,
251 &path);
252}
253
254static int tomoyo_sb_pivotroot(struct path *old_path, struct path *new_path)
255{
256 return tomoyo_check_2path_perm(tomoyo_domain(),
257 TOMOYO_TYPE_PIVOT_ROOT_ACL,
258 new_path, old_path);
259}
260
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900261/*
262 * tomoyo_security_ops is a "struct security_operations" which is used for
263 * registering TOMOYO.
264 */
Kentaro Takedaf7433242009-02-05 17:18:16 +0900265static struct security_operations tomoyo_security_ops = {
266 .name = "tomoyo",
David Howellsee18d642009-09-02 09:14:21 +0100267 .cred_alloc_blank = tomoyo_cred_alloc_blank,
Kentaro Takedaf7433242009-02-05 17:18:16 +0900268 .cred_prepare = tomoyo_cred_prepare,
David Howellsee18d642009-09-02 09:14:21 +0100269 .cred_transfer = tomoyo_cred_transfer,
Kentaro Takedaf7433242009-02-05 17:18:16 +0900270 .bprm_set_creds = tomoyo_bprm_set_creds,
271 .bprm_check_security = tomoyo_bprm_check_security,
Kentaro Takedaf7433242009-02-05 17:18:16 +0900272 .file_fcntl = tomoyo_file_fcntl,
273 .dentry_open = tomoyo_dentry_open,
274 .path_truncate = tomoyo_path_truncate,
275 .path_unlink = tomoyo_path_unlink,
276 .path_mkdir = tomoyo_path_mkdir,
277 .path_rmdir = tomoyo_path_rmdir,
278 .path_symlink = tomoyo_path_symlink,
279 .path_mknod = tomoyo_path_mknod,
280 .path_link = tomoyo_path_link,
281 .path_rename = tomoyo_path_rename,
Tetsuo Handa937bf612009-12-02 21:09:48 +0900282 .file_ioctl = tomoyo_file_ioctl,
283 .path_chmod = tomoyo_path_chmod,
284 .path_chown = tomoyo_path_chown,
285 .path_chroot = tomoyo_path_chroot,
286 .sb_mount = tomoyo_sb_mount,
287 .sb_umount = tomoyo_sb_umount,
288 .sb_pivotroot = tomoyo_sb_pivotroot,
Kentaro Takedaf7433242009-02-05 17:18:16 +0900289};
290
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900291/* Lock for GC. */
292struct srcu_struct tomoyo_ss;
293
Kentaro Takedaf7433242009-02-05 17:18:16 +0900294static int __init tomoyo_init(void)
295{
296 struct cred *cred = (struct cred *) current_cred();
297
298 if (!security_module_enable(&tomoyo_security_ops))
299 return 0;
300 /* register ourselves with the security framework */
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900301 if (register_security(&tomoyo_security_ops) ||
302 init_srcu_struct(&tomoyo_ss))
Kentaro Takedaf7433242009-02-05 17:18:16 +0900303 panic("Failure registering TOMOYO Linux");
304 printk(KERN_INFO "TOMOYO Linux initialized\n");
305 cred->security = &tomoyo_kernel_domain;
Tetsuo Handa1581e7d2009-02-21 20:40:50 +0900306 tomoyo_realpath_init();
Kentaro Takedaf7433242009-02-05 17:18:16 +0900307 return 0;
308}
309
310security_initcall(tomoyo_init);