Blame SOURCES/libarchive-3.1.2-CVE-2016-1541.patch

995285
From 1966a9c18521a15d79f64fe893040e3fdb5a3790 Mon Sep 17 00:00:00 2001
995285
From: Tim Kientzle <kientzle@acm.org>
995285
Date: Sun, 24 Apr 2016 17:13:45 -0700
995285
Subject: [PATCH] Issue #656: Fix CVE-2016-1541, VU#862384
995285
995285
When reading OS X metadata entries in Zip archives that were stored
995285
without compression, libarchive would use the uncompressed entry size
995285
to allocate a buffer but would use the compressed entry size to limit
995285
the amount of data copied into that buffer.  Since the compressed
995285
and uncompressed sizes are provided by data in the archive itself,
995285
an attacker could manipulate these values to write data beyond
995285
the end of the allocated buffer.
995285
995285
This fix provides three new checks to guard against such
995285
manipulation and to make libarchive generally more robust when
995285
handling this type of entry:
995285
 1. If an OS X metadata entry is stored without compression,
995285
    abort the entire archive if the compressed and uncompressed
995285
    data sizes do not match.
995285
 2. When sanity-checking the size of an OS X metadata entry,
995285
    abort this entry if either the compressed or uncompressed
995285
    size is larger than 4MB.
995285
 3. When copying data into the allocated buffer, check the copy
995285
    size against both the compressed entry size and uncompressed
995285
    entry size.
995285
---
995285
 libarchive/archive_read_support_format_zip.c | 13 +++++++++++++
995285
 1 file changed, 13 insertions(+)
995285
995285
diff --git a/libarchive/archive_read_support_format_zip.c b/libarchive/archive_read_support_format_zip.c
995285
index 1aed84a..fa3db11 100644
995285
--- a/libarchive/archive_read_support_format_zip.c
995285
+++ b/libarchive/archive_read_support_format_zip.c
995285
@@ -560,6 +560,11 @@ zip_read_mac_metadata(struct archive_read *a, struct archive_entry *entry,
995285
 
995285
 	switch(rsrc->compression) {
995285
 	case 0:  /* No compression. */
995285
+		if (rsrc->uncompressed_size != rsrc->compressed_size) {
995285
+			archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
995285
+			    "Malformed OS X metadata entry: inconsistent size");
995285
+			return (ARCHIVE_FATAL);
995285
+		}
995285
 #ifdef HAVE_ZLIB_H
995285
 	case 8: /* Deflate compression. */
995285
 #endif
995285
@@ -580,6 +585,12 @@ zip_read_mac_metadata(struct archive_read *a, struct archive_entry *entry,
995285
 		    (intmax_t)rsrc->uncompressed_size);
995285
 		return (ARCHIVE_WARN);
995285
 	}
995285
+	if (rsrc->compressed_size > (4 * 1024 * 1024)) {
995285
+		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
995285
+		    "Mac metadata is too large: %jd > 4M bytes",
995285
+		    (intmax_t)rsrc->compressed_size);
995285
+		return (ARCHIVE_WARN);
995285
+	}
995285
 
995285
 	metadata = malloc((size_t)rsrc->uncompressed_size);
995285
 	if (metadata == NULL) {
995285
@@ -619,6 +630,8 @@ zip_read_mac_metadata(struct archive_read *a, struct archive_entry *entry,
995285
 			bytes_avail = remaining_bytes;
995285
 		switch(rsrc->compression) {
995285
 		case 0:  /* No compression. */
995285
+			if ((size_t)bytes_avail > metadata_bytes)
995285
+				bytes_avail = metadata_bytes;
995285
 			memcpy(mp, p, bytes_avail);
995285
 			bytes_used = (size_t)bytes_avail;
995285
 			metadata_bytes -= bytes_used;
995285
-- 
995285
2.7.4
995285