From a4a1b37be0ff96492d13e87ce59c82482d961f56 Mon Sep 17 00:00:00 2001 From: Sebastian Herbszt Date: Wed, 26 Nov 2014 23:54:49 +0100 Subject: check_ntp: fix null termination Fix null termination introduced by commit a04df3e ("plugins/check_ntp.c - Verify struct from response"). Signed-off-by: Sebastian Herbszt --- plugins/check_ntp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'plugins/check_ntp.c') diff --git a/plugins/check_ntp.c b/plugins/check_ntp.c index 09a923eb..546802a3 100644 --- a/plugins/check_ntp.c +++ b/plugins/check_ntp.c @@ -616,7 +616,7 @@ double jitter_request(const char *host, int *status){ if (bytes_read != ntp_cm_ints + req.count) die(STATE_UNKNOWN, _("Invalid NTP response: %d bytes read does not equal %d plus %d data segment"), bytes_read, ntp_cm_ints, req.count); /* else null terminate */ - strncpy(req.data[req.count], "\0", 1); + req.data[req.count] = '\0'; DBG(print_ntp_control_message(&req)); -- cgit v1.2.3-74-g34f1 From 5871123e0a5f520f810b2cfe03cef16c4c5a1aee Mon Sep 17 00:00:00 2001 From: Holger Weiss Date: Sun, 30 Nov 2014 23:39:59 +0100 Subject: Revert "plugins/check_ntp.c - Verify struct from response" This reverts commit a04df3e1b67dc5eab3adc202cc89901f801cdeaa. The "fix" was bogus in many ways and broke jitter checking. Conflicts: plugins/check_ntp.c --- plugins/check_ntp.c | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) (limited to 'plugins/check_ntp.c') diff --git a/plugins/check_ntp.c b/plugins/check_ntp.c index 546802a3..0a7640a7 100644 --- a/plugins/check_ntp.c +++ b/plugins/check_ntp.c @@ -517,14 +517,13 @@ setup_control_request(ntp_control_message *p, uint8_t opcode, uint16_t seq){ double jitter_request(const char *host, int *status){ int conn=-1, i, npeers=0, num_candidates=0, syncsource_found=0; int run=0, min_peer_sel=PEER_INCLUDED, num_selected=0, num_valid=0; - int peers_size=0, peer_offset=0, bytes_read=0; + int peers_size=0, peer_offset=0; ntp_assoc_status_pair *peers=NULL; ntp_control_message req; const char *getvar = "jitter"; double rval = 0.0, jitter = -1.0; char *startofvalue=NULL, *nptr=NULL; void *tmp; - int ntp_cm_ints = sizeof(uint16_t) * 5 + sizeof(uint8_t) * 2; /* Long-winded explanation: * Getting the jitter requires a number of steps: @@ -609,15 +608,7 @@ double jitter_request(const char *host, int *status){ req.count = htons(MAX_CM_SIZE); DBG(printf("recieving READVAR response...\n")); - - /* cov-66524 - req.data not null terminated before usage. Also covers verifying struct was returned correctly*/ - if ((bytes_read = read(conn, &req, SIZEOF_NTPCM(req))) == -1) - die(STATE_UNKNOWN, _("Cannot read from socket: %s"), strerror(errno)); - if (bytes_read != ntp_cm_ints + req.count) - die(STATE_UNKNOWN, _("Invalid NTP response: %d bytes read does not equal %d plus %d data segment"), bytes_read, ntp_cm_ints, req.count); - /* else null terminate */ - req.data[req.count] = '\0'; - + read(conn, &req, SIZEOF_NTPCM(req)); DBG(print_ntp_control_message(&req)); if(req.op&REM_ERROR && strstr(getvar, "jitter")) { -- cgit v1.2.3-74-g34f1 From 99b3bfe488a856df059e933c796590eea0baae8d Mon Sep 17 00:00:00 2001 From: Holger Weiss Date: Mon, 1 Dec 2014 01:07:53 +0100 Subject: check_ntp: Nul-terminate jitter data Make sure the jitter response is nul-terminated before parsing the data using string functions. --- plugins/check_ntp.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'plugins/check_ntp.c') diff --git a/plugins/check_ntp.c b/plugins/check_ntp.c index 0a7640a7..a7d278de 100644 --- a/plugins/check_ntp.c +++ b/plugins/check_ntp.c @@ -590,6 +590,9 @@ double jitter_request(const char *host, int *status){ for (i = 0; i < npeers; i++){ /* Only query this server if it is the current sync source */ if (PEER_SEL(peers[i].status) >= min_peer_sel){ + char jitter_data[MAX_CM_SIZE+1]; + size_t jitter_data_count; + num_selected++; setup_control_request(&req, OP_READVAR, 2); req.assoc = peers[i].assoc; @@ -623,7 +626,14 @@ double jitter_request(const char *host, int *status){ if(verbose) { printf("parsing jitter from peer %.2x: ", ntohs(peers[i].assoc)); } - startofvalue = strchr(req.data, '='); + if((jitter_data_count = ntohs(req.count)) >= sizeof(jitter_data)){ + die(STATE_UNKNOWN, + _("jitter response too large (%lu bytes)\n"), + (unsigned long)jitter_data_count); + } + memcpy(jitter_data, req.data, jitter_data_count); + jitter_data[jitter_data_count] = '\0'; + startofvalue = strchr(jitter_data, '='); if(startofvalue != NULL) { startofvalue++; jitter = strtod(startofvalue, &nptr); -- cgit v1.2.3-74-g34f1