3f93bb91768b589575bbe13c02d7fe8a52b8099c
[safe/jmp/linux-2.6] / security / tomoyo / tomoyo.c
1 /*
2  * security/tomoyo/tomoyo.c
3  *
4  * LSM hooks for TOMOYO Linux.
5  *
6  * Copyright (C) 2005-2009  NTT DATA CORPORATION
7  *
8  * Version: 2.2.0   2009/04/01
9  *
10  */
11
12 #include <linux/security.h>
13 #include "common.h"
14 #include "tomoyo.h"
15 #include "realpath.h"
16
17 static int tomoyo_cred_alloc_blank(struct cred *new, gfp_t gfp)
18 {
19         new->security = NULL;
20         return 0;
21 }
22
23 static int tomoyo_cred_prepare(struct cred *new, const struct cred *old,
24                                gfp_t gfp)
25 {
26         /*
27          * Since "struct tomoyo_domain_info *" is a sharable pointer,
28          * we don't need to duplicate.
29          */
30         new->security = old->security;
31         return 0;
32 }
33
34 static void tomoyo_cred_transfer(struct cred *new, const struct cred *old)
35 {
36         /*
37          * Since "struct tomoyo_domain_info *" is a sharable pointer,
38          * we don't need to duplicate.
39          */
40         new->security = old->security;
41 }
42
43 static int tomoyo_bprm_set_creds(struct linux_binprm *bprm)
44 {
45         int rc;
46
47         rc = cap_bprm_set_creds(bprm);
48         if (rc)
49                 return rc;
50
51         /*
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         /*
64          * Tell tomoyo_bprm_check_security() is called for the first time of an
65          * execve operation.
66          */
67         bprm->cred->security = NULL;
68         return 0;
69 }
70
71 static int tomoyo_bprm_check_security(struct linux_binprm *bprm)
72 {
73         struct tomoyo_domain_info *domain = bprm->cred->security;
74
75         /*
76          * Execute permission is checked against pathname passed to do_execve()
77          * using current domain.
78          */
79         if (!domain)
80                 return tomoyo_find_next_domain(bprm);
81         /*
82          * Read permission is checked against interpreters using next domain.
83          * '1' is the result of open_to_namei_flags(O_RDONLY).
84          */
85         return tomoyo_check_open_permission(domain, &bprm->file->f_path, 1);
86 }
87
88 #ifdef CONFIG_SYSCTL
89
90 static int tomoyo_prepend(char **buffer, int *buflen, const char *str)
91 {
92         int namelen = strlen(str);
93
94         if (*buflen < namelen)
95                 return -ENOMEM;
96         *buflen -= namelen;
97         *buffer -= namelen;
98         memcpy(*buffer, str, namelen);
99         return 0;
100 }
101
102 /**
103  * tomoyo_sysctl_path - return the realpath of a ctl_table.
104  * @table: pointer to "struct ctl_table".
105  *
106  * Returns realpath(3) of the @table on success.
107  * Returns NULL on failure.
108  *
109  * This function uses tomoyo_alloc(), so the caller must call tomoyo_free()
110  * if this function didn't return NULL.
111  */
112 static char *tomoyo_sysctl_path(struct ctl_table *table)
113 {
114         int buflen = TOMOYO_MAX_PATHNAME_LEN;
115         char *buf = tomoyo_alloc(buflen);
116         char *end = buf + buflen;
117         int error = -ENOMEM;
118
119         if (!buf)
120                 return NULL;
121
122         *--end = '\0';
123         buflen--;
124         while (table) {
125                 if (tomoyo_prepend(&end, &buflen, table->procname) ||
126                     tomoyo_prepend(&end, &buflen, "/"))
127                         goto out;
128                 table = table->parent;
129         }
130         if (tomoyo_prepend(&end, &buflen, "/proc/sys"))
131                 goto out;
132         error = tomoyo_encode(buf, end - buf, end);
133  out:
134         if (!error)
135                 return buf;
136         tomoyo_free(buf);
137         return NULL;
138 }
139
140 static int tomoyo_sysctl(struct ctl_table *table, int op)
141 {
142         int error;
143         char *name;
144
145         op &= MAY_READ | MAY_WRITE;
146         if (!op)
147                 return 0;
148         name = tomoyo_sysctl_path(table);
149         if (!name)
150                 return -ENOMEM;
151         error = tomoyo_check_file_perm(tomoyo_domain(), name, op);
152         tomoyo_free(name);
153         return error;
154 }
155 #endif
156
157 static int tomoyo_path_truncate(struct path *path, loff_t length,
158                                 unsigned int time_attrs)
159 {
160         return tomoyo_check_1path_perm(tomoyo_domain(),
161                                        TOMOYO_TYPE_TRUNCATE_ACL,
162                                        path);
163 }
164
165 static int tomoyo_path_unlink(struct path *parent, struct dentry *dentry)
166 {
167         struct path path = { parent->mnt, dentry };
168         return tomoyo_check_1path_perm(tomoyo_domain(),
169                                        TOMOYO_TYPE_UNLINK_ACL,
170                                        &path);
171 }
172
173 static int tomoyo_path_mkdir(struct path *parent, struct dentry *dentry,
174                              int mode)
175 {
176         struct path path = { parent->mnt, dentry };
177         return tomoyo_check_1path_perm(tomoyo_domain(),
178                                        TOMOYO_TYPE_MKDIR_ACL,
179                                        &path);
180 }
181
182 static int tomoyo_path_rmdir(struct path *parent, struct dentry *dentry)
183 {
184         struct path path = { parent->mnt, dentry };
185         return tomoyo_check_1path_perm(tomoyo_domain(),
186                                        TOMOYO_TYPE_RMDIR_ACL,
187                                        &path);
188 }
189
190 static int tomoyo_path_symlink(struct path *parent, struct dentry *dentry,
191                                const char *old_name)
192 {
193         struct path path = { parent->mnt, dentry };
194         return tomoyo_check_1path_perm(tomoyo_domain(),
195                                        TOMOYO_TYPE_SYMLINK_ACL,
196                                        &path);
197 }
198
199 static int tomoyo_path_mknod(struct path *parent, struct dentry *dentry,
200                              int mode, unsigned int dev)
201 {
202         struct path path = { parent->mnt, dentry };
203         int type = TOMOYO_TYPE_CREATE_ACL;
204
205         switch (mode & S_IFMT) {
206         case S_IFCHR:
207                 type = TOMOYO_TYPE_MKCHAR_ACL;
208                 break;
209         case S_IFBLK:
210                 type = TOMOYO_TYPE_MKBLOCK_ACL;
211                 break;
212         case S_IFIFO:
213                 type = TOMOYO_TYPE_MKFIFO_ACL;
214                 break;
215         case S_IFSOCK:
216                 type = TOMOYO_TYPE_MKSOCK_ACL;
217                 break;
218         }
219         return tomoyo_check_1path_perm(tomoyo_domain(),
220                                        type, &path);
221 }
222
223 static int tomoyo_path_link(struct dentry *old_dentry, struct path *new_dir,
224                             struct dentry *new_dentry)
225 {
226         struct path path1 = { new_dir->mnt, old_dentry };
227         struct path path2 = { new_dir->mnt, new_dentry };
228         return tomoyo_check_2path_perm(tomoyo_domain(),
229                                        TOMOYO_TYPE_LINK_ACL,
230                                        &path1, &path2);
231 }
232
233 static int tomoyo_path_rename(struct path *old_parent,
234                               struct dentry *old_dentry,
235                               struct path *new_parent,
236                               struct dentry *new_dentry)
237 {
238         struct path path1 = { old_parent->mnt, old_dentry };
239         struct path path2 = { new_parent->mnt, new_dentry };
240         return tomoyo_check_2path_perm(tomoyo_domain(),
241                                        TOMOYO_TYPE_RENAME_ACL,
242                                        &path1, &path2);
243 }
244
245 static int tomoyo_file_fcntl(struct file *file, unsigned int cmd,
246                              unsigned long arg)
247 {
248         if (cmd == F_SETFL && ((arg ^ file->f_flags) & O_APPEND))
249                 return tomoyo_check_rewrite_permission(tomoyo_domain(), file);
250         return 0;
251 }
252
253 static int tomoyo_dentry_open(struct file *f, const struct cred *cred)
254 {
255         int flags = f->f_flags;
256
257         if ((flags + 1) & O_ACCMODE)
258                 flags++;
259         flags |= f->f_flags & (O_APPEND | O_TRUNC);
260         /* Don't check read permission here if called from do_execve(). */
261         if (current->in_execve)
262                 return 0;
263         return tomoyo_check_open_permission(tomoyo_domain(), &f->f_path, flags);
264 }
265
266 /*
267  * tomoyo_security_ops is a "struct security_operations" which is used for
268  * registering TOMOYO.
269  */
270 static struct security_operations tomoyo_security_ops = {
271         .name                = "tomoyo",
272         .cred_alloc_blank    = tomoyo_cred_alloc_blank,
273         .cred_prepare        = tomoyo_cred_prepare,
274         .cred_transfer       = tomoyo_cred_transfer,
275         .bprm_set_creds      = tomoyo_bprm_set_creds,
276         .bprm_check_security = tomoyo_bprm_check_security,
277 #ifdef CONFIG_SYSCTL
278         .sysctl              = tomoyo_sysctl,
279 #endif
280         .file_fcntl          = tomoyo_file_fcntl,
281         .dentry_open         = tomoyo_dentry_open,
282         .path_truncate       = tomoyo_path_truncate,
283         .path_unlink         = tomoyo_path_unlink,
284         .path_mkdir          = tomoyo_path_mkdir,
285         .path_rmdir          = tomoyo_path_rmdir,
286         .path_symlink        = tomoyo_path_symlink,
287         .path_mknod          = tomoyo_path_mknod,
288         .path_link           = tomoyo_path_link,
289         .path_rename         = tomoyo_path_rename,
290 };
291
292 static int __init tomoyo_init(void)
293 {
294         struct cred *cred = (struct cred *) current_cred();
295
296         if (!security_module_enable(&tomoyo_security_ops))
297                 return 0;
298         /* register ourselves with the security framework */
299         if (register_security(&tomoyo_security_ops))
300                 panic("Failure registering TOMOYO Linux");
301         printk(KERN_INFO "TOMOYO Linux initialized\n");
302         cred->security = &tomoyo_kernel_domain;
303         tomoyo_realpath_init();
304         return 0;
305 }
306
307 security_initcall(tomoyo_init);