Skip to content

Commit

Permalink
Squashfs: handle corruption of directory structure
Browse files Browse the repository at this point in the history
Handle the rare case where a directory metadata block is uncompressed and
corrupted, leading to a kernel oops in directory scanning (memcpy).
Normally corruption is detected at the decompression stage and dealt with
then, however, this will not happen if:

- metadata isn't compressed (users can optionally request no metadata
  compression), or
- the compressed metadata block was larger than the original, in which
  case the uncompressed version was used, or
- the data was corrupt after decompression

This patch fixes this by adding some sanity checks against known maximum
values.

Signed-off-by: Phillip Lougher <phillip@lougher.demon.co.uk>
  • Loading branch information
Phillip Lougher committed Mar 16, 2011
1 parent 003a319 commit 44cff8a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
9 changes: 9 additions & 0 deletions fs/squashfs/dir.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,11 @@ static int squashfs_readdir(struct file *file, void *dirent, filldir_t filldir)
length += sizeof(dirh);

dir_count = le32_to_cpu(dirh.count) + 1;

/* dir_count should never be larger than 256 */
if (dir_count > 256)
goto failed_read;

while (dir_count--) {
/*
* Read directory entry.
Expand All @@ -183,6 +188,10 @@ static int squashfs_readdir(struct file *file, void *dirent, filldir_t filldir)

size = le16_to_cpu(dire->size) + 1;

/* size should never be larger than SQUASHFS_NAME_LEN */
if (size > SQUASHFS_NAME_LEN)
goto failed_read;

err = squashfs_read_metadata(inode->i_sb, dire->name,
&block, &offset, size);
if (err < 0)
Expand Down
12 changes: 12 additions & 0 deletions fs/squashfs/namei.c
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,11 @@ static struct dentry *squashfs_lookup(struct inode *dir, struct dentry *dentry,
length += sizeof(dirh);

dir_count = le32_to_cpu(dirh.count) + 1;

/* dir_count should never be larger than 256 */
if (dir_count > 256)
goto data_error;

while (dir_count--) {
/*
* Read directory entry.
Expand All @@ -187,6 +192,10 @@ static struct dentry *squashfs_lookup(struct inode *dir, struct dentry *dentry,

size = le16_to_cpu(dire->size) + 1;

/* size should never be larger than SQUASHFS_NAME_LEN */
if (size > SQUASHFS_NAME_LEN)
goto data_error;

err = squashfs_read_metadata(dir->i_sb, dire->name,
&block, &offset, size);
if (err < 0)
Expand Down Expand Up @@ -228,6 +237,9 @@ static struct dentry *squashfs_lookup(struct inode *dir, struct dentry *dentry,
d_add(dentry, inode);
return ERR_PTR(0);

data_error:
err = -EIO;

read_failure:
ERROR("Unable to read directory block [%llx:%x]\n",
squashfs_i(dir)->start + msblk->directory_table,
Expand Down

0 comments on commit 44cff8a

Please sign in to comment.