Fix a integer overflow reported by Felix Weinrank.
#ifdef __FreeBSD__
#include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/netinet/sctp_input.c 323372 2017-09-09 19:49:50Z tuexen $");
+__FBSDID("$FreeBSD: head/sys/netinet/sctp_input.c 323774 2017-09-19 20:09:58Z tuexen $");
#endif
#include <netinet/sctp_os.h>
@@ -2681,9 +2681,6 @@ sctp_handle_cookie_echo(struct mbuf *m, int iphlen, int offset,
/* Expire time is in Ticks, so we convert to seconds */
time_expires.tv_sec = cookie->time_entered.tv_sec + TICKS_TO_SEC(cookie->cookie_life);
time_expires.tv_usec = cookie->time_entered.tv_usec;
- /* TODO sctp_constants.h needs alternative time macros when
- * _KERNEL is undefined.
- */
#ifndef __FreeBSD__
if (timercmp(&now, &time_expires, >))
#else
@@ -2693,7 +2690,9 @@ sctp_handle_cookie_echo(struct mbuf *m, int iphlen, int offset,
/* cookie is stale! */
struct mbuf *op_err;
struct sctp_error_stale_cookie *cause;
- uint32_t tim;
+ struct timeval diff;
+ uint32_t staleness;
+
op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_error_stale_cookie),
0, M_NOWAIT, 1, MT_DATA);
if (op_err == NULL) {
@@ -2706,12 +2705,23 @@ sctp_handle_cookie_echo(struct mbuf *m, int iphlen, int offset,
cause->cause.code = htons(SCTP_CAUSE_STALE_COOKIE);
cause->cause.length = htons((sizeof(struct sctp_paramhdr) +
(sizeof(uint32_t))));
- /* seconds to usec */
- tim = (now.tv_sec - time_expires.tv_sec) * 1000000;
- /* add in usec */
- if (tim == 0)
- tim = now.tv_usec - cookie->time_entered.tv_usec;
- cause->stale_time = htonl(tim);
+#ifndef __FreeBSD__
+ timersub(&now, &time_expires, &diff);
+#else
+ diff = now;
+ timevalsub(&diff, &time_expires);
+#endif
+ if (diff.tv_sec > UINT32_MAX / 1000000) {
+ staleness = UINT32_MAX;
+ } else {
+ staleness = diff.tv_sec * 1000000;
+ }
+ if (UINT32_MAX - staleness >= diff.tv_usec) {
+ staleness += diff.tv_usec;
+ } else {
+ staleness = UINT32_MAX;
+ }
+ cause->stale_time = htonl(staleness);
sctp_send_operr_to(src, dst, sh, cookie->peers_vtag, op_err,
#if defined(__FreeBSD__)
mflowtype, mflowid, l_inp->fibnum,
