blob: 87e82bfeac2f5e38dae6badd227c33936e5681cb [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"
Kentaro Takedaf7433242009-02-05 17:18:16 +090014
David Howellsee18d642009-09-02 09:14:21 +010015static int tomoyo_cred_alloc_blank(struct cred *new, gfp_t gfp)
16{
17 new->security = NULL;
18 return 0;
19}
20
Kentaro Takedaf7433242009-02-05 17:18:16 +090021static int tomoyo_cred_prepare(struct cred *new, const struct cred *old,
22 gfp_t gfp)
23{
Tetsuo Handaec8e6a42010-02-11 09:43:20 +090024 struct tomoyo_domain_info *domain = old->security;
25 new->security = domain;
26 if (domain)
27 atomic_inc(&domain->users);
Kentaro Takedaf7433242009-02-05 17:18:16 +090028 return 0;
29}
30
David Howellsee18d642009-09-02 09:14:21 +010031static void tomoyo_cred_transfer(struct cred *new, const struct cred *old)
32{
Tetsuo Handaec8e6a42010-02-11 09:43:20 +090033 tomoyo_cred_prepare(new, old, 0);
34}
35
36static void tomoyo_cred_free(struct cred *cred)
37{
38 struct tomoyo_domain_info *domain = cred->security;
39 if (domain)
40 atomic_dec(&domain->users);
David Howellsee18d642009-09-02 09:14:21 +010041}
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 /*
Tetsuo Handaec8e6a42010-02-11 09:43:20 +090064 * 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().
68 */
69 atomic_dec(&((struct tomoyo_domain_info *)
70 bprm->cred->security)->users);
71 /*
Kentaro Takedaf7433242009-02-05 17:18:16 +090072 * Tell tomoyo_bprm_check_security() is called for the first time of an
73 * execve operation.
74 */
75 bprm->cred->security = NULL;
76 return 0;
77}
78
79static int tomoyo_bprm_check_security(struct linux_binprm *bprm)
80{
81 struct tomoyo_domain_info *domain = bprm->cred->security;
82
83 /*
84 * Execute permission is checked against pathname passed to do_execve()
85 * using current domain.
86 */
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +090087 if (!domain) {
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +090088 const int idx = tomoyo_read_lock();
89 const int err = tomoyo_find_next_domain(bprm);
90 tomoyo_read_unlock(idx);
91 return err;
92 }
Kentaro Takedaf7433242009-02-05 17:18:16 +090093 /*
94 * Read permission is checked against interpreters using next domain.
95 * '1' is the result of open_to_namei_flags(O_RDONLY).
96 */
97 return tomoyo_check_open_permission(domain, &bprm->file->f_path, 1);
98}
99
Kentaro Takedaf7433242009-02-05 17:18:16 +0900100static int tomoyo_path_truncate(struct path *path, loff_t length,
101 unsigned int time_attrs)
102{
103 return tomoyo_check_1path_perm(tomoyo_domain(),
104 TOMOYO_TYPE_TRUNCATE_ACL,
105 path);
106}
107
108static int tomoyo_path_unlink(struct path *parent, struct dentry *dentry)
109{
110 struct path path = { parent->mnt, dentry };
111 return tomoyo_check_1path_perm(tomoyo_domain(),
112 TOMOYO_TYPE_UNLINK_ACL,
113 &path);
114}
115
116static int tomoyo_path_mkdir(struct path *parent, struct dentry *dentry,
117 int mode)
118{
119 struct path path = { parent->mnt, dentry };
120 return tomoyo_check_1path_perm(tomoyo_domain(),
121 TOMOYO_TYPE_MKDIR_ACL,
122 &path);
123}
124
125static int tomoyo_path_rmdir(struct path *parent, struct dentry *dentry)
126{
127 struct path path = { parent->mnt, dentry };
128 return tomoyo_check_1path_perm(tomoyo_domain(),
129 TOMOYO_TYPE_RMDIR_ACL,
130 &path);
131}
132
133static int tomoyo_path_symlink(struct path *parent, struct dentry *dentry,
134 const char *old_name)
135{
136 struct path path = { parent->mnt, dentry };
137 return tomoyo_check_1path_perm(tomoyo_domain(),
138 TOMOYO_TYPE_SYMLINK_ACL,
139 &path);
140}
141
142static int tomoyo_path_mknod(struct path *parent, struct dentry *dentry,
143 int mode, unsigned int dev)
144{
145 struct path path = { parent->mnt, dentry };
146 int type = TOMOYO_TYPE_CREATE_ACL;
147
148 switch (mode & S_IFMT) {
149 case S_IFCHR:
150 type = TOMOYO_TYPE_MKCHAR_ACL;
151 break;
152 case S_IFBLK:
153 type = TOMOYO_TYPE_MKBLOCK_ACL;
154 break;
155 case S_IFIFO:
156 type = TOMOYO_TYPE_MKFIFO_ACL;
157 break;
158 case S_IFSOCK:
159 type = TOMOYO_TYPE_MKSOCK_ACL;
160 break;
161 }
162 return tomoyo_check_1path_perm(tomoyo_domain(),
163 type, &path);
164}
165
166static int tomoyo_path_link(struct dentry *old_dentry, struct path *new_dir,
167 struct dentry *new_dentry)
168{
169 struct path path1 = { new_dir->mnt, old_dentry };
170 struct path path2 = { new_dir->mnt, new_dentry };
171 return tomoyo_check_2path_perm(tomoyo_domain(),
172 TOMOYO_TYPE_LINK_ACL,
173 &path1, &path2);
174}
175
176static int tomoyo_path_rename(struct path *old_parent,
177 struct dentry *old_dentry,
178 struct path *new_parent,
179 struct dentry *new_dentry)
180{
181 struct path path1 = { old_parent->mnt, old_dentry };
182 struct path path2 = { new_parent->mnt, new_dentry };
183 return tomoyo_check_2path_perm(tomoyo_domain(),
184 TOMOYO_TYPE_RENAME_ACL,
185 &path1, &path2);
186}
187
188static int tomoyo_file_fcntl(struct file *file, unsigned int cmd,
189 unsigned long arg)
190{
191 if (cmd == F_SETFL && ((arg ^ file->f_flags) & O_APPEND))
192 return tomoyo_check_rewrite_permission(tomoyo_domain(), file);
193 return 0;
194}
195
196static int tomoyo_dentry_open(struct file *f, const struct cred *cred)
197{
198 int flags = f->f_flags;
199
200 if ((flags + 1) & O_ACCMODE)
201 flags++;
202 flags |= f->f_flags & (O_APPEND | O_TRUNC);
203 /* Don't check read permission here if called from do_execve(). */
204 if (current->in_execve)
205 return 0;
206 return tomoyo_check_open_permission(tomoyo_domain(), &f->f_path, flags);
207}
208
Tetsuo Handa937bf612009-12-02 21:09:48 +0900209static int tomoyo_file_ioctl(struct file *file, unsigned int cmd,
210 unsigned long arg)
211{
212 return tomoyo_check_1path_perm(tomoyo_domain(), TOMOYO_TYPE_IOCTL_ACL,
213 &file->f_path);
214}
215
216static int tomoyo_path_chmod(struct dentry *dentry, struct vfsmount *mnt,
217 mode_t mode)
218{
219 struct path path = { mnt, dentry };
220 return tomoyo_check_1path_perm(tomoyo_domain(), TOMOYO_TYPE_CHMOD_ACL,
221 &path);
222}
223
224static int tomoyo_path_chown(struct path *path, uid_t uid, gid_t gid)
225{
226 int error = 0;
227 if (uid != (uid_t) -1)
228 error = tomoyo_check_1path_perm(tomoyo_domain(),
229 TOMOYO_TYPE_CHOWN_ACL, path);
230 if (!error && gid != (gid_t) -1)
231 error = tomoyo_check_1path_perm(tomoyo_domain(),
232 TOMOYO_TYPE_CHGRP_ACL, path);
233 return error;
234}
235
236static int tomoyo_path_chroot(struct path *path)
237{
238 return tomoyo_check_1path_perm(tomoyo_domain(), TOMOYO_TYPE_CHROOT_ACL,
239 path);
240}
241
242static int tomoyo_sb_mount(char *dev_name, struct path *path,
243 char *type, unsigned long flags, void *data)
244{
245 return tomoyo_check_1path_perm(tomoyo_domain(), TOMOYO_TYPE_MOUNT_ACL,
246 path);
247}
248
249static int tomoyo_sb_umount(struct vfsmount *mnt, int flags)
250{
251 struct path path = { mnt, mnt->mnt_root };
252 return tomoyo_check_1path_perm(tomoyo_domain(), TOMOYO_TYPE_UMOUNT_ACL,
253 &path);
254}
255
256static int tomoyo_sb_pivotroot(struct path *old_path, struct path *new_path)
257{
258 return tomoyo_check_2path_perm(tomoyo_domain(),
259 TOMOYO_TYPE_PIVOT_ROOT_ACL,
260 new_path, old_path);
261}
262
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900263/*
264 * tomoyo_security_ops is a "struct security_operations" which is used for
265 * registering TOMOYO.
266 */
Kentaro Takedaf7433242009-02-05 17:18:16 +0900267static struct security_operations tomoyo_security_ops = {
268 .name = "tomoyo",
David Howellsee18d642009-09-02 09:14:21 +0100269 .cred_alloc_blank = tomoyo_cred_alloc_blank,
Kentaro Takedaf7433242009-02-05 17:18:16 +0900270 .cred_prepare = tomoyo_cred_prepare,
David Howellsee18d642009-09-02 09:14:21 +0100271 .cred_transfer = tomoyo_cred_transfer,
Tetsuo Handaec8e6a42010-02-11 09:43:20 +0900272 .cred_free = tomoyo_cred_free,
Kentaro Takedaf7433242009-02-05 17:18:16 +0900273 .bprm_set_creds = tomoyo_bprm_set_creds,
274 .bprm_check_security = tomoyo_bprm_check_security,
Kentaro Takedaf7433242009-02-05 17:18:16 +0900275 .file_fcntl = tomoyo_file_fcntl,
276 .dentry_open = tomoyo_dentry_open,
277 .path_truncate = tomoyo_path_truncate,
278 .path_unlink = tomoyo_path_unlink,
279 .path_mkdir = tomoyo_path_mkdir,
280 .path_rmdir = tomoyo_path_rmdir,
281 .path_symlink = tomoyo_path_symlink,
282 .path_mknod = tomoyo_path_mknod,
283 .path_link = tomoyo_path_link,
284 .path_rename = tomoyo_path_rename,
Tetsuo Handa937bf612009-12-02 21:09:48 +0900285 .file_ioctl = tomoyo_file_ioctl,
286 .path_chmod = tomoyo_path_chmod,
287 .path_chown = tomoyo_path_chown,
288 .path_chroot = tomoyo_path_chroot,
289 .sb_mount = tomoyo_sb_mount,
290 .sb_umount = tomoyo_sb_umount,
291 .sb_pivotroot = tomoyo_sb_pivotroot,
Kentaro Takedaf7433242009-02-05 17:18:16 +0900292};
293
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900294/* Lock for GC. */
295struct srcu_struct tomoyo_ss;
296
Kentaro Takedaf7433242009-02-05 17:18:16 +0900297static int __init tomoyo_init(void)
298{
299 struct cred *cred = (struct cred *) current_cred();
300
301 if (!security_module_enable(&tomoyo_security_ops))
302 return 0;
303 /* register ourselves with the security framework */
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900304 if (register_security(&tomoyo_security_ops) ||
305 init_srcu_struct(&tomoyo_ss))
Kentaro Takedaf7433242009-02-05 17:18:16 +0900306 panic("Failure registering TOMOYO Linux");
307 printk(KERN_INFO "TOMOYO Linux initialized\n");
308 cred->security = &tomoyo_kernel_domain;
Tetsuo Handa1581e7d2009-02-21 20:40:50 +0900309 tomoyo_realpath_init();
Kentaro Takedaf7433242009-02-05 17:18:16 +0900310 return 0;
311}
312
313security_initcall(tomoyo_init);