Skip to content

Commit c16a5d6

Browse files
committed
harden JSON parsing against OOB reads
1 parent 6f52254 commit c16a5d6

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

src/json/load_config.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,16 +368,22 @@ static wolfsentry_errcode_t convert_eventconfig_flag(JSON_TYPE type, wolfsentry_
368368

369369
static wolfsentry_errcode_t convert_wolfsentry_duration(struct wolfsentry_context *wolfsentry, JSON_TYPE type, const unsigned char *data, size_t data_size, wolfsentry_time_t *out) {
370370
wolfsentry_errcode_t ret;
371+
char buf[24];
371372
char *endptr;
372373
long conv;
373374

374375
if ((type != JSON_STRING) && (type != JSON_NUMBER))
375376
WOLFSENTRY_ERROR_RETURN(CONFIG_INVALID_VALUE);
376377

378+
if (data_size >= sizeof buf)
379+
WOLFSENTRY_ERROR_RETURN(NUMERIC_ARG_TOO_BIG);
380+
memcpy(buf, data, data_size);
381+
buf[data_size] = 0;
382+
377383
#ifndef WOLFSENTRY_NO_ERRNO_H
378384
errno = 0;
379385
#endif
380-
conv = strtol((const char *)data, &endptr, 0);
386+
conv = strtol(buf, &endptr, 0);
381387
#ifndef WOLFSENTRY_NO_ERRNO_H
382388
if (errno != 0)
383389
WOLFSENTRY_ERROR_RETURN(CONFIG_INVALID_VALUE);
@@ -405,7 +411,7 @@ static wolfsentry_errcode_t convert_wolfsentry_duration(struct wolfsentry_contex
405411
default:
406412
break;
407413
}
408-
if ((size_t)(endptr - (char *)data) != data_size)
414+
if ((size_t)(endptr - buf) != data_size)
409415
WOLFSENTRY_ERROR_RETURN(CONFIG_INVALID_VALUE);
410416
if ((ret = wolfsentry_interval_from_seconds(wolfsentry, conv, 0 /* howlong_nsecs */, out)) < 0)
411417
WOLFSENTRY_ERROR_RERETURN(ret);
@@ -602,6 +608,10 @@ static inline int convert_hex_byte(const unsigned char **in, size_t *in_len, byt
602608
return d1;
603609
++(*in);
604610
--(*in_len);
611+
if (*in_len < 1) {
612+
*out = (byte)d1;
613+
return 0;
614+
}
605615
d2 = convert_hex_digit(**in);
606616
if (d2 < 0) {
607617
*out = (byte)d1;

0 commit comments

Comments
 (0)