Linux Device Mapper development
 help / color / mirror / Atom feed
From: Martin Wilck <mwilck@suse.com>
To: Christophe Varoqui <christophe.varoqui@opensvc.com>
Cc: Martin Wilck <mwilck@suse.com>, dm-devel@redhat.com
Subject: [PATCH v3 02/22] libmultipath: fix memory leaks from scandir() use
Date: Tue, 30 Oct 2018 22:06:33 +0100	[thread overview]
Message-ID: <20181030210653.29677-3-mwilck@suse.com> (raw)
In-Reply-To: <20181030210653.29677-1-mwilck@suse.com>

scandir() users must not only free the resulting dirent* array,
but also every member. Add a cleanup function, and fix the
existing users of scandir() in libmultipath.

Add a small helper macro for casting function pointers to the
type pthread_cleanup_push() expects.

Reviewed-by: Benjamin Marzinski <bmarzins@redhat.com>
Signed-off-by: Martin Wilck <mwilck@suse.com>
---
 libmultipath/config.c       | 10 ++++------
 libmultipath/foreign.c      |  5 ++++-
 libmultipath/foreign/nvme.c |  6 +++++-
 libmultipath/sysfs.c        |  5 ++++-
 libmultipath/util.c         |  9 +++++++++
 libmultipath/util.h         |  9 +++++++++
 6 files changed, 35 insertions(+), 9 deletions(-)

diff --git a/libmultipath/config.c b/libmultipath/config.c
index 0aef186a..5af7af58 100644
--- a/libmultipath/config.c
+++ b/libmultipath/config.c
@@ -639,17 +639,13 @@ free_config (struct config * conf)
 	FREE(conf);
 }
 
-static void free_namelist(void *nl)
-{
-	free(nl);
-}
-
 /* if multipath fails to process the config directory, it should continue,
  * with just a warning message */
 static void
 process_config_dir(struct config *conf, vector keywords, char *dir)
 {
 	struct dirent **namelist;
+	struct scandir_result sr;
 	int i, n;
 	char path[LINE_MAX];
 	int old_hwtable_size;
@@ -669,7 +665,9 @@ process_config_dir(struct config *conf, vector keywords, char *dir)
 		return;
 	} else if (n == 0)
 		return;
