blob: d6f68a0ec2dcd8990d0a68df9b82489556c13b49 [file] [log] [blame]
Kentaro Takedaf7433242009-02-05 17:18:16 +09001/*
2 * security/tomoyo/tomoyo.c
3 *
4 * LSM hooks for TOMOYO Linux.
5 *
Tetsuo Handac3ef1502010-05-17 10:12:46 +09006 * Copyright (C) 2005-2010 NTT DATA CORPORATION
Kentaro Takedaf7433242009-02-05 17:18:16 +09007 */
8
9#include <linux/security.h>
10#include "common.h"
Kentaro Takedaf7433242009-02-05 17:18:16 +090011
David Howellsee18d642009-09-02 09:14:21 +010012static int tomoyo_cred_alloc_blank(struct cred *new, gfp_t gfp)
13{
14 new->security = NULL;
15 return 0;
16}
17
Kentaro Takedaf7433242009-02-05 17:18:16 +090018static int tomoyo_cred_prepare(struct cred *new, const struct cred *old,
19 gfp_t gfp)
20{
Tetsuo Handaec8e6a42010-02-11 09:43:20 +090021 struct tomoyo_domain_info *domain = old->security;
22 new->security = domain;
23 if (domain)
24 atomic_inc(&domain->users);
Kentaro Takedaf7433242009-02-05 17:18:16 +090025 return 0;
26}
27
David Howellsee18d642009-09-02 09:14:21 +010028static void tomoyo_cred_transfer(struct cred *new, const struct cred *old)
29{
Tetsuo Handaec8e6a42010-02-11 09:43:20 +090030 tomoyo_cred_prepare(new, old, 0);
31}
32
33static void tomoyo_cred_free(struct cred *cred)
34{
35 struct tomoyo_domain_info *domain = cred->security;
36 if (domain)
37 atomic_dec(&domain->users);
David Howellsee18d642009-09-02 09:14:21 +010038}
39
Kentaro Takedaf7433242009-02-05 17:18:16 +090040static int tomoyo_bprm_set_creds(struct linux_binprm *bprm)
41{
Herton Ronaldo Krzesinskib1338d12009-05-26 12:15:53 +090042 int rc;
43
44 rc = cap_bprm_set_creds(bprm);
45 if (rc)
46 return rc;
47
Kentaro Takedaf7433242009-02-05 17:18:16 +090048 /*
49 * Do only if this function is called for the first time of an execve
50 * operation.
51 */
52 if (bprm->cred_prepared)
53 return 0;
Tetsuo Handa7986cf22011-06-29 13:07:52 +090054#ifndef CONFIG_SECURITY_TOMOYO_OMIT_USERSPACE_LOADER
Kentaro Takedaf7433242009-02-05 17:18:16 +090055 /*
56 * Load policy if /sbin/tomoyo-init exists and /sbin/init is requested
57 * for the first time.
58 */
59 if (!tomoyo_policy_loaded)
60 tomoyo_load_policy(bprm->filename);
Tetsuo Handa7986cf22011-06-29 13:07:52 +090061#endif
Kentaro Takedaf7433242009-02-05 17:18:16 +090062 /*
Tetsuo Handaec8e6a42010-02-11 09:43:20 +090063 * Release reference to "struct tomoyo_domain_info" stored inside
64 * "bprm->cred->security". New reference to "struct tomoyo_domain_info"
65 * stored inside "bprm->cred->security" will be acquired later inside
66 * tomoyo_find_next_domain().
67 */
68 atomic_dec(&((struct tomoyo_domain_info *)
69 bprm->cred->security)->users);
70 /*
Kentaro Takedaf7433242009-02-05 17:18:16 +090071 * Tell tomoyo_bprm_check_security() is called for the first time of an
72 * execve operation.
73 */
74 bprm->cred->security = NULL;
75 return 0;
76}
77
78static int tomoyo_bprm_check_security(struct linux_binprm *bprm)
79{
80 struct tomoyo_domain_info *domain = bprm->cred->security;
81
82 /*
83 * Execute permission is checked against pathname passed to do_execve()
84 * using current domain.
85 */
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +090086 if (!domain) {
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +090087 const int idx = tomoyo_read_lock();
88 const int err = tomoyo_find_next_domain(bprm);
89 tomoyo_read_unlock(idx);
90 return err;
91 }
Kentaro Takedaf7433242009-02-05 17:18:16 +090092 /*
93 * Read permission is checked against interpreters using next domain.
Kentaro Takedaf7433242009-02-05 17:18:16 +090094 */
Al Viro6d125522009-12-24 06:58:56 -050095 return tomoyo_check_open_permission(domain, &bprm->file->f_path, O_RDONLY);
Kentaro Takedaf7433242009-02-05 17:18:16 +090096}
97
Tetsuo Handa7c759642011-06-26 23:15:31 +090098static int tomoyo_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
99{
100 struct path path = { mnt, dentry };
101 return tomoyo_path_perm(TOMOYO_TYPE_GETATTR, &path);
102}
103
Tetsuo Handaea0d3ab2010-06-02 13:24:43 +0900104static int tomoyo_path_truncate(struct path *path)
Kentaro Takedaf7433242009-02-05 17:18:16 +0900105{
Tetsuo Handa97d69312010-02-16 09:46:15 +0900106 return tomoyo_path_perm(TOMOYO_TYPE_TRUNCATE, path);
Kentaro Takedaf7433242009-02-05 17:18:16 +0900107}
108
109static int tomoyo_path_unlink(struct path *parent, struct dentry *dentry)
110{
111 struct path path = { parent->mnt, dentry };
Tetsuo Handa97d69312010-02-16 09:46:15 +0900112 return tomoyo_path_perm(TOMOYO_TYPE_UNLINK, &path);
Kentaro Takedaf7433242009-02-05 17:18:16 +0900113}
114
115static int tomoyo_path_mkdir(struct path *parent, struct dentry *dentry,
116 int mode)
117{
118 struct path path = { parent->mnt, dentry };
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900119 return tomoyo_path_number_perm(TOMOYO_TYPE_MKDIR, &path,
120 mode & S_IALLUGO);
Kentaro Takedaf7433242009-02-05 17:18:16 +0900121}
122
123static int tomoyo_path_rmdir(struct path *parent, struct dentry *dentry)
124{
125 struct path path = { parent->mnt, dentry };
Tetsuo Handa97d69312010-02-16 09:46:15 +0900126 return tomoyo_path_perm(TOMOYO_TYPE_RMDIR, &path);
Kentaro Takedaf7433242009-02-05 17:18:16 +0900127}
128
129static int tomoyo_path_symlink(struct path *parent, struct dentry *dentry,
130 const char *old_name)
131{
132 struct path path = { parent->mnt, dentry };
Tetsuo Handa97d69312010-02-16 09:46:15 +0900133 return tomoyo_path_perm(TOMOYO_TYPE_SYMLINK, &path);
Kentaro Takedaf7433242009-02-05 17:18:16 +0900134}
135
136static int tomoyo_path_mknod(struct path *parent, struct dentry *dentry,
137 int mode, unsigned int dev)
138{
139 struct path path = { parent->mnt, dentry };
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900140 int type = TOMOYO_TYPE_CREATE;
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900141 const unsigned int perm = mode & S_IALLUGO;
Kentaro Takedaf7433242009-02-05 17:18:16 +0900142
143 switch (mode & S_IFMT) {
144 case S_IFCHR:
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900145 type = TOMOYO_TYPE_MKCHAR;
Kentaro Takedaf7433242009-02-05 17:18:16 +0900146 break;
147 case S_IFBLK:
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900148 type = TOMOYO_TYPE_MKBLOCK;
Kentaro Takedaf7433242009-02-05 17:18:16 +0900149 break;
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900150 default:
151 goto no_dev;
152 }
Tetsuo Handa75093152010-06-16 16:23:55 +0900153 return tomoyo_mkdev_perm(type, &path, perm, dev);
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900154 no_dev:
155 switch (mode & S_IFMT) {
Kentaro Takedaf7433242009-02-05 17:18:16 +0900156 case S_IFIFO:
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900157 type = TOMOYO_TYPE_MKFIFO;
Kentaro Takedaf7433242009-02-05 17:18:16 +0900158 break;
159 case S_IFSOCK:
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900160 type = TOMOYO_TYPE_MKSOCK;
Kentaro Takedaf7433242009-02-05 17:18:16 +0900161 break;
162 }
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900163 return tomoyo_path_number_perm(type, &path, perm);
Kentaro Takedaf7433242009-02-05 17:18:16 +0900164}
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 };
Tetsuo Handa97d69312010-02-16 09:46:15 +0900171 return tomoyo_path2_perm(TOMOYO_TYPE_LINK, &path1, &path2);
Kentaro Takedaf7433242009-02-05 17:18:16 +0900172}
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 };
Tetsuo Handa97d69312010-02-16 09:46:15 +0900181 return tomoyo_path2_perm(TOMOYO_TYPE_RENAME, &path1, &path2);
Kentaro Takedaf7433242009-02-05 17:18:16 +0900182}
183
184static int tomoyo_file_fcntl(struct file *file, unsigned int cmd,
185 unsigned long arg)
186{
Tetsuo Handa7c759642011-06-26 23:15:31 +0900187 if (!(cmd == F_SETFL && ((arg ^ file->f_flags) & O_APPEND)))
188 return 0;
189 return tomoyo_check_open_permission(tomoyo_domain(), &file->f_path,
190 O_WRONLY | (arg & O_APPEND));
Kentaro Takedaf7433242009-02-05 17:18:16 +0900191}
192
193static int tomoyo_dentry_open(struct file *f, const struct cred *cred)
194{
195 int flags = f->f_flags;
Kentaro Takedaf7433242009-02-05 17:18:16 +0900196 /* Don't check read permission here if called from do_execve(). */
197 if (current->in_execve)
198 return 0;
199 return tomoyo_check_open_permission(tomoyo_domain(), &f->f_path, flags);
200}
201
Tetsuo Handa937bf612009-12-02 21:09:48 +0900202static int tomoyo_file_ioctl(struct file *file, unsigned int cmd,
203 unsigned long arg)
204{
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900205 return tomoyo_path_number_perm(TOMOYO_TYPE_IOCTL, &file->f_path, cmd);
Tetsuo Handa937bf612009-12-02 21:09:48 +0900206}
207
208static int tomoyo_path_chmod(struct dentry *dentry, struct vfsmount *mnt,
209 mode_t mode)
210{
211 struct path path = { mnt, dentry };
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900212 return tomoyo_path_number_perm(TOMOYO_TYPE_CHMOD, &path,
213 mode & S_IALLUGO);
Tetsuo Handa937bf612009-12-02 21:09:48 +0900214}
215
216static int tomoyo_path_chown(struct path *path, uid_t uid, gid_t gid)
217{
218 int error = 0;
219 if (uid != (uid_t) -1)
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900220 error = tomoyo_path_number_perm(TOMOYO_TYPE_CHOWN, path, uid);
Tetsuo Handa937bf612009-12-02 21:09:48 +0900221 if (!error && gid != (gid_t) -1)
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900222 error = tomoyo_path_number_perm(TOMOYO_TYPE_CHGRP, path, gid);
Tetsuo Handa937bf612009-12-02 21:09:48 +0900223 return error;
224}
225
226static int tomoyo_path_chroot(struct path *path)
227{
Tetsuo Handa97d69312010-02-16 09:46:15 +0900228 return tomoyo_path_perm(TOMOYO_TYPE_CHROOT, path);
Tetsuo Handa937bf612009-12-02 21:09:48 +0900229}
230
231static int tomoyo_sb_mount(char *dev_name, struct path *path,
232 char *type, unsigned long flags, void *data)
233{
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900234 return tomoyo_mount_permission(dev_name, path, type, flags, data);
Tetsuo Handa937bf612009-12-02 21:09:48 +0900235}
236
237static int tomoyo_sb_umount(struct vfsmount *mnt, int flags)
238{
239 struct path path = { mnt, mnt->mnt_root };
Tetsuo Handa97d69312010-02-16 09:46:15 +0900240 return tomoyo_path_perm(TOMOYO_TYPE_UMOUNT, &path);
Tetsuo Handa937bf612009-12-02 21:09:48 +0900241}
242
243static int tomoyo_sb_pivotroot(struct path *old_path, struct path *new_path)
244{
Tetsuo Handa97d69312010-02-16 09:46:15 +0900245 return tomoyo_path2_perm(TOMOYO_TYPE_PIVOT_ROOT, new_path, old_path);
Tetsuo Handa937bf612009-12-02 21:09:48 +0900246}
247
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900248/*
249 * tomoyo_security_ops is a "struct security_operations" which is used for
250 * registering TOMOYO.
251 */
Kentaro Takedaf7433242009-02-05 17:18:16 +0900252static struct security_operations tomoyo_security_ops = {
253 .name = "tomoyo",
David Howellsee18d642009-09-02 09:14:21 +0100254 .cred_alloc_blank = tomoyo_cred_alloc_blank,
Kentaro Takedaf7433242009-02-05 17:18:16 +0900255 .cred_prepare = tomoyo_cred_prepare,
David Howellsee18d642009-09-02 09:14:21 +0100256 .cred_transfer = tomoyo_cred_transfer,
Tetsuo Handaec8e6a42010-02-11 09:43:20 +0900257 .cred_free = tomoyo_cred_free,
Kentaro Takedaf7433242009-02-05 17:18:16 +0900258 .bprm_set_creds = tomoyo_bprm_set_creds,
259 .bprm_check_security = tomoyo_bprm_check_security,
Kentaro Takedaf7433242009-02-05 17:18:16 +0900260 .file_fcntl = tomoyo_file_fcntl,
261 .dentry_open = tomoyo_dentry_open,
262 .path_truncate = tomoyo_path_truncate,
263 .path_unlink = tomoyo_path_unlink,
264 .path_mkdir = tomoyo_path_mkdir,
265 .path_rmdir = tomoyo_path_rmdir,
266 .path_symlink = tomoyo_path_symlink,
267 .path_mknod = tomoyo_path_mknod,
268 .path_link = tomoyo_path_link,
269 .path_rename = tomoyo_path_rename,
Tetsuo Handa7c759642011-06-26 23:15:31 +0900270 .inode_getattr = tomoyo_inode_getattr,
Tetsuo Handa937bf612009-12-02 21:09:48 +0900271 .file_ioctl = tomoyo_file_ioctl,
272 .path_chmod = tomoyo_path_chmod,
273 .path_chown = tomoyo_path_chown,
274 .path_chroot = tomoyo_path_chroot,
275 .sb_mount = tomoyo_sb_mount,
276 .sb_umount = tomoyo_sb_umount,
277 .sb_pivotroot = tomoyo_sb_pivotroot,
Kentaro Takedaf7433242009-02-05 17:18:16 +0900278};
279
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900280/* Lock for GC. */
281struct srcu_struct tomoyo_ss;
282
Kentaro Takedaf7433242009-02-05 17:18:16 +0900283static int __init tomoyo_init(void)
284{
285 struct cred *cred = (struct cred *) current_cred();
286
287 if (!security_module_enable(&tomoyo_security_ops))
288 return 0;
289 /* register ourselves with the security framework */
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900290 if (register_security(&tomoyo_security_ops) ||
291 init_srcu_struct(&tomoyo_ss))
Kentaro Takedaf7433242009-02-05 17:18:16 +0900292 panic("Failure registering TOMOYO Linux");
293 printk(KERN_INFO "TOMOYO Linux initialized\n");
294 cred->security = &tomoyo_kernel_domain;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900295 tomoyo_mm_init();
Kentaro Takedaf7433242009-02-05 17:18:16 +0900296 return 0;
297}
298
299security_initcall(tomoyo_init);