CVE-2024-52803: Llamafactory LLama Factory Remote OS Command Vulnerability

HIGH
Advisory Identifier: GHSA-hj3w-wrh4-44vp
CVSS 7.5
CWE-78 CWE-79
Published
Advisory Summary

CVE-2024-52803 is a HIGH (CVSS 7.5) llama factory remote os command vulnerability (CWE-78: Improper Neutralization of Special Elements used in an OS Command ('OS Command Injection')) affecting Llamafactory libraries in the PyPI ecosystem. The issue involves llama factory remote os command injection vulnerability. Remediation is available by upgrading affected packages to version 0.9.1.

CVE-2024-52803 at a Glance

CVE Identifier CVE-2024-52803
GHSA Identifier GHSA-hj3w-wrh4-44vp
Severity HIGH
CVSS v3.1 Base Score 7.5
CWE Classification CWE-78: Improper Neutralization of Special Elements used in an OS Command ('OS Command Injection')
Affected Product Llamafactory
Package Ecosystem PyPI
Published Date
Last Updated

Security Impact

CVSS: 7.5

The security impact metrics below are parsed directly from the CVSS v3.1 vector string (CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:N/A:N).

Attack Vector Network (AV:N)
Attack Complexity Low (AC:L)
Privileges Required None (PR:N)
User Interaction None (UI:N)
Scope Unchanged (S:U)
Confidentiality High (C:H)
Integrity None (I:N)
Availability None (A:N)

How to Fix CVE-2024-52803

To remediate this vulnerability, upgrade all affected Llamafactory dependencies to a patched release. Both direct and transitive dependencies should be checked, as frameworks or security libraries may pull in vulnerable versions transitively.

llamafactory fixed: 0.9.1
pip install llamafactory>=0.9.1
Verify Direct and Transitive Dependencies

Run the following command in your project directory to verify that all resolved dependencies use the patched release:

pip show llamafactory

How to Check if You Are Affected

To determine whether your Python environment is affected by CVE-2024-52803, inspect your installed packages using pip:

pip show llamafactory

Analyze the terminal output to locate any version of Llamafactory in your dependency tree. If any resolved version matches the vulnerable ranges listed in the table below (for example, versions earlier than 0.9.1), your application is affected.

Note: The pip show llamafactory command checks currently installed packages in your active Python environment.

Technical Details

Vulnerability Mechanism & Technical Analysis

This vulnerability is classified under CWE-78: Improper Neutralization of Special Elements used in an OS Command ('OS Command Injection'), where the product constructs all or part of an os command using externally-influenced input from an upstream component, but it does not neutralize or incorrectly neutralizes special elements that could modify the intended os command when it is sent to a downstream component. In Llamafactory, software state validation or input boundaries are circumvented during processing. Review the official source advisory statement below for specific affected components, classes, and reproduction details.

Source Advisory Statement

