]> git.karo-electronics.de Git - karo-tx-linux.git/blob - security/tomoyo/tomoyo.c
TOMOYO: Split files into some pieces.
[karo-tx-linux.git] / security / tomoyo / tomoyo.c
1 /*
2  * security/tomoyo/tomoyo.c
3  *
4  * LSM hooks for TOMOYO Linux.
5  *
6  * Copyright (C) 2005-2010  NTT DATA CORPORATION
7  */
8
9 #include <linux/security.h>
10 #include "common.h"
11
12 static int tomoyo_cred_alloc_blank(struct cred *new, gfp_t gfp)
13 {
14         new->security = NULL;
15         return 0;
16 }
17
18 static int tomoyo_cred_prepare(struct cred *new, const struct cred *old,
19                                gfp_t gfp)
20 {
21         struct tomoyo_domain_info *domain = old->security;
22         new->security = domain;
23         if (domain)
24                 atomic_inc(&domain->users);
25         return 0;
26 }
27
28 static void tomoyo_cred_transfer(struct cred *new, const struct cred *old)
29 {
30         tomoyo_cred_prepare(new, old, 0);
31 }
32
33 static void tomoyo_cred_free(struct cred *cred)
34 {
35         struct tomoyo_domain_info *domain = cred->security;
36         if (domain)
37                 atomic_dec(&domain->users);
38 }
39
40 static int tomoyo_bprm_set_creds(struct linux_binprm *bprm)
41 {
42         int rc;
43
44         rc = cap_bprm_set_creds(bprm);
45         if (rc)
46                 return rc;
47
48         /*
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         /*
61          * 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         /*
69          * 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
76 static 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          */
84         if (!domain) {
85                 const int idx = tomoyo_read_lock();
86                 const int err = tomoyo_find_next_domain(bprm);
87                 tomoyo_read_unlock(idx);
88                 return err;
89         }
90         /*
91          * Read permission is checked against interpreters using next domain.
92          */
93         return tomoyo_check_open_permission(domain, &bprm->file->f_path, O_RDONLY);
94 }
95
96 static int tomoyo_path_truncate(struct path *path, loff_t length,
97                                 unsigned int time_attrs)
98 {
99         return tomoyo_path_perm(TOMOYO_TYPE_TRUNCATE, path);
100 }
101
102 static int tomoyo_path_unlink(struct path *parent, struct dentry *dentry)
103 {
104         struct path path = { parent->mnt, dentry };
105         return tomoyo_path_perm(TOMOYO_TYPE_UNLINK, &path);
106 }
107
108 static int tomoyo_path_mkdir(struct path *parent, struct dentry *dentry,
109                              int mode)
110 {
111         struct path path = { parent->mnt, dentry };
112         return tomoyo_path_number_perm(TOMOYO_TYPE_MKDIR, &path,
113                                        mode & S_IALLUGO);
114 }
115
116 static int tomoyo_path_rmdir(struct path *parent, struct dentry *dentry)
117 {
118         struct path path = { parent->mnt, dentry };
119         return tomoyo_path_perm(TOMOYO_TYPE_RMDIR, &path);
120 }
121
122 static int tomoyo_path_symlink(struct path *parent, struct dentry *dentry,
123                                const char *old_name)
124 {
125         struct path path = { parent->mnt, dentry };
126         return tomoyo_path_perm(TOMOYO_TYPE_SYMLINK, &path);
127 }
128
129 static int tomoyo_path_mknod(struct path *parent, struct dentry *dentry,
130                              int mode, unsigned int dev)
131 {
132         struct path path = { parent->mnt, dentry };
133         int type = TOMOYO_TYPE_CREATE;
134         const unsigned int perm = mode & S_IALLUGO;
135
136         switch (mode & S_IFMT) {
137         case S_IFCHR:
138                 type = TOMOYO_TYPE_MKCHAR;
139                 break;
140         case S_IFBLK:
141                 type = TOMOYO_TYPE_MKBLOCK;
142                 break;
143         default:
144                 goto no_dev;
145         }
146         return tomoyo_path_number3_perm(type, &path, perm, dev);
147  no_dev:
148         switch (mode & S_IFMT) {
149         case S_IFIFO:
150                 type = TOMOYO_TYPE_MKFIFO;
151                 break;
152         case S_IFSOCK:
153                 type = TOMOYO_TYPE_MKSOCK;
154                 break;
155         }
156         return tomoyo_path_number_perm(type, &path, perm);
157 }
158
159 static int tomoyo_path_link(struct dentry *old_dentry, struct path *new_dir,
160                             struct dentry *new_dentry)
161 {
162         struct path path1 = { new_dir->mnt, old_dentry };
163         struct path path2 = { new_dir->mnt, new_dentry };
164         return tomoyo_path2_perm(TOMOYO_TYPE_LINK, &path1, &path2);
165 }
166
167 static int tomoyo_path_rename(struct path *old_parent,
168                               struct dentry *old_dentry,
169                               struct path *new_parent,
170                               struct dentry *new_dentry)
171 {
172         struct path path1 = { old_parent->mnt, old_dentry };
173         struct path path2 = { new_parent->mnt, new_dentry };
174         return tomoyo_path2_perm(TOMOYO_TYPE_RENAME, &path1, &path2);
175 }
176
177 static int tomoyo_file_fcntl(struct file *file, unsigned int cmd,
178                              unsigned long arg)
179 {
180         if (cmd == F_SETFL && ((arg ^ file->f_flags) & O_APPEND))
181                 return tomoyo_path_perm(TOMOYO_TYPE_REWRITE, &file->f_path);
182         return 0;
183 }
184
185 static int tomoyo_dentry_open(struct file *f, const struct cred *cred)
186 {
187         int flags = f->f_flags;
188         /* Don't check read permission here if called from do_execve(). */
189         if (current->in_execve)
190                 return 0;
191         return tomoyo_check_open_permission(tomoyo_domain(), &f->f_path, flags);
192 }
193
194 static int tomoyo_file_ioctl(struct file *file, unsigned int cmd,
195                              unsigned long arg)
196 {
197         return tomoyo_path_number_perm(TOMOYO_TYPE_IOCTL, &file->f_path, cmd);
198 }
199
200 static int tomoyo_path_chmod(struct dentry *dentry, struct vfsmount *mnt,
201                              mode_t mode)
202 {
203         struct path path = { mnt, dentry };
204         return tomoyo_path_number_perm(TOMOYO_TYPE_CHMOD, &path,
205                                        mode & S_IALLUGO);
206 }
207
208 static int tomoyo_path_chown(struct path *path, uid_t uid, gid_t gid)
209 {
210         int error = 0;
211         if (uid != (uid_t) -1)
212                 error = tomoyo_path_number_perm(TOMOYO_TYPE_CHOWN, path, uid);
213         if (!error && gid != (gid_t) -1)
214                 error = tomoyo_path_number_perm(TOMOYO_TYPE_CHGRP, path, gid);
215         return error;
216 }
217
218 static int tomoyo_path_chroot(struct path *path)
219 {
220         return tomoyo_path_perm(TOMOYO_TYPE_CHROOT, path);
221 }
222
223 static int tomoyo_sb_mount(char *dev_name, struct path *path,
224                            char *type, unsigned long flags, void *data)
225 {
226         return tomoyo_mount_permission(dev_name, path, type, flags, data);
227 }
228
229 static int tomoyo_sb_umount(struct vfsmount *mnt, int flags)
230 {
231         struct path path = { mnt, mnt->mnt_root };
232         return tomoyo_path_perm(TOMOYO_TYPE_UMOUNT, &path);
233 }
234
235 static int tomoyo_sb_pivotroot(struct path *old_path, struct path *new_path)
236 {
237         return tomoyo_path2_perm(TOMOYO_TYPE_PIVOT_ROOT, new_path, old_path);
238 }
239
240 /*
241  * tomoyo_security_ops is a "struct security_operations" which is used for
242  * registering TOMOYO.
243  */
244 static struct security_operations tomoyo_security_ops = {
245         .name                = "tomoyo",
246         .cred_alloc_blank    = tomoyo_cred_alloc_blank,
247         .cred_prepare        = tomoyo_cred_prepare,
248         .cred_transfer       = tomoyo_cred_transfer,
249         .cred_free           = tomoyo_cred_free,
250         .bprm_set_creds      = tomoyo_bprm_set_creds,
251         .bprm_check_security = tomoyo_bprm_check_security,
252         .file_fcntl          = tomoyo_file_fcntl,
253         .dentry_open         = tomoyo_dentry_open,
254         .path_truncate       = tomoyo_path_truncate,
255         .path_unlink         = tomoyo_path_unlink,
256         .path_mkdir          = tomoyo_path_mkdir,
257         .path_rmdir          = tomoyo_path_rmdir,
258         .path_symlink        = tomoyo_path_symlink,
259         .path_mknod          = tomoyo_path_mknod,
260         .path_link           = tomoyo_path_link,
261         .path_rename         = tomoyo_path_rename,
262         .file_ioctl          = tomoyo_file_ioctl,
263         .path_chmod          = tomoyo_path_chmod,
264         .path_chown          = tomoyo_path_chown,
265         .path_chroot         = tomoyo_path_chroot,
266         .sb_mount            = tomoyo_sb_mount,
267         .sb_umount           = tomoyo_sb_umount,
268         .sb_pivotroot        = tomoyo_sb_pivotroot,
269 };
270
271 /* Lock for GC. */
272 struct srcu_struct tomoyo_ss;
273
274 static int __init tomoyo_init(void)
275 {
276         struct cred *cred = (struct cred *) current_cred();
277
278         if (!security_module_enable(&tomoyo_security_ops))
279                 return 0;
280         /* register ourselves with the security framework */
281         if (register_security(&tomoyo_security_ops) ||
282             init_srcu_struct(&tomoyo_ss))
283                 panic("Failure registering TOMOYO Linux");
284         printk(KERN_INFO "TOMOYO Linux initialized\n");
285         cred->security = &tomoyo_kernel_domain;
286         tomoyo_mm_init();
287         return 0;
288 }
289
290 security_initcall(tomoyo_init);