From 7d6835958bbb6ddf2931c9e20f409eadfc8ca068 Mon Sep 17 00:00:00 2001 From: Victor Julien Date: Wed, 3 Mar 2021 13:41:26 +0100 Subject: [PATCH] detect/prefilter: fix null ptr deref on invalid rule A bad rule 'icode:<0; prefilter;' would trigger a null ptr deref in ApplyToU8Hash. Bug #4375. --- src/detect-engine-prefilter-common.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/detect-engine-prefilter-common.c b/src/detect-engine-prefilter-common.c index 931778976e..a0dafe2364 100644 --- a/src/detect-engine-prefilter-common.c +++ b/src/detect-engine-prefilter-common.c @@ -212,8 +212,8 @@ SetupEngineForPacketHeaderPrefilterPacketU8HashCtx(DetectEngineCtx *de_ctx, if (ctx == NULL) return -1; - int i; - for (i = 0; i < 256; i++) { + int set_cnt = 0; + for (int i = 0; i < 256; i++) { if (counts[i] == 0) continue; ctx->array[i] = SCCalloc(1, sizeof(SigsArray)); @@ -222,6 +222,12 @@ SetupEngineForPacketHeaderPrefilterPacketU8HashCtx(DetectEngineCtx *de_ctx, ctx->array[i]->cnt = counts[i]; ctx->array[i]->sigs = SCCalloc(ctx->array[i]->cnt, sizeof(SigIntId)); BUG_ON(ctx->array[i]->sigs == NULL); + set_cnt++; + } + if (set_cnt == 0) { + /* not an error */ + PrefilterPacketU8HashCtxFree(ctx); + return 0; } for (sig = 0; sig < sgh->sig_cnt; sig++) {