Skip to content

Commit 7314156

Browse files
[AUTO-CHERRYPICK] pytorch: fix CVE-2024-5187 - branch 3.0-dev (#11042)
Co-authored-by: SeanDougherty <sdougherty@microsoft.com>
1 parent 0134a6f commit 7314156

4 files changed

Lines changed: 96 additions & 111 deletions

File tree

SPECS/pytorch/CVE-2024-5187.patch

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
From a8c49a5fac46df180ba95810dcbb56c00dbd9c76 Mon Sep 17 00:00:00 2001
2+
From: sunflowersxu <166728538+sunflowersxu@users.noreply.github.com>
3+
Date: Thu, 13 Jun 2024 01:47:14 +0800
4+
Subject: [PATCH] Mitigate tarball directory traversal risks (#6164)
5+
6+
Hi, this pr is cleaner version than #6145
7+
8+
Signed-off-by: sunriseXu <15927176697@163.com>
9+
Co-authored-by: sunriseXu <15927176697@163.com>
10+
Co-authored-by: Justin Chu <justinchuby@users.noreply.github.com>
11+
---
12+
third_party/onnx/onnx/hub.py | 43 +++++++++++++++++++++++++++++++++++-
13+
1 file changed, 42 insertions(+), 1 deletion(-)
14+
15+
diff --git a/third_party/onnx/onnx/hub.py b/third_party/onnx/onnx/hub.py
16+
index e5ca9e2c..dc888742 100644
17+
--- a/third_party/onnx/onnx/hub.py
18+
+++ b/third_party/onnx/onnx/hub.py
19+
@@ -271,6 +271,35 @@ def load(
20+
return onnx.load(cast(IO[bytes], BytesIO(model_bytes)))
21+
22+
23+
+def _tar_members_filter(tar: tarfile.TarFile, base: str) -> list[tarfile.TarInfo]:
24+
+ """Check that the content of ``tar`` will be extracted safely
25+
+
26+
+ Args:
27+
+ tar: The tarball file
28+
+ base: The directory where the tarball will be extracted
29+
+
30+
+ Returns:
31+
+ list of tarball members
32+
+ """
33+
+ result = []
34+
+ for member in tar:
35+
+ member_path = os.path.join(base, member.name)
36+
+ abs_base = os.path.abspath(base)
37+
+ abs_member = os.path.abspath(member_path)
38+
+ if not abs_member.startswith(abs_base):
39+
+ raise RuntimeError(
40+
+ f"The tarball member {member_path} in downloading model contains "
41+
+ f"directory traversal sequence which may contain harmful payload."
42+
+ )
43+
+ elif member.issym() or member.islnk():
44+
+ raise RuntimeError(
45+
+ f"The tarball member {member_path} in downloading model contains "
46+
+ f"symbolic links which may contain harmful payload."
47+
+ )
48+
+ result.append(member)
49+
+ return result
50+
+
51+
+
52+
def download_model_with_test_data(
53+
model: str,
54+
repo: str = "onnx/models:main",
55+
@@ -280,6 +309,7 @@ def download_model_with_test_data(
56+
) -> Optional[str]:
57+
"""
58+
Downloads a model along with test data by name from the onnx model hub and returns the directory to which the files have been extracted.
59+
+ Users are responsible for making sure the model comes from a trusted source, and the data is safe to be extracted.
60+
61+
:param model: The name of the onnx model in the manifest. This field is case-sensitive
62+
:param repo: The location of the model repo in format "user/repo[:branch]".
63+
@@ -342,7 +372,18 @@ def download_model_with_test_data(
64+
local_model_with_data_dir_path = local_model_with_data_path[
65+
0 : len(local_model_with_data_path) - 7
66+
]
67+
- model_with_data_zipped.extractall(local_model_with_data_dir_path)
68+
+ # Mitigate tarball directory traversal risks
69+
+ if hasattr(tarfile, "data_filter"):
70+
+ model_with_data_zipped.extractall(
71+
+ path=local_model_with_data_dir_path, filter="data"
72+
+ )
73+
+ else:
74+
+ model_with_data_zipped.extractall(
75+
+ path=local_model_with_data_dir_path,
76+
+ members=_tar_members_filter(
77+
+ model_with_data_zipped, local_model_with_data_dir_path
78+
+ ),
79+
+ )
80+
model_with_data_path = (
81+
local_model_with_data_dir_path
82+
+ "/"
83+
--
84+
2.39.4
85+

SPECS/pytorch/generate_source_tarball.sh

Lines changed: 0 additions & 102 deletions
This file was deleted.
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{
2-
"Signatures": {
3-
"pytorch-2.2.2-submodules.tar.gz": "74d91f9cbba81848a0c07c718810889c46ca2d24a198444d8e3caca13eea9ffc",
4-
"pytorch-2.2.2.tar.gz": "57a1136095bdfe769acb87876dce77212da2c995c61957a67a1f16172d235d17"
5-
}
6-
}
2+
"Signatures": {
3+
"pytorch-2.2.2.tar.gz": "57a1136095bdfe769acb87876dce77212da2c995c61957a67a1f16172d235d17"
4+
}
5+
}

SPECS/pytorch/pytorch.spec

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@
22
Summary: Tensors and Dynamic neural networks in Python with strong GPU acceleration.
33
Name: pytorch
44
Version: 2.2.2
5-
Release: 2%{?dist}
5+
Release: 3%{?dist}
66
License: BSD-3-Clause
77
Vendor: Microsoft Corporation
88
Distribution: Azure Linux
99
Group: Development/Languages/Python
1010
URL: https://pytorch.org/
1111
Source0: https://github.com/pytorch/pytorch/releases/download/v%{version}/%{name}-v%{version}.tar.gz#/%{name}-%{version}.tar.gz
12-
# Use the generate_source_tarball.sh script to create a tarball of submodules during version updates.
13-
Source1: %{name}-%{version}-submodules.tar.gz
1412
BuildRequires: cmake
1513
BuildRequires: gcc
1614
BuildRequires: gcc-c++
@@ -26,6 +24,7 @@ BuildRequires: python3-six
2624

2725
Patch1: CVE-2024-27318.patch
2826
Patch2: CVE-2022-1941.patch
27+
Patch3: CVE-2024-5187.patch
2928

3029
%description
3130
PyTorch is a Python package that provides two high-level features:
@@ -59,7 +58,7 @@ PyTorch is a Python package that provides two high-level features:
5958
You can reuse your favorite Python packages such as NumPy, SciPy and Cython to extend PyTorch when needed.
6059

6160
%prep
62-
%autosetup -a 1 -p 1 -n %{name}-v%{version}
61+
%autosetup -p 1 -n %{name}-v%{version}
6362

6463
%build
6564
export USE_CUDA=0
@@ -87,6 +86,10 @@ cp -arf docs %{buildroot}/%{_pkgdocdir}
8786
%{_docdir}/*
8887

8988
%changelog
89+
* Tue Nov 12 2024 Sean Dougherty <sdougherty@microsoft.com> - 2.2.2-3
90+
- Add patch to address CVE-2024-5187
91+
- Remove unnecessary double vendoring of the third_party directory. Doubling happens because the contents of the submodule tarball are pulled directly from the original source tarball and then re-uploaded as this "submodule tarball".
92+
9093
* Tue Sep 17 2024 Archana Choudhary <archana1@microsoft.com> - 2.2.2-2
9194
- patch for CVE-2024-27318, CVE-2022-1941
9295

0 commit comments

Comments
 (0)