Skip to contentSkip to conversation

NULL-deref after failed malloc (STABLE3)

PATCH In review Jim Meyering 1 participant 7 messages Jun 23, 2009 – Jun 25, 2009

lore
Share & tools
lore
#0 lore raw

cover NULL-deref after failed malloc (STABLE3)

From: Jim Meyering <[email protected]>

I audited cluster's STABLE3 branch for NULL-deref after heap allocation failures. I took the time to fix a few and left FIXME comments marking the others.

#1 lore raw

PATCH 1/5 src/clulib/ckpt_state.c (ds_key_init_nt): detect failed malloc

From: Jim Meyering <[email protected]>

---
 rgmanager/src/clulib/ckpt_state.c |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
rgmanager/src/clulib/ckpt_state.c +1 -0
index fd85aee..766097b 100644
@@ -70,6 +70,7 @@ ds_key_init_nt(char *keyid, int maxsize, int timeout)
 	}
 
 	newnode = malloc(sizeof(*newnode));
+	// FIXME: detect failed malloc
 	memset(newnode,0,sizeof(*newnode));
 	snprintf((char *)newnode->kn_cpname.value, SA_MAX_NAME_LENGTH-1,
 		 "%s", keyid);
Signature · 1 line
1.6.3.3.420.gd4b46
#2 lore raw

PATCH 2/5 dlm_controld: handle heap allocation failure and plug leaks

In reply to Jim Meyering

From: Jim Meyering <[email protected]>

* group/dlm_controld/pacemaker.c (process_cluster): Don't dereference NULL upon failing malloc or realloc. Free "header" upon failure.

---
 group/dlm_controld/pacemaker.c |   16 +++++++++++-----
 1 files changed, 11 insertions(+), 5 deletions(-)
group/dlm_controld/pacemaker.c +11 -5
index fed9ca7..b9b38d0 100644
@@ -135,10 +135,12 @@ void process_cluster(int ci)
 
     AIS_Message *msg = NULL;
     SaAisErrorT rc = SA_AIS_OK;
-    mar_res_header_t *header = NULL;
+    mar_res_header_t *header;
+    mar_res_header_t *h;
     static int header_len = sizeof(mar_res_header_t);
 
-    header = malloc(header_len);
+    if ((header = malloc(header_len)) == NULL)
+	goto bail;
     memset(header, 0, header_len);
     
     errno = 0;
@@ -160,8 +162,12 @@ void process_cluster(int ci)
     } else if(header->error != 0) {
 	log_error("Header contined error: %d", header->error);
     }
-    
-    header = realloc(header, header->size);
+
+    h_new = realloc(header, header->size);
+    if (h_new == NULL)
+	goto bail;
+    header = h_new;
+
     /* Use a char* so we can store the remainder into an offset */
     data = (char*)header;
 
@@ -250,6 +256,7 @@ void process_cluster(int ci)
     goto done;
     
   bail:
+    free (header);
     log_error("AIS connection failed");
     return;
 }
@@ -406,4 +413,3 @@ int fence_in_progress(int *count)
 {
 	return 0;
 }
-
Signature · 1 line
1.6.3.3.420.gd4b46
#3 lore raw

[Cluster-devel] Re: [PATCH cluster 2/5] dlm_controld: handle heap allocation failure and plug leaks

Quoted 13 lines · Jim Meyering

From: Jim Meyering <[email protected]>

* group/dlm_controld/pacemaker.c (process_cluster): Don't dereference NULL upon failing malloc or realloc. Free "header" upon failure.

---
 group/dlm_controld/pacemaker.c |   16 +++++++++++-----
 1 files changed, 11 insertions(+), 5 deletions(-)
group/dlm_controld/pacemaker.c +0 -0
index fed9ca7..b9b38d0 100644

group/dlm_controld/pacemaker.c void process_cluster(int ci) · line 139

Quoted 6 lines
group/dlm_controld/pacemaker.c +2 -1
@@ -135,10 +135,12 @@ void process_cluster(int ci)
 
     AIS_Message *msg = NULL;
     SaAisErrorT rc = SA_AIS_OK;
-    mar_res_header_t *header = NULL;
+    mar_res_header_t *header;
+    mar_res_header_t *h;

Oops. That "h" have been "h_new".
(Andrew Beekhof noticed this)
...

group/dlm_controld/pacemaker.c void process_cluster(int ci) · line 140

Quoted 1 line
group/dlm_controld/pacemaker.c +1 -0
+    h_new = realloc(header, header->size);

Here's the corrected patch:

From bbc3ffcaf72d8089a1783af8ec9dc5b726a2b68e Mon Sep 17 00:00:00 2001
From: Jim Meyering <[email protected]>
Date: Mon, 22 Jun 2009 23:37:21 +0200
Subject: [PATCH cluster] dlm_controld: handle heap allocation failure and plug leaks

* group/dlm_controld/pacemaker.c (process_cluster): Don't dereference NULL upon failing malloc or realloc. Free "header" upon failure.

---
 group/dlm_controld/pacemaker.c |   16 +++++++++++-----
 1 files changed, 11 insertions(+), 5 deletions(-)
group/dlm_controld/pacemaker.c +11 -5
index 509696a..9b0a2d8 100644
@@ -135,10 +135,12 @@ void process_cluster(int ci)
 
     AIS_Message *msg = NULL;
     SaAisErrorT rc = SA_AIS_OK;