-	pthread_cleanup_push(free_namelist, namelist);
+	sr.di = namelist;
+	sr.n = n;
+	pthread_cleanup_push_cast(free_scandir_result, &sr);
 	for (i = 0; i < n; i++) {
 		if (!strstr(namelist[i]->d_name, ".conf"))
 			continue;
diff --git a/libmultipath/foreign.c b/libmultipath/foreign.c
index 80b399ba..48e8d247 100644
--- a/libmultipath/foreign.c
+++ b/libmultipath/foreign.c
@@ -115,6 +115,7 @@ static int _init_foreign(const char *multipath_dir)
 {
 	char pathbuf[PATH_MAX];
 	struct dirent **di;
+	struct scandir_result sr;
 	int r, i;
 
 	foreigns = vector_alloc();
@@ -135,7 +136,9 @@ static int _init_foreign(const char *multipath_dir)
 		return -r;
 	}
 
-	pthread_cleanup_push(free, di);
+	sr.di = di;
+	sr.n = r;
+	pthread_cleanup_push_cast(free_scandir_result, &sr);
 	for (i = 0; i < r; i++) {
 		const char *msg, *fn, *c;
 		struct foreign *fgn;
diff --git a/libmultipath/foreign/nvme.c b/libmultipath/foreign/nvme.c
index 8887a755..c753a747 100644
--- a/libmultipath/foreign/nvme.c
+++ b/libmultipath/foreign/nvme.c
@@ -27,6 +27,7 @@
 #include <dirent.h>
 #include <errno.h>
 #include <ctype.h>
+#include "util.h"
 #include "vector.h"
 #include "generic.h"
 #include "foreign.h"
@@ -534,6 +535,7 @@ static void _find_controllers(struct context *ctx, struct nvme_map *map)
 {
 	char pathbuf[PATH_MAX], realbuf[PATH_MAX];
 	struct dirent **di = NULL;
+	struct scandir_result sr;
 	struct udev_device *subsys;
 	struct nvme_path *path;
 	int r, i, n;
@@ -568,7 +570,9 @@ static void _find_controllers(struct context *ctx, struct nvme_map *map)
 		return;
 	}
 
-	pthread_cleanup_push(free, di);
+	sr.di = di;
+	sr.n = r;
+	pthread_cleanup_push_cast(free_scandir_result, &sr);
 	for (i = 0; i < r; i++) {
 		char *fn = di[i]->d_name;
 		struct udev_device *ctrl, *udev;
diff --git a/libmultipath/sysfs.c b/libmultipath/sysfs.c
index b7dacaad..558c8d6a 100644
--- a/libmultipath/sysfs.c
+++ b/libmultipath/sysfs.c
@@ -303,6 +303,7 @@ static void close_fd(void *arg)
 bool sysfs_is_multipathed(const struct path *pp)
 {
 	char pathbuf[PATH_MAX];
+	struct scandir_result sr;
 	struct dirent **di;
 	int n, r, i;
 	bool found = false;
@@ -323,7 +324,9 @@ bool sysfs_is_multipathed(const struct path *pp)
 		return false;
 	}
 
-	pthread_cleanup_push(free, di);
+	sr.di = di;
+	sr.n = r;
+	pthread_cleanup_push_cast(free_scandir_result, &sr);
 	for (i = 0; i < r && !found; i++) {
 		long fd;
 		int nr;
diff --git a/libmultipath/util.c b/libmultipath/util.c
index d08112db..66c47611 100644
--- a/libmultipath/util.c
+++ b/libmultipath/util.c
@@ -496,3 +496,12 @@ void set_max_fds(int max_fds)
 		}
 	}
 }
+
+void free_scandir_result(struct scandir_result *res)
+{
+	int i;
+
+	for (i = 0; i < res->n; i++)
+		FREE(res->di[i]);
+	FREE(res->di);
+}
diff --git a/libmultipath/util.h b/libmultipath/util.h
index c2462950..a818e29a 100644
--- a/libmultipath/util.h
+++ b/libmultipath/util.h
@@ -30,4 +30,13 @@ void set_max_fds(int max_fds);
 #define safe_snprintf(var, size, format, args...)      \
 	snprintf(var, size, format, ##args) >= size
 
+#define pthread_cleanup_push_cast(f, arg)		\
+	pthread_cleanup_push(((void (*)(void *))&f), (arg))
+
+struct scandir_result {
+	struct dirent **di;
+	int n;
+};
+void free_scandir_result(struct scandir_result *);
+
 #endif /* _UTIL_H */
-- 
2.19.1

  parent reply	other threads:[~2018-10-30 21:06 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-10-30 21:06 [PATCH v3 00/22] libmultipath: checkers overhaul Martin Wilck
2018-10-30 21:06 ` [PATCH v3 01/22] libmultipath: fix use of uninitialized memory in write() Martin Wilck
2018-10-30 21:06 ` Martin Wilck [this message]
2018-10-30 21:06 ` [PATCH v3 03/22] libmultipath/checkers: replace message by msgid Martin Wilck
2018-10-30 21:06 ` [PATCH v3 04/22] libmultipath/checkers: cciss_tur: use message id Martin Wilck
2018-10-30 21:06 ` [PATCH v3 05/22] libmultipath/checkers: directio: " Martin Wilck
2018-10-30 21:06 ` [PATCH v3 06/22] libmultipath/checkers: emc_clariion: " Martin Wilck
2018-10-30 21:06 ` [PATCH v3 07/22] libmultipath/checkers: hp_sw: " Martin Wilck
2018-10-30 21:06 ` [PATCH v3 08/22] libmultipath/checkers: rdac: " Martin Wilck
2018-10-30 21:06 ` [PATCH v3 09/22] libmultipath/checkers: readsector0: " Martin Wilck
2018-10-30 21:06 ` [PATCH v3 10/22] libmultipath/checkers: tur: " Martin Wilck
2018-10-30 21:06 ` [PATCH v3 11/22] multipathd: improve checker message logging Martin Wilck
2018-10-30 21:06 ` [PATCH v3 12/22] libmultipath/checkers: support unsupported paths Martin Wilck
2018-10-30 21:06 ` [PATCH v3 13/22] libmultipath: clariion checker: leave unsupported paths alone Martin Wilck
2018-11-01 19:49   ` Benjamin Marzinski
2018-10-30 21:06 ` [PATCH v3 14/22] libmultipath: hp_sw " Martin Wilck
2018-10-30 21:06 ` [PATCH v3 15/22] libmultipath: rdac " Martin Wilck
2018-10-30 21:06 ` [PATCH v3 16/22] libmultipath: tur " Martin Wilck
2018-10-30 21:06 ` [PATCH v3 17/22] libmultipath: pathinfo: don't blank wwid if checker fails Martin Wilck
2018-10-30 21:06 ` [PATCH v3 18/22] multipathd: check_path: improve logging for "unusable path" case Martin Wilck
2018-10-30 21:06 ` [PATCH v3 19/22] libmultipath: coalesce_paths: improve logging of orphaned paths Martin Wilck
2018-10-30 21:06 ` [PATCH v3 20/22] libmultipath: sync_map_state: log failing paths Martin Wilck
2018-10-30 21:06 ` [PATCH v3 21/22] libmultipath/checkers: cleanup class/instance model Martin Wilck
2018-10-30 21:06 ` [PATCH v3 22/22] libmultipath: make checker_message thread safe Martin Wilck
2018-11-01 19:53   ` Benjamin Marzinski
2018-11-02  8:50     ` Martin Wilck
2018-10-30 21:10 ` [PATCH v3 00/22] libmultipath: checkers overhaul Martin Wilck
2018-10-30 21:12 ` Martin Wilck

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20181030210653.29677-3-mwilck@suse.com \
    --to=mwilck@suse.com \
    --cc=christophe.varoqui@opensvc.com \
    --cc=dm-devel@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox