You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by bo...@apache.org on 2021/05/01 10:45:21 UTC

[commons-compress] branch master updated (ef765af -> c7c1a8b)

This is an automated email from the ASF dual-hosted git repository.

bodewig pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/commons-compress.git.


    from ef765af  COMPRESS-575 extract and harden parsing of GNU stuct_sparse entries
     new ecf514b  COMPRESS-575 harden parser of PAX 0.1 GNU sparse map
     new c7c1a8b  COMPRESS-575 forgot tests for parseSparseStructs

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../archivers/tar/TarArchiveInputStream.java       |   2 +-
 .../commons/compress/archivers/tar/TarFile.java    |   2 +-
 .../commons/compress/archivers/tar/TarUtils.java   |  51 ++++++++-
 .../compress/archivers/tar/TarUtilsTest.java       | 124 +++++++++++++++++++++
 4 files changed, 174 insertions(+), 5 deletions(-)

[commons-compress] 01/02: COMPRESS-575 harden parser of PAX 0.1 GNU sparse map

Posted by bo...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

bodewig pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-compress.git

commit ecf514b46342c783586c2703a5b0f371092933c3
Author: Stefan Bodewig <bo...@apache.org>
AuthorDate: Sat May 1 11:29:35 2021 +0200

    COMPRESS-575 harden parser of PAX 0.1 GNU sparse map
---
 .../archivers/tar/TarArchiveInputStream.java       |  2 +-
 .../commons/compress/archivers/tar/TarFile.java    |  2 +-
 .../commons/compress/archivers/tar/TarUtils.java   | 51 ++++++++++++++++--
 .../compress/archivers/tar/TarUtilsTest.java       | 61 ++++++++++++++++++++++
 4 files changed, 111 insertions(+), 5 deletions(-)

