Skip to content

Commit c544f6f

Browse files
committed
use 64 bit time stamps globally
This change now implies that 32 bit systems support the time in seconds since Epoch beyond 2038. Signed-off-by: Stephan Mueller <smueller@chronox.de>
1 parent 5f53703 commit c544f6f

File tree

9 files changed

+80
-52
lines changed

9 files changed

+80
-52
lines changed

TODO

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,3 @@
2222
- RISCV64 ASM / ZBB / ML-KEM: add to Linux kernel
2323

2424
- X.509/PKCS7 parser: add to the Linux kernel
25-
26-
- replace use of time() with clock_gettime(CLOCK_REALTIME)

asn1/src/pkcs7_generator.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -583,14 +583,13 @@ static int pkcs7_set_time(uint8_t *data, size_t *avail_datalen, uint8_t *tag)
583583
*/
584584
char datestr[X509_GENTIM_SIZE + 2];
585585
struct tm *time_detail;
586-
time_t timeval = time(NULL);
586+
time64_t timeval;
587587
int ret;
588588

589-
if (timeval == (time_t)-1)
590-
return -EFAULT;
589+
CKINT(lc_get_time(&timeval));
591590

592591
/* UTC time */
593-
time_detail = gmtime(&timeval);
592+
time_detail = gmtime((const time_t *)&timeval);
594593

595594
/*
596595
* The value is the time since EPOCH for 2050-01-01

asn1/tests/meson.build

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -303,19 +303,14 @@ if get_option('x509_generator').enabled()
303303
'--serial', '0102030405060708' ],
304304
suite: regression)
305305

306-
# This tests the different times: valid-from is UCTTime, valid-to is
307-
# General Time - only applicable on 64 bit systems as 32 bit systems
308-
# cannot represent time beyond 2038
309-
if not arm32_neon_asm
310-
test('X.509 Generator 13', asn1_enc_tester,
311-
args: [ '--eku', '50', '--ca', '--san-dns', 'leancrypto',
312-
'--san-ip', '1.1.1.1', '--keyusage', '64',
313-
'--skid', '0102030405', '--akid', '08090a0b',
314-
'--valid-from', '1729527728', '--valid-to', '2524608000',
315-
'--subject-cn', 'subject', '--issuer-cn', 'issuer',
316-
'--serial', '0102030405060708' ],
317-
suite: regression)
318-
endif
306+
test('X.509 Generator 13', asn1_enc_tester,
307+
args: [ '--eku', '50', '--ca', '--san-dns', 'leancrypto',
308+
'--san-ip', '1.1.1.1', '--keyusage', '64',
309+
'--skid', '0102030405', '--akid', '08090a0b',
310+
'--valid-from', '1729527728', '--valid-to', '2524608000',
311+
'--subject-cn', 'subject', '--issuer-cn', 'issuer',
312+
'--serial', '0102030405060708' ],
313+
suite: regression)
319314

320315
asn1_extensions_enc_tester = executable('asn1_extensions_enc_tester',
321316
[ 'asn1_extensions_enc_tester.c',

drng/src/seeded_rng.c

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ struct lc_seeded_rng_ctx {
101101
#define LC_SEEDED_RNG_MAX_BYTES (1 << 20) /* Max bytes without reseed */
102102
size_t bytes;
103103
#define LC_SEEDED_RNG_MAX_TIME 60 /* Max seconds without reseed */
104-
unsigned long last_seeded;
104+
time64_t last_seeded;
105105
pid_t pid; /* Detect a fork */
106106
mutex_w_t lock; /* Lock */
107107
};
@@ -121,9 +121,18 @@ static struct lc_seeded_rng_ctx seeded_rng = {
121121

122122
#ifdef LINUX_KERNEL
123123

124-
static unsigned long get_time(void)
124+
static int time64_after(time64_t curr, time64_t base)
125125
{
126-
return jiffies / HZ;
126+
if (curr < 0)
127+
return 0;
128+
if (base < 0)
129+
return 1;
130+
return (curr > base) ? 1 : 0;
131+
}
132+
133+
static time64_t get_time(void)
134+
{
135+
return (time64_t)(jiffies / HZ);
127136
}
128137

129138
#elif defined(LC_EFI_ENVIRONMENT)
@@ -132,36 +141,37 @@ static unsigned long get_time(void)
132141
* Time-based reseeding is disabled in EFI environment, it only relies on
133142
* the number of generated bytes to determine the reseed trigger.
134143
*/
135-
static int time_after(unsigned long curr, unsigned long base)
144+
static int time64_after(time64_t curr, time64_t base)
136145
{
137146
(void)curr;
138147
(void)base;
139148
return 0;
140149
}
141150

142-
static unsigned long get_time(void)
151+
static time64_t get_time(void)
143152
{
144153
return 0;
145154
}
146155

147156
#else /* LINUX_KERNEL */
148157

149-
static int time_after(unsigned long curr, unsigned long base)
158+
static int time64_after(time64_t curr, time64_t base)
150159
{
151-
if (curr == (unsigned long)-1)
160+
if (curr < 0)
152161
return 0;
153-
if (base == (unsigned long)-1)
162+
if (base < 0)
154163
return 1;
155164
return (curr > base) ? 1 : 0;
156165
}
157166

158-
static unsigned long get_time(void)
167+
static time64_t get_time(void)
159168
{
160-
time_t t = time(NULL);
169+
time64_t t;
161170

162-
if (t == (time_t)-1)
171+
if (lc_get_time(&t))
163172
return 0;
164-
return (unsigned long)t;
173+
174+
return t;
165175
}
166176

167177
#endif /* LINUX_KERNEL */
@@ -233,11 +243,11 @@ void lc_seeded_rng_zero_state(void)
233243
seeded_rng_noise_fini();
234244
}
235245

