Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit d52ee27

Browse files
Paulo Janottiericstj
authored andcommitted
Fix build and add using to WindowsIdentity objects
1 parent d7cbcde commit d52ee27

File tree

2 files changed

+22
-22
lines changed

2 files changed

+22
-22
lines changed

src/System.IO.Pipes/src/System/IO/Pipes/NamedPipeClientStream.Windows.cs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
using System.Diagnostics.CodeAnalysis;
66
using System.Runtime.InteropServices;
7-
using System.Security.AccessControl;
87
using System.Security.Principal;
98
using System.Threading;
109
using 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

src/System.IO.Pipes/src/System/IO/Pipes/NamedPipeServerStream.Windows.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,22 @@ private void Create(string pipeName, PipeDirection direction, int maxNumberOfSer
4343

4444
if (IsCurrentUserOnly)
4545
{
46-
SecurityIdentifier identifier = WindowsIdentity.GetCurrent().Owner;
47-
PipeAccessRule rule = new PipeAccessRule(identifier, PipeAccessRights.ReadWrite, AccessControlType.Allow);
48-
pipeSecurity = new PipeSecurity();
46+
using (WindowsIdentity currentIdentity = WindowsIdentity.GetCurrent())
47+
{
48+
SecurityIdentifier identifier = currentIdentity.Owner;
49+
PipeAccessRule rule = new PipeAccessRule(identifier, PipeAccessRights.ReadWrite, AccessControlType.Allow);
50+
pipeSecurity = new PipeSecurity();
4951

50-
pipeSecurity.AddAccessRule(rule);
51-
pipeSecurity.SetOwner(identifier);
52+
pipeSecurity.AddAccessRule(rule);
53+
pipeSecurity.SetOwner(identifier);
54+
}
5255

53-
// We need to remove this flag from options because it is not a valid flag for windows PInvoke to create a pipe.
56+
// PipeOptions.CurrentUserOnly is special since it doesn't match directly to a corresponding Win32 valid flag.
57+
// Remove it, while keeping others untouched since historically this has been used as a way to pass flags to CreateNamedPipe
58+
// that were not defined in the enumeration.
5459
options &= ~PipeOptions.CurrentUserOnly;
5560
}
5661

57-
Debug.Assert((options & PipeOptions.CurrentUserOnly) == 0);
58-
5962
int openMode = ((int)direction) |
6063
(maxNumberOfServerInstances == 1 ? Interop.Kernel32.FileOperations.FILE_FLAG_FIRST_PIPE_INSTANCE : 0) |
6164
(int)options;

0 commit comments

Comments
 (0)