{"id":1350,"cve":"CVE-2025-2999","cve_url":"https:\/\/www.cvedetails.com\/cve\/CVE-2025-2999\/","cwe":"CWE-119","cwe_number":119,"cwe_url":"https:\/\/cwe.mitre.org\/data\/definitions\/119","cvss_score":4.8,"published":"2025-03-31","owasp":"A04 Insecure Design","odc":"Algorithm","cdc":"Incorrect Functionality","category":"Memory Corruption","subcategory":"Buffer Overflows","scope":"Local","impact":"Arbitrary Code Execution","summary":"PyTorch 2.6.0 unpack_sequence mem corruption vuln; local access required.","permalink":"https:\/\/netpack.pt\/vaitp\/vulnerability\/CVE-2025-2999\/","description":"A vulnerability was found in PyTorch 2.6.0. It has been rated as critical. Affected by this issue is the function torch.nn.utils.rnn.unpack_sequence. The manipulation leads to memory corruption. Attacking locally is a requirement. The exploit has been disclosed to the public and may be used.","solution":"Upgrade to PyTorch version 2.6.1 or later.","affected_component":"PyTorch","fixed_by_upgrading":true,"vulnerable_code":"import torch\nimport torch.nn as nn\nfrom torch.nn.utils.rnn import pack_sequence, unpack_sequence\n\ndef demonstrate_potential_overflow(lengths):\n    \"\"\"\n    A hypothetical demonstration of a potential memory corruption issue.\n    This is NOT the actual exploit for CVE-2025-2999.\n\n    Args:\n        lengths: A list of sequence lengths.  Large values could hypothetically\n                 cause an issue.\n    \"\"\"\n\n    sequences = [torch.randn(length, 10) for length in lengths] # example sequences\n    packed_sequence = pack_sequence(sequences, enforce_sorted=False)\n\n    try:\n        unpacked_sequence, output_lengths = unpack_sequence(packed_sequence)  #This is where the problem was.\n\n        print(\"Unpacking successful.\")\n    except Exception as e:\n        print(f\"Unpacking failed: {e}\")\n\n\nif __name__ == '__main__':\n    # Experiment with different sequence lengths.  A very large length\n    # MIGHT (hypothetically) trigger a memory issue.  Again, this is\n    # for demonstration of a potential type of problem, not the actual CVE.\n    demonstrate_potential_overflow([10, 20, 1000]) #Normal lengths\n    demonstrate_potential_overflow([10, 20, 2**20]) # A Very large length to demonstrate the potential overflow problem.","patched_code":"# Disclaimer: This code is a *hypothetical* example and may not perfectly represent the actual CVE-2025-2999 fix.  It aims to illustrate a potential type of fix for memory corruption in sequence unpacking, given the information available about the vulnerability. Actual code could be different.\n\ndef unpack_sequence_fixed(packed_sequence, enforce_sorted=True):\n    \"\"\"\n    Unpacks a packed sequence into a padded tensor.\n\n    Args:\n        packed_sequence: A PackedSequence object.\n        enforce_sorted: If True, raise an error if the input sequence is not sorted.\n\n    Returns:\n        A tuple containing:\n        - A padded tensor of shape (T, B, *), where T is the length of the longest sequence,\n          B is the batch size, and * is the input_size.\n        - A LongTensor of shape (B,) containing the lengths of the sequences in the batch.\n    \"\"\"\n\n    data = packed_sequence.data\n    batch_sizes = packed_sequence.batch_sizes\n    sorted_indices = packed_sequence.sorted_indices\n    unsorted_indices = packed_sequence.unsorted_indices\n\n    batch_size = packed_sequence.batch_sizes[0].item() #fixed bug: convert numpy int to python int\n\n    if enforce_sorted and sorted_indices is None:\n        raise ValueError(\"Input sequence must be sorted if enforce_sorted is True.\")\n\n    max_seq_len = batch_sizes.size(0)\n\n    # Pre-allocate the padded tensor.  Crucial to prevent out-of-bounds writes.\n    padded_tensor = torch.zeros((max_seq_len, batch_size, data.size(1)), dtype=data.dtype, device=data.device)\n\n    # Iterate and unpack.  Adding bounds checks to prevent memory corruption.\n    current_index = 0\n    for t in range(max_seq_len):\n        current_batch_size = batch_sizes[t].item() #fixed bug: convert numpy int to python int\n\n        # IMPORTANT: Check if the current batch size is valid. If not, continue to prevent out-of-bounds write.\n        if current_batch_size > batch_size:\n            print(f\"Warning: batch_size[{t}] is {current_batch_size}, exceeding maximum batch size of {batch_size}. Skipping timestep.\")\n            continue\n\n        padded_tensor[t, :current_batch_size] = data[current_index:current_index + current_batch_size]\n        current_index += current_batch_size\n\n        #VERY IMPORTANT: Prevent reading past the end of the data.\n        if current_index > data.shape[0]:\n            print(f\"Warning: current index exceeded data size. Stopping at timestep {t}.\")\n            break\n\n    lengths = batch_sizes_to_lengths(batch_sizes) #Fixed bug: proper batch sizes to length convertion.\n\n    if unsorted_indices is not None:\n        padded_tensor = padded_tensor[:, unsorted_indices]\n        lengths = lengths[unsorted_indices]\n\n    return padded_tensor, lengths\n\ndef batch_sizes_to_lengths(batch_sizes):\n  \"\"\"\n  Convert batch_sizes from PackedSequence to lengths.\n\n  Args:\n    batch_sizes: A LongTensor of shape (T,) containing the batch sizes at each time step.\n\n  Returns:\n    A LongTensor of shape (B,) containing the lengths of the sequences in the batch.\n  \"\"\"\n  lengths = torch.zeros(batch_sizes[0].item(), dtype=torch.long, device=batch_sizes.device) #fixed bug: proper batch sizes to length convertion.\n  for i, batch_size in enumerate(batch_sizes):\n    lengths[:batch_size] += 1\n  return lengths\n\nimport torch\nfrom torch.nn.utils.rnn import PackedSequence\nif __name__ == '__main__':\n    # Example usage (illustrative)\n    # Create a dummy PackedSequence\n    data = torch.randn(10, 5)  # Example data\n    batch_sizes = torch.tensor([5, 4, 3, 2, 1])  # Example batch sizes\n    packed_sequence = PackedSequence(data, batch_sizes)\n\n    # Unpack the sequence\n    padded_tensor, lengths = unpack_sequence_fixed(packed_sequence)\n\n    print(\"Padded Tensor shape:\", padded_tensor.shape)\n    print(\"Lengths:\", lengths)","payload_needed":true,"payload":"```python\nimport torch\nimport torch.nn.utils.rnn as rnn_utils\n\n# Craft a malicious sequence with carefully chosen lengths to trigger the vulnerability.\n# This is a simplified example, the precise values may need adjustment depending on the exact memory corruption details.\n\ndata = torch.randn(100, 20)  # Example data\nsequence = [data[:i] for i in range(1, 101)]  # Create a sequence of varying lengths\n\n# Intentionally create an invalid lengths tensor likely to cause out-of-bounds access\nlengths = torch.tensor([i for i in range(1, 101)])\nlengths[50] = 1000 # Craft an invalid length\n\n# Pack the sequence with the malicious lengths\npacked_sequence = rnn_utils.pack_padded_sequence(sequence, lengths, batch_first=True, enforce_sorted=False)\n\n# Unpack the sequence, triggering the vulnerability\nunpacked_sequence, unpacked_lengths = rnn_utils.unpack_sequence(packed_sequence)\n```","payload_status":"available","published_fix":[],"references":null}