blob: e3945d0511b886aae1cd98d172a59cc55a5701c9 [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{
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900103 return tomoyo_path_perm(tomoyo_domain(), TOMOYO_TYPE_TRUNCATE, path);
Kentaro Takedaf7433242009-02-05 17:18:16 +0900104}
105
106static int tomoyo_path_unlink(struct path *parent, struct dentry *dentry)
107{
108 struct path path = { parent->mnt, dentry };
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900109 return tomoyo_path_perm(tomoyo_domain(), TOMOYO_TYPE_UNLINK, &path);
Kentaro Takedaf7433242009-02-05 17:18:16 +0900110}
111
112static int tomoyo_path_mkdir(struct path *parent, struct dentry *dentry,
113 int mode)
114{
115 struct path path = { parent->mnt, dentry };
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900116 return tomoyo_path_perm(tomoyo_domain(), TOMOYO_TYPE_MKDIR, &path);
Kentaro Takedaf7433242009-02-05 17:18:16 +0900117}
118
119static int tomoyo_path_rmdir(struct path *parent, struct dentry *dentry)
120{
121 struct path path = { parent->mnt, dentry };
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900122 return tomoyo_path_perm(tomoyo_domain(), TOMOYO_TYPE_RMDIR, &path);
Kentaro Takedaf7433242009-02-05 17:18:16 +0900123}
124
125static int tomoyo_path_symlink(struct path *parent, struct dentry *dentry,
126 const char *old_name)
127{
128 struct path path = { parent->mnt, dentry };
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900129 return tomoyo_path_perm(tomoyo_domain(), TOMOYO_TYPE_SYMLINK, &path);
Kentaro Takedaf7433242009-02-05 17:18:16 +0900130}
131
132static int tomoyo_path_mknod(struct path *parent, struct dentry *dentry,
133 int mode, unsigned int dev)
134{
135 struct path path = { parent->mnt, dentry };
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900136 int type = TOMOYO_TYPE_CREATE;
Kentaro Takedaf7433242009-02-05 17:18:16 +0900137
138 switch (mode & S_IFMT) {
139 case S_IFCHR:
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900140 type = TOMOYO_TYPE_MKCHAR;
Kentaro Takedaf7433242009-02-05 17:18:16 +0900141 break;
142 case S_IFBLK:
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900143 type = TOMOYO_TYPE_MKBLOCK;
Kentaro Takedaf7433242009-02-05 17:18:16 +0900144 break;
145 case S_IFIFO:
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900146 type = TOMOYO_TYPE_MKFIFO;
Kentaro Takedaf7433242009-02-05 17:18:16 +0900147 break;
148 case S_IFSOCK:
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900149 type = TOMOYO_TYPE_MKSOCK;
Kentaro Takedaf7433242009-02-05 17:18:16 +0900150 break;
151 }
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900152 return tomoyo_path_perm(tomoyo_domain(), type, &path);
Kentaro Takedaf7433242009-02-05 17:18:16 +0900153}
154
155static int tomoyo_path_link(struct dentry *old_dentry, struct path *new_dir,
156 struct dentry *new_dentry)
157{
158 struct path path1 = { new_dir->mnt, old_dentry };
159 struct path path2 = { new_dir->mnt, new_dentry };
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900160 return tomoyo_path2_perm(tomoyo_domain(), TOMOYO_TYPE_LINK, &path1,
161 &path2);
Kentaro Takedaf7433242009-02-05 17:18:16 +0900162}
163
164static int tomoyo_path_rename(struct path *old_parent,
165 struct dentry *old_dentry,
166 struct path *new_parent,
167 struct dentry *new_dentry)
168{
169 struct path path1 = { old_parent->mnt, old_dentry };
170 struct path path2 = { new_parent->mnt, new_dentry };
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900171 return tomoyo_path2_perm(tomoyo_domain(), TOMOYO_TYPE_RENAME, &path1,
172 &path2);
Kentaro Takedaf7433242009-02-05 17:18:16 +0900173}
174
175static int tomoyo_file_fcntl(struct file *file, unsigned int cmd,
176 unsigned long arg)
177{
178 if (cmd == F_SETFL && ((arg ^ file->f_flags) & O_APPEND))
179 return tomoyo_check_rewrite_permission(tomoyo_domain(), file);
180 return 0;
181}
182
183static int tomoyo_dentry_open(struct file *f, const struct cred *cred)
184{
185 int flags = f->f_flags;
186
187 if ((flags + 1) & O_ACCMODE)
188 flags++;
189 flags |= f->f_flags & (O_APPEND | O_TRUNC);
190 /* Don't check read permission here if called from do_execve(). */
191 if (current->in_execve)
192 return 0;
193 return tomoyo_check_open_permission(tomoyo_domain(), &f->f_path, flags);
194}
195
Tetsuo Handa937bf612009-12-02 21:09:48 +0900196static int tomoyo_file_ioctl(struct file *file, unsigned int cmd,
197 unsigned long arg)
198{
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900199 return tomoyo_path_perm(tomoyo_domain(), TOMOYO_TYPE_IOCTL,
200 &file->f_path);
Tetsuo Handa937bf612009-12-02 21:09:48 +0900201}
202
203static int tomoyo_path_chmod(struct dentry *dentry, struct vfsmount *mnt,
204 mode_t mode)
205{
206 struct path path = { mnt, dentry };
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900207 return tomoyo_path_perm(tomoyo_domain(), TOMOYO_TYPE_CHMOD, &path);
Tetsuo Handa937bf612009-12-02 21:09:48 +0900208}
209
210static int tomoyo_path_chown(struct path *path, uid_t uid, gid_t gid)
211{
212 int error = 0;
213 if (uid != (uid_t) -1)
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900214 error = tomoyo_path_perm(tomoyo_domain(), TOMOYO_TYPE_CHOWN,
215 path);
Tetsuo Handa937bf612009-12-02 21:09:48 +0900216 if (!error && gid != (gid_t) -1)
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900217 error = tomoyo_path_perm(tomoyo_domain(), TOMOYO_TYPE_CHGRP,
218 path);
Tetsuo Handa937bf612009-12-02 21:09:48 +0900219 return error;
220}
221
222static int tomoyo_path_chroot(struct path *path)
223{
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900224 return tomoyo_path_perm(tomoyo_domain(), TOMOYO_TYPE_CHROOT, path);
Tetsuo Handa937bf612009-12-02 21:09:48 +0900225}
226
227static int tomoyo_sb_mount(char *dev_name, struct path *path,
228 char *type, unsigned long flags, void *data)
229{
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900230 return tomoyo_path_perm(tomoyo_domain(), TOMOYO_TYPE_MOUNT, path);
Tetsuo Handa937bf612009-12-02 21:09:48 +0900231}
232
233static int tomoyo_sb_umount(struct vfsmount *mnt, int flags)
234{
235 struct path path = { mnt, mnt->mnt_root };
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900236 return tomoyo_path_perm(tomoyo_domain(), TOMOYO_TYPE_UMOUNT, &path);
Tetsuo Handa937bf612009-12-02 21:09:48 +0900237}
238
239static int tomoyo_sb_pivotroot(struct path *old_path, struct path *new_path)
240{
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900241 return tomoyo_path2_perm(tomoyo_domain(), TOMOYO_TYPE_PIVOT_ROOT,
242 new_path, old_path);
Tetsuo Handa937bf612009-12-02 21:09:48 +0900243}
244
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900245/*
246 * tomoyo_security_ops is a "struct security_operations" which is used for
247 * registering TOMOYO.
248 */
Kentaro Takedaf7433242009-02-05 17:18:16 +0900249static struct security_operations tomoyo_security_ops = {
250 .name = "tomoyo",
David Howellsee18d642009-09-02 09:14:21 +0100251 .cred_alloc_blank = tomoyo_cred_alloc_blank,
Kentaro Takedaf7433242009-02-05 17:18:16 +0900252 .cred_prepare = tomoyo_cred_prepare,
David Howellsee18d642009-09-02 09:14:21 +0100253 .cred_transfer = tomoyo_cred_transfer,
Tetsuo Handaec8e6a42010-02-11 09:43:20 +0900254 .cred_free = tomoyo_cred_free,
Kentaro Takedaf7433242009-02-05 17:18:16 +0900255 .bprm_set_creds = tomoyo_bprm_set_creds,
256 .bprm_check_security = tomoyo_bprm_check_security,
Kentaro Takedaf7433242009-02-05 17:18:16 +0900257 .file_fcntl = tomoyo_file_fcntl,
258 .dentry_open = tomoyo_dentry_open,
259 .path_truncate = tomoyo_path_truncate,
260 .path_unlink = tomoyo_path_unlink,
261 .path_mkdir = tomoyo_path_mkdir,
262 .path_rmdir = tomoyo_path_rmdir,
263 .path_symlink = tomoyo_path_symlink,
264 .path_mknod = tomoyo_path_mknod,
265 .path_link = tomoyo_path_link,
266 .path_rename = tomoyo_path_rename,
Tetsuo Handa937bf612009-12-02 21:09:48 +0900267 .file_ioctl = tomoyo_file_ioctl,
268 .path_chmod = tomoyo_path_chmod,
269 .path_chown = tomoyo_path_chown,
270 .path_chroot = tomoyo_path_chroot,
271 .sb_mount = tomoyo_sb_mount,
272 .sb_umount = tomoyo_sb_umount,
273 .sb_pivotroot = tomoyo_sb_pivotroot,
Kentaro Takedaf7433242009-02-05 17:18:16 +0900274};
275
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900276/* Lock for GC. */
277struct srcu_struct tomoyo_ss;
278
Kentaro Takedaf7433242009-02-05 17:18:16 +0900279static int __init tomoyo_init(void)
280{
281 struct cred *cred = (struct cred *) current_cred();
282
283 if (!security_module_enable(&tomoyo_security_ops))
284 return 0;
285 /* register ourselves with the security framework */
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900286 if (register_security(&tomoyo_security_ops) ||
287 init_srcu_struct(&tomoyo_ss))
Kentaro Takedaf7433242009-02-05 17:18:16 +0900288 panic("Failure registering TOMOYO Linux");
289 printk(KERN_INFO "TOMOYO Linux initialized\n");
290 cred->security = &tomoyo_kernel_domain;
Tetsuo Handa1581e7d2009-02-21 20:40:50 +0900291 tomoyo_realpath_init();
Kentaro Takedaf7433242009-02-05 17:18:16 +0900292 return 0;
293}
294
295security_initcall(tomoyo_init);