Merge security follow-ups: host-side PE-cache signing, worker no longer holds the HMAC key (#1014); Akka remoting untrusted-mode + trusted-selection-paths by default, opt-in mutual TLS (#1003)

This commit is contained in:
Joseph Doherty
2026-12-03 15:40:32 -05:00
26 changed files with 1720 additions and 105 deletions
@@ -58,7 +58,9 @@ public sealed class FleetDiagnosticsClient : IFleetDiagnosticsClient
{
// Inside the try: a malformed node value makes ActorSelection throw, and that must degrade to
// "unavailable" like a dead node does, not surface through the page's error boundary.
var selection = _system.ActorSelection($"akka.tcp://{_systemName}@{nodeId.Value}/user/driver-host");
// lmxopcua#1003: the scheme follows this node's own transport (akka.ssl.tcp under Cluster:Remoting:Tls).
var scheme = _system is ExtendedActorSystem ext ? ext.Provider.DefaultAddress.Protocol : "akka.tcp";
var selection = _system.ActorSelection($"{scheme}://{_systemName}@{nodeId.Value}/user/driver-host");
using var linked = CancellationTokenSource.CreateLinkedTokenSource(ct);
linked.CancelAfter(AskTimeout);
return await selection.Ask<NodeDiagnosticsSnapshot>(
@@ -78,7 +78,6 @@ internal sealed class WorkerHost
private int _queueCapacity = 4096;
private TimeSpan _hardGrace = TimeSpan.FromSeconds(1);
private TimeSpan _heartbeatInterval = TimeSpan.FromSeconds(1);
private ScriptPeCacheStore? _peCache;
/// <summary>
/// lmxopcua#827: this worker runs each evaluation's soft clock from the run's start and reports the crossing
@@ -129,6 +128,8 @@ internal sealed class WorkerHost
Pid = Environment.ProcessId,
RuntimeVersion = Environment.Version.ToString(),
WorkerSoftClock = _workerSoftClock,
// lmxopcua#1014: lets the host decide whether its compiled images bind here.
PeImageAbi = ScriptSandbox.ImageAbi,
},
});
ScriptWorkerFrameCodec.Write(_pipe, ready, _maxFrameBytes);
@@ -161,11 +162,9 @@ internal sealed class WorkerHost
if (hello.CompileThreads > 0) _compileThreads = (int)Math.Min(hello.CompileThreads, 64);
_workerSoftClock = hello.WorkerSoftClock;
_testStallReaderOnDiag = hello.TestStallReaderOnDiag;
if (!string.IsNullOrEmpty(hello.PeCacheDir) && hello.PeCacheKey.Length == ScriptPeCacheStore.KeyLength)
{
// Design §7: the host owns the key; it reaches us only here, never through the environment or a file.
_peCache = new ScriptPeCacheStore(hello.PeCacheDir, hello.PeCacheKey.ToByteArray());
}
// lmxopcua#1014: no PE-cache key or directory any more — the host verifies, compiles and signs, and sends a
// verified image inside the load (ScriptSource.pe_image). This process runs Designer code; it holds nothing that
// would let that code persist.
}
private void StartBackground(int evalThreads)
@@ -306,7 +305,7 @@ internal sealed class WorkerHost
var outcome = new LoadOutcome { Key = script.Key, Ok = true };
try
{
var loaded = LoadedScript.Load(script, _peCache, out var cache);
var loaded = LoadedScript.Load(script, out var cache);
outcome.Cache = cache;
if (_scripts.TryRemove(script.Key, out var previous)) previous.Dispose();
_scripts[script.Key] = loaded;
@@ -723,49 +722,44 @@ internal sealed class WorkerHost
public string[] InputPaths { get; }
/// <summary>
/// Loads a script: from a verified PE cache entry when there is one (Roslyn is not entered), otherwise by
/// compiling it and writing the entry (design §7). A compile error is never cached — the host's negative
/// cache covers that — and an entry that fails verification is deleted by the store and recompiled.
/// Loads a script: from the image the host sent with it when there is one (Roslyn is not entered), otherwise by
/// compiling the source. The image comes from the host's PE cache (lmxopcua#1014): the host verified it under a
/// key this process never holds and emitted it with its own compile pipeline, analyzer included, so it is
/// loaded as the host's own compile would be. One that does not load is reported (<see cref="PeCache.Rejected"/>)
/// and the source compiled; the host evicts the entry.
/// </summary>
public static LoadedScript Load(ScriptSource source, ScriptPeCacheStore? cache, out PeCache how) => source.Kind switch
public static LoadedScript Load(ScriptSource source, out PeCache how) => source.Kind switch
{
ContextKind.VirtualTag or ContextKind.Calculation => new LoadedScript(
[.. source.InputPaths], Load<VirtualTagContext, object?>(source, cache, out how), null),
[.. source.InputPaths], Load<VirtualTagContext, object?>(source, out how), null),
ContextKind.AlarmPredicate => new LoadedScript(
[.. source.InputPaths], null, Load<AlarmPredicateContext, bool>(source, cache, out how)),
[.. source.InputPaths], null, Load<AlarmPredicateContext, bool>(source, out how)),
_ => throw new ScriptWorkerProtocolException($"unknown script kind {source.Kind}"),
};
private static ScriptEvaluator<TContext, TResult> Load<TContext, TResult>(ScriptSource source, ScriptPeCacheStore? cache,
out PeCache how) where TContext : ScriptContext
private static ScriptEvaluator<TContext, TResult> Load<TContext, TResult>(ScriptSource source, out PeCache how)
where TContext : ScriptContext
{
if (cache is null)
{
how = PeCache.None;
return ScriptEvaluator<TContext, TResult>.Compile(source.Source);
}
var id = ScriptPeCacheStore.EntryId(ScriptEvaluator<TContext, TResult>.CacheFingerprint, (int)source.Kind, source.Source);
var lookup = cache.TryRead(id, out var image);
if (lookup == PeCacheLookup.Hit)
if (!source.PeImage.IsEmpty)
{
try
{
var evaluator = ScriptEvaluator<TContext, TResult>.FromImage(source.PeImage.ToByteArray());
how = PeCache.Hit;
return ScriptEvaluator<TContext, TResult>.FromImage(image!);
return evaluator;
}
catch (Exception ex) when (ex is BadImageFormatException or InvalidOperationException or TypeLoadException or MissingMethodException)
catch (Exception ex) when (ex is BadImageFormatException or InvalidOperationException or TypeLoadException
or MissingMethodException or FileLoadException or FileNotFoundException
or InvalidCastException or ArgumentException)
{
// Authenticated but unloadable: only possible across a runtime change the fingerprint missed.
try { File.Delete(cache.EntryPath(id)); } catch (IOException) { }
lookup = PeCacheLookup.Rejected;
// Verified by the host but unloadable here: a runtime difference the image ABI did not capture.
how = PeCache.Rejected;
return ScriptEvaluator<TContext, TResult>.Compile(source.Source);
}
}
var compiled = ScriptEvaluator<TContext, TResult>.CompileToImage(source.Source);
cache.TryWrite(id, compiled);
how = lookup == PeCacheLookup.Rejected ? PeCache.Rejected : PeCache.Miss;
return ScriptEvaluator<TContext, TResult>.FromImage(compiled);
how = PeCache.None;
return ScriptEvaluator<TContext, TResult>.Compile(source.Source);
}
public object? Run(IReadOnlyDictionary<string, DataValueSnapshot> inputs, ILogger logger, Func<DateTime> clock,