Merge git://git.kernel.org/pub/scm/linux/kernel/git/ebiederm/sysctl-2.6
[safe/jmp/linux-2.6] / security / tomoyo / realpath.c
index d47f16b..18369d4 100644 (file)
@@ -5,13 +5,16 @@
  *
  * Copyright (C) 2005-2009  NTT DATA CORPORATION
  *
- * Version: 2.2.0-pre   2009/02/01
+ * Version: 2.2.0   2009/04/01
  *
  */
 
 #include <linux/types.h>
 #include <linux/mount.h>
 #include <linux/mnt_namespace.h>
+#include <linux/fs_struct.h>
+#include <linux/hash.h>
+
 #include "common.h"
 #include "realpath.h"
 
@@ -107,6 +110,15 @@ int tomoyo_realpath_from_path2(struct path *path, char *newname,
                spin_unlock(&dcache_lock);
                path_put(&root);
                path_put(&ns_root);
+               /* Prepend "/proc" prefix if using internal proc vfs mount. */
+               if (!IS_ERR(sp) && (path->mnt->mnt_parent == path->mnt) &&
+                   (strcmp(path->mnt->mnt_sb->s_type->name, "proc") == 0)) {
+                       sp -= 5;
+                       if (sp >= newname)
+                               memcpy(sp, "/proc", 5);
+                       else
+                               sp = ERR_PTR(-ENOMEM);
+               }
        }
        if (IS_ERR(sp))
                error = PTR_ERR(sp);
@@ -164,11 +176,11 @@ char *tomoyo_realpath_from_path(struct path *path)
  */
 char *tomoyo_realpath(const char *pathname)
 {
-       struct nameidata nd;
+       struct path path;
 
-       if (pathname && path_lookup(pathname, LOOKUP_FOLLOW, &nd) == 0) {
-               char *buf = tomoyo_realpath_from_path(&nd.path);
-               path_put(&nd.path);
+       if (pathname && kern_path(pathname, LOOKUP_FOLLOW, &path) == 0) {
+               char *buf = tomoyo_realpath_from_path(&path);
+               path_put(&path);
                return buf;
        }
        return NULL;
@@ -183,11 +195,11 @@ char *tomoyo_realpath(const char *pathname)
  */
 char *tomoyo_realpath_nofollow(const char *pathname)
 {
-       struct nameidata nd;
+       struct path path;
 
-       if (pathname && path_lookup(pathname, 0, &nd) == 0) {
-               char *buf = tomoyo_realpath_from_path(&nd.path);
-               path_put(&nd.path);
+       if (pathname && kern_path(pathname, 0, &path) == 0) {
+               char *buf = tomoyo_realpath_from_path(&path);
+               path_put(&path);
                return buf;
        }
        return NULL;
@@ -219,7 +231,6 @@ void *tomoyo_alloc_element(const unsigned int size)
                = roundup(size, max(sizeof(void *), sizeof(long)));
        if (word_aligned_size > PATH_MAX)
                return NULL;
-       /***** EXCLUSIVE SECTION START *****/
        mutex_lock(&lock);
        if (buf_used_len + word_aligned_size > PATH_MAX) {
                if (!tomoyo_quota_for_elements ||
@@ -250,7 +261,6 @@ void *tomoyo_alloc_element(const unsigned int size)
                }
        }
        mutex_unlock(&lock);
-       /***** EXCLUSIVE SECTION END *****/
        return ptr;
 }
 
@@ -264,9 +274,19 @@ static unsigned int tomoyo_quota_for_savename;
  * table. Frequency of appending strings is very low. So we don't need
  * large (e.g. 64k) hash size. 256 will be sufficient.
  */
-#define TOMOYO_MAX_HASH 256
+#define TOMOYO_HASH_BITS  8
+#define TOMOYO_MAX_HASH (1u<<TOMOYO_HASH_BITS)
 
-/* Structure for string data. */
+/*
+ * tomoyo_name_entry is a structure which is used for linking
+ * "struct tomoyo_path_info" into tomoyo_name_list .
+ *
+ * Since tomoyo_name_list manages a list of strings which are shared by
+ * multiple processes (whereas "struct tomoyo_path_info" inside
+ * "struct tomoyo_path_info_with_data" is not shared), a reference counter will
+ * be added to "struct tomoyo_name_entry" rather than "struct tomoyo_path_info"
+ * when TOMOYO starts supporting garbage collector.
+ */
 struct tomoyo_name_entry {
        struct list_head list;
        struct tomoyo_path_info entry;
@@ -280,10 +300,10 @@ struct tomoyo_free_memory_block_list {
 };
 
 /*
- * The list for "struct tomoyo_name_entry".
- *
- * This list is updated only inside tomoyo_save_name(), thus
- * no global mutex exists.
+ * tomoyo_name_list is used for holding string data used by TOMOYO.
+ * Since same string data is likely used for multiple times (e.g.
+ * "/lib/libc-2.5.so"), TOMOYO shares string data in the form of
+ * "const struct tomoyo_path_info *".
  */
 static struct list_head tomoyo_name_list[TOMOYO_MAX_HASH];
 
@@ -307,6 +327,7 @@ const struct tomoyo_path_info *tomoyo_save_name(const char *name)
        struct tomoyo_free_memory_block_list *fmb;
        int len;
        char *cp;
+       struct list_head *head;
 
        if (!name)
                return NULL;
@@ -317,10 +338,10 @@ const struct tomoyo_path_info *tomoyo_save_name(const char *name)
                return NULL;
        }
        hash = full_name_hash((const unsigned char *) name, len - 1);
-       /***** EXCLUSIVE SECTION START *****/
+       head = &tomoyo_name_list[hash_long(hash, TOMOYO_HASH_BITS)];
+
        mutex_lock(&lock);
-       list_for_each_entry(ptr, &tomoyo_name_list[hash % TOMOYO_MAX_HASH],
-                            list) {
+       list_for_each_entry(ptr, head, list) {
                if (hash == ptr->entry.hash && !strcmp(name, ptr->entry.name))
                        goto out;
        }
@@ -358,14 +379,13 @@ const struct tomoyo_path_info *tomoyo_save_name(const char *name)
        tomoyo_fill_path_info(&ptr->entry);
        fmb->ptr += len;
        fmb->len -= len;
-       list_add_tail(&ptr->list, &tomoyo_name_list[hash % TOMOYO_MAX_HASH]);
+       list_add_tail(&ptr->list, head);
        if (fmb->len == 0) {
                list_del(&fmb->list);
                kfree(fmb);
        }
  out:
        mutex_unlock(&lock);
-       /***** EXCLUSIVE SECTION END *****/
        return ptr ? &ptr->entry : NULL;
 }