[PATCH] xodtemplate: Fix obscure crash while loading configuration

robin.sonefors at op5.com robin.sonefors at op5.com
Thu Nov 8 11:37:02 CET 2012


From: Robin Sonefors <robin.sonefors at op5.com>

We didn't initialize parent_map, meaning that it could have any value,
even though it appears it is not infrequently NULL, at least at first.

We then pointed it to a bitmap object iff
xodtemplate_servicedependency_list wasn't NULL, and finally freed it
unconditionally.

Thus, unless parent_map pointed at NULL or we have servicedependencies,
we'd get a crash.

Now, if we first add at least one servicedependency, run nagios, remove
all of them, and reload nagios, parent_map would still keep its old
value around, we wouldn't reallocate it, but we'd still free it, leading
to a crash.

This is prevented in the following ways:
 * First, make sure all bitmaps are NULL-initialized, clearly and
   obviously.
 * Second, only free parent_map if we did set it.
 * Third, set parent_map to NULL after we destroy() it - dangling
   pointers are evil.

Signed-off-by: Robin Sonefors <robin.sonefors at op5.com>
---
 xdata/xodtemplate.c | 21 +++++++++++----------
 1 file changed, 11 insertions(+), 10 deletions(-)

diff --git a/xdata/xodtemplate.c b/xdata/xodtemplate.c
index 5b3a099..34da566 100644
--- a/xdata/xodtemplate.c
+++ b/xdata/xodtemplate.c
@@ -118,9 +118,9 @@ static struct object_count xodcount;
 
 #ifndef NSCGI
 /* reusable bitmaps for expanding objects */
-static bitmap *host_map, *contact_map;
+static bitmap *host_map = NULL, *contact_map = NULL;
 #endif
-static bitmap *service_map, *parent_map;
+static bitmap *service_map = NULL, *parent_map = NULL;
 
 
 /*
@@ -6901,18 +6901,19 @@ int xodtemplate_register_objects(void) {
 			logit(NSLOG_CONFIG_ERROR, TRUE, "Error: Failed to create parent bitmap for service dependencies\n");
 			return ERROR;
 			}
-		}
-	for(sd = xodtemplate_servicedependency_list; sd; sd = next_sd) {
-		next_sd = sd->next;
+		for(sd = xodtemplate_servicedependency_list; sd; sd = next_sd) {
+			next_sd = sd->next;
 #ifdef NSCGI
-		if(xodtemplate_register_servicedependency(sd) == ERROR)
-			return ERROR;
+			if(xodtemplate_register_servicedependency(sd) == ERROR)
+				return ERROR;
 #else
-		if(xodtemplate_register_and_destroy_servicedependency(sd) == ERROR)
-			return ERROR;
+			if(xodtemplate_register_and_destroy_servicedependency(sd) == ERROR)
+				return ERROR;
 #endif
+			}
+		bitmap_destroy(parent_map);
+		parent_map = NULL;
 		}
-	bitmap_destroy(parent_map);
 	timing_point("%u unique / %u total servicedependencies registered\n",
 				 num_objects.servicedependencies, xodcount.servicedependencies);
 
-- 
1.7.11.7


------------------------------------------------------------------------------
Everyone hates slow websites. So do we.
Make your web apps faster with AppDynamics
Download AppDynamics Lite for free today:
http://p.sf.net/sfu/appdyn_d2d_nov




More information about the Developers mailing list