236-
static unsigned long time_after_now(unsigned long base)
246+
static time64_t time_after_now(time64_t base)
237247
{
238-
unsigned long curr = get_time();
248+
time64_t curr = get_time();
239249

240-
return time_after(curr, base) ? (curr - base) : 0;
250+
return time64_after(curr, base) ? (curr - base) : 0;
241251
}
242252

243253
static int lc_seeded_rng_must_reseed(struct lc_seeded_rng_ctx *rng,

internal/api/ext_headers.h

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,12 @@ typedef s64 time64_t;
8181

8282
static inline int lc_get_time(time64_t *time_since_epoch)
8383
{
84-
(void)time_since_epoch;
85-
return -EOPNOTSUPP;
84+
if (!time_since_epoch)
85+
return -EINVAL;
86+
87+
*time_since_epoch = (time64_t)(jiffies / HZ);
88+
89+
return 0;
8690
}
8791

8892
#elif (defined(LC_EFI_ENVIRONMENT))
@@ -303,15 +307,18 @@ typedef int64_t time64_t;
303307

304308
static inline int lc_get_time(time64_t *time_since_epoch)
305309
{
310+
struct timespec tp = { 0 };
311+
306312
if (!time_since_epoch)
307313
return -EINVAL;
308314

309-
*time_since_epoch = time(NULL);
310-
311-
if (*time_since_epoch == (time_t)-1)
312-
return -errno;
315+
if (clock_gettime(CLOCK_REALTIME, &tp) == 0) {
316+
*time_since_epoch = tp.tv_sec;
317+
return 0;
318+
}
313319

314-
return 0;
320+
*time_since_epoch = (time64_t)-1;
321+
return -errno;
315322
}
316323

317324
#else /* LINUX_KERNEL */
@@ -377,15 +384,18 @@ typedef int64_t time64_t;
377384

378385
static inline int lc_get_time(time64_t *time_since_epoch)
379386
{
387+
struct timespec tp = { 0 };
388+
380389
if (!time_since_epoch)
381390
return -EINVAL;
382391

383-
*time_since_epoch = time(NULL);
384-
385-
if (*time_since_epoch == (time_t)-1)
386-
return -errno;
392+
if (clock_gettime(CLOCK_REALTIME, &tp) == 0) {
393+
*time_since_epoch = tp.tv_sec;
394+
return 0;
395+
}
387396

388-
return 0;
397+
*time_since_epoch = (time64_t)-1;
398+
return -errno;
389399
}
390400

391401
#endif /* LINUX_KERNEL */

internal/tests/memcmp_secure_test.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "lc_hash_drbg.h"
2525
#include "lc_memcmp_secure.h"
2626
#include "small_stack_support.h"
27+
#include "ret_checkers.h"
2728

2829
static int memcmp_secure_tester(void)
2930
{
@@ -32,12 +33,14 @@ static int memcmp_secure_tester(void)
3233
};
3334
LC_DRBG_HASH_CTX_ON_STACK(drbg);
3435
uint8_t *ap, *bp;
35-
time_t now = time(NULL);
36+
time64_t now;
3637
unsigned int i;
3738
int ret = 1;
3839
unsigned short rnd = 0, add = 0;
3940
LC_DECLARE_MEM(ws, struct workspace, 32);
4041

42+
CKINT(lc_get_time(&now));
43+
4144
if (lc_rng_seed(drbg, (uint8_t *)&now, sizeof(now), NULL, 0))
4245
return 1;
4346

internal/tests/memcpy_secure_test.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "lc_hash_drbg.h"
2525
#include "lc_memcmp_secure.h"
2626
#include "lc_memcpy_secure.h"
27+
#include "ret_checkers.h"
2728
#include "small_stack_support.h"
2829

2930
static int memcpy_secure_tester(void)
@@ -33,12 +34,14 @@ static int memcpy_secure_tester(void)
3334
};
3435
LC_DRBG_HASH_CTX_ON_STACK(drbg);
3536
uint8_t *ap, *bp;
36-
time_t now = time(NULL);
37+
time64_t now;
3738
unsigned int i;
3839
int ret = 1;
3940
unsigned short rnd = 0, add = 0;
4041
LC_DECLARE_MEM(ws, struct workspace, 32);
4142

