netns xfrm: fix "ip xfrm state|policy count" misreport
[safe/jmp/linux-2.6] / security / tomoyo / common.c
index ddfb9cc..e0d0354 100644 (file)
@@ -28,7 +28,13 @@ static const char *tomoyo_mode_2[4] = {
        "disabled", "enabled", "enabled", "enabled"
 };
 
-/* Table for profile. */
+/*
+ * tomoyo_control_array is a static data which contains
+ *
+ *  (1) functionality name used by /sys/kernel/security/tomoyo/profile .
+ *  (2) initial values for "struct tomoyo_profile".
+ *  (3) max values for "struct tomoyo_profile".
+ */
 static struct {
        const char *keyword;
        unsigned int current_value;
@@ -39,7 +45,13 @@ static struct {
        [TOMOYO_VERBOSE]          = { "TOMOYO_VERBOSE",      1,       1 },
 };
 
-/* Profile table. Memory is allocated as needed. */
+/*
+ * tomoyo_profile is a structure which is used for holding the mode of access
+ * controls. TOMOYO has 4 modes: disabled, learning, permissive, enforcing.
+ * An administrator can define up to 256 profiles.
+ * The ->profile of "struct tomoyo_domain_info" is used for remembering
+ * the profile's number (0 - 255) assigned to that domain.
+ */
 static struct tomoyo_profile {
        unsigned int value[TOMOYO_MAX_CONTROL_INDEX];
        const struct tomoyo_path_info *comment;
@@ -175,6 +187,8 @@ bool tomoyo_is_correct_path(const char *filename, const s8 start_type,
                            const s8 pattern_type, const s8 end_type,
                            const char *function)
 {
+       const char *const start = filename;
+       bool in_repetition = false;
        bool contains_pattern = false;
        unsigned char c;
        unsigned char d;
@@ -200,9 +214,13 @@ bool tomoyo_is_correct_path(const char *filename, const s8 start_type,
                if (c == '/')
                        goto out;
        }
-       while ((c = *filename++) != '\0') {
+       while (1) {
+               c = *filename++;
+               if (!c)
+                       break;
                if (c == '\\') {
-                       switch ((c = *filename++)) {
+                       c = *filename++;
+                       switch (c) {
                        case '\\':  /* "\\" */
                                continue;
                        case '$':   /* "\$" */
@@ -219,6 +237,22 @@ bool tomoyo_is_correct_path(const char *filename, const s8 start_type,
                                        break; /* Must not contain pattern */
                                contains_pattern = true;
                                continue;
+                       case '{':   /* "/\{" */
+                               if (filename - 3 < start ||
+                                   *(filename - 3) != '/')
+                                       break;
+                               if (pattern_type == -1)
+                                       break; /* Must not contain pattern */
+                               contains_pattern = true;
+                               in_repetition = true;
+                               continue;
+                       case '}':   /* "\}/" */
+                               if (*filename != '/')
+                                       break;
+                               if (!in_repetition)
+                                       break;
+                               in_repetition = false;
+                               continue;
                        case '0':   /* "\ooo" */
                        case '1':
                        case '2':
@@ -234,6 +268,8 @@ bool tomoyo_is_correct_path(const char *filename, const s8 start_type,
                                        continue; /* pattern is not \000 */
                        }
                        goto out;
+               } else if (in_repetition && c == '/') {
+                       goto out;
                } else if (tomoyo_is_invalid(c)) {
                        goto out;
                }
@@ -242,6 +278,8 @@ bool tomoyo_is_correct_path(const char *filename, const s8 start_type,
                if (!contains_pattern)
                        goto out;
        }
+       if (in_repetition)
+               goto out;
        return true;
  out:
        printk(KERN_DEBUG "%s: Invalid pathname '%s'\n", function,
@@ -348,33 +386,6 @@ struct tomoyo_domain_info *tomoyo_find_domain(const char *domainname)
 }
 
 /**
- * tomoyo_path_depth - Evaluate the number of '/' in a string.
- *
- * @pathname: The string to evaluate.
- *
- * Returns path depth of the string.
- *
- * I score 2 for each of the '/' in the @pathname
- * and score 1 if the @pathname ends with '/'.
- */
-static int tomoyo_path_depth(const char *pathname)
-{
-       int i = 0;
-
-       if (pathname) {
-               const char *ep = pathname + strlen(pathname);
-               if (pathname < ep--) {
-                       if (*ep != '/')
-                               i++;
-                       while (pathname <= ep)
-                               if (*ep-- == '/')
-                                       i += 2;
-               }
-       }
-       return i;
-}
-
-/**
  * tomoyo_const_part_length - Evaluate the initial length without a pattern in a token.
  *
  * @filename: The string to evaluate.
@@ -428,16 +439,14 @@ void tomoyo_fill_path_info(struct tomoyo_path_info *ptr)
        const char *name = ptr->name;
        const int len = strlen(name);
 
-       ptr->total_len = len;
        ptr->const_len = tomoyo_const_part_length(name);
        ptr->is_dir = len && (name[len - 1] == '/');
        ptr->is_patterned = (ptr->const_len < len);
        ptr->hash = full_name_hash(name, len);
-       ptr->depth = tomoyo_path_depth(name);
 }
 
 /**
- * tomoyo_file_matches_to_pattern2 - Pattern matching without '/' character
+ * tomoyo_file_matches_pattern2 - Pattern matching without '/' character
  * and "\-" pattern.
  *
  * @filename:     The start of string to check.
@@ -447,10 +456,10 @@ void tomoyo_fill_path_info(struct tomoyo_path_info *ptr)
  *
  * Returns true if @filename matches @pattern, false otherwise.
  */
-static bool tomoyo_file_matches_to_pattern2(const char *filename,
-                                           const char *filename_end,
-                                           const char *pattern,
-                                           const char *pattern_end)
+static bool tomoyo_file_matches_pattern2(const char *filename,
+                                        const char *filename_end,
+                                        const char *pattern,
+                                        const char *pattern_end)
 {
        while (filename < filename_end && pattern < pattern_end) {
                char c;
@@ -508,7 +517,7 @@ static bool tomoyo_file_matches_to_pattern2(const char *filename,
                case '*':
                case '@':
                        for (i = 0; i <= filename_end - filename; i++) {
-                               if (tomoyo_file_matches_to_pattern2(
+                               if (tomoyo_file_matches_pattern2(
                                                    filename + i, filename_end,
                                                    pattern + 1, pattern_end))
                                        return true;
@@ -539,7 +548,7 @@ static bool tomoyo_file_matches_to_pattern2(const char *filename,
                                        j++;
                        }
                        for (i = 1; i <= j; i++) {
-                               if (tomoyo_file_matches_to_pattern2(
+                               if (tomoyo_file_matches_pattern2(
                                                    filename + i, filename_end,
                                                    pattern + 1, pattern_end))
                                        return true;
@@ -556,7 +565,7 @@ static bool tomoyo_file_matches_to_pattern2(const char *filename,
 }
 
 /**
- * tomoyo_file_matches_to_pattern - Pattern matching without without '/' character.
+ * tomoyo_file_matches_pattern - Pattern matching without without '/' character.
  *
  * @filename:     The start of string to check.
  * @filename_end: The end of string to check.
@@ -565,7 +574,7 @@ static bool tomoyo_file_matches_to_pattern2(const char *filename,
  *
  * Returns true if @filename matches @pattern, false otherwise.
  */
-static bool tomoyo_file_matches_to_pattern(const char *filename,
+static bool tomoyo_file_matches_pattern(const char *filename,
                                           const char *filename_end,
                                           const char *pattern,
                                           const char *pattern_end)
@@ -578,10 +587,10 @@ static bool tomoyo_file_matches_to_pattern(const char *filename,
                /* Split at "\-" pattern. */
                if (*pattern++ != '\\' || *pattern++ != '-')
                        continue;
-               result = tomoyo_file_matches_to_pattern2(filename,
-                                                        filename_end,
-                                                        pattern_start,
-                                                        pattern - 2);
+               result = tomoyo_file_matches_pattern2(filename,
+                                                     filename_end,
+                                                     pattern_start,
+                                                     pattern - 2);
                if (first)
                        result = !result;
                if (result)
@@ -589,13 +598,79 @@ static bool tomoyo_file_matches_to_pattern(const char *filename,
                first = false;
                pattern_start = pattern;
        }
-       result = tomoyo_file_matches_to_pattern2(filename, filename_end,
-                                                pattern_start, pattern_end);
+       result = tomoyo_file_matches_pattern2(filename, filename_end,
+                                             pattern_start, pattern_end);
        return first ? result : !result;
 }
 
 /**
+ * tomoyo_path_matches_pattern2 - Do pathname pattern matching.
+ *
+ * @f: The start of string to check.
+ * @p: The start of pattern to compare.
+ *
+ * Returns true if @f matches @p, false otherwise.
+ */
+static bool tomoyo_path_matches_pattern2(const char *f, const char *p)
+{
+       const char *f_delimiter;
+       const char *p_delimiter;
+
+       while (*f && *p) {
+               f_delimiter = strchr(f, '/');
+               if (!f_delimiter)
+                       f_delimiter = f + strlen(f);
+               p_delimiter = strchr(p, '/');
+               if (!p_delimiter)
+                       p_delimiter = p + strlen(p);
+               if (*p == '\\' && *(p + 1) == '{')
+                       goto recursive;
+               if (!tomoyo_file_matches_pattern(f, f_delimiter, p,
+                                                p_delimiter))
+                       return false;
+               f = f_delimiter;
+               if (*f)
+                       f++;
+               p = p_delimiter;
+               if (*p)
+                       p++;
+       }
+       /* Ignore trailing "\*" and "\@" in @pattern. */
+       while (*p == '\\' &&
+              (*(p + 1) == '*' || *(p + 1) == '@'))
+               p += 2;
+       return !*f && !*p;
+ recursive:
+       /*
+        * The "\{" pattern is permitted only after '/' character.
+        * This guarantees that below "*(p - 1)" is safe.
+        * Also, the "\}" pattern is permitted only before '/' character
+        * so that "\{" + "\}" pair will not break the "\-" operator.
+        */
+       if (*(p - 1) != '/' || p_delimiter <= p + 3 || *p_delimiter != '/' ||
+           *(p_delimiter - 1) != '}' || *(p_delimiter - 2) != '\\')
+               return false; /* Bad pattern. */
+       do {
+               /* Compare current component with pattern. */
+               if (!tomoyo_file_matches_pattern(f, f_delimiter, p + 2,
+                                                p_delimiter - 2))
+                       break;
+               /* Proceed to next component. */
+               f = f_delimiter;
+               if (!*f)
+                       break;
+               f++;
+               /* Continue comparison. */
+               if (tomoyo_path_matches_pattern2(f, p_delimiter + 1))
+                       return true;
+               f_delimiter = strchr(f, '/');
+       } while (f_delimiter);
+       return false; /* Not matched. */
+}
+
+/**
  * tomoyo_path_matches_pattern - Check whether the given filename matches the given pattern.
+ *
  * @filename: The filename to check.
  * @pattern:  The pattern to compare.
  *
@@ -604,24 +679,24 @@ static bool tomoyo_file_matches_to_pattern(const char *filename,
  * The following patterns are available.
  *   \\     \ itself.
  *   \ooo   Octal representation of a byte.
- *   \*     More than or equals to 0 character other than '/'.
- *   \@     More than or equals to 0 character other than '/' or '.'.
+ *   \*     Zero or more repetitions of characters other than '/'.
+ *   \@     Zero or more repetitions of characters other than '/' or '.'.
  *   \?     1 byte character other than '/'.
- *   \$     More than or equals to 1 decimal digit.
+ *   \$     One or more repetitions of decimal digits.
  *   \+     1 decimal digit.
- *   \X     More than or equals to 1 hexadecimal digit.
+ *   \X     One or more repetitions of hexadecimal digits.
  *   \x     1 hexadecimal digit.
- *   \A     More than or equals to 1 alphabet character.
+ *   \A     One or more repetitions of alphabet characters.
  *   \a     1 alphabet character.
+ *
  *   \-     Subtraction operator.
+ *
+ *   /\{dir\}/   '/' + 'One or more repetitions of dir/' (e.g. /dir/ /dir/dir/
+ *               /dir/dir/dir/ ).
  */
 bool tomoyo_path_matches_pattern(const struct tomoyo_path_info *filename,
                                 const struct tomoyo_path_info *pattern)
 {
-       /*
-         if (!filename || !pattern)
-         return false;
-       */
        const char *f = filename->name;
        const char *p = pattern->name;
        const int len = pattern->const_len;
@@ -629,37 +704,15 @@ bool tomoyo_path_matches_pattern(const struct tomoyo_path_info *filename,
        /* If @pattern doesn't contain pattern, I can use strcmp(). */
        if (!pattern->is_patterned)
                return !tomoyo_pathcmp(filename, pattern);
-       /* Dont compare if the number of '/' differs. */
-       if (filename->depth != pattern->depth)
+       /* Don't compare directory and non-directory. */
+       if (filename->is_dir != pattern->is_dir)
                return false;
        /* Compare the initial length without patterns. */
        if (strncmp(f, p, len))
                return false;
        f += len;
        p += len;
-       /* Main loop. Compare each directory component. */
-       while (*f && *p) {
-               const char *f_delimiter = strchr(f, '/');
-               const char *p_delimiter = strchr(p, '/');
-               if (!f_delimiter)
-                       f_delimiter = f + strlen(f);
-               if (!p_delimiter)
-                       p_delimiter = p + strlen(p);
-               if (!tomoyo_file_matches_to_pattern(f, f_delimiter,
-                                                   p, p_delimiter))
-                       return false;
-               f = f_delimiter;
-               if (*f)
-                       f++;
-               p = p_delimiter;
-               if (*p)
-                       p++;
-       }
-       /* Ignore trailing "\*" and "\@" in @pattern. */
-       while (*p == '\\' &&
-              (*(p + 1) == '*' || *(p + 1) == '@'))
-               p += 2;
-       return !*f && !*p;
+       return tomoyo_path_matches_pattern2(f, p);
 }
 
 /**
@@ -866,7 +919,6 @@ static struct tomoyo_profile *tomoyo_find_or_assign_new_profile(const unsigned
 
        if (profile >= TOMOYO_MAX_PROFILES)
                return NULL;
-       /***** EXCLUSIVE SECTION START *****/
        mutex_lock(&lock);
        ptr = tomoyo_profile_ptr[profile];
        if (ptr)
@@ -880,7 +932,6 @@ static struct tomoyo_profile *tomoyo_find_or_assign_new_profile(const unsigned
        tomoyo_profile_ptr[profile] = ptr;
  ok:
        mutex_unlock(&lock);
-       /***** EXCLUSIVE SECTION END *****/
        return ptr;
 }
 
@@ -1009,7 +1060,19 @@ static int tomoyo_read_profile(struct tomoyo_io_buffer *head)
        return 0;
 }
 
-/* Structure for policy manager. */
+/*
+ * tomoyo_policy_manager_entry is a structure which is used for holding list of
+ * domainnames or programs which are permitted to modify configuration via
+ * /sys/kernel/security/tomoyo/ interface.
+ * It has following fields.
+ *
+ *  (1) "list" which is linked to tomoyo_policy_manager_list .
+ *  (2) "manager" is a domainname or a program's pathname.
+ *  (3) "is_domain" is a bool which is true if "manager" is a domainname, false
+ *      otherwise.
+ *  (4) "is_deleted" is a bool which is true if marked as deleted, false
+ *      otherwise.
+ */
 struct tomoyo_policy_manager_entry {
        struct list_head list;
        /* A path to program or a domainname. */
@@ -1018,7 +1081,36 @@ struct tomoyo_policy_manager_entry {
        bool is_deleted; /* True if this entry is deleted. */
 };
 
-/* The list for "struct tomoyo_policy_manager_entry". */
+/*
+ * tomoyo_policy_manager_list is used for holding list of domainnames or
+ * programs which are permitted to modify configuration via
+ * /sys/kernel/security/tomoyo/ interface.
+ *
+ * An entry is added by
+ *
+ * # echo '<kernel> /sbin/mingetty /bin/login /bin/bash' > \
+ *                                        /sys/kernel/security/tomoyo/manager
+ *  (if you want to specify by a domainname)
+ *
+ *  or
+ *
+ * # echo '/usr/lib/ccs/editpolicy' > /sys/kernel/security/tomoyo/manager
+ *  (if you want to specify by a program's location)
+ *
+ * and is deleted by
+ *
+ * # echo 'delete <kernel> /sbin/mingetty /bin/login /bin/bash' > \
+ *                                        /sys/kernel/security/tomoyo/manager
+ *
+ *  or
+ *
+ * # echo 'delete /usr/lib/ccs/editpolicy' > \
+ *                                        /sys/kernel/security/tomoyo/manager
+ *
+ * and all entries are retrieved by
+ *
+ * # cat /sys/kernel/security/tomoyo/manager
+ */
 static LIST_HEAD(tomoyo_policy_manager_list);
 static DECLARE_RWSEM(tomoyo_policy_manager_list_lock);
 
@@ -1050,7 +1142,6 @@ static int tomoyo_update_manager_entry(const char *manager,
        saved_manager = tomoyo_save_name(manager);
        if (!saved_manager)
                return -ENOMEM;
-       /***** EXCLUSIVE SECTION START *****/
        down_write(&tomoyo_policy_manager_list_lock);
        list_for_each_entry(ptr, &tomoyo_policy_manager_list, list) {
                if (ptr->manager != saved_manager)
@@ -1072,7 +1163,6 @@ static int tomoyo_update_manager_entry(const char *manager,
        error = 0;
  out:
        up_write(&tomoyo_policy_manager_list_lock);
-       /***** EXCLUSIVE SECTION END *****/
        return error;
 }
 
@@ -1117,10 +1207,9 @@ static int tomoyo_read_manager_policy(struct tomoyo_io_buffer *head)
                                 list);
                if (ptr->is_deleted)
                        continue;
-               if (!tomoyo_io_printf(head, "%s\n", ptr->manager->name)) {
-                       done = false;
+               done = tomoyo_io_printf(head, "%s\n", ptr->manager->name);
+               if (!done)
                        break;
-               }
        }
        up_read(&tomoyo_policy_manager_list_lock);
        head->read_eof = done;
@@ -1197,13 +1286,11 @@ static bool tomoyo_is_select_one(struct tomoyo_io_buffer *head,
 
        if (sscanf(data, "pid=%u", &pid) == 1) {
                struct task_struct *p;
-               /***** CRITICAL SECTION START *****/
                read_lock(&tasklist_lock);
                p = find_task_by_vpid(pid);
                if (p)
                        domain = tomoyo_real_domain(p);
                read_unlock(&tasklist_lock);
-               /***** CRITICAL SECTION END *****/
        } else if (!strncmp(data, "domain=", 7)) {
                if (tomoyo_is_domain_def(data + 7)) {
                        down_read(&tomoyo_domain_list_lock);
@@ -1240,6 +1327,36 @@ static bool tomoyo_is_select_one(struct tomoyo_io_buffer *head,
 }
 
 /**
+ * tomoyo_delete_domain - Delete a domain.
+ *
+ * @domainname: The name of domain.
+ *
+ * Returns 0.
+ */
+static int tomoyo_delete_domain(char *domainname)
+{
+       struct tomoyo_domain_info *domain;
+       struct tomoyo_path_info name;
+
+       name.name = domainname;
+       tomoyo_fill_path_info(&name);
+       down_write(&tomoyo_domain_list_lock);
+       /* Is there an active domain? */
+       list_for_each_entry(domain, &tomoyo_domain_list, list) {
+               /* Never delete tomoyo_kernel_domain */
+               if (domain == &tomoyo_kernel_domain)
+                       continue;
+               if (domain->is_deleted ||
+                   tomoyo_pathcmp(domain->domainname, &name))
+                       continue;
+               domain->is_deleted = true;
+               break;
+       }
+       up_write(&tomoyo_domain_list_lock);
+       return 0;
+}
+
+/**
  * tomoyo_write_domain_policy - Write domain policy.
  *
  * @head: Pointer to "struct tomoyo_io_buffer".
@@ -1447,15 +1564,14 @@ static int tomoyo_read_domain_policy(struct tomoyo_io_buffer *head)
                    TOMOYO_DOMAIN_FLAGS_IGNORE_GLOBAL_ALLOW_READ)
                        ignore_global_allow_read
                                = TOMOYO_KEYWORD_IGNORE_GLOBAL_ALLOW_READ "\n";
-               if (!tomoyo_io_printf(head,
-                                     "%s\n" TOMOYO_KEYWORD_USE_PROFILE "%u\n"
-                                     "%s%s%s\n", domain->domainname->name,
-                                     domain->profile, quota_exceeded,
-                                     transition_failed,
-                                     ignore_global_allow_read)) {
-                       done = false;
+               done = tomoyo_io_printf(head, "%s\n" TOMOYO_KEYWORD_USE_PROFILE
+                                       "%u\n%s%s%s\n",
+                                       domain->domainname->name,
+                                       domain->profile, quota_exceeded,
+                                       transition_failed,
+                                       ignore_global_allow_read);
+               if (!done)
                        break;
-               }
                head->read_step = 2;
 acl_loop:
                if (head->read_step == 3)
@@ -1463,24 +1579,22 @@ acl_loop:
                /* Print ACL entries in the domain. */
                down_read(&tomoyo_domain_acl_info_list_lock);
                list_for_each_cookie(apos, head->read_var2,
-                                     &domain->acl_info_list) {
+                                    &domain->acl_info_list) {
                        struct tomoyo_acl_info *ptr
                                = list_entry(apos, struct tomoyo_acl_info,
-                                             list);
-                       if (!tomoyo_print_entry(head, ptr)) {
-                               done = false;
+                                            list);
+                       done = tomoyo_print_entry(head, ptr);
+                       if (!done)
                                break;
-                       }
                }
                up_read(&tomoyo_domain_acl_info_list_lock);
                if (!done)
                        break;
                head->read_step = 3;
 tail_mark:
-               if (!tomoyo_io_printf(head, "\n")) {
-                       done = false;
+               done = tomoyo_io_printf(head, "\n");
+               if (!done)
                        break;
-               }
                head->read_step = 1;
                if (head->read_single_domain)
                        break;
@@ -1550,11 +1664,10 @@ static int tomoyo_read_domain_profile(struct tomoyo_io_buffer *head)
                domain = list_entry(pos, struct tomoyo_domain_info, list);
                if (domain->is_deleted)
                        continue;
-               if (!tomoyo_io_printf(head, "%u %s\n", domain->profile,
-                                     domain->domainname->name)) {
-                       done = false;
+               done = tomoyo_io_printf(head, "%u %s\n", domain->profile,
+                                       domain->domainname->name);
+               if (!done)
                        break;
-               }
        }
        up_read(&tomoyo_domain_list_lock);
        head->read_eof = done;
@@ -1594,13 +1707,11 @@ static int tomoyo_read_pid(struct tomoyo_io_buffer *head)
                const int pid = head->read_step;
                struct task_struct *p;
                struct tomoyo_domain_info *domain = NULL;
-               /***** CRITICAL SECTION START *****/
                read_lock(&tasklist_lock);
                p = find_task_by_vpid(pid);
                if (p)
                        domain = tomoyo_real_domain(p);
                read_unlock(&tasklist_lock);
-               /***** CRITICAL SECTION END *****/
                if (domain)
                        tomoyo_io_printf(head, "%d %u %s", pid, domain->profile,
                                         domain->domainname->name);
@@ -2138,7 +2249,13 @@ static ssize_t tomoyo_write(struct file *file, const char __user *buf,
        return tomoyo_write_control(file, buf, count);
 }
 
-/* Operations for /sys/kernel/security/tomoyo/ interface. */
+/*
+ * tomoyo_operations is a "struct file_operations" which is used for handling
+ * /sys/kernel/security/tomoyo/ interface.
+ *
+ * Some files under /sys/kernel/security/tomoyo/ directory accept open(O_RDWR).
+ * See tomoyo_io_buffer for internals.
+ */
 static const struct file_operations tomoyo_operations = {
        .open    = tomoyo_open,
        .release = tomoyo_release,