Skip to content

Commit 44cff8a

Browse files
author
Phillip Lougher
committed
Squashfs: handle corruption of directory structure
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>
1 parent 003a319 commit 44cff8a

2 files changed

Lines changed: 21 additions & 0 deletions

File tree

‎fs/squashfs/dir.c‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,11 @@ static int squashfs_readdir(struct file *file, void *dirent, filldir_t filldir)
172172
length += sizeof(dirh);
173173

174174
dir_count = le32_to_cpu(dirh.count) + 1;
175+
176+
/* dir_count should never be larger than 256 */
177+
if (dir_count > 256)
178+
goto failed_read;
179+
175180
while (dir_count--) {
176181
/*
177182
* Read directory entry.
@@ -183,6 +188,10 @@ static int squashfs_readdir(struct file *file, void *dirent, filldir_t filldir)
183188

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

191+
/* size should never be larger than SQUASHFS_NAME_LEN */
192+
if (size > SQUASHFS_NAME_LEN)
193+
goto failed_read;
194+
186195
err = squashfs_read_metadata(inode->i_sb, dire->name,
187196
&block, &offset, size);
188197
if (err < 0)

‎fs/squashfs/namei.c‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,11 @@ static struct dentry *squashfs_lookup(struct inode *dir, struct dentry *dentry,
176176
length += sizeof(dirh);
177177

178178
dir_count = le32_to_cpu(dirh.count) + 1;
179+
180+
/* dir_count should never be larger than 256 */
181+
if (dir_count > 256)
182+
goto data_error;
183+
179184
while (dir_count--) {
180185
/*
181186
* Read directory entry.
@@ -187,6 +192,10 @@ static struct dentry *squashfs_lookup(struct inode *dir, struct dentry *dentry,
187192

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

195+
/* size should never be larger than SQUASHFS_NAME_LEN */
196+
if (size > SQUASHFS_NAME_LEN)
197+
goto data_error;
198+
190199
err = squashfs_read_metadata(dir->i_sb, dire->name,
191200
&block, &offset, size);
192201
if (err < 0)
@@ -228,6 +237,9 @@ static struct dentry *squashfs_lookup(struct inode *dir, struct dentry *dentry,
228237
d_add(dentry, inode);
229238
return ERR_PTR(0);
230239

240+
data_error:
241+
err = -EIO;
242+
231243
read_failure:
232244
ERROR("Unable to read directory block [%llx:%x]\n",
233245
squashfs_i(dir)->start + msblk->directory_table,

0 commit comments

Comments
 (0)