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

pjfanning pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/poi.git


The following commit(s) were added to refs/heads/trunk by this push:
     new 80f0e301f8 police overflow in blip calculations (#1054)
80f0e301f8 is described below

commit 80f0e301f87960d5dc76ce154ba15798cbf7a311
Author: PJ Fanning <[email protected]>
AuthorDate: Sun May 3 17:27:40 2026 +0100

    police overflow in blip calculations (#1054)
    
    * Fix integer overflow in HSLF blip array allocations; add 
TestBlipAllocation tests
    
    Agent-Logs-Url: 
https://github.com/pjfanning/poi/sessions/0e392120-4350-4d8a-91d1-1a1da3fbce08
    
    Co-authored-by: pjfanning <[email protected]>
    
    * Update Bitmap.java
    
    ---------
    
    Co-authored-by: copilot-swe-agent[bot] 
<[email protected]>
    Co-authored-by: pjfanning <[email protected]>
---
 .../main/java/org/apache/poi/hslf/blip/Bitmap.java | 19 ++++-
 .../main/java/org/apache/poi/hslf/blip/EMF.java    |  4 +-
 .../java/org/apache/poi/hslf/blip/Metafile.java    | 15 ++++
 .../main/java/org/apache/poi/hslf/blip/PICT.java   |  4 +-
 .../main/java/org/apache/poi/hslf/blip/WMF.java    |  4 +-
 .../apache/poi/hslf/blip/TestBlipAllocation.java   | 96 ++++++++++++++++++++++
 6 files changed, 138 insertions(+), 4 deletions(-)

diff --git a/poi-scratchpad/src/main/java/org/apache/poi/hslf/blip/Bitmap.java 
b/poi-scratchpad/src/main/java/org/apache/poi/hslf/blip/Bitmap.java
index c9b2a9bf4f..6e489c7012 100644
--- a/poi-scratchpad/src/main/java/org/apache/poi/hslf/blip/Bitmap.java
+++ b/poi-scratchpad/src/main/java/org/apache/poi/hslf/blip/Bitmap.java
@@ -27,6 +27,7 @@ import javax.imageio.ImageIO;
 import org.apache.commons.io.input.UnsynchronizedByteArrayInputStream;
 import org.apache.poi.ddf.EscherBSERecord;
 import org.apache.poi.ddf.EscherContainerRecord;
+import org.apache.poi.hslf.record.RecordAtom;
 import org.apache.poi.hslf.usermodel.HSLFPictureData;
 import org.apache.poi.util.IOUtils;
 import org.apache.poi.util.Internal;
@@ -60,7 +61,8 @@ public abstract class Bitmap extends HSLFPictureData {
     @Override
     protected byte[] formatImageForSlideshow(byte[] data) {
         byte[] checksum = getChecksum(data);
-        byte[] rawData = new byte[checksum.length * getUIDInstanceCount() + 1 
+ data.length];
+        long rawDataSize = calcRawDataSize(getUIDInstanceCount(), 
checksum.length, data.length);
+        byte[] rawData = IOUtils.safelyAllocate(rawDataSize, 
RecordAtom.getMaxRecordLength());
         int offset = 0;
 
         System.arraycopy(checksum, 0, rawData, offset, checksum.length);
@@ -76,6 +78,21 @@ public abstract class Bitmap extends HSLFPictureData {
         return rawData;
     }
 
+    /**
+     * Calculates the size in bytes of the raw data array produced by {@link 
#formatImageForSlideshow}.
+     * Exposed for testing overflow safety.
+     *
+     * @param uidInstanceCount UID instance count (1 or 2)
+     * @param checksumLength   length of the MD5 checksum array
+     * @param dataLength       length of the image data array
+     * @return required buffer size as a {@code long} to avoid integer overflow
+     * @throws ArithmeticException if there is an overflow
+     */
+    static long calcRawDataSize(int uidInstanceCount, int checksumLength, int 
dataLength) {
+        final long multiplicand = (long) checksumLength * uidInstanceCount;
+        return Math.addExact(multiplicand, 1L + dataLength);
+    }
+
     @Override
     public Dimension getImageDimension() {
         try (InputStream is = 
UnsynchronizedByteArrayInputStream.builder().setByteArray(getData()).get()){
diff --git a/poi-scratchpad/src/main/java/org/apache/poi/hslf/blip/EMF.java 
b/poi-scratchpad/src/main/java/org/apache/poi/hslf/blip/EMF.java
index 5e8f74f9ba..7631861237 100644
--- a/poi-scratchpad/src/main/java/org/apache/poi/hslf/blip/EMF.java
+++ b/poi-scratchpad/src/main/java/org/apache/poi/hslf/blip/EMF.java
@@ -27,6 +27,7 @@ import 
org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream;
 import org.apache.poi.ddf.EscherBSERecord;
 import org.apache.poi.ddf.EscherContainerRecord;
 import org.apache.poi.hslf.exceptions.HSLFException;
+import org.apache.poi.hslf.record.RecordAtom;
 import org.apache.poi.sl.image.ImageHeaderEMF;
 import org.apache.poi.util.IOUtils;
 import org.apache.poi.util.Internal;
@@ -85,7 +86,8 @@ public final class EMF extends Metafile {
         header.setZipSize(compressed.length);
 
         byte[] checksum = getChecksum(data);
-        byte[] rawData = new byte[checksum.length * getUIDInstanceCount() + 
header.getSize() + compressed.length];
+        long rawDataSize = calcRawDataSize(getUIDInstanceCount(), 
checksum.length, header.getSize(), compressed.length);
+        byte[] rawData = IOUtils.safelyAllocate(rawDataSize, 
RecordAtom.getMaxRecordLength());
         int offset = 0;
 
         System.arraycopy(checksum, 0, rawData, offset, checksum.length);
diff --git 
a/poi-scratchpad/src/main/java/org/apache/poi/hslf/blip/Metafile.java 
b/poi-scratchpad/src/main/java/org/apache/poi/hslf/blip/Metafile.java
index e449f13506..98b08ff101 100644
--- a/poi-scratchpad/src/main/java/org/apache/poi/hslf/blip/Metafile.java
+++ b/poi-scratchpad/src/main/java/org/apache/poi/hslf/blip/Metafile.java
@@ -226,4 +226,19 @@ public abstract class Metafile extends HSLFPictureData {
             (int)Math.round(Units.toPoints((long)header.size.getHeight()))
         );
     }
+
+    /**
+     * Calculates the size in bytes of the raw data array produced by {@code 
formatImageForSlideshow}
+     * implementations in EMF, WMF and PICT.
+     * Exposed for testing overflow safety.
+     *
+     * @param uidInstanceCount  UID instance count (1 or 2)
+     * @param checksumLength    length of the MD5 checksum array
+     * @param headerSize        size of the {@link Header} block in bytes
+     * @param compressedLength  length of the deflate-compressed image data
+     * @return required buffer size as a {@code long} to avoid integer overflow
+     */
+    static long calcRawDataSize(int uidInstanceCount, int checksumLength, int 
headerSize, int compressedLength) {
+        return (long) checksumLength * uidInstanceCount + headerSize + 
compressedLength;
+    }
 }
diff --git a/poi-scratchpad/src/main/java/org/apache/poi/hslf/blip/PICT.java 
b/poi-scratchpad/src/main/java/org/apache/poi/hslf/blip/PICT.java
index e94ef92a47..75a8d7f93b 100644
--- a/poi-scratchpad/src/main/java/org/apache/poi/hslf/blip/PICT.java
+++ b/poi-scratchpad/src/main/java/org/apache/poi/hslf/blip/PICT.java
@@ -33,6 +33,7 @@ import org.apache.poi.logging.PoiLogManager;
 import org.apache.poi.ddf.EscherBSERecord;
 import org.apache.poi.ddf.EscherContainerRecord;
 import org.apache.poi.hslf.exceptions.HSLFException;
+import org.apache.poi.hslf.record.RecordAtom;
 import org.apache.poi.sl.image.ImageHeaderPICT;
 import org.apache.poi.util.IOUtils;
 import org.apache.poi.util.Internal;
@@ -127,7 +128,8 @@ public final class PICT extends Metafile {
         header.setDimension(new Dimension(Units.toEMU(nDim.getWidth()), 
Units.toEMU(nDim.getHeight())));
 
         byte[] checksum = getChecksum(data);
-        byte[] rawData = new byte[checksum.length * getUIDInstanceCount() + 
header.getSize() + compressed.length];
+        long rawDataSize = calcRawDataSize(getUIDInstanceCount(), 
checksum.length, header.getSize(), compressed.length);
+        byte[] rawData = IOUtils.safelyAllocate(rawDataSize, 
RecordAtom.getMaxRecordLength());
         int offset = 0;
 
         System.arraycopy(checksum, 0, rawData, offset, checksum.length);
diff --git a/poi-scratchpad/src/main/java/org/apache/poi/hslf/blip/WMF.java 
b/poi-scratchpad/src/main/java/org/apache/poi/hslf/blip/WMF.java
index 8e79a5c2ae..d09b9d3964 100644
--- a/poi-scratchpad/src/main/java/org/apache/poi/hslf/blip/WMF.java
+++ b/poi-scratchpad/src/main/java/org/apache/poi/hslf/blip/WMF.java
@@ -27,6 +27,7 @@ import 
org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream;
 import org.apache.poi.ddf.EscherBSERecord;
 import org.apache.poi.ddf.EscherContainerRecord;
 import org.apache.poi.hslf.exceptions.HSLFException;
+import org.apache.poi.hslf.record.RecordAtom;
 import org.apache.poi.sl.image.ImageHeaderWMF;
 import org.apache.poi.util.IOUtils;
 import org.apache.poi.util.Internal;
@@ -90,7 +91,8 @@ public final class WMF extends Metafile {
         header.setZipSize(compressed.length);
 
         byte[] checksum = getChecksum(data);
-        byte[] rawData = new byte[checksum.length * getUIDInstanceCount() + 
header.getSize() + compressed.length];
+        long rawDataSize = calcRawDataSize(getUIDInstanceCount(), 
checksum.length, header.getSize(), compressed.length);
+        byte[] rawData = IOUtils.safelyAllocate(rawDataSize, 
RecordAtom.getMaxRecordLength());
         int offset = 0;
 
         System.arraycopy(checksum, 0, rawData, offset, checksum.length);
diff --git 
a/poi-scratchpad/src/test/java/org/apache/poi/hslf/blip/TestBlipAllocation.java 
b/poi-scratchpad/src/test/java/org/apache/poi/hslf/blip/TestBlipAllocation.java
new file mode 100644
index 0000000000..d57b875138
--- /dev/null
+++ 
b/poi-scratchpad/src/test/java/org/apache/poi/hslf/blip/TestBlipAllocation.java
@@ -0,0 +1,96 @@
+/* ====================================================================
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+==================================================================== */
+
+package org.apache.poi.hslf.blip;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import org.apache.poi.hslf.record.RecordAtom;
+import org.apache.poi.util.IOUtils;
+import org.apache.poi.util.RecordFormatException;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Tests that the raw-data size calculations in the HSLF blip classes use
+ * {@code long} arithmetic so that pathological (attacker-supplied) values
+ * cannot cause integer overflow before the array is allocated.
+ */
+public class TestBlipAllocation {
+
+    // -----------------------------------------------------------------------
+    // Bitmap.calcRawDataSize
+    // -----------------------------------------------------------------------
+
+    @Test
+    void testBitmapCalcRawDataSizeNormal() {
+        // checksumLen=16, uidCount=1, dataLen=100  =>  16*1 + 1 + 100 = 117
+        assertEquals(117L, Bitmap.calcRawDataSize(1, 16, 100));
+
+        // checksumLen=16, uidCount=2, dataLen=100  =>  16*2 + 1 + 100 = 133
+        assertEquals(133L, Bitmap.calcRawDataSize(2, 16, 100));
+    }
+
+    @Test
+    void testBitmapCalcRawDataSizeNoOverflow() {
+        // Values that would silently overflow with pure int arithmetic:
+        //   16 * 1 + 1 + Integer.MAX_VALUE  =  16 + 1 + 2_147_483_647
+        //   = 2_147_483_664  which wraps to a negative int but is fine as long
+        long expected = 16L + 1L + Integer.MAX_VALUE;
+        assertEquals(expected, Bitmap.calcRawDataSize(1, 16, 
Integer.MAX_VALUE));
+    }
+
+    @Test
+    void testBitmapSafelyAllocateRejectsOversizedBuffer() {
+        // A size larger than Integer.MAX_VALUE must be rejected by 
safelyAllocate
+        long oversized = (long) Integer.MAX_VALUE + 1L;
+        assertThrows(RecordFormatException.class,
+                () -> IOUtils.safelyAllocate(oversized, 
RecordAtom.getMaxRecordLength()));
+    }
+
+    // -----------------------------------------------------------------------
+    // Metafile.calcRawDataSize  (shared by EMF, WMF, PICT)
+    // -----------------------------------------------------------------------
+
+    @Test
+    void testMetafileCalcRawDataSizeNormal() {
+        // checksumLen=16, uidCount=1, headerSize=34, compressedLen=200
+        //   16*1 + 34 + 200 = 250
+        assertEquals(250L, Metafile.calcRawDataSize(1, 16, 34, 200));
+
+        // checksumLen=16, uidCount=2, headerSize=34, compressedLen=200
+        //   16*2 + 34 + 200 = 266
+        assertEquals(266L, Metafile.calcRawDataSize(2, 16, 34, 200));
+    }
+
+    @Test
+    void testMetafileCalcRawDataSizeNoOverflow() {
+        // checksumLen=16, uidCount=2, headerSize=34, 
compressedLen=Integer.MAX_VALUE-50
+        // Pure-int arithmetic: 32 + 34 + (Integer.MAX_VALUE - 50) overflows 
to a
+        // negative number. Long arithmetic must yield the correct positive 
result.
+        int compressedLen = Integer.MAX_VALUE - 50;
+        long expected = (long) 16 * 2 + 34L + compressedLen;
+        assertEquals(expected, Metafile.calcRawDataSize(2, 16, 34, 
compressedLen));
+    }
+
+    @Test
+    void testMetafileSafelyAllocateRejectsOversizedBuffer() {
+        long oversized = (long) Integer.MAX_VALUE + 5L;
+        assertThrows(RecordFormatException.class,
+                () -> IOUtils.safelyAllocate(oversized, 
RecordAtom.getMaxRecordLength()));
+    }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to