Skip to contentSkip to conversation

fs/squashfs: fix out-of-bounds free() in symlink path resolution

PATCH Superseded Dilip Mallavarapu 1 participant 1 message Nov 17 SquashFS file system

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fs/squashfs: fix out-of-bounds free() in symlink path resolution

sqfs_clean_base_path() computes the first index to free as count - updir - 1, where updir is the number of ".." components in an attacker-controlled symlink target and count is the base path depth. When a symlink escapes above its depth (updir >= count) the index is negative and free(base[i]) is called on pointer-sized values read from heap memory before the base_tokens[] array (CWE-590). The caller's "if (bc < 0)" guard runs only after the frees.

Reject the escaping path and free only entries we own.

Reachable from sqfs_search_dir() when resolving a path through a symlink on a crafted squashfs image (e.g. sqfsls host 0 /s/x with /s -> ../x).

---
 fs/squashfs/sqfs.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)
fs/squashfs/sqfs.c +14 -3
index 2092e3c1c71..a6e1e42ac2a 100644
@@ -391,12 +391,23 @@ free_strc:
  */
 static int sqfs_clean_base_path(char **base, int count, int updir)
 {
-	int i;
+	int i, start = count - updir - 1;
 
-	for (i = count - updir - 1; i < count; i++)
+	/*
+	 * A symlink target with more ".." components than the base path has
+	 * levels would make 'start' negative and free() out-of-bounds entries
+	 * before base[]. Free everything we own and reject the path instead.
+	 */
+	if (start < 0) {
+		for (i = 0; i < count; i++)
+			free(base[i]);
+		return -1;
+	}
+
+	for (i = start; i < count; i++)
 		free(base[i]);
 
-	return count - updir - 1;
+	return start;
 }
 
 /*
Signature · 1 line
2.53.0