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