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

   # [Bug] Integer Underflow in HMEF TNEF Named-Property Decoding 
(`(mplen/2)-1`)
   
   ## Description
   
   Apache POI's HMEF (TNEF / `winmail.dat`) parser contains an integer-underflow
   defect in `org.apache.poi.hmef.attribute.MAPIAttribute.create()`. When 
parsing a
   named (custom) MAPI property inside a TNEF attribute, the code reads a 4-byte
   `mplen` field directly from the untrusted stream and computes
   `(mplen / 2) - 1` without any lower-bound validation:
   
   ```java
   int mplen = LittleEndian.readInt(inp);                                   // 
line 168
   byte[] mpdata = IOUtils.safelyAllocate(mplen, MAX_RECORD_LENGTH, ...);    // 
line 169
   if (IOUtils.readFully(inp, mpdata) < 0) {
       throw new IOException("Not enough data to read " + mplen + " bytes ...");
   }
   name = StringUtil.getFromUnicodeLE(mpdata, 0, (mplen / 2) - 1);             
// line 173
   ```
   
   When the attacker supplies a small `mplen` value:
   
   - `mplen = 0` yields `(0 / 2) - 1 = -1`. With `mpdata` zero-length, the
     `StringUtil.getFromUnicodeLE` guard at `StringUtil.java:87` rejects
     `offset >= string.length` (`0 >= 0`) and throws
     `ArrayIndexOutOfBoundsException: Illegal offset 0 (String data is of 
length 0)`.
   - `mplen = 1` (an odd, non-UTF-16-aligned length) yields `(1 / 2) - 1 = -1` 
(Java
     integer division produces `1/2 = 0`). The `-1` length is then passed to
     `StringUtil.getFromUnicodeLE`, whose guard at `StringUtil.java:90` rejects
     `len < 0` and throws `IllegalArgumentException: Illegal length -1`.
   
   Both variants stem from the same root cause: the `(mplen / 2) - 1` 
arithmetic is
   performed with no validation that `mplen >= 2` (one UTF-16 code unit). The 
defect
   is reachable through the public `HMEFMessage` API, which is the documented 
parser
   for TNEF email attachments (`winmail.dat`) used by Microsoft Outlook. By 
emailing
   a single crafted `winmail.dat`, an attacker can crash any mail-gateway, 
archive,
   or DLP pipeline that parses TNEF with Apache POI.
   
   Per Apache POI's documented policy, malformed-attachment parsing exceptions 
are
   expected behavior; this is a software defect resulting in a denial-of-service
   (parse-thread crash), not a security issue.
   
   ## Steps to Reproduce
   
   1. Build a TNEF stream: TNEF signature `0x223e9f78` + file_id + level `0x01`.
   2. Add a `MAPIPROPERTIES` attribute (`id=0x9003`, `type=0x0000`) containing 
one
      MAPI attribute:
      - `count=1`
      - `typeAndMV = 0x0002` (BOOLEAN)
      - `id = 0x8000` (named property)
      - 16 zero bytes (GUID)
      - `mptype = 1` (custom name)
      - `mplen = 0` (triggers `ArrayIndexOutOfBoundsException`) **or** `mplen = 
1`
        (triggers `IllegalArgumentException: Illegal length -1`)
      - For `mplen = 1`, append a single `0x41` byte as `mpdata`.
   3. Add the checksum and the `0xFF` end-of-file level byte.
   4. Save the file and open it with:
      ```java
      try (FileInputStream fis = new FileInputStream("poc_winmail.dat")) {
          new org.apache.poi.hmef.HMEFMessage(fis);
      }
      ```
   
   ## Expected Behavior
   
   The parser should validate `mplen >= 2` (at least one UTF-16 code unit) 
before
   computing the name length. A malformed named-property with `mplen < 2` 
should be
   rejected (e.g. with an `IOException`) so the caller can decide whether to 
skip the
   attribute or fail the message — rather than letting the underflowed `-1` 
reach
   `StringUtil` and produce an unchecked runtime exception.
   
   ## Actual Behavior
   
   ### Variant A — `mplen = 0`
   
   ```
   java.lang.ArrayIndexOutOfBoundsException: Illegal offset 0 (String data is 
of length 0)
       at org.apache.poi.util.StringUtil.getFromUnicodeLE(StringUtil.java:88)
       at 
org.apache.poi.hmef.attribute.MAPIAttribute.create(MAPIAttribute.java:173)
       at 
org.apache.poi.hmef.attribute.TNEFMAPIAttribute.<init>(TNEFMAPIAttribute.java:41)
       at 
org.apache.poi.hmef.attribute.TNEFAttribute.create(TNEFAttribute.java:86)
       at org.apache.poi.hmef.HMEFMessage.processMessage(HMEFMessage.java:105)
       at org.apache.poi.hmef.HMEFMessage.process(HMEFMessage.java:87)
       at org.apache.poi.hmef.HMEFMessage.<init>(HMEFMessage.java:72)
       at TnefHarness.main(TnefHarness.java:10)
   ```
   
   ### Variant B — `mplen = 1`
   
   ```
   java.lang.IllegalArgumentException: Illegal length -1
       at org.apache.poi.util.StringUtil.getFromUnicodeLE(StringUtil.java:91)
       at 
org.apache.poi.hmef.attribute.MAPIAttribute.create(MAPIAttribute.java:173)
       at 
org.apache.poi.hmef.attribute.TNEFMAPIAttribute.<init>(TNEFMAPIAttribute.java:41)
       at 
org.apache.poi.hmef.attribute.TNEFAttribute.create(TNEFAttribute.java:86)
       at org.apache.poi.hmef.HMEFMessage.processMessage(HMEFMessage.java:105)
       at org.apache.poi.hmef.HMEFMessage.process(HMEFMessage.java:87)
       at org.apache.poi.hmef.HMEFMessage.<init>(HMEFMessage.java:72)
       at _PocHarness.main(_PocHarness.java:6)
   ```
   
   Exit code 134. Reproduced 3/3 times in a fresh container.
   
   ## Environment
   
   - Project: Apache POI
   - Commit: `17de566ac64dee14bc1c132b9b4972b9a21165b3` (6.0.1-SNAPSHOT)
   - Component: `poi-scratchpad` — `org.apache.poi.hmef.attribute.MAPIAttribute`
   - Source root: 
`poi-scratchpad/src/main/java/org/apache/poi/hmef/attribute/MAPIAttribute.java`
   
   ## Suggested Fix
   
   Validate `mplen` is at least `2` before the arithmetic at 
`MAPIAttribute.java:173`:
   
   ```java
   int mplen = LittleEndian.readInt(inp);
   if (mplen < 2) {
       throw new IOException("Named MAPI property name length " + mplen
           + " is too short (must be >= 2 for one UTF-16 code unit)");
   }
   byte[] mpdata = IOUtils.safelyAllocate(mplen, MAX_RECORD_LENGTH, ...);
   // ... rest unchanged
   name = StringUtil.getFromUnicodeLE(mpdata, 0, (mplen / 2) - 1);
   ```
   
   A more defensive alternative is to skip the malformed attribute (log a 
warning and
   continue parsing the rest of the message) so a single bad attribute does not 
abort
   the entire `winmail.dat`.
   


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