From 7becb926326544c78904221aeac3c3c5e10ddd02 Mon Sep 17 00:00:00 2001 From: Fs00 Date: Sat, 5 Jun 2021 11:32:51 +0200 Subject: [PATCH 1/5] Expose assembly informational version in PSVersionInfo --- .../engine/PSVersionInfo.cs | 10 ++++--- .../security/SecuritySupport.cs | 27 +++---------------- test/powershell/Host/Startup.Tests.ps1 | 1 - 3 files changed, 10 insertions(+), 28 deletions(-) diff --git a/src/System.Management.Automation/engine/PSVersionInfo.cs b/src/System.Management.Automation/engine/PSVersionInfo.cs index 5830cdc7f02..5dc19e33606 100644 --- a/src/System.Management.Automation/engine/PSVersionInfo.cs +++ b/src/System.Management.Automation/engine/PSVersionInfo.cs @@ -78,7 +78,7 @@ static PSVersionInfo() s_psVersionTable = new PSVersionHashTable(StringComparer.OrdinalIgnoreCase); Assembly currentAssembly = typeof(PSVersionInfo).Assembly; - string productVersion = currentAssembly.GetCustomAttribute().InformationalVersion; + ProductVersion = currentAssembly.GetCustomAttribute().InformationalVersion; // Get 'GitCommitId' and 'PSVersion' from the 'productVersion' assembly attribute. // @@ -93,11 +93,11 @@ static PSVersionInfo() // productVersion = '6.0.0 SHA: f1ec9...' convert to GitCommitId = 'v6.0.0' // PSVersion = '6.0.0' string rawGitCommitId; - string mainVersion = productVersion.Substring(0, productVersion.IndexOf(' ')); + string mainVersion = ProductVersion.Substring(0, ProductVersion.IndexOf(' ')); - if (productVersion.Contains(" Commits: ")) + if (ProductVersion.Contains(" Commits: ")) { - rawGitCommitId = productVersion.Replace(" Commits: ", "-").Replace(" SHA: ", "-g"); + rawGitCommitId = ProductVersion.Replace(" Commits: ", "-").Replace(" SHA: ", "-g"); } else { @@ -182,6 +182,8 @@ public static Version PSVersion } } + internal static string ProductVersion { get; } + internal static string GitCommitId { get diff --git a/src/System.Management.Automation/security/SecuritySupport.cs b/src/System.Management.Automation/security/SecuritySupport.cs index 67fc12e2ee8..8a8b63846d4 100644 --- a/src/System.Management.Automation/security/SecuritySupport.cs +++ b/src/System.Management.Automation/security/SecuritySupport.cs @@ -856,6 +856,7 @@ internal enum CertificatePurpose namespace System.Management.Automation { + using System.Reflection; using System.Security.Cryptography.Pkcs; /// @@ -1336,35 +1337,15 @@ public enum ResolutionPurpose internal static class AmsiUtils { - private static string GetProcessHostName(string processName) - { - return string.Concat("PowerShell_", processName, ".exe_0.0.0.0"); - } - internal static int Init() { Diagnostics.Assert(s_amsiContext == IntPtr.Zero, "Init should be called just once"); lock (s_amsiLockObject) { - Process currentProcess = Process.GetCurrentProcess(); - string hostname; - try - { - hostname = string.Concat("PowerShell_", Environment.ProcessPath, "_", - currentProcess.MainModule.FileVersionInfo.ProductVersion); - } - catch (ComponentModel.Win32Exception) - { - // This exception can be thrown during thread impersonation (Access Denied for process module access). - hostname = GetProcessHostName(currentProcess.ProcessName); - } - catch (FileNotFoundException) - { - // This exception can occur if the file is renamed or moved to some other folder - // (This has occurred during Exchange set up). - hostname = GetProcessHostName(currentProcess.ProcessName); - } + Assembly currentAssembly = typeof(AmsiUtils).Assembly; + string productVersion = currentAssembly.GetCustomAttribute().InformationalVersion; + string hostname = string.Concat("PowerShell_", Environment.ProcessPath, "_", productVersion); AppDomain.CurrentDomain.ProcessExit += CurrentDomain_ProcessExit; diff --git a/test/powershell/Host/Startup.Tests.ps1 b/test/powershell/Host/Startup.Tests.ps1 index 2f0f4db9b6e..34a298beff9 100644 --- a/test/powershell/Host/Startup.Tests.ps1 +++ b/test/powershell/Host/Startup.Tests.ps1 @@ -67,7 +67,6 @@ Describe "Validate start of console host" -Tag CI { if ($IsWindows) { $allowedAssemblies += @( 'Microsoft.PowerShell.CoreCLR.Eventing.dll' - 'System.Diagnostics.FileVersionInfo.dll' 'System.DirectoryServices.dll' 'System.Management.dll' 'System.Security.Claims.dll' From 6ee1c741cc5bd50e4bc09f1f4f190d0adb7d177d Mon Sep 17 00:00:00 2001 From: Fs00 Date: Sun, 6 Jun 2021 12:55:24 +0200 Subject: [PATCH 2/5] Use PSVersionInfo.ProductVersion in AmsiUtils.Init --- src/System.Management.Automation/security/SecuritySupport.cs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/System.Management.Automation/security/SecuritySupport.cs b/src/System.Management.Automation/security/SecuritySupport.cs index 8a8b63846d4..fed6081ca0b 100644 --- a/src/System.Management.Automation/security/SecuritySupport.cs +++ b/src/System.Management.Automation/security/SecuritySupport.cs @@ -856,7 +856,6 @@ internal enum CertificatePurpose namespace System.Management.Automation { - using System.Reflection; using System.Security.Cryptography.Pkcs; /// @@ -1343,9 +1342,7 @@ internal static int Init() lock (s_amsiLockObject) { - Assembly currentAssembly = typeof(AmsiUtils).Assembly; - string productVersion = currentAssembly.GetCustomAttribute().InformationalVersion; - string hostname = string.Concat("PowerShell_", Environment.ProcessPath, "_", productVersion); + string hostname = string.Concat("PowerShell_", Environment.ProcessPath, "_", PSVersionInfo.ProductVersion); AppDomain.CurrentDomain.ProcessExit += CurrentDomain_ProcessExit; From 121f7441aa9b53d254f4b41710df3021450c0407 Mon Sep 17 00:00:00 2001 From: Francesco Saltori Date: Thu, 10 Jun 2021 08:47:22 +0200 Subject: [PATCH 3/5] Rename hostname local variable --- src/System.Management.Automation/security/SecuritySupport.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/System.Management.Automation/security/SecuritySupport.cs b/src/System.Management.Automation/security/SecuritySupport.cs index fed6081ca0b..a6a358c7fdc 100644 --- a/src/System.Management.Automation/security/SecuritySupport.cs +++ b/src/System.Management.Automation/security/SecuritySupport.cs @@ -1342,11 +1342,11 @@ internal static int Init() lock (s_amsiLockObject) { - string hostname = string.Concat("PowerShell_", Environment.ProcessPath, "_", PSVersionInfo.ProductVersion); + string appName = string.Concat("PowerShell_", Environment.ProcessPath, "_", PSVersionInfo.ProductVersion); AppDomain.CurrentDomain.ProcessExit += CurrentDomain_ProcessExit; - var hr = AmsiNativeMethods.AmsiInitialize(hostname, ref s_amsiContext); + var hr = AmsiNativeMethods.AmsiInitialize(appName, ref s_amsiContext); if (!Utils.Succeeded(hr)) { s_amsiInitFailed = true; From 257602028d38f95a5b1aac32b704bc868027759f Mon Sep 17 00:00:00 2001 From: Fs00 Date: Fri, 11 Jun 2021 08:46:31 +0200 Subject: [PATCH 4/5] Handle exceptions coming from Environment.ProcessPath --- .../security/SecuritySupport.cs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/System.Management.Automation/security/SecuritySupport.cs b/src/System.Management.Automation/security/SecuritySupport.cs index a6a358c7fdc..53616f877ae 100644 --- a/src/System.Management.Automation/security/SecuritySupport.cs +++ b/src/System.Management.Automation/security/SecuritySupport.cs @@ -1342,7 +1342,16 @@ internal static int Init() lock (s_amsiLockObject) { - string appName = string.Concat("PowerShell_", Environment.ProcessPath, "_", PSVersionInfo.ProductVersion); + string appName; + try + { + appName = string.Concat("PowerShell_", Environment.ProcessPath, "_", PSVersionInfo.ProductVersion); + } + catch (Exception) + { + Process currentProcess = Process.GetCurrentProcess(); + appName = string.Concat("PowerShell_", currentProcess.ProcessName, ".exe_", PSVersionInfo.ProductVersion); + } AppDomain.CurrentDomain.ProcessExit += CurrentDomain_ProcessExit; From 38a0e4f7e8dab4854bf5d0ea01879b4f53295648 Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Mon, 14 Jun 2021 11:32:23 -0700 Subject: [PATCH 5/5] Add a comment --- src/System.Management.Automation/security/SecuritySupport.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/System.Management.Automation/security/SecuritySupport.cs b/src/System.Management.Automation/security/SecuritySupport.cs index 53616f877ae..fb99496181c 100644 --- a/src/System.Management.Automation/security/SecuritySupport.cs +++ b/src/System.Management.Automation/security/SecuritySupport.cs @@ -1349,6 +1349,7 @@ internal static int Init() } catch (Exception) { + // Fall back to 'Process.ProcessName' in case 'Environment.ProcessPath' throws exception. Process currentProcess = Process.GetCurrentProcess(); appName = string.Concat("PowerShell_", currentProcess.ProcessName, ".exe_", PSVersionInfo.ProductVersion); }