CVE-2023-38408
CVE-2023-38408
Weakness (CWE)
CVSS Vector
v3.1- Attack Vector
- Network
- Attack Complexity
- Low
- Privileges Required
- None
- User Interaction
- None
- Scope
- Unchanged
- Confidentiality
- High
- Integrity
- High
- Availability
- High
Description
The PKCS#11 feature in ssh-agent in OpenSSH before 9.3p2 has an insufficiently trustworthy search path, leading to remote code execution if an agent is forwarded to an attacker-controlled system. (Code in /usr/lib is not necessarily safe for loading into ssh-agent.) NOTE: this issue exists because of an incomplete fix for CVE-2016-10009.
Comprehensive Technical Analysis of CVE-2023-38408 (OpenSSH PKCS#11 Remote Code Execution Vulnerability)
1. Vulnerability Assessment and Severity Evaluation
CVE ID: CVE-2023-38408
CVSS Score: 9.8 (Critical) – AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H
Vulnerability Type: Remote Code Execution (RCE) via Insecure Library Loading (DLL Hijacking / Path Traversal)
Root Cause: Insufficiently trustworthy search path in OpenSSH’s ssh-agent PKCS#11 feature, allowing arbitrary code execution when an agent is forwarded to a malicious system.
Severity Justification
- Attack Vector (AV:N): Exploitable remotely over a network without authentication.
- Attack Complexity (AC:L): Low complexity; no special conditions required.
- Privileges Required (PR:N): No privileges needed; unauthenticated attackers can exploit.
- User Interaction (UI:N): No user interaction required.
- Scope (S:U): Impact confined to the vulnerable system (no lateral movement implied).
- Confidentiality (C:H), Integrity (I:H), Availability (A:H): Full compromise of affected systems.
The CVSS 9.8 rating reflects the high impact and low barrier to exploitation, making this a critical vulnerability requiring immediate remediation.
2. Potential Attack Vectors and Exploitation Methods
Exploitation Scenario
The vulnerability arises when an SSH agent is forwarded to a compromised or attacker-controlled system. The ssh-agent (when configured with PKCS#11 support) loads dynamic libraries from untrusted paths, enabling arbitrary code execution.
Step-by-Step Exploitation Flow
-
Victim Initiates SSH Agent Forwarding
- A user connects to a malicious or compromised intermediate host with agent forwarding enabled:
ssh -A attacker-controlled-host - The
ssh-agentsocket is forwarded to the attacker’s system.
- A user connects to a malicious or compromised intermediate host with agent forwarding enabled:
-
Attacker Triggers PKCS#11 Library Loading
- The attacker crafts a malicious PKCS#11 module (e.g.,
libpkcs11.so) and places it in a directory searched byssh-agent(e.g.,/usr/lib,/usr/local/lib, or a user-writable path). - The attacker then triggers a PKCS#11 operation (e.g., via
ssh-add -s /path/to/malicious/libpkcs11.so).
- The attacker crafts a malicious PKCS#11 module (e.g.,
-
Arbitrary Code Execution
ssh-agentloads the malicious library, executing attacker-controlled code with the privileges of the victim’s SSH agent process (typically the user’s privileges).- The attacker gains remote code execution (RCE) on the victim’s system.
Exploitation Requirements
- Agent Forwarding Must Be Enabled (
-Aflag orForwardAgent yesin~/.ssh/config). - PKCS#11 Support Must Be Compiled In (common in default OpenSSH builds).
- Attacker-Controlled Intermediate Host (e.g., a compromised jump server, malicious cloud instance, or phishing target).
Real-World Attack Surface
- Cloud Environments: Attackers compromise a shared jump host and wait for users to forward agents.
- DevOps Pipelines: CI/CD systems using SSH agent forwarding for deployments.
- Enterprise Networks: Users connecting to internal servers via bastion hosts.
3. Affected Systems and Software Versions
Vulnerable Versions
- OpenSSH versions before 9.3p2 (all versions with PKCS#11 support).
- Distributions with vulnerable packages:
- Debian (before
1:9.2p1-2+deb12u2) - Ubuntu (before
1:9.0p1-1ubuntu8.5) - RHEL/CentOS (before
openssh-8.7p1-34.el9) - Fedora (before
openssh-9.3p2-1.fc38) - macOS (before Security Update 2023-005)
- NetApp products (per NTAP-20230803-0010)
- Debian (before
Non-Vulnerable Versions
- OpenSSH 9.3p2 and later (patched).
- Systems without PKCS#11 support (rare, as it is often compiled in by default).
4. Recommended Mitigation Strategies
Immediate Actions
-
Upgrade OpenSSH
- Apply the latest patches:
- OpenSSH 9.3p2 (or later).
- Vendor-specific updates (e.g., Debian, RHEL, Ubuntu).
- Apply the latest patches:
-
Disable Agent Forwarding (Temporary Workaround)
- Per-User:
echo "ForwardAgent no" >> ~/.ssh/config - System-Wide (sshd_config):
echo "AllowAgentForwarding no" >> /etc/ssh/sshd_config systemctl restart sshd
- Per-User:
-
Restrict PKCS#11 Library Loading
- Set
PKCS11Providerexplicitly to a trusted path in~/.ssh/config:PKCS11Provider /usr/lib/x86_64-linux-gnu/opensc-pkcs11.so - Use
ssh-agent -Pto specify allowed PKCS#11 providers:ssh-agent -P /usr/lib/allowed-pkcs11-modules.so
- Set
-
Least Privilege Principle
- Avoid running
ssh-agentas root. - Use unprivileged users for SSH operations.
- Avoid running
Long-Term Hardening
-
Disable PKCS#11 Support (If Unused)
- Recompile OpenSSH without
--with-pkcs11(if PKCS#11 is not required).
- Recompile OpenSSH without
-
Network-Level Protections
- Firewall Rules: Restrict SSH agent forwarding to trusted hosts.
- VPN/Zero Trust: Enforce strict access controls for SSH connections.
-
Monitoring and Detection
- Audit Logs: Monitor
ssh-agentprocess execution and library loading. - File Integrity Monitoring (FIM): Detect unauthorized changes to
/usr/libor PKCS#11 modules. - Behavioral Analysis: Alert on unusual
ssh-agentactivity (e.g., loading unexpected libraries).
- Audit Logs: Monitor
-
Segmentation
- Isolate Jump Hosts: Ensure intermediate hosts are hardened and monitored.
- Microsegmentation: Limit lateral movement if an attacker gains RCE.
5. Impact on the Cybersecurity Landscape
Exploitation Likelihood
- High: The vulnerability is easily exploitable with minimal prerequisites (agent forwarding + PKCS#11 support).
- Active Exploitation: Proof-of-concept (PoC) exploits are publicly available (e.g., Qualys PoC).
Potential Consequences
- Initial Access: Attackers can gain a foothold in corporate networks via compromised jump hosts.
- Lateral Movement: Once RCE is achieved, attackers can pivot to other systems.
- Data Exfiltration: Sensitive keys, credentials, and data can be stolen.
- Persistence: Malicious PKCS#11 modules can maintain access across sessions.
Industry Response
- CISA KEV Catalog: Added to the Known Exploited Vulnerabilities list (mandatory patching for U.S. federal agencies).
- Vendor Advisories: Apple, NetApp, Debian, and others have released patches.
- Threat Intelligence: Active discussions in security communities (e.g., Qualys Blog).
Comparison to Historical Vulnerabilities
- CVE-2016-10009 (Incomplete Fix): This vulnerability is a regression of a previous issue, highlighting the challenges in securely implementing PKCS#11 in
ssh-agent. - Similar to CVE-2021-41617 (OpenSSH SCP RCE): Both involve trust issues in agent forwarding, reinforcing the need for secure defaults.
6. Technical Details for Security Professionals
Root Cause Analysis
-
PKCS#11 Library Loading Mechanism:
- OpenSSH’s
ssh-agentdynamically loads PKCS#11 modules (e.g., for smart cards or HSMs) usingdlopen(). - The search path for libraries is not sufficiently restricted, allowing loading from:
/usr/lib(system-wide, but writable by root).- User-controlled directories (e.g.,
LD_LIBRARY_PATH). - Relative paths (if the agent is run from an untrusted directory).
- OpenSSH’s
-
Incomplete Fix for CVE-2016-10009:
- The original fix (OpenSSH 7.4) restricted loading to
/usr/lib, but this was insufficient because:/usr/libis not guaranteed to be secure (e.g., in containerized environments).- Attackers with local access (e.g., via another vulnerability) could plant malicious libraries.
- The original fix (OpenSSH 7.4) restricted loading to
Patch Analysis (OpenSSH 9.3p2)
The fix introduces three key changes (visible in GitHub commits):
-
Restrict PKCS#11 Provider Paths
- Only allow loading from hardcoded trusted paths (e.g.,
/usr/lib/openssh/pkcs11). - Reject relative paths and user-controlled directories.
- Only allow loading from hardcoded trusted paths (e.g.,
-
Add
ssh-agent -PFlag- Allows explicit whitelisting of PKCS#11 providers:
ssh-agent -P /usr/lib/trusted-pkcs11.so
- Allows explicit whitelisting of PKCS#11 providers:
-
Improve Logging
- Logs attempted library loads for auditing:
ssh-agent[1234]: refused PKCS#11 provider /tmp/malicious.so: not in allowed paths
- Logs attempted library loads for auditing:
Exploit Technical Breakdown (Qualys PoC)
-
Malicious PKCS#11 Module
- A crafted
.sofile with a constructor function (__attribute__((constructor))) that executes arbitrary code on load. - Example:
#include <stdio.h> #include <stdlib.h> __attribute__((constructor)) void exploit() { system("id > /tmp/pwned"); } - Compiled with:
gcc -shared -fPIC -o libpkcs11.so exploit.c
- A crafted
-
Triggering the Exploit
- Attacker places
libpkcs11.soin a directory searched byssh-agent(e.g.,/usr/lib). - Victim runs:
ssh-add -s /usr/lib/libpkcs11.so ssh-agentloads the malicious library, executing the constructor.
- Attacker places
Detection and Forensics
-
Indicators of Compromise (IoCs)
- Unexpected
ssh-agentprocesses running with unusual libraries. - Suspicious files in
/usr/libor/tmp(e.g.,libpkcs11.so). - Network connections from
ssh-agentto unexpected hosts.
- Unexpected
-
Forensic Artifacts
- Process Memory: Check for injected code in
ssh-agentmemory. - Logs:
/var/log/auth.log(SSH agent activity).straceoutput (if monitoringdlopen()calls).
- File Timestamps: Look for recently modified
.sofiles in/usr/lib.
- Process Memory: Check for injected code in
-
YARA Rule for Malicious PKCS#11 Modules
rule Detect_Malicious_PKCS11_Module { meta: description = "Detects suspicious PKCS#11 modules with constructor functions" author = "Security Researcher" reference = "CVE-2023-38408" strings: $constructor = "__attribute__((constructor))" $system_call = "system(" condition: uint32(0) == 0x464c457f and ($constructor or $system_call) }
Conclusion and Recommendations
Key Takeaways
- CVE-2023-38408 is a critical RCE vulnerability in OpenSSH’s
ssh-agentPKCS#11 feature. - Exploitation is trivial if agent forwarding is enabled, making it a high-risk issue for enterprises.
- Patches are available, but disabling agent forwarding is a viable temporary mitigation.
- Monitoring and hardening are essential to detect and prevent exploitation.
Action Plan for Security Teams
| Priority | Action | Owner | Timeline |
|---|---|---|---|
| Critical | Patch OpenSSH to 9.3p2+ | IT/Security | Immediately |
| High | Disable agent forwarding (ForwardAgent no) | DevOps/Users | Within 24h |
| Medium | Audit PKCS#11 provider paths | Security Team | Within 1 week |
| Low | Deploy FIM and monitoring for ssh-agent | SOC | Within 2 weeks |
Final Thoughts
This vulnerability underscores the risks of agent forwarding and the importance of secure library loading mechanisms. Organizations should treat this as a critical patching priority and review SSH configurations to minimize exposure. Given the public PoCs and active discussions, exploitation in the wild is highly likely.
For further reading, refer to: