Add mount option to check uid of device being mounted = expect uid, CVE-2011-1833
[linux-flexiantxendom0-natty.git] / fs / ecryptfs / main.c
index 7638b0a..adc54c2 100644 (file)
@@ -35,6 +35,8 @@
 #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"
 
 /**
@@ -129,11 +131,10 @@ int ecryptfs_init_persistent_file(struct dentry *ecryptfs_dentry)
                lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry);
                rc = ecryptfs_privileged_open(&inode_info->lower_file,
                                              lower_dentry, lower_mnt, cred);
-               if (rc || IS_ERR(inode_info->lower_file)) {
+               if (rc) {
                        printk(KERN_ERR "Error opening lower persistent file "
                               "for lower_dentry [0x%p] and lower_mnt [0x%p]; "
                               "rc = [%d]\n", lower_dentry, lower_mnt, rc);
-                       rc = PTR_ERR(inode_info->lower_file);
                        inode_info->lower_file = NULL;
                }
        }
@@ -141,25 +142,12 @@ 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
- * @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)
+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;
@@ -189,17 +177,38 @@ 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;
-       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);
+       return inode;
+out:
+       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);
-out:
-       return rc;
+       return 0;
 }
 
 enum { ecryptfs_opt_sig, ecryptfs_opt_ecryptfs_sig,
@@ -208,6 +217,8 @@ enum { ecryptfs_opt_sig, ecryptfs_opt_ecryptfs_sig,
        ecryptfs_opt_passthrough, ecryptfs_opt_xattr_metadata,
        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 const match_table_t tokens = {
@@ -222,6 +233,9 @@ static const match_table_t tokens = {
        {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}
 };
 
@@ -265,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
@@ -280,7 +295,8 @@ 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;
@@ -292,7 +308,7 @@ static int ecryptfs_parse_options(struct super_block *sb, char *options)
        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;
@@ -305,6 +321,8 @@ static int ecryptfs_parse_options(struct super_block *sb, char *options)
        char *cipher_key_bytes_src;
        char *fn_cipher_key_bytes_src;
 
+       *check_ruid = 0;
+
        if (!options) {
                rc = -EINVAL;
                goto out;
@@ -402,6 +420,16 @@ static int ecryptfs_parse_options(struct super_block *sb, char *options)
                                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:
                        printk(KERN_WARNING
@@ -479,142 +507,121 @@ out:
 }
 
 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)
-{
-       struct path path;
-       int rc;
+       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;
 
+       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;
        }
-       ecryptfs_set_superblock_lower(sb, path.dentry->d_sb);
-       sb->s_maxbytes = path.dentry->d_sb->s_maxbytes;
-       sb->s_blocksize = path.dentry->d_sb->s_blocksize;
-       ecryptfs_set_dentry_lower(sb->s_root, path.dentry);
-       ecryptfs_set_dentry_lower_mnt(sb->s_root, path.mnt);
-       rc = ecryptfs_interpose(path.dentry, 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(&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);
 }
 
 /**
@@ -622,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
 };
@@ -817,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();