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

Commit 29cd6a0

Browse files
safernPaulo Janotti
authored andcommitted
Expose PipeOptions.CurrentUserOnly and add implementation when flag is passed (#26395)
* Expose PipeOptions.CurrentUserOnly and add windows implementation when flag is passed * PR Feedback * Add missing if statement * Move PipeSecurity types in implementation to System.IO.Pipes * PR Feedback and move tests to netcoreapp file * Add CurrentUserOnly implementation for NamedPipeClient in Unix * PR Feedback * Refactor to not have duplicated code * Implement server side current option only in named pipes for unix * More PR Feedback * PR Feedback round 3 * Add more tests * Fix build and add using to WindowsIdentity objects * Fix packaging issues * netstandard-Windows_NT needs to be built from sources
1 parent 0fbb5f1 commit 29cd6a0

26 files changed

+333
-52
lines changed

pkg/Microsoft.Private.CoreFx.NETCoreApp/Microsoft.Private.CoreFx.NETCoreApp.pkgproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
Microsoft.VisualBasic;
4040
Microsoft.Win32.Registry;
4141
System.IO.FileSystem.AccessControl;
42+
System.IO.Pipes.AccessControl;
4243
System.Private.DataContractSerialization;
4344
System.Private.Uri;
4445
System.Private.Xml;

pkg/Microsoft.Private.PackageBaseline/packageIndex.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2154,7 +2154,9 @@
21542154
"4.4.0"
21552155
],
21562156
"BaselineVersion": "4.4.0",
2157-
"InboxOn": {},
2157+
"InboxOn": {
2158+
"uap10.0.16300": "4.0.3.0"
2159+
},
21582160
"AssemblyVersionInPackageVersion": {
21592161
"4.0.1.0": "4.3.0",
21602162
"4.0.2.0": "4.4.0",

src/System.IO.Pipes.AccessControl/dir.props

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@
44
<PropertyGroup>
55
<AssemblyVersion>4.0.3.0</AssemblyVersion>
66
<AssemblyKey>MSFT</AssemblyKey>
7+
<IsNETCoreApp>true</IsNETCoreApp>
8+
<IsNETCoreAppRef>false</IsNETCoreAppRef>
9+
<IsUAP>true</IsUAP>
710
</PropertyGroup>
811
</Project>

src/System.IO.Pipes.AccessControl/pkg/System.IO.Pipes.AccessControl.pkgproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@
33
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />
44
<ItemGroup>
55
<ProjectReference Include="..\ref\System.IO.Pipes.AccessControl.csproj">
6-
<SupportedFramework>net461;netcoreapp2.0;$(AllXamarinFrameworks)</SupportedFramework>
6+
<SupportedFramework>net461;netcoreapp2.0;$(UAPvNextTFM);$(AllXamarinFrameworks)</SupportedFramework>
77
</ProjectReference>
88
<ProjectReference Include="..\src\System.IO.Pipes.AccessControl.csproj" />
99
<HarvestIncludePaths Include="ref/net46;lib/net46;runtimes/win/lib/net46" />
1010
<HarvestIncludePaths Include="ref/netstandard1.3">
1111
<SupportedFramework>netcore50</SupportedFramework>
1212
</HarvestIncludePaths>
1313
<HarvestIncludePaths Include="runtimes/win/lib/netstandard1.3;lib/netstandard1.3" />
14+
<InboxOnTargetFramework Include="$(UAPvNextTFM)" />
15+
<File Include="$(PlaceHolderFile)">
16+
<TargetPath>runtimes/win/lib/$(UAPvNextTFM)</TargetPath>
17+
</File>
1418
</ItemGroup>
1519
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
1620
</Project>

src/System.IO.Pipes.AccessControl/src/Configurations.props

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
<PropertyGroup>
44
<BuildConfigurations>
55
netfx-Windows_NT;
6+
netcoreapp-Windows_NT;
7+
uap-Windows_NT;
68
netstandard-Windows_NT;
79
netstandard;
810
</BuildConfigurations>

src/System.IO.Pipes.AccessControl/src/System.IO.Pipes.AccessControl.csproj

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,38 @@
44
<PropertyGroup>
55
<AssemblyName>System.IO.Pipes.AccessControl</AssemblyName>
66
<ProjectGuid>{40059634-BB03-4A6F-8657-CCE2D376BC8B}</ProjectGuid>
7-
<AllowUnsafeBlocks Condition="'$(TargetGroup)'=='netstandard'">true</AllowUnsafeBlocks>
7+
<IncludeDefaultReferences Condition="'$(TargetGroup)' == 'netcoreapp' OR '$(TargetGroup)' == 'uap'">false</IncludeDefaultReferences>
8+
<AllowUnsafeBlocks Condition="'$(TargetGroup)' == 'netstandard'">true</AllowUnsafeBlocks>
89
<GeneratePlatformNotSupportedAssemblyMessage Condition="'$(TargetGroup)' == 'netstandard' AND '$(TargetsWindows)' != 'true'">SR.PlatformNotSupported_AccessControl</GeneratePlatformNotSupportedAssemblyMessage>
9-
<IsPartialFacadeAssembly Condition="'$(TargetGroup)'=='netfx'">true</IsPartialFacadeAssembly>
10+
<IsPartialFacadeAssembly Condition="'$(TargetGroup)' != 'netstandard'">true</IsPartialFacadeAssembly>
1011
</PropertyGroup>
11-
<!-- Default configurations to help VS understand the options -->
1212
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'netfx-Windows_NT-Debug|AnyCPU'" />
1313
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'netfx-Windows_NT-Release|AnyCPU'" />
1414
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'netstandard-Debug|AnyCPU'" />
1515
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'netstandard-Release|AnyCPU'" />
1616
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'netstandard-Windows_NT-Debug|AnyCPU'" />
1717
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'netstandard-Windows_NT-Release|AnyCPU'" />
18-
<ItemGroup Condition="'$(IsPartialFacadeAssembly)'!='true' AND '$(TargetsWindows)'=='true'">
19-
<Compile Include="System\IO\PipeSecurity.cs" />
20-
<Compile Include="System\IO\PipeAccessRights.cs" />
21-
<Compile Include="System\IO\PipeAccessRule.cs" />
22-
<Compile Include="System\IO\PipeAuditRule.cs" />
23-
<Compile Include="System\IO\PipesAclExtensions.cs" />
18+
<ItemGroup Condition="'$(TargetGroup)' == 'netstandard' AND '$(TargetsWindows)' == 'true'">
19+
<Compile Include="..\..\System.IO.Pipes\src\System\IO\Pipes\PipeSecurity.cs" />
20+
<Compile Include="..\..\System.IO.Pipes\src\System\IO\Pipes\PipeAccessRights.cs" />
21+
<Compile Include="..\..\System.IO.Pipes\src\System\IO\Pipes\PipeAccessRule.cs" />
22+
<Compile Include="..\..\System.IO.Pipes\src\System\IO\Pipes\PipeAuditRule.cs" />
23+
<Compile Include="..\..\System.IO.Pipes\src\System\IO\Pipes\PipesAclExtensions.cs" />
2424
</ItemGroup>
25-
<ItemGroup Condition="'$(TargetGroup)'=='netfx'">
25+
<ItemGroup Condition="'$(TargetGroup)' == 'netfx'">
2626
<Reference Include="mscorlib" />
2727
<Reference Include="System.Core" />
2828
<Compile Include="System\IO\PipesAclExtensions.net46.cs" />
2929
</ItemGroup>
30-
<ItemGroup Condition="'$(TargetGroup)'!='netfx'">
30+
<ItemGroup Condition="'$(TargetGroup)' == 'netstandard'">
3131
<Reference Include="System.Security.AccessControl" />
3232
<Reference Include="System.Security.Principal.Windows" />
3333
</ItemGroup>
34+
<ItemGroup Condition="'$(TargetGroup)' == 'netcoreapp' OR '$(TargetGroup)' == 'uap'">
35+
<Reference Include="System.Runtime" />
36+
<Reference Include="System.Resources.ResourceManager" />
37+
<ProjectReference Include="..\..\System.IO.Pipes\src\System.IO.Pipes.csproj" />
38+
<ProjectReference Include="..\..\System.Security.AccessControl\src\System.Security.AccessControl.csproj" />
39+
</ItemGroup>
3440
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
35-
</Project>
41+
</Project>

src/System.IO.Pipes/ref/System.IO.Pipes.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ public enum PipeDirection
9090
public enum PipeOptions
9191
{
9292
Asynchronous = 1073741824,
93+
CurrentUserOnly = 536870912,
9394
None = 0,
9495
WriteThrough = -2147483648,
9596
}

src/System.IO.Pipes/src/Resources/Strings.resx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,18 @@
120120
<data name="ArgumentOutOfRange_NeedNonNegNum" xml:space="preserve">
121121
<value>Non negative number is required.</value>
122122
</data>
123+
<data name="ArgumentOutOfRange_NeedValidPipeAccessRights" xml:space="preserve">
124+
<value>Invalid PipeAccessRights value.</value>
125+
</data>
123126
<data name="Argument_InvalidOffLen" xml:space="preserve">
124127
<value>Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection.</value>
125128
</data>
126129
<data name="Argument_NeedNonemptyPipeName" xml:space="preserve">
127130
<value>pipeName cannot be an empty string.</value>
128131
</data>
132+
<data name="Argument_NonContainerInvalidAnyFlag" xml:space="preserve">
133+
<value>This flag may not be set on a pipe.</value>
134+
</data>
129135
<data name="Argument_EmptyServerName" xml:space="preserve">
130136
<value>serverName cannot be an empty string. Use \\\".\\\" for current machine.</value>
131137
</data>
@@ -201,6 +207,9 @@
201207
<data name="IO_FileExists_Name" xml:space="preserve">
202208
<value>The file '{0}' already exists.</value>
203209
</data>
210+
<data name="IO_IO_PipeBroken" xml:space="preserve">
211+
<value>Pipe is broken.</value>
212+
</data>
204213
<data name="IO_OperationAborted" xml:space="preserve">
205214
<value>IO operation was aborted unexpectedly.</value>
206215
</data>
@@ -273,4 +282,7 @@
273282
<data name="IO_PathTooLong_Path" xml:space="preserve">
274283
<value>The path '{0}' is too long, or a component of the specified path is too long.</value>
275284
</data>
285+
<data name="UnauthorizedAccess_NotOwnedByCurrentUser" xml:space="preserve">
286+
<value>Could not connect to the pipe because it was not owned by the current user.</value>
287+
</data>
276288
</root>

src/System.IO.Pipes/src/System.IO.Pipes.csproj

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,12 @@
140140
<Compile Include="System\IO\Pipes\ConnectionCompletionSource.cs" />
141141
<Compile Include="System\IO\Pipes\NamedPipeClientStream.Windows.cs" />
142142
<Compile Include="System\IO\Pipes\NamedPipeServerStream.Windows.cs" />
143+
<Compile Include="System\IO\Pipes\PipeAccessRights.cs" />
144+
<Compile Include="System\IO\Pipes\PipeAccessRule.cs" />
145+
<Compile Include="System\IO\Pipes\PipeAuditRule.cs" />
146+
<Compile Include="System\IO\Pipes\PipesAclExtensions.cs" />
143147
<Compile Include="System\IO\Pipes\PipeCompletionSource.cs" />
148+
<Compile Include="System\IO\Pipes\PipeSecurity.cs" />
144149
<Compile Include="System\IO\Pipes\PipeStream.Windows.cs" />
145150
<Compile Include="System\IO\Pipes\ReadWriteCompletionSource.cs" />
146151
</ItemGroup>
@@ -177,6 +182,9 @@
177182
<Compile Include="$(CommonPath)\Interop\Unix\Interop.IOErrors.cs">
178183
<Link>Common\Interop\Unix\Interop.IOErrors.cs</Link>
179184
</Compile>
185+
<Compile Include="$(CommonPath)\Interop\Unix\System.Native\Interop.ChMod.cs">
186+
<Link>Common\Interop\Unix\Interop.ChMod.cs</Link>
187+
</Compile>
180188
<Compile Include="$(CommonPath)\Interop\Unix\System.Native\Interop.Close.cs">
181189
<Link>Common\Interop\Unix\Interop.Close.cs</Link>
182190
</Compile>
@@ -204,7 +212,7 @@
204212
<Compile Include="$(CommonPath)\Interop\Unix\System.Native\Interop.OpenFlags.cs">
205213
<Link>Common\Interop\Unix\Interop.OpenFlags.cs</Link>
206214
</Compile>
207-
<Compile Include="$(CommonPath)\Interop\Unix\System.Native\Interop.Permissions.cs">
215+
<Compile Include="$(CommonPath)\CoreLib\Interop\Unix\System.Native\Interop.Permissions.cs">
208216
<Link>Common\Interop\Unix\Interop.Permissions.cs</Link>
209217
</Compile>
210218
<Compile Include="$(CommonPath)\Interop\Unix\System.Native\Interop.Pipe.cs">
@@ -255,8 +263,15 @@
255263
<Reference Include="System.Threading.Overlapped" />
256264
<Reference Include="System.Threading.Tasks" />
257265
</ItemGroup>
266+
<ItemGroup Condition="'$(TargetsWindows)' == 'true'">
267+
<Reference Include="System.Collections.NonGeneric" />
268+
<Reference Include="System.Security.AccessControl" />
269+
<Reference Include="System.Security.Principal.Windows" />
270+
<Reference Include="System.Security.Claims" />
271+
</ItemGroup>
258272
<ItemGroup Condition="'$(TargetsUnix)' == 'true'">
259273
<Reference Include="Microsoft.Win32.Primitives" />
274+
<Reference Include="System.IO.FileSystem" />
260275
<Reference Include="System.Net.Primitives" />
261276
<Reference Include="System.Net.Sockets" />
262277
</ItemGroup>

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
// See the LICENSE file in the project root for more information.
44

55
using Microsoft.Win32.SafeHandles;
6+
using System.ComponentModel;
67
using System.Diagnostics.CodeAnalysis;
8+
using System.Runtime.InteropServices;
79
using System.Net.Sockets;
810
using System.Security;
911
using System.Threading;
@@ -49,6 +51,17 @@ private bool TryConnect(int timeout, CancellationToken cancellationToken)
4951
}
5052
}
5153

54+
try
55+
{
56+
ValidateRemotePipeUser(clientHandle);
57+
}
58+
catch (Exception)
59+
{
60+
clientHandle.Dispose();
61+
socket.Dispose();
62+
throw;
63+
}
64+
5265
InitializeHandle(clientHandle, isExposed: false, isAsync: (_pipeOptions & PipeOptions.Asynchronous) != 0);
5366
State = PipeState.Connected;
5467
return true;
@@ -84,6 +97,23 @@ public override int OutBufferSize
8497
}
8598
}
8699

100+
private void ValidateRemotePipeUser(SafePipeHandle handle)
101+
{
102+
if (!IsCurrentUserOnly)
103+
return;
104+
105+
uint userId = Interop.Sys.GetEUid();
106+
if (Interop.Sys.GetPeerID(handle, out uint serverOwner) == -1)
107+
{
108+
throw CreateExceptionForLastError();
109+
}
110+
111+
if (userId != serverOwner)
112+
{
113+
throw new UnauthorizedAccessException(SR.UnauthorizedAccess_NotOwnedByCurrentUser);
114+
}
115+
}
116+
87117
// -----------------------------
88118
// ---- PAL layer ends here ----
89119
// -----------------------------

0 commit comments

Comments
 (0)