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

   # [Bug] ArrayIndexOutOfBoundsException in OLE2 ChainLoopDetector.claim 
(negative DIFAT entry not rejected)
   
   ## Description
   
   Apache POI's OLE2 (POIFS) container parser has a boundary-condition defect in
   `org.apache.poi.poifs.filesystem.BlockStore.ChainLoopDetector.claim(int 
offset)`.
   The method validates only the upper bound of a block offset
   (`offset >= used_blocks.length`) but omits the lower-bound check
   (`offset < 0`). When an attacker supplies a crafted OLE2 document whose 
header
   DIFAT array contains a negative integer (specifically `0x80000000` =
   `Integer.MIN_VALUE` = `-2147483648`), the value passes the upper-bound test
   (since `-2147483648 < 2`) and is then used directly as an array index, 
throwing
   an uncaught `ArrayIndexOutOfBoundsException` that terminates the parsing
   process.
   
   The vulnerable code in `BlockStore.java` (lines 99-114):
   
   ```java
   protected void claim(int offset) {
       if (offset >= used_blocks.length) {
           // ... writing new blocks, safe
           return;
       }
   
       // Claiming an existing block, ensure there's no loop
       if (used_blocks[offset]) {        // <-- line 108: unguarded array access
           throw new IllegalStateException(...);
       }
       used_blocks[offset] = true;
   }
   ```
   
   The check `offset >= used_blocks.length` only rejects offsets that are too
   large. For a negative `offset` the comparison is `false`, so execution falls
   through to `used_blocks[offset]`. With `offset = -2147483648` and
   `used_blocks.length = 2`, the JVM throws
   `ArrayIndexOutOfBoundsException: Index -2147483648 out of bounds for length 
2`.
   
   **Access trace:**
   
   1. `HeaderBlock.getBATArray()` reads the attacker-controlled DIFAT[0] =
      `0x80000000` from the OLE2 header (no validation).
   2. `POIFSFileSystem.readCoreContents()` (line 399) iterates the BAT array.
   3. `POIFSFileSystem.readBAT()` (line 446) passes the raw value to `claim()`.
   4. `BlockStore$ChainLoopDetector.claim()` (line 108) — missing lower-bound
      check leads to out-of-bounds array access.
   
   The defect is reachable through the publicly documented
   `POIFSFileSystem(InputStream)` API.
   
   Per Apache POI's documented policy, malformed file parsing exceptions are
   expected behavior; this is a software defect resulting in a denial-of-service
   (parse-thread crash), not a security vulnerability.
   
   ## Steps to Reproduce
   
   1. Build a minimal 512-byte OLE2 header:
      - Bytes 0-7: OLE2 signature `D0 CF 11 E0 A1 B1 1A E1`.
      - At the first DIFAT array slot (header offset `0x4C`): store `0x80000000`
        little-endian.
      - Set other required header fields (sector size power, BAT count = 1,
        remaining DIFAT entries set to `0xFFFFFFFF` = free).
   2. Save the file as `poc.ole2`.
   3. Open it with the public API:
      ```java
      try (InputStream is = new FileInputStream("poc.ole2")) {
          new org.apache.poi.poifs.filesystem.POIFSFileSystem(is);
      }
      ```
   4. Observe the JVM throws
      `ArrayIndexOutOfBoundsException: Index -2147483648 out of bounds for 
length 2`.
   
   ## Expected Behavior
   
   `ChainLoopDetector.claim(int offset)` should reject `offset < 0` before
   indexing `used_blocks`. Additionally, `readCoreContents()` / `readBAT()`
   should validate that BAT and XBAT entries coming from the header are
   non-negative and within the file's block range before being passed to 
`claim()`.
   A malformed DIFAT entry should be rejected with an `IOException` so the 
caller
   can handle the bad file gracefully without crashing the parsing thread.
   
   ## Actual Behavior
   
   ```
   Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 
-2147483648 out of bounds for length 2
       at 
org.apache.poi.poifs.filesystem.BlockStore$ChainLoopDetector.claim(BlockStore.java:108)
       at 
org.apache.poi.poifs.filesystem.POIFSFileSystem.readBAT(POIFSFileSystem.java:446)
       at 
org.apache.poi.poifs.filesystem.POIFSFileSystem.readCoreContents(POIFSFileSystem.java:399)
       at 
org.apache.poi.poifs.filesystem.POIFSFileSystem.<init>(POIFSFileSystem.java:364)
       at Harness.main(Harness.java:7)
   ```
   
   Reproduced 3/3 times in a fresh container with exit code 1.
   
   ## Environment
   
   - Project: Apache POI
   - Commit: `17de566ac64dee14bc1c132b9b4972b9a21165b3` (6.0.1-SNAPSHOT)
   - Component: `poi` — `org.apache.poi.poifs.filesystem.BlockStore` (inner 
class
     `ChainLoopDetector`) and `org.apache.poi.poifs.storage.HeaderBlock`
   - Source root:
     `poi/src/main/java/org/apache/poi/poifs/filesystem/BlockStore.java`
   
   ## Suggested Fix
   
   Add a lower-bound check at the entry of `claim()`:
   
   ```java
   protected void claim(int offset) {
       if (offset < 0) {
           throw new IllegalStateException("Negative block offset: " + offset);
       }
       if (offset >= used_blocks.length) {
           return;
       }
       if (used_blocks[offset]) {
           throw new IllegalStateException(...);
       }
       used_blocks[offset] = true;
   }
   ```
   
   Additionally, `readCoreContents()` / `readBAT()` should validate that BAT and
   XBAT entries coming from the header are non-negative and within the file's
   block range before being passed to `claim()`. For example, in
   `HeaderBlock.getBATArray()`:
   
   ```java
   public int[] getBATArray() {
       int[] result = new int[Math.min(_bat_count, _max_bats_in_header)];
       for (int i = 0; i < result.length; i++) {
           int entry = _header.getInt(_bat_array_offset + i * 4);
           if (entry < 0) {
               throw new IOException("BAT entry " + i + " is negative: " + 
entry);
           }
           result[i] = entry;
       }
       return result;
   }
   ```
   


-- 
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