public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
* [PATCH EDK2 v1 0/1] CryptoPkg/BaseCryptLib:time overflow
@ 2022-11-25  9:11 wenyi,xie
  2022-11-25  9:11 ` [PATCH EDK2 v1 1/1] " wenyi,xie
  0 siblings, 1 reply; 6+ messages in thread
From: wenyi,xie @ 2022-11-25  9:11 UTC (permalink / raw)
  To: devel, jiewen.yao, jian.j.wang, xiaoyu1.lu, guomin.jiang
  Cc: songdongkuang, xiewenyi2, yizihong

Main Changes :
1.Change type of time_t to INT64.
2.Using function DivS64x64Remainder to deal with INT64 divide.

Zihong Yi (1):
  CryptoPkg/BaseCryptLib:time overflow

 CryptoPkg/Library/Include/CrtLibSupport.h             |  2 +-
 CryptoPkg/Library/BaseCryptLib/SysCall/TimerWrapper.c | 51 ++++++++++++++------
 2 files changed, 38 insertions(+), 15 deletions(-)

-- 
2.20.1.windows.1


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH EDK2 v1 1/1] CryptoPkg/BaseCryptLib:time overflow
  2022-11-25  9:11 [PATCH EDK2 v1 0/1] CryptoPkg/BaseCryptLib:time overflow wenyi,xie
@ 2022-11-25  9:11 ` wenyi,xie
  2022-12-01  8:34   ` [edk2-devel] " Yao, Jiewen
  0 siblings, 1 reply; 6+ messages in thread
From: wenyi,xie @ 2022-11-25  9:11 UTC (permalink / raw)
  To: devel, jiewen.yao, jian.j.wang, xiaoyu1.lu, guomin.jiang
  Cc: songdongkuang, xiewenyi2, yizihong

From: Zihong Yi <yizihong@huawei.com>

REF: https://bugzilla.tianocore.org/show_bug.cgi?id=4167

In CrtLibSupport.h, time_t is defined as INT32, and its maximum value
is 2147483647. That is, the corresponding maximum timestamp is
2038-01-19 11:14:07. Therefore, overflow occurs when the test time
exceeds 2038-01-19 11:14:07. So change the type of time_t to INT64 and
also change the type of variables in function gmtime which calculated
with time_t.

Cc: Jiewen Yao <jiewen.yao@intel.com>
Cc: Jian J Wang <jian.j.wang@intel.com>
Cc: Xiaoyu Lu <xiaoyu1.lu@intel.com>
Cc: Guomin Jiang <guomin.jiang@intel.com>
Signed-off-by: Zihong Yi <yizihong@huawei.com>
---
 CryptoPkg/Library/Include/CrtLibSupport.h             |  2 +-
 CryptoPkg/Library/BaseCryptLib/SysCall/TimerWrapper.c | 51 ++++++++++++++------
 2 files changed, 38 insertions(+), 15 deletions(-)

diff --git a/CryptoPkg/Library/Include/CrtLibSupport.h b/CryptoPkg/Library/Include/CrtLibSupport.h
index 5072c343da57..94b0e6b6014f 100644
--- a/CryptoPkg/Library/Include/CrtLibSupport.h
+++ b/CryptoPkg/Library/Include/CrtLibSupport.h
@@ -109,7 +109,7 @@ typedef UINTN   off_t;
 typedef UINTN   u_int;
 typedef INTN    ptrdiff_t;
 typedef INTN    ssize_t;
-typedef INT32   time_t;
+typedef INT64   time_t;
 typedef UINT8   __uint8_t;
 typedef UINT8   sa_family_t;
 typedef UINT8   u_char;
diff --git a/CryptoPkg/Library/BaseCryptLib/SysCall/TimerWrapper.c b/CryptoPkg/Library/BaseCryptLib/SysCall/TimerWrapper.c
index bf8a5325817f..3ed64375d0ff 100644
--- a/CryptoPkg/Library/BaseCryptLib/SysCall/TimerWrapper.c
+++ b/CryptoPkg/Library/BaseCryptLib/SysCall/TimerWrapper.c
@@ -15,7 +15,6 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
 // -- Time Management Routines --
 //
 
-#define IsLeap(y)  (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
 #define SECSPERMIN   (60)
 #define SECSPERHOUR  (60 * 60)
 #define SECSPERDAY   (24 * SECSPERHOUR)
@@ -60,6 +59,26 @@ UINTN  CumulativeDays[2][14] = {
   }
 };
 
+/* Check the year is leap or not. */
+// BOOLEAN IsLeap(
+//  INTN timer
+//  )
+BOOLEAN
+IsLeap (
+  time_t  timer
+  )
+{
+  INT64  Remainder1;
+  INT64  Remainder2;
+  INT64  Remainder3;
+
+  DivS64x64Remainder (timer, 4, &Remainder1);
+  DivS64x64Remainder (timer, 100, &Remainder2);
+  DivS64x64Remainder (timer, 400, &Remainder3);
+
+  return (Remainder1 == 0 && (Remainder2 != 0 || Remainder3 == 0));
+}
+
 /* Get the system time as seconds elapsed since midnight, January 1, 1970. */
 // INTN time(
 //  INTN *timer
@@ -117,12 +136,13 @@ gmtime (
   )
 {
   struct tm  *GmTime;
-  UINT16     DayNo;
-  UINT32     DayRemainder;
+  UINT64     DayNo;
+  UINT64     DayRemainder;
   time_t     Year;
   time_t     YearNo;
-  UINT16     TotalDays;
-  UINT16     MonthNo;
+  UINT32     TotalDays;
+  UINT32     MonthNo;
+  INT64      Remainder;
 
   if (timer == NULL) {
     return NULL;
@@ -135,18 +155,21 @@ gmtime (
 
   ZeroMem ((VOID *)GmTime, (UINTN)sizeof (struct tm));
 
-  DayNo        = (UINT16)(*timer / SECSPERDAY);
-  DayRemainder = (UINT32)(*timer % SECSPERDAY);
+  DayNo        = (UINT64)DivS64x64Remainder (*timer, SECSPERDAY, &Remainder);
+  DayRemainder = (UINT64)Remainder;
 
-  GmTime->tm_sec  = (int)(DayRemainder % SECSPERMIN);
-  GmTime->tm_min  = (int)((DayRemainder % SECSPERHOUR) / SECSPERMIN);
-  GmTime->tm_hour = (int)(DayRemainder / SECSPERHOUR);
-  GmTime->tm_wday = (int)((DayNo + 4) % 7);
+  DivS64x64Remainder (DayRemainder, SECSPERMIN, &Remainder);
+  GmTime->tm_sec  = (int)Remainder;
+  DivS64x64Remainder (DayRemainder, SECSPERHOUR, &Remainder);
+  GmTime->tm_min  = (int)DivS64x64Remainder (Remainder, SECSPERMIN, NULL);
+  GmTime->tm_hour = (int)DivS64x64Remainder (DayRemainder, SECSPERHOUR, NULL);
+  DivS64x64Remainder ((DayNo + 4), 7, &Remainder);
+  GmTime->tm_wday = (int)Remainder;
 
   for (Year = 1970, YearNo = 0; DayNo > 0; Year++) {
-    TotalDays = (UINT16)(IsLeap (Year) ? 366 : 365);
+    TotalDays = (UINT32)(IsLeap (Year) ? 366 : 365);
     if (DayNo >= TotalDays) {
-      DayNo = (UINT16)(DayNo - TotalDays);
+      DayNo = (UINT64)(DayNo - TotalDays);
       YearNo++;
     } else {
       break;
@@ -158,7 +181,7 @@ gmtime (
 
   for (MonthNo = 12; MonthNo > 1; MonthNo--) {
     if (DayNo >= CumulativeDays[IsLeap (Year)][MonthNo]) {
-      DayNo = (UINT16)(DayNo - (UINT16)(CumulativeDays[IsLeap (Year)][MonthNo]));
+      DayNo = (UINT64)(DayNo - (UINT32)(CumulativeDays[IsLeap (Year)][MonthNo]));
       break;
     }
   }
-- 
2.20.1.windows.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [edk2-devel] [PATCH EDK2 v1 1/1] CryptoPkg/BaseCryptLib:time overflow
  2022-11-25  9:11 ` [PATCH EDK2 v1 1/1] " wenyi,xie
