]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
Enforce memcap limit before allocating hash table in host and flow engines.
authorVictor Julien <victor@inliniac.net>
Thu, 22 Mar 2012 16:02:25 +0000 (17:02 +0100)
committerVictor Julien <victor@inliniac.net>
Thu, 22 Mar 2012 16:02:25 +0000 (17:02 +0100)
src/flow.c
src/host.c

index 2bc884a329e6d51181b9bcb06205cd461e4185c0..80035b153aa4b5aa3f8ca7a3a49434a331a2be5d 100644 (file)
@@ -391,6 +391,16 @@ void FlowInitConfig(char quiet)
                flow_config.hash_size, flow_config.prealloc);
 
     /* alloc hash memory */
+    uint64_t hash_size = flow_config.hash_size * sizeof(FlowBucket);
+    if (!(FLOW_CHECK_MEMCAP(hash_size))) {
+        SCLogError(SC_ERR_FLOW_INIT, "allocating flow hash failed: "
+                "max flow memcap is smaller than projected hash size. "
+                "Memcap: %"PRIu64", Hash table size %"PRIu64". Calculate "
+                "total hash size by multiplying \"flow.hash-size\" with %"PRIuMAX", "
+                "which is the hash bucket size.", flow_config.memcap, hash_size,
+                sizeof(FlowBucket));
+        exit(EXIT_FAILURE);
+    }
     flow_hash = SCCalloc(flow_config.hash_size, sizeof(FlowBucket));
     if (flow_hash == NULL) {
         SCLogError(SC_ERR_FATAL, "Fatal error encountered in FlowInitConfig. Exiting...");
index 5737d5a1fce1bec6f9158181c3065727b7dc5f98..f0136ca740d432fabda5b4fdc0d04f151f2f4977 100644 (file)
@@ -109,7 +109,7 @@ void HostClearMemory(Host *h) {
 
 #define HOST_DEFAULT_HASHSIZE 4096
 #define HOST_DEFAULT_MEMCAP 16777216
-#define HOST_DEFAULT_PREALLOC 10000
+#define HOST_DEFAULT_PREALLOC 1000
 
 /** \brief initialize the configuration
  *  \warning Not thread safe */
@@ -166,6 +166,16 @@ void HostInitConfig(char quiet)
                host_config.hash_size, host_config.prealloc);
 
     /* alloc hash memory */
+    uint64_t hash_size = host_config.hash_size * sizeof(HostHashRow);
+    if (!(HOST_CHECK_MEMCAP(hash_size))) {
+        SCLogError(SC_ERR_HOST_INIT, "allocating host hash failed: "
+                "max host memcap is smaller than projected hash size. "
+                "Memcap: %"PRIu64", Hash table size %"PRIu64". Calculate "
+                "total hash size by multiplying \"host.hash-size\" with %"PRIuMAX", "
+                "which is the hash bucket size.", host_config.memcap, hash_size,
+                sizeof(HostHashRow));
+        exit(EXIT_FAILURE);
+    }
     host_hash = SCCalloc(host_config.hash_size, sizeof(HostHashRow));
     if (host_hash == NULL) {
         SCLogError(SC_ERR_FATAL, "Fatal error encountered in HostInitConfig. Exiting...");