## Summary A critical remote OS command injection vulnerability has been identified in the Llama Factory training process. This vulnerability arises from improper handling of user input, allowing malicious actors to execute arbitrary OS commands on the host system. The issue is caused by insecure usage of the `Popen` function with `shell=True`, coupled with unsanitized user input. Immediate remediation is required to mitigate the risk. ## Affected Version Llama Factory versions **<=0.9.0** are affected by this vulnerability. ## Impact Exploitation of this vulnerability allows attackers to: 1. Execute arbitrary OS commands on the server. 2. Potentially compromise sensitive data or escalate privileges. 3. Deploy malware or create persistent backdoors in the system. This significantly increases the risk of data breaches and operational disruption. ## Root Cause The vulnerability originates from the training process where the `output_dir` value, obtained from the user input, is injected into the popen function without any sanitization. Furthermore, popen is invoked in a unsafe way by enabling the interact shell (`shell=True`), leading to remote OS command injection vulnerability. Vulnerable snippet: ```python # https://github.com/hiyouga/LLaMA-Factory/blob/bd639a137e6f46e1a0005cc91572f5f1ec894f74/src/llamafactory/webui/runner.py#L304-L323 def _launch(self, data: Dict["Component", Any], do_train: bool) -> Generator[Dict["Component", Any], None, None]: ... args = self._parse_train_args(data) if do_train else self._parse_eval_args(data) ... self.trainer = Popen(f"llamafactory-cli train {save_cmd(args)}", env=env, shell=True) yield from self.monitor() ``` ## Proof of Concept (PoC) ### Steps to Reproduce - Deploy llama factory - Execute the exploitation script from: https://gist.github.com/superboy-zjc/f2d2b93ae511c445ba97e144b70e534d ```bash python3 llama-factory-rce.py --url http://127.0.0.1:7861 --cmd "curl XXX" --trace ``` ![llama-factory-rce](https://api.2h0ng.wiki:443/noteimages/2024/11/21/00-33-37-5347128a141f8765e7d218c89d94162a.gif) Bad actors are able to execute any OS command as they want. ## Remediation Recommendations **Avoid using `shell=True` in `Popen`.** - Instead, pass the command and its arguments as a list. This prevents user inputs from being executed as part of a shell command. ```python cmd = [ "llamafactory-cli", "train", *save_cmd(args).split(), ] self.trainer = Popen(cmd, env=env) ```

CVE-2024-52803 Affected Llamafactory Versions

1 component(s)
Fixed Versions:
0.9.1
Product / Package Ecosystem Affected Versions Fixed Version
llamafactory
PyPI
< 0.9.1
0.9.1

References

Frequently Asked Questions

