Blame SOURCES/libarchive-3.1.2-CVE-2015-8930.patch

58251f
From 139c528ab8016711b6b59a4460afd0d6b236beb5 Mon Sep 17 00:00:00 2001
58251f
From: Tim Kientzle <kientzle@acm.org>
58251f
Date: Sat, 11 Apr 2015 22:44:12 -0700
58251f
Subject: [PATCH] This is a combination of 2 commits.
58251f
58251f
==  The first commit's message is: ==
58251f
58251f
Issue #522: Dir loop in malformed ISO causes segfault
58251f
58251f
Github Issue #522 revealed that we could blow the stack
58251f
when recursing to assemble ISO paths.  I saw this happen
58251f
at 130,000 dir levels.  This patch addresses this by limiting
58251f
the directory recursion to 1,000 elements.
58251f
58251f
TODO:  It would be even better to track and detect the dir loop
58251f
directly.
58251f
58251f
== This is the 2nd commit message: ==
58251f
58251f
Github Issue #522: Detect cycles in the ISO directory tree
58251f
---
58251f
 libarchive/archive_read_support_format_iso9660.c | 43 +++++++++++++++++++-----
58251f
 1 file changed, 34 insertions(+), 9 deletions(-)
58251f
58251f
diff --git a/libarchive/archive_read_support_format_iso9660.c b/libarchive/archive_read_support_format_iso9660.c
58251f
index 47268a2..6934cee 100644
58251f
--- a/libarchive/archive_read_support_format_iso9660.c
58251f
+++ b/libarchive/archive_read_support_format_iso9660.c
58251f
@@ -387,7 +387,7 @@ static int	archive_read_format_iso9660_read_data(struct archive_read *,
58251f
 static int	archive_read_format_iso9660_read_data_skip(struct archive_read *);
58251f
 static int	archive_read_format_iso9660_read_header(struct archive_read *,
58251f
 		    struct archive_entry *);
58251f
-static const char *build_pathname(struct archive_string *, struct file_info *);
58251f
+static const char *build_pathname(struct archive_string *, struct file_info *, int);
58251f
 static int	build_pathname_utf16be(unsigned char *, size_t, size_t *,
58251f
 		    struct file_info *);
58251f
 #if DEBUG
58251f
@@ -1225,6 +1225,7 @@ archive_read_format_iso9660_read_header(struct archive_read *a,
58251f
 			archive_set_error(&a->archive,
58251f
 			    ARCHIVE_ERRNO_FILE_FORMAT,
58251f
 			    "Pathname is too long");
58251f
+			return (ARCHIVE_FATAL);
58251f
 		}
58251f
 
58251f
 		r = archive_entry_copy_pathname_l(entry,
58251f
@@ -1247,9 +1248,16 @@ archive_read_format_iso9660_read_header(struct archive_read *a,
58251f
 			rd_r = ARCHIVE_WARN;
58251f
 		}
58251f
 	} else {
58251f
-		archive_string_empty(&iso9660->pathname);
58251f
-		archive_entry_set_pathname(entry,
58251f
-		    build_pathname(&iso9660->pathname, file));
58251f
+		const char *path = build_pathname(&iso9660->pathname, file, 0);
58251f
+		if (path == NULL) {
58251f
+			archive_set_error(&a->archive,
58251f
+			    ARCHIVE_ERRNO_FILE_FORMAT,
58251f
+			    "Pathname is too long");
58251f
+			return (ARCHIVE_FATAL);
58251f
+		} else {
58251f
+			archive_string_empty(&iso9660->pathname);
58251f
+			archive_entry_set_pathname(entry, path);
58251f
+		}
58251f
 	}
58251f
 
58251f
 	iso9660->entry_bytes_remaining = file->size;
58251f
@@ -1744,12 +1752,12 @@ parse_file_info(struct archive_read *a, struct file_info *parent,
58251f
     const unsigned char *isodirrec)
58251f
 {
58251f
 	struct iso9660 *iso9660;
58251f
-	struct file_info *file;
58251f
+	struct file_info *file, *filep;
58251f
 	size_t name_len;
58251f
 	const unsigned char *rr_start, *rr_end;
58251f
 	const unsigned char *p;
58251f
 	size_t dr_len;
58251f
-	uint64_t fsize;
58251f
+	uint64_t fsize, offset;
58251f
 	int32_t location;
58251f
 	int flags;
58251f
 
58251f
@@ -1793,6 +1801,16 @@ parse_file_info(struct archive_read *a, struct file_info *parent,
58251f
 		return (NULL);
58251f
 	}
58251f
 
58251f
+	/* Sanity check that this entry does not create a cycle. */
58251f
+	offset = iso9660->logical_block_size * (uint64_t)location;
58251f
+	for (filep = parent; filep != NULL; filep = filep->parent) {
58251f
+		if (filep->offset == offset) {
58251f
+			archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
58251f
+			    "Directory structure contains loop");
58251f
+			return (NULL);
58251f
+		}
58251f
+	}
58251f
+
58251f
 	/* Create a new file entry and copy data from the ISO dir record. */
58251f
 	file = (struct file_info *)calloc(1, sizeof(*file));
58251f
 	if (file == NULL) {
58251f
@@ -1801,7 +1819,7 @@ parse_file_info(struct archive_read *a, struct file_info *parent,
58251f
 		return (NULL);
58251f
 	}
58251f
 	file->parent = parent;
58251f
-	file->offset = iso9660->logical_block_size * (uint64_t)location;
58251f
+	file->offset = offset;
58251f
 	file->size = fsize;
58251f
 	file->mtime = isodate7(isodirrec + DR_date_offset);
58251f
 	file->ctime = file->atime = file->mtime;
58251f
@@ -3169,10 +3187,17 @@ time_from_tm(struct tm *t)
58251f
 }
58251f
 
58251f
 static const char *
58251f
-build_pathname(struct archive_string *as, struct file_info *file)
58251f
+build_pathname(struct archive_string *as, struct file_info *file, int depth)
58251f
 {
58251f
+	// Plain ISO9660 only allows 8 dir levels; if we get
58251f
+	// to 1000, then something is very, very wrong.
58251f
+	if (depth > 1000) {
58251f
+		return NULL;
58251f
+	}
58251f
 	if (file->parent != NULL && archive_strlen(&file->parent->name) > 0) {
58251f
-		build_pathname(as, file->parent);
58251f
+		if (build_pathname(as, file->parent, depth + 1) == NULL) {
58251f
+			return NULL;
58251f
+		}
58251f
 		archive_strcat(as, "/");
58251f
 	}
58251f
 	if (archive_strlen(&file->name) == 0)
58251f
-- 
58251f
2.7.4
58251f