cmirrord memory handling fixes
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
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
@@ -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
PATCH 2/6 cmirrord: introduce the --foreground option to avoid daemonization
In reply to Ferenc WágnerAlso add the compulsory --help option.
--- daemons/cmirrord/clogd.c | 42 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-)
daemons/cmirrord/clogd.c +39 -3
@@ -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
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
@@ -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
PATCH 4/6 cmirrord: fix stack smashing
In reply to Ferenc WágnerWith 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
@@ -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
PATCH 5/6 cmirrord: avoid debugging buffer overflow in LOG_SPRINT
In reply to Ferenc WágnerUse 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
@@ -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
PATCH 6/6 cmirrord: avoid resync buffer overflow in LOG_SPRINT
In reply to Ferenc WágnerUse 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
@@ -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
cover cmirrord memory handling fixes
In reply to Ferenc WágnerQuoted 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_checkpointseems 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 unsafesprintf()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 fromdaemonize()toinit_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 inLOG_SPRINT
cmirrord: avoid resync buffer overflow inLOG_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.
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_checkpointseems 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 unsafesprintf()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 fromdaemonize()toinit_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 inLOG_SPRINT
cmirrord: avoid resync buffer overflow inLOG_SPRINTQuoted 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