Skip to content

Commit

Permalink
router: fix invalid routing due to invalid memory access (#967)
Browse files Browse the repository at this point in the history
Recently the router was adapted to use non-NULL terminated buffers
coming from a Task that was referencing a ChunkIO memory map. This
generated some issues since the logic of the matching routine was
not ported properly plus a future issue with referenced Tags could
lead to a corruption.

This patch gets back to the original mechanism where now a Tag
contains a NULL byte at the end.

Signed-off-by: Eduardo Silva <[email protected]>
  • Loading branch information
edsiper committed Dec 20, 2018
1 parent 463f0c1 commit c5ce9e9
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions src/flb_router.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@

#include <string.h>

static inline int router_match(const char *tag, int tag_len, const char *match,
off_t off, void *match_r)
/* wildcard support */
/* tag and match should be null terminated. */
static inline int router_match(const char *tag, int tag_len,
const char *match,
void *match_r)
{
int ret = 0;
int ret = FLB_FALSE;
char *pos = NULL;

#ifdef FLB_HAVE_REGEX
Expand Down Expand Up @@ -61,34 +64,32 @@ static inline int router_match(const char *tag, int tag_len, const char *match,
break;
}

while ((pos = memchr(tag + off, (int) *match, tag_len - off))) {
off += pos - (tag + off);

while ((pos = strchr(tag, (int) *match))) {
#ifndef FLB_HAVE_REGEX
if (router_match(tag, tag_len, match, off, NULL)) {
if (router_match(pos, tag_len, match, NULL)) {
#else
/* We don't need to pass the regex recursively,
* we matched in order above
*/
if (router_match(tag, tag_len, match, off, NULL)) {
if (router_match(pos, tag_len, match, NULL)) {
#endif
ret = 1;
break;
}
off++;
tag = pos+1;
}
break;
}
else if (tag[off] != *match ) {
else if (*tag != *match ) {
/* mismatch! */
break;
}
else if (off + 1 >= tag_len) {
else if (*tag == '\0'){
/* end of tag. so matched! */
ret = 1;
break;
}
off++;
tag++;
match++;
}

Expand All @@ -100,13 +101,13 @@ int flb_router_match(const char *tag, int tag_len,
const char *match,
struct flb_regex *match_regex)
{
return router_match(tag, tag_len, match, 0, match_regex);
return router_match(tag, tag_len, match, match_regex);
}

#else
int flb_router_match(const char *tag, int tag_len, const char *match)
{
return router_match(tag, tag_len, match, 0, NULL);
return router_match(tag, tag_len, match, NULL);
}
#endif

Expand Down

0 comments on commit c5ce9e9

Please sign in to comment.