Skip to contentSkip to conversation

multipath: fix memory leak and segfault in reconfigure

PATCH In review Germano Percossi 2 participants 4 messages Feb 11, 2016 – Apr 18, 2016 Device mapper

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 multipath: fix memory leak and segfault in reconfigure

Within the reconfigure function, the global pointer conf is stored in a local variable and then assigned NULL. If load_config should fail, for any reason, we end up with a memory leak, as soon as we leave the function, and with the global pointer conf set to NULL, leading to a segfault as soon as it is dereferenced.

I tested it by calling a reconfigure and making the first allocation in load_config fail but any failure in load_config would do.
From a user perspective the CLI reports "fail".

If something like this should happen there are at least 2 possible scenarios:

1) If a second immediate reconfigure succeeds, the conf now is fine but the leak stays
2) If the previous point does not happen, any command trying to access "conf" would fail. On my test box a "show conf" segfaulted.

The fix is simple but in case of failure at least the previous conf is kept in memory without leaks or segfaluts

---
 multipathd/main.c | 2 ++
 1 file changed, 2 insertions(+)
multipathd/main.c +2 -0
index 04f6d02..f83c849 100644
@@ -1551,6 +1551,8 @@ reconfigure (struct vectors * vecs)
 		configure(vecs, 1);
 		free_config(old);
 		retval = 0;
+	} else {
+		conf = old;
 	}
 
 	running_state = DAEMON_RUNNING;
Signature · 1 line
1.9.1
#1 lore raw

Ping

Quoted 42 lines · Germano Percossi

Within the reconfigure function, the global pointer conf is stored in a local variable and then assigned NULL. If load_config should fail, for any reason, we end up with a memory leak, as soon as we leave the function, and with the global pointer conf set to NULL, leading to a segfault as soon as it is dereferenced.

I tested it by calling a reconfigure and making the first allocation in load_config fail but any failure in load_config would do.

Quoted 1 line

From a user perspective the CLI reports "fail".

If something like this should happen there are at least 2 possible scenarios:

1) If a second immediate reconfigure succeeds, the conf now is fine but the leak stays
2) If the previous point does not happen, any command trying to access "conf" would fail. On my test box a "show conf" segfaulted.

The fix is simple but in case of failure at least the previous conf is kept in memory without leaks or segfaluts

---
  multipathd/main.c | 2 ++
  1 file changed, 2 insertions(+)
multipathd/main.c +2 -0
index 04f6d02..f83c849 100644
@@ -1551,6 +1551,8 @@ reconfigure (struct vectors * vecs)
  		configure(vecs, 1);
  		free_config(old);
  		retval = 0;
+	} else {
+		conf = old;
  	}
 
  	running_state = DAEMON_RUNNING;
#2 lore raw
In reply to Germano Percossi

Hi Christophe,

Is there a specific reason this silly patch is not getting through?

It seems a no-brainer to me but probably I am missing something.

Thanks,
Germano

Quoted 42 lines · Germano Percossi

Within the reconfigure function, the global pointer conf is stored in a local variable and then assigned NULL. If load_config should fail, for any reason, we end up with a memory leak, as soon as we leave the function, and with the global pointer conf set to NULL, leading to a segfault as soon as it is dereferenced.

I tested it by calling a reconfigure and making the first allocation in load_config fail but any failure in load_config would do.

Quoted 1 line

From a user perspective the CLI reports "fail".

If something like this should happen there are at least 2 possible scenarios:

1) If a second immediate reconfigure succeeds, the conf now is fine but the leak stays
2) If the previous point does not happen, any command trying to access "conf" would fail. On my test box a "show conf" segfaulted.

The fix is simple but in case of failure at least the previous conf is kept in memory without leaks or segfaluts

---
  multipathd/main.c | 2 ++
  1 file changed, 2 insertions(+)
multipathd/main.c +2 -0
index 04f6d02..f83c849 100644
@@ -1551,6 +1551,8 @@ reconfigure (struct vectors * vecs)
  		configure(vecs, 1);
  		free_config(old);
  		retval = 0;
+	} else {
+		conf = old;
  	}
 
  	running_state = DAEMON_RUNNING;
#3 lore raw

Hi Germano,

no reason at all ... it just missed my attention, twice. Sorry.

This patch is now merged.
Thanks,
Christophe Varoqui
www.opensvc.com

Quoted 57 lines · Germano Percossi

Hi Christophe,

Is there a specific reason this silly patch is not getting through?

It seems a no-brainer to me but probably I am missing something.

Thanks,
Germano

Quoted 45 lines · Germano Percossi

Within the reconfigure function, the global pointer conf is stored in a local variable and then assigned NULL. If load_config should fail, for any reason, we end up with a memory leak, as soon as we leave the function, and with the global pointer conf set to NULL, leading to a segfault as soon as it is dereferenced.

I tested it by calling a reconfigure and making the first allocation in load_config fail but any failure in load_config would do.

Quoted 2 lines

From a user perspective the CLI reports "fail".

If something like this should happen there are at least 2 possible scenarios:

1) If a second immediate reconfigure succeeds, the conf now is fine but the leak stays
2) If the previous point does not happen, any command trying to access "conf" would fail. On my test box a "show conf" segfaulted.

The fix is simple but in case of failure at least the previous conf is kept in memory without leaks or segfaluts

---
  multipathd/main.c | 2 ++
  1 file changed, 2 insertions(+)
multipathd/main.c +2 -0
index 04f6d02..f83c849 100644
@@ -1551,6 +1551,8 @@ reconfigure (struct vectors * vecs)
                configure(vecs, 1);
                free_config(old);
                retval = 0;
+       } else {
+               conf = old;
        }
 
        running_state = DAEMON_RUNNING;