From: Eric Leblond Date: Mon, 7 Mar 2016 08:16:41 +0000 (+0100) Subject: util-radix-tree: fix memleak X-Git-Tag: suricata-3.0.1RC1~51 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f001c10ac4239bef5492fead14bcb4f830458028;p=thirdparty%2Fsuricata.git util-radix-tree: fix memleak Logic used when adding a new prefix to a node was not correct as we were allocating a prefix that could be at the end unused. This patch is updating the code to have a complete creation to be done if and only if we are needing the complete object. In the other cases, it was enough to use the function input values. This fixes: 104 (48 direct, 56 indirect) bytes in 2 blocks are definitely lost in loss record 184 of 327 at 0x4C29C0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) by 0x9C2DAD: SCRadixCreatePrefix (util-radix-tree.c:144) by 0x9AFA5B: SCRadixAddKey (util-radix-tree.c:522) by 0x9B1A4D: SCRadixAddKeyIPV4Netblock (util-radix-tree.c:897) by 0x67C824: IPOnlyPrepare (detect-engine-iponly.c:1197) by 0x55172B: SigAddressPrepareStage2 (detect.c:3534) by 0x5486F4: SigGroupBuild (detect.c:4671) by 0x547C87: SigLoadSignatures (detect.c:538) by 0x8FB5D0: LoadSignatures (suricata.c:1976) by 0x8F3B32: main (suricata.c:2342) --- diff --git a/src/util-radix-tree.c b/src/util-radix-tree.c index cbf49c7de7..2304d593f4 100644 --- a/src/util-radix-tree.c +++ b/src/util-radix-tree.c @@ -496,9 +496,6 @@ static SCRadixNode *SCRadixAddKey(uint8_t *key_stream, uint16_t key_bitlen, SCRadixNode *parent = NULL; SCRadixNode *inter_node = NULL; SCRadixNode *bottom_node = NULL; - - SCRadixPrefix *prefix = NULL; - void *ptmp; uint8_t *stream = NULL; @@ -519,14 +516,14 @@ static SCRadixNode *SCRadixAddKey(uint8_t *key_stream, uint16_t key_bitlen, /* chop the ip address against a netmask */ MaskIPNetblock(key_stream, netmask, key_bitlen); - if ( (prefix = SCRadixCreatePrefix(key_stream, key_bitlen, user, - netmask)) == NULL) { - SCLogError(SC_ERR_RADIX_TREE_GENERIC, "Error creating prefix"); - return NULL; - } - /* the very first element in the radix tree */ if (tree->head == NULL) { + SCRadixPrefix *prefix = NULL; + if ( (prefix = SCRadixCreatePrefix(key_stream, key_bitlen, user, + netmask)) == NULL) { + SCLogError(SC_ERR_RADIX_TREE_GENERIC, "Error creating prefix"); + return NULL; + } node = SCRadixCreateNode(); if (node == NULL) return NULL; @@ -558,8 +555,8 @@ static SCRadixNode *SCRadixAddKey(uint8_t *key_stream, uint16_t key_bitlen, } node = tree->head; - stream = prefix->stream; - bitlen = prefix->bitlen; + stream = key_stream; + bitlen = key_bitlen; /* we walk down the tree only when we satisfy 2 conditions. The first one * being the incoming prefix is shorter than the differ bit of the current @@ -695,13 +692,19 @@ static SCRadixNode *SCRadixAddKey(uint8_t *key_stream, uint16_t key_bitlen, } } } else { - node->prefix = SCRadixCreatePrefix(prefix->stream, prefix->bitlen, + node->prefix = SCRadixCreatePrefix(key_stream, key_bitlen, user, 255); } return node; } /* create the leaf node for the new key */ + SCRadixPrefix *prefix = NULL; + if ( (prefix = SCRadixCreatePrefix(key_stream, key_bitlen, user, + netmask)) == NULL) { + SCLogError(SC_ERR_RADIX_TREE_GENERIC, "Error creating prefix"); + return NULL; + } new_node = SCRadixCreateNode(); new_node->prefix = prefix; new_node->bit = prefix->bitlen;