From bac621760e0e2f931f7c2ab4de47bca663a2af94 Mon Sep 17 00:00:00 2001 From: Victor Julien Date: Wed, 17 Nov 2010 11:50:36 +0100 Subject: [PATCH] Fix a potential invalid memory read in the protocol name code used by alert-fastlog. --- src/util-proto-name.c | 15 +++++++++------ src/util-proto-name.h | 14 ++++++++------ 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/util-proto-name.c b/src/util-proto-name.c index 84c71eb0a8..841d71f552 100644 --- a/src/util-proto-name.c +++ b/src/util-proto-name.c @@ -38,6 +38,7 @@ void SCProtoNameInit() if (fp != NULL) { char line[200]; char *ptr = NULL; + while(fgets(line, sizeof(line), fp) != NULL) { if (line[0] == '#') continue; @@ -45,14 +46,16 @@ void SCProtoNameInit() char *name = strtok_r(line," \t", &ptr); if (name == NULL) continue; + char *proto_ch = strtok_r(NULL," \t", &ptr); if (proto_ch == NULL) continue; + int proto = atoi(proto_ch); if (proto >= 255) continue; - char *cname = strtok_r(NULL, " \t", &ptr); + char *cname = strtok_r(NULL, " \t", &ptr); if (cname != NULL) { known_proto[proto] = strdup(cname); } else { @@ -67,14 +70,14 @@ void SCProtoNameInit() * \brief Function to check if the received protocol number is valid and do * we have corresponding name entry for this number or not. * - * @param proto Protocol number to be validated - * @return On success returns TRUE otherwise FALSE + * \param proto Protocol number to be validated + * \retval ret On success returns TRUE otherwise FALSE */ uint8_t SCProtoNameValid(uint16_t proto) { uint8_t ret = FALSE; - if ((proto <= 255) && known_proto[proto] != NULL) - { + + if (proto <= 255 && known_proto[proto] != NULL) { ret = TRUE; } @@ -91,4 +94,4 @@ void SCProtoNameDeInit() if(known_proto[cnt] != NULL) SCFree(known_proto[cnt]); } -} \ No newline at end of file +} diff --git a/src/util-proto-name.h b/src/util-proto-name.h index b3ffa2e414..667de93337 100644 --- a/src/util-proto-name.h +++ b/src/util-proto-name.h @@ -21,16 +21,18 @@ * \author Gurvinder Singh */ -#ifndef _UTIL_PROTO_NAME_H -#define _UTIL_PROTO_NAME_H +#ifndef __UTIL_PROTO_NAME_H__ +#define __UTIL_PROTO_NAME_H__ #define PROTO_FILE "/etc/protocols" -/* Structure to hold the information related to known protocol in /etc/protocols */ -char *known_proto[255]; -uint8_t SCProtoNameValid(uint16_t ); +/** Lookup array to hold the information related to known protocol + * in /etc/protocols */ +char *known_proto[256]; + +uint8_t SCProtoNameValid(uint16_t); void SCProtoNameInit(void); void SCProtoNameDeInit(void); -#endif /* _UTIL_PROTO_NAME_H */ +#endif /* __UTIL_PROTO_NAME_H__ */