Portable build framework for OpenNTPD
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
2.0 KiB

  1. From 0155adc8a0e61c0b1b7dc8febcba049b0d84bb31 Mon Sep 17 00:00:00 2001
  2. From: Brent Cook <busterb@gmail.com>
  3. Date: Sun, 22 Mar 2015 16:18:20 -0500
  4. Subject: [PATCH 11/11] Account for integer overflows with a 32-bit time_t
  5. If the current time is set past Feb. 2036, adding the NTP epoch offset
  6. overflows the time_t. While all systems in 2036 should have been running
  7. with a larger time_t for some time, today an invalid system clock cannot
  8. be reset with the overflow.
  9. Thanks to @Romua1d on github for point it out.
  10. ---
  11. src/usr.sbin/ntpd/client.c | 6 +++++-
  12. src/usr.sbin/ntpd/util.c | 12 ++++++++++--
  13. 2 files changed, 15 insertions(+), 3 deletions(-)
  14. diff --git a/src/usr.sbin/ntpd/client.c b/src/usr.sbin/ntpd/client.c
  15. index d47869b..70879c0 100644
  16. --- a/src/usr.sbin/ntpd/client.c
  17. +++ b/src/usr.sbin/ntpd/client.c
  18. @@ -269,7 +269,11 @@ client_dispatch(struct ntp_peer *p, u_int8_t settime)
  19. if (cmsg->cmsg_level == SOL_SOCKET &&
  20. cmsg->cmsg_type == SCM_TIMESTAMP) {
  21. memcpy(&tv, CMSG_DATA(cmsg), sizeof(tv));
  22. - T4 += tv.tv_sec + JAN_1970 + 1.0e-6 * tv.tv_usec;
  23. + /*
  24. + * account for overflow that occurs on OSes that still
  25. + * have a 32-bit time_t.
  26. + */
  27. + T4 += (uint64_t)tv.tv_sec + JAN_1970 + 1.0e-6 * tv.tv_usec;
  28. break;
  29. }
  30. }
  31. diff --git a/src/usr.sbin/ntpd/util.c b/src/usr.sbin/ntpd/util.c
  32. index 0867ed7..e9f6d50 100644
  33. --- a/src/usr.sbin/ntpd/util.c
  34. +++ b/src/usr.sbin/ntpd/util.c
  35. @@ -45,13 +45,21 @@ gettime(void)
  36. if (gettimeofday(&tv, NULL) == -1)
  37. fatal("gettimeofday");
  38. - return (tv.tv_sec + JAN_1970 + 1.0e-6 * tv.tv_usec);
  39. + /*
  40. + * account for overflow that occurs on OSes that still
  41. + * have a 32-bit time_t.
  42. + */
  43. + return ((uint64_t)tv.tv_sec + JAN_1970 + 1.0e-6 * tv.tv_usec);
  44. }
  45. double
  46. gettime_from_timeval(struct timeval *tv)
  47. {
  48. - return (tv->tv_sec + JAN_1970 + 1.0e-6 * tv->tv_usec);
  49. + /*
  50. + * account for overflow that occurs on OSes that still
  51. + * have a 32-bit time_t.
  52. + */
  53. + return ((uint64_t)tv->tv_sec + JAN_1970 + 1.0e-6 * tv->tv_usec);
  54. }
  55. time_t
  56. --
  57. 1.9.1