Skip to contentSkip to conversation

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

PATCH In review Dilip Mallavarapu 2 participants 2 messages Nov 17 SquashFS file system

lore
Share & tools
lore
#0 lore raw

PATCH v2 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).

---

Changes in v2:
- Drop the inline explanatory comment (Qu Wenruo, Tom Rini)

 fs/squashfs/sqfs.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
fs/squashfs/sqfs.c +9 -3
index 2092e3c1c71..caa8baecfeb 100644
@@ -391,12 +391,18 @@ 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++)
+	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
#1 lore raw

Le 16/11/2026 à 22:57, Dilip Mallavarapu a écrit :

Quoted 8 lines

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.

Quoted 5 lines

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).

While the driver has a real bug that should be fixed, forbidding /s -> ../x is not right IHMO
We should forbid /s -> ../../x
But at the root level, /s -> ../x is ok and is the same as /s -> x

Quoted 24 lines

---

Changes in v2:
- Drop the inline explanatory comment (Qu Wenruo, Tom Rini)

  fs/squashfs/sqfs.c | 12 +++++++++---
  1 file changed, 9 insertions(+), 3 deletions(-)
fs/squashfs/sqfs.c +5 -2
index 2092e3c1c71..caa8baecfeb 100644
@@ -391,12 +391,18 @@ 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++)
+	if (start < 0) {
+		for (i = 0; i < count; i++)
+			free(base[i]);
+		return -1;

fs/squashfs/sqfs.c free_strc: · line 408

Quoted 10 lines
fs/squashfs/sqfs.c +4 -1
+	}
+
+	for (i = start; i < count; i++)
  		free(base[i]);
 
-	return count - updir - 1;
+	return start;
  }
 
  /*

Thanks!

Regards,
Richard