@ 2022-12-01  8:34   ` Yao, Jiewen
  2022-12-01 12:34     ` wenyi,xie
  0 siblings, 1 reply; 6+ messages in thread
From: Yao, Jiewen @ 2022-12-01  8:34 UTC (permalink / raw)
  To: devel@edk2.groups.io, xiewenyi2@huawei.com, Wang, Jian J,
	Lu, Xiaoyu1, Jiang, Guomin
  Cc: songdongkuang@huawei.com, yizihong@huawei.com

Thank you!

Would you please share what test you have run for this patch, such as UEFI secure boot? HTTPS boot?



> -----Original Message-----
> From: devel@edk2.groups.io <devel@edk2.groups.io> On Behalf Of
> wenyi,xie via groups.io
> Sent: Friday, November 25, 2022 5:12 PM
> To: devel@edk2.groups.io; Yao, Jiewen <jiewen.yao@intel.com>; Wang, Jian
> J <jian.j.wang@intel.com>; Lu, Xiaoyu1 <xiaoyu1.lu@intel.com>; Jiang,
> Guomin <guomin.jiang@intel.com>
> Cc: songdongkuang@huawei.com; xiewenyi2@huawei.com;
> yizihong@huawei.com
> Subject: [edk2-devel] [PATCH EDK2 v1 1/1] CryptoPkg/BaseCryptLib:time
> overflow
> 
> From: Zihong Yi <yizihong@huawei.com>
> 
> REF: https://bugzilla.tianocore.org/show_bug.cgi?id=4167
> 
> In CrtLibSupport.h, time_t is defined as INT32, and its maximum value
> is 2147483647. That is, the corresponding maximum timestamp is
> 2038-01-19 11:14:07. Therefore, overflow occurs when the test time
> exceeds 2038-01-19 11:14:07. So change the type of time_t to INT64 and
> also change the type of variables in function gmtime which calculated
> with time_t.
> 
> Cc: Jiewen Yao <jiewen.yao@intel.com>
> Cc: Jian J Wang <jian.j.wang@intel.com>
> Cc: Xiaoyu Lu <xiaoyu1.lu@intel.com>
> Cc: Guomin Jiang <guomin.jiang@intel.com>
> Signed-off-by: Zihong Yi <yizihong@huawei.com>
> ---
>  CryptoPkg/Library/Include/CrtLibSupport.h             |  2 +-
>  CryptoPkg/Library/BaseCryptLib/SysCall/TimerWrapper.c | 51
> ++++++++++++++------
>  2 files changed, 38 insertions(+), 15 deletions(-)
> 
> diff --git a/CryptoPkg/Library/Include/CrtLibSupport.h
> b/CryptoPkg/Library/Include/CrtLibSupport.h
> index 5072c343da57..94b0e6b6014f 100644
> --- a/CryptoPkg/Library/Include/CrtLibSupport.h
> +++ b/CryptoPkg/Library/Include/CrtLibSupport.h
> @@ -109,7 +109,7 @@ typedef UINTN   off_t;
>  typedef UINTN   u_int;
>  typedef INTN    ptrdiff_t;
>  typedef INTN    ssize_t;
> -typedef INT32   time_t;
> +typedef INT64   time_t;
>  typedef UINT8   __uint8_t;
>  typedef UINT8   sa_family_t;
>  typedef UINT8   u_char;
> diff --git a/CryptoPkg/Library/BaseCryptLib/SysCall/TimerWrapper.c
> b/CryptoPkg/Library/BaseCryptLib/SysCall/TimerWrapper.c
> index bf8a5325817f..3ed64375d0ff 100644
> --- a/CryptoPkg/Library/BaseCryptLib/SysCall/TimerWrapper.c
> +++ b/CryptoPkg/Library/BaseCryptLib/SysCall/TimerWrapper.c
> @@ -15,7 +15,6 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
>  // -- Time Management Routines --
>  //
> 
> -#define IsLeap(y)  (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
>  #define SECSPERMIN   (60)
>  #define SECSPERHOUR  (60 * 60)
>  #define SECSPERDAY   (24 * SECSPERHOUR)
> @@ -60,6 +59,26 @@ UINTN  CumulativeDays[2][14] = {
>    }
>  };
> 
> +/* Check the year is leap or not. */
> +// BOOLEAN IsLeap(
> +//  INTN timer
> +//  )
> +BOOLEAN
> +IsLeap (
> +  time_t  timer
> +  )
> +{
> +  INT64  Remainder1;
> +  INT64  Remainder2;
> +  INT64  Remainder3;
> +
> +  DivS64x64Remainder (timer, 4, &Remainder1);
> +  DivS64x64Remainder (timer, 100, &Remainder2);
> +  DivS64x64Remainder (timer, 400, &Remainder3);
> +
> +  return (Remainder1 == 0 && (Remainder2 != 0 || Remainder3 == 0));
> +}
> +
>  /* Get the system time as seconds elapsed since midnight, January 1, 1970.
> */
>  // INTN time(
>  //  INTN *timer
> @@ -117,12 +136,13 @@ gmtime (
>    )
>  {
>    struct tm  *GmTime;
> -  UINT16     DayNo;
> -  UINT32     DayRemainder;
> +  UINT64     DayNo;
> +  UINT64     DayRemainder;
>    time_t     Year;
>    time_t     YearNo;
> -  UINT16     TotalDays;
> -  UINT16     MonthNo;
> +  UINT32     TotalDays;
> +  UINT32     MonthNo;
> +  INT64      Remainder;
> 
>    if (timer == NULL) {
>      return NULL;
> @@ -135,18 +155,21 @@ gmtime (
> 
>    ZeroMem ((VOID *)GmTime, (UINTN)sizeof (struct tm));
> 
> -  DayNo        = (UINT16)(*timer / SECSPERDAY);
> -  DayRemainder = (UINT32)(*timer % SECSPERDAY);
> +  DayNo        = (UINT64)DivS64x64Remainder (*timer, SECSPERDAY,
> &Remainder);
> +  DayRemainder = (UINT64)Remainder;
> 
> -  GmTime->tm_sec  = (int)(DayRemainder % SECSPERMIN);
> -  GmTime->tm_min  = (int)((DayRemainder % SECSPERHOUR) /
> SECSPERMIN);
> -  GmTime->tm_hour = (int)(DayRemainder / SECSPERHOUR);
> -  GmTime->tm_wday = (int)((DayNo + 4) % 7);
> +  DivS64x64Remainder (DayRemainder, SECSPERMIN, &Remainder);
> +  GmTime->tm_sec  = (int)Remainder;
> +  DivS64x64Remainder (DayRemainder, SECSPERHOUR, &Remainder);
> +  GmTime->tm_min  = (int)DivS64x64Remainder (Remainder, SECSPERMIN,
> NULL);
> +  GmTime->tm_hour = (int)DivS64x64Remainder (DayRemainder,
> SECSPERHOUR, NULL);
> +  DivS64x64Remainder ((DayNo + 4), 7, &Remainder);
> +  GmTime->tm_wday = (int)Remainder;
> 
>    for (Year = 1970, YearNo = 0; DayNo > 0; Year++) {
> -    TotalDays = (UINT16)(IsLeap (Year) ? 366 : 365);
> +    TotalDays = (UINT32)(IsLeap (Year) ? 366 : 365);
>      if (DayNo >= TotalDays) {
> -      DayNo = (UINT16)(DayNo - TotalDays);
> +      DayNo = (UINT64)(DayNo - TotalDays);
>        YearNo++;
>      } else {
>        break;
> @@ -158,7 +181,7 @@ gmtime (
> 
>    for (MonthNo = 12; MonthNo > 1; MonthNo--) {
>      if (DayNo >= CumulativeDays[IsLeap (Year)][MonthNo]) {
> -      DayNo = (UINT16)(DayNo - (UINT16)(CumulativeDays[IsLeap
> (Year)][MonthNo]));
> +      DayNo = (UINT64)(DayNo - (UINT32)(CumulativeDays[IsLeap
> (Year)][MonthNo]));
>        break;
>      }
>    }
> --
> 2.20.1.windows.1
> 
> 
> 
> 
> 


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [edk2-devel] [PATCH EDK2 v1 1/1] CryptoPkg/BaseCryptLib:time overflow
  2022-12-01  8:34   ` [edk2-devel] " Yao, Jiewen
@ 2022-12-01 12:34     ` wenyi,xie
  2022-12-01 13:36       ` Yao, Jiewen
       [not found]       ` <172CAED90E086CFF.1705@groups.io>
  0 siblings, 2 replies; 6+ messages in thread
From: wenyi,xie @ 2022-12-01 12:34 UTC (permalink / raw)
  To: Yao, Jiewen, devel@edk2.groups.io, Wang, Jian J, Lu, Xiaoyu1,
	Jiang, Guomin
  Cc: songdongkuang@huawei.com, yizihong@huawei.com



On 2022/12/1 16:34, Yao, Jiewen wrote:
> Thank you!
> 
> Would you please share what test you have run for this patch, such as UEFI secure boot? HTTPS boot?
> 
Hi, Jiewen

We found this problem when importing certification and verifying the expiration date(the verifying code is customed), so we only using a X64 board to do the test like below:
1. Entering setup page
2. Setting system time to 2038.9.27 19:23
3. Importing a X509 certification, which is valid from 2022.9.27 1:59 to 2023.9.27 1:59

before merging the patch
the certification can be imported successfully.

after merging the patch
importing failed.
> 
> 
>> -----Original Message-----
>> From: devel@edk2.groups.io <devel@edk2.groups.io> On Behalf Of
>> wenyi,xie via groups.io
>> Sent: Friday, November 25, 2022 5:12 PM
>> To: devel@edk2.groups.io; Yao, Jiewen <jiewen.yao@intel.com>; Wang, Jian
>> J <jian.j.wang@intel.com>; Lu, Xiaoyu1 <xiaoyu1.lu@intel.com>; Jiang,
>> Guomin <guomin.jiang@intel.com>
>> Cc: songdongkuang@huawei.com; xiewenyi2@huawei.com;
>> yizihong@huawei.com
>> Subject: [edk2-devel] [PATCH EDK2 v1 1/1] CryptoPkg/BaseCryptLib:time
>> overflow
>>
>> From: Zihong Yi <yizihong@huawei.com>
>>
>> REF: https://bugzilla.tianocore.org/show_bug.cgi?id=4167
>>
>> In CrtLibSupport.h, time_t is defined as INT32, and its maximum value
>> is 2147483647. That is, the corresponding maximum timestamp is
>> 2038-01-19 11:14:07. Therefore, overflow occurs when the test time
>> exceeds 2038-01-19 11:14:07. So change the type of time_t to INT64 and
>> also change the type of variables in function gmtime which calculated
>> with time_t.
>>
>> Cc: Jiewen Yao <jiewen.yao@intel.com>
>> Cc: Jian J Wang <jian.j.wang@intel.com>
>> Cc: Xiaoyu Lu <xiaoyu1.lu@intel.com>
>> Cc: Guomin Jiang <guomin.jiang@intel.com>
>> Signed-off-by: Zihong Yi <yizihong@huawei.com>
>> ---
>>  CryptoPkg/Library/Include/CrtLibSupport.h             |  2 +-
>>  CryptoPkg/Library/BaseCryptLib/SysCall/TimerWrapper.c | 51
>> ++++++++++++++------
>>  2 files changed, 38 insertions(+), 15 deletions(-)
>>
>> diff --git a/CryptoPkg/Library/Include/CrtLibSupport.h
>> b/CryptoPkg/Library/Include/CrtLibSupport.h
>> index 5072c343da57..94b0e6b6014f 100644
>> --- a/CryptoPkg/Library/Include/CrtLibSupport.h
>> +++ b/CryptoPkg/Library/Include/CrtLibSupport.h
>> @@ -109,7 +109,7 @@ typedef UINTN   off_t;
>>  typedef UINTN   u_int;
>>  typedef INTN    ptrdiff_t;
>>  typedef INTN    ssize_t;
>> -typedef INT32   time_t;
>> +typedef INT64   time_t;
>>  typedef UINT8   __uint8_t;
>>  typedef UINT8   sa_family_t;
>>  typedef UINT8   u_char;
>> diff --git a/CryptoPkg/Library/BaseCryptLib/SysCall/TimerWrapper.c
>> b/CryptoPkg/Library/BaseCryptLib/SysCall/TimerWrapper.c
>> index bf8a5325817f..3ed64375d0ff 100644
>> --- a/CryptoPkg/Library/BaseCryptLib/SysCall/TimerWrapper.c
>> +++ b/CryptoPkg/Library/BaseCryptLib/SysCall/TimerWrapper.c
>> @@ -15,7 +15,6 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
>>  // -- Time Management Routines --
>>  //
>>
>> -#define IsLeap(y)  (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
>>  #define SECSPERMIN   (60)
>>  #define SECSPERHOUR  (60 * 60)
>>  #define SECSPERDAY   (24 * SECSPERHOUR)
>> @@ -60,6 +59,26 @@ UINTN  CumulativeDays[2][14] = {
>>    }
>>  };
>>
>> +/* Check the year is leap or not. */
>> +// BOOLEAN IsLeap(
>> +//  INTN timer
>> +//  )
>> +BOOLEAN
>> +IsLeap (
>> +  time_t  timer
>> +  )
>> +{
>> +  INT64  Remainder1;
>> +  INT64  Remainder2;
>> +  INT64  Remainder3;
>> +
>> +  DivS64x64Remainder (timer, 4, &Remainder1);
>> +  DivS64x64Remainder (timer, 100, &Remainder2);
>> +  DivS64x64Remainder (timer, 400, &Remainder3);
>> +
>> +  return (Remainder1 == 0 && (Remainder2 != 0 || Remainder3 == 0));
>> +}
>> +
>>  /* Get the system time as seconds elapsed since midnight, January 1, 1970.
>> */
>>  // INTN time(
>>  //  INTN *timer
>> @@ -117,12 +136,13 @@ gmtime (
>>    )
>>  {
>>    struct tm  *GmTime;
>> -  UINT16     DayNo;
>> -  UINT32     DayRemainder;
>> +  UINT64     DayNo;
>> +  UINT64     DayRemainder;
>>    time_t     Year;
>>    time_t     YearNo;
>> -  UINT16     TotalDays;
>> -  UINT16     MonthNo;
>> +  UINT32     TotalDays;
>> +  UINT32     MonthNo;
>> +  INT64      Remainder;
>>
>>    if (timer == NULL) {
>>      return NULL;
>> @@ -135,18 +155,21 @@ gmtime (
>>
>>    ZeroMem ((VOID *)GmTime, (UINTN)sizeof (struct tm));
>>
>> -  DayNo        = (UINT16)(*timer / SECSPERDAY);
>> -  DayRemainder = (UINT32)(*timer % SECSPERDAY);
>> +  DayNo        = (UINT64)DivS64x64Remainder (*timer, SECSPERDAY,
>> &Remainder);
>> +  DayRemainder = (UINT64)Remainder;
>>
>> -  GmTime->tm_sec  = (int)(DayRemainder % SECSPERMIN);
>> -  GmTime->tm_min  = (int)((DayRemainder % SECSPERHOUR) /
>> SECSPERMIN);
>> -  GmTime->tm_hour = (int)(DayRemainder / SECSPERHOUR);
>> -  GmTime->tm_wday = (int)((DayNo + 4) % 7);
>> +  DivS64x64Remainder (DayRemainder, SECSPERMIN, &Remainder);
>> +  GmTime->tm_sec  = (int)Remainder;
>> +  DivS64x64Remainder (DayRemainder, SECSPERHOUR, &Remainder);
>> +  GmTime->tm_min  = (int)DivS64x64Remainder (Remainder, SECSPERMIN,
>> NULL);
>> +  GmTime->tm_hour = (int)DivS64x64Remainder (DayRemainder,
>> SECSPERHOUR, NULL);
>> +  DivS64x64Remainder ((DayNo + 4), 7, &Remainder);
>> +  GmTime->tm_wday = (int)Remainder;
>>
>>    for (Year = 1970, YearNo = 0; DayNo > 0; Year++) {
>> -    TotalDays = (UINT16)(IsLeap (Year) ? 366 : 365);
>> +    TotalDays = (UINT32)(IsLeap (Year) ? 366 : 365);
>>      if (DayNo >= TotalDays) {
>> -      DayNo = (UINT16)(DayNo - TotalDays);
>> +      DayNo = (UINT64)(DayNo - TotalDays);
>>        YearNo++;
>>      } else {
>>        break;
>> @@ -158,7 +181,7 @@ gmtime (
>>
>>    for (MonthNo = 12; MonthNo > 1; MonthNo--) {
>>      if (DayNo >= CumulativeDays[IsLeap (Year)][MonthNo]) {
>> -      DayNo = (UINT16)(DayNo - (UINT16)(CumulativeDays[IsLeap
>> (Year)][MonthNo]));
>> +      DayNo = (UINT64)(DayNo - (UINT32)(CumulativeDays[IsLeap
>> (Year)][MonthNo]));
>>        break;
>>      }
>>    }
>> --
>> 2.20.1.windows.1
>>
>>
>>
>> 
>>
> 
> 
> .
> 

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [edk2-devel] [PATCH EDK2 v1 1/1] CryptoPkg/BaseCryptLib:time overflow
  2022-12-01 12:34     ` wenyi,xie
@ 2022-12-01 13:36       ` Yao, Jiewen
       [not found]       ` <172CAED90E086CFF.1705@groups.io>
  1 sibling, 0 replies; 6+ messages in thread
From: Yao, Jiewen @ 2022-12-01 13:36 UTC (permalink / raw)
  To: xiewenyi (A), devel@edk2.groups.io, Wang, Jian J, Lu, Xiaoyu1,
	Jiang, Guomin
  Cc: songdongkuang@huawei.com, yizihong@huawei.com

Thank you!

Reviewed-by: Jiewen Yao <Jiewen.yao@intel.com>

> -----Original Message-----
> From: xiewenyi (A) <xiewenyi2@huawei.com>
> Sent: Thursday, December 1, 2022 8:34 PM
> To: Yao, Jiewen <jiewen.yao@intel.com>; devel@edk2.groups.io; Wang, Jian
> J <jian.j.wang@intel.com>; Lu, Xiaoyu1 <xiaoyu1.lu@intel.com>; Jiang,
> Guomin <guomin.jiang@intel.com>
> Cc: songdongkuang@huawei.com; yizihong@huawei.com
> Subject: Re: [edk2-devel] [PATCH EDK2 v1 1/1]
> CryptoPkg/BaseCryptLib:time overflow
> 
> 
> 
> On 2022/12/1 16:34, Yao, Jiewen wrote:
> > Thank you!
> >
> > Would you please share what test you have run for this patch, such as
> UEFI secure boot? HTTPS boot?
> >
> Hi, Jiewen
> 
> We found this problem when importing certification and verifying the
> expiration date(the verifying code is customed), so we only using a X64
> board to do the test like below:
> 1. Entering setup page
> 2. Setting system time to 2038.9.27 19:23
> 3. Importing a X509 certification, which is valid from 2022.9.27 1:59 to
> 2023.9.27 1:59
> 
> before merging the patch
> the certification can be imported successfully.
> 
> after merging the patch
> importing failed.
> >
> >
> >> -----Original Message-----
> >> From: devel@edk2.groups.io <devel@edk2.groups.io> On Behalf Of
> >> wenyi,xie via groups.io
> >> Sent: Friday, November 25, 2022 5:12 PM
> >> To: devel@edk2.groups.io; Yao, Jiewen <jiewen.yao@intel.com>; Wang,
> Jian
> >> J <jian.j.wang@intel.com>; Lu, Xiaoyu1 <xiaoyu1.lu@intel.com>; Jiang,
> >> Guomin <guomin.jiang@intel.com>
> >> Cc: songdongkuang@huawei.com; xiewenyi2@huawei.com;
> >> yizihong@huawei.com
> >> Subject: [edk2-devel] [PATCH EDK2 v1 1/1] CryptoPkg/BaseCryptLib:time
> >> overflow
> >>
> >> From: Zihong Yi <yizihong@huawei.com>
> >>
> >> REF: https://bugzilla.tianocore.org/show_bug.cgi?id=4167
> >>
> >> In CrtLibSupport.h, time_t is defined as INT32, and its maximum value
> >> is 2147483647. That is, the corresponding maximum timestamp is
> >> 2038-01-19 11:14:07. Therefore, overflow occurs when the test time
> >> exceeds 2038-01-19 11:14:07. So change the type of time_t to INT64
> and
> >> also change the type of variables in function gmtime which calculated
> >> with time_t.
> >>
> >> Cc: Jiewen Yao <jiewen.yao@intel.com>
> >> Cc: Jian J Wang <jian.j.wang@intel.com>
> >> Cc: Xiaoyu Lu <xiaoyu1.lu@intel.com>
> >> Cc: Guomin Jiang <guomin.jiang@intel.com>
> >> Signed-off-by: Zihong Yi <yizihong@huawei.com>
> >> ---
> >>  CryptoPkg/Library/Include/CrtLibSupport.h             |  2 +-
> >>  CryptoPkg/Library/BaseCryptLib/SysCall/TimerWrapper.c | 51
> >> ++++++++++++++------
> >>  2 files changed, 38 insertions(+), 15 deletions(-)
> >>
> >> diff --git a/CryptoPkg/Library/Include/CrtLibSupport.h
> >> b/CryptoPkg/Library/Include/CrtLibSupport.h
> >> index 5072c343da57..94b0e6b6014f 100644
> >> --- a/CryptoPkg/Library/Include/CrtLibSupport.h
> >> +++ b/CryptoPkg/Library/Include/CrtLibSupport.h
> >> @@ -109,7 +109,7 @@ typedef UINTN   off_t;
> >>  typedef UINTN   u_int;
> >>  typedef INTN    ptrdiff_t;
> >>  typedef INTN    ssize_t;
> >> -typedef INT32   time_t;
> >> +typedef INT64   time_t;
> >>  typedef UINT8   __uint8_t;
> >>  typedef UINT8   sa_family_t;
> >>  typedef UINT8   u_char;
> >> diff --git a/CryptoPkg/Library/BaseCryptLib/SysCall/TimerWrapper.c
> >> b/CryptoPkg/Library/BaseCryptLib/SysCall/TimerWrapper.c
> >> index bf8a5325817f..3ed64375d0ff 100644
> >> --- a/CryptoPkg/Library/BaseCryptLib/SysCall/TimerWrapper.c
> >> +++ b/CryptoPkg/Library/BaseCryptLib/SysCall/TimerWrapper.c
> >> @@ -15,7 +15,6 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
> >>  // -- Time Management Routines --
> >>  //
> >>
> >> -#define IsLeap(y)  (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) ==
> 0))
> >>  #define SECSPERMIN   (60)
> >>  #define SECSPERHOUR  (60 * 60)
> >>  #define SECSPERDAY   (24 * SECSPERHOUR)
> >> @@ -60,6 +59,26 @@ UINTN  CumulativeDays[2][14] = {
> >>    }
> >>  };
> >>
> >> +/* Check the year is leap or not. */
> >> +// BOOLEAN IsLeap(
> >> +//  INTN timer
> >> +//  )
> >> +BOOLEAN
> >> +IsLeap (
> >> +  time_t  timer
> >> +  )
> >> +{
> >> +  INT64  Remainder1;
> >> +  INT64  Remainder2;
> >> +  INT64  Remainder3;
> >> +
> >> +  DivS64x64Remainder (timer, 4, &Remainder1);
> >> +  DivS64x64Remainder (timer, 100, &Remainder2);
> >> +  DivS64x64Remainder (timer, 400, &Remainder3);
> >> +
> >> +  return (Remainder1 == 0 && (Remainder2 != 0 || Remainder3 == 0));
> >> +}
> >> +
> >>  /* Get the system time as seconds elapsed since midnight, January 1,
> 1970.
> >> */
> >>  // INTN time(
> >>  //  INTN *timer
> >> @@ -117,12 +136,13 @@ gmtime (
> >>    )
> >>  {
> >>    struct tm  *GmTime;
> >> -  UINT16     DayNo;
> >> -  UINT32     DayRemainder;
> >> +  UINT64     DayNo;
> >> +  UINT64     DayRemainder;
> >>    time_t     Year;
> >>    time_t     YearNo;
> >> -  UINT16     TotalDays;
> >> -  UINT16     MonthNo;
> >> +  UINT32     TotalDays;
> >> +  UINT32     MonthNo;
> >> +  INT64      Remainder;
> >>
> >>    if (timer == NULL) {
> >>      return NULL;
> >> @@ -135,18 +155,21 @@ gmtime (
> >>
> >>    ZeroMem ((VOID *)GmTime, (UINTN)sizeof (struct tm));
> >>
> >> -  DayNo        = (UINT16)(*timer / SECSPERDAY);
> >> -  DayRemainder = (UINT32)(*timer % SECSPERDAY);
> >> +  DayNo        = (UINT64)DivS64x64Remainder (*timer, SECSPERDAY,
> >> &Remainder);
> >> +  DayRemainder = (UINT64)Remainder;
> >>
> >> -  GmTime->tm_sec  = (int)(DayRemainder % SECSPERMIN);
> >> -  GmTime->tm_min  = (int)((DayRemainder % SECSPERHOUR) /
> >> SECSPERMIN);
> >> -  GmTime->tm_hour = (int)(DayRemainder / SECSPERHOUR);
> >> -  GmTime->tm_wday = (int)((DayNo + 4) % 7);
> >> +  DivS64x64Remainder (DayRemainder, SECSPERMIN, &Remainder);
> >> +  GmTime->tm_sec  = (int)Remainder;
> >> +  DivS64x64Remainder (DayRemainder, SECSPERHOUR, &Remainder);
> >> +  GmTime->tm_min  = (int)DivS64x64Remainder (Remainder,
> SECSPERMIN,
> >> NULL);
> >> +  GmTime->tm_hour = (int)DivS64x64Remainder (DayRemainder,
> >> SECSPERHOUR, NULL);
> >> +  DivS64x64Remainder ((DayNo + 4), 7, &Remainder);
> >> +  GmTime->tm_wday = (int)Remainder;
> >>
> >>    for (Year = 1970, YearNo = 0; DayNo > 0; Year++) {
> >> -    TotalDays = (UINT16)(IsLeap (Year) ? 366 : 365);
> >> +    TotalDays = (UINT32)(IsLeap (Year) ? 366 : 365);
> >>      if (DayNo >= TotalDays) {
> >> -      DayNo = (UINT16)(DayNo - TotalDays);
> >> +      DayNo = (UINT64)(DayNo - TotalDays);
> >>        YearNo++;
> >>      } else {
> >>        break;
> >> @@ -158,7 +181,7 @@ gmtime (
> >>
> >>    for (MonthNo = 12; MonthNo > 1; MonthNo--) {
> >>      if (DayNo >= CumulativeDays[IsLeap (Year)][MonthNo]) {
> >> -      DayNo = (UINT16)(DayNo - (UINT16)(CumulativeDays[IsLeap
> >> (Year)][MonthNo]));
> >> +      DayNo = (UINT64)(DayNo - (UINT32)(CumulativeDays[IsLeap
> >> (Year)][MonthNo]));
> >>        break;
> >>      }
> >>    }
> >> --
> >> 2.20.1.windows.1
> >>
> >>
> >>
> >> 
> >>
> >
> >
> > .
> >

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [edk2-devel] [PATCH EDK2 v1 1/1] CryptoPkg/BaseCryptLib:time overflow
       [not found]       ` <172CAED90E086CFF.1705@groups.io>
@ 2022-12-01 14:23         ` Yao, Jiewen
  0 siblings, 0 replies; 6+ messages in thread
From: Yao, Jiewen @ 2022-12-01 14:23 UTC (permalink / raw)
  To: devel@edk2.groups.io, Yao, Jiewen, xiewenyi (A), Wang, Jian J,
	Lu, Xiaoyu1, Jiang, Guomin
  Cc: songdongkuang@huawei.com, yizihong@huawei.com

Hey
The patch caused CI failure - https://github.com/tianocore/edk2/pull/3691

Please always run Tiano CI before submit the patch.


> -----Original Message-----
> From: devel@edk2.groups.io <devel@edk2.groups.io> On Behalf Of Yao,
> Jiewen
> Sent: Thursday, December 1, 2022 9:37 PM
> To: xiewenyi (A) <xiewenyi2@huawei.com>; devel@edk2.groups.io; Wang,
> Jian J <jian.j.wang@intel.com>; Lu, Xiaoyu1 <xiaoyu1.lu@intel.com>; Jiang,
> Guomin <guomin.jiang@intel.com>
> Cc: songdongkuang@huawei.com; yizihong@huawei.com
> Subject: Re: [edk2-devel] [PATCH EDK2 v1 1/1]
> CryptoPkg/BaseCryptLib:time overflow
> 
> Thank you!
> 
> Reviewed-by: Jiewen Yao <Jiewen.yao@intel.com>
> 
> > -----Original Message-----
> > From: xiewenyi (A) <xiewenyi2@huawei.com>
> > Sent: Thursday, December 1, 2022 8:34 PM
> > To: Yao, Jiewen <jiewen.yao@intel.com>; devel@edk2.groups.io; Wang,
> Jian
> > J <jian.j.wang@intel.com>; Lu, Xiaoyu1 <xiaoyu1.lu@intel.com>; Jiang,
> > Guomin <guomin.jiang@intel.com>
> > Cc: songdongkuang@huawei.com; yizihong@huawei.com
> > Subject: Re: [edk2-devel] [PATCH EDK2 v1 1/1]
> > CryptoPkg/BaseCryptLib:time overflow
> >
> >
> >
> > On 2022/12/1 16:34, Yao, Jiewen wrote:
> > > Thank you!
> > >
> > > Would you please share what test you have run for this patch, such as
> > UEFI secure boot? HTTPS boot?
> > >
> > Hi, Jiewen
> >
> > We found this problem when importing certification and verifying the
> > expiration date(the verifying code is customed), so we only using a X64
> > board to do the test like below:
> > 1. Entering setup page
> > 2. Setting system time to 2038.9.27 19:23
> > 3. Importing a X509 certification, which is valid from 2022.9.27 1:59 to
> > 2023.9.27 1:59
> >
> > before merging the patch
> > the certification can be imported successfully.
> >
> > after merging the patch
> > importing failed.
> > >
> > >
> > >> -----Original Message-----
> > >> From: devel@edk2.groups.io <devel@edk2.groups.io> On Behalf Of
> > >> wenyi,xie via groups.io
> > >> Sent: Friday, November 25, 2022 5:12 PM
> > >> To: devel@edk2.groups.io; Yao, Jiewen <jiewen.yao@intel.com>; Wang,
> > Jian
> > >> J <jian.j.wang@intel.com>; Lu, Xiaoyu1 <xiaoyu1.lu@intel.com>; Jiang,
> > >> Guomin <guomin.jiang@intel.com>
> > >> Cc: songdongkuang@huawei.com; xiewenyi2@huawei.com;
> > >> yizihong@huawei.com
> > >> Subject: [edk2-devel] [PATCH EDK2 v1 1/1]
> CryptoPkg/BaseCryptLib:time
> > >> overflow
> > >>
> > >> From: Zihong Yi <yizihong@huawei.com>
> > >>
> > >> REF: https://bugzilla.tianocore.org/show_bug.cgi?id=4167
> > >>
> > >> In CrtLibSupport.h, time_t is defined as INT32, and its maximum value
> > >> is 2147483647. That is, the corresponding maximum timestamp is
> > >> 2038-01-19 11:14:07. Therefore, overflow occurs when the test time
> > >> exceeds 2038-01-19 11:14:07. So change the type of time_t to INT64
> > and
> > >> also change the type of variables in function gmtime which calculated
> > >> with time_t.
> > >>
> > >> Cc: Jiewen Yao <jiewen.yao@intel.com>
> > >> Cc: Jian J Wang <jian.j.wang@intel.com>
> > >> Cc: Xiaoyu Lu <xiaoyu1.lu@intel.com>
> > >> Cc: Guomin Jiang <guomin.jiang@intel.com>
> > >> Signed-off-by: Zihong Yi <yizihong@huawei.com>
> > >> ---
> > >>  CryptoPkg/Library/Include/CrtLibSupport.h             |  2 +-
> > >>  CryptoPkg/Library/BaseCryptLib/SysCall/TimerWrapper.c | 51
> > >> ++++++++++++++------
> > >>  2 files changed, 38 insertions(+), 15 deletions(-)
> > >>
> > >> diff --git a/CryptoPkg/Library/Include/CrtLibSupport.h
> > >> b/CryptoPkg/Library/Include/CrtLibSupport.h
> > >> index 5072c343da57..94b0e6b6014f 100644
> > >> --- a/CryptoPkg/Library/Include/CrtLibSupport.h
> > >> +++ b/CryptoPkg/Library/Include/CrtLibSupport.h
> > >> @@ -109,7 +109,7 @@ typedef UINTN   off_t;
> > >>  typedef UINTN   u_int;
> > >>  typedef INTN    ptrdiff_t;
> > >>  typedef INTN    ssize_t;
> > >> -typedef INT32   time_t;
> > >> +typedef INT64   time_t;
> > >>  typedef UINT8   __uint8_t;
> > >>  typedef UINT8   sa_family_t;
> > >>  typedef UINT8   u_char;
> > >> diff --git a/CryptoPkg/Library/BaseCryptLib/SysCall/TimerWrapper.c
> > >> b/CryptoPkg/Library/BaseCryptLib/SysCall/TimerWrapper.c
> > >> index bf8a5325817f..3ed64375d0ff 100644
> > >> --- a/CryptoPkg/Library/BaseCryptLib/SysCall/TimerWrapper.c
> > >> +++ b/CryptoPkg/Library/BaseCryptLib/SysCall/TimerWrapper.c
> > >> @@ -15,7 +15,6 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
> > >>  // -- Time Management Routines --
> > >>  //
> > >>
> > >> -#define IsLeap(y)  (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) ==
> > 0))
> > >>  #define SECSPERMIN   (60)
> > >>  #define SECSPERHOUR  (60 * 60)
> > >>  #define SECSPERDAY   (24 * SECSPERHOUR)
> > >> @@ -60,6 +59,26 @@ UINTN  CumulativeDays[2][14] = {
> > >>    }
> > >>  };
> > >>
> > >> +/* Check the year is leap or not. */
> > >> +// BOOLEAN IsLeap(
> > >> +//  INTN timer
> > >> +//  )
> > >> +BOOLEAN
> > >> +IsLeap (
> > >> +  time_t  timer
> > >> +  )
> > >> +{
> > >> +  INT64  Remainder1;
> > >> +  INT64  Remainder2;
> > >> +  INT64  Remainder3;
> > >> +
> > >> +  DivS64x64Remainder (timer, 4, &Remainder1);
> > >> +  DivS64x64Remainder (timer, 100, &Remainder2);
> > >> +  DivS64x64Remainder (timer, 400, &Remainder3);
> > >> +
> > >> +  return (Remainder1 == 0 && (Remainder2 != 0 || Remainder3 ==
> 0));
> > >> +}
> > >> +
> > >>  /* Get the system time as seconds elapsed since midnight, January 1,
> > 1970.
> > >> */
> > >>  // INTN time(
> > >>  //  INTN *timer
> > >> @@ -117,12 +136,13 @@ gmtime (
> > >>    )
> > >>  {
> > >>    struct tm  *GmTime;
> > >> -  UINT16     DayNo;
> > >> -  UINT32     DayRemainder;
> > >> +  UINT64     DayNo;
> > >> +  UINT64     DayRemainder;
> > >>    time_t     Year;
> > >>    time_t     YearNo;
> > >> -  UINT16     TotalDays;
> > >> -  UINT16     MonthNo;
> > >> +  UINT32     TotalDays;
> > >> +  UINT32     MonthNo;
> > >> +  INT64      Remainder;
> > >>
> > >>    if (timer == NULL) {
> > >>      return NULL;
> > >> @@ -135,18 +155,21 @@ gmtime (
> > >>
> > >>    ZeroMem ((VOID *)GmTime, (UINTN)sizeof (struct tm));
> > >>
> > >> -  DayNo        = (UINT16)(*timer / SECSPERDAY);
> > >> -  DayRemainder = (UINT32)(*timer % SECSPERDAY);
> > >> +  DayNo        = (UINT64)DivS64x64Remainder (*timer, SECSPERDAY,
> > >> &Remainder);
> > >> +  DayRemainder = (UINT64)Remainder;
> > >>
> > >> -  GmTime->tm_sec  = (int)(DayRemainder % SECSPERMIN);
> > >> -  GmTime->tm_min  = (int)((DayRemainder % SECSPERHOUR) /
> > >> SECSPERMIN);
> > >> -  GmTime->tm_hour = (int)(DayRemainder / SECSPERHOUR);
> > >> -  GmTime->tm_wday = (int)((DayNo + 4) % 7);
> > >> +  DivS64x64Remainder (DayRemainder, SECSPERMIN, &Remainder);
> > >> +  GmTime->tm_sec  = (int)Remainder;
> > >> +  DivS64x64Remainder (DayRemainder, SECSPERHOUR, &Remainder);
> > >> +  GmTime->tm_min  = (int)DivS64x64Remainder (Remainder,
> > SECSPERMIN,
> > >> NULL);
> > >> +  GmTime->tm_hour = (int)DivS64x64Remainder (DayRemainder,
> > >> SECSPERHOUR, NULL);
> > >> +  DivS64x64Remainder ((DayNo + 4), 7, &Remainder);
> > >> +  GmTime->tm_wday = (int)Remainder;
> > >>
> > >>    for (Year = 1970, YearNo = 0; DayNo > 0; Year++) {
> > >> -    TotalDays = (UINT16)(IsLeap (Year) ? 366 : 365);
> > >> +    TotalDays = (UINT32)(IsLeap (Year) ? 366 : 365);
> > >>      if (DayNo >= TotalDays) {
> > >> -      DayNo = (UINT16)(DayNo - TotalDays);
> > >> +      DayNo = (UINT64)(DayNo - TotalDays);
> > >>        YearNo++;
> > >>      } else {
> > >>        break;
> > >> @@ -158,7 +181,7 @@ gmtime (
> > >>
> > >>    for (MonthNo = 12; MonthNo > 1; MonthNo--) {
> > >>      if (DayNo >= CumulativeDays[IsLeap (Year)][MonthNo]) {
> > >> -      DayNo = (UINT16)(DayNo - (UINT16)(CumulativeDays[IsLeap
> > >> (Year)][MonthNo]));
> > >> +      DayNo = (UINT64)(DayNo - (UINT32)(CumulativeDays[IsLeap
> > >> (Year)][MonthNo]));
> > >>        break;
> > >>      }
> > >>    }
> > >> --
> > >> 2.20.1.windows.1
> > >>
> > >>
> > >>
> > >>
> > >>
> > >
> > >
> > > .
> > >
> 
> 
> 
> 


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2022-12-01 14:23 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-11-25  9:11 [PATCH EDK2 v1 0/1] CryptoPkg/BaseCryptLib:time overflow wenyi,xie
2022-11-25  9:11 ` [PATCH EDK2 v1 1/1] " wenyi,xie
2022-12-01  8:34   ` [edk2-devel] " Yao, Jiewen
2022-12-01 12:34     ` wenyi,xie
2022-12-01 13:36       ` Yao, Jiewen
     [not found]       ` <172CAED90E086CFF.1705@groups.io>
2022-12-01 14:23         ` Yao, Jiewen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox