TOMOYO: Use common code for open and mkdir etc.
[firefly-linux-kernel-4.4.55.git] / security / tomoyo / domain.c
index 7b8693e29a138693556160d7d16833faeb4790ab..35317e783f3458c6a06bfa4fae4441b6a959fd33 100644 (file)
 /* The initial domain. */
 struct tomoyo_domain_info tomoyo_kernel_domain;
 
+/**
+ * tomoyo_update_policy - Update an entry for exception policy.
+ *
+ * @new_entry:       Pointer to "struct tomoyo_acl_info".
+ * @size:            Size of @new_entry in bytes.
+ * @is_delete:       True if it is a delete request.
+ * @list:            Pointer to "struct list_head".
+ * @check_duplicate: Callback function to find duplicated entry.
+ *
+ * Returns 0 on success, negative value otherwise.
+ *
+ * Caller holds tomoyo_read_lock().
+ */
+int tomoyo_update_policy(struct tomoyo_acl_head *new_entry, const int size,
+                        bool is_delete, struct list_head *list,
+                        bool (*check_duplicate) (const struct tomoyo_acl_head
+                                                 *,
+                                                 const struct tomoyo_acl_head
+                                                 *))
+{
+       int error = is_delete ? -ENOENT : -ENOMEM;
+       struct tomoyo_acl_head *entry;
+
+       if (mutex_lock_interruptible(&tomoyo_policy_lock))
+               return -ENOMEM;
+       list_for_each_entry_rcu(entry, list, list) {
+               if (!check_duplicate(entry, new_entry))
+                       continue;
+               entry->is_deleted = is_delete;
+               error = 0;
+               break;
+       }
+       if (error && !is_delete) {
+               entry = tomoyo_commit_ok(new_entry, size);
+               if (entry) {
+                       list_add_tail_rcu(&entry->list, list);
+                       error = 0;
+               }
+       }
+       mutex_unlock(&tomoyo_policy_lock);
+       return error;
+}
+
+/**
+ * tomoyo_update_domain - Update an entry for domain policy.
+ *
+ * @new_entry:       Pointer to "struct tomoyo_acl_info".
+ * @size:            Size of @new_entry in bytes.
+ * @is_delete:       True if it is a delete request.
+ * @domain:          Pointer to "struct tomoyo_domain_info".
+ * @check_duplicate: Callback function to find duplicated entry.
+ * @merge_duplicate: Callback function to merge duplicated entry.
+ *
+ * Returns 0 on success, negative value otherwise.
+ *
+ * Caller holds tomoyo_read_lock().
+ */
+int tomoyo_update_domain(struct tomoyo_acl_info *new_entry, const int size,
+                        bool is_delete, struct tomoyo_domain_info *domain,
+                        bool (*check_duplicate) (const struct tomoyo_acl_info
+                                                 *,
+                                                 const struct tomoyo_acl_info
+                                                 *),
+                        bool (*merge_duplicate) (struct tomoyo_acl_info *,
+                                                 struct tomoyo_acl_info *,
+                                                 const bool))
+{
+       int error = is_delete ? -ENOENT : -ENOMEM;
+       struct tomoyo_acl_info *entry;
+
+       if (mutex_lock_interruptible(&tomoyo_policy_lock))
+               return error;
+       list_for_each_entry_rcu(entry, &domain->acl_info_list, list) {
+               if (!check_duplicate(entry, new_entry))
+                       continue;
+               if (merge_duplicate)
+                       entry->is_deleted = merge_duplicate(entry, new_entry,
+                                                           is_delete);
+               else
+                       entry->is_deleted = is_delete;
+               error = 0;
+               break;
+       }
+       if (error && !is_delete) {
+               entry = tomoyo_commit_ok(new_entry, size);
+               if (entry) {
+                       list_add_tail_rcu(&entry->list, &domain->acl_info_list);
+                       error = 0;
+               }
+       }
+       mutex_unlock(&tomoyo_policy_lock);
+       return error;
+}
+
 /*
  * tomoyo_domain_list is used for holding list of domains.
  * The ->acl_info_list of "struct tomoyo_domain_info" is used for holding
@@ -110,6 +204,20 @@ const char *tomoyo_get_last_name(const struct tomoyo_domain_info *domain)
  */
 LIST_HEAD(tomoyo_domain_initializer_list);
 
