Skip to contentSkip to conversation

squashfs: Fix heap corruption in sqfs_search_dir()

PATCH In review Richard Weinberger 3 participants 3 messages Aug 2, 2024 – Aug 16, 2024 SquashFS file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 squashfs: Fix heap corruption in sqfs_search_dir()

res needs to be large enough to store both strings rem and target, plus the path separator and the terminator.
Currently the space for the path separator is not accounted, so the heap is corrupted by one byte.

---
 fs/squashfs/sqfs.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
fs/squashfs/sqfs.c +5 -2
index af7ff80a7b..b9314019b1 100644
@@ -567,8 +567,11 @@ static int sqfs_search_dir(struct squashfs_dir_stream *dirs, char **token_list,
 				ret = -ENOMEM;
 				goto out;
 			}
-			/* Concatenate remaining tokens and symlink's target */
-			res = malloc(strlen(rem) + strlen(target) + 1);
+			/*
+			 * Concatenate remaining tokens and symlink's target.
+			 * Allocate enough space for rem, target, '/' and '\0'.
+			 */
+			res = malloc(strlen(rem) + strlen(target) + 2);
 			if (!res) {
 				ret = -ENOMEM;
 				goto out;
Signature · 1 line
2.35.3
Reviewed-by on v1 1/1
#1 lore raw

Hi Richard,

[email protected] wrote on Fri, 2 Aug 2024 22:05:09 +0200:

Quoted 4 lines

res needs to be large enough to store both strings rem and target, plus the path separator and the terminator.
Currently the space for the path separator is not accounted, so the heap is corrupted by one byte.

Mmm, subtle.

Thanks for the fix!
Miquèl

#2 lore raw
In reply to Richard Weinberger
Quoted 6 lines · Richard Weinberger

res needs to be large enough to store both strings rem and target, plus the path separator and the terminator.
Currently the space for the path separator is not accounted, so the heap is corrupted by one byte.

Applied to u-boot/next, thanks!

Signature · 1 line
Tom