Skip to content

Commit 90bfbda

Browse files
committed
Merged PR 42182: Use explicit full-path for loading MsQuic.dll
Port of https://dev.azure.com/dnceng/internal/_git/dotnet-runtime/pullrequest/42181 for 8.0 # Description Use explicit full-path for loading MsQuic.dll - it prevents accidental load of MsQuic.dll from another unintended directory (e.g. CWD = Current Working Directory) if the bundled MsQuic.dll fails to load (which can happen e.g. on WinServer 2012, where MsQuic is not supported). # Customer Impact Defense-in-depth # Regression No # Testing Targeted manual test on affected OS (Windows Server 2012), including self-contained and single-file publish scenarios. # Risk Low, changes are contained and have been thoroughly tested manually and in CI suite.
2 parents fd1ed01 + c0d9021 commit 90bfbda

File tree

1 file changed

+19
-4
lines changed
  • src/libraries/System.Net.Quic/src/System/Net/Quic/Internal

1 file changed

+19
-4
lines changed

src/libraries/System.Net.Quic/src/System/Net/Quic/Internal/MsQuicApi.cs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,23 @@ static MsQuicApi()
8383

8484
if (OperatingSystem.IsWindows())
8585
{
86-
// Windows ships msquic in the assembly directory.
87-
loaded = NativeLibrary.TryLoad(Interop.Libraries.MsQuic, typeof(MsQuicApi).Assembly, DllImportSearchPath.AssemblyDirectory, out msQuicHandle);
86+
#pragma warning disable IL3000 // Avoid accessing Assembly file path when publishing as a single file
87+
// Windows ships msquic in the assembly directory next to System.Net.Quic, so load that.
88+
// For single-file deployments, the assembly location is an empty string so we fall back
89+
// to AppContext.BaseDirectory which is the directory containing the single-file executable.
90+
string path = typeof(MsQuicApi).Assembly.Location is string assemblyLocation && !string.IsNullOrEmpty(assemblyLocation)
91+
? System.IO.Path.GetDirectoryName(assemblyLocation)!
92+
: AppContext.BaseDirectory;
93+
#pragma warning restore IL3000
94+
95+
path = System.IO.Path.Combine(path, Interop.Libraries.MsQuic);
96+
97+
if (NetEventSource.Log.IsEnabled())
98+
{
99+
NetEventSource.Info(null, $"Attempting to load MsQuic from {path}");
100+
}
101+
102+
loaded = NativeLibrary.TryLoad(path, typeof(MsQuicApi).Assembly, DllImportSearchPath.LegacyBehavior, out msQuicHandle);
88103
}
89104
else
90105
{
@@ -154,7 +169,7 @@ static MsQuicApi()
154169

155170
if (version < s_minMsQuicVersion)
156171
{
157-
NotSupportedReason = $"Incompatible MsQuic library version '{version}', expecting higher than '{s_minMsQuicVersion}'.";
172+
NotSupportedReason = $"Incompatible MsQuic library version '{version}', expecting higher than '{s_minMsQuicVersion}'.";
158173
if (NetEventSource.Log.IsEnabled())
159174
{
160175
NetEventSource.Info(null, NotSupportedReason);
@@ -178,7 +193,7 @@ static MsQuicApi()
178193
// Implies windows platform, check TLS1.3 availability
179194
if (!IsWindowsVersionSupported())
180195
{
181-
NotSupportedReason = $"Current Windows version ({Environment.OSVersion}) is not supported by QUIC. Minimal supported version is {s_minWindowsVersion}.";
196+
NotSupportedReason = $"Current Windows version ({Environment.OSVersion}) is not supported by QUIC. Minimal supported version is {s_minWindowsVersion}.";
182197
if (NetEventSource.Log.IsEnabled())
183198
{
184199
NetEventSource.Info(null, NotSupportedReason);

0 commit comments

Comments
 (0)