已开启
fix CVE-2026-113717: verify seed wheels before caching, install with --no-index (openEuler-24.03-LTS-SP4) #80
ljtian创建于 2 天前
fix CVE-2026-113717: verify seed wheels before caching, install with --no-index (openEuler-24.03-LTS-SP4) #80
已开启
共 2 个文件变更+345-1
| @@ -0,0 +1,338 @@ | |||
| 1 | +From 7babec66e043e1cc8bc66869a063653898719d96 Mon Sep 17 00:00:00 2001 | ||
| 2 | +From: Bernát Gábor <gaborjbernat@gmail.com> | ||
| 3 | +Date: Sat, 17 Oct 2026 12:30:06 +0200 | ||
| 4 | +Subject: [PATCH] fix(seed): verify wheels before caching (#3430) | ||
| 5 | + | ||
| 6 | +Verify downloads in a private staging directory before adding them to the | ||
| 7 | +reusable wheel cache. Select the downloaded archive from that directory | ||
| 8 | +instead of pip's output. The pip seeder now uses the same download | ||
| 9 | +verification path and installs pinned versions from a temporary directory | ||
| 10 | +containing only the selected wheels, without another index lookup. Seeded | ||
| 11 | +packages retain version pins in pip freeze output. | ||
| 12 | + | ||
| 13 | +CVE-2026-113717 (GHSA-cx28-75q4-x26q, CWE-494): with `--seeder pip | ||
| 14 | +--download`, PipInvoke.get_pip_install_cmd() acquired wheels with | ||
| 15 | +download=False and left pip free to consult the configured index, so an | ||
| 16 | +index wheel carrying the same package and version as the selected wheel | ||
| 17 | +could be installed without virtualenv ever comparing its sha256 with the | ||
| 18 | +PyPI metadata (download_wheel()/verify_wheel_digest() are skipped). | ||
| 19 | + | ||
| 20 | +Backport of upstream commit 7babec66e043e1cc8bc66869a063653898719d96 | ||
| 21 | +(PR #3430, first released in virtualenv 21.14.9) to 20.26.6. The | ||
| 22 | +verification infrastructure was introduced in 21.7.12, so 20.x needs the | ||
| 23 | +same primitives added along with the fix: | ||
| 24 | + | ||
| 25 | +- periodic_update.py: add verify_wheel_digest(), UnverifiedWheelError and | ||
| 26 | + _pypi_sha256_for_wheel(); the PyPI JSON lookup helpers already exist in | ||
| 27 | + 20.x (they serve release-date metadata), so no new dependency is added. | ||
| 28 | +- acquire.py: download into a private staging directory inside to_folder, | ||
| 29 | + require exactly one archive, check its distribution/version against the | ||
| 30 | + request, verify the PyPI sha256 when the default index is in use, and | ||
| 31 | + only then move it into the cache. | ||
| 32 | +- pip_invoke.py: always pass --no-index, download through get_wheel() | ||
| 33 | + when --download is requested, and expose only the selected archives to | ||
| 34 | + pip through a private temporary find-links directory. | ||
| 35 | + | ||
| 36 | +Adapted to the 20.x code style only (no type annotations, module-level | ||
| 37 | +logging, `python -m pip` instead of the PIP_RUN trampoline); the selection | ||
| 38 | +and verification logic is the same as upstream. The PyPI digest lookup is | ||
| 39 | +performed only when none of PIP_INDEX_URL / PIP_EXTRA_INDEX_URL / PIP_INDEX | ||
| 40 | +is set, matching the upstream policy that explicit custom-index settings | ||
| 41 | +opt out of the comparison (a pip.conf index has the same effect and is not | ||
| 42 | +detected, as upstream documents). | ||
| 43 | + | ||
| 44 | +diff --git a/src/virtualenv/seed/embed/pip_invoke.py b/src/virtualenv/seed/embed/pip_invoke.py | ||
| 45 | +index 815753c..8ff779b 100644 | ||
| 46 | +--- a/src/virtualenv/seed/embed/pip_invoke.py | ||
| 47 | ++++ b/src/virtualenv/seed/embed/pip_invoke.py | ||
| 48 | +@@ -2,7 +2,10 @@ from __future__ import annotations | ||
| 49 | + | ||
| 50 | + import logging | ||
| 51 | + from contextlib import contextmanager | ||
| 52 | ++from pathlib import Path | ||
| 53 | ++from shutil import copyfile | ||
| 54 | + from subprocess import Popen | ||
| 55 | ++from tempfile import TemporaryDirectory | ||
| 56 | + | ||
| 57 | + from virtualenv.discovery.cached_py_info import LogCmd | ||
| 58 | + from virtualenv.seed.embed.base_embed import BaseEmbed | ||
| 59 | +@@ -33,29 +36,37 @@ class PipInvoke(BaseEmbed): | ||
| 60 | + | ||
| 61 | + @contextmanager | ||
| 62 | + def get_pip_install_cmd(self, exe, for_py_version): | ||
| 63 | +- cmd = [str(exe), "-m", "pip", "-q", "install", "--only-binary", ":all:", "--disable-pip-version-check"] | ||
| 64 | +- if not self.download: | ||
| 65 | +- cmd.append("--no-index") | ||
| 66 | +- folders = set() | ||
| 67 | +- for dist, version in self.distribution_to_versions().items(): | ||
| 68 | +- wheel = get_wheel( | ||
| 69 | +- distribution=dist, | ||
| 70 | +- version=version, | ||
| 71 | +- for_py_version=for_py_version, | ||
| 72 | +- search_dirs=self.extra_search_dir, | ||
| 73 | +- download=False, | ||
| 74 | +- app_data=self.app_data, | ||
| 75 | +- do_periodic_update=self.periodic_update, | ||
| 76 | +- env=self.env, | ||
| 77 | +- ) | ||
| 78 | +- if wheel is None: | ||
| 79 | +- msg = f"could not get wheel for distribution {dist}" | ||
| 80 | +- raise RuntimeError(msg) | ||
| 81 | +- folders.add(str(wheel.path.parent)) | ||
| 82 | +- cmd.append(Version.as_pip_req(dist, wheel.version)) | ||
| 83 | +- for folder in sorted(folders): | ||
| 84 | +- cmd.extend(["--find-links", str(folder)]) | ||
| 85 | +- yield cmd | ||
| 86 | ++ cmd = [ | ||
| 87 | ++ str(exe), | ||
| 88 | ++ "-m", | ||
| 89 | ++ "pip", | ||
| 90 | ++ "-q", | ||
| 91 | ++ "install", | ||
| 92 | ++ "--only-binary", | ||
| 93 | ++ ":all:", | ||
| 94 | ++ "--disable-pip-version-check", | ||
| 95 | ++ "--no-index", | ||
| 96 | ++ ] | ||
| 97 | ++ # Only the selected archives may participate in pip's filename-based selection. | ||
| 98 | ++ with TemporaryDirectory(prefix="virtualenv-seed-") as folder: | ||
| 99 | ++ cmd.extend(["--find-links", folder]) | ||
| 100 | ++ for dist, version in self.distribution_to_versions().items(): | ||
| 101 | ++ wheel = get_wheel( | ||
| 102 | ++ distribution=dist, | ||
| 103 | ++ version=version, | ||
| 104 | ++ for_py_version=for_py_version, | ||
| 105 | ++ search_dirs=self.extra_search_dir, | ||
| 106 | ++ download=self.download, | ||
| 107 | ++ app_data=self.app_data, | ||
| 108 | ++ do_periodic_update=self.periodic_update, | ||
| 109 | ++ env=self.env, | ||
| 110 | ++ ) | ||
| 111 | ++ if wheel is None: | ||
| 112 | ++ msg = f"could not get wheel for distribution {dist}" | ||
| 113 | ++ raise RuntimeError(msg) | ||
| 114 | ++ copyfile(str(wheel.path), str(Path(folder) / wheel.name)) | ||
| 115 | ++ cmd.append(Version.as_pip_req(dist, wheel.version)) | ||
| 116 | ++ yield cmd | ||
| 117 | + | ||
| 118 | + | ||
| 119 | + __all__ = [ | ||
| 120 | +diff --git a/src/virtualenv/seed/wheels/acquire.py b/src/virtualenv/seed/wheels/acquire.py | ||
| 121 | +index 8b3ddd8..6243247 100644 | ||
| 122 | +--- a/src/virtualenv/seed/wheels/acquire.py | ||
| 123 | ++++ b/src/virtualenv/seed/wheels/acquire.py | ||
| 124 | +@@ -7,11 +7,36 @@ import sys | ||
| 125 | + from operator import eq, lt | ||
| 126 | + from pathlib import Path | ||
| 127 | + from subprocess import PIPE, CalledProcessError, Popen | ||
| 128 | ++from tempfile import TemporaryDirectory | ||
| 129 | + | ||
| 130 | + from .bundle import from_bundle | ||
| 131 | +-from .periodic_update import add_wheel_to_update_log | ||
| 132 | ++from .periodic_update import UnverifiedWheelError, add_wheel_to_update_log, verify_wheel_digest | ||
| 133 | + from .util import Version, Wheel, discover_wheels | ||
| 134 | + | ||
| 135 | ++# A private index can legitimately serve a rebuilt wheel under the same distribution, version, and filename PyPI also | ||
| 136 | ++# uses - an internal mirror re-signing or re-packaging a release for policy reasons, for example. Verifying that file | ||
| 137 | ++# against PyPI's own digest would then reject a wheel that came from exactly the source it was configured to come from, | ||
| 138 | ++# not a compromised one. This does not attempt to find every way pip can be pointed elsewhere (a pip.conf file has the | ||
| 139 | ++# same effect and is invisible here), only the common, explicit one, so treat this as a conservative signal rather than | ||
| 140 | ++# proof either way: it exists to avoid false rejections, not to guarantee verification runs whenever it safely could. | ||
| 141 | ++_CUSTOM_INDEX_ENV_VARS = ("PIP_INDEX_URL", "PIP_EXTRA_INDEX_URL", "PIP_INDEX") | ||
| 142 | ++ | ||
| 143 | ++ | ||
| 144 | ++def _uses_default_index(env): | ||
| 145 | ++ return not any(env.get(var) for var in _CUSTOM_INDEX_ENV_VARS) | ||
| 146 | ++ | ||
| 147 | ++ | ||
| 148 | ++def _canonical_name(name): | ||
| 149 | ++ return name.lower().replace("_", "-").replace(".", "-") | ||
| 150 | ++ | ||
| 151 | ++ | ||
| 152 | ++def _version_spec_matches(version_spec, wheel): | ||
| 153 | ++ if version_spec.startswith("=="): | ||
| 154 | ++ return wheel.version_tuple == Wheel.as_version_tuple(version_spec[2:]) | ||
| 155 | ++ if version_spec.startswith("<"): | ||
| 156 | ++ return wheel.version_tuple < Wheel.as_version_tuple(version_spec[1:]) | ||
| 157 | ++ raise ValueError(version_spec) | ||
| 158 | ++ | ||
| 159 | + | ||
| 160 | + def get_wheel( # noqa: PLR0913 | ||
| 161 | + distribution, | ||
| 162 | +@@ -49,33 +74,67 @@ def get_wheel( # noqa: PLR0913 | ||
| 163 | + | ||
| 164 | + | ||
| 165 | + def download_wheel(distribution, version_spec, for_py_version, search_dirs, app_data, to_folder, env): # noqa: PLR0913 | ||
| 166 | ++ """Download one wheel into a private staging directory and move it into ``to_folder`` once it checks out. | ||
| 167 | ++ | ||
| 168 | ++ :raises CalledProcessError: when ``pip download`` fails. | ||
| 169 | ++ :raises UnverifiedWheelError: when PyPI cannot supply the digest to verify against. | ||
| 170 | ++ :raises RuntimeError: when the staged file is not the requested wheel, its digest differs from PyPI's, or it does | ||
| 171 | ++ not support ``for_py_version``; nothing reaches ``to_folder`` in these cases. | ||
| 172 | ++ | ||
| 173 | ++ """ | ||
| 174 | + to_download = f"{distribution}{version_spec or ''}" | ||
| 175 | + logging.debug("download wheel %s %s to %s", to_download, for_py_version, to_folder) | ||
| 176 | +- cmd = [ | ||
| 177 | +- sys.executable, | ||
| 178 | +- "-m", | ||
| 179 | +- "pip", | ||
| 180 | +- "download", | ||
| 181 | +- "--progress-bar", | ||
| 182 | +- "off", | ||
| 183 | +- "--disable-pip-version-check", | ||
| 184 | +- "--only-binary=:all:", | ||
| 185 | +- "--no-deps", | ||
| 186 | +- "--python-version", | ||
| 187 | +- for_py_version, | ||
| 188 | +- "-d", | ||
| 189 | +- str(to_folder), | ||
| 190 | +- to_download, | ||
| 191 | +- ] | ||
| 192 | ++ verify_against_pypi = _uses_default_index(env) | ||
| 193 | + # pip has no interface in python - must be a new sub-process | ||
| 194 | + env = pip_wheel_env_run(search_dirs, app_data, env) | ||
| 195 | +- process = Popen(cmd, env=env, stdout=PIPE, stderr=PIPE, universal_newlines=True, encoding="utf-8") | ||
| 196 | +- out, err = process.communicate() | ||
| 197 | +- if process.returncode != 0: | ||
| 198 | +- kwargs = {"output": out, "stderr": err} | ||
| 199 | +- raise CalledProcessError(process.returncode, cmd, **kwargs) | ||
| 200 | +- result = _find_downloaded_wheel(distribution, version_spec, for_py_version, to_folder, out) | ||
| 201 | +- logging.debug("downloaded wheel %s", result.name) | ||
| 202 | ++ to_folder.mkdir(parents=True, exist_ok=True) | ||
| 203 | ++ with TemporaryDirectory(dir=str(to_folder), prefix="download-") as staging: | ||
| 204 | ++ cmd = [ | ||
| 205 | ++ sys.executable, | ||
| 206 | ++ "-m", | ||
| 207 | ++ "pip", | ||
| 208 | ++ "download", | ||
| 209 | ++ "--progress-bar", | ||
| 210 | ++ "off", | ||
| 211 | ++ "--disable-pip-version-check", | ||
| 212 | ++ "--only-binary=:all:", | ||
| 213 | ++ "--no-deps", | ||
| 214 | ++ "--python-version", | ||
| 215 | ++ for_py_version, | ||
| 216 | ++ "-d", | ||
| 217 | ++ staging, | ||
| 218 | ++ to_download, | ||
| 219 | ++ ] | ||
| 220 | ++ process = Popen(cmd, env=env, stdout=PIPE, stderr=PIPE, universal_newlines=True, encoding="utf-8") | ||
| 221 | ++ out, err = process.communicate() | ||
| 222 | ++ if process.returncode != 0: | ||
| 223 | ++ kwargs = {"output": out, "stderr": err} | ||
| 224 | ++ raise CalledProcessError(process.returncode, cmd, **kwargs) | ||
| 225 | ++ result = _downloaded_wheel(Path(staging), distribution, version_spec) | ||
| 226 | ++ if verify_against_pypi: | ||
| 227 | ++ verify_wheel_digest(result) | ||
| 228 | ++ if not result.support_py(for_py_version): | ||
| 229 | ++ msg = f"downloaded wheel {result.name} does not support Python {for_py_version}" | ||
| 230 | ++ raise RuntimeError(msg) | ||
| 231 | ++ destination = to_folder / result.name | ||
| 232 | ++ result.path.replace(destination) | ||
| 233 | ++ logging.debug("downloaded wheel %s", result.name) | ||
| 234 | ++ return Wheel(destination) | ||
| 235 | ++ | ||
| 236 | ++ | ||
| 237 | ++def _downloaded_wheel(folder, distribution, version_spec): | ||
| 238 | ++ files = list(folder.iterdir()) | ||
| 239 | ++ if len(files) != 1 or files[0].is_symlink() or not files[0].is_file(): | ||
| 240 | ++ msg = f"could not find downloaded wheel for {distribution}: expected one archive" | ||
| 241 | ++ raise RuntimeError(msg) | ||
| 242 | ++ result = Wheel.from_path(files[0]) | ||
| 243 | ++ if ( | ||
| 244 | ++ result is None | ||
| 245 | ++ or _canonical_name(result.distribution) != _canonical_name(distribution) | ||
| 246 | ++ or (version_spec and not _version_spec_matches(version_spec, result)) | ||
| 247 | ++ ): | ||
| 248 | ++ msg = f"unexpected downloaded wheel {files[0].name} for {distribution}{version_spec or ''}" | ||
| 249 | ++ raise RuntimeError(msg) | ||
| 250 | + return result | ||
| 251 | + | ||
| 252 | + | ||
| 253 | +diff --git a/src/virtualenv/seed/wheels/periodic_update.py b/src/virtualenv/seed/wheels/periodic_update.py | ||
| 254 | +index 32ffad6..c39fa8d 100644 | ||
| 255 | +--- a/src/virtualenv/seed/wheels/periodic_update.py | ||
| 256 | ++++ b/src/virtualenv/seed/wheels/periodic_update.py | ||
| 257 | +@@ -2,6 +2,7 @@ | ||
| 258 | + | ||
| 259 | + from __future__ import annotations | ||
| 260 | + | ||
| 261 | ++import hashlib | ||
| 262 | + import json | ||
| 263 | + import logging | ||
| 264 | + import os | ||
| 265 | +@@ -14,6 +15,7 @@ from shutil import copy2 | ||
| 266 | + from subprocess import DEVNULL, Popen | ||
| 267 | + from textwrap import dedent | ||
| 268 | + from threading import Thread | ||
| 269 | ++from time import sleep | ||
| 270 | + from urllib.error import URLError | ||
| 271 | + from urllib.request import urlopen | ||
| 272 | + | ||
| 273 | +@@ -413,8 +415,59 @@ def _run_manual_upgrade(app_data, distribution, for_py_version, env): | ||
| 274 | + logging.warning(msg, *args) | ||
| 275 | + | ||
| 276 | + | ||
| 277 | ++#: Pauses between PyPI metadata lookups before refusing a wheel we could not verify, to ride out a brief outage or a | ||
| 278 | ++#: JSON API that has not caught up with a fresh upload. | ||
| 279 | ++_DIGEST_RETRY_DELAYS = (1.0, 2.0, 4.0) | ||
| 280 | ++ | ||
| 281 | ++ | ||
| 282 | ++def verify_wheel_digest(wheel): | ||
| 283 | ++ """Verify a downloaded wheel's sha256 against the digest PyPI's JSON API reports for that release. | ||
| 284 | ++ | ||
| 285 | ++ ``pip download`` trusts whatever index it is configured to use, and nothing else in this module checks the bytes it | ||
| 286 | ++ hands back. Callers run this only when pip used the default index, so PyPI served the file and must list its digest. | ||
| 287 | ++ A failed metadata request, an unknown filename or a missing sha256 therefore fails like a mismatch: accepting the | ||
| 288 | ++ wheel in those cases would let anyone who can block ``pypi.org/pypi`` turn the check off. | ||
| 289 | ++ | ||
| 290 | ++ :raises UnverifiedWheelError: if PyPI's sha256 for this exact filename cannot be obtained after retrying. | ||
| 291 | ++ :raises RuntimeError: if the sha256 does not match. | ||
| 292 | ++ | ||
| 293 | ++ """ | ||
| 294 | ++ expected = _pypi_sha256_for_wheel(wheel) | ||
| 295 | ++ for delay in _DIGEST_RETRY_DELAYS: | ||
| 296 | ++ if expected is not None: | ||
| 297 | ++ break | ||
| 298 | ++ logging.warning("could not obtain the sha256 of %s from PyPI, retrying in %s seconds", wheel.name, delay) | ||
| 299 | ++ _PYPI_CACHE.pop(wheel.distribution, None) | ||
| 300 | ++ sleep(delay) | ||
| 301 | ++ expected = _pypi_sha256_for_wheel(wheel) | ||
| 302 | ++ if expected is None: | ||
| 303 | ++ msg = f"refusing downloaded wheel {wheel.name}: could not obtain its sha256 from PyPI to verify it" | ||
| 304 | ++ raise UnverifiedWheelError(msg) | ||
| 305 | ++ actual = hashlib.sha256(wheel.path.read_bytes()).hexdigest() | ||
| 306 | ++ if actual != expected: | ||
| 307 | ++ msg = f"downloaded wheel {wheel.name} has sha256 {actual}, but PyPI reports {expected} for this release" | ||
| 308 | ++ raise RuntimeError(msg) | ||
| 309 | ++ | ||
| 310 | ++ | ||
| 311 | ++class UnverifiedWheelError(RuntimeError): | ||
| 312 | ++ """PyPI's digest for a downloaded wheel was unavailable, so a caller may fall back to the bundled wheel.""" | ||
| 313 | ++ | ||
| 314 | ++ | ||
| 315 | ++def _pypi_sha256_for_wheel(wheel): | ||
| 316 | ++ content = _pypi_get_distribution_info_cached(wheel.distribution) | ||
| 317 | ++ releases = content.get("releases") if isinstance(content, dict) else None | ||
| 318 | ++ entries = releases.get(wheel.version) if isinstance(releases, dict) else None | ||
| 319 | ++ if not isinstance(entries, list): | ||
| 320 | ++ return None | ||
| 321 | ++ entry = next((entry for entry in entries if isinstance(entry, dict) and entry.get("filename") == wheel.name), {}) | ||
| 322 | ++ digests = entry.get("digests") | ||
| 323 | ++ sha256 = digests.get("sha256") if isinstance(digests, dict) else None | ||
| 324 | ++ return sha256 if isinstance(sha256, str) else None | ||
| 325 | ++ | ||
| 326 | ++ | ||
| 327 | + __all__ = [ | ||
| 328 | + "NewVersion", | ||
| 329 | ++ "UnverifiedWheelError", | ||
| 330 | + "UpdateLog", | ||
| 331 | + "add_wheel_to_update_log", | ||
| 332 | + "do_update", | ||
| 333 | +@@ -424,4 +477,5 @@ __all__ = [ | ||
| 334 | + "periodic_update", | ||
| 335 | + "release_date_for_wheel_path", | ||
| 336 | + "trigger_update", | ||
| 337 | ++ "verify_wheel_digest", | ||
| 338 | + ] | ||
| @@ -1,13 +1,14 @@ | |||
| 1 | %global pypi_name virtualenv | 1 | %global pypi_name virtualenv |
| 2 | Name: python-%{pypi_name} | 2 | Name: python-%{pypi_name} |
| 3 | Version: 20.26.6 | 3 | Version: 20.26.6 |
| 4 | -Release: 3 | 4 | +Release: 4 |
| 5 | Summary: A tool to create isolated Python environments | 5 | Summary: A tool to create isolated Python environments |
| 6 | License: MIT | 6 | License: MIT |
| 7 | URL: http://pypi.python.org/pypi/virtualenv | 7 | URL: http://pypi.python.org/pypi/virtualenv |
| 8 | Source0: %{pypi_source %{pypi_name}} | 8 | Source0: %{pypi_source %{pypi_name}} |
| 9 | Patch0: CVE-2026-22702.patch | 9 | Patch0: CVE-2026-22702.patch |
| 10 | Patch1: CVE-2026-113726.patch | 10 | Patch1: CVE-2026-113726.patch |
| 11 | +Patch2: CVE-2026-113717.patch | ||
| 11 | 12 | ||
| 12 | BuildArch: noarch | 13 | BuildArch: noarch |
| 13 | BuildRequires: python3-distlib python3-filelock | 14 | BuildRequires: python3-distlib python3-filelock |
| @@ -62,6 +63,11 @@ sed -i "s/^version_tuple = /__version_tuple__ = version_tuple = /g" ${ver_pyfile | |||
| 62 | %{python3_sitelib}/%{pypi_name}*.dist-info/ | 63 | %{python3_sitelib}/%{pypi_name}*.dist-info/ |
| 63 | 64 | ||
| 64 | %changelog | 65 | %changelog |
| 66 | +* Wed Nov 18 2026 tianlijun <tianlijun@uniontech.com> - 20.26.6-4 | ||
| 67 | +- Fix CVE-2026-113717 / GHSA-cx28-75q4-x26q: verify downloaded seed wheels | ||
| 68 | + before caching and install them with --no-index from a private directory | ||
| 69 | + (backport of upstream commit 7babec66, released in 21.14.9) | ||
| 70 | + | ||
| 65 | * Sun Nov 01 2026 infra_team <zhaiwenjie1@huawei.com> - 20.26.6-3 | 71 | * Sun Nov 01 2026 infra_team <zhaiwenjie1@huawei.com> - 20.26.6-3 |
| 66 | - Fix CVE-2026-113726: escape the environment name in the bash/zsh activate prompt | 72 | - Fix CVE-2026-113726: escape the environment name in the bash/zsh activate prompt |
| 67 | 73 | ||