0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/lsm_hooks.h>
0009 #include "common.h"
0010
0011
0012
0013
0014
0015
0016 struct tomoyo_domain_info *tomoyo_domain(void)
0017 {
0018 struct tomoyo_task *s = tomoyo_task(current);
0019
0020 if (s->old_domain_info && !current->in_execve) {
0021 atomic_dec(&s->old_domain_info->users);
0022 s->old_domain_info = NULL;
0023 }
0024 return s->domain_info;
0025 }
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036 static int tomoyo_cred_prepare(struct cred *new, const struct cred *old,
0037 gfp_t gfp)
0038 {
0039
0040 struct tomoyo_task *s = tomoyo_task(current);
0041
0042 if (s->old_domain_info && !current->in_execve) {
0043 atomic_dec(&s->domain_info->users);
0044 s->domain_info = s->old_domain_info;
0045 s->old_domain_info = NULL;
0046 }
0047 return 0;
0048 }
0049
0050
0051
0052
0053
0054
0055 static void tomoyo_bprm_committed_creds(struct linux_binprm *bprm)
0056 {
0057
0058 struct tomoyo_task *s = tomoyo_task(current);
0059
0060 atomic_dec(&s->old_domain_info->users);
0061 s->old_domain_info = NULL;
0062 }
0063
0064 #ifndef CONFIG_SECURITY_TOMOYO_OMIT_USERSPACE_LOADER
0065
0066
0067
0068
0069
0070
0071
0072 static int tomoyo_bprm_creds_for_exec(struct linux_binprm *bprm)
0073 {
0074
0075
0076
0077
0078 if (!tomoyo_policy_loaded)
0079 tomoyo_load_policy(bprm->filename);
0080 return 0;
0081 }
0082 #endif
0083
0084
0085
0086
0087
0088
0089
0090
0091 static int tomoyo_bprm_check_security(struct linux_binprm *bprm)
0092 {
0093 struct tomoyo_task *s = tomoyo_task(current);
0094
0095
0096
0097
0098
0099 if (!s->old_domain_info) {
0100 const int idx = tomoyo_read_lock();
0101 const int err = tomoyo_find_next_domain(bprm);
0102
0103 tomoyo_read_unlock(idx);
0104 return err;
0105 }
0106
0107
0108
0109 return tomoyo_check_open_permission(s->domain_info,
0110 &bprm->file->f_path, O_RDONLY);
0111 }
0112
0113
0114
0115
0116
0117
0118
0119
0120 static int tomoyo_inode_getattr(const struct path *path)
0121 {
0122 return tomoyo_path_perm(TOMOYO_TYPE_GETATTR, path, NULL);
0123 }
0124
0125
0126
0127
0128
0129
0130
0131
0132 static int tomoyo_path_truncate(const struct path *path)
0133 {
0134 return tomoyo_path_perm(TOMOYO_TYPE_TRUNCATE, path, NULL);
0135 }
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145 static int tomoyo_path_unlink(const struct path *parent, struct dentry *dentry)
0146 {
0147 struct path path = { .mnt = parent->mnt, .dentry = dentry };
0148
0149 return tomoyo_path_perm(TOMOYO_TYPE_UNLINK, &path, NULL);
0150 }
0151
0152
0153
0154
0155
0156
0157
0158
0159
0160
0161 static int tomoyo_path_mkdir(const struct path *parent, struct dentry *dentry,
0162 umode_t mode)
0163 {
0164 struct path path = { .mnt = parent->mnt, .dentry = dentry };
0165
0166 return tomoyo_path_number_perm(TOMOYO_TYPE_MKDIR, &path,
0167 mode & S_IALLUGO);
0168 }
0169
0170
0171
0172
0173
0174
0175
0176
0177
0178 static int tomoyo_path_rmdir(const struct path *parent, struct dentry *dentry)
0179 {
0180 struct path path = { .mnt = parent->mnt, .dentry = dentry };
0181
0182 return tomoyo_path_perm(TOMOYO_TYPE_RMDIR, &path, NULL);
0183 }
0184
0185
0186
0187
0188
0189
0190
0191
0192
0193
0194 static int tomoyo_path_symlink(const struct path *parent, struct dentry *dentry,
0195 const char *old_name)
0196 {
0197 struct path path = { .mnt = parent->mnt, .dentry = dentry };
0198
0199 return tomoyo_path_perm(TOMOYO_TYPE_SYMLINK, &path, old_name);
0200 }
0201
0202
0203
0204
0205
0206
0207
0208
0209
0210
0211
0212 static int tomoyo_path_mknod(const struct path *parent, struct dentry *dentry,
0213 umode_t mode, unsigned int dev)
0214 {
0215 struct path path = { .mnt = parent->mnt, .dentry = dentry };
0216 int type = TOMOYO_TYPE_CREATE;
0217 const unsigned int perm = mode & S_IALLUGO;
0218
0219 switch (mode & S_IFMT) {
0220 case S_IFCHR:
0221 type = TOMOYO_TYPE_MKCHAR;
0222 break;
0223 case S_IFBLK:
0224 type = TOMOYO_TYPE_MKBLOCK;
0225 break;
0226 default:
0227 goto no_dev;
0228 }
0229 return tomoyo_mkdev_perm(type, &path, perm, dev);
0230 no_dev:
0231 switch (mode & S_IFMT) {
0232 case S_IFIFO:
0233 type = TOMOYO_TYPE_MKFIFO;
0234 break;
0235 case S_IFSOCK:
0236 type = TOMOYO_TYPE_MKSOCK;
0237 break;
0238 }
0239 return tomoyo_path_number_perm(type, &path, perm);
0240 }
0241
0242
0243
0244
0245
0246
0247
0248
0249
0250
0251 static int tomoyo_path_link(struct dentry *old_dentry, const struct path *new_dir,
0252 struct dentry *new_dentry)
0253 {
0254 struct path path1 = { .mnt = new_dir->mnt, .dentry = old_dentry };
0255 struct path path2 = { .mnt = new_dir->mnt, .dentry = new_dentry };
0256
0257 return tomoyo_path2_perm(TOMOYO_TYPE_LINK, &path1, &path2);
0258 }
0259
0260
0261
0262
0263
0264
0265
0266
0267
0268
0269
0270
0271 static int tomoyo_path_rename(const struct path *old_parent,
0272 struct dentry *old_dentry,
0273 const struct path *new_parent,
0274 struct dentry *new_dentry,
0275 const unsigned int flags)
0276 {
0277 struct path path1 = { .mnt = old_parent->mnt, .dentry = old_dentry };
0278 struct path path2 = { .mnt = new_parent->mnt, .dentry = new_dentry };
0279
0280 if (flags & RENAME_EXCHANGE) {
0281 const int err = tomoyo_path2_perm(TOMOYO_TYPE_RENAME, &path2,
0282 &path1);
0283
0284 if (err)
0285 return err;
0286 }
0287 return tomoyo_path2_perm(TOMOYO_TYPE_RENAME, &path1, &path2);
0288 }
0289
0290
0291
0292
0293
0294
0295
0296
0297
0298
0299 static int tomoyo_file_fcntl(struct file *file, unsigned int cmd,
0300 unsigned long arg)
0301 {
0302 if (!(cmd == F_SETFL && ((arg ^ file->f_flags) & O_APPEND)))
0303 return 0;
0304 return tomoyo_check_open_permission(tomoyo_domain(), &file->f_path,
0305 O_WRONLY | (arg & O_APPEND));
0306 }
0307
0308
0309
0310
0311
0312
0313
0314
0315 static int tomoyo_file_open(struct file *f)
0316 {
0317
0318 if (current->in_execve)
0319 return 0;
0320 return tomoyo_check_open_permission(tomoyo_domain(), &f->f_path,
0321 f->f_flags);
0322 }
0323
0324
0325
0326
0327
0328
0329
0330
0331
0332
0333 static int tomoyo_file_ioctl(struct file *file, unsigned int cmd,
0334 unsigned long arg)
0335 {
0336 return tomoyo_path_number_perm(TOMOYO_TYPE_IOCTL, &file->f_path, cmd);
0337 }
0338
0339
0340
0341
0342
0343
0344
0345
0346
0347 static int tomoyo_path_chmod(const struct path *path, umode_t mode)
0348 {
0349 return tomoyo_path_number_perm(TOMOYO_TYPE_CHMOD, path,
0350 mode & S_IALLUGO);
0351 }
0352
0353
0354
0355
0356
0357
0358
0359
0360
0361
0362 static int tomoyo_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
0363 {
0364 int error = 0;
0365
0366 if (uid_valid(uid))
0367 error = tomoyo_path_number_perm(TOMOYO_TYPE_CHOWN, path,
0368 from_kuid(&init_user_ns, uid));
0369 if (!error && gid_valid(gid))
0370 error = tomoyo_path_number_perm(TOMOYO_TYPE_CHGRP, path,
0371 from_kgid(&init_user_ns, gid));
0372 return error;
0373 }
0374
0375
0376
0377
0378
0379
0380
0381
0382 static int tomoyo_path_chroot(const struct path *path)
0383 {
0384 return tomoyo_path_perm(TOMOYO_TYPE_CHROOT, path, NULL);
0385 }
0386
0387
0388
0389
0390
0391
0392
0393
0394
0395
0396
0397
0398 static int tomoyo_sb_mount(const char *dev_name, const struct path *path,
0399 const char *type, unsigned long flags, void *data)
0400 {
0401 return tomoyo_mount_permission(dev_name, path, type, flags, data);
0402 }
0403
0404
0405
0406
0407
0408
0409
0410
0411
0412 static int tomoyo_sb_umount(struct vfsmount *mnt, int flags)
0413 {
0414 struct path path = { .mnt = mnt, .dentry = mnt->mnt_root };
0415
0416 return tomoyo_path_perm(TOMOYO_TYPE_UMOUNT, &path, NULL);
0417 }
0418
0419
0420
0421
0422
0423
0424
0425
0426
0427 static int tomoyo_sb_pivotroot(const struct path *old_path, const struct path *new_path)
0428 {
0429 return tomoyo_path2_perm(TOMOYO_TYPE_PIVOT_ROOT, new_path, old_path);
0430 }
0431
0432
0433
0434
0435
0436
0437
0438
0439
0440 static int tomoyo_socket_listen(struct socket *sock, int backlog)
0441 {
0442 return tomoyo_socket_listen_permission(sock);
0443 }
0444
0445
0446
0447
0448
0449
0450
0451
0452
0453
0454 static int tomoyo_socket_connect(struct socket *sock, struct sockaddr *addr,
0455 int addr_len)
0456 {
0457 return tomoyo_socket_connect_permission(sock, addr, addr_len);
0458 }
0459
0460
0461
0462
0463
0464
0465
0466
0467
0468
0469 static int tomoyo_socket_bind(struct socket *sock, struct sockaddr *addr,
0470 int addr_len)
0471 {
0472 return tomoyo_socket_bind_permission(sock, addr, addr_len);
0473 }
0474
0475
0476
0477
0478
0479
0480
0481
0482
0483
0484 static int tomoyo_socket_sendmsg(struct socket *sock, struct msghdr *msg,
0485 int size)
0486 {
0487 return tomoyo_socket_sendmsg_permission(sock, msg, size);
0488 }
0489
0490 struct lsm_blob_sizes tomoyo_blob_sizes __lsm_ro_after_init = {
0491 .lbs_task = sizeof(struct tomoyo_task),
0492 };
0493
0494
0495
0496
0497
0498
0499
0500
0501
0502 static int tomoyo_task_alloc(struct task_struct *task,
0503 unsigned long clone_flags)
0504 {
0505 struct tomoyo_task *old = tomoyo_task(current);
0506 struct tomoyo_task *new = tomoyo_task(task);
0507
0508 new->domain_info = old->domain_info;
0509 atomic_inc(&new->domain_info->users);
0510 new->old_domain_info = NULL;
0511 return 0;
0512 }
0513
0514
0515
0516
0517
0518
0519 static void tomoyo_task_free(struct task_struct *task)
0520 {
0521 struct tomoyo_task *s = tomoyo_task(task);
0522
0523 if (s->domain_info) {
0524 atomic_dec(&s->domain_info->users);
0525 s->domain_info = NULL;
0526 }
0527 if (s->old_domain_info) {
0528 atomic_dec(&s->old_domain_info->users);
0529 s->old_domain_info = NULL;
0530 }
0531 }
0532
0533
0534
0535
0536
0537 static struct security_hook_list tomoyo_hooks[] __lsm_ro_after_init = {
0538 LSM_HOOK_INIT(cred_prepare, tomoyo_cred_prepare),
0539 LSM_HOOK_INIT(bprm_committed_creds, tomoyo_bprm_committed_creds),
0540 LSM_HOOK_INIT(task_alloc, tomoyo_task_alloc),
0541 LSM_HOOK_INIT(task_free, tomoyo_task_free),
0542 #ifndef CONFIG_SECURITY_TOMOYO_OMIT_USERSPACE_LOADER
0543 LSM_HOOK_INIT(bprm_creds_for_exec, tomoyo_bprm_creds_for_exec),
0544 #endif
0545 LSM_HOOK_INIT(bprm_check_security, tomoyo_bprm_check_security),
0546 LSM_HOOK_INIT(file_fcntl, tomoyo_file_fcntl),
0547 LSM_HOOK_INIT(file_open, tomoyo_file_open),
0548 LSM_HOOK_INIT(path_truncate, tomoyo_path_truncate),
0549 LSM_HOOK_INIT(path_unlink, tomoyo_path_unlink),
0550 LSM_HOOK_INIT(path_mkdir, tomoyo_path_mkdir),
0551 LSM_HOOK_INIT(path_rmdir, tomoyo_path_rmdir),
0552 LSM_HOOK_INIT(path_symlink, tomoyo_path_symlink),
0553 LSM_HOOK_INIT(path_mknod, tomoyo_path_mknod),
0554 LSM_HOOK_INIT(path_link, tomoyo_path_link),
0555 LSM_HOOK_INIT(path_rename, tomoyo_path_rename),
0556 LSM_HOOK_INIT(inode_getattr, tomoyo_inode_getattr),
0557 LSM_HOOK_INIT(file_ioctl, tomoyo_file_ioctl),
0558 LSM_HOOK_INIT(path_chmod, tomoyo_path_chmod),
0559 LSM_HOOK_INIT(path_chown, tomoyo_path_chown),
0560 LSM_HOOK_INIT(path_chroot, tomoyo_path_chroot),
0561 LSM_HOOK_INIT(sb_mount, tomoyo_sb_mount),
0562 LSM_HOOK_INIT(sb_umount, tomoyo_sb_umount),
0563 LSM_HOOK_INIT(sb_pivotroot, tomoyo_sb_pivotroot),
0564 LSM_HOOK_INIT(socket_bind, tomoyo_socket_bind),
0565 LSM_HOOK_INIT(socket_connect, tomoyo_socket_connect),
0566 LSM_HOOK_INIT(socket_listen, tomoyo_socket_listen),
0567 LSM_HOOK_INIT(socket_sendmsg, tomoyo_socket_sendmsg),
0568 };
0569
0570
0571 DEFINE_SRCU(tomoyo_ss);
0572
0573 int tomoyo_enabled __lsm_ro_after_init = 1;
0574
0575
0576
0577
0578
0579
0580 static int __init tomoyo_init(void)
0581 {
0582 struct tomoyo_task *s = tomoyo_task(current);
0583
0584
0585 security_add_hooks(tomoyo_hooks, ARRAY_SIZE(tomoyo_hooks), "tomoyo");
0586 pr_info("TOMOYO Linux initialized\n");
0587 s->domain_info = &tomoyo_kernel_domain;
0588 atomic_inc(&tomoyo_kernel_domain.users);
0589 s->old_domain_info = NULL;
0590 tomoyo_mm_init();
0591
0592 return 0;
0593 }
0594
0595 DEFINE_LSM(tomoyo) = {
0596 .name = "tomoyo",
0597 .enabled = &tomoyo_enabled,
0598 .flags = LSM_FLAG_LEGACY_MAJOR,
0599 .blobs = &tomoyo_blob_sizes,
0600 .init = tomoyo_init,
0601 };