Skip to content
Memory corruption Write-up Advanced

Shell in the Ghost: Ghostscript CVE202328879 writeup

A heap buffer overflow in Ghostscript's BCPEncode filter allows an attacker to corrupt stream structures and achieve remote code execution. The exploit uses PostScript to leak heap memory and write to the path_control_active variable, disabling the sandbox and enabling command execution.

sigabrt9 Follow

Published on offsec.almond.consulting

A few months ago, during a web application audit we noticed that the application was using the Python Image Library (PIL) to perform resizing on the uploaded images. With a bit of curiosity we went to read the code and stumbled on the src/PIL/EpsImagePulgin.py file, which is in fact a wrapper around the Ghostscript…

sigabrt9 (@sigabrt9), on offsec.almond.consulting

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.

How it worked

Root cause

The BCPEncode filter's processing function can increment the write pointer beyond the buffer limit when an escaped character is written at the end of the buffer, causing a heap overflow.

Impact

An attacker could execute arbitrary code on the server if they can make the application process a malicious PostScript file, though the exploit's reliability depends on the Ghostscript version and options.

What the attacker needed

Application processes PostScript; attacker can supply input

The fix

The vulnerability was fixed in Ghostscript; users should update to a patched version.

How it was found

  1. Fuzz Ghostscript with a grammar-based fuzzer to find crashes.
  2. Triage crashes and identify a filter-chaining PostScript that corrupts a function pointer.
  3. Analyse the swritebuf function and debug output to find the overflow in s_xBCPE_process.
  4. Craft a minimal PostScript to trigger the overflow with a small string and an escaped character.
  5. Chain filters to bypass the ioerror and overwrite stream structures for leak and write primitives.
  6. Use the primitives to disable the sandbox and execute commands via the %pipe% device.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.

Ghostscript is a program that reads PostScript and PDF files. A bug in one of its filters lets an attacker write past the end of a buffer, which can crash the program or let the attacker run their own commands. This matters because many apps use Ghostscript to handle images or PDFs.

What is Memory corruption?

Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.

More from Artifex Ghostscript

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Memory corruption

The lesson behind this report.

Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.