From a8b64dba013d5aa899d5ce29370bf1a52fb3fca9 Mon Sep 17 00:00:00 2001 From: Jeffrey Hutzelman Date: Wed, 14 Jul 2010 01:20:22 -0400 Subject: [PATCH] Don't overflow a buffer on the server's stack at startup The servers like to log the command line it was invoked with. It does this by concatenating its arguments, separated by spaces, into a 150-character buffer on the stack of main(). That's just wrong. Use a dynamically-allocated buffer instead. Change-Id: Ibe398e5f449eec176f11f301090a1b68ef13e94b Reviewed-on: http://gerrit.openafs.org/2275 Tested-by: Derrick Brashear Reviewed-by: Andrew Deason Reviewed-by: Derrick Brashear --- src/ptserver/ptserver.c | 8 ++++++++ src/util/afsutil_prototypes.h | 3 +++ src/util/serverLog.c | 28 ++++++++++++++++++++++++++++ src/viced/viced.c | 6 +----- src/vlserver/vldbint.xg | 2 ++ src/vlserver/vlserver.c | 11 ++--------- src/vol/salvaged.c | 31 ++++++++++++++++--------------- src/vol/salvager.c | 11 ++--------- src/volser/volmain.c | 11 ++--------- 9 files changed, 64 insertions(+), 47 deletions(-) diff --git a/src/ptserver/ptserver.c b/src/ptserver/ptserver.c index d6860ffc6..b0fe4f984 100644 --- a/src/ptserver/ptserver.c +++ b/src/ptserver/ptserver.c @@ -556,6 +556,14 @@ main(int argc, char **argv) /* allow super users to manage RX statistics */ rx_SetRxStatUserOk(pr_rxstat_userok); + LogCommandLine(argc, argv, "ptserver", +#if defined(SUPERGROUPS) + "1.1", +#else + "1.0", +#endif + "Starting AFS", FSLog); + rx_StartServer(1); osi_audit(PTS_FinishEvent, -1, AUD_END); exit(0); diff --git a/src/util/afsutil_prototypes.h b/src/util/afsutil_prototypes.h index c881ff4a9..87d3de628 100644 --- a/src/util/afsutil_prototypes.h +++ b/src/util/afsutil_prototypes.h @@ -171,6 +171,9 @@ extern void SetupLogSignals(void); extern int OpenLog(const char *fileName); extern int ReOpenLog(const char *fileName); extern int LogThreadNum(void); +extern void LogCommandLine(int argc, char **argv, const char *progname, + const char *version, const char *logstring, + void (*log) (const char *format, ...)); /* snprintf.c */ diff --git a/src/util/serverLog.c b/src/util/serverLog.c index 8bc51afbd..6a60b5f8c 100644 --- a/src/util/serverLog.c +++ b/src/util/serverLog.c @@ -35,6 +35,7 @@ #include /* signal(), kill(), wait(), etc. */ #include #include +#include #include #include #include "afsutil.h" @@ -167,6 +168,33 @@ FSLog(const char *format, ...) va_end(args); } /*FSLog */ +void +LogCommandLine(int argc, char **argv, const char *progname, + const char *version, const char *logstring, + void (*log) (const char *format, ...)) +{ + int i, l; + char *commandLine, *cx; + + for (l = i = 0; i < argc; i++) + l += strlen(argv[i]) + 1; + if ((commandLine = malloc(l))) { + for (cx = commandLine, i = 0; i < argc; i++) { + strcpy(cx, argv[i]); + cx += strlen(cx); + *(cx++) = ' '; + } + commandLine[l-1] = '\0'; + (*log)("%s %s %s%s(%s)\n", logstring, progname, + version, strlen(version)>0?" ":"", commandLine); + free(commandLine); + } else { + /* What, we're out of memory already!? */ + (*log)("%s %s%s%s\n", logstring, + progname, strlen(version)>0?" ":"", version); + } +} + static void* DebugOn(void *param) { diff --git a/src/viced/viced.c b/src/viced/viced.c index 2c259c029..3901e9a76 100644 --- a/src/viced/viced.c +++ b/src/viced/viced.c @@ -1984,11 +1984,7 @@ main(int argc, char *argv[]) ViceLog(0, ("Both -spare and -pctspare specified, exiting.\n")); exit(-1); } -#ifdef AFS_SGI_XFS_IOPS_ENV - ViceLog(0, ("XFS/EFS File server starting\n")); -#else - ViceLog(0, ("File server starting\n")); -#endif + LogCommandLine(argc, argv, "starting", "", "File server", FSLog); #if defined(AFS_PTHREAD_ENV) && !defined(AFS_NT40_ENV) /* initialize the pthread soft signal handler thread */ diff --git a/src/vlserver/vldbint.xg b/src/vlserver/vldbint.xg index 3cad550a8..4d81cee7a 100644 --- a/src/vlserver/vldbint.xg +++ b/src/vlserver/vldbint.xg @@ -19,6 +19,8 @@ statindex 15 %#endif /* Current limitations on parameters that affect other packages (i.e. volume) */ +%#define VldbVersion "4" + const VLDBVERSION_4 = 4; const VLDBVERSION = 3; const OVLDBVERSION = 2; diff --git a/src/vlserver/vlserver.c b/src/vlserver/vlserver.c index 448d0f510..417347943 100644 --- a/src/vlserver/vlserver.c +++ b/src/vlserver/vlserver.c @@ -141,8 +141,7 @@ main(int argc, char **argv) struct afsconf_cell info; struct hostent *th; char hostname[VL_MAXNAMELEN]; - int noAuth = 0, index, i; - char commandLine[150]; + int noAuth = 0, index; char clones[MAXHOSTSPERCELL]; char *auditFileName = NULL; afs_uint32 host = ntohl(INADDR_ANY); @@ -398,13 +397,7 @@ main(int argc, char **argv) rx_SetMinProcs(tservice, 2); rx_SetMaxProcs(tservice, 4); - for (commandLine[0] = '\0', i = 0; i < argc; i++) { - if (i > 0) - strcat(commandLine, " "); - strcat(commandLine, argv[i]); - } - ViceLog(0, - ("Starting AFS vlserver %d (%s)\n", VLDBVERSION_4, commandLine)); + LogCommandLine(argc, argv, "vlserver", VldbVersion, "Starting AFS", FSLog); printf("%s\n", cml_version_number); /* Goes to the log */ /* allow super users to manage RX statistics */ diff --git a/src/vol/salvaged.c b/src/vol/salvaged.c index d073f8963..f1dde1f94 100644 --- a/src/vol/salvaged.c +++ b/src/vol/salvaged.c @@ -154,7 +154,7 @@ static pthread_cond_t worker_cv; static void * SalvageChildReaperThread(void *); static int DoSalvageVolume(struct SalvageQueueNode * node, int slot); -static void SalvageServer(void); +static void SalvageServer(int argc, char **argv); static void SalvageClient(VolumeId vid, char * pname); static int Reap_Child(char * prog, int * pid, int * status); @@ -165,6 +165,11 @@ static int SalvageLogCleanup(int pid); static void * SalvageLogScanningThread(void *); static void ScanLogs(struct rx_queue *log_watch_queue); +struct cmdline_rock { + int argc; + char **argv; +}; + struct log_cleanup_node { struct rx_queue q; int pid; @@ -184,6 +189,7 @@ handleit(struct cmd_syndesc *as, void *arock) register struct cmd_item *ti; char pname[100], *temp; afs_int32 seenpart = 0, seenvol = 0, vid = 0; + struct cmdline_rock *rock = (struct cmdline_rock *)arock; #ifdef AFS_SGI_VNODE_GLUE if (afs_init_kernel_config(-1) < 0) { @@ -286,7 +292,7 @@ handleit(struct cmd_syndesc *as, void *arock) SalvageClient(vid, pname); } else { /* salvageserver mode */ - SalvageServer(); + SalvageServer(rock->argc, rock->argv); } return (0); } @@ -302,15 +308,12 @@ int n_save_args = 0; pthread_t main_thread; #endif -static char commandLine[150]; - int main(int argc, char **argv) { struct cmd_syndesc *ts; int err = 0; - - int i; + struct cmdline_rock arock; #ifdef AFS_AIX32_ENV /* @@ -347,11 +350,6 @@ main(int argc, char **argv) exit(3); } else { #endif - for (commandLine[0] = '\0', i = 0; i < argc; i++) { - if (i > 0) - strlcat(commandLine, " ", sizeof(commandLine)); - strlcat(commandLine, argv[i], sizeof(commandLine)); - } #ifndef AFS_NT40_ENV if (geteuid() != 0) { @@ -367,7 +365,10 @@ main(int argc, char **argv) } #endif - ts = cmd_CreateSyntax("initcmd", handleit, NULL, "initialize the program"); + arock.argc = argc; + arock.argv = argv; + + ts = cmd_CreateSyntax("initcmd", handleit, &arock, "initialize the program"); cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_OPTIONAL, "Name of partition to salvage"); cmd_AddParm(ts, "-volumeid", CMD_SINGLE, CMD_OPTIONAL, @@ -472,7 +473,7 @@ SalvageClient(VolumeId vid, char * pname) static int * child_slot; static void -SalvageServer(void) +SalvageServer(int argc, char **argv) { int pid, ret; struct SalvageQueueNode * node; @@ -496,8 +497,8 @@ SalvageServer(void) setlinebuf(logFile); fprintf(logFile, "%s\n", cml_version_number); - Log("Starting OpenAFS Online Salvage Server %s (%s)\n", SalvageVersion, commandLine); - + LogCommandLine(argc, argv, "Online Salvage Server", + SalvageVersion, "Starting OpenAFS", Log); /* Get and hold a lock for the duration of the salvage to make sure * that no other salvage runs at the same time. The routine * VInitVolumePackage2 (called below) makes sure that a file server or diff --git a/src/vol/salvager.c b/src/vol/salvager.c index 8716dc2de..8d7ec45cb 100644 --- a/src/vol/salvager.c +++ b/src/vol/salvager.c @@ -372,9 +372,7 @@ main(int argc, char **argv) { struct cmd_syndesc *ts; int err = 0; - char commandLine[150]; - int i; extern char cml_version_number[]; #ifdef AFS_AIX32_ENV @@ -412,12 +410,6 @@ main(int argc, char **argv) exit(3); } else { #endif - for (commandLine[0] = '\0', i = 0; i < argc; i++) { - if (i > 0) - strcat(commandLine, " "); - strcat(commandLine, argv[i]); - } - /* All entries to the log will be appended. Useful if there are * multiple salvagers appending to the log. */ @@ -443,7 +435,8 @@ main(int argc, char **argv) /* bad for normal help flag processing, but can do nada */ fprintf(logFile, "%s\n", cml_version_number); - Log("STARTING AFS SALVAGER %s (%s)\n", SalvageVersion, commandLine); + LogCommandLine(argc, argv, "SALVAGER", SalvageVersion, "STARTING AFS", + Log); /* Get and hold a lock for the duration of the salvage to make sure * that no other salvage runs at the same time. The routine diff --git a/src/volser/volmain.c b/src/volser/volmain.c index 8827e71af..592a1f31e 100644 --- a/src/volser/volmain.c +++ b/src/volser/volmain.c @@ -248,8 +248,6 @@ main(int argc, char **argv) struct rx_service *service; struct ktc_encryptionKey tkey; int rxpackets = 100; - char commandLine[150]; - int i; int rxJumbograms = 0; /* default is to send and receive jumbograms. */ int rxMaxMTU = -1; int bufSize = 0; /* temp variable to read in udp socket buf size */ @@ -285,12 +283,6 @@ main(int argc, char **argv) exit(2); } - for (commandLine[0] = '\0', i = 0; i < argc; i++) { - if (i > 0) - strcat(commandLine, " "); - strcat(commandLine, argv[i]); - } - TTsleep = TTrun = 0; /* parse cmd line */ @@ -541,7 +533,8 @@ main(int argc, char **argv) rx_SetMinProcs(service, 2); rx_SetMaxProcs(service, 4); - Log("Starting AFS Volserver %s (%s)\n", VolserVersion, commandLine); + LogCommandLine(argc, argv, "Volserver", VolserVersion, "Starting AFS", + Log); if (TTsleep) { Log("Will sleep %d second%s every %d second%s\n", TTsleep, (TTsleep > 1) ? "s" : "", TTrun + TTsleep, -- 2.39.5