Add mount option to check uid of device being mounted = expect uid, CVE-2011-1833
[linux-flexiantxendom0-natty.git] / fs / ecryptfs / main.c
index d25ac95..adc54c2 100644 (file)
 #include <linux/namei.h>
 #include <linux/skbuff.h>
 #include <linux/crypto.h>
-#include <linux/netlink.h>
 #include <linux/mount.h>
 #include <linux/pagemap.h>
 #include <linux/key.h>
 #include <linux/parser.h>
 #include <linux/fs_stack.h>
+#include <linux/slab.h>
+#include <linux/magic.h>
 #include "ecryptfs_kernel.h"
 
 /**
@@ -49,8 +50,7 @@ MODULE_PARM_DESC(ecryptfs_verbosity,
                 "0, which is Quiet)");
 
 /**
- * Module parameter that defines the number of netlink message buffer
- * elements
+ * Module parameter that defines the number of message buffer elements
  */
 unsigned int ecryptfs_message_buf_len = ECRYPTFS_DEFAULT_MSG_CTX_ELEMS;
 
@@ -60,9 +60,9 @@ MODULE_PARM_DESC(ecryptfs_message_buf_len,
 
 /**
  * Module parameter that defines the maximum guaranteed amount of time to wait
- * for a response through netlink.  The actual sleep time will be, more than
+ * for a response from ecryptfsd.  The actual sleep time will be, more than
  * likely, a small amount greater than this specified value, but only less if
- * the netlink message successfully arrives.
+ * the message successfully arrives.
  */
 signed long ecryptfs_message_wait_timeout = ECRYPTFS_MAX_MSG_CTX_TTL / HZ;
 
@@ -83,8 +83,6 @@ module_param(ecryptfs_number_of_users, uint, 0);
 MODULE_PARM_DESC(ecryptfs_number_of_users, "An estimate of the number of "
                 "concurrent users of eCryptfs");
 
-unsigned int ecryptfs_transport = ECRYPTFS_DEFAULT_TRANSPORT;
-
 void __ecryptfs_printk(const char *fmt, ...)
 {
        va_list args;
@@ -117,8 +115,9 @@ void __ecryptfs_printk(const char *fmt, ...)
  *
  * Returns zero on success; non-zero otherwise
  */
-static int ecryptfs_init_persistent_file(struct dentry *ecryptfs_dentry)
+int ecryptfs_init_persistent_file(struct dentry *ecryptfs_dentry)
 {
+       const struct cred *cred = current_cred();
        struct ecryptfs_inode_info *inode_info =
                ecryptfs_inode_to_private(ecryptfs_dentry->d_inode);
        int rc = 0;
@@ -130,27 +129,12 @@ static int ecryptfs_init_persistent_file(struct dentry *ecryptfs_dentry)
                        ecryptfs_dentry_to_lower_mnt(ecryptfs_dentry);
 
                lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry);
-               /* Corresponding dput() and mntput() are done when the
-                * persistent file is fput() when the eCryptfs inode
-                * is destroyed. */
-               dget(lower_dentry);
-               mntget(lower_mnt);
-               inode_info->lower_file = dentry_open(lower_dentry,
-                                                    lower_mnt,
-                                                    (O_RDWR | O_LARGEFILE));
-               if (IS_ERR(inode_info->lower_file)) {
-                       dget(lower_dentry);
-                       mntget(lower_mnt);
-                       inode_info->lower_file = dentry_open(lower_dentry,
-                                                            lower_mnt,
-                                                            (O_RDONLY
-                                                             | O_LARGEFILE));
-               }
-               if (IS_ERR(inode_info->lower_file)) {
+               rc = ecryptfs_privileged_open(&inode_info->lower_file,
+                                             lower_dentry, lower_mnt, cred);
+               if (rc) {
                        printk(KERN_ERR "Error opening lower persistent file "
-                              "for lower_dentry [0x%p] and lower_mnt [0x%p]\n",
-                              lower_dentry, lower_mnt);
-                       rc = PTR_ERR(inode_info->lower_file);
+                              "for lower_dentry [0x%p] and lower_mnt [0x%p]; "
+                              "rc = [%d]\n", lower_dentry, lower_mnt, rc);
                        inode_info->lower_file = NULL;
                }
        }
@@ -158,25 +142,12 @@ static int ecryptfs_init_persistent_file(struct dentry *ecryptfs_dentry)
        return rc;
 }
 
