在一些 Windows 7 系统上,根据 dotnet 官方文档,需要安装上 KB2533623 补丁,才能运行 dotnet core 或 .NET 5 等应用。尽管非所有的设备都需要安装此,但这也让应用的分发不便,安装包上都需要带上补丁给用户安装。此补丁同时也要求安装完成之后重启系统,这对用户端来说,也是较不方便。本文来聊聊为什么 dotnet core 一系的框架依赖于此补丁

特别感谢 lsj 给我讲解 Win32 调用部分的知识和帮我调查具体的原因,我只是记录的工具人

补丁

开始之前,先来理一下所需补丁的情况,不想看补丁细节还请跳到下文主题这章。准确来说,在当前 2021.12.25 官方推荐 Win7 系统打上的补丁是 KB3063858 补丁

KB3063858: MS15-063:Windows 内核中的漏洞可能允许特权提升:2015 年 6 月 9 日 此安全更新可解决 Windows 中的一个漏洞。如果用户访问包含特殊设计的文件的网络共享(或指向网络共享的网站)时,此漏洞可能允许特权提升。但是在所有情况下,攻击者都无法强制用户访问此类网络共享或网站。

也有伙伴推荐给我的是安装 KB4457144 补丁。但是 KB4457144 补丁太大了,包含太多内容,带上这个补丁没什么优势

KB4457144: 2018 年 9 月 11 日 - KB4457144(月度汇总)

在 GitHub 上的 Security update KB2533623 no longer available · Issue #20459 · dotnet/docs 讨论上,有大佬表示 KB3063858 或 KB4457144 包含了 KB2533623 补丁内容:

The thread claims that KB2533623 is superseded by KB3063858 or KB4457144. https://github.com/dotnet/docs/issues/20459#issuecomment-689513876

值得一说的是对需要安装 KB3063858 补丁的系统来说,大多数都需要额外加上证书,参阅 https://www.microsoft.com/pkiops/Docs/Repository.htm 因此我认为对于客户端分发来说,打上 KB2533623 补丁似乎更好。但是 KB2533623 当前被微软下架了,请看 Security update KB2533623 no longer available · Issue #20459 · dotnet/docs

这是 KB2533623 的下载地址: http://www.microsoft.com/download/details.aspx?familyid=c79c41b0-fbfb-4d61-b5d8-cadbe184b9fc

另外,在刚推送 dotnet core 3.0 的预览版本时,有伙伴在 WPF 官方仓库反馈说需要加上 KB2999226 补丁。此 KB2999226 补丁是 Windows 中的 Universal C Runtime 更新 的内容,参阅 https://github.com/dotnet/wpf/issues/2009#issuecomment-543872453

也许可以使用 runtime.win7-x64.Microsoft.NETCore.Windows.ApiSets NuGet 库 代替 KB2999226 补丁内容,只需要将 api-xxxxx.dll 这些文件拷贝到输出路径即可。或者是解包 VC++ 2015 的分发包里的文件,将 api-xxxxx.dll 和 ucrtbase.dll 拷贝到输出路径即可

因此,对于客户端分发来说,似乎采用 KB2533623 最小补丁,然后在输出路径上拷贝好 api-xxxxx.dll 这些文件到输出路径是最佳方法

下载地址:

  • KB2533623 x86

    • MD5:EDF1D538C85F24EC0EF0991E6B27F0D7

    • SHA1:25BECC0815F3E47B0BA2AE84480E75438C119859

  • KB2533623 x64

    • MD5:0A894C59C245DAD32E6E48ECBDBC8921

    • SHA1:8A59EA3C7378895791E6CDCA38CC2AD9E83BEBFF

  • KB3063858 32-bit

  • KB3063858 64-bit

主题

清理好了各个补丁的关系之后,咱回到主题。为什么在 dotnet core 一系都有此要求?而且还不是对所有 Win7 系统都有此要求,这是为什么?回答这两个问题,可以从 dotnet core 的 dotnet host core run 开始聊起

在 Windows 下,咱双击运行的 dotnet core 的可执行 exe 文件,其实是一个 AppHost 文件。咱编写的 Main 函数,在非单文件模式下,是放在同名的 dll 里面。详细关于 AppHost 请参阅 dotnet core 应用是如何跑起来的 通过AppHost理解运行过程