What is CVE-2024-52803?
CVE-2024-52803 is a security vulnerability in Llamafactory involving LLama Factory Remote OS Command Injection Vulnerability under CWE-78: Improper Neutralization of Special Elements used in an OS Command ('OS Command Injection').
Which Llamafactory versions are affected?
Affected versions include releases prior to 0.9.1 across affected packages: llamafactory.
What version fixes CVE-2024-52803?
Upgrading to 0.9.1 (or the latest patched release for your respective branch) resolves the vulnerability.
Is llamafactory affected?
Yes, llamafactory is affected in releases prior to 0.9.1. Upgrade your dependency to 0.9.1 or later.
What is the CVSS score of CVE-2024-52803?
CVE-2024-52803 has a CVSS v3.1 base score of 7.5 (HIGH) with vector string CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:N/A:N.
What CWE is associated with CVE-2024-52803?
CVE-2024-52803 is classified under CWE-78: Improper Neutralization of Special Elements used in an OS Command ('OS Command Injection') (The product constructs all or part of an OS command using externally-influenced input from an upstream component, but it does not neutralize or incorrectly neutralizes special elements that could modify the intended OS command when it is sent to a downstream component.).
How can I check PyPI dependencies for CVE-2024-52803?
Run the dependency tree command: 'pip show llamafactory'. This command lists direct and transitive dependencies in your build configuration so you can verify if a vulnerable version is resolved.
Machine-readable OSV JSON OSV v1.5 Schema
Normalized schema output
{
  "schema_version" : "1.4.0",
  "id" : "GHSA-hj3w-wrh4-44vp",
  "modified" : "2025-08-27T18:40:58Z",
  "published" : "2024-11-21T22:20:45Z",
  "aliases" : [ "CVE-2024-52803" ],
  "summary" : "LLama Factory Remote OS Command Injection Vulnerability",
  "details" : "## Summary\n\nA critical remote OS command injection vulnerability has been identified in the Llama Factory training process. This vulnerability arises from improper handling of user input, allowing malicious actors to execute arbitrary OS commands on the host system. The issue is caused by insecure usage of the `Popen` function with `shell=True`, coupled with unsanitized user input. Immediate remediation is required to mitigate the risk.\n\n## Affected Version\n\nLlama Factory versions **<=0.9.0** are affected by this vulnerability.\n\n## Impact\n\nExploitation of this vulnerability allows attackers to:\n\n1. Execute arbitrary OS commands on the server.\n2. Potentially compromise sensitive data or escalate privileges.\n3. Deploy malware or create persistent backdoors in the system.\n\nThis significantly increases the risk of data breaches and operational disruption.\n\n## Root Cause\n\nThe vulnerability originates from the training process where the `output_dir` value, obtained from the user input, is injected into the popen function without any sanitization. Furthermore, popen is invoked in a unsafe way by enabling the interact shell (`shell=True`), leading to remote OS command injection vulnerability.\n\nVulnerable snippet: \n\n```python\n# https://github.com/hiyouga/LLaMA-Factory/blob/bd639a137e6f46e1a0005cc91572f5f1ec894f74/src/llamafactory/webui/runner.py#L304-L323\ndef _launch(self, data: Dict[\"Component\", Any], do_train: bool) -> Generator[Dict[\"Component\", Any], None, None]:\n\t\t\t\t...\n        args = self._parse_train_args(data) if do_train else self._parse_eval_args(data)\n\t\t\t\t...\n        self.trainer = Popen(f\"llamafactory-cli train {save_cmd(args)}\", env=env, shell=True)\n        yield from self.monitor()\n```\n\n## Proof of Concept (PoC)\n\n### Steps to Reproduce\n\n- Deploy llama factory\n\n- Execute the exploitation script from: https://gist.github.com/superboy-zjc/f2d2b93ae511c445ba97e144b70e534d\n\n  ```bash\n  python3 llama-factory-rce.py --url http://127.0.0.1:7861 --cmd \"curl XXX\" --trace\n  ```\n\n![llama-factory-rce](https://api.2h0ng.wiki:443/noteimages/2024/11/21/00-33-37-5347128a141f8765e7d218c89d94162a.gif)\n\nBad actors are able to execute any OS command as they want.\n\n## Remediation Recommendations\n\n**Avoid using `shell=True` in `Popen`.**\n\n- Instead, pass the command and its arguments as a list. This prevents user inputs from being executed as part of a shell command.\n\n```python\ncmd = [\n    \"llamafactory-cli\",\n    \"train\", \n  \t*save_cmd(args).split(),\n]\nself.trainer = Popen(cmd, env=env)\n```",
  "severity" : [ {
    "type" : "CVSS_V3",
    "score" : "CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:N/A:N"
  } ],
  "affected" : [ {
    "package" : {
      "ecosystem" : "PyPI",
      "name" : "llamafactory"
    },
    "ranges" : [ {
      "type" : "ECOSYSTEM",
      "events" : [ {
        "introduced" : "0"
      }, {
        "fixed" : "0.9.1"
      } ]
    } ],
    "database_specific" : {
      "last_known_affected_version_range" : "<= 0.9.0"
    }
  } ],
  "references" : [ {
    "type" : "WEB",
    "url" : "https://github.com/hiyouga/LLaMA-Factory/security/advisories/GHSA-hj3w-wrh4-44vp"
  }, {
    "type" : "ADVISORY",
    "url" : "https://nvd.nist.gov/vuln/detail/CVE-2024-52803"
  }, {
    "type" : "WEB",
    "url" : "https://github.com/hiyouga/LLaMA-Factory/commit/b3aa80d54a67da45e9e237e349486fb9c162b2ac"
  }, {
    "type" : "WEB",
    "url" : "https://gist.github.com/superboy-zjc/f2d2b93ae511c445ba97e144b70e534d"
  }, {
    "type" : "PACKAGE",
    "url" : "https://github.com/hiyouga/LLaMA-Factory"
  } ],
  "database_specific" : {
    "cwe_ids" : [ "CWE-78", "CWE-79" ],
    "severity" : "HIGH",
    "github_reviewed" : true,
    "github_reviewed_at" : "2024-11-21T22:20:45Z",
    "nvd_published_at" : "2024-11-21T17:15:24Z"
  }
}