43+
CKINT(lc_get_time(&now));
44+
4245
if (lc_rng_seed(drbg, (uint8_t *)&now, sizeof(now), NULL, 0))
4346
return 1;
4447

meson.build

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@ if (get_option('fips140').enabled())
169169
add_global_arguments([ '-DLC_FIPS140' ], language: 'c')
170170
endif
171171

172+
# 64 bit time to not suffer from Y2038 problem
173+
add_global_arguments([ '-D_TIME_BITS=64' ], language: 'c')
174+
172175
# Header generation
173176
header_script = find_program('addon/generate_header.sh', required: true)
174177

@@ -400,6 +403,12 @@ if get_option('pkcs7_parser').disabled() and get_option('pkcs7_generator').enabl
400403
error('PKCS#7 / CMS generator support requires PKCS#7 / CMS parser support')
401404
endif
402405

406+
if (get_option('efi').enabled() and
407+
(get_option('pkcs7_generator').enabled() or
408+
get_option('x509_generator').enabled()))
409+
error('EFI compilation support for PKCS#7 message generator / X.509 certificate generator not provided')
410+
endif
411+
403412
################################################################################
404413
# Enable assembler support
405414
################################################################################

otp/src/totp.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,17 @@ LC_INTERFACE_FUNCTION(int, lc_totp, const uint8_t *hmac_key,
3131
size_t hmac_key_len, uint32_t step, uint32_t digits,
3232
uint32_t *totp_val)
3333
{
34-
time_t now;
34+
time64_t now;
3535
uint64_t counter;
36+
int ret;
3637

3738
if (!totp_val)
3839
return -EINVAL;
3940

4041
/* Get time in seconds since Epoch */
41-
now = time(NULL);
42-
if (now == (time_t)-1)
43-
return -errno;
42+
ret = lc_get_time(&now);
43+
if (ret)
44+
return ret;
4445

4546
counter = (uint64_t)now;
4647
counter /= step;

0 commit comments

Comments
 (0)