{"id":"GHSA-hj3w-wrh4-44vp","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 **\u003c=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) -\u003e 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```","aliases":["CVE-2024-52803"],"modified":"2025-08-27T18:54:15.524345Z","published":"2024-11-21T22:20:45Z","database_specific":{"nvd_published_at":"2024-11-21T17:15:24Z","cwe_ids":["CWE-78","CWE-79"],"severity":"HIGH","github_reviewed_at":"2024-11-21T22:20:45Z","github_reviewed":true},"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"}],"affected":[{"package":{"name":"llamafactory","ecosystem":"PyPI","purl":"pkg:pypi/llamafactory"},"ranges":[{"type":"ECOSYSTEM","events":[{"introduced":"0"},{"fixed":"0.9.1"}]}],"versions":["0.7.1","0.8.0","0.8.1","0.8.2","0.8.3","0.9.0"],"database_specific":{"last_known_affected_version_range":"\u003c= 0.9.0","source":"https://github.com/github/advisory-database/blob/main/advisories/github-reviewed/2024/11/GHSA-hj3w-wrh4-44vp/GHSA-hj3w-wrh4-44vp.json"}}],"schema_version":"1.7.3","severity":[{"type":"CVSS_V3","score":"CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:N/A:N"}]}