From 4becae174a3301eac5955f7900d3eef66728e440 Mon Sep 17 00:00:00 2001 From: Rainer Gerhards Date: Wed, 30 Jan 2008 13:54:20 +0000 Subject: - fixed a bug that could cause invalid string handling via strerror_r varmojfekoj provided the patch - many thanks! - adopted varmojfekoj's patch to v3, some more sterror_r's ;) --- net.c | 1 + pidfile.c | 7 +++++-- plugins/imudp/imudp.c | 3 ++- plugins/imuxsock/imuxsock.c | 3 ++- srUtils.c | 31 +++++++++++++++++++++++++++++++ srUtils.h | 1 + syslogd.c | 15 --------------- syslogd.h | 1 - tcpsyslog.c | 1 + 9 files changed, 43 insertions(+), 20 deletions(-) diff --git a/net.c b/net.c index 4654282f..33dfc851 100644 --- a/net.c +++ b/net.c @@ -53,6 +53,7 @@ #include "syslogd-types.h" #include "net.h" #include "parse.h" +#include "srUtils.h" /* support for defining allowed TCP and UDP senders. We use the same * structure to implement this (a linked list), but we define two different diff --git a/pidfile.c b/pidfile.c index 9555c4a2..7ada92e7 100644 --- a/pidfile.c +++ b/pidfile.c @@ -23,6 +23,7 @@ #include "rsyslog.h" +#include "syslogd.h" /* * Sat Aug 19 13:24:33 MET DST 1995: Martin Schulze @@ -40,6 +41,8 @@ #include #endif +#include "srUtils.h" + /* read_pid * * Reads the specified pidfile and returns the read pid. @@ -121,7 +124,7 @@ int write_pid (char *pidfile) pid = getpid(); if (!fprintf(f,"%d\n", pid)) { char errStr[1024]; - strerror_r(errno, errStr, sizeof(errStr)); + rs_strerror_r(errno, errStr, sizeof(errStr)); printf("Can't write pid , %s.\n", errStr); close(fd); return 0; @@ -131,7 +134,7 @@ int write_pid (char *pidfile) #ifndef __sun if (flock(fd, LOCK_UN) == -1) { char errStr[1024]; - strerror_r(errno, errStr, sizeof(errStr)); + rs_strerror_r(errno, errStr, sizeof(errStr)); printf("Can't unlock pidfile %s, %s.\n", pidfile, errStr); close(fd); return 0; diff --git a/plugins/imudp/imudp.c b/plugins/imudp/imudp.c index d68e3772..fa9db53a 100644 --- a/plugins/imudp/imudp.c +++ b/plugins/imudp/imudp.c @@ -37,6 +37,7 @@ #include "net.h" #include "cfsysline.h" #include "module-template.h" +#include "srUtils.h" MODULE_TYPE_INPUT @@ -200,7 +201,7 @@ CODESTARTrunInput } } else if (l < 0 && errno != EINTR && errno != EAGAIN) { char errStr[1024]; - strerror_r(errno, errStr, sizeof(errStr)); + rs_strerror_r(errno, errStr, sizeof(errStr)); dbgprintf("INET socket error: %d = %s.\n", errno, errStr); logerror("recvfrom inet"); /* should be harmless */ diff --git a/plugins/imuxsock/imuxsock.c b/plugins/imuxsock/imuxsock.c index 530bb2d4..54bbe039 100644 --- a/plugins/imuxsock/imuxsock.c +++ b/plugins/imuxsock/imuxsock.c @@ -38,6 +38,7 @@ #include "syslogd.h" #include "cfsysline.h" #include "module-template.h" +#include "srUtils.h" MODULE_TYPE_INPUT @@ -169,7 +170,7 @@ static rsRetVal readSocket(int fd, int bParseHost) printchopped(LocalHostName, line, iRcvd, fd, bParseHost); } else if (iRcvd < 0 && errno != EINTR) { char errStr[1024]; - strerror_r(errno, errStr, sizeof(errStr)); + rs_strerror_r(errno, errStr, sizeof(errStr)); dbgprintf("UNIX socket error: %d = %s.\n", errno, errStr); logerror("recvfrom UNIX"); } diff --git a/srUtils.c b/srUtils.c index 6da1010d..24decdfe 100755 --- a/srUtils.c +++ b/srUtils.c @@ -389,6 +389,37 @@ srSleep(int iSeconds, int iuSeconds) select(0, NULL, NULL, NULL, &tvSelectTimeout); ENDfunc } + + +/* From varmojfekoj's mail on why he provided rs_strerror_r(): + * There are two problems with strerror_r(): + * I see you've rewritten some of the code which calls it to use only + * the supplied buffer; unfortunately the GNU implementation sometimes + * doesn't use the buffer at all and returns a pointer to some + * immutable string instead, as noted in the man page. + * + * The other problem is that on some systems strerror_r() has a return + * type of int. + * + * So I've written a wrapper function rs_strerror_r(), which should + * take care of all this and be used instead. + * + * Added 2008-01-30 + */ +char *rs_strerror_r(int errnum, char *buf, size_t buflen) { +#ifdef STRERROR_R_CHAR_P + char *p = strerror_r(errnum, buf, buflen); + if (p != buf) { + strncpy(buf, p, buflen); + buf[buflen - 1] = '\0'; + } +#else + strerror_r(errnum, buf, buflen); +#endif + return buf; +} + + /* * vi:set ai: */ diff --git a/srUtils.h b/srUtils.h index 8436cf0b..6b4bb1bb 100755 --- a/srUtils.h +++ b/srUtils.h @@ -70,6 +70,7 @@ rsRetVal timeoutComp(struct timespec *pt, long iTimeout); long timeoutVal(struct timespec *pt); void mutexCancelCleanup(void *arg); void srSleep(int iSeconds, int iuSeconds); +char *rs_strerror_r(int errnum, char *buf, size_t buflen); /* mutex operations */ /* some macros to cancel-safe lock a mutex (it will automatically be released diff --git a/syslogd.c b/syslogd.c index 5d2531bf..0f0076ed 100644 --- a/syslogd.c +++ b/syslogd.c @@ -4132,21 +4132,6 @@ int decode(uchar *name, struct code *codetab) } - -char *rs_strerror_r(int errnum, char *buf, size_t buflen) { -#ifdef STRERROR_R_CHAR_P - char *p = strerror_r(errnum, buf, buflen); - if (p != buf) { - strncpy(buf, p, buflen); - buf[buflen - 1] = '\0'; - } -#else - strerror_r(errnum, buf, buflen); -#endif - return buf; -} - - /* * The following function is resposible for handling a SIGHUP signal. Since * we are now doing mallocs/free as part of init we had better not being diff --git a/syslogd.h b/syslogd.h index 6163c292..17b45161 100644 --- a/syslogd.h +++ b/syslogd.h @@ -49,7 +49,6 @@ #define ADDDATE 0x004 /* add a date to the message */ #define MARK 0x008 /* this message is a mark */ -char *rs_strerror_r(int errnum, char *buf, size_t buflen); void logerror(char *type); void logerrorSz(char *type, char *errMsg); void logerrorInt(char *type, int iErr); diff --git a/tcpsyslog.c b/tcpsyslog.c index 0674f669..f8620e38 100644 --- a/tcpsyslog.c +++ b/tcpsyslog.c @@ -53,6 +53,7 @@ #include "gss-misc.h" #endif #include "tcpsyslog.h" +#include "srUtils.h" /******************************************************************** * ### SYSLOG/TCP CODE ### * This is code for syslog/tcp. This code would belong to a separate -- cgit