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