]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
error checking: add missing alloc error treatment
authorEric Leblond <eric@regit.org>
Mon, 9 Dec 2013 17:58:32 +0000 (18:58 +0100)
committerVictor Julien <victor@inliniac.net>
Tue, 10 Dec 2013 10:35:09 +0000 (11:35 +0100)
The return of some malloc like functions was not treated in some
places of the code.

18 files changed:
src/alert-unified2-alert.c
src/app-layer-htp.c
src/app-layer-ssl.c
src/conf-yaml-loader.c
src/conf.c
src/counters.c
src/detect-flowvar.c
src/detect-tls.c
src/log-pcap.c
src/output.c
src/runmode-pfring.c
src/runmode-unix-socket.c
src/runmodes.c
src/source-mpipe.c
src/source-pfring.c
src/util-debug.c
src/util-device.c
src/util-proto-name.c

index 8f3beee9b2e49ab2a3a54abf4efebccc774c9e15..6cf3114339378f67dc856c28b7b9e223999103d3 100644 (file)
@@ -1332,6 +1332,10 @@ OutputCtx *Unified2AlertInitCtx(ConfNode *conf)
     if (filename == NULL)
         filename = DEFAULT_LOG_FILENAME;
     file_ctx->prefix = SCStrdup(filename);
+    if (unlikely(file_ctx->prefix == NULL)) {
+        SCLogError(SC_ERR_MEM_ALLOC, "Failed to allocate file prefix");
+        exit(EXIT_FAILURE);
+    }
 
     const char *s_limit = NULL;
     file_ctx->size_limit = DEFAULT_LIMIT;
@@ -1462,9 +1466,10 @@ int Unified2AlertOpenFileCtx(LogFileCtx *file_ctx, const char *prefix)
     if (file_ctx->filename != NULL)
         filename = file_ctx->filename;
     else {
-        filename = file_ctx->filename = SCMalloc(PATH_MAX); /* XXX some sane default? */
-        if (filename == NULL)
+        filename = SCMalloc(PATH_MAX); /* XXX some sane default? */
+        if (unlikely(filename == NULL))
             return -1;
+        file_ctx->filename =  filename;
 
         memset(filename, 0x00, PATH_MAX);
     }
index 1cbcf73fcb2eb2faa587a279aae8592f6f68c5de..d79ed0626972251c371265d58967dabb44402126 100644 (file)
@@ -2460,11 +2460,13 @@ void HTPConfigure(void)
         SCLogDebug("LIBHTP server %s", s->name);
 
         HTPCfgRec *nextrec = cfglist.next;
-        HTPCfgRec *htprec = cfglist.next = SCMalloc(sizeof(HTPCfgRec));
+        HTPCfgRec *htprec = SCMalloc(sizeof(HTPCfgRec));
         if (NULL == htprec)
             exit(EXIT_FAILURE);
         memset(htprec, 0x00, sizeof(*htprec));
 
