Skip to content

Commit 6d6e511

Browse files
[AUTO-CHERRYPICK] Patch pytorch for CVE-2025-32434, CVE-2025-3730 [Critical] - branch main (#13592)
Co-authored-by: Kanishk Bansal <103916909+Kanishk-Bansal@users.noreply.github.com>
1 parent 1993f1e commit 6d6e511

3 files changed

Lines changed: 122 additions & 2 deletions

File tree

SPECS/pytorch/CVE-2025-32434.patch

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
From c27a81c2aee58626189631800841af0cc44e0873 Mon Sep 17 00:00:00 2001
2+
From: Kanishk-Bansal <kbkanishk975@gmail.com>
3+
Date: Wed, 23 Apr 2025 06:43:41 +0000
4+
Subject: [PATCH] Address CVE-2025-32434
5+
6+
Upstream Patch Reference : https://github.com/pytorch/pytorch/commit/8d4b8a920a2172523deb95bf20e8e52d50649c04
7+
8+
Signed-off-by: Kanishk-Bansal <kbkanishk975@gmail.com>
9+
---
10+
test/test_serialization.py | 6 +++++-
11+
torch/serialization.py | 17 ++++++++++++-----
12+
2 files changed, 17 insertions(+), 6 deletions(-)
13+
14+
diff --git a/test/test_serialization.py b/test/test_serialization.py
15+
index 9b9a7133..593f802a 100644
16+
--- a/test/test_serialization.py
17+
+++ b/test/test_serialization.py
18+
@@ -404,7 +404,11 @@ class SerializationMixin:
19+
b += [a[0].storage()]
20+
b += [a[0].reshape(-1)[1:4].clone().storage()]
21+
path = download_file('https://download.pytorch.org/test_data/legacy_serialized.pt')
22+
- c = torch.load(path, weights_only=weights_only)
23+
+ if weights_only:
24+
+ with self.assertRaisesRegex(RuntimeError,
25+
+ "Cannot use ``weights_only=True`` with files saved in the legacy .tar format."):
26+
+ c = torch.load(path, weights_only=weights_only)
27+
+ c = torch.load(path, weights_only=False)
28+
self.assertEqual(b, c, atol=0, rtol=0)
29+
self.assertTrue(isinstance(c[0], torch.FloatTensor))
30+
self.assertTrue(isinstance(c[1], torch.FloatTensor))
31+
diff --git a/torch/serialization.py b/torch/serialization.py
32+
index 83f6fa27..21ba1d07 100644
33+
--- a/torch/serialization.py
34+
+++ b/torch/serialization.py
35+
@@ -33,6 +33,13 @@ STORAGE_KEY_SEPARATOR = ','
36+
FILE_LIKE: TypeAlias = Union[str, os.PathLike, BinaryIO, IO[bytes]]
37+
MAP_LOCATION: TypeAlias = Optional[Union[Callable[[torch.Tensor, str], torch.Tensor], torch.device, str, Dict[str, str]]]
38+
39+
+UNSAFE_MESSAGE = (
40+
+ "In PyTorch 2.6, we changed the default value of the `weights_only` argument in `torch.load` "
41+
+ "from `False` to `True`. Re-running `torch.load` with `weights_only` set to `False` will likely succeed, "
42+
+ "but it can result in arbitrary code execution. Do it only if you got the file from a "
43+
+ "trusted source."
44+
+ )
45+
+
46+
__all__ = [
47+
'SourceChangeWarning',
48+
'mkdtemp',
49+
@@ -767,11 +774,6 @@ def load(
50+
>>> torch.load('module.pt', encoding='ascii')
51+
"""
52+
torch._C._log_api_usage_once("torch.load")
53+
- UNSAFE_MESSAGE = (
54+
- "Weights only load failed. Re-running `torch.load` with `weights_only` set to `False`"
55+
- " will likely succeed, but it can result in arbitrary code execution."
56+
- "Do it only if you get the file from a trusted source. WeightsUnpickler error: "
57+
- )
58+
# Add ability to force safe only weight loads via environment variable
59+
if os.getenv("TORCH_FORCE_WEIGHTS_ONLY_LOAD", "0").lower() in ['1', 'y', 'yes', 'true']:
60+
weights_only = True
61+
@@ -900,6 +902,11 @@ def _legacy_load(f, map_location, pickle_module, **pickle_load_args):
62+
63+
with closing(tarfile.open(fileobj=f, mode='r:', format=tarfile.PAX_FORMAT)) as tar, \
64+
mkdtemp() as tmpdir:
65+
+ if pickle_module is _weights_only_unpickler:
66+
+ raise RuntimeError(
67+
+ "Cannot use ``weights_only=True`` with files saved in the "
68+
+ "legacy .tar format. " + UNSAFE_MESSAGE
69+
+ )
70+
71+
tar.extract('storages', path=tmpdir)
72+
with open(os.path.join(tmpdir, 'storages'), 'rb', 0) as f:
73+
--
74+
2.45.2
75+

SPECS/pytorch/CVE-2025-3730.patch

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
From ee301f0a1132fa9bd7e3223fc64aa282eca12734 Mon Sep 17 00:00:00 2001
2+
From: Kanishk-Bansal <kbkanishk975@gmail.com>
3+
Date: Wed, 23 Apr 2025 06:37:47 +0000
4+
Subject: [PATCH] Address CVE-2025-3730
5+
6+
Upstream Patch Reference : https://github.com/timocafe/pytorch/commit/46fc5d8e360127361211cb237d5f9eef0223e567
7+
8+
Signed-off-by: Kanishk-Bansal <kbkanishk975@gmail.com>
9+
---
10+
aten/src/ATen/native/LossCTC.cpp | 1 +
11+
aten/src/ATen/native/cuda/LossCTC.cu | 1 +
12+
2 files changed, 2 insertions(+)
13+
14+
diff --git a/aten/src/ATen/native/LossCTC.cpp b/aten/src/ATen/native/LossCTC.cpp
15+
index 98733364..118cb467 100644
16+
--- a/aten/src/ATen/native/LossCTC.cpp
17+
+++ b/aten/src/ATen/native/LossCTC.cpp
18+
@@ -59,6 +59,7 @@ static inline int64_t get_target_prime(target_t* target, int64_t offset, int64_t
19+
// the alphas from the user by only returning the loss.
20+
template<typename scalar_t, ScalarType target_scalar_type>
21+
std::tuple<Tensor, Tensor> ctc_loss_cpu_template(const Tensor& log_probs, const Tensor& targets, IntArrayRef input_lengths, IntArrayRef target_lengths, int64_t BLANK) {
22+
+ TORCH_CHECK(log_probs.numel() > 0, "log_probs tensor must not be empty");
23+
// log_probs: input_len x batch_size x num_labels
24+
// targets [int64]: batch_size x target_length OR sum(target_lengths)
25+
constexpr scalar_t neginf = -std::numeric_limits<scalar_t>::infinity();
26+
diff --git a/aten/src/ATen/native/cuda/LossCTC.cu b/aten/src/ATen/native/cuda/LossCTC.cu
27+
index bb70b831..3c862993 100644
28+
--- a/aten/src/ATen/native/cuda/LossCTC.cu
29+
+++ b/aten/src/ATen/native/cuda/LossCTC.cu
30+
@@ -211,6 +211,7 @@ ctc_loss_log_alpha_gpu_kernel(scalar_t* __restrict__ log_alpha_data,
31+
// backward. The dispatch function will only return the loss.
32+
template<typename scalar_t, ScalarType target_scalar_type>
33+
std::tuple<Tensor, Tensor> ctc_loss_gpu_template(const Tensor& log_probs, const Tensor& targets, IntArrayRef input_lengths, IntArrayRef target_lengths, int64_t BLANK) {
34+
+ TORCH_CHECK(log_probs.numel() > 0, "log_probs tensor must not be empty");
35+
// log_probs: input_len x batch_size x num_labels
36+
// targets [int64]: batch_size x target_length OR sum(target_lengths)
37+
CheckedFrom c = "ctc_loss_gpu";
38+
--
39+
2.45.2
40+

SPECS/pytorch/pytorch.spec

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Summary: Tensors and Dynamic neural networks in Python with strong GPU acceleration.
33
Name: pytorch
44
Version: 2.0.0
5-
Release: 7%{?dist}
5+
Release: 8%{?dist}
66
License: BSD-3-Clause
77
Vendor: Microsoft Corporation
88
Distribution: Mariner
@@ -16,7 +16,9 @@ Patch1: CVE-2024-31583.patch
1616
Patch2: CVE-2024-27319.patch
1717
Patch3: CVE-2024-31584.patch
1818
Patch4: CVE-2024-27318.patch
19-
Patch5: CVE-2022-1941.patch
19+
Patch5: CVE-2022-1941.patch
20+
Patch6: CVE-2025-32434.patch
21+
Patch7: CVE-2025-3730.patch
2022

2123
BuildRequires: cmake
2224
BuildRequires: gcc
@@ -89,6 +91,9 @@ cp -arf docs %{buildroot}/%{_pkgdocdir}
8991
%{_docdir}/*
9092

9193
%changelog
94+
* Wed Apr 23 2025 Kanishk Bansal <kanbansal@microsoft.com> - 2.0.0-8
95+
- Patch CVE-2025-32434, CVE-2025-3730
96+
9297
* Tue Dec 10 2024 Bhagyashri Pathak <bhapathak@microsoft.com> - 2.0.0-7
9398
- patch CVE-2022-1941
9499

0 commit comments

Comments
 (0)