在 dotnet host core run 里,对应代码是 src\coreclr\hosts\corerun\corerun.hpp 文件,在这里需要拉起 hostpolicy.dll 组件。加载此组件的代码如下,不过代码内容不重要哈

inline bool try_load_hostpolicy(pal::string_t mock_hostpolicy_value){const char_t* hostpolicyName = W("hostpolicy.dll");pal::mod_t hMod = (pal::mod_t)::GetModuleHandleW(hostpolicyName);if (hMod != nullptr)return true;// Check if a hostpolicy exists and if it does, load it.if (pal::does_file_exist(mock_hostpolicy_value))hMod = (pal::mod_t)::LoadLibraryExW(mock_hostpolicy_value.c_str(), nullptr, LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | LOAD_LIBRARY_SEARCH_DEFAULT_DIRS);if (hMod == nullptr)pal::fprintf(stderr, W("Failed to load mock hostpolicy at path '%s'. Error: 0x%08x\n"), mock_hostpolicy_value.c_str(), ::GetLastError());return hMod != nullptr;}

以上最关键的只有这一行代码 LoadLibraryExW(mock_hostpolicy_value.c_str(), nullptr, LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | LOAD_LIBRARY_SEARCH_DEFAULT_DIRS)

以上代码通过 LoadLibraryExW 函数进行加载库。调用时传入了 LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR 参数。根据官方文档的描述,调用此函数,如果加入了 LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR 参数,将要求 KB2533623 补丁

If this value is used, the directory that contains the DLL is temporarily added to the beginning of the list of directories that are searched for the DLL’s dependencies. Directories in the standard search path are not searched. The lpFileName parameter must specify a fully qualified path. This value cannot be combined with LOAD_WITH_ALTERED_SEARCH_PATH. For example, if Lib2.dll is a dependency of C:\Dir1\Lib1.dll, loading Lib1.dll with this value causes the system to search for Lib2.dll only in C:\Dir1. To search for Lib2.dll in C:\Dir1 and all of the directories in the DLL search path, combine this value with LOAD_LIBRARY_DEFAULT_DIRS.

Windows 7, Windows Server 2008 R2, Windows Vista and Windows Server 2008: This value requires KB2533623 to be installed. Windows Server 2003 and Windows XP: This value is not supported

除以上逻辑之外,在 dotnet 仓库里还可以找到其他的各个部分的 LoadLibraryExW 函数上,也都带上了 LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR 参数,列表如下:

  • src\coreclr\dlls\dbgshim\dbgshim.cpp: CreateDebuggingInterfaceFromVersion2 函数

  • src\coreclr\hosts\corerun\corerun.hpp: try_load_hostpolicy 函数

  • src\coreclr\hosts\coreshim\CoreShim.cpp: TryLoadHostPolicy 函数

  • src\coreclr\vm\nativelibrary.cpp: DetermineLibNameVariations 函数

  • src\native\corehost\hostmisc\pal.windows.cpp: load_library 函数

看起来文件不多,就看哪个伙伴想不开就去改改挖坑吧

此 LoadLibraryExW 函数是放在 Kernel32.dll 的,相同的需求,在 dotnet core 里也间接依赖于 AddDllDirectory 和 SetDefaultDllDirectories 和 RemoveDllDirectory 等方法。如官方文档描述,刚好这些方法也都相同依赖 KB2533623 补丁

Windows 7, Windows Server 2008 R2, Windows Vista and Windows Server 2008: To use this function in an application, call GetProcAddress to retrieve the function’s address from Kernel32.dll. KB2533623 must be installed on the target platform.

通过如上描述,可以了解到,在 dotnet core 需要补丁的原因是调用了 Kernel32.dll 的新(大约10年前加的)函数,对于一些 Win7 旧设备上,没有更新 Kernel32.dll 加上函数

因此,判断 dotnet core 所依赖环境可以有两个方法,第一个方法是判断 KB2533623 补丁是否有安装,另一个方法是判断 Kernel32.dll 里是否存在 AddDllDirectory 函数。第一个方法判断是不靠谱的,因为不一定需要 KB2533623 补丁,如上文描述,换成其他几个补丁也可以。判断补丁安装可以使用下面命令行代码,更多请参阅 dotnet 通过 WMI 获取系统补丁 和 PowerShell 通过 WMI 获取补丁

BAT:wmic qfe GET hotfixid | findstr /C:"KB2533623"

另外,判断是否存在 KB2533623 补丁,不存在则安装的 bat 脚本代码如下

echo off
cd /d %~dp0
wmic qfe GET hotfixid | findstr /C:"KB2533623"
if %errorlevel% equ 0 (Echo Patch KB2533623 installed) else (
wusa Windows6.1-KB2533623-x64.msu /quiet /norestart
wusa Windows6.1-KB2533623-x86.msu /quiet /norestart
exit /b 1
)
exit /b 0

第二个方法我比较推荐,判断代码如下:

using PInvoke;
using System;namespace AddDllDirectoryDetectCs
{class Program{static void Main(string[] args){using (var hModule = Kernel32.LoadLibrary("Kernel32.dll")){if (!hModule.IsInvalid){IntPtr hFarProc = Kernel32.GetProcAddress(hModule, "AddDllDirectory");if (hFarProc != IntPtr.Zero){Console.WriteLine("Either running on Win8+, or KB2533623 is installed");}else{Console.Write("Likely running on Win7 or older OS, and KB2533623 is not installed");}}}// 以下是判断 Universal C Runtime 的逻辑,可以忽略using (var hModule = Kernel32.LoadLibraryEx("UCRTBASE.dll", IntPtr.Zero, Kernel32.LoadLibraryExFlags.LOAD_LIBRARY_SEARCH_SYSTEM32)){if (!hModule.IsInvalid){Console.WriteLine("UCRT is available - Either running on Win10+ or KB2999226 is installed");}else{Console.WriteLine("UCRT is not available - Likely running on OS older than Win10 and KB2999226 is not installed");}}}}
}

以上代码由 WPF 框架官方开发 Vatsan Madhavan 大佬提供,请看 vatsan-madhavan/checknetcoreprereqs: Helper utility to check .NET Core Prerequisites on Windows systems

收到 Vatsan Madhavan 大佬赞

参考

LoadLibraryExW function (libloaderapi.h) - Win32 apps Microsoft Docs

LoadLibraryExA function (libloaderapi.h) - Win32 apps Microsoft Docs

LoadLibraryW function (libloaderapi.h) - Win32 apps Microsoft Docs

windows - SetDllDirectory does not cascade, so dependency DLLs cannot be loaded - Stack Overflow

SetDllDirectoryA function (winbase.h) - Win32 apps Microsoft Docs

AddDllDirectory function (libloaderapi.h) - Win32 apps Microsoft Docs

Windows installer should warn about missing required updated · Issue #2452 · dotnet/runtime

RemoveDllDirectory function (libloaderapi.h) - Win32 apps

Unable to load DLL ‘wpfgfx_cor3.dll’ or one of its dependencies · Issue #2009 · dotnet/wpf

SqlClient: Unable to load DLL ‘sni.dll’ · Issue #16905 · dotnet/runtime

Failed to bind to coreclr dotnet run failure on Windows 7 and Windows 2008 R2 · Issue #5590 · dotnet/sdk

API Set Usage Question · Issue #5075 · dotnet/runtime

Cannot load CoreCLR.dll · Issue #5076 · dotnet/runtime

vatsan-madhavan/checknetcoreprereqs: Helper utility to check .NET Core Prerequisites on Windows systems

使用 C# 判断指定的 Windows 更新是否已安装-码农很忙

Windows 7 安装 msu 系统更新时的常见报错及解决办法-码农很忙

Windows 7 安装 .NET 5 / .NET Core 3.1 环境的方法和依赖文件-码农很忙

Windows 7 SP 1 部署 .NET 6 Desktop Runtime 桌面运行时会遇到的问题-码农很忙

探索 dotnet core 为何在 Windows7 系统需要补丁的原因相关推荐

  1. 适合win7的python版本_windows下多个python版本共存,如何在Windows7系统上安装最新的64位Python3.6.2...

    windows下多个python版本共存,如何在Windows7系统上安装最新的64位Python3.6.2 1.官网下载python3.6.2 https://www.python.org/ftp/ ...

  2. dotnet core开源博客系统XBlog介绍

    XBlog是dotnet core平台下的个人博客开源系统,它只需要通过Copy的方式即可以部署到Linux和windows系统中:如果你有安全证书那只需要简单配置一下即可提供安全的Https服务.接 ...

  3. JAVA:如何在Windows7系统中配置环境变量。

    笔者之前因为操作系统老旧而在网络上苦苦搜寻不到环境变量配置的方法,最终在<Java:从入门到精通>的旧版书上找到了解决办法,故将其抄录下来分享给大家. 在Windows 7系统中配置环境变 ...

  4. windows7系统蓝屏代码大全 收藏下次蓝屏可查

    蓝屏代码查询器 1.1.9 绿色免安装版 1.代码查询 即根据蓝屏代码查找电脑出现蓝屏的原因,并且可搜索微软知识库(强烈推荐). 2.解决方案 讲述了经常发生蓝屏的原因及防范对策,这篇文章内容较全面并 ...

  5. 如何在windows7上安装启明星系统。

    本文将以win7为例,介绍如何手动安装启明星系统.win8,win10,win2008,win2012 安装方法大同小异. 关于windows2008安装可以参考此处   win2012参考此处 关于 ...

  6. Linux系统上部署dotnet core

    Linux系统上部署dotnet core 主要步骤如下: 安装linux Linux下安装dotnet core环境 Linux下部署dotnet core系统 主要步骤如下: 因我公司需要,做一个 ...

  7. dotnet core TargetFramework 解析顺序探索

    dotnet core TargetFramework 解析顺序测试 Intro 现在 dotnet 的 TargetFramework 越来越多,抛开 .NET Framework 不谈,如果一个类 ...

  8. centos 6.5 安装dotnet core 2.2

    .net core 官网地址 https://dotnet.microsoft.com/download 本次安装版本为.net core SDK v2.2.101 1.查看系统版本, 升级系统基本l ...

  9. k8s pod部署到不同node_部署Dotnet Core应用到Kubernetes(一) - 老王Plus

    最近闲了点,写个大活:部署Dotnet应用到K8s. 写在前边的话 一直想完成这个主题.但这个主题实在太大了,各种拖延症的小宇宙不时爆发一下,结果就拖到了现在. 这个主题,会是一个系列.在这个系列中, ...

最新文章

  1. 一个请求从 URL 字符串到 HTML 代码的“漫长曲折”之路
  2. python twisted教程_Python Twisted系列教程16:Twisted 进程守护
  3. python画抛物线_如何使用python的matplotlib模块画抛物线
  4. 搭建zookeeper+kafka集群
  5. NXP KW38开发杂记(一)MCUXpress 运行进入NMI_Handler
  6. 在pycharm中使用conda虚拟环境(conda虚拟环境是已经创建好的),解决python安装包文件很费劲的问题
  7. Game Center Achievements and Leaderboards part 1 转
  8. vim 查找匹配字符串次数
  9. 《Oracle数据库管理与维护实战》——1.2 Oracle各版本异同
  10. 1.视频全屏展示(适应各种屏幕尺寸)
  11. java 反射 框架_Java——利用反射实现框架类
  12. 【HDU6194】string string string(统计出现k次的子串数目---后缀数组+st表)
  13. unity实现前后左右移动代码_Unity实现物体左右移动效果
  14. 微信小程序 《数字华容道》
  15. 普兰特印花设备A3UV 手机壳 金属裁片 塑料免费打样数码打印机
  16. 使用HttpClient下载网页
  17. chrome 插件导出与导入
  18. SuperMap iClient3D for WebGL教程 粒子特效-基础火焰特效
  19. 关于代码审查的一些探讨和总结
  20. 北京等一线城市租房通鉴

热门文章

  1. 演示:使用Sniffer统计与分析流量
  2. 使用Nagios监控esx、esxi、vcenter
  3. RxSwift 之官方文档
  4. html中radio,checkbox值的获取、赋值、注册事件
  5. vi 编辑器跳转到指定行数
  6. python3-day4(装饰器)
  7. Binding在WPF中的使用
  8. Xubuntu菜单删改条记
  9. iPhone5:4G是否进入主流的风向标?
  10. Symbian开发平台的搭建之VC++6.0Carbide C++ 2.0