]> bbs.cooldavid.org Git - net-next-2.6.git/blobdiff - security/tomoyo/tomoyo.c
TOMOYO: Add refcounter on domain structure.
[net-next-2.6.git] / security / tomoyo / tomoyo.c
index 8a00ade85166615a67fc768f595e00ac782b0863..87e82bfeac2f5e38dae6badd227c33936e5681cb 100644 (file)
@@ -11,8 +11,6 @@
 
 #include <linux/security.h>
 #include "common.h"
-#include "tomoyo.h"
-#include "realpath.h"
 
 static int tomoyo_cred_alloc_blank(struct cred *new, gfp_t gfp)
 {
@@ -23,21 +21,23 @@ static int tomoyo_cred_alloc_blank(struct cred *new, gfp_t gfp)
 static int tomoyo_cred_prepare(struct cred *new, const struct cred *old,
                               gfp_t gfp)
 {
-       /*
-        * Since "struct tomoyo_domain_info *" is a sharable pointer,
-        * we don't need to duplicate.
-        */
-       new->security = old->security;
+       struct tomoyo_domain_info *domain = old->security;
+       new->security = domain;
+       if (domain)
+               atomic_inc(&domain->users);
        return 0;
 }
 
 static void tomoyo_cred_transfer(struct cred *new, const struct cred *old)
 {
-       /*
-        * Since "struct tomoyo_domain_info *" is a sharable pointer,
-        * we don't need to duplicate.
-        */
-       new->security = old->security;
+       tomoyo_cred_prepare(new, old, 0);
+}
+
+static void tomoyo_cred_free(struct cred *cred)
+{
+       struct tomoyo_domain_info *domain = cred->security;
+       if (domain)
+               atomic_dec(&domain->users);
 }
 
 static int tomoyo_bprm_set_creds(struct linux_binprm *bprm)
@@ -60,6 +60,14 @@ static int tomoyo_bprm_set_creds(struct linux_binprm *bprm)
         */
        if (!tomoyo_policy_loaded)
                tomoyo_load_policy(bprm->filename);
+       /*
+        * Release reference to "struct tomoyo_domain_info" stored inside
+        * "bprm->cred->security". New reference to "struct tomoyo_domain_info"
+        * stored inside "bprm->cred->security" will be acquired later inside
+        * tomoyo_find_next_domain().
+        */
+       atomic_dec(&((struct tomoyo_domain_info *)
+                    bprm->cred->security)->users);
        /*
         * Tell tomoyo_bprm_check_security() is called for the first time of an
         * execve operation.
@@ -76,8 +84,12 @@ static int tomoyo_bprm_check_security(struct linux_binprm *bprm)
         * Execute permission is checked against pathname passed to do_execve()
         * using current domain.
         */
-       if (!domain)
-               return tomoyo_find_next_domain(bprm);
+       if (!domain) {
+               const int idx = tomoyo_read_lock();
+               const int err = tomoyo_find_next_domain(bprm);
+               tomoyo_read_unlock(idx);
+               return err;
+       }
        /*
         * Read permission is checked against interpreters using next domain.
         * '1' is the result of open_to_namei_flags(O_RDONLY).
@@ -194,6 +206,60 @@ static int tomoyo_dentry_open(struct file *f, const struct cred *cred)
        return tomoyo_check_open_permission(tomoyo_domain(), &f->f_path, flags);
 }
 
+static int tomoyo_file_ioctl(struct file *file, unsigned int cmd,
+                            unsigned long arg)
+{
+       return tomoyo_check_1path_perm(tomoyo_domain(), TOMOYO_TYPE_IOCTL_ACL,
+                                      &file->f_path);
+}
+
+static int tomoyo_path_chmod(struct dentry *dentry, struct vfsmount *mnt,
+                            mode_t mode)
+{
+       struct path path = { mnt, dentry };
+       return tomoyo_check_1path_perm(tomoyo_domain(), TOMOYO_TYPE_CHMOD_ACL,
+                                      &path);
+}
+
+static int tomoyo_path_chown(struct path *path, uid_t uid, gid_t gid)
+{
+       int error = 0;
+       if (uid != (uid_t) -1)
+               error = tomoyo_check_1path_perm(tomoyo_domain(),
+                                               TOMOYO_TYPE_CHOWN_ACL, path);
+       if (!error && gid != (gid_t) -1)
+               error = tomoyo_check_1path_perm(tomoyo_domain(),
+                                               TOMOYO_TYPE_CHGRP_ACL, path);
+       return error;
+}
+
+static int tomoyo_path_chroot(struct path *path)
+{
+       return tomoyo_check_1path_perm(tomoyo_domain(), TOMOYO_TYPE_CHROOT_ACL,
+                                      path);
+}
+
+static int tomoyo_sb_mount(char *dev_name, struct path *path,
+                          char *type, unsigned long flags, void *data)
+{
+       return tomoyo_check_1path_perm(tomoyo_domain(), TOMOYO_TYPE_MOUNT_ACL,
+                                      path);
+}
+
+static int tomoyo_sb_umount(struct vfsmount *mnt, int flags)
+{
+       struct path path = { mnt, mnt->mnt_root };
+       return tomoyo_check_1path_perm(tomoyo_domain(), TOMOYO_TYPE_UMOUNT_ACL,
+                                      &path);
+}
+
+static int tomoyo_sb_pivotroot(struct path *old_path, struct path *new_path)
+{
+       return tomoyo_check_2path_perm(tomoyo_domain(),
+                                      TOMOYO_TYPE_PIVOT_ROOT_ACL,
+                                      new_path, old_path);
+}
+
 /*
  * tomoyo_security_ops is a "struct security_operations" which is used for
  * registering TOMOYO.
@@ -203,6 +269,7 @@ static struct security_operations tomoyo_security_ops = {
        .cred_alloc_blank    = tomoyo_cred_alloc_blank,
        .cred_prepare        = tomoyo_cred_prepare,
        .cred_transfer       = tomoyo_cred_transfer,
+       .cred_free           = tomoyo_cred_free,
        .bprm_set_creds      = tomoyo_bprm_set_creds,
        .bprm_check_security = tomoyo_bprm_check_security,
        .file_fcntl          = tomoyo_file_fcntl,
@@ -215,8 +282,18 @@ static struct security_operations tomoyo_security_ops = {
        .path_mknod          = tomoyo_path_mknod,
        .path_link           = tomoyo_path_link,
        .path_rename         = tomoyo_path_rename,
+       .file_ioctl          = tomoyo_file_ioctl,
+       .path_chmod          = tomoyo_path_chmod,
+       .path_chown          = tomoyo_path_chown,
+       .path_chroot         = tomoyo_path_chroot,
+       .sb_mount            = tomoyo_sb_mount,
+       .sb_umount           = tomoyo_sb_umount,
+       .sb_pivotroot        = tomoyo_sb_pivotroot,
 };
 
+/* Lock for GC. */
+struct srcu_struct tomoyo_ss;
+
 static int __init tomoyo_init(void)
 {
        struct cred *cred = (struct cred *) current_cred();
@@ -224,7 +301,8 @@ static int __init tomoyo_init(void)
        if (!security_module_enable(&tomoyo_security_ops))
                return 0;
        /* register ourselves with the security framework */
-       if (register_security(&tomoyo_security_ops))
+       if (register_security(&tomoyo_security_ops) ||
+           init_srcu_struct(&tomoyo_ss))
                panic("Failure registering TOMOYO Linux");
        printk(KERN_INFO "TOMOYO Linux initialized\n");
        cred->security = &tomoyo_kernel_domain;