44
55using System . Diagnostics . CodeAnalysis ;
66using System . Runtime . InteropServices ;
7- using System . Security . AccessControl ;
87using System . Security . Principal ;
98using System . Threading ;
109using Microsoft . Win32 . SafeHandles ;
@@ -25,15 +24,10 @@ private bool TryConnect(int timeout, CancellationToken cancellationToken)
2524 {
2625 Interop . Kernel32 . SECURITY_ATTRIBUTES secAttrs = PipeStream . GetSecAttrs ( _inheritability ) ;
2726
28- if ( ( _pipeOptions & PipeOptions . CurrentUserOnly ) != 0 )
29- {
30- // We need to remove this flag from options because it is not a valid flag for windows PInvoke to create a pipe.
31- _pipeOptions &= ~ PipeOptions . CurrentUserOnly ;
32- }
33-
34- Debug . Assert ( ( options & PipeOptions . CurrentUserOnly ) == 0 ) ;
35-
36- int _pipeFlags = ( int ) _pipeOptions ;
27+ // PipeOptions.CurrentUserOnly is special since it doesn't match directly to a corresponding Win32 valid flag.
28+ // Remove it, while keeping others untouched since historically this has been used as a way to pass flags to
29+ // CreateNamedPipeClient that were not defined in the enumeration.
30+ int _pipeFlags = ( int ) ( _pipeOptions & ~ PipeOptions . CurrentUserOnly ) ;
3731 if ( _impersonationLevel != TokenImpersonationLevel . None )
3832 {
3933 _pipeFlags |= Interop . Kernel32 . SecurityOptions . SECURITY_SQOS_PRESENT ;
@@ -142,13 +136,16 @@ private void ValidateRemotePipeUser()
142136 if ( ! IsCurrentUserOnly )
143137 return ;
144138
145- SecurityIdentifier currentUserSid = WindowsIdentity . GetCurrent ( ) . Owner ;
146139 PipeSecurity accessControl = this . GetAccessControl ( ) ;
147140 IdentityReference remoteOwnerSid = accessControl . GetOwner ( typeof ( SecurityIdentifier ) ) ;
148- if ( remoteOwnerSid != currentUserSid )
141+ using ( WindowsIdentity currentIdentity = WindowsIdentity . GetCurrent ( ) )
149142 {
150- State = PipeState . Closed ;
151- throw new UnauthorizedAccessException ( SR . UnauthorizedAccess_NotOwnedByCurrentUser ) ;
143+ SecurityIdentifier currentUserSid = currentIdentity . Owner ;
144+ if ( remoteOwnerSid != currentUserSid )
145+ {
146+ State = PipeState . Closed ;
147+ throw new UnauthorizedAccessException ( SR . UnauthorizedAccess_NotOwnedByCurrentUser ) ;
148+ }
152149 }
153150 }
154151
0 commit comments