Skip to contentSkip to conversation

Squashfs: Add check for cache->entry to avoid NULL pointer dereference

PATCH NAK Jiasheng Jiang 2 participants 2 messages Jan 31, 2025 – Feb 1, 2025 SquashFS file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Squashfs: Add check for cache->entry to avoid NULL pointer dereference

Add a check for "cache->entry". Otherwise, if the allocation for "cache->entry" fails, "cache->entry[i].data" will cause a NULL pointer dereference.

Fixes f400e12656ab ("Squashfs: cache operations")
---
 fs/squashfs/cache.c | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)
fs/squashfs/cache.c +10 -7
index 4db0d2b0aab8..5a3081583ea9 100644
@@ -201,16 +201,19 @@ void squashfs_cache_delete(struct squashfs_cache *cache)
 	if (cache == NULL)
 		return;
 
-	for (i = 0; i < cache->entries; i++) {
-		if (cache->entry[i].data) {
-			for (j = 0; j < cache->pages; j++)
-				kfree(cache->entry[i].data[j]);
-			kfree(cache->entry[i].data);
+	if (cache->entry) {
+		for (i = 0; i < cache->entries; i++) {
+			if (cache->entry[i].data) {
+				for (j = 0; j < cache->pages; j++)
+					kfree(cache->entry[i].data[j]);
+				kfree(cache->entry[i].data);
+			}
+			kfree(cache->entry[i].actor);
 		}
-		kfree(cache->entry[i].actor);
+
+		kfree(cache->entry);
 	}
 
-	kfree(cache->entry);
 	kfree(cache);
 }
 
Signature · 1 line
2.25.1
maintainer NAK
#1 lore raw
Quoted 3 lines · Jiasheng Jiang

Add a check for "cache->entry". Otherwise, if the allocation for "cache->entry" fails, "cache->entry[i].data" will cause a NULL pointer dereference.

NACK.

You're ignoring the i < cache->entries loop condition.

If cache->entries is > 0, then cache->entry is non-NULL.

If cache->entry is NULL, then cache->entries == 0, and the code is skipped.

In squashfs_cache_init() the cache structure is kzalloc'ed (all 0s). If allocation of cache->entry fails, then cache->entries is 0.

Phillip

Quoted 38 lines
Fixes f400e12656ab ("Squashfs: cache operations")
---
  fs/squashfs/cache.c | 17 ++++++++++-------
  1 file changed, 10 insertions(+), 7 deletions(-)
fs/squashfs/cache.c +10 -7
index 4db0d2b0aab8..5a3081583ea9 100644
@@ -201,16 +201,19 @@ void squashfs_cache_delete(struct squashfs_cache *cache)
  	if (cache == NULL)
  		return;
 
-	for (i = 0; i < cache->entries; i++) {
-		if (cache->entry[i].data) {
-			for (j = 0; j < cache->pages; j++)
-				kfree(cache->entry[i].data[j]);
-			kfree(cache->entry[i].data);
+	if (cache->entry) {
+		for (i = 0; i < cache->entries; i++) {
+			if (cache->entry[i].data) {
+				for (j = 0; j < cache->pages; j++)
+					kfree(cache->entry[i].data[j]);
+				kfree(cache->entry[i].data);
+			}
+			kfree(cache->entry[i].actor);
  		}
-		kfree(cache->entry[i].actor);
+
+		kfree(cache->entry);
  	}
 
-	kfree(cache->entry);
  	kfree(cache);
  }