summaryrefslogtreecommitdiffstats
path: root/src/tests/fuzz/fuzz_siginit.c
blob: 80514b2d9a5244536b95a679fdeac9ccf929223f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
 * @file
 * @author Philippe Antoine <contact@catenacyber.fr>
 * fuzz target for SigInit
 */


#include "suricata-common.h"
#include "util-reference-config.h"
#include "util-classification-config.h"
#include "detect-engine.h"
#include "detect-parse.h"

int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);

static uint32_t cnt = 0;
DetectEngineCtx *de_ctx = NULL;

int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
{
    if (de_ctx == NULL) {
        setenv("SC_LOG_OP_IFACE", "file", 0);
        setenv("SC_LOG_FILE", "/dev/null", 0);
        //global init
        InitGlobal();
        run_mode = RUNMODE_UNITTEST;
        MpmTableSetup();
        SpmTableSetup();
        EngineModeSetIDS();
        SigTableSetup();
    }
    if (cnt++ == 1024) {
        DetectEngineCtxFree(de_ctx);
        de_ctx = NULL;
        cnt = 0;
    }
    if (de_ctx == NULL) {
        de_ctx = DetectEngineCtxInit();
        BUG_ON(de_ctx == NULL);
        de_ctx->flags |= DE_QUIET;
        de_ctx->rule_file = (char *)"fuzzer";
    }

    char * buffer = malloc(size+1);
    if (buffer) {
        memcpy(buffer, data, size);
        //null terminate string
        buffer[size] = 0;
        Signature *s = SigInit(de_ctx, buffer);
        free(buffer);
        if (s && s->next) {
            SigFree(de_ctx, s->next);
            s->next = NULL;
        }
        SigFree(de_ctx, s);
    }

    return 0;
}