[PATCH 2/3] Checks: Escape command line arguments correctly

Max Sikstrom max.sikstrom at op5.com
Wed May 22 11:50:32 CEST 2013


From: Max Sikström <msikstrom at op5.com>

Command line arguments should be stripped of a couple characters, so it isn't
possible to break out of for example strings, and start redirecting output and
write to files, given values of macros. And extra important is for macros that
can be updated from a user.

Previously, this wasn't that big issue, when macros itself could say that they
should be escaped. But the concept of escaping also depending which macro it
entered gives lot of other problems (notable for url:s at least, see previous
commit).

Only the requestor of the macro expansion knows where it's going to be used, so
only the requestor has the information to tell what to escape, and therefore,
this patch tells the macro expansion to escape the macros to be used as a
command line for check commands...

Signed-off-by: Max Sikström <msikstrom at op5.com>
---
 base/checks.c |   15 +++++++++------
 1 files changed, 9 insertions(+), 6 deletions(-)

diff --git a/base/checks.c b/base/checks.c
index 0f99f82..c9c4851 100644
--- a/base/checks.c
+++ b/base/checks.c
@@ -155,6 +155,7 @@ int run_async_service_check(service *svc, int check_options, double latency, int
 	double old_latency = 0.0;
 	check_result *cr;
 	int runchk_result = OK;
+	int macro_options = STRIP_ILLEGAL_MACRO_CHARS | ESCAPE_MACRO_CHARS;
 #ifdef USE_EVENT_BROKER
 	int neb_result = OK;
 #endif
@@ -222,7 +223,7 @@ int run_async_service_check(service *svc, int check_options, double latency, int
 	grab_service_macros_r(&mac, svc);
 
 	/* get the raw command line */
-	get_raw_command_line_r(&mac, svc->check_command_ptr, svc->check_command, &raw_command, 0);
+	get_raw_command_line_r(&mac, svc->check_command_ptr, svc->check_command, &raw_command, macro_options);
 	if(raw_command == NULL) {
 		clear_volatile_macros_r(&mac);
 		log_debug_info(DEBUGL_CHECKS, 0, "Raw check command for service '%s' on host '%s' was NULL - aborting.\n", svc->description, svc->host_name);
@@ -233,7 +234,7 @@ int run_async_service_check(service *svc, int check_options, double latency, int
 		}
 
 	/* process any macros contained in the argument */
-	process_macros_r(&mac, raw_command, &processed_command, 0);
+	process_macros_r(&mac, raw_command, &processed_command, macro_options);
 	my_free(raw_command);
 	if(processed_command == NULL) {
 		clear_volatile_macros_r(&mac);
@@ -2060,6 +2061,7 @@ int execute_sync_host_check(host *hst) {
 	int early_timeout = FALSE;
 	double exectime;
 	char *temp_plugin_output = NULL;
+	int macro_options = STRIP_ILLEGAL_MACRO_CHARS | ESCAPE_MACRO_CHARS;
 #ifdef USE_EVENT_BROKER
 	int neb_result = OK;
 #endif
@@ -2109,14 +2111,14 @@ int execute_sync_host_check(host *hst) {
 	time(&hst->last_check);
 
 	/* get the raw command line */
-	get_raw_command_line_r(&mac, hst->check_command_ptr, hst->check_command, &raw_command, 0);
+	get_raw_command_line_r(&mac, hst->check_command_ptr, hst->check_command, &raw_command, macro_options);
 	if(raw_command == NULL) {
 		clear_volatile_macros_r(&mac);
 		return ERROR;
 		}
 
 	/* process any macros contained in the argument */
-	process_macros_r(&mac, raw_command, &processed_command, 0);
+	process_macros_r(&mac, raw_command, &processed_command, macro_options);
 	if(processed_command == NULL) {
 		my_free(raw_command);
 		clear_volatile_macros_r(&mac);
@@ -2302,6 +2304,7 @@ int run_async_host_check(host *hst, int check_options, double latency, int sched
 	double old_latency = 0.0;
 	check_result *cr;
 	int runchk_result = OK;
+	int macro_options = STRIP_ILLEGAL_MACRO_CHARS | ESCAPE_MACRO_CHARS;
 #ifdef USE_EVENT_BROKER
 	int neb_result = OK;
 #endif
@@ -2365,7 +2368,7 @@ int run_async_host_check(host *hst, int check_options, double latency, int sched
 	grab_host_macros_r(&mac, hst);
 
 	/* get the raw command line */
-	get_raw_command_line_r(&mac, hst->check_command_ptr, hst->check_command, &raw_command, 0);
+	get_raw_command_line_r(&mac, hst->check_command_ptr, hst->check_command, &raw_command, macro_options);
 	if(raw_command == NULL) {
 		clear_volatile_macros_r(&mac);
 		log_debug_info(DEBUGL_CHECKS, 0, "Raw check command for host '%s' was NULL - aborting.\n", hst->name);
@@ -2373,7 +2376,7 @@ int run_async_host_check(host *hst, int check_options, double latency, int sched
 		}
 
 	/* process any macros contained in the argument */
-	process_macros_r(&mac, raw_command, &processed_command, 0);
+	process_macros_r(&mac, raw_command, &processed_command, macro_options);
 	my_free(raw_command);
 	if(processed_command == NULL) {
 		clear_volatile_macros_r(&mac);
-- 
1.7.1


------------------------------------------------------------------------------
Try New Relic Now & We'll Send You this Cool Shirt
New Relic is the only SaaS-based application performance monitoring service 
that delivers powerful full stack analytics. Optimize and monitor your
browser, app, & servers with just a few lines of code. Try New Relic
and get this awesome Nerd Life shirt! http://p.sf.net/sfu/newrelic_d2d_may
_______________________________________________
Nagios-devel mailing list
Nagios-devel at lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/nagios-devel


More information about the Developers mailing list