From nobody Mon May 6 22:51:14 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+96625+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+96625+1787277+3901457@groups.io ARC-Seal: i=1; a=rsa-sha256; t=1669367519; cv=none; d=zohomail.com; s=zohoarc; b=FZwgbTH7HO5kGY5EE/TbPfOV8t+8XKiHSfRbRSVUoLrbKkeS4wPAzOt8qNj7qc0EQEjYn/tzN+5ZWy9aaWXlgVChxukImgAdyo3iUShwWBgEURRzs4j61H7FC/6Dj2Zd5sgrRA7kZXkZvg695T4kyc8DBxnJaPhYq8SRbGvxPag= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1669367519; h=Content-Type:Cc:Date:From:In-Reply-To:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=QysgeQkdCJwwJNr4w4H+6He5at/oc0Vtve8+6bN9C58=; b=IzqI+ZMFtjsMzo3E3yLXWtO0fBwSX0Xx2/utj8fHpk7rgP2VfCN/t9eiTeHD0duzNzuEAICYA3DhpcW8+JWbFnZB9+rqo9K1DYqOVK0kjt6ki7eT8fT9ZkpHJeRNh1yUn6yygdc01gpex8BOcH9X/BXIV0qGaaUTBg4rOTFb6B4= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+96625+1787277+3901457@groups.io Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1669367519481194.41051401544985; Fri, 25 Nov 2022 01:11:59 -0800 (PST) Return-Path: X-Received: by 127.0.0.2 with SMTP id cuQBYY1788612xYGgM8LM1Mm; Fri, 25 Nov 2022 01:11:58 -0800 X-Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by mx.groups.io with SMTP id smtpd.web10.43394.1669367516629100965 for ; Fri, 25 Nov 2022 01:11:57 -0800 X-Received: from dggemv703-chm.china.huawei.com (unknown [172.30.72.57]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4NJTcn1KSVzRpQY for ; Fri, 25 Nov 2022 17:11:21 +0800 (CST) X-Received: from kwepemm600004.china.huawei.com (7.193.23.242) by dggemv703-chm.china.huawei.com (10.3.19.46) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Fri, 25 Nov 2022 17:11:54 +0800 X-Received: from kwephisprg16640.huawei.com (10.247.83.252) by kwepemm600004.china.huawei.com (7.193.23.242) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Fri, 25 Nov 2022 17:11:53 +0800 From: "wenyi,xie via groups.io" To: , , , , CC: , , Subject: [edk2-devel] [PATCH EDK2 v1 1/1] CryptoPkg/BaseCryptLib:time overflow Date: Fri, 25 Nov 2022 17:11:48 +0800 Message-ID: <20221125091148.2405866-2-xiewenyi2@huawei.com> In-Reply-To: <20221125091148.2405866-1-xiewenyi2@huawei.com> References: <20221125091148.2405866-1-xiewenyi2@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.247.83.252] X-ClientProxiedBy: dggems702-chm.china.huawei.com (10.3.19.179) To kwepemm600004.china.huawei.com (7.193.23.242) X-CFilter-Loop: Reflected Precedence: Bulk List-Unsubscribe: List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,xiewenyi2@huawei.com X-Gm-Message-State: eFIOWhGNN9SqdhZaFZUpDc9xx1787277AA= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1669367518; bh=7ro2nJ73i7DerpRUtSgiaTMcb4L1lOoQJRBM2Zne504=; h=CC:Content-Type:Date:From:Reply-To:Subject:To; b=Il4vrBCqss4gyjmdEYX9Il5ODDjdiRynk9XESP5TbZ/UgwNT8KXcNQ/s3nBvWchdTeY 8k/A9370sZ3urD5kIUyZZ/95wdAIYv3GQQ/D9WEMJwIbVGHeQifqUvfz9gLznRZ9cll8m ApahOBEt7N2wJGbZ9efl9B28LcWOJ0DvuIw= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1669367520972100003 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Zihong Yi REF: https://bugzilla.tianocore.org/show_bug.cgi?id=3D4167 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 Cc: Jian J Wang Cc: Xiaoyu Lu Cc: Guomin Jiang Signed-off-by: Zihong Yi Reviewed-by: Jiewen Yao --- 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/Crypto= Pkg/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 -- // =20 -#define IsLeap(y) (((y) % 4) =3D=3D 0 && (((y) % 100) !=3D 0 || ((y) % 40= 0) =3D=3D 0)) #define SECSPERMIN (60) #define SECSPERHOUR (60 * 60) #define SECSPERDAY (24 * SECSPERHOUR) @@ -60,6 +59,26 @@ UINTN CumulativeDays[2][14] =3D { } }; =20 +/* 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 =3D=3D 0 && (Remainder2 !=3D 0 || Remainder3 =3D=3D 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; =20 if (timer =3D=3D NULL) { return NULL; @@ -135,18 +155,21 @@ gmtime ( =20 ZeroMem ((VOID *)GmTime, (UINTN)sizeof (struct tm)); =20 - DayNo =3D (UINT16)(*timer / SECSPERDAY); - DayRemainder =3D (UINT32)(*timer % SECSPERDAY); + DayNo =3D (UINT64)DivS64x64Remainder (*timer, SECSPERDAY, &Remain= der); + DayRemainder =3D (UINT64)Remainder; =20 - GmTime->tm_sec =3D (int)(DayRemainder % SECSPERMIN); - GmTime->tm_min =3D (int)((DayRemainder % SECSPERHOUR) / SECSPERMIN); - GmTime->tm_hour =3D (int)(DayRemainder / SECSPERHOUR); - GmTime->tm_wday =3D (int)((DayNo + 4) % 7); + DivS64x64Remainder (DayRemainder, SECSPERMIN, &Remainder); + GmTime->tm_sec =3D (int)Remainder; + DivS64x64Remainder (DayRemainder, SECSPERHOUR, &Remainder); + GmTime->tm_min =3D (int)DivS64x64Remainder (Remainder, SECSPERMIN, NULL= ); + GmTime->tm_hour =3D (int)DivS64x64Remainder (DayRemainder, SECSPERHOUR, = NULL); + DivS64x64Remainder ((DayNo + 4), 7, &Remainder); + GmTime->tm_wday =3D (int)Remainder; =20 for (Year =3D 1970, YearNo =3D 0; DayNo > 0; Year++) { - TotalDays =3D (UINT16)(IsLeap (Year) ? 366 : 365); + TotalDays =3D (UINT32)(IsLeap (Year) ? 366 : 365); if (DayNo >=3D TotalDays) { - DayNo =3D (UINT16)(DayNo - TotalDays); + DayNo =3D (UINT64)(DayNo - TotalDays); YearNo++; } else { break; @@ -158,7 +181,7 @@ gmtime ( =20 for (MonthNo =3D 12; MonthNo > 1; MonthNo--) { if (DayNo >=3D CumulativeDays[IsLeap (Year)][MonthNo]) { - DayNo =3D (UINT16)(DayNo - (UINT16)(CumulativeDays[IsLeap (Year)][Mo= nthNo])); + DayNo =3D (UINT64)(DayNo - (UINT32)(CumulativeDays[IsLeap (Year)][Mo= nthNo])); break; } } --=20 2.20.1.windows.1 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#96625): https://edk2.groups.io/g/devel/message/96625 Mute This Topic: https://groups.io/mt/95251049/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-