feat(scripting): ScriptWorker isolation on Windows: restricted worker identity, pipe ACL, firewall + PeCacheDir installer (lmxopcua#780)
v2-ci / unit-tests (push) Has been cancelled
v2-ci / unit-tests (push) Has been cancelled
Owner decisions §14 #5/#6 as built (design §12.5): - Scripting:Worker:Identity (default unset, Windows only, fail-closed validation): each worker starts under a restricted token derived from the host's. Every privilege but SeChangeNotify is removed; the restricting SIDs are NT SERVICE\OtOpcUaScriptWorker's SID + Everyone + Users + RESTRICTED; the token and default DACLs are host + SYSTEM + identity + RESTRICTED. The worker is created suspended, put in its Job Object before it runs, inherits only its stdio pipes, gets an ACL'd work dir as TEMP, and runs with DOTNET_EnableDiagnostics=0. The identity is granted on the host's window station and desktop, because USER32 init made coreclr.dll fail with ERROR_DLL_INIT_FAILED on windev. The logon SID is not used because the host's process DACL gives it PROCESS_VM_READ. - The worker pipe on Windows always has an explicit protected DACL: host account + identity only. There is no owner ACE, so an elevated host's Administrators gets no access. - The worker treats access denied on its parent-pid poll as alive; the Job Object covers that case. - scripts/install/Install-ScriptWorkerIsolation.ps1 + ScriptWorkerIsolation.psm1 are idempotent and support -WhatIf, -Uninstall and -PlanOnly. They create the disabled anchor service, the PeCacheDir ACL (protected, no Users, key and markers host-only, pe\ Modify for the identity), read+execute on script-worker\, a ReadData deny on the install root, the outbound firewall block on the worker exe, and optionally the host-service env. - Tests: the dry-run harness scripts/install/tests/Test-ScriptWorkerIsolation.ps1 (46 assertions, mutation-checked); ScriptWorkerIdentityTests; WorkerIsolationTests, which cover the pipe ACL, token access and a real restricted worker running a script, all green on windev. Claude-Session: https://claude.ai/code/session_01GT67AEHd2Acv1YPwGfXEoV
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
using System.Text;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Core.Scripting.OutOfProcess;
|
||||
|
||||
/// <summary>
|
||||
/// The text a native Windows process launch takes (<see cref="RestrictedWorkerLauncher"/>): the command line and the
|
||||
/// environment block. Pure string work, so it is testable on any OS.
|
||||
/// </summary>
|
||||
internal static class WindowsProcessText
|
||||
{
|
||||
/// <summary>A Windows command line for <paramref name="fileName"/> and <paramref name="arguments"/> (MSVC quoting rules).</summary>
|
||||
/// <param name="fileName">The executable.</param>
|
||||
/// <param name="arguments">The arguments.</param>
|
||||
/// <returns>The command line.</returns>
|
||||
internal static string BuildCommandLine(string fileName, IEnumerable<string> arguments)
|
||||
{
|
||||
var sb = new StringBuilder();
|
||||
AppendQuoted(sb, fileName);
|
||||
foreach (var argument in arguments)
|
||||
{
|
||||
sb.Append(' ');
|
||||
AppendQuoted(sb, argument);
|
||||
}
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
private static void AppendQuoted(StringBuilder sb, string argument)
|
||||
{
|
||||
if (argument.Length > 0 && argument.IndexOfAny([' ', '\t', '\n', '\v', '"']) < 0)
|
||||
{
|
||||
sb.Append(argument);
|
||||
return;
|
||||
}
|
||||
sb.Append('"');
|
||||
var backslashes = 0;
|
||||
foreach (var c in argument)
|
||||
{
|
||||
if (c == '\\')
|
||||
{
|
||||
backslashes++;
|
||||
continue;
|
||||
}
|
||||
if (c == '"') sb.Append('\\', (backslashes * 2) + 1);
|
||||
else sb.Append('\\', backslashes);
|
||||
backslashes = 0;
|
||||
sb.Append(c);
|
||||
}
|
||||
sb.Append('\\', backslashes * 2);
|
||||
sb.Append('"');
|
||||
}
|
||||
|
||||
/// <summary>A sorted, double-NUL-terminated UTF-16 environment block.</summary>
|
||||
/// <param name="environment">The variables.</param>
|
||||
/// <returns>The block.</returns>
|
||||
internal static string BuildEnvironmentBlock(IDictionary<string, string?> environment)
|
||||
{
|
||||
var sb = new StringBuilder();
|
||||
foreach (var (name, value) in environment.Where(kv => kv.Value is not null).OrderBy(kv => kv.Key, StringComparer.OrdinalIgnoreCase))
|
||||
sb.Append(name).Append('=').Append(value).Append('\0');
|
||||
if (sb.Length == 0) sb.Append('\0');
|
||||
sb.Append('\0');
|
||||
return sb.ToString();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user