Skip to contentSkip to conversation

fs/squashfs: fix NULL dereference on failed symlink resolution

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

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fs/squashfs: fix NULL dereference on failed symlink resolution

sqfs_resolve_symlink() can return NULL, on its symlink_size overflow guard or on an allocation failure. sqfs_read_nest() and sqfs_size_nest() use the result without checking it, unlike sqfs_search_dir(), and the NULL reaches sqfs_split_path() which dereferences path[0]. A crafted or corrupted image can thus crash U-Boot (a NULL read) while reading or sizing a file through a symlink.

Check the result in both callers and guard sqfs_split_path() against a NULL path. Found by fuzzing the SquashFS parser.

---
 fs/squashfs/sqfs.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
fs/squashfs/sqfs.c +12 -0
index 0ea6dcaba1a..28d6e64e79c 100644
@@ -1295,6 +1295,10 @@ static int sqfs_split_path(char **file, char **dir, const char *path)
 
 	*file = NULL;
 	*dir = NULL;
+
+	if (!path)
+		return -EINVAL;
+
 	dirc = NULL;
 	basec = NULL;
 	bname = NULL;
@@ -1561,6 +1565,10 @@ static int sqfs_read_nest(const char *filename, void *buf, loff_t offset,
 		dirs->entry = NULL;
 		sqfs_closedir(dirsp);
 		dirsp = NULL;
+		if (!resolved) {
+			ret = -EINVAL;
+			goto out;
+		}
 		ret = sqfs_read_nest(resolved, buf, offset, len, actread);
 		free(resolved);
 		goto out;
@@ -1848,6 +1856,10 @@ static int sqfs_size_nest(const char *filename, loff_t *size)
 		 */
 		sqfs_closedir(dirsp);
 		dirsp = NULL;
+		if (!resolved) {
+			ret = -EINVAL;
+			break;
+		}
 		ret = sqfs_size(resolved, size);
 		free(resolved);
 		break;
Signature · 1 line
2.43.0
Reviewed-by on v1 1/1
#1 lore raw

Hi Oscar,

Quoted 11 lines · Oscar Deschamps

sqfs_resolve_symlink() can return NULL, on its symlink_size overflow guard or on an allocation failure. sqfs_read_nest() and sqfs_size_nest() use the result without checking it, unlike sqfs_search_dir(), and the NULL reaches sqfs_split_path() which dereferences path[0]. A crafted or corrupted image can thus crash U-Boot (a NULL read) while reading or sizing a file through a symlink.

Check the result in both callers and guard sqfs_split_path() against a NULL path. Found by fuzzing the SquashFS parser.

Thanks for the patch!

Miquèl

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

sqfs_resolve_symlink() can return NULL, on its symlink_size overflow guard or on an allocation failure. sqfs_read_nest() and sqfs_size_nest() use the result without checking it, unlike sqfs_search_dir(), and the NULL reaches sqfs_split_path() which dereferences path[0]. A crafted or corrupted image can thus crash U-Boot (a NULL read) while reading or sizing a file through a symlink.

[...]

Applied to u-boot/main, thanks!

[1/1] fs/squashfs: fix NULL dereference on failed symlink resolution

      commit: 351e0e1e4c8dee49e24868a2e7bb624d5f8bf0fe
Signature · 1 line
Tom