aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/ntp/tests
diff options
context:
space:
mode:
authorXin LI <delphij@FreeBSD.org>2016-12-22 16:19:05 +0000
committerXin LI <delphij@FreeBSD.org>2016-12-22 16:19:05 +0000
commitb06b7e647fe7b4eefbd29369cf0c24e1902bf72a (patch)
treed18590df36faea870c6827c64a75516475938e4d /contrib/ntp/tests
parent8c7ff71d356bdceffa6cd5b5017c65d0d63d9abc (diff)
downloadsrc-releng/9.3.tar.gz
src-releng/9.3.zip
Fix multiple vulnerabilities of ntp.releng/9.3
Approved by: so
Notes
Notes: svn path=/releng/9.3/; revision=310419
Diffstat (limited to 'contrib/ntp/tests')
-rw-r--r--contrib/ntp/tests/libntp/a_md5encrypt.c4
-rw-r--r--contrib/ntp/tests/libntp/calendar.c127
-rw-r--r--contrib/ntp/tests/libntp/run-calendar.c35
-rw-r--r--contrib/ntp/tests/libntp/sfptostr.c6
-rw-r--r--contrib/ntp/tests/ntpd/Makefile.am1
-rw-r--r--contrib/ntp/tests/ntpd/Makefile.in18
6 files changed, 152 insertions, 39 deletions
diff --git a/contrib/ntp/tests/libntp/a_md5encrypt.c b/contrib/ntp/tests/libntp/a_md5encrypt.c
index d8e7ab93e1ff..a87aa796de72 100644
--- a/contrib/ntp/tests/libntp/a_md5encrypt.c
+++ b/contrib/ntp/tests/libntp/a_md5encrypt.c
@@ -49,9 +49,7 @@ test_Encrypt(void) {
u_int32 *packetPtr;
int length;
- packetPtr = emalloc(totalLength * sizeof(*packetPtr));
-
- memset(packetPtr + packetLength, 0, keyIdLength);
+ packetPtr = emalloc_zero(totalLength * sizeof(*packetPtr));
memcpy(packetPtr, packet, packetLength);
cache_secretsize = keyLength;
diff --git a/contrib/ntp/tests/libntp/calendar.c b/contrib/ntp/tests/libntp/calendar.c
index 9d25c41b1688..b631565c0c8f 100644
--- a/contrib/ntp/tests/libntp/calendar.c
+++ b/contrib/ntp/tests/libntp/calendar.c
@@ -2,6 +2,7 @@
#include "ntp_stdlib.h" /* test fail without this include, for some reason */
#include "ntp_calendar.h"
+#include "ntp_unixtime.h"
#include "unity.h"
#include <string.h>
@@ -19,6 +20,7 @@ char * DateFromCalToString(const struct calendar *cal);
char * DateFromIsoToString(const struct isodate *iso);
int IsEqualDateCal(const struct calendar *expected, const struct calendar *actual);
int IsEqualDateIso(const struct isodate *expected, const struct isodate *actual);
+
void test_DaySplitMerge(void);
void test_SplitYearDays1(void);
void test_SplitYearDays2(void);
@@ -35,6 +37,8 @@ void test_IsoCalWeeksToYearStart(void);
void test_IsoCalWeeksToYearEnd(void);
void test_DaySecToDate(void);
+void test_NtpToNtp(void);
+void test_NtpToTime(void);
void
setUp(void)
@@ -608,3 +612,126 @@ test_DaySecToDate(void)
return;
}
+
+/* --------------------------------------------------------------------
+ * unfolding of (truncated) NTP time stamps to full 64bit values.
+ *
+ * Note: These tests need a 64bit time_t to be useful.
+ */
+
+void
+test_NtpToNtp(void)
+{
+# if SIZEOF_TIME_T <= 4
+
+ TEST_IGNORE_MESSAGE("test only useful for sizeof(time_t) > 4, skipped");
+
+# else
+
+ static const uint32_t ntp_vals[6] = {
+ UINT32_C(0x00000000),
+ UINT32_C(0x00000001),
+ UINT32_C(0x7FFFFFFF),
+ UINT32_C(0x80000000),
+ UINT32_C(0x80000001),
+ UINT32_C(0xFFFFFFFF)
+ };
+
+ static char lbuf[128];
+ vint64 hold;
+ time_t pivot, texp, diff;
+ int loops, iloop;
+
+ pivot = 0;
+ for (loops = 0; loops < 16; ++loops) {
+ for (iloop = 0; iloop < 6; ++iloop) {
+ hold = ntpcal_ntp_to_ntp(
+ ntp_vals[iloop], &pivot);
+ texp = vint64_to_time(&hold);
+
+ /* constraint 1: texp must be in the
+ * (right-open) intervall [p-(2^31), p+(2^31)[,
+ * but the pivot 'p' must be taken in full NTP
+ * time scale!
+ */
+ diff = texp - (pivot + JAN_1970);
+ snprintf(lbuf, sizeof(lbuf),
+ "bounds check: piv=%lld exp=%lld dif=%lld",
+ (long long)pivot,
+ (long long)texp,
+ (long long)diff);
+ TEST_ASSERT_MESSAGE((diff >= INT32_MIN) && (diff <= INT32_MAX),
+ lbuf);
+
+ /* constraint 2: low word must be equal to
+ * input
+ */
+ snprintf(lbuf, sizeof(lbuf),
+ "low check: ntp(in)=$%08lu ntp(out[0:31])=$%08lu",
+ (unsigned long)ntp_vals[iloop],
+ (unsigned long)hold.D_s.lo);
+ TEST_ASSERT_EQUAL_MESSAGE(ntp_vals[iloop], hold.D_s.lo, lbuf);
+ }
+ pivot += 0x20000000;
+ }
+# endif
+}
+
+void
+test_NtpToTime(void)
+{
+# if SIZEOF_TIME_T <= 4
+
+ TEST_IGNORE_MESSAGE("test only useful for sizeof(time_t) > 4, skipped");
+
+# else
+
+ static const uint32_t ntp_vals[6] = {
+ UINT32_C(0x00000000),
+ UINT32_C(0x00000001),
+ UINT32_C(0x7FFFFFFF),
+ UINT32_C(0x80000000),
+ UINT32_C(0x80000001),
+ UINT32_C(0xFFFFFFFF)
+ };
+
+ static char lbuf[128];
+ vint64 hold;
+ time_t pivot, texp, diff;
+ uint32_t back;
+ int loops, iloop;
+
+ pivot = 0;
+ for (loops = 0; loops < 16; ++loops) {
+ for (iloop = 0; iloop < 6; ++iloop) {
+ hold = ntpcal_ntp_to_time(
+ ntp_vals[iloop], &pivot);
+ texp = vint64_to_time(&hold);
+
+ /* constraint 1: texp must be in the
+ * (right-open) intervall [p-(2^31), p+(2^31)[
+ */
+ diff = texp - pivot;
+ snprintf(lbuf, sizeof(lbuf),
+ "bounds check: piv=%lld exp=%lld dif=%lld",
+ (long long)pivot,
+ (long long)texp,
+ (long long)diff);
+ TEST_ASSERT_MESSAGE((diff >= INT32_MIN) && (diff <= INT32_MAX),
+ lbuf);
+
+ /* constraint 2: conversion from full time back
+ * to truncated NTP time must yield same result
+ * as input.
+ */
+ back = (uint32_t)texp + JAN_1970;
+ snprintf(lbuf, sizeof(lbuf),
+ "modulo check: ntp(in)=$%08lu ntp(out)=$%08lu",
+ (unsigned long)ntp_vals[iloop],
+ (unsigned long)back);
+ TEST_ASSERT_EQUAL_MESSAGE(ntp_vals[iloop], back, lbuf);
+ }
+ pivot += 0x20000000;
+ }
+# endif
+}
diff --git a/contrib/ntp/tests/libntp/run-calendar.c b/contrib/ntp/tests/libntp/run-calendar.c
index 555f3ba6d67c..82309bd3b8f7 100644
--- a/contrib/ntp/tests/libntp/run-calendar.c
+++ b/contrib/ntp/tests/libntp/run-calendar.c
@@ -25,6 +25,7 @@
#include "config.h"
#include "ntp_stdlib.h"
#include "ntp_calendar.h"
+#include "ntp_unixtime.h"
#include <string.h>
//=======External Functions This Runner Calls=====
@@ -45,6 +46,8 @@ extern void test_IsoCalYearsToWeeks(void);
extern void test_IsoCalWeeksToYearStart(void);
extern void test_IsoCalWeeksToYearEnd(void);
extern void test_DaySecToDate(void);
+extern void test_NtpToNtp(void);
+extern void test_NtpToTime(void);
//=======Test Reset Option=====
@@ -63,21 +66,23 @@ int main(int argc, char *argv[])
{
progname = argv[0];
UnityBegin("calendar.c");
- RUN_TEST(test_DaySplitMerge, 22);
- RUN_TEST(test_SplitYearDays1, 23);
- RUN_TEST(test_SplitYearDays2, 24);
- RUN_TEST(test_RataDie1, 25);
- RUN_TEST(test_LeapYears1, 26);
- RUN_TEST(test_LeapYears2, 27);
- RUN_TEST(test_RoundTripDate, 28);
- RUN_TEST(test_RoundTripYearStart, 29);
- RUN_TEST(test_RoundTripMonthStart, 30);
- RUN_TEST(test_RoundTripWeekStart, 31);
- RUN_TEST(test_RoundTripDayStart, 32);
- RUN_TEST(test_IsoCalYearsToWeeks, 33);
- RUN_TEST(test_IsoCalWeeksToYearStart, 34);
- RUN_TEST(test_IsoCalWeeksToYearEnd, 35);
- RUN_TEST(test_DaySecToDate, 36);
+ RUN_TEST(test_DaySplitMerge, 24);
+ RUN_TEST(test_SplitYearDays1, 25);
+ RUN_TEST(test_SplitYearDays2, 26);
+ RUN_TEST(test_RataDie1, 27);
+ RUN_TEST(test_LeapYears1, 28);
+ RUN_TEST(test_LeapYears2, 29);
+ RUN_TEST(test_RoundTripDate, 30);
+ RUN_TEST(test_RoundTripYearStart, 31);
+ RUN_TEST(test_RoundTripMonthStart, 32);
+ RUN_TEST(test_RoundTripWeekStart, 33);
+ RUN_TEST(test_RoundTripDayStart, 34);
+ RUN_TEST(test_IsoCalYearsToWeeks, 35);
+ RUN_TEST(test_IsoCalWeeksToYearStart, 36);
+ RUN_TEST(test_IsoCalWeeksToYearEnd, 37);
+ RUN_TEST(test_DaySecToDate, 38);
+ RUN_TEST(test_NtpToNtp, 40);
+ RUN_TEST(test_NtpToTime, 41);
return (UnityEnd());
}
diff --git a/contrib/ntp/tests/libntp/sfptostr.c b/contrib/ntp/tests/libntp/sfptostr.c
index c7616c7e3837..c781c03a4a07 100644
--- a/contrib/ntp/tests/libntp/sfptostr.c
+++ b/contrib/ntp/tests/libntp/sfptostr.c
@@ -39,7 +39,7 @@ void test_PositiveInteger(void)
void test_NegativeInteger(void)
{
- s_fp test = -200 << 16; // exact -200.000000
+ s_fp test = -(200 << 16); // exact -200.000000
TEST_ASSERT_EQUAL_STRING("-200.000000", fptoa(test, SFP_MAX_PRECISION));
TEST_ASSERT_EQUAL_STRING("-200000.000", fptoms(test, SFP_MAX_PRECISION));
@@ -55,7 +55,7 @@ void test_PositiveIntegerPositiveFraction(void)
void test_NegativeIntegerNegativeFraction(void)
{
- s_fp test = (-200 << 16) - (1 << 15); // -200 - 0.5
+ s_fp test = -(200 << 16) - (1 << 15); // -200 - 0.5
TEST_ASSERT_EQUAL_STRING("-200.500000", fptoa(test, SFP_MAX_PRECISION));
TEST_ASSERT_EQUAL_STRING("-200500.000", fptoms(test, SFP_MAX_PRECISION));
@@ -71,7 +71,7 @@ void test_PositiveIntegerNegativeFraction(void)
void test_NegativeIntegerPositiveFraction(void)
{
- s_fp test = (-200 << 16) + (1 << 14)*3; // -200 + 0.75
+ s_fp test = -(200 << 16) + (1 << 14)*3; // -200 + 0.75
TEST_ASSERT_EQUAL_STRING("-199.250000", fptoa(test, SFP_MAX_PRECISION));
TEST_ASSERT_EQUAL_STRING("-199250.000", fptoms(test, SFP_MAX_PRECISION));
diff --git a/contrib/ntp/tests/ntpd/Makefile.am b/contrib/ntp/tests/ntpd/Makefile.am
index 2cab14f4c722..3ce512d89ae3 100644
--- a/contrib/ntp/tests/ntpd/Makefile.am
+++ b/contrib/ntp/tests/ntpd/Makefile.am
@@ -156,7 +156,6 @@ test_ntp_signd_SOURCES = \
t-ntp_signd.c \
run-t-ntp_signd.c \
$(srcdir)/../libntp/test-libntp.c \
- $(srcdir)/../../ntpd/ntp_signd.c \
$(NULL)
$(srcdir)/run-t-ntp_signd.c: $(srcdir)/t-ntp_signd.c $(std_unity_list)
diff --git a/contrib/ntp/tests/ntpd/Makefile.in b/contrib/ntp/tests/ntpd/Makefile.in
index 2dc7a04350b0..5c83a371a01a 100644
--- a/contrib/ntp/tests/ntpd/Makefile.in
+++ b/contrib/ntp/tests/ntpd/Makefile.in
@@ -205,7 +205,7 @@ test_ntp_scanner_LINK = $(LIBTOOL) $(AM_V_lt) --tag=CC \
-o $@
am_test_ntp_signd_OBJECTS = t-ntp_signd.$(OBJEXT) \
run-t-ntp_signd.$(OBJEXT) test-libntp.$(OBJEXT) \
- ntp_signd.$(OBJEXT) $(am__objects_1)
+ $(am__objects_1)
test_ntp_signd_OBJECTS = $(am_test_ntp_signd_OBJECTS)
test_ntp_signd_DEPENDENCIES = $(am__DEPENDENCIES_3) \
$(top_builddir)/ntpd/ntp_config.o \
@@ -871,7 +871,6 @@ test_ntp_signd_SOURCES = \
t-ntp_signd.c \
run-t-ntp_signd.c \
$(srcdir)/../libntp/test-libntp.c \
- $(srcdir)/../../ntpd/ntp_signd.c \
$(NULL)
@@ -971,7 +970,6 @@ mostlyclean-compile:
distclean-compile:
-rm -f *.tab.c
-@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ntp_signd.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/run-t-ntp_signd.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/t-ntp_signd.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/test-libntp.Po@am__quote@
@@ -1194,20 +1192,6 @@ test-libntp.obj: $(srcdir)/../libntp/test-libntp.c
@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o test-libntp.obj `if test -f '$(srcdir)/../libntp/test-libntp.c'; then $(CYGPATH_W) '$(srcdir)/../libntp/test-libntp.c'; else $(CYGPATH_W) '$(srcdir)/$(srcdir)/../libntp/test-libntp.c'; fi`
-ntp_signd.o: $(srcdir)/../../ntpd/ntp_signd.c
-@am__fastdepCC_TRUE@ $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT ntp_signd.o -MD -MP -MF $(DEPDIR)/ntp_signd.Tpo -c -o ntp_signd.o `test -f '$(srcdir)/../../ntpd/ntp_signd.c' || echo '$(srcdir)/'`$(srcdir)/../../ntpd/ntp_signd.c
-@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/ntp_signd.Tpo $(DEPDIR)/ntp_signd.Po
-@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='$(srcdir)/../../ntpd/ntp_signd.c' object='ntp_signd.o' libtool=no @AMDEPBACKSLASH@
-@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
-@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o ntp_signd.o `test -f '$(srcdir)/../../ntpd/ntp_signd.c' || echo '$(srcdir)/'`$(srcdir)/../../ntpd/ntp_signd.c
-
-ntp_signd.obj: $(srcdir)/../../ntpd/ntp_signd.c
-@am__fastdepCC_TRUE@ $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT ntp_signd.obj -MD -MP -MF $(DEPDIR)/ntp_signd.Tpo -c -o ntp_signd.obj `if test -f '$(srcdir)/../../ntpd/ntp_signd.c'; then $(CYGPATH_W) '$(srcdir)/../../ntpd/ntp_signd.c'; else $(CYGPATH_W) '$(srcdir)/$(srcdir)/../../ntpd/ntp_signd.c'; fi`
-@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/ntp_signd.Tpo $(DEPDIR)/ntp_signd.Po
-@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='$(srcdir)/../../ntpd/ntp_signd.c' object='ntp_signd.obj' libtool=no @AMDEPBACKSLASH@
-@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
-@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o ntp_signd.obj `if test -f '$(srcdir)/../../ntpd/ntp_signd.c'; then $(CYGPATH_W) '$(srcdir)/../../ntpd/ntp_signd.c'; else $(CYGPATH_W) '$(srcdir)/$(srcdir)/../../ntpd/ntp_signd.c'; fi`
-
test_rc_cmdlength-rc_cmdlength.o: rc_cmdlength.c
@am__fastdepCC_TRUE@ $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(test_rc_cmdlength_CFLAGS) $(CFLAGS) -MT test_rc_cmdlength-rc_cmdlength.o -MD -MP -MF $(DEPDIR)/test_rc_cmdlength-rc_cmdlength.Tpo -c -o test_rc_cmdlength-rc_cmdlength.o `test -f 'rc_cmdlength.c' || echo '$(srcdir)/'`rc_cmdlength.c
@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/test_rc_cmdlength-rc_cmdlength.Tpo $(DEPDIR)/test_rc_cmdlength-rc_cmdlength.Po