Package: netsurf
Version: 3.11-2
Severity: normal
Tags: security upstream
X-Debbugs-CC: [email protected]

A heap out-of-bounds write exists in the libnsbmp copy bundled in the
netsurf source package (libnsbmp/src/libnsbmp.c). A crafted BMP makes
the decoder compute a wrapped row stride in uint32_t (swidth =
sizeof(uint32_t) * width) and then write scanlines past the allocated
pixel buffer. The same wrapped value is used by the per-row bounds
check, so the check does not stop the write.

On the official i386 packages the out-of-bounds dwords overlap heap
objects, including the nsfb_t plotter function table. This is reachable
from an image loaded from the local filesystem or fetched over HTTP.
With ASLR enabled I verified code execution (arbitrary command) in the
browser process, with the privileges of the user opening the image; if
the guessed PIE base is wrong the process crashes instead.

Affected and tested:
  * trixie: netsurf-fb 3.11-2, netsurf-gtk 3.11-2
  * bookworm: netsurf-fb 3.10-1+b3, netsurf-gtk 3.10-1+b3
  * upstream libnsbmp (0.1.7, commit 3e54099) and current master are
    still affected

I have also reported this to [email protected]. The detailed
write-up (root cause, affected code locations, exploitability analysis
and suggested fix) is attached. A proof-of-concept is available
privately to the security team on request; I would prefer not to publish
exploit details before a fix or a coordinated disclosure date.

Please assign a CVE identifier for this issue (or advise whether it
should be requested from MITRE), and let me know the planned fix and
disclosure timeline. I am happy to test a proposed patch.

Thanks,
Yuxiang Huang
# NetSurf libnsbmp Out-of-Bounds Write Report

## Summary

libnsbmp, the BMP decoder built into NetSurf, computes the row stride and row addresses with 32-bit unsigned arithmetic. A BMP whose width is close to `INT32_MAX` makes those multiplications wrap, and the decode loop then writes pixels past the allocated buffer. Both the value and the location of the out-of-bounds write are controllable; on the tested builds this was turned into code execution inside the browser process.

Affected builds (Debian official packages, i386):

- Debian 13 trixie: `netsurf-fb 3.11-2`
- Debian 12 bookworm: `netsurf-fb 3.10-1+b3`

`netsurf-gtk` uses the same libnsbmp code, and upstream libnsbmp 0.1.7 (commit `3e54099`) and current master still contain the bug.

## Root cause

The width from the file header is used directly in the stride computation:

```c
swidth = sizeof(uint32_t) * bmp->width;              /* uint32_t, wraps for width >= 0x40000000 */
bottom = top + (uint64_t)swidth * (bmp->height - 1);
```

Those two statements are at `libnsbmp/src/libnsbmp.c:533` and `:537`; the per-row check is at `:551` (`data + (4 * bmp->width) > end`), the row address at `:553-557` and the BITFIELDS write loop at `:559-570`. Line numbers follow Debian 13 trixie netsurf 3.11-2 (<https://sources.debian.org/src/netsurf/3.11-2/libnsbmp/src/libnsbmp.c/>); bookworm 3.10-1+b3 has the same line numbers.

With `swidth` wrapped, the row address and the number of dwords written by the inner loop disagree: the loop writes `bmp->width` dwords per row while each row only advances by the wrapped stride. The per-row data check `data + 4 * bmp->width > end` wraps in the same way, so the decoder does not reject the image early.

The same pattern appears in seven decoders (line numbers as above):

| Function | Line | Function | Line |
| --- | --- | --- | --- |
| `bmp_decode_rgb32` | `:533` | `bmp_decode_rgb24` | `:615` |
| `bmp_decode_rgb16` | `:686` | `bmp_decode_rgb` | `:780` |
| `bmp_decode_mask` | `:845` | `bmp_decode_rle8` | `:900` |
| `bmp_decode_rle4` | `:1054` | | |

The header parser is `bmp_info_header_parse` (`src/libnsbmp.c:84`); a negative height is normalised at `:200` into a huge unsigned value and reverses the write direction, so rows are written upward from the buffer. The `mask[3] == 0` check that sets opaque is at `:302`. With identity BITFIELDS masks every pixel dword is ORed into the destination, which on zeroed heap memory amounts to an arbitrary 32-bit write; the plain RGB paths restrict the writable byte pattern but still corrupt heap structures.

## Trigger

The browser hands the image to the BMP content handler: `nsbmp_redraw` (`content/handlers/image/bmp.c:186`) calls `bmp_decode` (`src/libnsbmp.c:1263`) on the first paint; the latter parses the header through `bmp_analyse` (`:1226`) and `bmp_info_header_parse` (`:84`), and the out-of-bounds write happens inside the decode loop. Page content rendering and history-thumbnail rendering both reach it. No further user interaction is needed beyond opening a link, a local file, or a page that contains the image.

```text
browser window
  -> content handler (content/handlers/image/bmp.c)
    -> bmp_decode -> bmp_decode_rgb32 / rgb24 / ...   (src/libnsbmp.c)
      -> out-of-bounds write
```

## Exploitability

- Primitive: a sparse heap write with a controlled offset and a controlled 32-bit value (one dword per row). Graphics surfaces allocated near the decoded buffer hold function-table pointers; the primitive redirects such a pointer into a known-zero heap area, where a replacement callback turns the next drawing call into a stack pivot and starts a ROP chain. The hijacked call is `nsfb_plot_copy` (libnsfb `src/plot/api.c:162`), reached from `framebuffer_plot_bitmap` (`frontends/framebuffer/framebuffer.c:336`).
- Mitigations: the target is a PIE with full RELRO and BIND_NOW, and the binary contains no `int 0x80` or `syscall` instruction, so shellcode and direct system calls are not available; the chain reads an already-resolved libc function pointer from the GOT, derives `system`, and calls it.
- No heap address is required: the payload needs the PIE base and relative heap layout only. Those relative offsets are reproducible for a given build and load path.
- ASLR: the i386 PIE base has 8 bits of entropy (256 page-aligned candidates). Payloads are generated for each candidate and tried in turn; the correct guess executes the command, a wrong guess crashes the process, and repeated visits accumulate the probability. This is the main practical cost of the exploit; it does not change the conclusion that code execution is possible.
- Verified: on the Debian official `netsurf-fb` packages (trixie and bookworm, i386, ASLR enabled) the PoC makes the browser process run an arbitrary command with the privileges of the user running it. Both the local `file://` path and the remote HTTP path work, the latter either by pointing the browser at a malicious BMP URL or at a page that answers with a 302 redirect.
- Limitations: a wrong guess is a browser crash (remote denial of service). The payload depends on the build and on the rendering path, so it must be generated per target. Execution stays within the browser process; no sandbox escape is involved (`netsurf-fb` itself has no sandbox).

## Fix

1. Compute `swidth` with `size_t` in every decoder and validate `width * height * 4` and the stride with 64-bit or checked arithmetic before allocating; reject widths or heights above a sane limit.
2. Validate dimensions both in the `bitmap_create` callback and at the decode entry, rather than relying on a single check.
3. Backport the upstream libnsbmp fix to Debian's `netsurf-fb` and `netsurf-gtk` and issue a security update.
4. Defence in depth: move image decoding into a separate process or sandbox; for untrusted images, apply a size-limit pre-check or refuse to decode.

Reply via email to