Skip to content

Commit aff4e39

Browse files
committed
fix(connection): trust MSIX-packaged wslrelay.exe when Authenticode check fails
Modern WSL distributes as an MSIX package (Program Files\WSL / WindowsApps), which doesn't embed per-file Authenticode/catalog signatures on its EXEs -- trust is established at the package level instead. This caused genuine Microsoft wslrelay.exe binaries to be rejected during setup's loopback listener provenance check with "WSL relay Authenticode verification failed (Unsigned)". Add a fallback in WindowsAuthenticodeVerifier: when the classic Authenticode check fails and the file is wslrelay.exe, corroborate trust via the installed AppX package instead. Requires an exact match on the known WSL package family name (MicrosoftCorporationII.WindowsSubsystemForLinux_8wekyb3d8bbwe, whose suffix is derived from the publisher's signing cert), a real SignatureKind, and a Microsoft publisher (reusing the existing HasMicrosoftPublisherIdentity check). Looked up via a PowerShell Get-AppxPackage shell-out, matching this file's existing pattern for invoking schtasks.exe/wsl.exe. The primary Authenticode/catalog check is unchanged and always consulted first; the fallback narrows to wslrelay.exe specifically and never weakens the trust bar for any other binary.
1 parent b37307c commit aff4e39

2 files changed

Lines changed: 285 additions & 1 deletion

File tree

‎src/OpenClaw.Connection/WindowsAuthenticodeVerifier.cs‎

Lines changed: 197 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
using System;
2+
using System.Diagnostics;
23
using System.IO;
4+
using System.Linq;
5+
using System.Text.Json;
6+
using System.Threading;
37
using Microsoft.Security.Extensions;
48

59
namespace OpenClaw.Connection;
@@ -11,9 +15,51 @@ internal readonly record struct AuthenticodeTrustResult(bool IsTrusted, string?
1115
public static AuthenticodeTrustResult Rejected(string detail) => new(false, detail);
1216
}
1317

18+
/// <summary>
19+
/// Minimal projection of a Windows AppX/MSIX package as reported by
20+
/// <c>Get-AppxPackage</c>, used to corroborate genuine Microsoft WSL binaries that
21+
/// ship without a per-file Authenticode/catalog signature (MSIX packages are
22+
/// trusted at the package level, not per-file).
23+
/// </summary>
24+
internal readonly record struct AppxPackageInfo(
25+
string? PackageFamilyName,
26+
string? Publisher,
27+
string? SignatureKind);
28+
1429
internal static class WindowsAuthenticodeVerifier
1530
{
16-
public static AuthenticodeTrustResult VerifyMicrosoftSignedFile(string path)
31+
// Modern WSL ships as an MSIX package. The trailing suffix is a hash derived
32+
// from the publisher's signing certificate/public key, so an impostor package
33+
// re-signed with a different certificate would get a different family name.
34+
private const string WslPackageFamilyName =
35+
"MicrosoftCorporationII.WindowsSubsystemForLinux_8wekyb3d8bbwe";
36+
37+
public static AuthenticodeTrustResult VerifyMicrosoftSignedFile(string path) =>
38+
VerifyMicrosoftSignedFile(path, LookupWslAppxPackageViaPowerShell);
39+
40+
/// <summary>
41+
/// Test seam: allows callers to substitute a fake WSL AppX package lookup
42+
/// instead of shelling out to PowerShell.
43+
/// </summary>
44+
internal static AuthenticodeTrustResult VerifyMicrosoftSignedFile(
45+
string path,
46+
Func<AppxPackageInfo?> lookupWslPackage)
47+
{
48+
var primary = VerifyAuthenticodeSignature(path);
49+
if (primary.IsTrusted)
50+
return primary;
51+
52+
// MSIX packages (modern WSL) don't carry a per-file Authenticode/catalog
53+
// signature, so a failed classic check isn't conclusive for wslrelay.exe.
54+
// Only consult the package-level fallback for the WSL binaries this
55+
// codebase actually cares about; never widen trust for arbitrary files.
56+
if (!string.Equals(Path.GetFileName(path), "wslrelay.exe", StringComparison.OrdinalIgnoreCase))
57+
return primary;
58+
59+
return VerifyWslPackageFallback(primary, lookupWslPackage);
60+
}
61+
62+
private static AuthenticodeTrustResult VerifyAuthenticodeSignature(string path)
1763
{
1864
try
1965
{
@@ -45,6 +91,50 @@ public static AuthenticodeTrustResult VerifyMicrosoftSignedFile(string path)
4591
}
4692
}
4793

94+
private static AuthenticodeTrustResult VerifyWslPackageFallback(
95+
AuthenticodeTrustResult primaryFailure,
96+
Func<AppxPackageInfo?> lookupWslPackage)
97+
{
98+
AppxPackageInfo? package;
99+
try
100+
{
101+
package = lookupWslPackage();
102+
}
103+
catch
104+
{
105+
package = null;
106+
}
107+
108+
if (package is not { } info)
109+
{
110+
// No genuine WSL AppX package installed to corroborate; surface the
111+
// original Authenticode diagnostic rather than a new, less useful one.
112+
return primaryFailure;
113+
}
114+
115+
if (!string.Equals(info.PackageFamilyName, WslPackageFamilyName, StringComparison.Ordinal))
116+
{
117+
// Doesn't match the well-known family name (a different/impostor
118+
// package can't corroborate this binary); surface the original detail.
119+
return primaryFailure;
120+
}
121+
122+
if (string.IsNullOrEmpty(info.SignatureKind) ||
123+
string.Equals(info.SignatureKind, "None", StringComparison.OrdinalIgnoreCase))
124+
{
125+
return AuthenticodeTrustResult.Rejected(
126+
"WSL package signature verification failed: the installed WSL package is unsigned.");
127+
}
128+
129+
if (info.Publisher is null || !HasMicrosoftPublisherIdentity(info.Publisher))
130+
{
131+
return AuthenticodeTrustResult.Rejected(
132+
"WSL package signature verification failed: the installed WSL package's publisher is not Microsoft Corporation.");
133+
}
134+
135+
return AuthenticodeTrustResult.Trusted();
136+
}
137+
48138
internal static bool HasMicrosoftPublisherIdentity(string subject) =>
49139
subject.Split(',')
50140
.Select(part => part.Trim())
@@ -53,4 +143,110 @@ internal static bool HasMicrosoftPublisherIdentity(string subject) =>
53143
part,
54144
"O=Microsoft Corporation",
55145
StringComparison.OrdinalIgnoreCase));
146+
147+
private static AppxPackageInfo? LookupWslAppxPackageViaPowerShell()
148+
{
149+
Process? process = null;
150+
try
151+
{
152+
var psi = new ProcessStartInfo
153+
{
154+
FileName = ResolvePowerShellPath(),
155+
UseShellExecute = false,
156+
CreateNoWindow = true,
157+
RedirectStandardOutput = true,
158+
RedirectStandardError = true,
159+
};
160+
psi.ArgumentList.Add("-NoProfile");
161+
psi.ArgumentList.Add("-NonInteractive");
162+
psi.ArgumentList.Add("-Command");
163+
psi.ArgumentList.Add(
164+
"Get-AppxPackage -Name 'MicrosoftCorporationII.WindowsSubsystemForLinux' | " +
165+
"Select-Object -First 1 PackageFamilyName, Publisher, SignatureKind | " +
166+
"ConvertTo-Json -Compress");
167+
168+
process = Process.Start(psi);
169+
if (process is null)
170+
return null;
171+
172+
using var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(8));
173+
var stdoutTask = process.StandardOutput.ReadToEndAsync(timeout.Token);
174+
var stderrTask = process.StandardError.ReadToEndAsync(timeout.Token);
175+
176+
if (!process.WaitForExit(8_000))
177+
{
178+
try { process.Kill(entireProcessTree: true); } catch { }
179+
return null;
180+
}
181+
182+
string output;
183+
try
184+
{
185+
output = stdoutTask.GetAwaiter().GetResult();
186+
_ = stderrTask.GetAwaiter().GetResult();
187+
}
188+
catch
189+
{
190+
return null;
191+
}
192+
193+
if (process.ExitCode != 0 || string.IsNullOrWhiteSpace(output))
194+
return null;
195+
196+
return ParseAppxPackageJson(output);
197+
}
198+
catch
199+
{
200+
return null;
201+
}
202+
finally
203+
{
204+
process?.Dispose();
205+
}
206+
}
207+
208+
private static string ResolvePowerShellPath()
209+
{
210+
var systemRoot = Environment.GetFolderPath(Environment.SpecialFolder.Windows);
211+
if (string.IsNullOrWhiteSpace(systemRoot))
212+
systemRoot = Environment.GetEnvironmentVariable("SystemRoot");
213+
if (string.IsNullOrWhiteSpace(systemRoot))
214+
systemRoot = @"C:\Windows";
215+
return Path.Combine(systemRoot, "System32", "WindowsPowerShell", "v1.0", "powershell.exe");
216+
}
217+
218+
private static AppxPackageInfo? ParseAppxPackageJson(string json)
219+
{
220+
try
221+
{
222+
using var document = JsonDocument.Parse(json);
223+
var root = document.RootElement;
224+
if (root.ValueKind == JsonValueKind.Array)
225+
{
226+
if (root.GetArrayLength() == 0)
227+
return null;
228+
root = root[0];
229+
}
230+
if (root.ValueKind != JsonValueKind.Object)
231+
return null;
232+
233+
var familyName = GetStringProperty(root, "PackageFamilyName");
234+
if (familyName is null)
235+
return null;
236+
237+
return new AppxPackageInfo(
238+
familyName,
239+
GetStringProperty(root, "Publisher"),
240+
GetStringProperty(root, "SignatureKind"));
241+
}
242+
catch
243+
{
244+
return null;
245+
}
246+
}
247+
248+
private static string? GetStringProperty(JsonElement element, string propertyName) =>
249+
element.TryGetProperty(propertyName, out var value) && value.ValueKind == JsonValueKind.String
250+
? value.GetString()
251+
: null;
56252
}

