1
2
3
4
5
6
7#include <linux/security.h>
8#include "common.h"
9
10
11
12
13
14
15
16
17
18static int tomoyo_cred_alloc_blank(struct cred *new, gfp_t gfp)
19{
20 new->security = NULL;
21 return 0;
22}
23
24
25
26
27
28
29
30
31
32
33static int tomoyo_cred_prepare(struct cred *new, const struct cred *old,
34 gfp_t gfp)
35{
36 struct tomoyo_domain_info *domain = old->security;
37 new->security = domain;
38 if (domain)
39 atomic_inc(&domain->users);
40 return 0;
41}
42
43
44
45
46
47
48
49static void tomoyo_cred_transfer(struct cred *new, const struct cred *old)
50{
51 tomoyo_cred_prepare(new, old, 0);
52}
53
54
55
56
57
58
59static void tomoyo_cred_free(struct cred *cred)
60{
61 struct tomoyo_domain_info *domain = cred->security;
62 if (domain)
63 atomic_dec(&domain->users);
64}
65
66
67
68
69
70
71
72
73static int tomoyo_bprm_set_creds(struct linux_binprm *bprm)
74{
75 int rc;
76
77 rc = cap_bprm_set_creds(bprm);
78 if (rc)
79 return rc;
80
81
82
83
84
85 if (bprm->cred_prepared)
86 return 0;
87#ifndef CONFIG_SECURITY_TOMOYO_OMIT_USERSPACE_LOADER
88
89
90
91
92 if (!tomoyo_policy_loaded)
93 tomoyo_load_policy(bprm->filename);
94#endif
95
96
97
98
99
100
101 atomic_dec(&((struct tomoyo_domain_info *)
102 bprm->cred->security)->users);
103
104
105
106
107 bprm->cred->security = NULL;
108 return 0;
109}
110
111
112
113
114
115
116
117
118static int tomoyo_bprm_check_security(struct linux_binprm *bprm)
119{
120 struct tomoyo_domain_info *domain = bprm->cred->security;
121
122
123
124
125
126 if (!domain) {
127 const int idx = tomoyo_read_lock();
128 const int err = tomoyo_find_next_domain(bprm);
129 tomoyo_read_unlock(idx);
130 return err;
131 }
132
133
134
135 return tomoyo_check_open_permission(domain, &bprm->file->f_path,
136 O_RDONLY);
137}
138
139
140
141
142
143
144
145
146
147static int tomoyo_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
148{
149 struct path path = { mnt, dentry };
150 return tomoyo_path_perm(TOMOYO_TYPE_GETATTR, &path, NULL);
151}
152
153
154
155
156
157
158
159
160static int tomoyo_path_truncate(struct path *path)
161{
162 return tomoyo_path_perm(TOMOYO_TYPE_TRUNCATE, path, NULL);
163}
164
165
166
167
168
169
170
171
172
173static int tomoyo_path_unlink(struct path *parent, struct dentry *dentry)
174{
175 struct path path = { parent->mnt, dentry };
176 return tomoyo_path_perm(TOMOYO_TYPE_UNLINK, &path, NULL);
177}
178
179
180
181
182
183
184
185
186
187
188static int tomoyo_path_mkdir(struct path *parent, struct dentry *dentry,
189 umode_t mode)
190{
191 struct path path = { parent->mnt, dentry };
192 return tomoyo_path_number_perm(TOMOYO_TYPE_MKDIR, &path,
193 mode & S_IALLUGO);
194}
195
196
197
198
199
200
201
202
203
204static int tomoyo_path_rmdir(struct path *parent, struct dentry *dentry)
205{
206 struct path path = { parent->mnt, dentry };
207 return tomoyo_path_perm(TOMOYO_TYPE_RMDIR, &path, NULL);
208}
209
210
211
212
213
214
215
216
217
218
219static int tomoyo_path_symlink(struct path *parent, struct dentry *dentry,
220 const char *old_name)
221{
222 struct path path = { parent->mnt, dentry };
223 return tomoyo_path_perm(TOMOYO_TYPE_SYMLINK, &path, old_name);
224}
225
226
227
228
229
230
231
232
233
234
235
236static int tomoyo_path_mknod(struct path *parent, struct dentry *dentry,
237 umode_t mode, unsigned int dev)
238{
239 struct path path = { parent->mnt, dentry };
240 int type = TOMOYO_TYPE_CREATE;
241 const unsigned int perm = mode & S_IALLUGO;
242
243 switch (mode & S_IFMT) {
244 case S_IFCHR:
245 type = TOMOYO_TYPE_MKCHAR;
246 break;
247 case S_IFBLK:
248 type = TOMOYO_TYPE_MKBLOCK;
249 break;
250 default:
251 goto no_dev;
252 }
253 return tomoyo_mkdev_perm(type, &path, perm, dev);
254 no_dev:
255 switch (mode & S_IFMT) {
256 case S_IFIFO:
257 type = TOMOYO_TYPE_MKFIFO;
258 break;
259 case S_IFSOCK:
260 type = TOMOYO_TYPE_MKSOCK;
261 break;
262 }
263 return tomoyo_path_number_perm(type, &path, perm);
264}
265
266
267
268
269
270
271
272
273
274
275static int tomoyo_path_link(struct dentry *old_dentry, struct path *new_dir,
276 struct dentry *new_dentry)
277{
278 struct path path1 = { new_dir->mnt, old_dentry };
279 struct path path2 = { new_dir->mnt, new_dentry };
280 return tomoyo_path2_perm(TOMOYO_TYPE_LINK, &path1, &path2);
281}
282
283
284
285
286
287
288
289
290
291
292
293static int tomoyo_path_rename(struct path *old_parent,
294 struct dentry *old_dentry,
295 struct path *new_parent,
296 struct dentry *new_dentry)
297{
298 struct path path1 = { old_parent->mnt, old_dentry };
299 struct path path2 = { new_parent->mnt, new_dentry };
300 return tomoyo_path2_perm(TOMOYO_TYPE_RENAME, &path1, &path2);
301}
302
303
304
305
306
307
308
309
310
311
312static int tomoyo_file_fcntl(struct file *file, unsigned int cmd,
313 unsigned long arg)
314{
315 if (!(cmd == F_SETFL && ((arg ^ file->f_flags) & O_APPEND)))
316 return 0;
317 return tomoyo_check_open_permission(tomoyo_domain(), &file->f_path,
318 O_WRONLY | (arg & O_APPEND));
319}
320
321
322
323
324
325
326
327
328
329static int tomoyo_file_open(struct file *f, const struct cred *cred)
330{
331 int flags = f->f_flags;
332
333 if (current->in_execve)
334 return 0;
335 return tomoyo_check_open_permission(tomoyo_domain(), &f->f_path, flags);
336}
337
338
339
340
341
342
343
344
345
346
347static int tomoyo_file_ioctl(struct file *file, unsigned int cmd,
348 unsigned long arg)
349{
350 return tomoyo_path_number_perm(TOMOYO_TYPE_IOCTL, &file->f_path, cmd);
351}
352
353
354
355
356
357
358
359
360
361static int tomoyo_path_chmod(struct path *path, umode_t mode)
362{
363 return tomoyo_path_number_perm(TOMOYO_TYPE_CHMOD, path,
364 mode & S_IALLUGO);
365}
366
367
368
369
370
371
372
373
374
375
376static int tomoyo_path_chown(struct path *path, uid_t uid, gid_t gid)
377{
378 int error = 0;
379 if (uid != (uid_t) -1)
380 error = tomoyo_path_number_perm(TOMOYO_TYPE_CHOWN, path, uid);
381 if (!error && gid != (gid_t) -1)
382 error = tomoyo_path_number_perm(TOMOYO_TYPE_CHGRP, path, gid);
383 return error;
384}
385
386
387
388
389
390
391
392
393static int tomoyo_path_chroot(struct path *path)
394{
395 return tomoyo_path_perm(TOMOYO_TYPE_CHROOT, path, NULL);
396}
397
398
399
400
401
402
403
404
405
406
407
408
409static int tomoyo_sb_mount(char *dev_name, struct path *path,
410 char *type, unsigned long flags, void *data)
411{
412 return tomoyo_mount_permission(dev_name, path, type, flags, data);
413}
414
415
416
417
418
419
420
421
422
423static int tomoyo_sb_umount(struct vfsmount *mnt, int flags)
424{
425 struct path path = { mnt, mnt->mnt_root };
426 return tomoyo_path_perm(TOMOYO_TYPE_UMOUNT, &path, NULL);
427}
428
429
430
431
432
433
434
435
436
437static int tomoyo_sb_pivotroot(struct path *old_path, struct path *new_path)
438{
439 return tomoyo_path2_perm(TOMOYO_TYPE_PIVOT_ROOT, new_path, old_path);
440}
441
442
443
444
445
446
447
448
449
450static int tomoyo_socket_listen(struct socket *sock, int backlog)
451{
452 return tomoyo_socket_listen_permission(sock);
453}
454
455
456
457
458
459
460
461
462
463
464static int tomoyo_socket_connect(struct socket *sock, struct sockaddr *addr,
465 int addr_len)
466{
467 return tomoyo_socket_connect_permission(sock, addr, addr_len);
468}
469
470
471
472
473
474
475
476
477
478
479static int tomoyo_socket_bind(struct socket *sock, struct sockaddr *addr,
480 int addr_len)
481{
482 return tomoyo_socket_bind_permission(sock, addr, addr_len);
483}
484
485
486
487
488
489
490
491
492
493
494static int tomoyo_socket_sendmsg(struct socket *sock, struct msghdr *msg,
495 int size)
496{
497 return tomoyo_socket_sendmsg_permission(sock, msg, size);
498}
499
500
501
502
503
504static struct security_operations tomoyo_security_ops = {
505 .name = "tomoyo",
506 .cred_alloc_blank = tomoyo_cred_alloc_blank,
507 .cred_prepare = tomoyo_cred_prepare,
508 .cred_transfer = tomoyo_cred_transfer,
509 .cred_free = tomoyo_cred_free,
510 .bprm_set_creds = tomoyo_bprm_set_creds,
511 .bprm_check_security = tomoyo_bprm_check_security,
512 .file_fcntl = tomoyo_file_fcntl,
513 .file_open = tomoyo_file_open,
514 .path_truncate = tomoyo_path_truncate,
515 .path_unlink = tomoyo_path_unlink,
516 .path_mkdir = tomoyo_path_mkdir,
517 .path_rmdir = tomoyo_path_rmdir,
518 .path_symlink = tomoyo_path_symlink,
519 .path_mknod = tomoyo_path_mknod,
520 .path_link = tomoyo_path_link,
521 .path_rename = tomoyo_path_rename,
522 .inode_getattr = tomoyo_inode_getattr,
523 .file_ioctl = tomoyo_file_ioctl,
524 .path_chmod = tomoyo_path_chmod,
525 .path_chown = tomoyo_path_chown,
526 .path_chroot = tomoyo_path_chroot,
527 .sb_mount = tomoyo_sb_mount,
528 .sb_umount = tomoyo_sb_umount,
529 .sb_pivotroot = tomoyo_sb_pivotroot,
530 .socket_bind = tomoyo_socket_bind,
531 .socket_connect = tomoyo_socket_connect,
532 .socket_listen = tomoyo_socket_listen,
533 .socket_sendmsg = tomoyo_socket_sendmsg,
534};
535
536
537struct srcu_struct tomoyo_ss;
538
539
540
541
542
543
544static int __init tomoyo_init(void)
545{
546 struct cred *cred = (struct cred *) current_cred();
547
548 if (!security_module_enable(&tomoyo_security_ops))
549 return 0;
550
551 if (register_security(&tomoyo_security_ops) ||
552 init_srcu_struct(&tomoyo_ss))
553 panic("Failure registering TOMOYO Linux");
554 printk(KERN_INFO "TOMOYO Linux initialized\n");
555 cred->security = &tomoyo_kernel_domain;
556 tomoyo_mm_init();
557 return 0;
558}
559
560security_initcall(tomoyo_init);
561