befs: Validate length of long symbolic links, CVE-2011-2928
[linux-flexiantxendom0-natty.git] / fs / befs / linuxvfs.c
index e0a6025..e8ff0d7 100644 (file)
@@ -22,7 +22,6 @@
 #include "datastream.h"
 #include "super.h"
 #include "io.h"
-#include "endian.h"
 
 MODULE_DESCRIPTION("BeOS File System (BeFS) driver");
 MODULE_AUTHOR("Will Dyson");
@@ -36,7 +35,7 @@ static int befs_get_block(struct inode *, sector_t, struct buffer_head *, int);
 static int befs_readpage(struct file *file, struct page *page);
 static sector_t befs_bmap(struct address_space *mapping, sector_t block);
 static struct dentry *befs_lookup(struct inode *, struct dentry *, struct nameidata *);
-static void befs_read_inode(struct inode *ino);
+static struct inode *befs_iget(struct super_block *, unsigned long);
 static struct inode *befs_alloc_inode(struct super_block *sb);
 static void befs_destroy_inode(struct inode *inode);
 static int befs_init_inodecache(void);
@@ -49,43 +48,38 @@ static int befs_nls2utf(struct super_block *sb, const char *in, int in_len,
                        char **out, int *out_len);
 static void befs_put_super(struct super_block *);
 static int befs_remount(struct super_block *, int *, char *);
-static int befs_statfs(struct super_block *, struct kstatfs *);
+static int befs_statfs(struct dentry *, struct kstatfs *);
 static int parse_options(char *, befs_mount_options *);
 
 static const struct super_operations befs_sops = {
-       .read_inode     = befs_read_inode,      /* initialize & read inode */
        .alloc_inode    = befs_alloc_inode,     /* allocate a new inode */
        .destroy_inode  = befs_destroy_inode, /* deallocate an inode */
        .put_super      = befs_put_super,       /* uninit super */
        .statfs         = befs_statfs,  /* statfs */
        .remount_fs     = befs_remount,
+       .show_options   = generic_show_options,
 };
 
 /* slab cache for befs_inode_info objects */
-static kmem_cache_t *befs_inode_cachep;
+static struct kmem_cache *befs_inode_cachep;
 
