CVE-2024-34359
CVE-2024-34359
Weakness (CWE)
CVSS Vector
v3.1- Attack Vector
- Network
- Attack Complexity
- Low
- Privileges Required
- None
- User Interaction
- Required
- Scope
- Changed
- Confidentiality
- High
- Integrity
- High
- Availability
- High
Description
llama-cpp-python is the Python bindings for llama.cpp. `llama-cpp-python` depends on class `Llama` in `llama.py` to load `.gguf` llama.cpp or Latency Machine Learning Models. The `__init__` constructor built in the `Llama` takes several parameters to configure the loading and running of the model. Other than `NUMA, LoRa settings`, `loading tokenizers,` and `hardware settings`, `__init__` also loads the `chat template` from targeted `.gguf` 's Metadata and furtherly parses it to `llama_chat_format.Jinja2ChatFormatter.to_chat_handler()` to construct the `self.chat_handler` for this model. Nevertheless, `Jinja2ChatFormatter` parse the `chat template` within the Metadate with sandbox-less `jinja2.Environment`, which is furthermore rendered in `__call__` to construct the `prompt` of interaction. This allows `jinja2` Server Side Template Injection which leads to remote code execution by a carefully constructed payload.
Comprehensive Technical Analysis of CVE-2024-34359
1. Vulnerability Assessment and Severity Evaluation
CVE ID: CVE-2024-34359
CVSS Score: 9.6
Severity: Critical
Description: The vulnerability in llama-cpp-python arises from the use of an unsandboxed jinja2.Environment to parse and render chat templates from .gguf files. This allows for Server Side Template Injection (SSTI), which can lead to remote code execution (RCE) if an attacker crafts a malicious payload within the chat template.
Impact:
- Confidentiality: High
- Integrity: High
- Availability: High
The high CVSS score indicates that this vulnerability poses a significant risk, as it can be exploited to execute arbitrary code on the affected system, potentially leading to data breaches, system compromise, and further lateral movement within the network.
2. Potential Attack Vectors and Exploitation Methods
Attack Vectors:
- Malicious
.ggufFiles: An attacker could craft a.gguffile with a malicious chat template designed to exploit the SSTI vulnerability. - Supply Chain Attacks: Compromising the source of
.gguffiles or injecting malicious templates during the distribution process.
Exploitation Methods:
- Payload Injection: The attacker injects a specially crafted payload into the chat template, which is then parsed and rendered by
jinja2.Environment. - Remote Code Execution: The injected payload can execute arbitrary code on the server, allowing the attacker to gain control over the system.
3. Affected Systems and Software Versions
Affected Software:
llama-cpp-pythonversions prior to the patch commitb454f40a9a1787b2b5659cd2cb00819d983185df.
Affected Systems:
- Any system running the vulnerable versions of
llama-cpp-pythonand processing.gguffiles with chat templates.
4. Recommended Mitigation Strategies
Immediate Actions:
- Patching: Upgrade to the latest version of
llama-cpp-pythonthat includes the fix for this vulnerability. - Input Validation: Implement strict input validation and sanitization for chat templates to prevent injection attacks.
- Sandboxing: Ensure that
jinja2.Environmentis used in a sandboxed mode to limit the execution of untrusted code.
Long-Term Strategies:
- Code Review: Conduct thorough code reviews to identify and mitigate similar vulnerabilities.
- Security Training: Educate developers on secure coding practices and the risks associated with template injection.
- Regular Updates: Maintain a regular update schedule for all software dependencies to ensure timely patching of vulnerabilities.
5. Impact on Cybersecurity Landscape
Broader Implications:
- Supply Chain Security: Highlights the importance of securing the software supply chain, as compromised dependencies can introduce critical vulnerabilities.
- Template Injection Risks: Emphasizes the need for secure handling of template engines to prevent SSTI and RCE attacks.
- Open Source Security: Reinforces the necessity for continuous monitoring and patching of open-source projects, which are widely used and can have far-reaching impacts if compromised.
6. Technical Details for Security Professionals
Vulnerability Details:
- The
Llamaclass inllama.pyinitializes with several parameters, including loading a chat template from.gguffiles. - The chat template is parsed using
llama_chat_format.Jinja2ChatFormatter.to_chat_handler(), which utilizes an unsandboxedjinja2.Environment. - The unsandboxed environment allows for the execution of arbitrary code if a malicious payload is injected into the chat template.
Exploitation Steps:
- Craft Malicious Template: Create a
.gguffile with a chat template containing a malicious payload designed to exploit the SSTI vulnerability. - Inject Payload: Ensure the malicious
.gguffile is processed by the vulnerablellama-cpp-pythonversion. - Execute Code: The payload is parsed and rendered, leading to remote code execution on the affected system.
Mitigation Code Example:
from jinja2 import Environment, select_autoescape, sandbox
# Ensure the environment is sandboxed
env = sandbox.SandboxedEnvironment(
autoescape=select_autoescape(['html', 'xml'])
)
# Use the sandboxed environment to parse and render templates
template = env.from_string(chat_template)
rendered_template = template.render(context)
Conclusion:
The CVE-2024-34359 vulnerability in llama-cpp-python is a critical issue that requires immediate attention. By understanding the attack vectors, affected systems, and mitigation strategies, cybersecurity professionals can effectively address this vulnerability and enhance the overall security posture of their systems.