-/**
- * ecryptfs_interpose
- * @lower_dentry: Existing dentry in the lower filesystem
- * @dentry: ecryptfs' dentry
- * @sb: ecryptfs's super_block
- * @flag: If set to true, then d_add is called, else d_instantiate is called
- *
- * Interposes upper and lower dentries.
- *
- * Returns zero on success; non-zero otherwise
- */
-int ecryptfs_interpose(struct dentry *lower_dentry, struct dentry *dentry,
-                      struct super_block *sb, int flag)
+static struct inode *ecryptfs_get_inode(struct inode *lower_inode,
+                      struct super_block *sb)
 {
-       struct inode *lower_inode;
        struct inode *inode;
        int rc = 0;
 
-       lower_inode = lower_dentry->d_inode;
        if (lower_inode->i_sb != ecryptfs_superblock_to_lower(sb)) {
                rc = -EXDEV;
                goto out;
@@ -206,33 +177,51 @@ int ecryptfs_interpose(struct dentry *lower_dentry, struct dentry *dentry,
        if (special_file(lower_inode->i_mode))
                init_special_inode(inode, lower_inode->i_mode,
                                   lower_inode->i_rdev);
-       dentry->d_op = &ecryptfs_dops;
-       if (flag)
-               d_add(dentry, inode);
-       else
-               d_instantiate(dentry, inode);
-       fsstack_copy_attr_all(inode, lower_inode, NULL);
+       fsstack_copy_attr_all(inode, lower_inode);
        /* This size will be overwritten for real files w/ headers and
         * other metadata */
        fsstack_copy_inode_size(inode, lower_inode);
-       rc = ecryptfs_init_persistent_file(dentry);
-       if (rc) {
-               printk(KERN_ERR "%s: Error attempting to initialize the "
-                      "persistent file for the dentry with name [%s]; "
-                      "rc = [%d]\n", __FUNCTION__, dentry->d_name.name, rc);
-               goto out;
-       }
+       return inode;
 out:
-       return rc;
+       return ERR_PTR(rc);
+}
+
+/**
+ * ecryptfs_interpose
+ * @lower_dentry: Existing dentry in the lower filesystem
+ * @dentry: ecryptfs' dentry
+ * @sb: ecryptfs's super_block
+ * @flags: flags to govern behavior of interpose procedure
+ *
+ * Interposes upper and lower dentries.
+ *
+ * Returns zero on success; non-zero otherwise
+ */
+int ecryptfs_interpose(struct dentry *lower_dentry, struct dentry *dentry,
+                      struct super_block *sb, u32 flags)
+{
+       struct inode *lower_inode = lower_dentry->d_inode;
+       struct inode *inode = ecryptfs_get_inode(lower_inode, sb);
+       if (IS_ERR(inode))
+               return PTR_ERR(inode);
+       if (flags & ECRYPTFS_INTERPOSE_FLAG_D_ADD)
+               d_add(dentry, inode);
+       else
+               d_instantiate(dentry, inode);
+       return 0;
 }
 
 enum { ecryptfs_opt_sig, ecryptfs_opt_ecryptfs_sig,
        ecryptfs_opt_cipher, ecryptfs_opt_ecryptfs_cipher,
        ecryptfs_opt_ecryptfs_key_bytes,
        ecryptfs_opt_passthrough, ecryptfs_opt_xattr_metadata,
-       ecryptfs_opt_encrypted_view, ecryptfs_opt_err };
+       ecryptfs_opt_encrypted_view, ecryptfs_opt_fnek_sig,
+       ecryptfs_opt_fn_cipher, ecryptfs_opt_fn_cipher_key_bytes,
+       ecryptfs_opt_unlink_sigs, ecryptfs_opt_mount_auth_tok_only,
+       ecryptfs_opt_check_dev_ruid,
+       ecryptfs_opt_err };
 
-static match_table_t tokens = {
+static const match_table_t tokens = {
        {ecryptfs_opt_sig, "sig=%s"},
        {ecryptfs_opt_ecryptfs_sig, "ecryptfs_sig=%s"},
        {ecryptfs_opt_cipher, "cipher=%s"},
@@ -241,6 +230,12 @@ static match_table_t tokens = {
        {ecryptfs_opt_passthrough, "ecryptfs_passthrough"},
        {ecryptfs_opt_xattr_metadata, "ecryptfs_xattr_metadata"},
        {ecryptfs_opt_encrypted_view, "ecryptfs_encrypted_view"},
+       {ecryptfs_opt_fnek_sig, "ecryptfs_fnek_sig=%s"},
+       {ecryptfs_opt_fn_cipher, "ecryptfs_fn_cipher=%s"},
+       {ecryptfs_opt_fn_cipher_key_bytes, "ecryptfs_fn_key_bytes=%u"},
+       {ecryptfs_opt_unlink_sigs, "ecryptfs_unlink_sigs"},
+       {ecryptfs_opt_mount_auth_tok_only, "ecryptfs_mount_auth_tok_only"},
+       {ecryptfs_opt_check_dev_ruid, "ecryptfs_check_dev_ruid"},
        {ecryptfs_opt_err, NULL}
 };
 
@@ -262,10 +257,11 @@ static int ecryptfs_init_global_auth_toks(
                               "session keyring for sig specified in mount "
                               "option: [%s]\n", global_auth_tok->sig);
                        global_auth_tok->flags |= ECRYPTFS_AUTH_TOK_INVALID;
-                       rc = 0;
+                       goto out;
                } else
                        global_auth_tok->flags &= ~ECRYPTFS_AUTH_TOK_INVALID;
        }
+out:
        return rc;
 }
 
@@ -283,6 +279,7 @@ static void ecryptfs_init_mount_crypt_stat(
  * ecryptfs_parse_options
  * @sb: The ecryptfs super block
  * @options: The options pased to the kernel
+ * @check_ruid: set to 1 if device uid should be checked against the ruid
  *
  * Parse mount options:
  * debug=N        - ecryptfs_verbosity level for debug output
@@ -298,23 +295,33 @@ static void ecryptfs_init_mount_crypt_stat(
  *
  * Returns zero on success; non-zero on error
  */
-static int ecryptfs_parse_options(struct super_block *sb, char *options)
+static int ecryptfs_parse_options(struct ecryptfs_sb_info *sbi, char *options,
+                                 uid_t *check_ruid)
 {
        char *p;
        int rc = 0;
        int sig_set = 0;
        int cipher_name_set = 0;
+       int fn_cipher_name_set = 0;
        int cipher_key_bytes;
        int cipher_key_bytes_set = 0;
+       int fn_cipher_key_bytes;
+       int fn_cipher_key_bytes_set = 0;
        struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
-               &ecryptfs_superblock_to_private(sb)->mount_crypt_stat;
+               &sbi->mount_crypt_stat;
        substring_t args[MAX_OPT_ARGS];
        int token;
        char *sig_src;
        char *cipher_name_dst;
        char *cipher_name_src;
+       char *fn_cipher_name_dst;
+       char *fn_cipher_name_src;
+       char *fnek_dst;
+       char *fnek_src;
        char *cipher_key_bytes_src;
-       int cipher_name_len;
+       char *fn_cipher_key_bytes_src;
+
+       *check_ruid = 0;
 
        if (!options) {
                rc = -EINVAL;
@@ -330,7 +337,7 @@ static int ecryptfs_parse_options(struct super_block *sb, char *options)
                case ecryptfs_opt_ecryptfs_sig:
                        sig_src = args[0].from;
                        rc = ecryptfs_add_global_auth_tok(mount_crypt_stat,
-                                                         sig_src);
+                                                         sig_src, 0);
                        if (rc) {
                                printk(KERN_ERR "Error attempting to register "
                                       "global sig; rc = [%d]\n", rc);
@@ -346,10 +353,7 @@ static int ecryptfs_parse_options(struct super_block *sb, char *options)
                                global_default_cipher_name;
                        strncpy(cipher_name_dst, cipher_name_src,
                                ECRYPTFS_MAX_CIPHER_NAME_SIZE);
-                       ecryptfs_printk(KERN_DEBUG,
-                                       "The mount_crypt_stat "
-                                       "global_default_cipher_name set to: "
-                                       "[%s]\n", cipher_name_dst);
+                       cipher_name_dst[ECRYPTFS_MAX_CIPHER_NAME_SIZE] = '\0';
                        cipher_name_set = 1;
                        break;
                case ecryptfs_opt_ecryptfs_key_bytes:
@@ -359,11 +363,6 @@ static int ecryptfs_parse_options(struct super_block *sb, char *options)
                                                   &cipher_key_bytes_src, 0);
                        mount_crypt_stat->global_default_cipher_key_size =
                                cipher_key_bytes;
-                       ecryptfs_printk(KERN_DEBUG,
-                                       "The mount_crypt_stat "
-                                       "global_default_cipher_key_size "
-                                       "set to: [%d]\n", mount_crypt_stat->
-                                       global_default_cipher_key_size);
                        cipher_key_bytes_set = 1;
                        break;
                case ecryptfs_opt_passthrough:
@@ -380,11 +379,62 @@ static int ecryptfs_parse_options(struct super_block *sb, char *options)
                        mount_crypt_stat->flags |=
                                ECRYPTFS_ENCRYPTED_VIEW_ENABLED;
                        break;
+               case ecryptfs_opt_fnek_sig:
+                       fnek_src = args[0].from;
+                       fnek_dst =
+                               mount_crypt_stat->global_default_fnek_sig;
+                       strncpy(fnek_dst, fnek_src, ECRYPTFS_SIG_SIZE_HEX);
+                       mount_crypt_stat->global_default_fnek_sig[
+                               ECRYPTFS_SIG_SIZE_HEX] = '\0';
+                       rc = ecryptfs_add_global_auth_tok(
+                               mount_crypt_stat,
+                               mount_crypt_stat->global_default_fnek_sig,
+                               ECRYPTFS_AUTH_TOK_FNEK);
+                       if (rc) {
+                               printk(KERN_ERR "Error attempting to register "
+                                      "global fnek sig [%s]; rc = [%d]\n",
+                                      mount_crypt_stat->global_default_fnek_sig,
+                                      rc);
+                               goto out;
+                       }
+                       mount_crypt_stat->flags |=
+                               (ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES
+                                | ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK);
+                       break;
+               case ecryptfs_opt_fn_cipher:
+                       fn_cipher_name_src = args[0].from;
+                       fn_cipher_name_dst =
+                               mount_crypt_stat->global_default_fn_cipher_name;
+                       strncpy(fn_cipher_name_dst, fn_cipher_name_src,
+                               ECRYPTFS_MAX_CIPHER_NAME_SIZE);
+                       mount_crypt_stat->global_default_fn_cipher_name[
+                               ECRYPTFS_MAX_CIPHER_NAME_SIZE] = '\0';
+                       fn_cipher_name_set = 1;
+                       break;
+               case ecryptfs_opt_fn_cipher_key_bytes:
+                       fn_cipher_key_bytes_src = args[0].from;
+                       fn_cipher_key_bytes =
+                               (int)simple_strtol(fn_cipher_key_bytes_src,
+                                                  &fn_cipher_key_bytes_src, 0);
+                       mount_crypt_stat->global_default_fn_cipher_key_bytes =
+                               fn_cipher_key_bytes;
+                       fn_cipher_key_bytes_set = 1;
+                       break;
+               case ecryptfs_opt_unlink_sigs:
+                       mount_crypt_stat->flags |= ECRYPTFS_UNLINK_SIGS;
+                       break;
+               case ecryptfs_opt_mount_auth_tok_only:
+                       mount_crypt_stat->flags |=
+                               ECRYPTFS_GLOBAL_MOUNT_AUTH_TOK_ONLY;
+                       break;
+               case ecryptfs_opt_check_dev_ruid:
+                       *check_ruid = 1;
+                       break;
                case ecryptfs_opt_err:
                default:
-                       ecryptfs_printk(KERN_WARNING,
-                                       "eCryptfs: unrecognized option '%s'\n",
-                                       p);
+                       printk(KERN_WARNING
+                              "%s: eCryptfs: unrecognized option [%s]\n",
+                              __func__, p);
                }
        }
        if (!sig_set) {
@@ -395,188 +445,183 @@ static int ecryptfs_parse_options(struct super_block *sb, char *options)
                goto out;
        }
        if (!cipher_name_set) {
-               cipher_name_len = strlen(ECRYPTFS_DEFAULT_CIPHER);
-               if (unlikely(cipher_name_len
-                            >= ECRYPTFS_MAX_CIPHER_NAME_SIZE)) {
-                       rc = -EINVAL;
-                       BUG();
-                       goto out;
-               }
-               memcpy(mount_crypt_stat->global_default_cipher_name,
-                      ECRYPTFS_DEFAULT_CIPHER, cipher_name_len);
-               mount_crypt_stat->global_default_cipher_name[cipher_name_len]
-                   = '\0';
+               int cipher_name_len = strlen(ECRYPTFS_DEFAULT_CIPHER);
+
+               BUG_ON(cipher_name_len >= ECRYPTFS_MAX_CIPHER_NAME_SIZE);
+               strcpy(mount_crypt_stat->global_default_cipher_name,
+                      ECRYPTFS_DEFAULT_CIPHER);
        }
-       if (!cipher_key_bytes_set) {
+       if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
+           && !fn_cipher_name_set)
+               strcpy(mount_crypt_stat->global_default_fn_cipher_name,
+                      mount_crypt_stat->global_default_cipher_name);
+       if (!cipher_key_bytes_set)
                mount_crypt_stat->global_default_cipher_key_size = 0;
-       }
+       if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
+           && !fn_cipher_key_bytes_set)
+               mount_crypt_stat->global_default_fn_cipher_key_bytes =
+                       mount_crypt_stat->global_default_cipher_key_size;
        mutex_lock(&key_tfm_list_mutex);
        if (!ecryptfs_tfm_exists(mount_crypt_stat->global_default_cipher_name,
-                                NULL))
+                                NULL)) {
                rc = ecryptfs_add_new_key_tfm(
                        NULL, mount_crypt_stat->global_default_cipher_name,
                        mount_crypt_stat->global_default_cipher_key_size);
-       mutex_unlock(&key_tfm_list_mutex);
-       if (rc) {
-               printk(KERN_ERR "Error attempting to initialize cipher with "
-                      "name = [%s] and key size = [%td]; rc = [%d]\n",
-                      mount_crypt_stat->global_default_cipher_name,
-                      mount_crypt_stat->global_default_cipher_key_size, rc);
-               rc = -EINVAL;
-               goto out;
+               if (rc) {
+                       printk(KERN_ERR "Error attempting to initialize "
+                              "cipher with name = [%s] and key size = [%td]; "
+                              "rc = [%d]\n",
+                              mount_crypt_stat->global_default_cipher_name,
+                              mount_crypt_stat->global_default_cipher_key_size,
+                              rc);
+                       rc = -EINVAL;
+                       mutex_unlock(&key_tfm_list_mutex);
+                       goto out;
+               }
        }
+       if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
+           && !ecryptfs_tfm_exists(
+                   mount_crypt_stat->global_default_fn_cipher_name, NULL)) {
+               rc = ecryptfs_add_new_key_tfm(
+                       NULL, mount_crypt_stat->global_default_fn_cipher_name,
+                       mount_crypt_stat->global_default_fn_cipher_key_bytes);
+               if (rc) {
+                       printk(KERN_ERR "Error attempting to initialize "
+                              "cipher with name = [%s] and key size = [%td]; "
+                              "rc = [%d]\n",
+                              mount_crypt_stat->global_default_fn_cipher_name,
+                              mount_crypt_stat->global_default_fn_cipher_key_bytes,
+                              rc);
+                       rc = -EINVAL;
+                       mutex_unlock(&key_tfm_list_mutex);
+                       goto out;
+               }
+       }
+       mutex_unlock(&key_tfm_list_mutex);
        rc = ecryptfs_init_global_auth_toks(mount_crypt_stat);