+static bool tomoyo_same_domain_initializer_entry(const struct tomoyo_acl_head *
+                                                a,
+                                                const struct tomoyo_acl_head *
+                                                b)
+{
+       const struct tomoyo_domain_initializer_entry *p1 =
+               container_of(a, typeof(*p1), head);
+       const struct tomoyo_domain_initializer_entry *p2 =
+               container_of(b, typeof(*p2), head);
+       return p1->is_not == p2->is_not && p1->is_last_name == p2->is_last_name
+               && p1->domainname == p2->domainname
+               && p1->program == p2->program;
+}
+
 /**
  * tomoyo_update_domain_initializer_entry - Update "struct tomoyo_domain_initializer_entry" list.
  *
@@ -127,15 +235,14 @@ static int tomoyo_update_domain_initializer_entry(const char *domainname,
                                                  const bool is_not,
                                                  const bool is_delete)
 {
-       struct tomoyo_domain_initializer_entry *ptr;
        struct tomoyo_domain_initializer_entry e = { .is_not = is_not };
        int error = is_delete ? -ENOENT : -ENOMEM;
 
-       if (!tomoyo_is_correct_path(program, 1, -1, -1))
-               return -EINVAL; /* No patterns allowed. */
+       if (!tomoyo_is_correct_path(program))
+               return -EINVAL;
        if (domainname) {
                if (!tomoyo_is_domain_def(domainname) &&
-                   tomoyo_is_correct_path(domainname, 1, -1, -1))
+                   tomoyo_is_correct_path(domainname))
                        e.is_last_name = true;
                else if (!tomoyo_is_correct_domain(domainname))
                        return -EINVAL;
