PoppingSnack opened a new issue, #1545:
URL: https://github.com/apache/poi/issues/1545

   # [Bug] ArrayIndexOutOfBoundsException in OLE2 Property Table Parsing 
(`_name_size` upper-bound check missing)
   
   ## Description
   
   Apache POI's OLE2 (POIFS) compound file parser has an input-validation 
defect in the
   `Property(int index, byte[] array, int offset)` constructor (in
   `org.apache.poi.poifs.property.Property`). When reading a property table 
entry from
   untrusted file data, the constructor reads the 2-byte `_name_size` field at 
offset
   `0x40`, computes `name_length = _name_size / 2 - 1`, and only validates the 
lower
   bound (`name_length < 1`). It never compares `name_length` against 
`_max_name_length`
   (31) — even though the write path `setName()` enforces that upper bound via
   `Math.min(char_array.length, _max_name_length)`. The constant 
`_max_name_length` is
   already defined (`(0x40 / 2) - 1 = 31`) but the read path bypasses it.
   
   A crafted OLE2 file that sets `_name_size` to a large value (e.g. `0x7FFE`, 
giving
   `name_length = 16382`) drives the name-reading loop in
   `Property.<init>` (lines 152-157) past the 128-byte `_raw_data` buffer 
(allocated via
   `Arrays.copyOfRange(..., PROPERTY_SIZE)` where `PROPERTY_SIZE = 128`). The 
first
   out-of-bounds access occurs at index 128, throwing
   `ArrayIndexOutOfBoundsException: Index 128 out of bounds for length 128`, 
which
   propagates uncaught through `POIFSFileSystem.<init>(InputStream)` and aborts
   document parsing.
   
   This is a software defect, not a security issue, per Apache POI's documented
   policy (malformed file parsing exceptions are expected behavior). It results 
in a
   denial of service against any application that opens attacker-supplied 
OLE2-based
   files (`.xls`, `.doc`, `.ppt`) via `POIFSFileSystem`.
   
   ## Steps to Reproduce
   
   1. Build a minimal OLE2 (compound file) with a 512-byte header, a 512-byte 
FAT
      sector, and a 512-byte property sector. In the first property entry (Root
      Property), set the `_name_size` short field at offset `0x40` to `0x7FFE` 
and
      the property type at `0x42` to `5` (ROOT_TYPE).
   2. Save the file as `poc.bin`.
   3. Compile and run a Java harness that opens the file with the public API:
      ```java
      try (InputStream is = new FileInputStream("poc.bin")) {
          new org.apache.poi.poifs.filesystem.POIFSFileSystem(is);
      }
      ```
   4. Observe the JVM throws `ArrayIndexOutOfBoundsException` and exits with 
code 134.
   
   ## Expected Behavior
   
   The OLE2 parser should validate that `name_length` is within `[1, 
_max_name_length]`
   before using it as the loop bound for reading characters from `_raw_data`. A
   malformed `_name_size` value should be rejected with an `IOException` (or 
the name
   should be clamped to `_max_name_length`), allowing the caller to handle the 
malformed
   input gracefully without crashing the parsing thread.
   
   ## Actual Behavior
   
   ```
   java.lang.ArrayIndexOutOfBoundsException: Index 128 out of bounds for length 
128
       at org.apache.poi.util.LittleEndian.getShort(LittleEndian.java:186)
       at org.apache.poi.util.ShortField.readFromBytes(ShortField.java:164)
       at org.apache.poi.util.ShortField.<init>(ShortField.java:89)
       at org.apache.poi.poifs.property.Property.<init>(Property.java:154)
       at 
org.apache.poi.poifs.property.DirectoryProperty.<init>(DirectoryProperty.java:67)
       at 
org.apache.poi.poifs.property.RootProperty.<init>(RootProperty.java:46)
       at 
org.apache.poi.poifs.property.PropertyFactory.convertToProperties(PropertyFactory.java:62)
       at 
org.apache.poi.poifs.property.PropertyTable.<init>(PropertyTable.java:109)
       at 
org.apache.poi.poifs.property.PropertyTable.<init>(PropertyTable.java:72)
       at 
org.apache.poi.poifs.filesystem.POIFSFileSystem.readCoreContents(POIFSFileSystem.java:428)
       at 
org.apache.poi.poifs.filesystem.POIFSFileSystem.<init>(POIFSFileSystem.java:364)
       at PocHarness.main(PocHarness.java:6)
   ```
   
   Exit code: 134 (JVM SIGABRT). Reproduced 3/3 times in a fresh container.
   
   ## Environment
   
   - Project: Apache POI
   - Commit: `17de566ac64dee14bc1c132b9b4972b9a21165b3` (6.0.1-SNAPSHOT)
   - Component: `poi` — `org.apache.poi.poifs.property.Property`
   - Source root: 
`poi/src/main/java/org/apache/poi/poifs/property/Property.java`
   
   ## Suggested Fix
   
   Add the upper-bound check (already applied in `setName()` at line 265) to 
the read
   constructor at `Property.java:143`. For example:
   
   ```java
   int name_length = (_name_size.get() / LittleEndianConsts.SHORT_SIZE) - 1;
   
   if (name_length < 1) {
       _name = "";
   } else if (name_length > _max_name_length) {
       // reject or clamp — matches the constraint setName() already enforces
       throw new IOException("Property name length " + name_length
           + " exceeds maximum " + _max_name_length);
   } else {
       // existing name-reading loop
   }
   ```
   
   Alternatively, reuse `Math.min(name_length, _max_name_length)` before the 
loop.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


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

Reply via email to