Summary
pam_usb versions prior to 0.8.7 contain an OS command injection flaw in the tmux helper that builds a shell command from the user's $TMUX environment variable without proper sanitization.
By crafting a malicious TMUX value containing unescaped double-quotes, an attacker can break out of the intended command line and execute arbitrary shell commands, which run with root privileges inside the PAM stack.
Affected context
The vulnerability appears in pam_usb deployments on Linux systems that:
- Use the tmux-related integration code in
src/tmux.c - Invoke pam_usb within authentication flows where the $TMUX environment variable is present and trusted
- Construct shell commands using data derived from environment variables and execute them via
popen()
It is most relevant on systems where tmux is commonly used, and PAM is configured to call pam_usb as part of login or session management.
Exploit preconditions
- Attack surface: Local login or session establishment on systems using pam_usb with the tmux helper.
- Required access: Ability to influence or set the $TMUX environment variable for a session that will trigger the vulnerable PAM code (e.g., user-controlled shell, SSH session, or other process launching authentication with inherited environment).
- Trigger point: Execution of the pam_usb module code path that calls
src/tmux.c, which reads $TMUX, splits it on commas, and interpolates the socket-path into a shell command passed topopen(). - Why exploitation is possible: The socket-path component derived from $TMUX is injected into a double-quoted shell argument without sanitization or escaping. A double-quote character in the attacker-controlled value prematurely terminates the quoted string, allowing arbitrary shell syntax to be appended and interpreted by the shell that
popen()spawns as root.
Why this happens
The root cause is direct interpolation of untrusted input into a shell command string that is then executed by popen() with elevated privileges.
The code in src/tmux.c assumes that parsing $TMUX (splitting on commas and taking the socket path) produces a safe value and embeds it directly into a double-quoted portion of a shell command. No escaping or validation is applied, and the shell command is executed through popen() as part of the PAM stack running as root. This aligns with CWE-78: Improper Neutralization of Special Elements used in an OS Command and CWE-116: Improper Encoding or Escaping of Output, because shell metacharacters such as double-quotes are not neutralized before being passed to a shell interpreter.
Potential impact
- Execution of arbitrary shell commands as root on the affected system
- Local privilege escalation from an unprivileged user to full root access
- Installation of persistent backdoors or modification of system authentication configuration
- Exfiltration or destruction of sensitive system files and credentials
- Lateral movement within the environment by pivoting from the compromised host
General mitigation guidance
Effective mitigation focuses on removing shell interpretation from the code path and strictly controlling how external commands are constructed:
- Replace dynamic shell command construction with direct invocation of underlying functionality using APIs or
execve-style calls that do not invoke a shell. - If a helper binary must be called, pass arguments as an array (e.g.,
execv/execve/posix_spawn) rather than building command strings; never allow user-controlled data to be interpreted as shell syntax. - Apply strict validation to environment-derived values like $TMUX: parse them with a clear schema, allow only expected characters (e.g., path-safe characters), and reject or ignore data that fails validation.
- Introduce proper output encoding/escaping when data must flow into command contexts, ensuring that metacharacters such as quotes, backticks, semicolons, and ampersands are either disallowed or safely escaped.
- Run PAM modules and helper code according to least-privilege principles where possible, limiting the effect of any unexpected command execution.
How teams detect issues like this
Detection usually combines code-centric analysis with configuration review:
- Static analysis: Add rules to identify calls to
popen(),system(), or similar APIs where the command string incorporates data from environment variables (e.g.,getenv("TMUX")) or other untrusted sources. Flag string concatenation or formatting functions that join these inputs into shell commands. - Code review: During manual reviews of PAM modules and authentication helpers, pay attention to any use of shell commands, especially in root or setuid code. Look for patterns where environment variables, command-line arguments, or configuration values are interpolated into command strings.
- Policy enforcement: Establish secure coding guidelines that prohibit shell-based command execution in privileged components, or require justification and sanitization review when such patterns are unavoidable.
- Automated tooling: Use linters or security scanners capable of tracing tainted data from environment variables into command execution APIs, and integrate them into CI pipelines for early detection.

