From 0a65ff9a74959c1d1bfa1488f2f924668f4def20 Mon Sep 17 00:00:00 2001 From: Robert Holt Date: Sat, 16 Jun 2018 09:43:30 -0700 Subject: [PATCH 1/2] Remove MapSecurityZoneWithUrlmon method and related code --- .../utils/ClrFacade.cs | 105 +----------------- 1 file changed, 3 insertions(+), 102 deletions(-) diff --git a/src/System.Management.Automation/utils/ClrFacade.cs b/src/System.Management.Automation/utils/ClrFacade.cs index 03e05527904..d0c4b2538e5 100644 --- a/src/System.Management.Automation/utils/ClrFacade.cs +++ b/src/System.Management.Automation/utils/ClrFacade.cs @@ -139,19 +139,11 @@ internal static SecurityZone GetFileSecurityZone(string filePath) Diagnostics.Assert(Path.IsPathRooted(filePath), "Caller makes sure the path is rooted."); Diagnostics.Assert(Utils.FileExists(filePath), "Caller makes sure the file exists."); string sysRoot = System.Environment.GetEnvironmentVariable("SystemRoot"); - string urlmonPath = Path.Combine(sysRoot, @"System32\urlmon.dll"); - if (Utils.FileExists(urlmonPath)) - { - return MapSecurityZoneWithUrlmon(filePath); - } - return MapSecurityZoneWithoutUrlmon(filePath); + return MapSecurityZone(filePath); } - #region WithoutUrlmon - /// - /// Map the file to SecurityZone without using urlmon.dll. - /// This is needed on NanoServer because urlmon.dll is not in OneCore. + /// Map the file to SecurityZone. /// /// /// The algorithm is as follows: @@ -186,7 +178,7 @@ internal static SecurityZone GetFileSecurityZone(string filePath) /// (2) When it's a UNC path and is actually a loopback (\\127.0.0.1\c$\test.txt), "Zone.CreateFromUrl" returns "Internet", but /// the above algorithm changes it to be "MyComputer" because it's actually the same computer. /// - private static SecurityZone MapSecurityZoneWithoutUrlmon(string filePath) + private static SecurityZone MapSecurityZone(string filePath) { SecurityZone reval = ReadFromZoneIdentifierDataStream(filePath); if (reval != SecurityZone.NoZone) { return reval; } @@ -287,43 +279,6 @@ private static SecurityZone ReadFromZoneIdentifierDataStream(string filePath) return SecurityZone.NoZone; } - #endregion WithoutUrlmon - - /// - /// Map the file to SecurityZone using urlmon.dll, depending on 'IInternetSecurityManager::MapUrlToZone'. - /// - private static SecurityZone MapSecurityZoneWithUrlmon(string filePath) - { - uint zoneId; - object curSecMgr = null; - const UInt32 MUTZ_DONT_USE_CACHE = 0x00001000; - - int hr = NativeMethods.CoInternetCreateSecurityManager(null, out curSecMgr, 0); - if (hr != NativeMethods.S_OK) - { - // Returns an error value if it's not S_OK - throw new System.ComponentModel.Win32Exception(hr); - } - - try - { - NativeMethods.IInternetSecurityManager ism = (NativeMethods.IInternetSecurityManager)curSecMgr; - hr = ism.MapUrlToZone(filePath, out zoneId, MUTZ_DONT_USE_CACHE); - if (hr == NativeMethods.S_OK) - { - SecurityZone result; - return LanguagePrimitives.TryConvertTo(zoneId, out result) ? result : SecurityZone.NoZone; - } - return SecurityZone.NoZone; - } - finally - { - if (curSecMgr != null) - { - Marshal.ReleaseComObject(curSecMgr); - } - } - } #endregion Security #endif @@ -448,60 +403,6 @@ private static class NativeMethods internal static extern uint GetACP(); public const int S_OK = 0x00000000; - - /// - /// Pinvoke to create an IInternetSecurityManager interface.. - /// - [DllImport("urlmon.dll", ExactSpelling = true)] - internal static extern int CoInternetCreateSecurityManager([MarshalAs(UnmanagedType.Interface)] object pIServiceProvider, - [MarshalAs(UnmanagedType.Interface)] out object ppISecurityManager, - int dwReserved); - - /// - /// IInternetSecurityManager interface - /// - [ComImport, ComVisible(false), Guid("79EAC9EE-BAF9-11CE-8C82-00AA004BA90B"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] - internal interface IInternetSecurityManager - { - [return: MarshalAs(UnmanagedType.I4)] - [PreserveSig] - int SetSecuritySite([In] IntPtr pSite); - - [return: MarshalAs(UnmanagedType.I4)] - [PreserveSig] - int GetSecuritySite([Out] IntPtr pSite); - - [return: MarshalAs(UnmanagedType.I4)] - [PreserveSig] - int MapUrlToZone([In, MarshalAs(UnmanagedType.LPWStr)] string pwszUrl, out uint pdwZone, uint dwFlags); - - [return: MarshalAs(UnmanagedType.I4)] - [PreserveSig] - int GetSecurityId([MarshalAs(UnmanagedType.LPWStr)] string pwszUrl, - [MarshalAs(UnmanagedType.LPArray)] byte[] pbSecurityId, - ref uint pcbSecurityId, uint dwReserved); - - [return: MarshalAs(UnmanagedType.I4)] - [PreserveSig] - int ProcessUrlAction([In, MarshalAs(UnmanagedType.LPWStr)] string pwszUrl, - uint dwAction, out byte pPolicy, uint cbPolicy, - byte pContext, uint cbContext, uint dwFlags, - uint dwReserved); - - [return: MarshalAs(UnmanagedType.I4)] - [PreserveSig] - int QueryCustomPolicy([In, MarshalAs(UnmanagedType.LPWStr)] string pwszUrl, - ref Guid guidKey, ref byte ppPolicy, ref uint pcbPolicy, - ref byte pContext, uint cbContext, uint dwReserved); - - [return: MarshalAs(UnmanagedType.I4)] - [PreserveSig] - int SetZoneMapping(uint dwZone, [In, MarshalAs(UnmanagedType.LPWStr)] string lpszPattern, uint dwFlags); - - [return: MarshalAs(UnmanagedType.I4)] - [PreserveSig] - int GetZoneMappings(uint dwZone, out IEnumString ppenumString, uint dwFlags); - } } } } From ad2059e99ae501e52f316bdf3ea4c6df5cc4ee9a Mon Sep 17 00:00:00 2001 From: Robert Holt Date: Mon, 18 Jun 2018 17:03:47 -0700 Subject: [PATCH 2/2] Remove remaining unused code --- src/System.Management.Automation/utils/ClrFacade.cs | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/System.Management.Automation/utils/ClrFacade.cs b/src/System.Management.Automation/utils/ClrFacade.cs index d0c4b2538e5..fdb9bc10ab9 100644 --- a/src/System.Management.Automation/utils/ClrFacade.cs +++ b/src/System.Management.Automation/utils/ClrFacade.cs @@ -138,7 +138,6 @@ internal static SecurityZone GetFileSecurityZone(string filePath) { Diagnostics.Assert(Path.IsPathRooted(filePath), "Caller makes sure the path is rooted."); Diagnostics.Assert(Utils.FileExists(filePath), "Caller makes sure the file exists."); - string sysRoot = System.Environment.GetEnvironmentVariable("SystemRoot"); return MapSecurityZone(filePath); } @@ -395,14 +394,6 @@ private static class NativeMethods /// [DllImport(PinvokeDllNames.GetOEMCPDllName, SetLastError = false, CharSet = CharSet.Unicode)] internal static extern uint GetOEMCP(); - - /// - /// Pinvoke for GetACP to get the Windows operating system code page. - /// - [DllImport(PinvokeDllNames.GetACPDllName, SetLastError = false, CharSet = CharSet.Unicode)] - internal static extern uint GetACP(); - - public const int S_OK = 0x00000000; } } }