From cb9d315a549252d3f14d2ab0c34edd5baa6fdba9 Mon Sep 17 00:00:00 2001 From: Berkan Diler Date: Sun, 30 Oct 2022 02:15:07 +0200 Subject: [PATCH 1/2] Make use of new Random.Shared property --- .../cimSupport/cmdletization/cim/cimChildJobBase.cs | 9 +-------- .../commands/management/Computer.cs | 2 +- .../engine/hostifaces/MshHostUserInterface.cs | 2 +- .../engine/runtime/CompiledScriptBlock.cs | 2 +- .../help/UpdatableHelpCommandBase.cs | 4 +--- 5 files changed, 5 insertions(+), 14 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/cim/cimChildJobBase.cs b/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/cim/cimChildJobBase.cs index d787389e9f0..95e842acaad 100644 --- a/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/cim/cimChildJobBase.cs +++ b/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/cim/cimChildJobBase.cs @@ -49,11 +49,6 @@ internal CimChildJobBase(CimJobContext jobContext) this.Name = this.GetType().Name + _myJobNumber.ToString(CultureInfo.InvariantCulture); UsesResultsCollection = true; - lock (s_globalRandom) - { - _random = new Random(s_globalRandom.Next()); - } - _jobSpecificCustomOptions = new Lazy(this.CalculateJobSpecificCustomOptions); } @@ -158,8 +153,6 @@ public virtual void OnCompleted() }); } - private static readonly Random s_globalRandom = new(); - private readonly Random _random; private int _sleepAndRetryDelayRangeMs = 1000; private int _sleepAndRetryExtraDelayMs = 0; @@ -194,7 +187,7 @@ private void SleepAndRetry_OnWakeup(object state) private void SleepAndRetry() { - int tmpRandomDelay = _random.Next(0, _sleepAndRetryDelayRangeMs); + int tmpRandomDelay = Random.Shared.Next(0, _sleepAndRetryDelayRangeMs); int delay = MinRetryDelayMs + _sleepAndRetryExtraDelayMs + tmpRandomDelay; _sleepAndRetryExtraDelayMs = _sleepAndRetryDelayRangeMs - tmpRandomDelay; if (_sleepAndRetryDelayRangeMs < MaxRetryDelayMs) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/Computer.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/Computer.cs index 126596818af..e24c49b63f7 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/Computer.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/Computer.cs @@ -797,7 +797,7 @@ protected override void BeginProcessing() if (Wait) { - _activityId = (new Random()).Next(); + _activityId = Random.Shared.Next(); if (_timeout == -1 || _timeout >= int.MaxValue / 1000) { _timeoutInMilliseconds = int.MaxValue; diff --git a/src/System.Management.Automation/engine/hostifaces/MshHostUserInterface.cs b/src/System.Management.Automation/engine/hostifaces/MshHostUserInterface.cs index 3e4c082e101..ddc6fce782d 100644 --- a/src/System.Management.Automation/engine/hostifaces/MshHostUserInterface.cs +++ b/src/System.Management.Automation/engine/hostifaces/MshHostUserInterface.cs @@ -892,7 +892,7 @@ private void FlushPendingOutput() { // System transcripts can have high contention. Do exponential back-off on writing // if needed. - int delay = new Random().Next(10) + 1; + int delay = Random.Shared.Next(10) + 1; bool written = false; while (!written) diff --git a/src/System.Management.Automation/engine/runtime/CompiledScriptBlock.cs b/src/System.Management.Automation/engine/runtime/CompiledScriptBlock.cs index 3088667f6b2..dc4850b9208 100644 --- a/src/System.Management.Automation/engine/runtime/CompiledScriptBlock.cs +++ b/src/System.Management.Automation/engine/runtime/CompiledScriptBlock.cs @@ -1466,7 +1466,7 @@ internal static void LogScriptBlockCreation(ScriptBlock scriptBlock, bool force) // But split the segments into random sizes (10k + between 0 and 10kb extra) // so that attackers can't creatively force their scripts to span well-known // segments (making simple rules less reliable). - int segmentSize = 10000 + (new Random()).Next(10000); + int segmentSize = 10000 + Random.Shared.Next(10000); int segments = (int)Math.Floor((double)(scriptBlockText.Length / segmentSize)) + 1; int currentLocation = 0; int currentSegmentSize = 0; diff --git a/src/System.Management.Automation/help/UpdatableHelpCommandBase.cs b/src/System.Management.Automation/help/UpdatableHelpCommandBase.cs index 48e44bd3013..21d0609a353 100644 --- a/src/System.Management.Automation/help/UpdatableHelpCommandBase.cs +++ b/src/System.Management.Automation/help/UpdatableHelpCommandBase.cs @@ -210,9 +210,7 @@ internal UpdatableHelpCommandBase(UpdatableHelpCommandType commandType) _exceptions = new Dictionary(); _helpSystem.OnProgressChanged += HandleProgressChanged; - Random rand = new Random(); - - activityId = rand.Next(); + activityId = Random.Shared.Next(); } #endregion From 13385cdb84681eb528840b3a559ef107ca988573 Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Mon, 12 Jun 2023 16:54:23 -0700 Subject: [PATCH 2/2] Revert the changes to CimChildJobBase to be safe --- .../cimSupport/cmdletization/cim/cimChildJobBase.cs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/cim/cimChildJobBase.cs b/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/cim/cimChildJobBase.cs index 95e842acaad..d787389e9f0 100644 --- a/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/cim/cimChildJobBase.cs +++ b/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/cim/cimChildJobBase.cs @@ -49,6 +49,11 @@ internal CimChildJobBase(CimJobContext jobContext) this.Name = this.GetType().Name + _myJobNumber.ToString(CultureInfo.InvariantCulture); UsesResultsCollection = true; + lock (s_globalRandom) + { + _random = new Random(s_globalRandom.Next()); + } + _jobSpecificCustomOptions = new Lazy(this.CalculateJobSpecificCustomOptions); } @@ -153,6 +158,8 @@ public virtual void OnCompleted() }); } + private static readonly Random s_globalRandom = new(); + private readonly Random _random; private int _sleepAndRetryDelayRangeMs = 1000; private int _sleepAndRetryExtraDelayMs = 0; @@ -187,7 +194,7 @@ private void SleepAndRetry_OnWakeup(object state) private void SleepAndRetry() { - int tmpRandomDelay = Random.Shared.Next(0, _sleepAndRetryDelayRangeMs); + int tmpRandomDelay = _random.Next(0, _sleepAndRetryDelayRangeMs); int delay = MinRetryDelayMs + _sleepAndRetryExtraDelayMs + tmpRandomDelay; _sleepAndRetryExtraDelayMs = _sleepAndRetryDelayRangeMs - tmpRandomDelay; if (_sleepAndRetryDelayRangeMs < MaxRetryDelayMs)