diff --git a/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java b/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java
index e67f1d7..4aaeba9 100644
--- a/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java
+++ b/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java
@@ -607,7 +607,7 @@ public class TarArchiveInputStream extends ArchiveInputStream {
 
         // for 0.1 PAX Headers
         if (headers.containsKey("GNU.sparse.map")) {
-            sparseHeaders = TarUtils.parsePAX01SparseHeaders(headers.get("GNU.sparse.map"));
+            sparseHeaders = new ArrayList<>(TarUtils.parseFromPAX01SparseHeaders(headers.get("GNU.sparse.map")));
         }
         getNextEntry(); // Get the actual file entry
         if (currEntry == null) {
diff --git a/src/main/java/org/apache/commons/compress/archivers/tar/TarFile.java b/src/main/java/org/apache/commons/compress/archivers/tar/TarFile.java
index 6d4a4a8..9b82025 100644
--- a/src/main/java/org/apache/commons/compress/archivers/tar/TarFile.java
+++ b/src/main/java/org/apache/commons/compress/archivers/tar/TarFile.java
@@ -431,7 +431,7 @@ public class TarFile implements Closeable {
 
         // for 0.1 PAX Headers
         if (headers.containsKey("GNU.sparse.map")) {
-            sparseHeaders = TarUtils.parsePAX01SparseHeaders(headers.get("GNU.sparse.map"));
+            sparseHeaders = new ArrayList<>(TarUtils.parseFromPAX01SparseHeaders(headers.get("GNU.sparse.map")));
         }
         getNextTarEntry(); // Get the actual file entry
         if (currEntry == null) {
diff --git a/src/main/java/org/apache/commons/compress/archivers/tar/TarUtils.java b/src/main/java/org/apache/commons/compress/archivers/tar/TarUtils.java
index fe6fe4d..eee894e 100644
--- a/src/main/java/org/apache/commons/compress/archivers/tar/TarUtils.java
+++ b/src/main/java/org/apache/commons/compress/archivers/tar/TarUtils.java
@@ -803,20 +803,65 @@ public class TarUtils {
      * GNU.sparse.map
      *    Map of non-null data chunks. It is a string consisting of comma-separated values "offset,size[,offset-1,size-1...]"
      *
+     * <p>Will internally invoke {@link #parseFromPAX01SparseHeaders} and map IOExceptions to a RzuntimeException, You
+     * should use {@link #parseFromPAX01SparseHeaders} directly instead.
+     *
      * @param sparseMap the sparse map string consisting of comma-separated values "offset,size[,offset-1,size-1...]"
      * @return sparse headers parsed from sparse map
+     * @deprecated use #parseFromPAX01SparseHeaders instead
      */
     protected static List<TarArchiveStructSparse> parsePAX01SparseHeaders(String sparseMap) {
+        try {
+            return parseFromPAX01SparseHeaders(sparseMap);
+        } catch (IOException ex) {
+            throw new RuntimeException(ex.getMessage(), ex);
+        }
+    }
+
+    /**
+     * For PAX Format 0.1, the sparse headers are stored in a single variable : GNU.sparse.map
+     * GNU.sparse.map
+     *    Map of non-null data chunks. It is a string consisting of comma-separated values "offset,size[,offset-1,size-1...]"
+     *
+     * @param sparseMap the sparse map string consisting of comma-separated values "offset,size[,offset-1,size-1...]"
+     * @return unmodifiable list of sparse headers parsed from sparse map
+     * @since 1.21
+     */
+    protected static List<TarArchiveStructSparse> parseFromPAX01SparseHeaders(String sparseMap)
+        throws IOException {
         List<TarArchiveStructSparse> sparseHeaders = new ArrayList<>();
         String[] sparseHeaderStrings = sparseMap.split(",");
+        if (sparseHeaderStrings.length % 2 == 1) {
+            throw new IOException("Corrupted TAR archive. Bad format in GNU.sparse.map PAX Header");
+        }
 
         for (int i = 0; i < sparseHeaderStrings.length; i += 2) {
-            long sparseOffset = Long.parseLong(sparseHeaderStrings[i]);
-            long sparseNumbytes = Long.parseLong(sparseHeaderStrings[i + 1]);
+            long sparseOffset;
+            try {
+                sparseOffset = Long.parseLong(sparseHeaderStrings[i]);
+            } catch (NumberFormatException ex) {
+                throw new IOException("Corrupted TAR archive."
+                    + " Sparse struct offset contains a non-numeric value");
+            }
+            if (sparseOffset < 0) {
+                throw new IOException("Corrupted TAR archive."
+                    + " Sparse struct offset contains negative value");
+            }
+            long sparseNumbytes;
+            try {
+                sparseNumbytes = Long.parseLong(sparseHeaderStrings[i + 1]);
+            } catch (NumberFormatException ex) {
+                throw new IOException("Corrupted TAR archive."
+                    + " Sparse struct numbytes contains a non-numeric value");
+            }
+            if (sparseNumbytes < 0) {
+                throw new IOException("Corrupted TAR archive."
+                    + " Sparse struct numbytes contains negative value");
+            }
             sparseHeaders.add(new TarArchiveStructSparse(sparseOffset, sparseNumbytes));
         }
 
-        return sparseHeaders;
+        return Collections.unmodifiableList(sparseHeaders);
     }
 
     /**
diff --git a/src/test/java/org/apache/commons/compress/archivers/tar/TarUtilsTest.java b/src/test/java/org/apache/commons/compress/archivers/tar/TarUtilsTest.java
index 4e04f06..1727ecd 100644
--- a/src/test/java/org/apache/commons/compress/archivers/tar/TarUtilsTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/tar/TarUtilsTest.java
@@ -548,4 +548,65 @@ public class TarUtilsTest {
             null, Collections.emptyMap());
     }
 
+    @Test
+    public void parseFromPAX01SparseHeaders() throws Exception {
+        final String map = "0,10,20,0,20,5";
+        final List<TarArchiveStructSparse> sparse = TarUtils.parseFromPAX01SparseHeaders(map);
+        assertEquals(3, sparse.size());
+        assertEquals(0, sparse.get(0).getOffset());
+        assertEquals(10, sparse.get(0).getNumbytes());
+        assertEquals(20, sparse.get(1).getOffset());
+        assertEquals(0, sparse.get(1).getNumbytes());
+        assertEquals(20, sparse.get(2).getOffset());
+        assertEquals(5, sparse.get(2).getNumbytes());
+    }
+
+    @Test
+    public void parseFromPAX01SparseHeadersRejectsOddNumberOfEntries() throws Exception {
+        thrown.expect(IOException.class);
+        thrown.expectMessage(startsWith("Corrupted TAR archive"));
+        final String map = "0,10,20,0,20";
+        TarUtils.parseFromPAX01SparseHeaders(map);
+    }
+
+    @Test
+    public void parseFromPAX01SparseHeadersRejectsNonNumericOffset() throws Exception {
+        thrown.expect(IOException.class);
+        thrown.expectMessage(startsWith("Corrupted TAR archive"));
+        final String map = "0,10,20,0,2a,5";
+        TarUtils.parseFromPAX01SparseHeaders(map);
+    }
+
+    @Test
+    public void parseFromPAX01SparseHeadersRejectsNonNumericNumbytes() throws Exception {
+        thrown.expect(IOException.class);
+        thrown.expectMessage(startsWith("Corrupted TAR archive"));
+        final String map = "0,10,20,0,20,b";
+        TarUtils.parseFromPAX01SparseHeaders(map);
+    }
+
+    @Test
+    public void parseFromPAX01SparseHeadersRejectsNegativeOffset() throws Exception {
+        thrown.expect(IOException.class);
+        thrown.expectMessage(startsWith("Corrupted TAR archive"));
+        final String map = "0,10,20,0,-2,5";
+        TarUtils.parseFromPAX01SparseHeaders(map);
+    }
+
+    @Test
+    public void parseFromPAX01SparseHeadersRejectsNegativeNumbytes() throws Exception {
+        thrown.expect(IOException.class);
+        thrown.expectMessage(startsWith("Corrupted TAR archive"));
+        final String map = "0,10,20,0,20,-5";
+        TarUtils.parseFromPAX01SparseHeaders(map);
+    }
+
+    @Test
+    public void parsePAX01SparseHeadersRejectsOddNumberOfEntries() throws Exception {
+        thrown.expect(RuntimeException.class);
+        thrown.expectMessage(startsWith("Corrupted TAR archive"));
+        final String map = "0,10,20,0,20";
+        TarUtils.parsePAX01SparseHeaders(map);
+    }
+
 }

[commons-compress] 02/02: COMPRESS-575 forgot tests for parseSparseStructs

Posted by bo...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

bodewig pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-compress.git

commit c7c1a8b561bb94dc1154ac654cb957e807a21d77
Author: Stefan Bodewig <bo...@apache.org>
AuthorDate: Sat May 1 12:43:25 2021 +0200

    COMPRESS-575 forgot tests for parseSparseStructs
---
 .../compress/archivers/tar/TarUtilsTest.java       | 63 ++++++++++++++++++++++
 1 file changed, 63 insertions(+)

diff --git a/src/test/java/org/apache/commons/compress/archivers/tar/TarUtilsTest.java b/src/test/java/org/apache/commons/compress/archivers/tar/TarUtilsTest.java
index 1727ecd..0bca2f0 100644
--- a/src/test/java/org/apache/commons/compress/archivers/tar/TarUtilsTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/tar/TarUtilsTest.java
@@ -549,6 +549,69 @@ public class TarUtilsTest {
     }
 
     @Test
+    public void readSparseStructsOctal() throws Exception {
+        final byte[] header = "00000000000 00000000007 ".getBytes();
+        assertEquals(24, header.length);
+        final List<TarArchiveStructSparse> sparse = TarUtils.readSparseStructs(header, 0, 1);
+        assertEquals(1, sparse.size());
+        assertEquals(0, sparse.get(0).getOffset());
+        assertEquals(7, sparse.get(0).getNumbytes());
+    }
+
+    @Test
+    public void readSparseStructsBinary() throws Exception {
+        final byte[] header = {
+            (byte) 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+            (byte) 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7,
+        };
+        assertEquals(24, header.length);
+        final List<TarArchiveStructSparse> sparse = TarUtils.readSparseStructs(header, 0, 1);
+        assertEquals(1, sparse.size());
+        assertEquals(0, sparse.get(0).getOffset());
+        assertEquals(7, sparse.get(0).getNumbytes());
+    }
+
+    @Test
+    public void readSparseStructsRejectsNonNumericOffset() throws Exception {
+        thrown.expect(IOException.class);
+        thrown.expectMessage(startsWith("Corrupted TAR archive"));
+        final byte[] header = "0000000000x 00000000007 ".getBytes();
+        TarUtils.readSparseStructs(header, 0, 1);
+    }
+
+    @Test
+    public void readSparseStructsRejectsNegativeOffset() throws Exception {
+        thrown.expect(IOException.class);
+        thrown.expectMessage(startsWith("Corrupted TAR archive"));
+        final byte[] header = {
+            (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
+            (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
+            (byte) 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7,
+        };
+        TarUtils.readSparseStructs(header, 0, 1);
+    }
+
+    @Test
+    public void readSparseStructsRejectsNonNumericNumbytes() throws Exception {
+        thrown.expect(IOException.class);
+        thrown.expectMessage(startsWith("Corrupted TAR archive"));
+        final byte[] header = "00000000000 0000000000x ".getBytes();
+        TarUtils.readSparseStructs(header, 0, 1);
+    }
+
+    @Test
+    public void readSparseStructsRejectsNegativeNumbytes() throws Exception {
+        thrown.expect(IOException.class);
+        thrown.expectMessage(startsWith("Corrupted TAR archive"));
+        final byte[] header = {
+            (byte) 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+            (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
+            (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
+        };
+        TarUtils.readSparseStructs(header, 0, 1);
+    }
+
+    @Test
     public void parseFromPAX01SparseHeaders() throws Exception {
         final String map = "0,10,20,0,20,5";
         final List<TarArchiveStructSparse> sparse = TarUtils.parseFromPAX01SparseHeaders(map);