python / python/cpython

Windows version from `sys.getwindowsversion()` is wrong in binaries that do not declare Windows 10 support

オープン
#146,299 コメント 7 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

interpreter-core OS-windows type-bug
主要言語
Python
スター
77.2k
フォーク
35.9k
PR マージ指標
PR 指標を取得中

説明

Bug report

Bug description:

Repro:

  1. Install Python for Windows (ideally create a new installation, since you'll need to patch it).
  2. Using Resource Hacker, or any other PE resource editing tool, remove the <supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}"/> line from the PE manifest, or just delete the whole manifest from the binary. Write back the patched binary. This enables compatibility shims for Windows 8 in the Win32 API when running the binary.
  3. Run ...\python.exe -c "import sys; print(sys.getwindowsversion())"
  4. Observe that the returned version indicates Windows 8 (major=6, minor=3) instead of the actual OS version (major=10, minor=0 for both Windows 10 and 11).

The root cause is very similar to https://issues.chromium.org/issues/494938698, see the full description there.

To retrieve the Windows version, Python reads the file version from kernel32.dll, because GetVersionEx will report old values due to backwards compatibility. However, GetFileVersionInfoW, which is used in the sysmodule.c implementation of sys.getwindowsversion() also has an internal compatibility shim where if the calling binary does not declare support for Windows 10, it will shim the actual DLL versions with a fake Windows 8 version for system libraries, resulting in sys.getwindowsversion() reporting a wrong OS version.

This is not an issue for python.exe directly, because it has a proper PE manifest that declares Windows 10 support, but it is an issue for apps that embed CPython and do not have a PE manifest, or do not declare Windows 10 support.

Proposed fix

Replace the whole implementation with a call to RtlGetVersion from ntdll, which is now officially documented, returns the correct version without shims and there's a substantial list of prominent projects relying on it already.

CPython versions tested on:

3.15

Operating systems tested on:

Windows

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

Python/sysmodule.c の sys.getwindowsversion() 実装から始め、パッチを適用した、またはマニフェストのない Windows バイナリで報告された結果を再現します。文書化された RtlGetVersion エントリポイントを読み、その動作を現在のバージョン取得処理と比較します。Windows 10 をサポートしていない埋め込みバイナリが、互換性 shim によって変更された値ではなく実際の Windows バージョンを報告すれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
c, python
領域
operating-systems
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
静か
明瞭さ
おおむね明確
初心者へのやさしさ
55/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。