Summary
Perl GD versions before 2.86 contain an OS command injection flaw in the _make_filehandle helper used by all file-based image constructors.
When untrusted filenames are passed into methods like newFromPng or newFromJpeg, specially crafted values can execute arbitrary commands or overwrite files under the process UID.
Affected context
The vulnerability arises in applications that use Perl GD (before 2.86) and pass attacker-controlled or partially untrusted strings as filenames to image constructors such as GD::Image->new, newFromPng, newFromJpeg, and similar file-based methods.
It specifically affects the code path that converts these filename arguments into filehandles via _make_filehandle, which relies on Perl's 2-argument open() semantics.
Deployments that only use the in-memory *Data variants (e.g., newFromPngData) and never interpret untrusted data as a filesystem path are not directly exposed to this bug.
Exploit preconditions
- Attack surface: Any API endpoint, script, or background job that accepts user input and passes it—directly or indirectly—as a filename to GD image constructors.
- Required access: Ability to influence or control the path string passed into
GD::Image::_make_filehandle(for example via HTTP parameters, uploaded metadata, form fields, job configuration, or message payloads). - Trigger point: Invocation of a GD constructor that takes a filename (e.g.,
GD::Image->new("user_value"),newFromPng("user_value")), which internally calls_make_filehandle. - Why exploitation is possible:
_make_filehandleuses Perl's 2-argopen()with the filename string, so filenames starting or ending with a pipe ("| cmd","cmd |") or starting with redirection syntax ("> path",">> path") are interpreted by Perl as shell commands or redirection targets instead of literal file paths.
Why this happens
The core issue is reliance on Perl's 2-argument open() with attacker-influenced strings, which conflates file path interpretation with shell-like command execution and redirection.
Under this model, a "filename" that begins or ends with a pipe character is treated as a command to execute, and leading > or >> denotes redirection targets, allowing arbitrary command execution or file truncation/overwrite.
This behavior maps primarily to CWE-78: Improper Neutralization of Special Elements used in an OS Command, because special shell metacharacters in untrusted input are not neutralized or removed before being passed to open().
It is also related to CWE-73: External Control of File Name or Path, since external input controls a file-or-command string that the library assumes to be a plain filename.
Potential impact
- Attackers can execute arbitrary OS commands under the privileges of the process running Perl GD.
- Files can be truncated or overwritten (e.g., log files, configuration files, temporary data) via redirection-like filename patterns.
- Compromise of the hosting application, including theft or corruption of data handled by the process.
- Lateral movement or further privilege escalation if combined with other local misconfigurations.
- Service disruption, denial of service, or environmental instability due to destructive commands or file corruption.
General mitigation guidance
Effective mitigation centers on strict separation between filenames and commands, and on reducing trust in any externally derived path string.
Recommended defensive patterns include:
- Avoid passing untrusted or semi-trusted input directly as filenames to GD constructors; instead, map user input to a vetted whitelist of server-side paths or use generated temporary filenames.
- Prefer in-memory
*Datavariants (e.g.,newFromPngData,newFromJpegData) when handling user-supplied image content, so raw user data is never interpreted as a filesystem path. - Sanitize and validate all filenames by rejecting values containing shell metacharacters such as
|,>,<,&,;, and newline characters, and enforce strict patterns for allowable filenames. - Use safer file-opening idioms that do not invoke shell semantics (e.g., 3-argument
open()with explicit mode and no shell interpretation) in any wrapper or utility code around GD. - Run the application under the least-privileged account possible, with restricted filesystem write access and constrained execution environment to limit damage if a path-handling bug is reached.
How teams detect issues like this
Detection typically involves both code-level analysis and runtime monitoring:
- Static analysis: Search for usage of GD image constructors that accept filenames in code paths reachable by user input, especially where raw parameters or request fields are passed directly as arguments.
- Pattern-based scans: Flag any internal helper functions that call 2-argument
open()with variables influenced by external data; enforce policies that require 3-argumentopen()or equivalent safe patterns. - Code review: Examine file handling and image loading logic for assumptions that filenames are "safe," focusing on areas where user data is cast to a filesystem path or where path normalization is missing.
- Runtime monitoring: Use logging or application-level security controls to detect suspicious filename values containing pipes or redirection characters, and to alert on unexpected command executions or file truncations.
- Automated code review tools and SAST platforms can be configured with custom rules to spot Perl 2-arg
open()and untrusted path flows specifically in GD-related code paths.

