Skip to contentSkip to conversation

cmirrord memory handling fixes

PATCH New Ferenc Wágner 2 participants 9 messages Jul 8, 2015 – Aug 11, 2015

lore
Share & tools
lore
#0 lore raw

cover cmirrord memory handling fixes

Hi,

During clustered pvmove tests cmirrord kept on dying. To ease debugging, I added a --foreground option to it [1-3], which made the GCC stack protector messages stand out at once. The backtraces made the fix straightforward [4]. As log_checkpoint seems a constant false condition, preparing the message is generally wasted effort, but I didn't want to change the logic, and now it does not crash the process at least. Looking for other unsafe sprintf() usages provided plentiful results, I fixed the two easy ones [5,6]. Please use as you see fit.

Ferenc W?gner (6):
cmirrord: move generic setup from daemonize() to init_all()
cmirrord: introduce the --foreground option to avoid daemonization
cmirrord: document the --foreground and --help options
cmirrord: fix stack smashing
cmirrord: avoid debugging buffer overflow in LOG_SPRINT
cmirrord: avoid resync buffer overflow in LOG_SPRINT

 daemons/cmirrord/clogd.c     |   62 +++++++++++++++++++++++++++++++++---------
 daemons/cmirrord/cluster.c   |   23 ++++++++++++----
 daemons/cmirrord/functions.c |    5 ++--
 man/cmirrord.8.in            |    8 +++++-
 4 files changed, 76 insertions(+), 22 deletions(-)
Signature · 1 line
1.7.10.4
#1 lore raw

PATCH 1/6 cmirrord: move generic setup from daemonize() to init_all()

Pidfile creation, removal and signal setup are applicable to foreground processes as well.

---
 daemons/cmirrord/clogd.c |   20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
daemons/cmirrord/clogd.c +10 -10
index 86d06bd..b4736e1 100644
@@ -209,6 +209,16 @@ static void daemonize(void)
 	}
 
 	LOG_OPEN("cmirrord", LOG_PID, LOG_DAEMON);
