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.
pull/2050/head
maxtors 11 years ago committed by Victor Julien
parent 10d1450e49
commit 88a6e79607

@ -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;
}
/**

@ -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);

@ -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);
}

Loading…
Cancel
Save