util: check for gmtime() error

Fix the UTI_TimeToLogForm() function to check if gmtime() didn't fail.
This caused chronyc to crash due to dereferencing a NULL pointer when
a response to the "manual list" request contained time which gmtime()
could not convert to broken-down representation.

This issue was found in an audit performed by Cure53 and sponsored by
Mozilla.
This commit is contained in:
Miroslav Lichvar 2017-08-24 11:12:14 +02:00
parent a06c9909a6
commit f40b0024bd

10
util.c
View file

@ -610,13 +610,17 @@ UTI_SockaddrFamilyToString(int family)
char * char *
UTI_TimeToLogForm(time_t t) UTI_TimeToLogForm(time_t t)
{ {
struct tm stm; struct tm *stm;
char *result; char *result;
result = NEXT_BUFFER; result = NEXT_BUFFER;
stm = *gmtime(&t); stm = gmtime(&t);
strftime(result, BUFFER_LENGTH, "%Y-%m-%d %H:%M:%S", &stm);
if (stm)
strftime(result, BUFFER_LENGTH, "%Y-%m-%d %H:%M:%S", stm);
else
snprintf(result, BUFFER_LENGTH, "INVALID INVALID ");
return result; return result;
} }