-static struct file_operations befs_dir_operations = {
+static const struct file_operations befs_dir_operations = {
        .read           = generic_read_dir,
        .readdir        = befs_readdir,
+       .llseek         = generic_file_llseek,
 };
 
-static struct inode_operations befs_dir_inode_operations = {
+static const struct inode_operations befs_dir_inode_operations = {
        .lookup         = befs_lookup,
 };
 
-static struct file_operations befs_file_operations = {
-       .llseek         = default_llseek,
-       .read           = generic_file_read,
-       .mmap           = generic_file_readonly_mmap,
-};
-
-static struct address_space_operations befs_aops = {
+static const struct address_space_operations befs_aops = {
        .readpage       = befs_readpage,
        .sync_page      = block_sync_page,
        .bmap           = befs_bmap,
 };
 
-static struct inode_operations befs_symlink_inode_operations = {
+static const struct inode_operations befs_symlink_inode_operations = {
        .readlink       = generic_readlink,
        .follow_link    = befs_follow_link,
        .put_link       = befs_put_link,
@@ -205,9 +199,9 @@ befs_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
                return ERR_PTR(-ENODATA);
        }
 
-       inode = iget(dir->i_sb, (ino_t) offset);
-       if (!inode)
-               return ERR_PTR(-EACCES);
+       inode = befs_iget(dir->i_sb, (ino_t) offset);
+       if (IS_ERR(inode))
+               return ERR_CAST(inode);
 
        d_add(dentry, inode);
 
@@ -219,7 +213,7 @@ befs_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
 static int
 befs_readdir(struct file *filp, void *dirent, filldir_t filldir)
 {
-       struct inode *inode = filp->f_dentry->d_inode;
+       struct inode *inode = filp->f_path.dentry->d_inode;
        struct super_block *sb = inode->i_sb;
        befs_data_stream *ds = &BEFS_I(inode)->i_data.ds;
        befs_off_t value;
@@ -229,7 +223,7 @@ befs_readdir(struct file *filp, void *dirent, filldir_t filldir)
        char keybuf[BEFS_NAME_LEN + 1];
        char *nlsname;
        int nlsnamelen;
-       const char *dirname = filp->f_dentry->d_name.name;
+       const char *dirname = filp->f_path.dentry->d_name.name;
 
        befs_debug(sb, "---> befs_readdir() "
                   "name %s, inode %ld, filp->f_pos %Ld",
@@ -284,39 +278,48 @@ befs_alloc_inode(struct super_block *sb)
 {
         struct befs_inode_info *bi;
         bi = (struct befs_inode_info *)kmem_cache_alloc(befs_inode_cachep,
-                                                       SLAB_KERNEL);
+                                                       GFP_KERNEL);
         if (!bi)
                 return NULL;
         return &bi->vfs_inode;
 }
 
-static void
-befs_destroy_inode(struct inode *inode)
+static void befs_i_callback(struct rcu_head *head)
 {
+       struct inode *inode = container_of(head, struct inode, i_rcu);
+       INIT_LIST_HEAD(&inode->i_dentry);
         kmem_cache_free(befs_inode_cachep, BEFS_I(inode));
 }
 
-static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
+static void befs_destroy_inode(struct inode *inode)
+{
+       call_rcu(&inode->i_rcu, befs_i_callback);
+}
+
+static void init_once(void *foo)
 {
         struct befs_inode_info *bi = (struct befs_inode_info *) foo;
-       
-               if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
-                           SLAB_CTOR_CONSTRUCTOR) {
-                       inode_init_once(&bi->vfs_inode);
-               }
+
+       inode_init_once(&bi->vfs_inode);
 }
 
-static void
-befs_read_inode(struct inode *inode)
+static struct inode *befs_iget(struct super_block *sb, unsigned long ino)
 {
        struct buffer_head *bh = NULL;
        befs_inode *raw_inode = NULL;
 
-       struct super_block *sb = inode->i_sb;
        befs_sb_info *befs_sb = BEFS_SB(sb);
        befs_inode_info *befs_ino = NULL;
+       struct inode *inode;
+       long ret = -EIO;
+
+       befs_debug(sb, "---> befs_read_inode() " "inode = %lu", ino);
 
-       befs_debug(sb, "---> befs_read_inode() " "inode = %lu", inode->i_ino);
+       inode = iget_locked(sb, ino);
+       if (IS_ERR(inode))
+               return inode;
+       if (!(inode->i_state & I_NEW))
+               return inode;
 
        befs_ino = BEFS_I(inode);
 
@@ -331,7 +334,7 @@ befs_read_inode(struct inode *inode)
        if (!bh) {
                befs_error(sb, "unable to read inode block - "
                           "inode = %lu", inode->i_ino);
-               goto unaquire_none;
+               goto unacquire_none;
        }
 
        raw_inode = (befs_inode *) bh->b_data;
@@ -340,7 +343,7 @@ befs_read_inode(struct inode *inode)
 
        if (befs_check_inode(sb, raw_inode, inode->i_ino) != BEFS_OK) {
                befs_error(sb, "Bad inode: %lu", inode->i_ino);
-               goto unaquire_bh;
+               goto unacquire_bh;
        }
 
        inode->i_mode = (umode_t) fs32_to_cpu(sb, raw_inode->mode);
@@ -371,7 +374,6 @@ befs_read_inode(struct inode *inode)
        inode->i_mtime.tv_nsec = 0;   /* lower 16 bits are not a time */        
        inode->i_ctime = inode->i_mtime;
        inode->i_atime = inode->i_mtime;
-       inode->i_blksize = befs_sb->block_size;
 
        befs_ino->i_inode_num = fsrun_to_cpu(sb, raw_inode->inode_num);
        befs_ino->i_parent = fsrun_to_cpu(sb, raw_inode->parent);
@@ -382,12 +384,13 @@ befs_read_inode(struct inode *inode)
                inode->i_size = 0;
                inode->i_blocks = befs_sb->block_size / VFS_BLOCK_SIZE;
                strncpy(befs_ino->i_data.symlink, raw_inode->data.symlink,
-                       BEFS_SYMLINK_LEN);
+                       BEFS_SYMLINK_LEN - 1);
+               befs_ino->i_data.symlink[BEFS_SYMLINK_LEN - 1] = '\0';
        } else {
                int num_blks;
 
                befs_ino->i_data.ds =
-                   fsds_to_cpu(sb, raw_inode->data.datastream);
+                   fsds_to_cpu(sb, &raw_inode->data.datastream);
 
                num_blks = befs_count_blocks(sb, &befs_ino->i_data.ds);
                inode->i_blocks =
@@ -398,7 +401,7 @@ befs_read_inode(struct inode *inode)
        inode->i_mapping->a_ops = &befs_aops;
 
        if (S_ISREG(inode->i_mode)) {
-               inode->i_fop = &befs_file_operations;
+               inode->i_fop = &generic_ro_fops;
        } else if (S_ISDIR(inode->i_mode)) {
                inode->i_op = &befs_dir_inode_operations;
                inode->i_fop = &befs_dir_operations;
@@ -408,24 +411,25 @@ befs_read_inode(struct inode *inode)
                befs_error(sb, "Inode %lu is not a regular file, "
                           "directory or symlink. THAT IS WRONG! BeFS has no "
                           "on disk special files", inode->i_ino);
-               goto unaquire_bh;
+               goto unacquire_bh;
        }
 
        brelse(bh);
        befs_debug(sb, "<--- befs_read_inode()");
-       return;
+       unlock_new_inode(inode);
+       return inode;
 
-      unaquire_bh:
+      unacquire_bh:
        brelse(bh);
 
-      unaquire_none:
-       make_bad_inode(inode);
+      unacquire_none:
+       iget_failed(inode);
        befs_debug(sb, "<--- befs_read_inode() - Bad inode");
-       return;
+       return ERR_PTR(ret);
 }
 
 /* Initialize the inode cache. Called at fs setup.
- * 
+ *
  * Taken from NFS implementation by Al Viro.
  */
 static int
@@ -433,11 +437,12 @@ befs_init_inodecache(void)
 {
        befs_inode_cachep = kmem_cache_create("befs_inode_cache",
                                              sizeof (struct befs_inode_info),
-                                             0, SLAB_RECLAIM_ACCOUNT,
-                                             init_once, NULL);
+                                             0, (SLAB_RECLAIM_ACCOUNT|
+                                               SLAB_MEM_SPREAD),
+                                             init_once);
        if (befs_inode_cachep == NULL) {
                printk(KERN_ERR "befs_init_inodecache: "
-                      "Couldn't initalize inode slabcache\n");
+                      "Couldn't initialize inode slabcache\n");
                return -ENOMEM;
        }
 
@@ -451,9 +456,7 @@ befs_init_inodecache(void)
 static void
 befs_destroy_inodecache(void)
 {
-       if (kmem_cache_destroy(befs_inode_cachep))
-               printk(KERN_ERR "befs_destroy_inodecache: "
-                      "not all structures were freed\n");
+       kmem_cache_destroy(befs_inode_cachep);
 }
 
 /*
@@ -472,15 +475,22 @@ befs_follow_link(struct dentry *dentry, struct nameidata *nd)
                befs_data_stream *data = &befs_ino->i_data.ds;
                befs_off_t len = data->size;
 
-               befs_debug(sb, "Follow long symlink");
-
-               link = kmalloc(len, GFP_NOFS);
-               if (!link) {
-                       link = ERR_PTR(-ENOMEM);
-               } else if (befs_read_lsymlink(sb, data, link, len) != len) {
-                       kfree(link);
-                       befs_error(sb, "Failed to read entire long symlink");
+               if (len == 0) {
+                       befs_error(sb, "Long symlink with illegal length");
                        link = ERR_PTR(-EIO);
+               } else {
+                       befs_debug(sb, "Follow long symlink");
+
+                       link = kmalloc(len, GFP_NOFS);
+                       if (!link) {
+                               link = ERR_PTR(-ENOMEM);
+                       } else if (befs_read_lsymlink(sb, data, link, len) != len) {
+                               kfree(link);
+                               befs_error(sb, "Failed to read entire long symlink");
+                               link = ERR_PTR(-EIO);
+                       } else {
+                               link[len - 1] = '\0';
+                       }
                }
        } else {
                link = befs_ino->i_data.symlink;
@@ -494,9 +504,9 @@ static void befs_put_link(struct dentry *dentry, struct nameidata *nd, void *p)
 {
        befs_inode_info *befs_ino = BEFS_I(dentry->d_inode);
        if (befs_ino->i_flags & BEFS_LONG_SYMLINK) {
-               char *p = nd_get_link(nd);
-               if (!IS_ERR(p))
-                       kfree(p);
+               char *link = nd_get_link(nd);
+               if (!IS_ERR(link))
+                       kfree(link);
        }
 }
 
@@ -514,10 +524,14 @@ befs_utf2nls(struct super_block *sb, const char *in,
 {
        struct nls_table *nls = BEFS_SB(sb)->nls;
        int i, o;
-       wchar_t uni;
+       unicode_t uni;
        int unilen, utflen;
        char *result;
-       int maxlen = in_len; /* The utf8->nls conversion can't make more chars */
+       /* The utf8->nls conversion won't make the final nls string bigger
+        * than the utf one, but if the string is pure ascii they'll have the
+        * same width and an extra char is needed to save the additional \0
+        */
+       int maxlen = in_len + 1;
 
        befs_debug(sb, "---> utf2nls()");
 
@@ -536,16 +550,16 @@ befs_utf2nls(struct super_block *sb, const char *in,
        for (i = o = 0; i < in_len; i += utflen, o += unilen) {
 
                /* convert from UTF-8 to Unicode */
-               utflen = utf8_mbtowc(&uni, &in[i], in_len - i);
-               if (utflen < 0) {
+               utflen = utf8_to_utf32(&in[i], in_len - i, &uni);
+               if (utflen < 0)
                        goto conv_err;
-               }
 
                /* convert from Unicode to nls */
+               if (uni > MAX_WCHAR_T)
+                       goto conv_err;
                unilen = nls->uni2char(uni, &result[o], in_len - o);
-               if (unilen < 0) {
+               if (unilen < 0)
                        goto conv_err;
-               }
        }
        result[o] = '\0';
        *out_len = o;
@@ -567,7 +581,7 @@ befs_utf2nls(struct super_block *sb, const char *in,
  * @sb: Superblock
  * @src: Input string buffer in NLS format
  * @srclen: Length of input string in bytes
- * @dest: The output string in UTF8 format
+ * @dest: The output string in UTF-8 format
  * @destlen: Length of the output buffer
  * 
  * Converts input string @src, which is in the format of the loaded NLS map,
@@ -593,7 +607,10 @@ befs_nls2utf(struct super_block *sb, const char *in,
        wchar_t uni;
        int unilen, utflen;
        char *result;
-       int maxlen = 3 * in_len;
+       /* There're nls characters that will translate to 3-chars-wide UTF-8
+        * characters, a additional byte is needed to save the final \0
+        * in special cases */
+       int maxlen = (3 * in_len) + 1;
 
        befs_debug(sb, "---> nls2utf()\n");
 
@@ -613,15 +630,13 @@ befs_nls2utf(struct super_block *sb, const char *in,
 
                /* convert from nls to unicode */
                unilen = nls->char2uni(&in[i], in_len - i, &uni);
-               if (unilen < 0) {
+               if (unilen < 0)
                        goto conv_err;
-               }
 
                /* convert from unicode to UTF-8 */
-               utflen = utf8_wctomb(&result[o], uni, 3);
-               if (utflen <= 0) {
+               utflen = utf32_to_utf8(uni, &result[o], 3);
+               if (utflen <= 0)
                        goto conv_err;
-               }
        }
 
        result[o] = '\0';
@@ -647,7 +662,7 @@ enum {
        Opt_uid, Opt_gid, Opt_charset, Opt_debug, Opt_err,
 };
 
-static match_table_t befs_tokens = {
+static const match_table_t befs_tokens = {
        {Opt_uid, "uid=%d"},
        {Opt_gid, "gid=%d"},
        {Opt_charset, "iocharset=%s"},
@@ -731,21 +746,11 @@ parse_options(char *options, befs_mount_options * opts)
 static void
 befs_put_super(struct super_block *sb)
 {
-       if (BEFS_SB(sb)->mount_opts.iocharset) {
-               kfree(BEFS_SB(sb)->mount_opts.iocharset);
-               BEFS_SB(sb)->mount_opts.iocharset = NULL;
-       }
-
-       if (BEFS_SB(sb)->nls) {
-               unload_nls(BEFS_SB(sb)->nls);
-               BEFS_SB(sb)->nls = NULL;
-       }
-
-       if (sb->s_fs_info) {
-               kfree(sb->s_fs_info);
-               sb->s_fs_info = NULL;
-       }
-       return;
+       kfree(BEFS_SB(sb)->mount_opts.iocharset);
+       BEFS_SB(sb)->mount_opts.iocharset = NULL;
+       unload_nls(BEFS_SB(sb)->nls);
+       kfree(sb->s_fs_info);
+       sb->s_fs_info = NULL;
 }
 
 /* Allocate private field of the superblock, fill it.
@@ -761,23 +766,25 @@ befs_fill_super(struct super_block *sb, void *data, int silent)
        befs_sb_info *befs_sb;
        befs_super_block *disk_sb;
        struct inode *root;
-
+       long ret = -EINVAL;
        const unsigned long sb_block = 0;
        const off_t x86_sb_off = 512;
 
+       save_mount_options(sb, data);
+
        sb->s_fs_info = kmalloc(sizeof (*befs_sb), GFP_KERNEL);
        if (sb->s_fs_info == NULL) {
                printk(KERN_ERR
                       "BeFS(%s): Unable to allocate memory for private "
                       "portion of superblock. Bailing.\n", sb->s_id);
-               goto unaquire_none;
+               goto unacquire_none;
        }
        befs_sb = BEFS_SB(sb);
        memset(befs_sb, 0, sizeof(befs_sb_info));
 
        if (!parse_options((char *) data, &befs_sb->mount_opts)) {
                befs_error(sb, "cannot parse mount options");
-               goto unaquire_priv_sbp;
+               goto unacquire_priv_sbp;
        }
 
        befs_debug(sb, "---> befs_fill_super()");
@@ -803,13 +810,13 @@ befs_fill_super(struct super_block *sb, void *data, int silent)
 
        if (!(bh = sb_bread(sb, sb_block))) {
                befs_error(sb, "unable to read superblock");
-               goto unaquire_priv_sbp;
+               goto unacquire_priv_sbp;
        }
 
        /* account for offset of super block on x86 */
        disk_sb = (befs_super_block *) bh->b_data;
-       if ((le32_to_cpu(disk_sb->magic1) == BEFS_SUPER_MAGIC1) ||
-           (be32_to_cpu(disk_sb->magic1) == BEFS_SUPER_MAGIC1)) {
+       if ((disk_sb->magic1 == BEFS_SUPER_MAGIC1_LE) ||
+           (disk_sb->magic1 == BEFS_SUPER_MAGIC1_BE)) {
                befs_debug(sb, "Using PPC superblock location");
        } else {
                befs_debug(sb, "Using x86 superblock location");
@@ -818,20 +825,20 @@ befs_fill_super(struct super_block *sb, void *data, int silent)
        }
 
        if (befs_load_sb(sb, disk_sb) != BEFS_OK)
-               goto unaquire_bh;
+               goto unacquire_bh;
 
        befs_dump_super_block(sb, disk_sb);
 
        brelse(bh);
 
        if (befs_check_sb(sb) != BEFS_OK)
-               goto unaquire_priv_sbp;
+               goto unacquire_priv_sbp;
 
        if( befs_sb->num_blocks > ~((sector_t)0) ) {
                befs_error(sb, "blocks count: %Lu "
                        "is larger than the host can use",
                        befs_sb->num_blocks);
-               goto unaquire_priv_sbp;
+               goto unacquire_priv_sbp;
        }
 
        /*
@@ -841,13 +848,17 @@ befs_fill_super(struct super_block *sb, void *data, int silent)
        sb->s_magic = BEFS_SUPER_MAGIC;
        /* Set real blocksize of fs */
        sb_set_blocksize(sb, (ulong) befs_sb->block_size);
-       sb->s_op = (struct super_operations *) &befs_sops;
-       root = iget(sb, iaddr2blockno(sb, &(befs_sb->root_dir)));
+       sb->s_op = &befs_sops;
+       root = befs_iget(sb, iaddr2blockno(sb, &(befs_sb->root_dir)));
+       if (IS_ERR(root)) {
+               ret = PTR_ERR(root);
+               goto unacquire_priv_sbp;
+       }
        sb->s_root = d_alloc_root(root);
        if (!sb->s_root) {
                iput(root);
                befs_error(sb, "get root inode failed");
-               goto unaquire_priv_sbp;
+               goto unacquire_priv_sbp;
        }
 
        /* load nls library */
@@ -869,15 +880,16 @@ befs_fill_super(struct super_block *sb, void *data, int silent)
 
        return 0;
 /*****************/
-      unaquire_bh:
+      unacquire_bh:
        brelse(bh);
 
-      unaquire_priv_sbp:
+      unacquire_priv_sbp:
+       kfree(befs_sb->mount_opts.iocharset);
        kfree(sb->s_fs_info);
 
-      unaquire_none:
+      unacquire_none:
        sb->s_fs_info = NULL;
-       return -EINVAL;
+       return ret;
 }
 
 static int
@@ -889,8 +901,10 @@ befs_remount(struct super_block *sb, int *flags, char *data)
 }
 
 static int
-befs_statfs(struct super_block *sb, struct kstatfs *buf)
+befs_statfs(struct dentry *dentry, struct kstatfs *buf)
 {
+       struct super_block *sb = dentry->d_sb;
+       u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
 
        befs_debug(sb, "---> befs_statfs()");
 
@@ -901,6 +915,8 @@ befs_statfs(struct super_block *sb, struct kstatfs *buf)
        buf->f_bavail = buf->f_bfree;
        buf->f_files = 0;       /* UNKNOWN */
        buf->f_ffree = 0;       /* UNKNOWN */
+       buf->f_fsid.val[0] = (u32)id;
+       buf->f_fsid.val[1] = (u32)(id >> 32);
        buf->f_namelen = BEFS_NAME_LEN;
 
        befs_debug(sb, "<--- befs_statfs()");
@@ -908,17 +924,17 @@ befs_statfs(struct super_block *sb, struct kstatfs *buf)
        return 0;
 }
 
-static struct super_block *
-befs_get_sb(struct file_system_type *fs_type, int flags, const char *dev_name,
+static struct dentry *
+befs_mount(struct file_system_type *fs_type, int flags, const char *dev_name,
            void *data)
 {
-       return get_sb_bdev(fs_type, flags, dev_name, data, befs_fill_super);
+       return mount_bdev(fs_type, flags, dev_name, data, befs_fill_super);
 }
 
 static struct file_system_type befs_fs_type = {
        .owner          = THIS_MODULE,
        .name           = "befs",
-       .get_sb         = befs_get_sb,
+       .mount          = befs_mount,
        .kill_sb        = kill_block_super,
        .fs_flags       = FS_REQUIRES_DEV,      
 };
@@ -932,18 +948,18 @@ init_befs_fs(void)
 
        err = befs_init_inodecache();
        if (err)
-               goto unaquire_none;
+               goto unacquire_none;
 
        err = register_filesystem(&befs_fs_type);
        if (err)
-               goto unaquire_inodecache;
+               goto unacquire_inodecache;
 
        return 0;
 
-unaquire_inodecache:
+unacquire_inodecache:
        befs_destroy_inodecache();
 
-unaquire_none:
+unacquire_none:
        return err;
 }