From 61515d769e957ebd3cabefd4c3b11d52047336cd 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. (cherry picked from commit 7d6835958bbb6ddf2931c9e20f409eadfc8ca068) --- 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 53f408f07f..39cbf04d34 100644 --- a/src/detect-engine-prefilter-common.c +++ b/src/detect-engine-prefilter-common.c @@ -211,8 +211,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)); @@ -221,6 +221,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++) { -- 2.47.2