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)
pull/1910/head
Eric Leblond 10 years ago
parent 9c7e18dc89
commit f001c10ac4

@ -496,9 +496,6 @@ static SCRadixNode *SCRadixAddKey(uint8_t *key_stream, uint16_t key_bitlen,
SCRadixNode *parent = NULL; SCRadixNode *parent = NULL;
SCRadixNode *inter_node = NULL; SCRadixNode *inter_node = NULL;
SCRadixNode *bottom_node = NULL; SCRadixNode *bottom_node = NULL;
SCRadixPrefix *prefix = NULL;
void *ptmp; void *ptmp;
uint8_t *stream = NULL; 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 */ /* chop the ip address against a netmask */
MaskIPNetblock(key_stream, netmask, key_bitlen); 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 */ /* the very first element in the radix tree */
if (tree->head == NULL) { 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(); node = SCRadixCreateNode();
if (node == NULL) if (node == NULL)
return NULL; return NULL;
@ -558,8 +555,8 @@ static SCRadixNode *SCRadixAddKey(uint8_t *key_stream, uint16_t key_bitlen,
} }
node = tree->head; node = tree->head;
stream = prefix->stream; stream = key_stream;
bitlen = prefix->bitlen; bitlen = key_bitlen;
/* we walk down the tree only when we satisfy 2 conditions. The first one /* 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 * 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 { } else {
node->prefix = SCRadixCreatePrefix(prefix->stream, prefix->bitlen, node->prefix = SCRadixCreatePrefix(key_stream, key_bitlen,
user, 255); user, 255);
} }
return node; return node;
} }
/* create the leaf node for the new key */ /* 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 = SCRadixCreateNode();
new_node->prefix = prefix; new_node->prefix = prefix;
new_node->bit = prefix->bitlen; new_node->bit = prefix->bitlen;

Loading…
Cancel
Save