NetRandomInitSeed() function use current time to initialize the random seed,
while in some platform the time service is not accuracy that make the random
seed collision. This patch add the monotonic count to the seed to avoid this.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Fu Siyuan <[email protected]>
---
 MdeModulePkg/Include/Library/NetLib.h      | 10 +++++-----
 MdeModulePkg/Library/DxeNetLib/DxeNetLib.c | 12 ++++++++----
 2 files changed, 13 insertions(+), 9 deletions(-)

diff --git a/MdeModulePkg/Include/Library/NetLib.h 
b/MdeModulePkg/Include/Library/NetLib.h
index 7ad8dac..280c51a 100644
--- a/MdeModulePkg/Include/Library/NetLib.h
+++ b/MdeModulePkg/Include/Library/NetLib.h
@@ -528,17 +528,17 @@ NetPutUint32 (
   IN OUT UINT8                 *Buf,
   IN     UINT32                Data
   );
 
 /**
-  Initialize a random seed using current time.
+  Initialize a random seed using current time and monotonic count.
 
-  Get current time first. Then initialize a random seed based on some basic
-  mathematical operations on the hour, day, minute, second, nanosecond and year
-  of the current time.
+  Get current time and monotonic count first. Then initialize a random seed 
+  based on some basic mathematics operation on the hour, day, minute, second,
+  nanosecond and year of the current time and the monotonic count value.
 
-  @return The random seed, initialized with current time.
+  @return The random seed initialized with current time.
 
 **/
 UINT32
 EFIAPI
 NetRandomInitSeed (
diff --git a/MdeModulePkg/Library/DxeNetLib/DxeNetLib.c 
b/MdeModulePkg/Library/DxeNetLib/DxeNetLib.c
index ce26b32..57e8f9f 100644
--- a/MdeModulePkg/Library/DxeNetLib/DxeNetLib.c
+++ b/MdeModulePkg/Library/DxeNetLib/DxeNetLib.c
@@ -851,15 +851,15 @@ Ip6Swap128 (
 
   return Ip6;
 }
 
 /**
-  Initialize a random seed using current time.
+  Initialize a random seed using current time and monotonic count.
 
-  Get current time first. Then initialize a random seed based on some basic
-  mathematics operation on the hour, day, minute, second, nanosecond and year
-  of the current time.
+  Get current time and monotonic count first. Then initialize a random seed 
+  based on some basic mathematics operation on the hour, day, minute, second,
+  nanosecond and year of the current time and the monotonic count value.
 
   @return The random seed initialized with current time.
 
 **/
 UINT32
@@ -868,16 +868,20 @@ NetRandomInitSeed (
   VOID
   )
 {
   EFI_TIME                  Time;
   UINT32                    Seed;
+  UINT64                    MonotonicCount;
 
   gRT->GetTime (&Time, NULL);
   Seed = (~Time.Hour << 24 | Time.Day << 16 | Time.Minute << 8 | Time.Second);
   Seed ^= Time.Nanosecond;
   Seed ^= Time.Year << 7;
 
+  gBS->GetNextMonotonicCount (&MonotonicCount);
+  Seed += (UINT32) MonotonicCount;
+
   return Seed;
 }
 
 
 /**
-- 
1.9.5.msysgit.1

_______________________________________________
edk2-devel mailing list
[email protected]
https://lists.01.org/mailman/listinfo/edk2-devel

Reply via email to