+}
+
+/*
+ * init_all
+ *
+ * Initialize modules.  Exit on failure.
+ */
+static void init_all(void)
+{
+	int r;
 
 	(void) dm_prepare_selinux_context(CMIRRORD_PIDFILE, S_IFREG);
 	if (dm_create_lockfile(CMIRRORD_PIDFILE) == 0)
@@ -227,16 +237,6 @@ static void daemonize(void)
 	signal(SIGUSR2, &sig_handler);
 	sigemptyset(&signal_mask);
 	signal_received = 0;
-}
-
-/*
- * init_all
- *
- * Initialize modules.  Exit on failure.
- */
-static void init_all(void)
-{
-	int r;
 
 	if ((r = init_local()) ||
 	    (r = init_cluster())) {
Signature · 1 line
1.7.10.4
#2 lore raw

PATCH 2/6 cmirrord: introduce the --foreground option to avoid daemonization

In reply to Ferenc Wágner

Also add the compulsory --help option.

---
 daemons/cmirrord/clogd.c |   42 +++++++++++++++++++++++++++++++++++++++---
 1 file changed, 39 insertions(+), 3 deletions(-)
daemons/cmirrord/clogd.c +39 -3
index b4736e1..6638843 100644
@@ -15,6 +15,7 @@
 #include "link_mon.h"
 #include "local.h"
 
+#include <getopt.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <sys/socket.h>
@@ -32,14 +33,49 @@ static void daemonize(void);
 static void init_all(void);
 static void cleanup_all(void);
 
-int main(int argc __attribute__((unused)), char *argv[] __attribute__((unused)))
+static void usage (FILE *dest)
 {
-	daemonize();
+	fprintf (dest, "Usage: cmirrord [options]\n"
+		 "   -f, --foreground    stay in the foreground, log to the terminal\n"
+		 "   -h, --help          print this help\n");
+}
+
+int main(int argc, char *argv[])
+{
+	int foreground_mode = 0;
+	struct option longopts[] = {
+		{ "foreground", no_argument, NULL, 'f' },
+		{ "help"      , no_argument, NULL, 'h' },
+		{ 0, 0, 0, 0 }
+	};
+	int opt;
+
+	while ((opt = getopt_long (argc, argv, "fh", longopts, NULL)) != -1) {
+		switch (opt) {
+		case 'f':
+			foreground_mode = 1;
+			break;
+		case 'h':
+			usage (stdout);
+			exit (0);
+		default:
+			usage (stderr);
+			exit (2);
+		}
+	}
+	if (optind < argc) {
+		usage (stderr);
+		exit (2);
+	}
+
+	if (!foreground_mode)
+		daemonize();
 
 	init_all();
 
 	/* Parent can now exit, we're ready to handle requests */
-	kill(getppid(), SIGTERM);
+	if (!foreground_mode)
+		kill(getppid(), SIGTERM);
 
 	LOG_PRINT("Starting cmirrord:");
 	LOG_PRINT(" Built: "__DATE__" "__TIME__"\n");
Signature · 1 line
1.7.10.4
#3 lore raw

PATCH 3/6 cmirrord: document the --foreground and --help options

In reply to Ferenc Wágner
---
 man/cmirrord.8.in |    8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
man/cmirrord.8.in +7 -1
index 8ef6794..8ea2ba9 100644
@@ -3,7 +3,7 @@
 cmirrord \(em cluster mirror log daemon
 
 .SH SYNOPSIS
-.B cmirrord
+\fBcmirrord\fR [\fB\-f\fR] [\fB\-h\fR]
 
 .SH DESCRIPTION
 cmirrord is the daemon that tracks mirror log information in a cluster.
@@ -24,6 +24,12 @@ there are still active cluster mirrors, however, the signal will be
 ignored.  Active cluster mirrors should be shutdown before stopping the
 cluster mirror log daemon.
 
+.SH OPTIONS
+.IP "\fB\-f\fR, \fB\-\-foreground\fR" 4
+Do not daemonize and log to the terminal.
+.IP "\fB\-h\fR, \fB\-\-help\fR" 4
+Print usage.
+
 .SH SEE ALSO
 .BR lvm (8)
 .BR clvmd (8)
Signature · 1 line
1.7.10.4
#4 lore raw

PATCH 4/6 cmirrord: fix stack smashing

In reply to Ferenc Wágner

With clusters larger than 3 nodes, the 32-byte debug buffer in cpg_join_callback() is too small to contain all the node IDs, because 32-bit identifiers are generally rendered in 10 decimal digits. No fixed size is good in all cases, but this is conditionally logged debug info, so we can simply truncate it. Double the size, nevertheless.

---
 daemons/cmirrord/cluster.c |   18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)
daemons/cmirrord/cluster.c +14 -4
index 3fd5d23..4a4bdab 100644
@@ -1294,7 +1294,9 @@ static void cpg_join_callback(struct clog_cpg *match,
 	uint32_t my_pid = (uint32_t)getpid();
 	uint32_t lowest = match->lowest_id;
 	struct clog_request *rq;
-	char dbuf[32] = { 0 };
+	char dbuf[64] = { 0 };
+	char *dbuf_p = dbuf;
+	size_t dbuf_rem = sizeof dbuf;
 
 	/* Assign my_cluster_id */
 	if ((my_cluster_id == 0xDEAD) && (joined->pid == my_pid))
@@ -1310,9 +1312,17 @@ static void cpg_join_callback(struct clog_cpg *match,
 	if (joined->nodeid == my_cluster_id)
 		goto out;
 
-	for (i = 0; i < member_list_entries - 1; i++)
-		sprintf(dbuf+strlen(dbuf), "%u-", member_list[i].nodeid);
-	sprintf(dbuf+strlen(dbuf), "(%u)", joined->nodeid);
+	for (i = 0; i < member_list_entries - 1; i++) {
+		int written = snprintf(dbuf_p, dbuf_rem, "%u-", member_list[i].nodeid);
+		if (written < 0) continue; /* impossible */
+		if ((unsigned)written >= dbuf_rem) {
+			dbuf_rem = 0;
+			break;
+		}
+		dbuf_rem -= written;
+		dbuf_p += written;
+	}
+	snprintf(dbuf_p, dbuf_rem, "(%u)", joined->nodeid);
 	LOG_COND(log_checkpoint, "[%s] Joining node, %u needs checkpoint [%s]",
 		 SHORT_UUID(match->name.value), joined->nodeid, dbuf);
 
Signature · 1 line
1.7.10.4
#5 lore raw

PATCH 5/6 cmirrord: avoid debugging buffer overflow in LOG_SPRINT

In reply to Ferenc Wágner

Use snprintf() instead of sprintf() to exclude the possibility of overflowing the debugging history buffers.

---
 daemons/cmirrord/cluster.c |    5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
daemons/cmirrord/cluster.c +3 -2
index 4a4bdab..2850d9b 100644
@@ -104,10 +104,11 @@ static SaVersionT version = { 'B', 1, 1 };
 #endif
 
 #define DEBUGGING_HISTORY 100
+#define DEBUGGING_BUFLEN 128
 #define LOG_SPRINT(cc, f, arg...) do {				\
 		cc->idx++;					\
 		cc->idx = cc->idx % DEBUGGING_HISTORY;		\
-		sprintf(cc->debugging[cc->idx], f, ## arg);	\
+		snprintf(cc->debugging[cc->idx], DEBUGGING_BUFLEN, f, ## arg); \
 	} while (0)
 
 static int log_resp_rec = 0;
@@ -150,7 +151,7 @@ struct clog_cpg {
 	uint32_t checkpoint_requesters[MAX_CHECKPOINT_REQUESTERS];
 	struct checkpoint_data *checkpoint_list;
 	int idx;
-	char debugging[DEBUGGING_HISTORY][128];
+	char debugging[DEBUGGING_HISTORY][DEBUGGING_BUFLEN];
 };
 
 static struct dm_list clog_cpg_list;
Signature · 1 line
1.7.10.4
#6 lore raw

PATCH 6/6 cmirrord: avoid resync buffer overflow in LOG_SPRINT

In reply to Ferenc Wágner

Use snprintf() instead of sprintf() to exclude the possibility of overflowing the resync history buffers.

---
 daemons/cmirrord/functions.c |    5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
daemons/cmirrord/functions.c +3 -2
index f6e0918..61b3e11 100644
@@ -32,12 +32,13 @@
 #define LOG_OFFSET 2
 
 #define RESYNC_HISTORY 50
+#define RESYNC_BUFLEN 128
 //static char resync_history[RESYNC_HISTORY][128];
 //static int idx = 0;
 #define LOG_SPRINT(_lc, f, arg...) do {					\
 		lc->idx++;						\
 		lc->idx = lc->idx % RESYNC_HISTORY;			\
-		sprintf(lc->resync_history[lc->idx], f, ## arg);	\
+		snprintf(lc->resync_history[lc->idx], RESYNC_BUFLEN, f, ## arg); \
 	} while (0)
 
 struct log_header {
@@ -88,7 +89,7 @@ struct log_c {
 	size_t disk_size;       /* size of disk_buffer in bytes */
 	void *disk_buffer;      /* aligned memory for O_DIRECT */
 	int idx;
-	char resync_history[RESYNC_HISTORY][128];
+	char resync_history[RESYNC_HISTORY][RESYNC_BUFLEN];
 };
 
 struct mark_entry {
Signature · 1 line
1.7.10.4
#7 lore raw

cover cmirrord memory handling fixes

In reply to Ferenc Wágner
Quoted 17 lines · Ferenc W?gner

During clustered pvmove tests cmirrord kept on dying. To ease debugging, I added a --foreground option to it [1-3], which made the GCC stack protector messages stand out at once. The backtraces made the fix straightforward [4]. As log_checkpoint seems a constant false condition, preparing the message is generally wasted effort, but I didn't want to change the logic, and now it does not crash the process at least. Looking for other unsafe sprintf() usages provided plentiful results, I fixed the two easy ones [5,6]. Please use as you see fit.

Ferenc W?gner (6):
cmirrord: move generic setup from daemonize() to init_all()
cmirrord: introduce the --foreground option to avoid daemonization
cmirrord: document the --foreground and --help options
cmirrord: fix stack smashing
cmirrord: avoid debugging buffer overflow in LOG_SPRINT
cmirrord: avoid resync buffer overflow in LOG_SPRINT

Quoted 5 lines
 daemons/cmirrord/clogd.c     |   62 +++++++++++++++++++++++++++++++++---------
 daemons/cmirrord/cluster.c   |   23 ++++++++++++----
 daemons/cmirrord/functions.c |    5 ++--
 man/cmirrord.8.in            |    8 +++++-
 4 files changed, 76 insertions(+), 22 deletions(-)

Hi,

Are there any objections to these patches? Shall I rework them to have any hope for inclusion?

Signature · 2 lines
Thanks,
Feri.
#8 lore raw

cover cmirrord memory handling fixes

Quoted 28 lines · Ferenc Wagner
Quoted 17 lines · Ferenc W?gner

During clustered pvmove tests cmirrord kept on dying. To ease debugging, I added a --foreground option to it [1-3], which made the GCC stack protector messages stand out at once. The backtraces made the fix straightforward [4]. As log_checkpoint seems a constant false condition, preparing the message is generally wasted effort, but I didn't want to change the logic, and now it does not crash the process at least. Looking for other unsafe sprintf() usages provided plentiful results, I fixed the two easy ones [5,6]. Please use as you see fit.

Ferenc W?gner (6):
cmirrord: move generic setup from daemonize() to init_all()
cmirrord: introduce the --foreground option to avoid daemonization
cmirrord: document the --foreground and --help options
cmirrord: fix stack smashing
cmirrord: avoid debugging buffer overflow in LOG_SPRINT
cmirrord: avoid resync buffer overflow in LOG_SPRINT

Quoted 5 lines
daemons/cmirrord/clogd.c     |   62 +++++++++++++++++++++++++++++++++---------
daemons/cmirrord/cluster.c   |   23 ++++++++++++----
daemons/cmirrord/functions.c |    5 ++--
man/cmirrord.8.in            |    8 +++++-
4 files changed, 76 insertions(+), 22 deletions(-)
Quoted 4 lines

Hi,

Are there any objections to these patches? Shall I rework them to have any hope for inclusion?

No objections. They look helpful, thanks!

I?ve looked them over, but I haven?t had a chance to test them. I?ll try to get them pulled in soon.

brassow