CVE-2025-46148
Executive Summary
CVE-2025-46148 is a computational correctness vulnerability in PyTorch, affecting versions through 2.6.0. The flaw resides in the nn.PairwiseDistance(p=2) module when executed in eager mode, causing it to produce incorrect results for the L2 (Euclidean) distance calculation. This is not a memory corruption or remote code execution issue, but a logic error that leads to inaccurate pairwise distance computations. For applications relying on precise distance metrics—such as clustering, similarity search, or anomaly detection—this can result in flawed model outputs, degraded performance, or incorrect security decisions. The vulnerability has a CVSS score of 0.0, indicating no direct security impact, but its operational impact on data integrity and model reliability is significant.
This record — and 170,000+ others — are available via the ValtersIT API. One endpoint replaces five: NVD + GitHub CVE + CISA KEV + EPSS + Nuclei + Exploit-DB, unified.
No signup required
Supply Chain & Package Security
PyTorch is a widely used open-source machine learning framework distributed via PyPI, Conda, and source. This vulnerability affects all users of PyTorch versions up to 2.6.0 who utilize nn.PairwiseDistance in eager mode. Since the issue is in the core library, any downstream package or application that depends on PyTorch and uses this module is indirectly affected. Supply chain risk is moderate: while no malicious code is injected, the incorrect computation can propagate errors through ML pipelines, affecting model training, inference, and decision-making. Users should verify their usage of nn.PairwiseDistance and consider patching or replacing the function until an official fix is released.
CVSS Breakdown & Threat Level
- CVSS Score: 0.0 (None)
- Vector: UNKNOWN
- Attack Vector: N/A (no remote exploitation)
- Attack Complexity: N/A
- Privileges Required: N/A
- User Interaction: N/A
- Scope: N/A
- Confidentiality Impact: N/A
- Integrity Impact: N/A
- Availability Impact: N/A
Threat Level: Low (Operational). While the CVSS score is 0.0, the vulnerability has real-world consequences for data integrity and model accuracy. It is not exploitable for remote code execution or data theft, but it can cause silent data corruption in ML workflows.
Deep Dive & Mechanics
The nn.PairwiseDistance module computes pairwise distances between two sets of vectors. When p=2, it should compute the Euclidean distance: sqrt(sum((x - y)^2)). In PyTorch versions through 2.6.0, when running in eager mode (as opposed to JIT or TorchScript), the computation produces incorrect results due to a bug in the internal implementation.
The root cause is a logic error in the eager-mode kernel for nn.PairwiseDistance. Specifically, the function may incorrectly handle broadcasting or reduction operations, leading to off-by-one errors or incorrect axis summation. This results in distance values that are either too large, too small, or NaN in some edge cases. The bug does not affect JIT-compiled or TorchScript paths, only the default eager execution.
Technical Details:
- Affected Function:
torch.nn.PairwiseDistance(p=2) - Mode: Eager (default)
- Impact: Incorrect Euclidean distance computation
- Example: For two vectors
[1, 2, 3]and[4, 5, 6], the correct Euclidean distance issqrt(27) ≈ 5.196. The bug may produce a different value, e.g.,sqrt(9) = 3orNaN.
Vulnerable vs. Patched Code
Vulnerable Code (PyTorch <= 2.6.0, eager mode):
import torch
import torch.nn as nn
# Incorrect computation in eager mode
dist = nn.PairwiseDistance(p=2)
x = torch.tensor([[1.0, 2.0, 3.0]])
y = torch.tensor([[4.0, 5.0, 6.0]])
result = dist(x, y) # May produce incorrect value
Patched Code (Hypothetical fix):
import torch
import torch.nn as nn
# Correct computation using manual implementation or JIT
dist = nn.PairwiseDistance(p=2)
x = torch.tensor([[1.0, 2.0, 3.0]])
y = torch.tensor([[4.0, 5.0, 6.0]])
# Workaround: use torch.cdist for correct results
result = torch.cdist(x, y, p=2) # Correct Euclidean distance
Workaround: Until an official patch is released, users can replace nn.PairwiseDistance(p=2) with torch.cdist(x, y, p=2) or implement a manual Euclidean distance function.
Exploitation & Lab Setup
Red Team (Adversarial Simulation)
This vulnerability is not exploitable for remote code execution or privilege escalation. However, an adversary could leverage it to degrade ML model performance or cause incorrect outputs in security-critical applications (e.g., anomaly detection, facial recognition). The attack vector is limited to scenarios where the attacker can influence input data to a model using nn.PairwiseDistance.
Lab Setup:
- Install PyTorch 2.6.0:
pip install torch==2.6.0 - Create a script to demonstrate the bug:
import torch
import torch.nn as nn
# Demonstrate incorrect computation
dist = nn.PairwiseDistance(p=2)
x = torch.tensor([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
y = torch.tensor([[7.0, 8.0, 9.0], [10.0, 11.0, 12.0]])
result = dist(x, y)
print("Incorrect result:", result)
# Compare with correct computation
correct = torch.cdist(x, y, p=2)
print("Correct result:", correct)
- Observe the discrepancy. The bug may produce values that are off by a factor or completely wrong.
Blue Team (Defensive Detection)
Detection Strategy:
- Monitor for usage of
nn.PairwiseDistancein PyTorch codebases. - Alert on any model outputs that show unexpected distance values (e.g., NaN, negative distances).
- Use static analysis to flag calls to
nn.PairwiseDistance(p=2)in eager mode.
Mitigation:
- Replace
nn.PairwiseDistancewithtorch.cdistor a manual implementation. - Use JIT compilation (
torch.jit.script) to bypass the bug. - Update to a patched version when available.
Indicators of Compromise (IOCs)
- File Hashes: N/A (no malicious files)
- Network Indicators: N/A (no network exploitation)
- Behavioral Indicators:
- Model outputs containing NaN or infinite values for distance metrics.
- Unexpected clustering or similarity results in ML pipelines.
- Logs showing
nn.PairwiseDistanceusage in eager mode.
- Registry/Config: N/A
Detection Engineering
YARA Rule
rule Detect_PyTorch_PairwiseDistance_Bug {
meta:
description = "Detects usage of nn.PairwiseDistance in PyTorch code that may be affected by CVE-2025-46148"
author = "Valters IT"
date = "2025-04-10"
cve = "CVE-2025-46148"
strings:
$s1 = "nn.PairwiseDistance" ascii wide nocase
$s2 = "PairwiseDistance" ascii wide nocase
condition:
any of them
}
Sigma Rule
title: PyTorch PairwiseDistance Bug Usage
id: 7a8b9c0d-1e2f-3a4b-5c6d-7e8f9a0b1c2d
status: experimental
description: Detects usage of nn.PairwiseDistance in PyTorch code that may be affected by CVE-2025-46148
author: Valters IT
date: 2025-04-10
logsource:
category: process_creation
product: windows
detection:
selection:
Image|endswith: '\python.exe'
CommandLine|contains: 'nn.PairwiseDistance'
condition: selection
falsepositives:
- Legitimate usage of PairwiseDistance with p!=2 or in JIT mode
level: low
Suricata Rule
alert http any any -> any any (msg:"CVE-2025-46148 PyTorch PairwiseDistance Bug"; content:"nn.PairwiseDistance"; classtype:attempted-recon; sid:1000001; rev:1;)
Remediation & Mitigation
- Immediate Workaround: Replace
nn.PairwiseDistance(p=2)withtorch.cdist(x, y, p=2)or a manual Euclidean distance function. - Code Review: Audit all codebases using PyTorch for calls to
nn.PairwiseDistanceand replace them. - JIT Compilation: Use
torch.jit.scriptto compile models, as the bug only affects eager mode. - Monitor for Patches: Track PyTorch releases for an official fix (expected in version > 2.6.0).
- Validation: Add unit tests to verify distance computations against known correct values.
Long-term: Consider using alternative distance functions or libraries (e.g., SciPy) for critical applications until PyTorch releases a patch.
Valters IT
DevSecOps & Cyber Threat Intelligence
Report generated for CVE-2025-46148