-    mar_res_header_t *header = NULL;
+    mar_res_header_t *header;
+    mar_res_header_t *h_new;
     static int header_len = sizeof(mar_res_header_t);
 
-    header = malloc(header_len);
+    if ((header = malloc(header_len)) == NULL)
+	goto bail;
     memset(header, 0, header_len);
     
     errno = 0;
@@ -160,8 +162,12 @@ void process_cluster(int ci)
     } else if(header->error != 0) {
 	log_error("Header contined error: %d", header->error);
     }
-    
-    header = realloc(header, header->size);
+
+    h_new = realloc(header, header->size);
+    if (h_new == NULL)
+	goto bail;
+    header = h_new;
+
     /* Use a char* so we can store the remainder into an offset */
     data = (char*)header;
 
@@ -252,6 +258,7 @@ void process_cluster(int ci)
     goto done;
     
   bail:
+    free (header);
     log_error("AIS connection failed");
     return;
 }
@@ -408,4 +415,3 @@ int fence_in_progress(int *count)
 {
 	return 0;
 }
-
Signature · 1 line
1.6.3.3.420.gd4b46
#4 lore raw

PATCH 3/5 dlm_controld: add comments: mark memory problems

In reply to Jim Meyering

From: Jim Meyering <[email protected]>

* group/dlm_controld/pacemaker.c (process_cluster): Mark additional memory problems.

---
 group/dlm_controld/pacemaker.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
group/dlm_controld/pacemaker.c +2 -0
index b9b38d0..b06e73d 100644
@@ -191,6 +191,7 @@ void process_cluster(int ci)
 
 	log_debug("Decompressing message data");
 	uncompressed = malloc(new_size);
+	// FIXME: handle malloc failure
 	memset(uncompressed, 0, new_size);
 	
 	rc = BZ2_bzBuffToBuffDecompress(
@@ -212,6 +213,7 @@ void process_cluster(int ci)
     } else if(safe_str_eq("identify", data)) {
 	int pid = getpid();
 	char *pid_s = crm_itoa(pid);
+	// FIXME: can crm_itoa fail?  if so, does it return NULL?
 	send_ais_text(0, pid_s, TRUE, NULL, crm_msg_ais);
 	crm_free(pid_s);
 	goto done;
Signature · 1 line
1.6.3.3.420.gd4b46
#5 lore raw

PATCH 4/5 dlm/tests: handle malloc failure

In reply to Jim Meyering

From: Jim Meyering <[email protected]>

* dlm/tests/usertest/flood.c (main): Handle malloc failure.

---
 dlm/tests/usertest/flood.c |    7 +++++--
 1 files changed, 5 insertions(+), 2 deletions(-)
dlm/tests/usertest/flood.c +5 -2
index efe3a4b..c01bc0d 100644
@@ -112,7 +112,11 @@ int main(int argc, char *argv[])
 	}
     }
 
-    resources = malloc(sizeof(char*) * rescount);
+    if ((resources = malloc(sizeof(char*) * rescount)) == NULL)
+    {
+	    perror("exhausted virtual memory");
+	    return 1;
+    }
     for (i=0; i < rescount; i++) {
 	    char resname[256];
 	    sprintf(resname, "TESTLOCK%d", i);
@@ -164,4 +168,3 @@ int main(int argc, char *argv[])
 
     return 0;
 }
-
Signature · 1 line
1.6.3.3.420.gd4b46
#6 lore raw

PATCH 5/5 cman: handle malloc failure (i.e., don't deref NULL)

In reply to Jim Meyering

From: Jim Meyering <[email protected]>

* cman/daemon/commands.c (do_cmd_get_extrainfo):
Handle malloc failure (i.e., don't deref NULL).
(do_cmd_get_all_members): Likewise.

---
 cman/daemon/commands.c |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)
cman/daemon/commands.c +4 -0
index b343bcb..c360909 100644
@@ -538,6 +538,8 @@ static int do_cmd_get_extrainfo(char *cmdbuf, char **retbuf, int retsize, int *r
 		       sizeof(struct sockaddr_storage) * (MAX_INTERFACES*2))) {
 
 		*retbuf = malloc(sizeof(struct cl_extra_info) + sizeof(struct sockaddr_storage) * (MAX_INTERFACES*2));
+		if (*retbuf == NULL)
+			return -ENOMEM;
 		outbuf = *retbuf + offset;
 		einfo = (struct cl_extra_info *)outbuf;
 
@@ -635,6 +637,8 @@ static int do_cmd_get_all_members(char *cmdbuf, char **retbuf, int retsize, int
 		/* If there is not enough space in the default buffer, allocate some more. */
 		if ((retsize / sizeof(struct cl_cluster_node)) < total_nodes) {
 			*retbuf = malloc(sizeof(struct cl_cluster_node) * total_nodes + offset);
+			if (!*retbuf)
+				return -ENOMEM;
 			outbuf = *retbuf + offset;
 			log_printf(LOGSYS_LEVEL_DEBUG, "memb: get_all_members: allocated new buffer (retsize=%d)\n", retsize);
 		}
Signature · 1 line
1.6.3.3.420.gd4b46