Files
mxaccessgw/src/ZB.MOM.WW.MxGateway.Server/Sessions/WorkerPipeSecurity.cs
T
Joseph Doherty 777fef9cad
ci / windows-x86 (push) Successful in 1m26s
ci / nightly-windev (push) Has been skipped
ci / java (push) Successful in 2m30s
ci / portable (push) Failing after 4m47s
fix(sessions): drop the deny-NETWORK pipe ACE (#187)
NT AUTHORITY\NETWORK marks a network logon, not a remote caller. On windev
a gateway run from an SSH session carries it in its own token, so the deny
entry locked the gateway's same-machine workers out (UnauthorizedAccess on
connect; 15 fake-worker tests failed). The ACL now holds exactly one entry:
FullControl for the gateway's user.
2026-11-06 15:52:46 -05:00

63 lines
2.8 KiB
C#

using System.IO.Pipes;
using System.Runtime.Versioning;
using System.Security.AccessControl;
using System.Security.Principal;
namespace ZB.MOM.WW.MxGateway.Server.Sessions;
/// <summary>
/// Builds the access control applied to each gateway↔worker named pipe. The pipe carries
/// credential-bearing commands (secured writes, <c>AuthenticateUser</c>, alarm-ack passwords), so
/// its unguessable name is not enough: only the account the gateway runs as — which is also the
/// account the worker is launched as, because the launcher sets no alternate credentials — may
/// open it.
/// </summary>
/// <remarks>
/// There is deliberately no deny entry for <c>NT AUTHORITY\NETWORK</c>. That SID marks a network
/// <em>logon</em>, not a remote caller: a gateway started from an SSH or WinRM session carries it in
/// its own token, so a deny entry locked its own same-machine workers out (found on windev, #187).
/// </remarks>
internal static class WorkerPipeSecurity
{
/// <summary>Gets the options every worker pipe is created with on non-Windows hosts.</summary>
/// <remarks>
/// On Unix the pipe is a Unix domain socket; <see cref="PipeOptions.CurrentUserOnly"/> makes it
/// owner-only and rejects peers running as a different user. Windows uses the explicit ACL from
/// <see cref="Create(SecurityIdentifier)"/> instead.
/// </remarks>
public const PipeOptions UnixPipeOptions = PipeOptions.Asynchronous | PipeOptions.CurrentUserOnly;
/// <summary>Creates the pipe ACL for the account the gateway process runs as.</summary>
/// <returns>The pipe security descriptor.</returns>
[SupportedOSPlatform("windows")]
public static PipeSecurity CreateForCurrentUser()
{
using WindowsIdentity identity = WindowsIdentity.GetCurrent();
SecurityIdentifier user = identity.User
?? throw new InvalidOperationException("The gateway process identity has no user SID.");
return Create(user);
}
/// <summary>
/// Creates a pipe ACL that grants full control to <paramref name="user"/> only and does not
/// inherit any other entries.
/// </summary>
/// <param name="user">The account the gateway and its workers run as.</param>
/// <returns>The pipe security descriptor.</returns>
[SupportedOSPlatform("windows")]
public static PipeSecurity Create(SecurityIdentifier user)
{
ArgumentNullException.ThrowIfNull(user);
PipeSecurity security = new();
// Drop inherited entries so the default pipe DACL (Everyone/Anonymous read) cannot creep back in.
security.SetAccessRuleProtection(isProtected: true, preserveInheritance: false);
security.SetOwner(user);
security.AddAccessRule(new PipeAccessRule(user, PipeAccessRights.FullControl, AccessControlType.Allow));
return security;
}
}