Bruno Wolff III 90e6c5
From: Phillip Lougher <phillip@squashfs.org.uk>
Bruno Wolff III 90e6c5
Date: Thu, 22 Nov 2012 04:58:39 +0000 (+0000)
Bruno Wolff III 90e6c5
Subject: unsquashfs: fix CVE-2012-4024
Bruno Wolff III 90e6c5
X-Git-Url: http://squashfs.git.sourceforge.net/git/gitweb.cgi?p=squashfs%2Fsquashfs;a=commitdiff_plain;h=19c38fba0be1ce949ab44310d7f49887576cc123;hp=f7bbe5a202648b505879e2570672c012498f31fb
Bruno Wolff III 90e6c5
Bruno Wolff III 90e6c5
unsquashfs: fix CVE-2012-4024
Bruno Wolff III 90e6c5
Bruno Wolff III 90e6c5
Fix potential stack overflow in get_component() where an individual
Bruno Wolff III 90e6c5
pathname component in an extract file (specified on the command line
Bruno Wolff III 90e6c5
or in an extract file) could exceed the 1024 byte sized targname
Bruno Wolff III 90e6c5
allocated on the stack.
Bruno Wolff III 90e6c5
Bruno Wolff III 90e6c5
Fix by dynamically allocating targname rather than storing it as
Bruno Wolff III 90e6c5
a fixed size on the stack.
Bruno Wolff III 90e6c5
Bruno Wolff III 90e6c5
Signed-off-by: Phillip Lougher <phillip@squashfs.org.uk>
Bruno Wolff III 90e6c5
---
Bruno Wolff III 90e6c5
Bruno Wolff III 90e6c5
diff --git a/squashfs-tools/unsquashfs.c b/squashfs-tools/unsquashfs.c
Bruno Wolff III 90e6c5
index 90ed1c2..d9d1377 100644
Bruno Wolff III 90e6c5
--- a/squashfs-tools/unsquashfs.c
Bruno Wolff III 90e6c5
+++ b/squashfs-tools/unsquashfs.c
Bruno Wolff III 90e6c5
@@ -1099,15 +1099,18 @@ void squashfs_closedir(struct dir *dir)
Bruno Wolff III 90e6c5
 }
Bruno Wolff III 90e6c5
 
Bruno Wolff III 90e6c5
 
Bruno Wolff III 90e6c5
-char *get_component(char *target, char *targname)
Bruno Wolff III 90e6c5
+char *get_component(char *target, char **targname)
Bruno Wolff III 90e6c5
 {
Bruno Wolff III 90e6c5
+	char *start;
Bruno Wolff III 90e6c5
+
Bruno Wolff III 90e6c5
 	while(*target == '/')
Bruno Wolff III 90e6c5
 		target ++;
Bruno Wolff III 90e6c5
 
Bruno Wolff III 90e6c5
+	start = target;
Bruno Wolff III 90e6c5
 	while(*target != '/' && *target!= '\0')
Bruno Wolff III 90e6c5
-		*targname ++ = *target ++;
Bruno Wolff III 90e6c5
+		target ++;
Bruno Wolff III 90e6c5
 
Bruno Wolff III 90e6c5
-	*targname = '\0';
Bruno Wolff III 90e6c5
+	*targname = strndup(start, target - start);
Bruno Wolff III 90e6c5
 
Bruno Wolff III 90e6c5
 	return target;
Bruno Wolff III 90e6c5
 }
Bruno Wolff III 90e6c5
@@ -1133,12 +1136,12 @@ void free_path(struct pathname *paths)
Bruno Wolff III 90e6c5
 
Bruno Wolff III 90e6c5
 struct pathname *add_path(struct pathname *paths, char *target, char *alltarget)
Bruno Wolff III 90e6c5
 {
Bruno Wolff III 90e6c5
-	char targname[1024];
Bruno Wolff III 90e6c5
+	char *targname;
Bruno Wolff III 90e6c5
 	int i, error;
Bruno Wolff III 90e6c5
 
Bruno Wolff III 90e6c5
 	TRACE("add_path: adding \"%s\" extract file\n", target);
Bruno Wolff III 90e6c5
 
Bruno Wolff III 90e6c5
-	target = get_component(target, targname);
Bruno Wolff III 90e6c5
+	target = get_component(target, &targname);
Bruno Wolff III 90e6c5
 
Bruno Wolff III 90e6c5
 	if(paths == NULL) {
Bruno Wolff III 90e6c5
 		paths = malloc(sizeof(struct pathname));
Bruno Wolff III 90e6c5
@@ -1162,7 +1165,7 @@ struct pathname *add_path(struct pathname *paths, char *target, char *alltarget)
Bruno Wolff III 90e6c5
 			sizeof(struct path_entry));
Bruno Wolff III 90e6c5
 		if(paths->name == NULL)
Bruno Wolff III 90e6c5
 			EXIT_UNSQUASH("Out of memory in add_path\n");	
Bruno Wolff III 90e6c5
-		paths->name[i].name = strdup(targname);
Bruno Wolff III 90e6c5
+		paths->name[i].name = targname;
Bruno Wolff III 90e6c5
 		paths->name[i].paths = NULL;
Bruno Wolff III 90e6c5
 		if(use_regex) {
Bruno Wolff III 90e6c5
 			paths->name[i].preg = malloc(sizeof(regex_t));
Bruno Wolff III 90e6c5
@@ -1195,6 +1198,8 @@ struct pathname *add_path(struct pathname *paths, char *target, char *alltarget)
Bruno Wolff III 90e6c5
 		/*
Bruno Wolff III 90e6c5
 		 * existing matching entry
Bruno Wolff III 90e6c5
 		 */
Bruno Wolff III 90e6c5
+		free(targname);
Bruno Wolff III 90e6c5
+
Bruno Wolff III 90e6c5
 		if(paths->name[i].paths == NULL) {
Bruno Wolff III 90e6c5
 			/*
Bruno Wolff III 90e6c5
 			 * No sub-directory which means this is the leaf