-       if (rc) {
+       if (rc)
                printk(KERN_WARNING "One or more global auth toks could not "
                       "properly register; rc = [%d]\n", rc);
-       }
-       rc = 0;
 out:
        return rc;
 }
 
 struct kmem_cache *ecryptfs_sb_info_cache;
+static struct file_system_type ecryptfs_fs_type;
 
 /**
- * ecryptfs_fill_super
- * @sb: The ecryptfs super block
- * @raw_data: The options passed to mount
- * @silent: Not used but required by function prototype
- *
- * Sets up what we can of the sb, rest is done in ecryptfs_read_super
- *
- * Returns zero on success; non-zero otherwise
+ * ecryptfs_get_sb
+ * @fs_type
+ * @flags
+ * @dev_name: The path to mount over
+ * @raw_data: The options passed into the kernel
  */
-static int
-ecryptfs_fill_super(struct super_block *sb, void *raw_data, int silent)
+static struct dentry *ecryptfs_mount(struct file_system_type *fs_type, int flags,
+                       const char *dev_name, void *raw_data)
 {
-       int rc = 0;
+       struct super_block *s;
+       struct ecryptfs_sb_info *sbi;
+       struct ecryptfs_dentry_info *root_info;
+       const char *err = "Getting sb failed";
+       struct inode *inode;
+       struct path path;
+       uid_t check_ruid;
+       int rc;
 
-       /* Released in ecryptfs_put_super() */
-       ecryptfs_set_superblock_private(sb,
-                                       kmem_cache_zalloc(ecryptfs_sb_info_cache,
-                                                        GFP_KERNEL));
-       if (!ecryptfs_superblock_to_private(sb)) {
-               ecryptfs_printk(KERN_WARNING, "Out of memory\n");
+       sbi = kmem_cache_zalloc(ecryptfs_sb_info_cache, GFP_KERNEL);
+       if (!sbi) {
                rc = -ENOMEM;
                goto out;
        }
-       sb->s_op = &ecryptfs_sops;
-       /* Released through deactivate_super(sb) from get_sb_nodev */
-       sb->s_root = d_alloc(NULL, &(const struct qstr) {
-                            .hash = 0,.name = "/",.len = 1});
-       if (!sb->s_root) {
-               ecryptfs_printk(KERN_ERR, "d_alloc failed\n");
-               rc = -ENOMEM;
+
+       rc = ecryptfs_parse_options(sbi, raw_data, &check_ruid);
+       if (rc) {
+               err = "Error parsing options";
                goto out;
        }
-       sb->s_root->d_op = &ecryptfs_dops;
-       sb->s_root->d_sb = sb;
-       sb->s_root->d_parent = sb->s_root;
-       /* Released in d_release when dput(sb->s_root) is called */
-       /* through deactivate_super(sb) from get_sb_nodev() */
-       ecryptfs_set_dentry_private(sb->s_root,
-                                   kmem_cache_zalloc(ecryptfs_dentry_info_cache,
-                                                    GFP_KERNEL));
-       if (!ecryptfs_dentry_to_private(sb->s_root)) {
-               ecryptfs_printk(KERN_ERR,
-                               "dentry_info_cache alloc failed\n");
-               rc = -ENOMEM;
+
+       s = sget(fs_type, NULL, set_anon_super, NULL);
+       if (IS_ERR(s)) {
+               rc = PTR_ERR(s);
                goto out;
        }
-       rc = 0;
-out:
-       /* Should be able to rely on deactivate_super called from
-        * get_sb_nodev */
-       return rc;
-}
 
-/**
- * ecryptfs_read_super
- * @sb: The ecryptfs super block
- * @dev_name: The path to mount over
- *
- * Read the super block of the lower filesystem, and use
- * ecryptfs_interpose to create our initial inode and super block
- * struct.
- */
-static int ecryptfs_read_super(struct super_block *sb, const char *dev_name)
-{
-       int rc;
-       struct nameidata nd;
-       struct dentry *lower_root;
-       struct vfsmount *lower_mnt;
+       s->s_flags = flags;
+       rc = bdi_setup_and_register(&sbi->bdi, "ecryptfs", BDI_CAP_MAP_COPY);
+       if (rc)
+               goto out1;
+
+       ecryptfs_set_superblock_private(s, sbi);
+       s->s_bdi = &sbi->bdi;
+
+       /* ->kill_sb() will take care of sbi after that point */
+       sbi = NULL;
+       s->s_op = &ecryptfs_sops;
+       s->s_d_op = &ecryptfs_dops;
 
-       memset(&nd, 0, sizeof(struct nameidata));
-       rc = path_lookup(dev_name, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &nd);
+       err = "Reading sb failed";
+       rc = kern_path(dev_name, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
        if (rc) {
-               ecryptfs_printk(KERN_WARNING, "path_lookup() failed\n");
-               goto out;
+               ecryptfs_printk(KERN_WARNING, "kern_path() failed\n");
+               goto out1;
        }
-       lower_root = nd.path.dentry;
-       lower_mnt = nd.path.mnt;
-       ecryptfs_set_superblock_lower(sb, lower_root->d_sb);
-       sb->s_maxbytes = lower_root->d_sb->s_maxbytes;
-       sb->s_blocksize = lower_root->d_sb->s_blocksize;
-       ecryptfs_set_dentry_lower(sb->s_root, lower_root);
-       ecryptfs_set_dentry_lower_mnt(sb->s_root, lower_mnt);
-       rc = ecryptfs_interpose(lower_root, sb->s_root, sb, 0);
-       if (rc)
+       if (path.dentry->d_sb->s_type == &ecryptfs_fs_type) {
+               rc = -EINVAL;
+               printk(KERN_ERR "Mount on filesystem of type "
+                       "eCryptfs explicitly disallowed due to "
+                       "known incompatibilities\n");
                goto out_free;
-       rc = 0;
-       goto out;
-out_free:
-       path_put(&nd.path);
-out:
-       return rc;
-}
-
-/**
- * ecryptfs_get_sb
- * @fs_type
- * @flags
- * @dev_name: The path to mount over
- * @raw_data: The options passed into the kernel
- *
- * The whole ecryptfs_get_sb process is broken into 4 functions:
- * ecryptfs_parse_options(): handle options passed to ecryptfs, if any
- * ecryptfs_fill_super(): used by get_sb_nodev, fills out the super_block
- *                        with as much information as it can before needing
- *                        the lower filesystem.
- * ecryptfs_read_super(): this accesses the lower filesystem and uses
- *                        ecryptfs_interpolate to perform most of the linking
- * ecryptfs_interpolate(): links the lower filesystem into ecryptfs
- */
-static int ecryptfs_get_sb(struct file_system_type *fs_type, int flags,
-                       const char *dev_name, void *raw_data,
-                       struct vfsmount *mnt)
-{
-       int rc;
-       struct super_block *sb;
-
-       rc = get_sb_nodev(fs_type, flags, raw_data, ecryptfs_fill_super, mnt);
-       if (rc < 0) {
-               printk(KERN_ERR "Getting sb failed; rc = [%d]\n", rc);
-               goto out;
        }
-       sb = mnt->mnt_sb;
-       rc = ecryptfs_parse_options(sb, raw_data);
-       if (rc) {
-               printk(KERN_ERR "Error parsing options; rc = [%d]\n", rc);
-               goto out_abort;
+
+       if (check_ruid && path.dentry->d_inode->i_uid != current_uid()) {
+               rc = -EPERM;
+               printk(KERN_ERR "Mount of device (uid: %d) not owned by "
+                      "requested user (uid: %d)\n",
+                      path.dentry->d_inode->i_uid, current_uid());
+               goto out_free;
        }
-       rc = ecryptfs_read_super(sb, dev_name);
-       if (rc) {
-               printk(KERN_ERR "Reading sb failed; rc = [%d]\n", rc);
-               goto out_abort;
+
+       ecryptfs_set_superblock_lower(s, path.dentry->d_sb);
+       s->s_maxbytes = path.dentry->d_sb->s_maxbytes;
+       s->s_blocksize = path.dentry->d_sb->s_blocksize;
+       s->s_magic = ECRYPTFS_SUPER_MAGIC;
+
+       inode = ecryptfs_get_inode(path.dentry->d_inode, s);
+       rc = PTR_ERR(inode);
+       if (IS_ERR(inode))
+               goto out_free;
+
+       s->s_root = d_alloc_root(inode);
+       if (!s->s_root) {
+               iput(inode);
+               rc = -ENOMEM;
+               goto out_free;
        }
-       goto out;
-out_abort:
-       dput(sb->s_root);
-       up_write(&sb->s_umount);
-       deactivate_super(sb);
+
+       rc = -ENOMEM;
+       root_info = kmem_cache_zalloc(ecryptfs_dentry_info_cache, GFP_KERNEL);
+       if (!root_info)
+               goto out_free;
+
+       /* ->kill_sb() will take care of root_info */
+       ecryptfs_set_dentry_private(s->s_root, root_info);
+       ecryptfs_set_dentry_lower(s->s_root, path.dentry);
+       ecryptfs_set_dentry_lower_mnt(s->s_root, path.mnt);
+
+       s->s_flags |= MS_ACTIVE;
+       return dget(s->s_root);
+
+out_free:
+       path_put(&path);
+out1:
+       deactivate_locked_super(s);
 out:
-       return rc;
+       if (sbi) {
+               ecryptfs_destroy_mount_crypt_stat(&sbi->mount_crypt_stat);
+               kmem_cache_free(ecryptfs_sb_info_cache, sbi);
+       }
+       printk(KERN_ERR "%s; rc = [%d]\n", err, rc);
+       return ERR_PTR(rc);
 }
 
 /**
@@ -584,17 +629,22 @@ out:
  * @sb: The ecryptfs super block
  *
  * Used to bring the superblock down and free the private data.
- * Private data is free'd in ecryptfs_put_super()
  */
 static void ecryptfs_kill_block_super(struct super_block *sb)
 {
-       generic_shutdown_super(sb);
+       struct ecryptfs_sb_info *sb_info = ecryptfs_superblock_to_private(sb);
+       kill_anon_super(sb);
+       if (!sb_info)
+               return;
+       ecryptfs_destroy_mount_crypt_stat(&sb_info->mount_crypt_stat);
+       bdi_destroy(&sb_info->bdi);
+       kmem_cache_free(ecryptfs_sb_info_cache, sb_info);
 }
 
 static struct file_system_type ecryptfs_fs_type = {
        .owner = THIS_MODULE,
        .name = "ecryptfs",
-       .get_sb = ecryptfs_get_sb,
+       .mount = ecryptfs_mount,
        .kill_sb = ecryptfs_kill_block_super,
        .fs_flags = 0
 };
@@ -605,7 +655,7 @@ static struct file_system_type ecryptfs_fs_type = {
  * Initializes the ecryptfs_inode_info_cache when it is created
  */
 static void
-inode_info_init_once(struct kmem_cache *cachep, void *vptr)
+inode_info_init_once(void *vptr)
 {
        struct ecryptfs_inode_info *ei = (struct ecryptfs_inode_info *)vptr;
 
@@ -616,7 +666,7 @@ static struct ecryptfs_cache_info {
        struct kmem_cache **cache;
        const char *name;
        size_t size;
-       void (*ctor)(struct kmem_cache *cache, void *obj);
+       void (*ctor)(void *obj);
 } ecryptfs_cache_infos[] = {
        {
                .cache = &ecryptfs_auth_tok_list_item_cache,
@@ -679,6 +729,11 @@ static struct ecryptfs_cache_info {
                .name = "ecryptfs_key_tfm_cache",
                .size = sizeof(struct ecryptfs_key_tfm),
        },
+       {
+               .cache = &ecryptfs_open_req_cache,
+               .name = "ecryptfs_open_req_cache",
+               .size = sizeof(struct ecryptfs_open_req),
+       },
 };
 
 static void ecryptfs_free_kmem_caches(void)
@@ -774,9 +829,10 @@ static int __init ecryptfs_init(void)
                ecryptfs_printk(KERN_ERR, "The eCryptfs extent size is "
                                "larger than the host's page size, and so "
                                "eCryptfs cannot run on this system. The "
-                               "default eCryptfs extent size is [%d] bytes; "
-                               "the page size is [%d] bytes.\n",
-                               ECRYPTFS_DEFAULT_EXTENT_SIZE, PAGE_CACHE_SIZE);
+                               "default eCryptfs extent size is [%u] bytes; "
+                               "the page size is [%lu] bytes.\n",
+                               ECRYPTFS_DEFAULT_EXTENT_SIZE,
+                               (unsigned long)PAGE_CACHE_SIZE);
                goto out;
        }
        rc = ecryptfs_init_kmem_caches();
@@ -795,12 +851,19 @@ static int __init ecryptfs_init(void)
                printk(KERN_ERR "sysfs registration failed\n");
                goto out_unregister_filesystem;
        }
-       rc = ecryptfs_init_messaging(ecryptfs_transport);
+       rc = ecryptfs_init_kthread();
        if (rc) {
-               ecryptfs_printk(KERN_ERR, "Failure occured while attempting to "
-                               "initialize the eCryptfs netlink socket\n");
+               printk(KERN_ERR "%s: kthread initialization failed; "
+                      "rc = [%d]\n", __func__, rc);
                goto out_do_sysfs_unregistration;
        }
+       rc = ecryptfs_init_messaging();
+       if (rc) {
+               printk(KERN_ERR "Failure occured while attempting to "
+                               "initialize the communications channel to "
+                               "ecryptfsd\n");
+               goto out_destroy_kthread;
+       }
        rc = ecryptfs_init_crypto();
        if (rc) {
                printk(KERN_ERR "Failure whilst attempting to init crypto; "
@@ -813,7 +876,9 @@ static int __init ecryptfs_init(void)
 
        goto out;
 out_release_messaging:
-       ecryptfs_release_messaging(ecryptfs_transport);
+       ecryptfs_release_messaging();
+out_destroy_kthread:
+       ecryptfs_destroy_kthread();
 out_do_sysfs_unregistration:
        do_sysfs_unregistration();
 out_unregister_filesystem:
@@ -832,7 +897,8 @@ static void __exit ecryptfs_exit(void)
        if (rc)
                printk(KERN_ERR "Failure whilst attempting to destroy crypto; "
                       "rc = [%d]\n", rc);
-       ecryptfs_release_messaging(ecryptfs_transport);
+       ecryptfs_release_messaging();
+       ecryptfs_destroy_kthread();
        do_sysfs_unregistration();
        unregister_filesystem(&ecryptfs_fs_type);
        ecryptfs_free_kmem_caches();