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