@@ -146,25 +253,9 @@ static int tomoyo_update_domain_initializer_entry(const char *domainname,
        e.program = tomoyo_get_name(program);
        if (!e.program)
                goto out;
-       if (mutex_lock_interruptible(&tomoyo_policy_lock))
-               goto out;
-       list_for_each_entry_rcu(ptr, &tomoyo_domain_initializer_list, list) {
-               if (!tomoyo_is_same_domain_initializer_entry(ptr, &e))
-                       continue;
-               ptr->is_deleted = is_delete;
-               error = 0;
-               break;
-       }
-       if (!is_delete && error) {
-               struct tomoyo_domain_initializer_entry *entry =
-                       tomoyo_commit_ok(&e, sizeof(e));
-               if (entry) {
-                       list_add_tail_rcu(&entry->list,
-                                         &tomoyo_domain_initializer_list);
-                       error = 0;
-               }
-       }
-       mutex_unlock(&tomoyo_policy_lock);
+       error = tomoyo_update_policy(&e.head, sizeof(e), is_delete,
+                                    &tomoyo_domain_initializer_list,
+                                    tomoyo_same_domain_initializer_entry);
  out:
        tomoyo_put_name(e.domainname);
        tomoyo_put_name(e.program);
@@ -192,8 +283,8 @@ bool tomoyo_read_domain_initializer_policy(struct tomoyo_io_buffer *head)
                const char *domain = "";
                struct tomoyo_domain_initializer_entry *ptr;
                ptr = list_entry(pos, struct tomoyo_domain_initializer_entry,
-                                 list);
-               if (ptr->is_deleted)
+                                head.list);
+               if (ptr->head.is_deleted)
                        continue;
                no = ptr->is_not ? "no_" : "";
                if (ptr->domainname) {
@@ -257,8 +348,9 @@ static bool tomoyo_is_domain_initializer(const struct tomoyo_path_info *
        struct tomoyo_domain_initializer_entry *ptr;
        bool flag = false;
 
-       list_for_each_entry_rcu(ptr, &tomoyo_domain_initializer_list, list) {
-               if (ptr->is_deleted)
+       list_for_each_entry_rcu(ptr, &tomoyo_domain_initializer_list,
+                               head.list) {
+               if (ptr->head.is_deleted)
                        continue;
                if (ptr->domainname) {
                        if (!ptr->is_last_name) {
@@ -320,6 +412,18 @@ static bool tomoyo_is_domain_initializer(const struct tomoyo_path_info *
  */
 LIST_HEAD(tomoyo_domain_keeper_list);
 
+static bool tomoyo_same_domain_keeper_entry(const struct tomoyo_acl_head *a,
+                                           const struct tomoyo_acl_head *b)
+{
+       const struct tomoyo_domain_keeper_entry *p1 =
+               container_of(a, typeof(*p1), head);
+       const struct tomoyo_domain_keeper_entry *p2 =
+               container_of(b, typeof(*p2), head);
+       return p1->is_not == p2->is_not && p1->is_last_name == p2->is_last_name
+               && p1->domainname == p2->domainname
+               && p1->program == p2->program;
+}
+
 /**
  * tomoyo_update_domain_keeper_entry - Update "struct tomoyo_domain_keeper_entry" list.
  *
@@ -337,17 +441,16 @@ static int tomoyo_update_domain_keeper_entry(const char *domainname,
                                             const bool is_not,
                                             const bool is_delete)
 {
-       struct tomoyo_domain_keeper_entry *ptr;
        struct tomoyo_domain_keeper_entry e = { .is_not = is_not };
        int error = is_delete ? -ENOENT : -ENOMEM;
 
        if (!tomoyo_is_domain_def(domainname) &&
-           tomoyo_is_correct_path(domainname, 1, -1, -1))
+           tomoyo_is_correct_path(domainname))
                e.is_last_name = true;
        else if (!tomoyo_is_correct_domain(domainname))
                return -EINVAL;
        if (program) {
-               if (!tomoyo_is_correct_path(program, 1, -1, -1))
+               if (!tomoyo_is_correct_path(program))
                        return -EINVAL;
                e.program = tomoyo_get_name(program);
                if (!e.program)
@@ -356,25 +459,9 @@ static int tomoyo_update_domain_keeper_entry(const char *domainname,
        e.domainname = tomoyo_get_name(domainname);
        if (!e.domainname)
                goto out;
-       if (mutex_lock_interruptible(&tomoyo_policy_lock))
-               goto out;
-       list_for_each_entry_rcu(ptr, &tomoyo_domain_keeper_list, list) {
-               if (!tomoyo_is_same_domain_keeper_entry(ptr, &e))
-                       continue;
-               ptr->is_deleted = is_delete;
-               error = 0;
-               break;
-       }
-       if (!is_delete && error) {
-               struct tomoyo_domain_keeper_entry *entry =
-                       tomoyo_commit_ok(&e, sizeof(e));
-               if (entry) {
-                       list_add_tail_rcu(&entry->list,
-                                         &tomoyo_domain_keeper_list);
-                       error = 0;
-               }
-       }
-       mutex_unlock(&tomoyo_policy_lock);
+       error = tomoyo_update_policy(&e.head, sizeof(e), is_delete,
+                                    &tomoyo_domain_keeper_list,
+                                    tomoyo_same_domain_keeper_entry);
  out:
        tomoyo_put_name(e.domainname);
        tomoyo_put_name(e.program);
@@ -424,8 +511,9 @@ bool tomoyo_read_domain_keeper_policy(struct tomoyo_io_buffer *head)
                const char *from = "";
                const char *program = "";
 
-               ptr = list_entry(pos, struct tomoyo_domain_keeper_entry, list);
-               if (ptr->is_deleted)
+               ptr = list_entry(pos, struct tomoyo_domain_keeper_entry,
+                                head.list);
+               if (ptr->head.is_deleted)
                        continue;
                no = ptr->is_not ? "no_" : "";
                if (ptr->program) {
@@ -461,8 +549,8 @@ static bool tomoyo_is_domain_keeper(const struct tomoyo_path_info *domainname,
        struct tomoyo_domain_keeper_entry *ptr;
        bool flag = false;
 
-       list_for_each_entry_rcu(ptr, &tomoyo_domain_keeper_list, list) {
-               if (ptr->is_deleted)
+       list_for_each_entry_rcu(ptr, &tomoyo_domain_keeper_list, head.list) {
+               if (ptr->head.is_deleted)
                        continue;
                if (!ptr->is_last_name) {
                        if (ptr->domainname != domainname)
@@ -482,6 +570,131 @@ static bool tomoyo_is_domain_keeper(const struct tomoyo_path_info *domainname,
        return flag;
 }
 
+/*
+ * tomoyo_aggregator_list is used for holding list of rewrite table for
+ * execve() request. Some programs provides similar functionality. This keyword
+ * allows users to aggregate such programs.
+ *
+ * Entries are added by
+ *
+ * # echo 'aggregator /usr/bin/vi /./editor' > \
+ *                            /sys/kernel/security/tomoyo/exception_policy
+ * # echo 'aggregator /usr/bin/emacs /./editor' > \
+ *                            /sys/kernel/security/tomoyo/exception_policy
+ *
+ * and are deleted by
+ *
+ * # echo 'delete aggregator /usr/bin/vi /./editor' > \
+ *                            /sys/kernel/security/tomoyo/exception_policy
+ * # echo 'delete aggregator /usr/bin/emacs /./editor' > \
+ *                            /sys/kernel/security/tomoyo/exception_policy
+ *
+ * and all entries are retrieved by
+ *
+ * # grep ^aggregator /sys/kernel/security/tomoyo/exception_policy
+ *
+ * In the example above, if /usr/bin/vi or /usr/bin/emacs are executed,
+ * permission is checked for /./editor and domainname which the current process
+ * will belong to after execve() succeeds is calculated using /./editor .
+ */
+LIST_HEAD(tomoyo_aggregator_list);
+
+static bool tomoyo_same_aggregator_entry(const struct tomoyo_acl_head *a,
+                                        const struct tomoyo_acl_head *b)
+{
+       const struct tomoyo_aggregator_entry *p1 = container_of(a, typeof(*p1),
+                                                               head);
+       const struct tomoyo_aggregator_entry *p2 = container_of(b, typeof(*p2),
+                                                               head);
+       return p1->original_name == p2->original_name &&
+               p1->aggregated_name == p2->aggregated_name;
+}
+
+/**
+ * tomoyo_update_aggregator_entry - Update "struct tomoyo_aggregator_entry" list.
+ *
+ * @original_name:   The original program's name.
+ * @aggregated_name: The program name to use.
+ * @is_delete:       True if it is a delete request.
+ *
+ * Returns 0 on success, negative value otherwise.
+ *
+ * Caller holds tomoyo_read_lock().
+ */
+static int tomoyo_update_aggregator_entry(const char *original_name,
+                                         const char *aggregated_name,
+                                         const bool is_delete)
+{
+       struct tomoyo_aggregator_entry e = { };
+       int error = is_delete ? -ENOENT : -ENOMEM;
+
+       if (!tomoyo_is_correct_path(original_name) ||
+           !tomoyo_is_correct_path(aggregated_name))
+               return -EINVAL;
+       e.original_name = tomoyo_get_name(original_name);
+       e.aggregated_name = tomoyo_get_name(aggregated_name);
+       if (!e.original_name || !e.aggregated_name ||
+           e.aggregated_name->is_patterned) /* No patterns allowed. */
+               goto out;
+       error = tomoyo_update_policy(&e.head, sizeof(e), is_delete,
+                                    &tomoyo_aggregator_list,
+                                    tomoyo_same_aggregator_entry);
+ out:
+       tomoyo_put_name(e.original_name);
+       tomoyo_put_name(e.aggregated_name);
+       return error;
+}
+
+/**
+ * tomoyo_read_aggregator_policy - Read "struct tomoyo_aggregator_entry" list.
+ *
+ * @head: Pointer to "struct tomoyo_io_buffer".
+ *
+ * Returns true on success, false otherwise.
+ *
+ * Caller holds tomoyo_read_lock().
+ */
+bool tomoyo_read_aggregator_policy(struct tomoyo_io_buffer *head)
+{
+       struct list_head *pos;
+       bool done = true;
+
+       list_for_each_cookie(pos, head->read_var2, &tomoyo_aggregator_list) {
+               struct tomoyo_aggregator_entry *ptr;
+
+               ptr = list_entry(pos, struct tomoyo_aggregator_entry,
+                                head.list);
+               if (ptr->head.is_deleted)
+                       continue;
+               done = tomoyo_io_printf(head, TOMOYO_KEYWORD_AGGREGATOR
+                                       "%s %s\n", ptr->original_name->name,
+                                       ptr->aggregated_name->name);
+               if (!done)
+                       break;
+       }
+       return done;
+}
+
+/**
+ * tomoyo_write_aggregator_policy - Write "struct tomoyo_aggregator_entry" list.
+ *
+ * @data:      String to parse.
+ * @is_delete: True if it is a delete request.
+ *
+ * Returns 0 on success, negative value otherwise.
+ *
+ * Caller holds tomoyo_read_lock().
+ */
+int tomoyo_write_aggregator_policy(char *data, const bool is_delete)
+{
+       char *cp = strchr(data, ' ');
+
+       if (!cp)
+               return -EINVAL;
+       *cp++ = '\0';
+       return tomoyo_update_aggregator_entry(data, cp, is_delete);
+}
+
 /*
  * tomoyo_alias_list is used for holding list of symlink's pathnames which are
  * allowed to be passed to an execve() request. Normally, the domainname which
@@ -514,6 +727,17 @@ static bool tomoyo_is_domain_keeper(const struct tomoyo_path_info *domainname,
  */
 LIST_HEAD(tomoyo_alias_list);
 
+static bool tomoyo_same_alias_entry(const struct tomoyo_acl_head *a,
+                                   const struct tomoyo_acl_head *b)
+{
+       const struct tomoyo_alias_entry *p1 = container_of(a, typeof(*p1),
+                                                          head);
+       const struct tomoyo_alias_entry *p2 = container_of(b, typeof(*p2),
+                                                          head);
+       return p1->original_name == p2->original_name &&
+               p1->aliased_name == p2->aliased_name;
+}
+
 /**
  * tomoyo_update_alias_entry - Update "struct tomoyo_alias_entry" list.
  *
@@ -529,35 +753,20 @@ static int tomoyo_update_alias_entry(const char *original_name,
                                     const char *aliased_name,
                                     const bool is_delete)
 {
-       struct tomoyo_alias_entry *ptr;
        struct tomoyo_alias_entry e = { };
        int error = is_delete ? -ENOENT : -ENOMEM;
 
-       if (!tomoyo_is_correct_path(original_name, 1, -1, -1) ||
-           !tomoyo_is_correct_path(aliased_name, 1, -1, -1))
-               return -EINVAL; /* No patterns allowed. */
+       if (!tomoyo_is_correct_path(original_name) ||
+           !tomoyo_is_correct_path(aliased_name))
+               return -EINVAL;
        e.original_name = tomoyo_get_name(original_name);
        e.aliased_name = tomoyo_get_name(aliased_name);
-       if (!e.original_name || !e.aliased_name)
-               goto out;
-       if (mutex_lock_interruptible(&tomoyo_policy_lock))
-               goto out;
-       list_for_each_entry_rcu(ptr, &tomoyo_alias_list, list) {
-               if (!tomoyo_is_same_alias_entry(ptr, &e))
-                       continue;
-               ptr->is_deleted = is_delete;
-               error = 0;
-               break;
-       }
-       if (!is_delete && error) {
-               struct tomoyo_alias_entry *entry =
-                       tomoyo_commit_ok(&e, sizeof(e));
-               if (entry) {
-                       list_add_tail_rcu(&entry->list, &tomoyo_alias_list);
-                       error = 0;
-               }
-       }
-       mutex_unlock(&tomoyo_policy_lock);
+       if (!e.original_name || !e.aliased_name ||
+           e.original_name->is_patterned || e.aliased_name->is_patterned)
+               goto out; /* No patterns allowed. */
+       error = tomoyo_update_policy(&e.head, sizeof(e), is_delete,
+                                    &tomoyo_alias_list,
+                                    tomoyo_same_alias_entry);
  out:
        tomoyo_put_name(e.original_name);
        tomoyo_put_name(e.aliased_name);
@@ -581,8 +790,8 @@ bool tomoyo_read_alias_policy(struct tomoyo_io_buffer *head)
        list_for_each_cookie(pos, head->read_var2, &tomoyo_alias_list) {
                struct tomoyo_alias_entry *ptr;
 
-               ptr = list_entry(pos, struct tomoyo_alias_entry, list);
-               if (ptr->is_deleted)
+               ptr = list_entry(pos, struct tomoyo_alias_entry, head.list);
+               if (ptr->head.is_deleted)
                        continue;
                done = tomoyo_io_printf(head, TOMOYO_KEYWORD_ALIAS "%s %s\n",
                                        ptr->original_name->name,
@@ -681,8 +890,8 @@ int tomoyo_find_next_domain(struct linux_binprm *bprm)
        struct tomoyo_domain_info *domain = NULL;
        const char *old_domain_name = old_domain->domainname->name;
        const char *original_name = bprm->filename;
-       const u8 mode = tomoyo_check_flags(old_domain, TOMOYO_MAC_FOR_FILE);
-       const bool is_enforce = (mode == TOMOYO_CONFIG_ENFORCING);
+       u8 mode;
+       bool is_enforce;
        int retval = -ENOMEM;
        bool need_kfree = false;
        struct tomoyo_path_info rn = { }; /* real name */
@@ -691,7 +900,8 @@ int tomoyo_find_next_domain(struct linux_binprm *bprm)
 
        ln.name = tomoyo_get_last_name(old_domain);
        tomoyo_fill_path_info(&ln);
-       tomoyo_init_request_info(&r, NULL);
+       mode = tomoyo_init_request_info(&r, NULL, TOMOYO_MAC_FILE_EXECUTE);
+       is_enforce = (mode == TOMOYO_CONFIG_ENFORCING);
        if (!tmp)
                goto out;
 
@@ -718,8 +928,8 @@ int tomoyo_find_next_domain(struct linux_binprm *bprm)
        if (tomoyo_pathcmp(&rn, &sn)) {
                struct tomoyo_alias_entry *ptr;
                /* Is this program allowed to be called via symbolic links? */
-               list_for_each_entry_rcu(ptr, &tomoyo_alias_list, list) {
-                       if (ptr->is_deleted ||
+               list_for_each_entry_rcu(ptr, &tomoyo_alias_list, head.list) {
+                       if (ptr->head.is_deleted ||
                            tomoyo_pathcmp(&rn, ptr->original_name) ||
                            tomoyo_pathcmp(&sn, ptr->aliased_name))
                                continue;
@@ -731,8 +941,26 @@ int tomoyo_find_next_domain(struct linux_binprm *bprm)
                }
        }
 
+       /* Check 'aggregator' directive. */
+       {
+               struct tomoyo_aggregator_entry *ptr;
+               list_for_each_entry_rcu(ptr, &tomoyo_aggregator_list,
+                                       head.list) {
+                       if (ptr->head.is_deleted ||
+                           !tomoyo_path_matches_pattern(&rn,
+                                                        ptr->original_name))
+                               continue;
+                       if (need_kfree)
+                               kfree(rn.name);
+                       need_kfree = false;
+                       /* This is OK because it is read only. */
+                       rn = *ptr->aggregated_name;
+                       break;
+               }
+       }
+
        /* Check execute permission. */
-       retval = tomoyo_check_exec_perm(old_domain, &rn);
+       retval = tomoyo_path_permission(&r, TOMOYO_TYPE_EXECUTE, &rn);
        if (retval == TOMOYO_RETRY_REQUEST)
                goto retry;
        if (retval < 0)