+        cfglist.next = htprec;
+
         cfglist.next->next = nextrec;
         cfglist.next->cfg = htp_config_create();
         if (NULL == cfglist.next->cfg) {
index 76e9e2e9056cc26805ece9c5bcc0d7d08ef2ac28..149550bb90c57317519e81260e01db2f351fe3ca 100644 (file)
@@ -154,7 +154,7 @@ static int SSLv3ParseHandshakeType(SSLState *ssl_state, uint8_t *input,
                 ssl_state->curr_connp->trec_len = ssl_state->curr_connp->trec_len + 2 * input_len + 1;
                 ssl_state->curr_connp->trec = SCRealloc( ssl_state->curr_connp->trec, ssl_state->curr_connp->trec_len );
             }
-            if (ssl_state->curr_connp->trec == NULL) {
+            if (unlikely(ssl_state->curr_connp->trec == NULL)) {
                 ssl_state->curr_connp->trec_len = 0;
                 /* error, skip packet */
                 parsed += input_len;
index a5e0d27e66cf4b8a03b661b642798014e389dae9..dece14642fdd61be911896e656b409bd1e9f3165 100644 (file)
@@ -211,6 +211,10 @@ ConfYamlParse(yaml_parser_t *parser, ConfNode *parent, int inseq)
                     return -1;
                 snprintf(seq_node->name, DEFAULT_NAME_LEN, "%d", seq_idx++);
                 seq_node->val = SCStrdup(value);
+                if (unlikely(seq_node->val == NULL)) {
+                    SCFree(seq_node->name);
+                    return -1;
+                }
                 TAILQ_INSERT_TAIL(&parent->head, seq_node, next);
             }
             else {
index 6fd6805ea8d6e9d49a833e4e6d743c4b8a954718..9f70d811619d32d12be2f2eb95b06626bdf71b44 100644 (file)
@@ -89,6 +89,12 @@ ConfGetNodeOrCreate(char *name, int final)
                 goto end;
             }
             node->name = SCStrdup(key);
+            if (unlikely(node->name == NULL)) {
+                ConfNodeFree(node);
+                SCLogWarning(SC_ERR_MEM_ALLOC,
+                    "Failed to allocate memory for configuration.");
+                goto end;
+            }
             node->parent = parent;
             node->final = final;
             TAILQ_INSERT_TAIL(&parent->head, node, next);
@@ -226,6 +232,9 @@ ConfSet(char *name, char *val)
     if (node->val != NULL)
         SCFree(node->val);
     node->val = SCStrdup(val);
+    if (unlikely(node->val == NULL)) {
+        return 0;
+    }
     return 1;
 }
 
@@ -253,6 +262,9 @@ ConfSetFinal(char *name, char *val)
     if (node->val != NULL)
         SCFree(node->val);
     node->val = SCStrdup(val);
+    if (unlikely(node->val == NULL)) {
+        return 0;
+    }
     node->final = 1;
     return 1;
 }
