All of lore.kernel.org
 help / color / mirror / Atom feed
From: mwilck@suse.com
To: Christophe Varoqui <christophe.varoqui@opensvc.com>,
	Benjamin Marzinski <bmarzins@redhat.com>
Cc: dm-devel@redhat.com, Martin Wilck <mwilck@suse.com>
Subject: [dm-devel] [PATCH 2/7] libmultipath: protect logarea with logq_lock
Date: Thu, 17 Dec 2020 12:00:13 +0100	[thread overview]
Message-ID: <20201217110018.3347-3-mwilck@suse.com> (raw)
In-Reply-To: <20201217110018.3347-1-mwilck@suse.com>

From: Martin Wilck <mwilck@suse.com>

Make sure the global logarea (la) is only allocated and freed
hile holding logq_lock. This avoids invalid memory access.

This patch makes free_logarea() static. libmultipath.version
is unchanged, as free_logarea() wasn't exported anyway.

Signed-off-by: Martin Wilck <mwilck@suse.com>
---
 libmultipath/log.c | 32 +++++++++++++++++++++++---------
 libmultipath/log.h |  1 -
 2 files changed, 23 insertions(+), 10 deletions(-)

diff --git a/libmultipath/log.c b/libmultipath/log.c
index 7f33787..95c8f01 100644
--- a/libmultipath/log.c
+++ b/libmultipath/log.c
@@ -77,16 +77,23 @@ static int logarea_init (int size)
 
 int log_init(char *program_name, int size)
 {
+	int ret = 1;
+
 	logdbg(stderr,"enter log_init\n");
+
+	pthread_mutex_lock(&logq_lock);
+	pthread_cleanup_push(cleanup_mutex, &logq_lock);
+
 	openlog(program_name, 0, LOG_DAEMON);
+	if (!la)
+		ret = logarea_init(size);
 
-	if (logarea_init(size))
-		return 1;
+	pthread_cleanup_pop(1);
 
-	return 0;
+	return ret;
 }
 
-void free_logarea (void)
+static void free_logarea (void)
 {
 	FREE(la->start);
 	FREE(la->buff);
@@ -96,9 +103,14 @@ void free_logarea (void)
 
 void log_close (void)
 {
-	free_logarea();
+	pthread_mutex_lock(&logq_lock);
+	pthread_cleanup_push(cleanup_mutex, &logq_lock);
+
+	if (la)
+		free_logarea();
 	closelog();
 
+	pthread_cleanup_pop(1);
 	return;
 }
 
@@ -175,11 +187,12 @@ static int _log_enqueue(int prio, const char * fmt, va_list ap)
 
 int log_enqueue(int prio, const char *fmt, va_list ap)
 {
-	int ret;
+	int ret = 1;
 
 	pthread_mutex_lock(&logq_lock);
 	pthread_cleanup_push(cleanup_mutex, &logq_lock);
-	ret = _log_enqueue(prio, fmt, ap);
+	if (la)
+		ret = _log_enqueue(prio, fmt, ap);
 	pthread_cleanup_pop(1);
 	return ret;
 }
@@ -215,11 +228,12 @@ static int _log_dequeue(void *buff)
 
 int log_dequeue(void *buff)
 {
-	int ret;
+	int ret = 1;
 
 	pthread_mutex_lock(&logq_lock);
 	pthread_cleanup_push(cleanup_mutex, &logq_lock);
-	ret = _log_dequeue(buff);
+	if (la)
+		ret = _log_dequeue(buff);
 	pthread_cleanup_pop(1);
 	return ret;
 }
diff --git a/libmultipath/log.h b/libmultipath/log.h
index d2448f6..fa224e4 100644
--- a/libmultipath/log.h
+++ b/libmultipath/log.h
@@ -39,6 +39,5 @@ int log_enqueue (int prio, const char * fmt, va_list ap)
 int log_dequeue (void *);
 void log_syslog (void *);
 void dump_logmsg (void *);
-void free_logarea (void);
 
 #endif /* LOG_H */
-- 
2.29.0


--
dm-devel mailing list
dm-devel@redhat.com
https://www.redhat.com/mailman/listinfo/dm-devel


  parent reply	other threads:[~2020-12-17 11:00 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-17 11:00 [dm-devel] [PATCH 0/7] Various multipath-tools patches mwilck
2020-12-17 11:00 ` [dm-devel] [PATCH 1/7] libmultipath: move logq_lock handling to log.c mwilck
2020-12-17 23:56   ` Benjamin Marzinski
2020-12-17 11:00 ` mwilck [this message]
2020-12-18  0:03   ` [dm-devel] [PATCH 2/7] libmultipath: protect logarea with logq_lock Benjamin Marzinski
2020-12-18 16:24     ` Martin Wilck
2020-12-18 16:32       ` Benjamin Marzinski
2020-12-17 11:00 ` [dm-devel] [PATCH 3/7] libmultipath: prevent DSO unloading with astray checker threads mwilck
2020-12-18  4:22   ` Benjamin Marzinski
2020-12-17 11:00 ` [dm-devel] [PATCH 4/7] libmultipath: force map reload if udev incomplete mwilck
2020-12-18  5:48   ` Benjamin Marzinski
2020-12-18 15:06     ` Martin Wilck
2020-12-18 15:08       ` Martin Wilck
2020-12-18 17:57   ` Benjamin Marzinski
2020-12-17 11:00 ` [dm-devel] [PATCH 5/7] multipath-tools: avoid access to /etc/localtime mwilck
2020-12-18 18:14   ` Benjamin Marzinski
2020-12-17 11:00 ` [dm-devel] [PATCH 6/7] multipath-tools: make sure plugin DSOs use symbol versions mwilck
2020-12-18 18:36   ` Benjamin Marzinski
2020-12-17 11:00 ` [dm-devel] [PATCH 7/7] libmultipath.version: add missing symbol mwilck
2020-12-18 18:36   ` Benjamin Marzinski

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=20201217110018.3347-3-mwilck@suse.com \
    --to=mwilck@suse.com \
    --cc=bmarzins@redhat.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.