Skip to contentSkip to conversation

fs/squashfs: fix NULL dereference on empty-directory lookup

PATCH In review Oscar Deschamps 3 participants 3 messages Oct 29 – Nov 11 SquashFS file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fs/squashfs: fix NULL dereference on empty-directory lookup

The three directory-search loops in sqfs.c use the local ret to tell whether the requested name was found. When the loop runs zero times ret keeps the 0 left by the preceding sqfs_opendir_nest(), so the "not found" check is skipped and the NULL dirs->entry is dereferenced (dirs->entry->inode_offset). A crafted or corrupted image can thus crash U-Boot (a NULL read) while resolving,
reading or sizing a path.

Treat a loop that produced no entry as "not found" in sqfs_search_dir(), sqfs_read_nest() and sqfs_size_nest(). Found by fuzzing the SquashFS parser.

---
 fs/squashfs/sqfs.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
fs/squashfs/sqfs.c +3 -3
index 0ea6dcaba1a..c8d2ade83e4 100644
@@ -581,7 +581,7 @@ static int sqfs_search_dir(struct squashfs_dir_stream *dirs, char **token_list,
 			dirs->entry = NULL;
 		}
 
-		if (ret) {
+		if (ret || !dirs->entry) {
 			printf("** Cannot find directory. **\n");
 			ret = -EINVAL;
 			goto out;
@@ -1501,7 +1501,7 @@ static int sqfs_read_nest(const char *filename, void *buf, loff_t offset,
 		dirs->entry = NULL;
 	}
 
-	if (ret) {
+	if (ret || !dirs->entry) {
 		printf("File not found.\n");
 		*actread = 0;
 		ret = -ENOENT;
@@ -1803,7 +1803,7 @@ static int sqfs_size_nest(const char *filename, loff_t *size)
 		dirs->entry = NULL;
 	}
 
-	if (ret) {
+	if (ret || !dirs->entry) {
 		printf("File not found.\n");
 		*size = 0;
 		ret = -EINVAL;
Signature · 1 line
2.43.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Oscar Deschamps

The three directory-search loops in sqfs.c use the local ret to tell whether the requested name was found. When the loop runs zero times ret keeps the 0 left by the preceding sqfs_opendir_nest(), so the "not found" check is skipped and the NULL dirs->entry is dereferenced (dirs->entry->inode_offset). A crafted or corrupted image can thus crash U-Boot (a NULL read) while resolving,
reading or sizing a path.

Quoted 5 lines

Treat a loop that produced no entry as "not found" in sqfs_search_dir(), sqfs_read_nest() and sqfs_size_nest(). Found by fuzzing the SquashFS parser.

#2 lore raw
In reply to Oscar Deschamps
Quoted 9 lines · Oscar Deschamps

The three directory-search loops in sqfs.c use the local ret to tell whether the requested name was found. When the loop runs zero times ret keeps the 0 left by the preceding sqfs_opendir_nest(), so the "not found" check is skipped and the NULL dirs->entry is dereferenced (dirs->entry->inode_offset). A crafted or corrupted image can thus crash U-Boot (a NULL read) while resolving,
reading or sizing a path.

[...]

Applied to u-boot/main, thanks!

[1/1] fs/squashfs: fix NULL dereference on empty-directory lookup

      commit: 106b3d678ee8e33fd1bfae4f2e831056c87f71e7
Signature · 1 line
Tom