]> git.ipfire.org Git - thirdparty/libarchive.git/commitdiff
Issue 521: Properly check reading from lzss decompression buffer
authorTim Kientzle <kientzle@acm.org>
Sun, 19 Jun 2016 22:31:46 +0000 (15:31 -0700)
committerTim Kientzle <kientzle@acm.org>
Sun, 19 Jun 2016 22:31:46 +0000 (15:31 -0700)
Prior code could be tricked into trying to copy data
from beyond the end of the internal decompression buffer.

Thanks to Hanno Böck for his ongoing fuzz-testing work with libarchive.

Makefile.am
libarchive/archive_read_support_format_rar.c
libarchive/test/CMakeLists.txt
libarchive/test/test_read_format_rar_invalid1.c [new file with mode: 0644]
libarchive/test/test_read_format_rar_invalid1.rar.uu [new file with mode: 0644]

index 3e2dc629807d9f9b7f1907cc81237f554833f3ed..b93b921a8b5b15e7a0a2eb706a96651da4e5874a 100644 (file)
@@ -454,6 +454,7 @@ libarchive_test_SOURCES= \
        libarchive/test/test_read_format_rar_encryption_data.c \
        libarchive/test/test_read_format_rar_encryption_partially.c \
        libarchive/test/test_read_format_rar_encryption_header.c \
+       libarchive/test/test_read_foramt_rar_invalid1.c \
        libarchive/test/test_read_format_raw.c \
        libarchive/test/test_read_format_tar.c \
        libarchive/test/test_read_format_tar_concatenated.c \
index 6c49f1a1501c9d341dd7ea2f843d3016a1ecfc7b..f729f173645d6f83a32524c761a749963f391da5 100644 (file)
@@ -2890,11 +2890,10 @@ copy_from_lzss_window(struct archive_read *a, const void **buffer,
   }
 
   windowoffs = lzss_offset_for_position(&rar->lzss, startpos);
-  if(windowoffs + length <= lzss_size(&rar->lzss))
+  if(windowoffs + length <= lzss_size(&rar->lzss)) {
     memcpy(&rar->unp_buffer[rar->unp_offset], &rar->lzss.window[windowoffs],
            length);
-  else
-  {
+  } else if (length <= lzss_size(&rar->lzss)) {
     firstpart = lzss_size(&rar->lzss) - windowoffs;
     if (firstpart < 0) {
       archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
@@ -2906,9 +2905,14 @@ copy_from_lzss_window(struct archive_read *a, const void **buffer,
              &rar->lzss.window[windowoffs], firstpart);
       memcpy(&rar->unp_buffer[rar->unp_offset + firstpart],
              &rar->lzss.window[0], length - firstpart);
-    } else
+    } else {
       memcpy(&rar->unp_buffer[rar->unp_offset],
              &rar->lzss.window[windowoffs], length);
+    }
+  } else {
+      archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
+                        "Bad RAR file data");
+      return (ARCHIVE_FATAL);
   }
   rar->unp_offset += length;
   if (rar->unp_offset >= rar->unp_buffer_size)
index b70be74221664d11f19447c2ce61e5938ff3e68d..124aa3a8b1bae86db1c5fec5023ea1942fc35fbf 100644 (file)
@@ -143,6 +143,7 @@ IF(ENABLE_TEST)
     test_read_format_rar_encryption_data.c
     test_read_format_rar_encryption_header.c
     test_read_format_rar_encryption_partially.c
+    test_read_format_rar_invalid1.c
     test_read_format_raw.c
     test_read_format_tar.c
     test_read_format_tar_concatenated.c
diff --git a/libarchive/test/test_read_format_rar_invalid1.c b/libarchive/test/test_read_format_rar_invalid1.c
new file mode 100644 (file)
index 0000000..61dea16
--- /dev/null
@@ -0,0 +1,44 @@
+/*-
+ * Copyright (c) 2003-2016 Tim Kientzle
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+#include "test.h"
+__FBSDID("$FreeBSD$");
+
+DEFINE_TEST(test_read_format_rar_invalid1)
+{
+       const char *refname = "test_read_format_rar_invalid1.rar";
+       struct archive *a;
+       struct archive_entry *ae;
+       char *buff[100];
+
+       extract_reference_file(refname);
+       assert((a = archive_read_new()) != NULL);
+       assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
+       assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
+       assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a, refname, 10240));
+       assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
+       assertEqualIntA(a, ARCHIVE_FATAL, archive_read_data(a, buff, 99));
+       assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
+       assertEqualInt(ARCHIVE_OK, archive_read_free(a));
+}
diff --git a/libarchive/test/test_read_format_rar_invalid1.rar.uu b/libarchive/test/test_read_format_rar_invalid1.rar.uu
new file mode 100644 (file)
index 0000000..2380399
--- /dev/null
@@ -0,0 +1,5 @@
+begin 644 test_read_format_rar_invalid1.rar
+M4F%R(1H'`,^0<P``#0````````"9SG0@D"8`#`````,````#+7,'\(^>B$4=
+2,P0`I($``'1E<W0`P/\````)
+`
+end