diff options
author | Maggie Tsang <mbytsang@gmail.com> | 2017-08-03 01:49:41 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-08-03 01:49:41 +0300 |
commit | d211fc3ad8541b33d6912afc6354757d3b3981dc (patch) | |
tree | 709b4702fc91b8992da9f2beee7ffffe63ee7fed /src/CoreFx.Private.TestUtilities | |
parent | 8f0edb4ab37fca628eeeb6a240dee17fc717438c (diff) |
Elevation Helpers (#22836)
* Elevation Helpers
Detects if user is running as elevated.
* Fix unsafe blocks.
Diffstat (limited to 'src/CoreFx.Private.TestUtilities')
6 files changed, 124 insertions, 12 deletions
diff --git a/src/CoreFx.Private.TestUtilities/CoreFx.Private.TestUtilities.sln b/src/CoreFx.Private.TestUtilities/CoreFx.Private.TestUtilities.sln index 442da093c7..670e1b54e1 100644 --- a/src/CoreFx.Private.TestUtilities/CoreFx.Private.TestUtilities.sln +++ b/src/CoreFx.Private.TestUtilities/CoreFx.Private.TestUtilities.sln @@ -1,6 +1,6 @@ Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 14 -VisualStudioVersion = 14.0.25420.1 +# Visual Studio 15 +VisualStudioVersion = 15.0.26430.16 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CoreFx.Private.TestUtilities.Tests", "tests\CoreFx.Private.TestUtilities.Tests.csproj", "{5E0DB390-A45E-41BE-8304-B840327FE597}" ProjectSection(ProjectDependencies) = postProject @@ -17,6 +17,9 @@ EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{1A2F9F4A-A032-433E-B914-ADD5992BB178}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{E107E9C1-E893-4E87-987E-04EF0DCEAEFD}" + ProjectSection(SolutionItems) = preProject + src\Configurations.props = src\Configurations.props + EndProjectSection EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ref", "ref", "{2E666815-2EDB-464B-9DF6-380BF4789AD4}" EndProject diff --git a/src/CoreFx.Private.TestUtilities/ref/CoreFx.Private.TestUtilities.cs b/src/CoreFx.Private.TestUtilities/ref/CoreFx.Private.TestUtilities.cs index 61b5aefd8c..20afd67608 100644 --- a/src/CoreFx.Private.TestUtilities/ref/CoreFx.Private.TestUtilities.cs +++ b/src/CoreFx.Private.TestUtilities/ref/CoreFx.Private.TestUtilities.cs @@ -13,6 +13,7 @@ namespace System public static class AdminHelpers { public static int RunAsSudo(string commandLine) => throw null; + public static bool IsProcessElevated() => throw null; } public static class AssertExtensions diff --git a/src/CoreFx.Private.TestUtilities/src/CoreFx.Private.TestUtilities.csproj b/src/CoreFx.Private.TestUtilities/src/CoreFx.Private.TestUtilities.csproj index f67502b3ff..b23505cd02 100644 --- a/src/CoreFx.Private.TestUtilities/src/CoreFx.Private.TestUtilities.csproj +++ b/src/CoreFx.Private.TestUtilities/src/CoreFx.Private.TestUtilities.csproj @@ -7,17 +7,17 @@ <RuntimeProjectFile>$(ProjectDir)\external\test-runtime\XUnit.Runtime.depproj</RuntimeProjectFile> <ClsCompliant>false</ClsCompliant> <ShouldWriteSigningRequired>false</ShouldWriteSigningRequired> + <AllowUnsafeBlocks>true</AllowUnsafeBlocks> </PropertyGroup> <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'netcoreapp-Unix-Debug|AnyCPU'" /> <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'netcoreapp-Unix-Release|AnyCPU'" /> - <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'netcoreapp-Windows_NT-Debug|AnyCPU'" /> - <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'netcoreapp-Windows_NT-Release|AnyCPU'" /> - <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'uap-Windows_NT-Debug|AnyCPU'" /> - <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'uap-Windows_NT-Release|AnyCPU'" /> - <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'uapaot-Windows_NT-Debug|AnyCPU'" /> - <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'uapaot-Windows_NT-Release|AnyCPU'" /> - <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'netfx-Windows_NT-Debug|AnyCPU'" /> - <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'netfx-Windows_NT-Release|AnyCPU'" /> + <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'netcoreapp-Windows_NT-Debug|AnyCPU'"/> + <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'netcoreapp-Windows_NT-Release|AnyCPU'"/> + <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'uap-Windows_NT-Debug|AnyCPU'"/> + <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'uap-Windows_NT-Release|AnyCPU'"/> + <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'uapaot-Windows_NT-Debug|AnyCPU'"/> + <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'uapaot-Windows_NT-Release|AnyCPU'"/> + <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'netfx-Windows_NT-Debug|AnyCPU'"/> <ItemGroup> <Compile Include="System\AdminHelpers.cs" /> <Compile Include="System\IO\FileCleanupTestBase.cs" /> @@ -43,10 +43,51 @@ <Reference Include="System.Diagnostics.Process" /> <Reference Include="System.ComponentModel.Primitives" /> <Reference Include="System.Runtime.InteropServices.RuntimeInformation" /> + <Reference Include="System.Runtime.InteropServices" /> <ReferenceFromRuntime Include="xunit.core" /> <ReferenceFromRuntime Include="Xunit.NetCore.Extensions" /> <ReferenceFromRuntime Include="xunit.assert" /> </ItemGroup> + <ItemGroup Condition="'$(TargetsWindows)' == 'true'"> + <Reference Include="System.Security.Principal.Windows" /> + <Reference Include="Microsoft.Win32.Primitives" /> + </ItemGroup> + <ItemGroup Condition="'$(TargetsWindows)' == 'true'"> + <Compile Include="$(CommonPath)\Interop\Windows\kernel32\Interop.GetCurrentProcess_IntPtr.cs"> + <Link>Common\Interop\Windows\kernel32\Interop.GetCurrentProcess_IntPtr.cs</Link> + </Compile> + <Compile Include="$(CommonPath)\Interop\Windows\advapi32\Interop.OpenProcessToken_SafeAccessTokenHandle.cs"> + <Link>Common\Interop\Windows\advapi32\Interop.OpenProcessToken_SafeAccessTokenHandle.cs</Link> + </Compile> + <Compile Include="$(CommonPath)\Interop\Windows\Interop.Libraries.cs"> + <Link>Common\Interop\Windows\Interop.Libraries.cs</Link> + </Compile> + <Compile Include="$(CommonPath)\Interop\Windows\kernel32\Interop.CloseHandle.cs"> + <Link>Common\Interop\Windows\kernel32\Interop.CloseHandle.cs</Link> + </Compile> + <Compile Include="$(CommonPath)\Interop\Windows\advapi32\Interop.TOKEN_INFORMATION_CLASS.cs"> + <Link>Common\Interop\Windows\advapi32\Interop.TOKEN_INFORMATION_CLASS.cs</Link> + </Compile> + <Compile Include="$(CommonPath)\Interop\Windows\advapi32\Interop.GetTokenInformation_void.cs"> + <Link>Common\Interop\Windows\advapi32\Interop.GetTokenInformation_void.cs</Link> + </Compile> + <Compile Include="$(CommonPath)\Interop\Windows\advapi32\Interop.TOKEN_ELEVATION.cs"> + <Link>Common\Interop\Windows\advapi32\Interop.TOKEN_ELEVATION.cs</Link> + </Compile> + <Compile Include="$(CommonPath)\Interop\Windows\Interop.BOOL.cs"> + <Link>Common\Interop\Windows\Interop.BOOL.cs</Link> + </Compile> + <Compile Include="System\AdminHelpers.Windows.cs" /> + </ItemGroup> + <ItemGroup Condition="'$(TargetsUnix)' == 'true'"> + <Compile Include="System\AdminHelpers.Unix.cs" /> + <Compile Include="$(CommonPath)\Interop\Unix\System.Native\Interop.GetEUid.cs"> + <Link>Common\Interop\Unix\Interop.GetEUid.cs</Link> + </Compile> + <Compile Include="$(CommonPath)\Interop\Unix\Interop.Libraries.cs"> + <Link>Common\Interop\Unix\Interop.Libraries.cs</Link> + </Compile> + </ItemGroup> <ItemGroup Condition="'$(TargetGroup)' == 'uap'"> <Reference Include="Windows" /> <Reference Include="mscorlib" /> diff --git a/src/CoreFx.Private.TestUtilities/src/System/AdminHelpers.Unix.cs b/src/CoreFx.Private.TestUtilities/src/System/AdminHelpers.Unix.cs new file mode 100644 index 0000000000..dd20faab20 --- /dev/null +++ b/src/CoreFx.Private.TestUtilities/src/System/AdminHelpers.Unix.cs @@ -0,0 +1,24 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace System +{ + /// <summary> + /// Returns true if the current process is elevated (in Unix). + /// </summary> + public static partial class AdminHelpers + { + public unsafe static bool IsProcessElevated() + { + uint userId = Interop.Sys.GetEUid(); + return(userId == 0); + } + } +} diff --git a/src/CoreFx.Private.TestUtilities/src/System/AdminHelpers.Windows.cs b/src/CoreFx.Private.TestUtilities/src/System/AdminHelpers.Windows.cs new file mode 100644 index 0000000000..64173a9448 --- /dev/null +++ b/src/CoreFx.Private.TestUtilities/src/System/AdminHelpers.Windows.cs @@ -0,0 +1,44 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.ComponentModel; +using System.Runtime.InteropServices; +using System.Security.Principal; +using Microsoft.Win32.SafeHandles; + +namespace System +{ + public static partial class AdminHelpers + { + /// <summary> + /// Returns true if the current process is elevated (in Windows). + /// </summary> + public unsafe static bool IsProcessElevated() + { + IntPtr processHandle = Interop.Kernel32.GetCurrentProcess(); + SafeAccessTokenHandle token; + if (!Interop.Advapi32.OpenProcessToken(processHandle, TokenAccessLevels.Read, out token)) + { + throw new Win32Exception(Marshal.GetLastWin32Error(), "Open process token failed"); + } + + using (token) + { + Interop.Advapi32.TOKEN_ELEVATION elevation = new Interop.Advapi32.TOKEN_ELEVATION(); + uint ignore; + if (!Interop.Advapi32.GetTokenInformation( + token, + Interop.Advapi32.TOKEN_INFORMATION_CLASS.TokenElevation, + &elevation, + (uint)sizeof(Interop.Advapi32.TOKEN_ELEVATION), + out ignore)) + { + throw new Win32Exception(Marshal.GetLastWin32Error(), "Get token information failed"); + } + return elevation.TokenIsElevated != Interop.BOOL.FALSE; + } + } + + } +} diff --git a/src/CoreFx.Private.TestUtilities/src/System/AdminHelpers.cs b/src/CoreFx.Private.TestUtilities/src/System/AdminHelpers.cs index ca13f24382..526e6bd500 100644 --- a/src/CoreFx.Private.TestUtilities/src/System/AdminHelpers.cs +++ b/src/CoreFx.Private.TestUtilities/src/System/AdminHelpers.cs @@ -7,9 +7,8 @@ using Xunit; namespace System { - public static class AdminHelpers + public static partial class AdminHelpers { - /// <summary> /// Runs the given command as sudo (for Unix). /// </summary> |