‎tests/OpenClaw.Connection.Tests/ManagedLocalGatewayPortProvenanceServiceTests.cs‎

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,94 @@ public void VerifyMicrosoftSignedFile_RejectsUnsignedAssembly()
5353
Assert.Contains("Authenticode verification failed", result.Detail);
5454
}
5555

56+
private const string WslPackageFamilyName =
57+
"MicrosoftCorporationII.WindowsSubsystemForLinux_8wekyb3d8bbwe";
58+
private const string NonCanonicalWslRelayPath = @"C:\Program Files\WSL\wslrelay.exe";
59+
60+
[Fact]
61+
public void VerifyMicrosoftSignedFile_AuthenticodeFailsButWslPackageCorroborates_IsTrusted()
62+
{
63+
var result = WindowsAuthenticodeVerifier.VerifyMicrosoftSignedFile(
64+
NonCanonicalWslRelayPath,
65+
() => new AppxPackageInfo(
66+
WslPackageFamilyName,
67+
"CN=Microsoft Windows, O=Microsoft Corporation, C=US",
68+
"Developer"));
69+
70+
Assert.True(result.IsTrusted, result.Detail);
71+
}
72+
73+
[Fact]
74+
public void VerifyMicrosoftSignedFile_AuthenticodeFailsAndNoWslPackageFound_IsRejectedWithOriginalDetail()
75+
{
76+
var result = WindowsAuthenticodeVerifier.VerifyMicrosoftSignedFile(
77+
NonCanonicalWslRelayPath,
78+
() => null);
79+
80+
Assert.False(result.IsTrusted);
81+
Assert.Contains("Authenticode", result.Detail, StringComparison.Ordinal);
82+
}
83+
84+
[Fact]
85+
public void VerifyMicrosoftSignedFile_AuthenticodeFailsAndWslPackageFamilyNameMismatches_IsRejected()
86+
{
87+
var result = WindowsAuthenticodeVerifier.VerifyMicrosoftSignedFile(
88+
NonCanonicalWslRelayPath,
89+
() => new AppxPackageInfo(
90+
"SomeImpostor.WindowsSubsystemForLinux_deadbeefcafe",
91+
"CN=Microsoft Windows, O=Microsoft Corporation, C=US",
92+
"Developer"));
93+
94+
Assert.False(result.IsTrusted);
95+
}
96+
97+
[Fact]
98+
public void VerifyMicrosoftSignedFile_AuthenticodeFailsAndWslPackageUnsigned_IsRejected()
99+
{
100+
var result = WindowsAuthenticodeVerifier.VerifyMicrosoftSignedFile(
101+
NonCanonicalWslRelayPath,
102+
() => new AppxPackageInfo(
103+
WslPackageFamilyName,
104+
"CN=Microsoft Windows, O=Microsoft Corporation, C=US",
105+
"None"));
106+
107+
Assert.False(result.IsTrusted);
108+
Assert.Contains("WSL package", result.Detail, StringComparison.Ordinal);
109+
}
110+
111+
[Fact]
112+
public void VerifyMicrosoftSignedFile_AuthenticodeFailsAndWslPackagePublisherNotMicrosoft_IsRejected()
113+
{
114+
var result = WindowsAuthenticodeVerifier.VerifyMicrosoftSignedFile(
115+
NonCanonicalWslRelayPath,
116+
() => new AppxPackageInfo(
117+
WslPackageFamilyName,
118+
"CN=Evil Corp, O=Evil Corp, C=US",
119+
"Developer"));
120+
121+
Assert.False(result.IsTrusted);
122+
Assert.Contains("WSL package", result.Detail, StringComparison.Ordinal);
123+
}
124+
125+
[Fact]
126+
public void VerifyMicrosoftSignedFile_AuthenticodeSucceeds_NeverConsultsWslPackageFallback()
127+
{
128+
var windowsDir = Environment.GetFolderPath(Environment.SpecialFolder.Windows);
129+
var wslPath = Path.Combine(windowsDir, "System32", "wsl.exe");
130+
var fallbackInvoked = false;
131+
132+
var result = WindowsAuthenticodeVerifier.VerifyMicrosoftSignedFile(
133+
wslPath,
134+
() =>
135+
{
136+
fallbackInvoked = true;
137+
throw new InvalidOperationException("Fallback should not be consulted.");
138+
});
139+
140+
Assert.True(result.IsTrusted, result.Detail);
141+
Assert.False(fallbackInvoked);
142+
}
143+
56144
[Theory]
57145
[InlineData("CN=Microsoft Windows, O=Microsoft Corporation, C=US", true)]
58146
[InlineData("CN=Microsoft Corporation Test Certificate, O=Example Corp, C=US", false)]

0 commit comments

Comments
 (0)