fixed unsafe usage of gmtime

reported by lgtm
This commit is contained in:
Frank Morgner 2019-01-14 10:01:14 +01:00
parent b8b4f7c36f
commit d806ee3245
3 changed files with 28 additions and 9 deletions

View File

@ -2265,6 +2265,7 @@ pgp_store_creationtime(sc_card_t *card, u8 key_id, time_t *outtime)
const size_t timestrlen = 64;
char timestring[65];
u8 buf[4];
struct tm tm;
LOG_FUNC_CALLED(card->ctx);
@ -2278,7 +2279,14 @@ pgp_store_creationtime(sc_card_t *card, u8 key_id, time_t *outtime)
/* set output */
*outtime = createtime = time(NULL);
strftime(timestring, timestrlen, "%c %Z", gmtime(&createtime));
#ifdef _WIN32
if (0 != gmtime_s(&tm, &createtime))
LOG_FUNC_RETURN(card->ctx, SC_ERROR_INTERNAL);
#else
if (NULL == gmtime_r(&createtime, &tm))
LOG_FUNC_RETURN(card->ctx, SC_ERROR_INTERNAL);
#endif
strftime(timestring, timestrlen, "%c %Z", &tm);
sc_log(card->ctx, "Creation time %s.", timestring);
/* Code borrowed from GnuPG */
ulong2bebytes(buf, (unsigned long)createtime);

View File

@ -2605,7 +2605,7 @@ sc_pkcs15_get_generalized_time(struct sc_context *ctx, char **out)
#ifdef HAVE_GETTIMEOFDAY
struct timeval tv;
#endif
struct tm *tm_time;
struct tm tm;
time_t t;
if (!ctx || !out)
@ -2618,16 +2618,21 @@ sc_pkcs15_get_generalized_time(struct sc_context *ctx, char **out)
#else
t = time(NULL);
#endif
tm_time = gmtime(&t);
if (!tm_time)
LOG_TEST_RET(ctx, SC_ERROR_INTERNAL, "gmtime failed");
#ifdef _WIN32
if (0 != gmtime_s(&tm, &t))
LOG_FUNC_RETURN(ctx, SC_ERROR_INTERNAL);
#else
if (NULL == gmtime_r(&t, &tm))
LOG_FUNC_RETURN(ctx, SC_ERROR_INTERNAL);
#endif
*out = calloc(1, 16);
if (*out == NULL)
LOG_TEST_RET(ctx, SC_ERROR_OUT_OF_MEMORY, "memory failure");
/* print time in generalized time format */
if (!strftime(*out, 16, "%Y%m%d%H%M%SZ", tm_time)) {
if (!strftime(*out, 16, "%Y%m%d%H%M%SZ", &tm)) {
free(*out);
LOG_TEST_RET(ctx, SC_ERROR_INTERNAL, "strftime failed");
}

View File

@ -240,11 +240,17 @@ static char *prettify_date(u8 *data, size_t length)
{
if (data != NULL && length == 4) {
time_t time = (time_t) (data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3]);
struct tm *tp;
struct tm tm;
static char result[64]; /* large enough */
tp = gmtime(&time);
strftime(result, sizeof(result), "%Y-%m-%d %H:%M:%S", tp);
#ifdef _WIN32
if (0 != gmtime_s(&tm, &time))
return NULL;
#else
if (NULL == gmtime_r(&time, &tm))
return NULL;
#endif
strftime(result, sizeof(result), "%Y-%m-%d %H:%M:%S", &tm);
return result;
}
return NULL;