From: maxtors Date: Wed, 15 Apr 2015 14:38:58 +0000 (+0200) Subject: Fixed string copy and cat functions and made shortening safer. X-Git-Tag: suricata-3.1RC1~225 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=88a6e7960799370b9e9f56a94a3be630043e3c3d;p=thirdparty%2Fsuricata.git Fixed string copy and cat functions and made shortening safer. Changed out strcpy, strncpy to strlcat and strlcpy. Also added checks to see if the shortening did work or if it would fail in advance. Fixed code in util-device and util-runmodes. --- diff --git a/src/util-device.c b/src/util-device.c index 917e989537..6d0e0d3d7d 100644 --- a/src/util-device.c +++ b/src/util-device.c @@ -115,19 +115,27 @@ char *LiveGetDeviceName(int number) * * \retval None, is added to destination char *newdevname */ -void LiveSafeDeviceName(const char *devname, char *newdevname) +int LiveSafeDeviceName(const char *devname, char *newdevname, size_t destlen) { size_t devnamelen = strlen(devname); + // If we have to shorten the interface name if (devnamelen > MAX_DEVNAME) { - strncpy(newdevname, devname, DEVNAME_CHUNCK); - strncpy(newdevname+DEVNAME_CHUNCK, "...", 3); - strncpy(newdevname+8, devname+(devnamelen-DEVNAME_CHUNCK), DEVNAME_CHUNCK); - strncpy(newdevname+13, "\0", 1); + + // We need 13 chars to do this shortening + if (destlen < 13) { + return 1; + } + + size_t length; + length = strlcpy(newdevname, devname, DEVNAME_CHUNCK); + length = strlcat(newdevname, "...", DEVNAME_CHUNCK+3); + length = strlcat(newdevname, devname+(devnamelen-DEVNAME_CHUNCK), length+DEVNAME_CHUNCK); SCLogInfo("Shortening device name to: %s", newdevname); } else { - strcpy(newdevname, devname); + strlcpy(newdevname, devname, destlen); } + return 0; } /** diff --git a/src/util-device.h b/src/util-device.h index 5260db16f0..a6f78dd50b 100644 --- a/src/util-device.h +++ b/src/util-device.h @@ -35,7 +35,7 @@ typedef struct LiveDevice_ { int LiveRegisterDevice(const char *dev); int LiveGetDeviceCount(void); char *LiveGetDeviceName(int number); -void LiveSafeDeviceName(const char *devname, char *newdevname); +int LiveSafeDeviceName(const char *devname, char *newdevname, size_t destlen); LiveDevice *LiveGetDevice(const char *dev); int LiveBuildDeviceList(const char *base); void LiveDeviceHasNoStats(void); diff --git a/src/util-runmodes.c b/src/util-runmodes.c index cfeba9a845..85c1d50190 100644 --- a/src/util-runmodes.c +++ b/src/util-runmodes.c @@ -187,6 +187,7 @@ int RunModeSetLiveCaptureAutoFp(ConfigIfaceParserFunc ConfigParser, for (lthread = 0; lthread < nlive; lthread++) { char *live_dev = LiveGetDeviceName(lthread); char visual_devname[14] = ""; + int shortening_result; void *aconf; int threads_count; @@ -205,9 +206,20 @@ int RunModeSetLiveCaptureAutoFp(ConfigIfaceParserFunc ConfigParser, threads_count = ModThreadsCount(aconf); for (thread = 0; thread < threads_count; thread++) { - LiveSafeDeviceName(live_dev, visual_devname); + shortening_result = LiveSafeDeviceName(live_dev, visual_devname, 13); + if (shortening_result != 0) { + SCLogError(SC_ERR_INVALID_VALUE, "Could not shorten long devicename: %s", live_dev); + exit(EXIT_FAILURE); + } + snprintf(tname, sizeof(tname), "%s%s%d", thread_name, live_dev, thread+1); + + char *thread_name = SCStrdup(tname); + if (unlikely(thread_name == NULL)) { + SCLogError(SC_ERR_MEM_ALLOC, "Can't allocate thread name"); + exit(EXIT_FAILURE); + } ThreadVars *tv_receive = TmThreadCreatePacketHandler(tname, "packetpool", "packetpool", @@ -317,15 +329,20 @@ static int RunModeSetLiveCaptureWorkersForDevice(ConfigIfaceThreadsCountFunc Mod for (thread = 0; thread < threads_count; thread++) { char tname[TM_THREAD_NAME_MAX]; char *n_thread_name = NULL; - char visual_devname[13] = ""; + char visual_devname[14] = ""; + int shortening_result; ThreadVars *tv = NULL; TmModule *tm_module = NULL; if (single_mode) { snprintf(tname, sizeof(tname), "%s", thread_name); } else { - LiveSafeDeviceName(live_dev, visual_devname); - SCLogInfo("New dev name %s", visual_devname); + shortening_result = LiveSafeDeviceName(live_dev, visual_devname, 13); + if (shortening_result != 0) { + SCLogError(SC_ERR_INVALID_VALUE, "Could not shorten long devicename: %s", live_dev); + exit(EXIT_FAILURE); + } + snprintf(tname, sizeof(tname), "%s%s%d", thread_name, live_dev, thread+1); }