Skip to content

Commit 37986f6

Browse files
author
Phillip Lougher
committed
Squashfs: add sanity checks to id reading at mount time
Fsfuzzer generates corrupted filesystems which throw a warn_on in kmalloc. One of these is due to a corrupted superblock no_ids field. Fix this by checking that the number of bytes to be read (and allocated) does not extend into the next filesystem structure. Also add a couple of other sanity checks of the mount-time id table structures. Signed-off-by: Phillip Lougher <phillip@lougher.demon.co.uk>
1 parent 6f04864 commit 37986f6

4 files changed

Lines changed: 37 additions & 6 deletions

File tree

‎fs/squashfs/id.c‎

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,37 @@ int squashfs_get_id(struct super_block *sb, unsigned int index,
6666
* Read uncompressed id lookup table indexes from disk into memory
6767
*/
6868
__le64 *squashfs_read_id_index_table(struct super_block *sb,
69-
u64 id_table_start, unsigned short no_ids)
69+
u64 id_table_start, u64 next_table, unsigned short no_ids)
7070
{
7171
unsigned int length = SQUASHFS_ID_BLOCK_BYTES(no_ids);
72+
__le64 *table;
7273

7374
TRACE("In read_id_index_table, length %d\n", length);
7475

75-
return squashfs_read_table(sb, id_table_start, length);
76+
/* Sanity check values */
77+
78+
/* there should always be at least one id */
79+
if (no_ids == 0)
80+
return ERR_PTR(-EINVAL);
81+
82+
/*
83+
* length bytes should not extend into the next table - this check
84+
* also traps instances where id_table_start is incorrectly larger
85+
* than the next table start
86+
*/
87+
if (id_table_start + length > next_table)
88+
return ERR_PTR(-EINVAL);
89+
90+
table = squashfs_read_table(sb, id_table_start, length);
91+
92+
/*
93+
* table[0] points to the first id lookup table metadata block, this
94+
* should be less than id_table_start
95+
*/
96+
if (!IS_ERR(table) && table[0] >= id_table_start) {
97+
kfree(table);
98+
return ERR_PTR(-EINVAL);
99+
}
100+
101+
return table;
76102
}

‎fs/squashfs/squashfs.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ extern __le64 *squashfs_read_fragment_index_table(struct super_block *,
6161

6262
/* id.c */
6363
extern int squashfs_get_id(struct super_block *, unsigned int, unsigned int *);
64-
extern __le64 *squashfs_read_id_index_table(struct super_block *, u64,
64+
extern __le64 *squashfs_read_id_index_table(struct super_block *, u64, u64,
6565
unsigned short);
6666

6767
/* inode.c */

‎fs/squashfs/super.c‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ static int squashfs_fill_super(struct super_block *sb, void *data, int silent)
8383
long long root_inode;
8484
unsigned short flags;
8585
unsigned int fragments;
86-
u64 lookup_table_start, xattr_id_table_start;
86+
u64 lookup_table_start, xattr_id_table_start, next_table;
8787
int err;
8888

8989
TRACE("Entered squashfs_fill_superblock\n");
@@ -217,8 +217,10 @@ static int squashfs_fill_super(struct super_block *sb, void *data, int silent)
217217
/* Handle xattrs */
218218
sb->s_xattr = squashfs_xattr_handlers;
219219
xattr_id_table_start = le64_to_cpu(sblk->xattr_id_table_start);
220-
if (xattr_id_table_start == SQUASHFS_INVALID_BLK)
220+
if (xattr_id_table_start == SQUASHFS_INVALID_BLK) {
221+
next_table = msblk->bytes_used;
221222
goto allocate_id_index_table;
223+
}
222224

223225
/* Allocate and read xattr id lookup table */
224226
msblk->xattr_id_table = squashfs_read_xattr_id_table(sb,
@@ -230,11 +232,13 @@ static int squashfs_fill_super(struct super_block *sb, void *data, int silent)
230232
if (err != -ENOTSUPP)
231233
goto failed_mount;
232234
}
235+
next_table = msblk->xattr_table;
233236

234237
allocate_id_index_table:
235238
/* Allocate and read id index table */
236239
msblk->id_table = squashfs_read_id_index_table(sb,
237-
le64_to_cpu(sblk->id_table_start), le16_to_cpu(sblk->no_ids));
240+
le64_to_cpu(sblk->id_table_start), next_table,
241+
le16_to_cpu(sblk->no_ids));
238242
if (IS_ERR(msblk->id_table)) {
239243
ERROR("unable to read id index table\n");
240244
err = PTR_ERR(msblk->id_table);

‎fs/squashfs/xattr.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ static inline __le64 *squashfs_read_xattr_id_table(struct super_block *sb,
3131
u64 start, u64 *xattr_table_start, int *xattr_ids)
3232
{
3333
ERROR("Xattrs in filesystem, these will be ignored\n");
34+
*xattr_table_start = start;
3435
return ERR_PTR(-ENOTSUPP);
3536
}
3637

0 commit comments

Comments
 (0)