Make valid_temp_time overflow-resistant

This commit is contained in:
Ed Kellett 2022-03-18 18:13:31 +00:00
parent 1fcdacb424
commit ba95896969
2 changed files with 20 additions and 5 deletions

View file

@ -40,6 +40,8 @@
#include <openssl/rsa.h> #include <openssl/rsa.h>
#endif #endif
#define MAX_TEMP_TIME (52 * 7 * 24 * 60 * 60)
struct Client; struct Client;
struct ConfItem; struct ConfItem;

View file

@ -687,8 +687,11 @@ valid_temp_time(const char *p)
time_t result = 0; time_t result = 0;
long current = 0; long current = 0;
time_t max_time = (uintmax_t) (~(time_t)0) >> 1;
while (*p) { while (*p) {
char *endp; char *endp;
int mul;
errno = 0; errno = 0;
current = strtol(p, &endp, 10); current = strtol(p, &endp, 10);
@ -703,28 +706,38 @@ valid_temp_time(const char *p)
switch (*endp) { switch (*endp) {
case '\0': /* No unit was given so send it back as minutes */ case '\0': /* No unit was given so send it back as minutes */
case 'm': case 'm':
result += current * 60; mul = 60;
break; break;
case 'h': case 'h':
result += current * 3600; mul = 3600;
break; break;
case 'd': case 'd':
result += current * 86400; mul = 86400;
break; break;
case 'w': case 'w':
result += current * 604800; mul = 604800;
break; break;
default: default:
return -1; return -1;
} }
if (current > LONG_MAX / mul)
return MAX_TEMP_TIME;
current *= mul;
if (current > max_time - result)
return MAX_TEMP_TIME;
result += current;
if (*endp == '\0') if (*endp == '\0')
break; break;
p = endp + 1; p = endp + 1;
} }
return MIN(result, 60 * 60 * 24 * 7 * 52); return MIN(result, MAX_TEMP_TIME);
} }
/* Propagated bans are expired elsewhere. */ /* Propagated bans are expired elsewhere. */