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