CVE-2024-29510 – Exploiting Ghostscript using format strings
A format string vulnerability in Ghostscript's uniprint device allows a Postscript file to read and write memory via crafted device parameters, bypassing the -dSAFER sandbox and leading to code execution. An attacker could exploit this by supplying a malicious document to any service that uses Ghostscript for conversion.
Published on codeanlabs.com
This is a write-up for CVE-2024-29510, a format string vulnerability in Ghostscript ≤ 10.03.0 (but ≥ 9.50). We show how this can be exploited to bypass the -dSAFER sandbox and gain code execution.
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
Likely the uniprint device uses user-controllable device parameters such as upYMoveCommand as a format string in gs_snprintf without validating format specifiers, allowing stack memory reads and writes.
Impact
An attacker could bypass the -dSAFER sandbox and achieve code execution on systems that process untrusted documents with Ghostscript, though the exploit requires a controlled pointer on the stack and may be limited by format string length constraints.
What the attacker needed
Ghostscript version 9.50 to 10.03.0; Ability to supply a Postscript file; Service uses Ghostscript for conversion
The fix
The vulnerability was fixed in Ghostscript 10.03.0 by updating to the latest version.
How it was found
- Create a Postscript file that calls setpagedevice to switch to the uniprint device with the Pcl output format.
- Set the upYMoveCommand parameter to a string containing format specifiers to read stack values.
- Render a page with showpage so the device writes output containing the leaked stack data.
- Read back the output file to observe leaked pointers and environment variables.
- Use %n or %hn in the format string to write to a controlled pointer on the stack, achieving arbitrary write.
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 converts documents like PDFs and Postscript files into other formats. A format string bug happens when a program treats user-supplied text as instructions for formatting output, letting an attacker read or write memory. Here, a malicious document could break out of Ghostscript's safety sandbox and run commands on the server.
What is RCE?
Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.