@@ -638,6 +650,9 @@ ConfNodeDump(ConfNode *node, const char *prefix)
     level++;
     TAILQ_FOREACH(child, &node->head, next) {
         name[level] = SCStrdup(child->name);
+        if (unlikely(name[level] == NULL)) {
+            continue;
+        }
         if (prefix == NULL) {
             printf("%s = %s\n", ConfPrintNameArray(name, level),
                 child->val);
index 5238058b5a16a8d0ff4f2f5f78526e3ccc2ebd00..4b4f5369e4542a6e5e3963e5e6e1b631930a4714 100644 (file)
@@ -1064,6 +1064,10 @@ int SCPerfAddToClubbedTMTable(char *tm_name, SCPerfContext *pctx)
         }
         temp->head[0] = pctx;
         temp->tm_name = SCStrdup(tm_name);
+        if (unlikely(temp->tm_name == NULL)) {
+            SCMutexUnlock(&sc_perf_op_ctx->pctmi_lock);
+            return 0;
+        }
 
         if (prev == NULL)
             sc_perf_op_ctx->pctmi = temp;
index aa630b61a189429b0af2a1890ebc45fae536bc91..13000503b46a61577abab4220dd98bc7166faf0a 100644 (file)
@@ -184,6 +184,8 @@ static int DetectFlowvarSetup (DetectEngineCtx *de_ctx, Signature *s, char *raws
     fd->flags = contentflags;
 
     fd->name = SCStrdup(varname);
+    if (unlikely(fd->name == NULL))
+        goto error;
     fd->idx = VariableNameGetIdx(de_ctx, varname, DETECT_FLOWVAR);
 
     /* Okay so far so good, lets get this into a SigMatch
index 929fc6558b5d79498ab5e810f6714eb79c39fa66..6b7fad46b7f86f19996604dcc15dab037192d4be 100644 (file)
@@ -258,7 +258,7 @@ static DetectTlsData *DetectTlsSubjectParse (char *str)
     int ret = 0, res = 0;
     int ov[MAX_SUBSTRINGS];
     const char *str_ptr;
-    char *orig;
+    char *orig = NULL;
     char *tmp_str;
     uint32_t flag = 0;
 
@@ -304,6 +304,9 @@ static DetectTlsData *DetectTlsSubjectParse (char *str)
     }
 
     tls->subject = SCStrdup(tmp_str);
+    if (unlikely(tls->subject == NULL)) {
+        goto error;
+    }
 
     SCFree(orig);
 
@@ -312,6 +315,8 @@ static DetectTlsData *DetectTlsSubjectParse (char *str)
     return tls;
 
 error:
+    if (orig != NULL)
+        SCFree(orig);
     if (tls != NULL)
         DetectTlsSubjectFree(tls);
     return NULL;
@@ -459,7 +464,7 @@ static DetectTlsData *DetectTlsIssuerDNParse(char *str)
     int ret = 0, res = 0;
     int ov[MAX_SUBSTRINGS];
     const char *str_ptr;
-    char *orig;
+    char *orig = NULL;
     char *tmp_str;
     uint32_t flag = 0;
 
@@ -506,6 +511,9 @@ static DetectTlsData *DetectTlsIssuerDNParse(char *str)
     }
 
     tls->issuerdn = SCStrdup(tmp_str);
+    if (unlikely(tls->issuerdn == NULL)) {
+        goto error;
+    }
 
     SCFree(orig);
 
@@ -514,6 +522,8 @@ static DetectTlsData *DetectTlsIssuerDNParse(char *str)
     return tls;
 
 error:
+    if (orig != NULL)
+        SCFree(orig);
     if (tls != NULL)
         DetectTlsIssuerDNFree(tls);
     return NULL;
index 652379e04c7ccb5af7158158ee87aa01aca0ab36..d42df1ddacfc1d5ce86ccdcae367cd3b3dace75c 100644 (file)
@@ -592,10 +592,11 @@ int PcapLogOpenFileCtx(PcapLogData *pl)
     if (pl->filename != NULL)
         filename = pl->filename;
     else {
-        filename = pl->filename = SCMalloc(PATH_MAX);
-        if (filename == NULL) {
+        filename = SCMalloc(PATH_MAX);
+        if (unlikely(filename == NULL)) {
             return -1;
         }
+        pl->filename = filename;
     }
 
     /** get the time so we can have a filename with seconds since epoch */
index 33547c30001b6d4711ac6ffd104b753ae2602f0d..5010672023a65a017d423e71549fb42b1b1c4996 100644 (file)
@@ -47,17 +47,25 @@ OutputRegisterModule(char *name, char *conf_name,
     OutputCtx *(*InitFunc)(ConfNode *))
 {
     OutputModule *module = SCCalloc(1, sizeof(*module));
-    if (unlikely(module == NULL)) {
-        SCLogError(SC_ERR_FATAL, "Fatal error encountered in OutputRegisterModule. Exiting...");
-        exit(EXIT_FAILURE);
-    }
+    if (unlikely(module == NULL))
+        goto error;
 
     module->name = SCStrdup(name);
+    if (unlikely(module->name == NULL))
+        goto error;
     module->conf_name = SCStrdup(conf_name);
+    if (unlikely(module->conf_name == NULL))
+        goto error;
     module->InitFunc = InitFunc;
     TAILQ_INSERT_TAIL(&output_modules, module, entries);
 
     SCLogDebug("Output module \"%s\" registered.", name);
+
+    return;
+
+error:
+    SCLogError(SC_ERR_FATAL, "Fatal error encountered in OutputRegisterModule. Exiting...");
+    exit(EXIT_FAILURE);
 }
 
 /**
index 6acb24f84090f012faf022da4b25184ff827c0a0..1d7613b4d837324253a2859e5b3b02a224e54c06 100644 (file)
@@ -287,14 +287,25 @@ void *ParsePfringConfig(const char *iface)
     if (ConfGet("bpf-filter", &bpf_filter) == 1) {
         if (strlen(bpf_filter) > 0) {
             pfconf->bpf_filter = SCStrdup(bpf_filter);
-            SCLogDebug("Going to use command-line provided bpf filter %s",
-                       pfconf->bpf_filter);
+            if (unlikely(pfconf->bpf_filter == NULL)) {
+                SCLogError(SC_ERR_MEM_ALLOC,
+                           "Can't allocate BPF filter string");
+            } else {
+                SCLogDebug("Going to use command-line provided bpf filter %s",
+                           pfconf->bpf_filter);
+            }
         }
     } else {
         if (ConfGetChildValueWithDefault(if_root, if_default, "bpf-filter", &bpf_filter) == 1) {
             if (strlen(bpf_filter) > 0) {
                 pfconf->bpf_filter = SCStrdup(bpf_filter);
-                SCLogDebug("Going to use bpf filter %s", pfconf->bpf_filter);
+                if (unlikely(pfconf->bpf_filter == NULL)) {
+                    SCLogError(SC_ERR_MEM_ALLOC,
+                               "Can't allocate BPF filter string");
+                } else {
+                    SCLogDebug("Going to use bpf filter %s",
+                               pfconf->bpf_filter);
+                }
             }
         }
     }
index fd198c84ff4291147bbc2a477f86fc37de091cff..8a3d017f438e72cc7854645fea65ace8c0268a32 100644 (file)
@@ -320,6 +320,10 @@ TmEcode UnixSocketPcapFilesCheck(void *data)
             }
         }
         this->currentfile = SCStrdup(cfile->filename);
+        if (unlikely(this->currentfile == NULL)) {
+            SCLogError(SC_ERR_MEM_ALLOC, "Failed file name allocation");
+            return TM_ECODE_FAILED;
+        }
         PcapFilesFree(cfile);
         SCPerfInitCounterApi();
         DefragInit();
index 5f20ed974a7e8ab4580a0a44cfb333a572120b86..be6ce36ce0e6ef95808d942011a2d6fd01f7ca0a 100644 (file)
@@ -374,7 +374,15 @@ void RunModeRegisterNewRunMode(int runmode, const char *name,
 
     mode->runmode = runmode;
     mode->name = SCStrdup(name);
+    if (unlikely(mode->name == NULL)) {
+        SCLogError(SC_ERR_MEM_ALLOC, "Failed to allocate string");
+        exit(EXIT_FAILURE);
+    }
     mode->description = SCStrdup(description);
+    if (unlikely(mode->description == NULL)) {
+        SCLogError(SC_ERR_MEM_ALLOC, "Failed to allocate string");
+        exit(EXIT_FAILURE);
+    }
     mode->RunModeFunc = RunModeFunc;
 
     return;
index b8f835ec8d7124f24e70744039452b2176bb4a9a..7276c2997172c62418481ca1195f0afb3be2b4a5 100644 (file)
@@ -1014,6 +1014,10 @@ int MpipeLiveRegisterDevice(char *dev)
     }
 
     nd->dev = SCStrdup(dev);
+    if (unlikely(nd->dev == NULL)) {
+        SCFree(nd);
+        return -1;
+    }
     TAILQ_INSERT_TAIL(&mpipe_devices, nd, next);
 
     SCLogDebug("Mpipe device \"%s\" registered.", dev);
index f095603116e7f4bb05ff1643fbf4de4f89105733..90e8ef8af296b0df04193559aec2edeb97ba0bee 100644 (file)
@@ -377,6 +377,10 @@ TmEcode ReceivePfringThreadInit(ThreadVars *tv, void *initdata, void **data) {
     ptv->threads = 1;
 
     ptv->interface = SCStrdup(pfconf->iface);
+    if (unlikely(ptv->interface == NULL)) {
+        SCLogError(SC_ERR_MEM_ALLOC, "Unable to allocate device string");
+        SCReturnInt(TM_ECODE_FAILED);
+    }
 
     ptv->livedev = LiveGetDevice(pfconf->iface);
     if (ptv->livedev == NULL) {
@@ -452,13 +456,17 @@ TmEcode ReceivePfringThreadInit(ThreadVars *tv, void *initdata, void **data) {
 #ifdef HAVE_PFRING_SET_BPF_FILTER
     if (pfconf->bpf_filter) {
         ptv->bpf_filter = SCStrdup(pfconf->bpf_filter);
+        if (unlikely(ptv->bpf_filter == NULL)) {
+            SCLogError(SC_ERR_MEM_ALLOC, "Set PF_RING bpf filter failed.");
+        } else {
+            SCMutexLock(&pfring_bpf_set_filter_lock);
+            rc = pfring_set_bpf_filter(ptv->pd, ptv->bpf_filter);
+            SCMutexUnlock(&pfring_bpf_set_filter_lock);
 
-        SCMutexLock(&pfring_bpf_set_filter_lock);
-        rc = pfring_set_bpf_filter(ptv->pd, ptv->bpf_filter);
-        SCMutexUnlock(&pfring_bpf_set_filter_lock);
-
-        if (rc < 0) {
-            SCLogInfo("Set PF_RING bpf filter \"%s\" failed.", ptv->bpf_filter);
+            if (rc < 0) {
+                SCLogInfo("Set PF_RING bpf filter \"%s\" failed.",
+                          ptv->bpf_filter);
+            }
         }
     }
 #endif /* HAVE_PFRING_SET_BPF_FILTER */
index 4da2cc3d4ccc4378f327097ec275f9f18f581202..f994aae07684cc5a136ee1a785f12ad7a28241ae 100644 (file)
@@ -895,8 +895,13 @@ static inline void SCLogSetOPFilter(SCLogInitData *sc_lid, SCLogConfig *sc_lc)
 
     if (filter != NULL && strcmp(filter, "") != 0) {
         sc_lc->op_filter = SCStrdup(filter);
+        if (sc_lc->op_filter == NULL) {
+            printf("pcre filter alloc failed\n");
+            return;
+        }
         sc_lc->op_filter_regex = pcre_compile(filter, opts, &ep, &eo, NULL);
         if (sc_lc->op_filter_regex == NULL) {
+            SCFree(sc_lc->op_filter);
             printf("pcre compile of \"%s\" failed at offset %d : %s\n", filter,
                    eo, ep);
             return;
index 14459f004040804918909e86f4dd19fa85092390..55e7286df4bfe3caa958ac515e2abd1ce6c3754e 100644 (file)
@@ -48,6 +48,10 @@ int LiveRegisterDevice(char *dev)
     }
 
     pd->dev = SCStrdup(dev);
+    if (unlikely(pd->dev == NULL)) {
+        SCFree(pd);
+        return -1;
+    }
     SC_ATOMIC_INIT(pd->pkts);
     SC_ATOMIC_INIT(pd->drop);
     SC_ATOMIC_INIT(pd->invalid_checksums);
index 810d494098992047910e382740f40c2125382949..7131593cde60a79ba60a0d595af238b05ae30fd6 100644 (file)
@@ -76,6 +76,10 @@ void SCProtoNameInit()
             } else {
                 known_proto[proto] = SCStrdup(name);
             }
+            if (unlikely(known_proto[proto] == NULL)) {
+                SCLogError(SC_ERR_MEM_ALLOC, "Failed proto name allocation");
+                continue;
+            }
             int proto_len = strlen(known_proto[proto]);
             if (proto_len > 0 && known_proto[proto][proto_len - 1] == '\n')
                 known_proto[proto][proto_len - 1] = '\0';