Data Leak in Ghostscript Could Allow Command Execution
Data leak vulnerabilities in Ghostscript could allow an attacker to take over a routine and even execute commands on systems, Google Project Zero researcher Tavis Ormandy has discovered.
A suite of software based on an interpreter for Adobe’s PostScript and PDF page description languages, Ghostscript is maintained by Artifex and is widely used in both stand-alone and web applications, including highly popular packages such as GIMP and ImageMagick.
The software is not vulnerability-free, and some of the security bugs found in it were previously exploited by North Korea-linked hackers. Last year, Ormandy revealed that multiple critical vulnerabilities in Ghostscript also impacted ImageMagick, Evince, GIMP, and most other PDF/PS tools.
In December 2018, a month after Ghostscript 9.26 was released, the security researcher discovered that Ghostscript didn’t protect subroutines correctly, which could result in data leaks and in an attacker taking over the routine.
The researcher explains that the manner in which subroutines are defined in PostScript could allow users to “peek inside and get references to operators they shouldn’t be allowed to use” if subroutines are not marked as executeonly when using system operators.
Furthermore, he notes that, even if subroutines are properly marked, they could still expose the contents to error handlers, meaning that they need to be made pseudo-operators with odef.
On top of that, contents need to be bind, otherwise “name resolution happens on execution, not when you define it,” allowing an intruder to change the dictstack “so that commands and operators do something different than when you defined the subroutine.”
However, it’s not only simple routines that need to be marked executeonly, made pseudo-operators with odef, and bind to be protected, but the more complicated ephemeral routines as well, and Ghostscript failed to apply the proper protections to some ephemeral routines, Ormandy discovered.
The researcher also published proof-of-concept code that works with Ghostscript 9.26 and which allows the user to take over names that couldn’t be resolved at define time, thus providing a high degree of control over the routine.
“This exploit should work via evince, ImageMagick, nautilus, less, gimp, gv, etc, etc. It might require some adjustment to work on older versions, because it requires precise alignment of the operand stack, but 9.26 and earlier are all affected,” Ormandy says.
The researcher reported the discovery to Artifex in early December. After a series of failed fixes, the maintainers managed to address the bugs with patches released earlier this month. The vulnerability is tracked as CVE-2019-6116.
The fix replaces references to operators with name objects in saved stacks for error handlers; makes all ephemeral procedures that contain dangerous operators executeonly, and any outer procedures; changes how error handlers behave in executeonly procedures so that faulting operators don’t leak; rewrites code so fewer pseudo-operators are exposed to users.
“I think this will work, although it’s hard to be confident they found all the transient routines – PostScript is really hard to read,” Ormandy says.