From patchwork Fri Nov 2 12:21:06 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Wilck X-Patchwork-Id: 10665461 X-Patchwork-Delegate: christophe.varoqui@free.fr Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 5F56815E9 for ; Fri, 2 Nov 2018 12:47:25 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 465952B462 for ; Fri, 2 Nov 2018 12:47:25 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 35ACF2B47D; Fri, 2 Nov 2018 12:47:25 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id A00722B462 for ; Fri, 2 Nov 2018 12:47:24 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 389903082E4C; Fri, 2 Nov 2018 12:47:23 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id B15F61A7EA; Fri, 2 Nov 2018 12:47:22 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id BFADF3D384; Fri, 2 Nov 2018 12:47:21 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id wA2CNJNB011611 for ; Fri, 2 Nov 2018 08:23:19 -0400 Received: by smtp.corp.redhat.com (Postfix) id B48515D76A; Fri, 2 Nov 2018 12:23:19 +0000 (UTC) Delivered-To: dm-devel@redhat.com Received: from mx1.redhat.com (ext-mx04.extmail.prod.ext.phx2.redhat.com [10.5.110.28]) by smtp.corp.redhat.com (Postfix) with ESMTPS id A5AEE5D6A6; Fri, 2 Nov 2018 12:23:17 +0000 (UTC) Received: from smtp2.provo.novell.com (smtp2.provo.novell.com [137.65.250.81]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 8653E88311; Fri, 2 Nov 2018 12:23:16 +0000 (UTC) Received: from apollon.suse.de.de (prv-ext-foundry1int.gns.novell.com [137.65.251.240]) by smtp2.provo.novell.com with ESMTP (TLS encrypted); Fri, 02 Nov 2018 06:23:05 -0600 From: Martin Wilck To: Christophe Varoqui , Benjamin Marzinski Date: Fri, 2 Nov 2018 13:21:06 +0100 Message-Id: <20181102122125.30906-3-mwilck@suse.com> In-Reply-To: <20181102122125.30906-1-mwilck@suse.com> References: <20181102122125.30906-1-mwilck@suse.com> MIME-Version: 1.0 X-Greylist: Sender passed SPF test, Sender IP whitelisted by DNSRBL, ACL 216 matched, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Fri, 02 Nov 2018 12:23:16 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Fri, 02 Nov 2018 12:23:16 +0000 (UTC) for IP:'137.65.250.81' DOMAIN:'smtp2.provo.novell.com' HELO:'smtp2.provo.novell.com' FROM:'mwilck@suse.com' RCPT:'' X-RedHat-Spam-Score: -2.301 (RCVD_IN_DNSWL_MED, SPF_PASS) 137.65.250.81 smtp2.provo.novell.com 137.65.250.81 smtp2.provo.novell.com X-Scanned-By: MIMEDefang 2.78 on 10.5.110.28 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-loop: dm-devel@redhat.com Cc: dm-devel@redhat.com, Martin Wilck Subject: [dm-devel] [PATCH v5 02/21] libmultipath: fix memory leaks from scandir() use X-BeenThere: dm-devel@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: device-mapper development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: dm-devel-bounces@redhat.com Errors-To: dm-devel-bounces@redhat.com X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.46]); Fri, 02 Nov 2018 12:47:24 +0000 (UTC) X-Virus-Scanned: ClamAV using ClamSMTP 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 Signed-off-by: Martin Wilck --- 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 #include #include +#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 */