blob: 7be732cadd475fbb5c16269b33b786c6581bc9a7 [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;
54 /*
55 * Load policy if /sbin/tomoyo-init exists and /sbin/init is requested
56 * for the first time.
57 */
58 if (!tomoyo_policy_loaded)
59 tomoyo_load_policy(bprm->filename);
60 /*
Tetsuo Handaec8e6a42010-02-11 09:43:20 +090061 * Release reference to "struct tomoyo_domain_info" stored inside
62 * "bprm->cred->security". New reference to "struct tomoyo_domain_info"
63 * stored inside "bprm->cred->security" will be acquired later inside
64 * tomoyo_find_next_domain().
65 */
66 atomic_dec(&((struct tomoyo_domain_info *)
67 bprm->cred->security)->users);
68 /*
Kentaro Takedaf7433242009-02-05 17:18:16 +090069 * Tell tomoyo_bprm_check_security() is called for the first time of an
70 * execve operation.
71 */
72 bprm->cred->security = NULL;
73 return 0;
74}
75
76static int tomoyo_bprm_check_security(struct linux_binprm *bprm)
77{
78 struct tomoyo_domain_info *domain = bprm->cred->security;
79
80 /*
81 * Execute permission is checked against pathname passed to do_execve()
82 * using current domain.
83 */
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +090084 if (!domain) {
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +090085 const int idx = tomoyo_read_lock();
86 const int err = tomoyo_find_next_domain(bprm);
87 tomoyo_read_unlock(idx);
88 return err;
89 }
Kentaro Takedaf7433242009-02-05 17:18:16 +090090 /*
91 * Read permission is checked against interpreters using next domain.
Kentaro Takedaf7433242009-02-05 17:18:16 +090092 */
Al Viro6d125522009-12-24 06:58:56 -050093 return tomoyo_check_open_permission(domain, &bprm->file->f_path, O_RDONLY);
Kentaro Takedaf7433242009-02-05 17:18:16 +090094}
95
Tetsuo Handaea0d3ab2010-06-02 13:24:43 +090096static int tomoyo_path_truncate(struct path *path)
Kentaro Takedaf7433242009-02-05 17:18:16 +090097{
Tetsuo Handa97d69312010-02-16 09:46:15 +090098 return tomoyo_path_perm(TOMOYO_TYPE_TRUNCATE, path);
Kentaro Takedaf7433242009-02-05 17:18:16 +090099}
100
101static int tomoyo_path_unlink(struct path *parent, struct dentry *dentry)
102{
103 struct path path = { parent->mnt, dentry };
Tetsuo Handa97d69312010-02-16 09:46:15 +0900104 return tomoyo_path_perm(TOMOYO_TYPE_UNLINK, &path);
Kentaro Takedaf7433242009-02-05 17:18:16 +0900105}
106
107static int tomoyo_path_mkdir(struct path *parent, struct dentry *dentry,
108 int mode)
109{
110 struct path path = { parent->mnt, dentry };
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900111 return tomoyo_path_number_perm(TOMOYO_TYPE_MKDIR, &path,
112 mode & S_IALLUGO);
Kentaro Takedaf7433242009-02-05 17:18:16 +0900113}
114
115static int tomoyo_path_rmdir(struct path *parent, struct dentry *dentry)
116{
117 struct path path = { parent->mnt, dentry };
Tetsuo Handa97d69312010-02-16 09:46:15 +0900118 return tomoyo_path_perm(TOMOYO_TYPE_RMDIR, &path);
Kentaro Takedaf7433242009-02-05 17:18:16 +0900119}
120
121static int tomoyo_path_symlink(struct path *parent, struct dentry *dentry,
122 const char *old_name)
123{
124 struct path path = { parent->mnt, dentry };
Tetsuo Handa97d69312010-02-16 09:46:15 +0900125 return tomoyo_path_perm(TOMOYO_TYPE_SYMLINK, &path);
Kentaro Takedaf7433242009-02-05 17:18:16 +0900126}
127
128static int tomoyo_path_mknod(struct path *parent, struct dentry *dentry,
129 int mode, unsigned int dev)
130{
131 struct path path = { parent->mnt, dentry };
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900132 int type = TOMOYO_TYPE_CREATE;
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900133 const unsigned int perm = mode & S_IALLUGO;
Kentaro Takedaf7433242009-02-05 17:18:16 +0900134
135 switch (mode & S_IFMT) {
136 case S_IFCHR:
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900137 type = TOMOYO_TYPE_MKCHAR;
Kentaro Takedaf7433242009-02-05 17:18:16 +0900138 break;
139 case S_IFBLK:
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900140 type = TOMOYO_TYPE_MKBLOCK;
Kentaro Takedaf7433242009-02-05 17:18:16 +0900141 break;
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900142 default:
143 goto no_dev;
144 }
145 return tomoyo_path_number3_perm(type, &path, perm, dev);
146 no_dev:
147 switch (mode & S_IFMT) {
Kentaro Takedaf7433242009-02-05 17:18:16 +0900148 case S_IFIFO:
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900149 type = TOMOYO_TYPE_MKFIFO;
Kentaro Takedaf7433242009-02-05 17:18:16 +0900150 break;
151 case S_IFSOCK:
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900152 type = TOMOYO_TYPE_MKSOCK;
Kentaro Takedaf7433242009-02-05 17:18:16 +0900153 break;
154 }
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900155 return tomoyo_path_number_perm(type, &path, perm);
Kentaro Takedaf7433242009-02-05 17:18:16 +0900156}
157
158static int tomoyo_path_link(struct dentry *old_dentry, struct path *new_dir,
159 struct dentry *new_dentry)
160{
161 struct path path1 = { new_dir->mnt, old_dentry };
162 struct path path2 = { new_dir->mnt, new_dentry };
Tetsuo Handa97d69312010-02-16 09:46:15 +0900163 return tomoyo_path2_perm(TOMOYO_TYPE_LINK, &path1, &path2);
Kentaro Takedaf7433242009-02-05 17:18:16 +0900164}
165
166static int tomoyo_path_rename(struct path *old_parent,
167 struct dentry *old_dentry,
168 struct path *new_parent,
169 struct dentry *new_dentry)
170{
171 struct path path1 = { old_parent->mnt, old_dentry };
172 struct path path2 = { new_parent->mnt, new_dentry };
Tetsuo Handa97d69312010-02-16 09:46:15 +0900173 return tomoyo_path2_perm(TOMOYO_TYPE_RENAME, &path1, &path2);
Kentaro Takedaf7433242009-02-05 17:18:16 +0900174}
175
176static int tomoyo_file_fcntl(struct file *file, unsigned int cmd,
177 unsigned long arg)
178{
179 if (cmd == F_SETFL && ((arg ^ file->f_flags) & O_APPEND))
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900180 return tomoyo_path_perm(TOMOYO_TYPE_REWRITE, &file->f_path);
Kentaro Takedaf7433242009-02-05 17:18:16 +0900181 return 0;
182}
183
184static int tomoyo_dentry_open(struct file *f, const struct cred *cred)
185{
186 int flags = f->f_flags;
Kentaro Takedaf7433242009-02-05 17:18:16 +0900187 /* Don't check read permission here if called from do_execve(). */
188 if (current->in_execve)
189 return 0;
190 return tomoyo_check_open_permission(tomoyo_domain(), &f->f_path, flags);
191}
192
Tetsuo Handa937bf612009-12-02 21:09:48 +0900193static int tomoyo_file_ioctl(struct file *file, unsigned int cmd,
194 unsigned long arg)
195{
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900196 return tomoyo_path_number_perm(TOMOYO_TYPE_IOCTL, &file->f_path, cmd);
Tetsuo Handa937bf612009-12-02 21:09:48 +0900197}
198
199static int tomoyo_path_chmod(struct dentry *dentry, struct vfsmount *mnt,
200 mode_t mode)
201{
202 struct path path = { mnt, dentry };
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900203 return tomoyo_path_number_perm(TOMOYO_TYPE_CHMOD, &path,
204 mode & S_IALLUGO);
Tetsuo Handa937bf612009-12-02 21:09:48 +0900205}
206
207static int tomoyo_path_chown(struct path *path, uid_t uid, gid_t gid)
208{
209 int error = 0;
210 if (uid != (uid_t) -1)
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900211 error = tomoyo_path_number_perm(TOMOYO_TYPE_CHOWN, path, uid);
Tetsuo Handa937bf612009-12-02 21:09:48 +0900212 if (!error && gid != (gid_t) -1)
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900213 error = tomoyo_path_number_perm(TOMOYO_TYPE_CHGRP, path, gid);
Tetsuo Handa937bf612009-12-02 21:09:48 +0900214 return error;
215}
216
217static int tomoyo_path_chroot(struct path *path)
218{
Tetsuo Handa97d69312010-02-16 09:46:15 +0900219 return tomoyo_path_perm(TOMOYO_TYPE_CHROOT, path);
Tetsuo Handa937bf612009-12-02 21:09:48 +0900220}
221
222static int tomoyo_sb_mount(char *dev_name, struct path *path,
223 char *type, unsigned long flags, void *data)
224{
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900225 return tomoyo_mount_permission(dev_name, path, type, flags, data);
Tetsuo Handa937bf612009-12-02 21:09:48 +0900226}
227
228static int tomoyo_sb_umount(struct vfsmount *mnt, int flags)
229{
230 struct path path = { mnt, mnt->mnt_root };
Tetsuo Handa97d69312010-02-16 09:46:15 +0900231 return tomoyo_path_perm(TOMOYO_TYPE_UMOUNT, &path);
Tetsuo Handa937bf612009-12-02 21:09:48 +0900232}
233
234static int tomoyo_sb_pivotroot(struct path *old_path, struct path *new_path)
235{
Tetsuo Handa97d69312010-02-16 09:46:15 +0900236 return tomoyo_path2_perm(TOMOYO_TYPE_PIVOT_ROOT, new_path, old_path);
Tetsuo Handa937bf612009-12-02 21:09:48 +0900237}
238
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900239/*
240 * tomoyo_security_ops is a "struct security_operations" which is used for
241 * registering TOMOYO.
242 */
Kentaro Takedaf7433242009-02-05 17:18:16 +0900243static struct security_operations tomoyo_security_ops = {
244 .name = "tomoyo",
David Howellsee18d642009-09-02 09:14:21 +0100245 .cred_alloc_blank = tomoyo_cred_alloc_blank,
Kentaro Takedaf7433242009-02-05 17:18:16 +0900246 .cred_prepare = tomoyo_cred_prepare,
David Howellsee18d642009-09-02 09:14:21 +0100247 .cred_transfer = tomoyo_cred_transfer,
Tetsuo Handaec8e6a42010-02-11 09:43:20 +0900248 .cred_free = tomoyo_cred_free,
Kentaro Takedaf7433242009-02-05 17:18:16 +0900249 .bprm_set_creds = tomoyo_bprm_set_creds,
250 .bprm_check_security = tomoyo_bprm_check_security,
Kentaro Takedaf7433242009-02-05 17:18:16 +0900251 .file_fcntl = tomoyo_file_fcntl,
252 .dentry_open = tomoyo_dentry_open,
253 .path_truncate = tomoyo_path_truncate,
254 .path_unlink = tomoyo_path_unlink,
255 .path_mkdir = tomoyo_path_mkdir,
256 .path_rmdir = tomoyo_path_rmdir,
257 .path_symlink = tomoyo_path_symlink,
258 .path_mknod = tomoyo_path_mknod,
259 .path_link = tomoyo_path_link,
260 .path_rename = tomoyo_path_rename,
Tetsuo Handa937bf612009-12-02 21:09:48 +0900261 .file_ioctl = tomoyo_file_ioctl,
262 .path_chmod = tomoyo_path_chmod,
263 .path_chown = tomoyo_path_chown,
264 .path_chroot = tomoyo_path_chroot,
265 .sb_mount = tomoyo_sb_mount,
266 .sb_umount = tomoyo_sb_umount,
267 .sb_pivotroot = tomoyo_sb_pivotroot,
Kentaro Takedaf7433242009-02-05 17:18:16 +0900268};
269
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900270/* Lock for GC. */
271struct srcu_struct tomoyo_ss;
272
Kentaro Takedaf7433242009-02-05 17:18:16 +0900273static int __init tomoyo_init(void)
274{
275 struct cred *cred = (struct cred *) current_cred();
276
277 if (!security_module_enable(&tomoyo_security_ops))
278 return 0;
279 /* register ourselves with the security framework */
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900280 if (register_security(&tomoyo_security_ops) ||
281 init_srcu_struct(&tomoyo_ss))
Kentaro Takedaf7433242009-02-05 17:18:16 +0900282 panic("Failure registering TOMOYO Linux");
283 printk(KERN_INFO "TOMOYO Linux initialized\n");
284 cred->security = &tomoyo_kernel_domain;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900285 tomoyo_mm_init();
Kentaro Takedaf7433242009-02-05 17:18:16 +0900286 return 0;
287}
288
289security_initcall(tomoyo_init);