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 440834229f EMF: bound bitmap sizes by the record and reject 
overflowing sizes (#1408)
440834229f is described below

commit 440834229fab7f2d6326ae9aeb0e87c5ec9c80df
Author: PJ Fanning <[email protected]>
AuthorDate: Fri Oct 16 18:31:35 2026 +0100

    EMF: bound bitmap sizes by the record and reject overflowing sizes (#1408)
    
    Follow-up to #1407, in HemfFill.readBitmap:
    
    - reject a bitmap whose offBmi+cbBmi or offBits+cbBits ends past the record,
      so the copies are bounded by the record rather than untrusted header sizes
    - compute cbBmi+cbBits as a long and reject sizes that overflow an int,
      instead of passing a negative dibSize to HwmfBitmapDib.init
    - cap the presize hint with the EMF limit (HemfPicture.getMaxRecordLength())
      rather than the WMF one
    - make TestHemfFill assert RecordFormatException instead of "not an OOM"
    
    Co-authored-by: Claude Opus 5.5 <[email protected]>
---
 .../org/apache/poi/hemf/record/emf/HemfFill.java   | 29 +++++++++++++-------
 .../org/apache/poi/hemf/record/emf/HemfMisc.java   |  6 ++---
 .../apache/poi/hemf/record/emf/TestHemfFill.java   | 31 +++++++++++++++-------
 3 files changed, 44 insertions(+), 22 deletions(-)

diff --git 
a/poi-scratchpad/src/main/java/org/apache/poi/hemf/record/emf/HemfFill.java 
b/poi-scratchpad/src/main/java/org/apache/poi/hemf/record/emf/HemfFill.java
index 7d8cc6a894..36571c9fbf 100644
--- a/poi-scratchpad/src/main/java/org/apache/poi/hemf/record/emf/HemfFill.java
+++ b/poi-scratchpad/src/main/java/org/apache/poi/hemf/record/emf/HemfFill.java
@@ -38,6 +38,7 @@ import 
org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream;
 import org.apache.logging.log4j.Logger;
 import org.apache.poi.hemf.draw.HemfDrawProperties;
 import org.apache.poi.hemf.draw.HemfGraphics;
+import org.apache.poi.hemf.usermodel.HemfPicture;
 import org.apache.poi.hwmf.draw.HwmfGraphics;
 import org.apache.poi.hwmf.record.HwmfBitmapDib;
 import org.apache.poi.hwmf.record.HwmfColorRef;
@@ -46,7 +47,6 @@ import org.apache.poi.hwmf.record.HwmfFill;
 import org.apache.poi.hwmf.record.HwmfFill.ColorUsage;
 import org.apache.poi.hwmf.record.HwmfRegionMode;
 import org.apache.poi.hwmf.record.HwmfTernaryRasterOp;
-import org.apache.poi.hwmf.usermodel.HwmfPicture;
 import org.apache.poi.logging.PoiLogManager;
 import org.apache.poi.util.GenericRecordJsonWriter;
 import org.apache.poi.util.GenericRecordUtil;
@@ -184,7 +184,7 @@ public final class HemfFill {
                 srcBounds.setRect(srcPnt.getX(), srcPnt.getY(), srcWidth, 
srcHeight);
             }
 
-            size += readBitmap(leis, bitmap, startIdx, offBmiSrc, cbBmiSrc, 
offBitsSrc, cbBitsSrc);
+            size += readBitmap(leis, bitmap, startIdx, recordSize, offBmiSrc, 
cbBmiSrc, offBitsSrc, cbBitsSrc);
 
             return size;
         }
@@ -290,7 +290,7 @@ public final class HemfFill {
 
             size += 8*LittleEndianConsts.INT_SIZE;
 
-            size += readBitmap(leis, bitmap, startIdx, offBmiSrc, cbBmiSrc, 
offBitsSrc, cbBitsSrc);
+            size += readBitmap(leis, bitmap, startIdx, recordSize, offBmiSrc, 
cbBmiSrc, offBitsSrc, cbBitsSrc);
 
             return size;
         }
@@ -655,7 +655,7 @@ public final class HemfFill {
 
             size += 7 * LittleEndianConsts.INT_SIZE;
 
-            size += readBitmap(leis, bitmap, startIdx, offBmiSrc, cbBmiSrc, 
offBitsSrc, cbBitsSrc);
+            size += readBitmap(leis, bitmap, startIdx, recordSize, offBmiSrc, 
cbBmiSrc, offBitsSrc, cbBitsSrc);
 
             return size;
         }
@@ -724,7 +724,7 @@ public final class HemfFill {
             final int cScans = Math.toIntExact(leis.readUInt());
             size += 7*LittleEndianConsts.INT_SIZE;
 
-            size += readBitmap(leis, bitmap, startIdx, offBmiSrc, cbBmiSrc, 
offBitsSrc, cbBitsSrc);
+            size += readBitmap(leis, bitmap, startIdx, recordSize, offBmiSrc, 
cbBmiSrc, offBitsSrc, cbBitsSrc);
 
             return size;
         }
@@ -767,7 +767,7 @@ public final class HemfFill {
     }
 
     static long readBitmap(final LittleEndianInputStream leis, final 
HwmfBitmapDib bitmap,
-            final int startIdx, final int offBmi, final int cbBmi, final int 
offBits, int cbBits)
+            final int startIdx, final long recordSize, final int offBmi, final 
int cbBmi, final int offBits, int cbBits)
     throws IOException {
         if (offBmi == 0) {
             return 0;
@@ -790,13 +790,24 @@ public final class HemfFill {
             return undefinedSpace1;
         }
 
-        final int dibSize = cbBmi+cbBits;
+        // the offsets are relative to the start of the record, so the bitmap 
must end within the record
+        final long recordEnd = HEADER_SIZE + recordSize;
+        if ((long)offBmi + cbBmi > recordEnd || (long)offBits + cbBits > 
recordEnd) {
+            throw new RecordFormatException("Bitmap exceeds record - offBmi: " 
+ offBmi + ", cbBmi: " + cbBmi
+                    + ", offBits: " + offBits + ", cbBits: " + cbBits + ", 
recordSize: " + recordEnd);
+        }
+
+        final long dibSizeLong = (long)cbBmi + cbBits;
+        if (dibSizeLong > Integer.MAX_VALUE) {
+            throw new RecordFormatException("Invalid bitmap size - cbBmi: " + 
cbBmi + ", cbBits: " + cbBits);
+        }
+        final int dibSize = (int)dibSizeLong;
         if (undefinedSpace2 == 0) {
             return (long)undefinedSpace1 + bitmap.init(leis, dibSize);
         }
 
-        // cbBmi and cbBits are untrusted, so don't presize the buffer beyond 
the record limit
-        final int bufferSize = (int) Math.min((long) cbBmi + cbBits, 
HwmfPicture.getMaxRecordLength());
+        // cbBmi and cbBits are untrusted, so only use them as a hint, capped 
by the EMF record limit
+        final int bufferSize = Math.min(dibSize, 
HemfPicture.getMaxRecordLength());
         final UnsynchronizedByteArrayOutputStream bos = 
UnsynchronizedByteArrayOutputStream.builder().setBufferSize(bufferSize).get();
         final long cbBmiSrcAct = IOUtils.copy(leis, bos, cbBmi);
         assert (cbBmiSrcAct == cbBmi);
diff --git 
a/poi-scratchpad/src/main/java/org/apache/poi/hemf/record/emf/HemfMisc.java 
b/poi-scratchpad/src/main/java/org/apache/poi/hemf/record/emf/HemfMisc.java
index f792d94762..2f7ea45218 100644
--- a/poi-scratchpad/src/main/java/org/apache/poi/hemf/record/emf/HemfMisc.java
+++ b/poi-scratchpad/src/main/java/org/apache/poi/hemf/record/emf/HemfMisc.java
@@ -434,7 +434,7 @@ public class HemfMisc {
             int size = 6*LittleEndianConsts.INT_SIZE;
 
             patternDib = new HwmfBitmapDib();
-            size = Math.toIntExact(size + readBitmap(leis, patternDib, 
startIdx, offBmi, cbBmi, offBits, cbBits));
+            size = Math.toIntExact(size + readBitmap(leis, patternDib, 
startIdx, recordSize, offBmi, cbBmi, offBits, cbBits));
             return size;
         }
 
@@ -638,7 +638,7 @@ public class HemfMisc {
 
             size = Math.addExact(size, numStyleEntries * 
LittleEndianConsts.INT_SIZE);
 
-            size = Math.toIntExact(size + readBitmap(leis, bitmap, startIdx, 
offBmi, cbBmi, offBits, cbBits));
+            size = Math.toIntExact(size + readBitmap(leis, bitmap, startIdx, 
recordSize, offBmi, cbBmi, offBits, cbBits));
 
             return size;
         }
@@ -902,7 +902,7 @@ public class HemfMisc {
 
             int size = 6 * LittleEndianConsts.INT_SIZE;
 
-            size = Math.toIntExact(size + readBitmap(leis, bitmap, startIdx, 
offBmi, cbBmi, offBits, cbBits));
+            size = Math.toIntExact(size + readBitmap(leis, bitmap, startIdx, 
recordSize, offBmi, cbBmi, offBits, cbBits));
 
             return size;
         }
diff --git 
a/poi-scratchpad/src/test/java/org/apache/poi/hemf/record/emf/TestHemfFill.java 
b/poi-scratchpad/src/test/java/org/apache/poi/hemf/record/emf/TestHemfFill.java
index f05f76ee66..b5ddd95928 100644
--- 
a/poi-scratchpad/src/test/java/org/apache/poi/hemf/record/emf/TestHemfFill.java
+++ 
b/poi-scratchpad/src/test/java/org/apache/poi/hemf/record/emf/TestHemfFill.java
@@ -18,32 +18,43 @@
 package org.apache.poi.hemf.record.emf;
 
 import static org.apache.poi.hemf.record.emf.HemfRecordIterator.HEADER_SIZE;
-import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 
 import java.io.ByteArrayInputStream;
 
 import org.apache.poi.hwmf.record.HwmfBitmapDib;
 import org.apache.poi.util.LittleEndianInputStream;
+import org.apache.poi.util.RecordFormatException;
 import org.junit.jupiter.api.Test;
 
 class TestHemfFill {
 
     /**
-     * When the bitmap header and bits are not contiguous, readBitmap copies 
them
-     * into an intermediate buffer. cbBmi and cbBits come straight from the 
record,
-     * so they must not be used to presize that buffer beyond the record limit:
-     * here they add up to almost 2 GB while the record only holds a few bytes.
+     * cbBmi and cbBits come straight from the record, so a bitmap that claims 
to
+     * extend past the end of the record must be rejected before anything is
+     * buffered: here they add up to almost 2 GB while the record only holds 8 
bytes.
      */
     @Test
-    void oversizedBitmapSizesDoNotPresizeBuffer() throws Exception {
+    void bitmapExceedingRecordIsRejected() throws Exception {
         byte[] data = new byte[16];
         try (LittleEndianInputStream leis = new LittleEndianInputStream(new 
ByteArrayInputStream(data))) {
-            // the record is truncated, so parsing still fails, but not by 
allocating ~2 GB first
-            Throwable t = assertThrows(Throwable.class, () -> 
HemfFill.readBitmap(
-                    leis, new HwmfBitmapDib(), HEADER_SIZE,
+            assertThrows(RecordFormatException.class, () -> 
HemfFill.readBitmap(
+                    leis, new HwmfBitmapDib(), HEADER_SIZE, 8,
                     1, 0x40000000, 0x7FFFFFF0, 0x3FFFFF00));
-            assertFalse(t instanceof OutOfMemoryError, "buffer was presized 
from cbBmi + cbBits");
+        }
+    }
+
+    /**
+     * cbBmi + cbBits must not overflow into a negative bitmap size, even when
+     * the record claims to be big enough to hold them.
+     */
+    @Test
+    void bitmapSizeOverflowIsRejected() throws Exception {
+        byte[] data = new byte[16];
+        try (LittleEndianInputStream leis = new LittleEndianInputStream(new 
ByteArrayInputStream(data))) {
+            assertThrows(RecordFormatException.class, () -> 
HemfFill.readBitmap(
+                    leis, new HwmfBitmapDib(), HEADER_SIZE, 0xFFFFFFFFL,
+                    1, 0x40000000, 0x40000001, 0x40000000));
         }
     }
 }


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

Reply via email to