Skip to content

Commit bb1ed16

Browse files
inashivbvictorjulien
authored andcommitted
src: check retval of VarNameStoreRegister
VarNameStoreRegister can return 0 in case of any error conditions. Handle this case in all the users of this function. It is an unlikely event so add branch assistance accordingly. Bug 8054
1 parent 6b3c21a commit bb1ed16

8 files changed

Lines changed: 64 additions & 17 deletions

File tree

src/detect-flowbits.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,10 @@ static int FlowbitOrAddData(DetectEngineCtx *de_ctx, DetectFlowbitsData *cd, cha
134134
if (unlikely(cd->or_list == NULL))
135135
return -1;
136136
for (uint8_t j = 0; j < cd->or_list_size ; j++) {
137-
cd->or_list[j] = VarNameStoreRegister(strarr[j], VAR_TYPE_FLOW_BIT);
137+
uint32_t varname_id = VarNameStoreRegister(strarr[j], VAR_TYPE_FLOW_BIT);
138+
if (unlikely(varname_id == 0))
139+
return -1;
140+
cd->or_list[j] = varname_id;
138141
de_ctx->max_fb_id = MAX(cd->or_list[j], de_ctx->max_fb_id);
139142
}
140143

@@ -349,7 +352,10 @@ int DetectFlowbitSetup (DetectEngineCtx *de_ctx, Signature *s, const char *rawst
349352
}
350353
cd->cmd = fb_cmd;
351354
} else {
352-
cd->idx = VarNameStoreRegister(fb_name, VAR_TYPE_FLOW_BIT);
355+
uint32_t varname_id = VarNameStoreRegister(fb_name, VAR_TYPE_FLOW_BIT);
356+
if (unlikely(varname_id == 0))
357+
goto error;
358+
cd->idx = varname_id;
353359
de_ctx->max_fb_id = MAX(cd->idx, de_ctx->max_fb_id);
354360
cd->cmd = fb_cmd;
355361
cd->or_list_size = 0;
@@ -1610,6 +1616,7 @@ static int FlowBitsTestSig06(void)
16101616
FAIL_IF_NULL(s);
16111617

16121618
idx = VarNameStoreRegister("myflow", VAR_TYPE_FLOW_BIT);
1619+
FAIL_IF_NOT(idx);
16131620
SigGroupBuild(de_ctx);
16141621
DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
16151622

@@ -1684,6 +1691,7 @@ static int FlowBitsTestSig07(void)
16841691
FAIL_IF_NULL(s);
16851692

16861693
idx = VarNameStoreRegister("myflow", VAR_TYPE_FLOW_BIT);
1694+
FAIL_IF_NOT(idx);
16871695
SigGroupBuild(de_ctx);
16881696
DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
16891697

@@ -1759,6 +1767,7 @@ static int FlowBitsTestSig08(void)
17591767
FAIL_IF_NULL(s);
17601768

17611769
idx = VarNameStoreRegister("myflow", VAR_TYPE_FLOW_BIT);
1770+
FAIL_IF_NOT(idx);
17621771
SigGroupBuild(de_ctx);
17631772
DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
17641773

src/detect-flowint.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,10 @@ static DetectFlowintData *DetectFlowintParse(DetectEngineCtx *de_ctx, const char
334334
SCLogError("malloc from strdup failed");
335335
goto error;
336336
}
337-
sfd->idx = VarNameStoreRegister(varname, VAR_TYPE_FLOW_INT);
337+
uint32_t varname_id = VarNameStoreRegister(varname, VAR_TYPE_FLOW_INT);
338+
if (unlikely(varname_id == 0))
339+
goto error;
340+
sfd->idx = varname_id;
338341
SCLogDebug("sfd->name %s id %u", sfd->name, sfd->idx);
339342
sfd->modifier = modifier;
340343

src/detect-flowvar.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (C) 2007-2020 Open Information Security Foundation
1+
/* Copyright (C) 2007-2025 Open Information Security Foundation
22
*
33
* You can copy, redistribute or modify this Program under the terms of
44
* the GNU General Public License version 2 as published by the Free
@@ -178,7 +178,10 @@ static int DetectFlowvarSetup (DetectEngineCtx *de_ctx, Signature *s, const char
178178
fd->name = SCStrdup(varname);
179179
if (unlikely(fd->name == NULL))
180180
goto error;
181-
fd->idx = VarNameStoreRegister(varname, VAR_TYPE_FLOW_VAR);
181+
uint32_t varname_id = VarNameStoreRegister(varname, VAR_TYPE_FLOW_VAR);
182+
if (unlikely(varname_id == 0))
183+
goto error;
184+
fd->idx = varname_id;
182185

183186
/* Okay so far so good, lets get this into a SigMatch
184187
* and put it in the Signature. */

src/detect-hostbits.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,10 @@ int DetectHostbitSetup (DetectEngineCtx *de_ctx, Signature *s, const char *rawst
395395
if (unlikely(cd == NULL))
396396
goto error;
397397

398-
cd->idx = VarNameStoreRegister(fb_name, VAR_TYPE_HOST_BIT);
398+
uint32_t varname_id = VarNameStoreRegister(fb_name, VAR_TYPE_HOST_BIT);
399+
if (unlikely(varname_id == 0))
400+
goto error;
401+
cd->idx = varname_id;
399402
cd->cmd = fb_cmd;
400403
cd->tracker = hb_dir;
401404
cd->type = VAR_TYPE_HOST_BIT;

src/detect-lua.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (C) 2007-2024 Open Information Security Foundation
1+
/* Copyright (C) 2007-2025 Open Information Security Foundation
22
*
33
* You can copy, redistribute or modify this Program under the terms of
44
* the GNU General Public License version 2 as published by the Free
@@ -575,6 +575,8 @@ static int DetectLuaSetupPrime(DetectEngineCtx *de_ctx, DetectLuaData *ld, const
575575
}
576576

577577
uint32_t idx = VarNameStoreRegister(value, VAR_TYPE_FLOW_VAR);
578+
if (unlikely(idx == 0))
579+
goto error;
578580
ld->flowvar[ld->flowvars++] = idx;
579581
SCLogDebug("script uses flowvar %u with script id %u", idx, ld->flowvars - 1);
580582
}
@@ -597,6 +599,8 @@ static int DetectLuaSetupPrime(DetectEngineCtx *de_ctx, DetectLuaData *ld, const
597599
}
598600

599601
uint32_t idx = VarNameStoreRegister(value, VAR_TYPE_FLOW_INT);
602+
if (unlikely(idx == 0))
603+
goto error;
600604
ld->flowint[ld->flowints++] = idx;
601605
SCLogDebug("script uses flowint %u with script id %u", idx, ld->flowints - 1);
602606
}

src/detect-pcre.c

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -815,21 +815,30 @@ static int DetectPcreParseCapture(const char *regexstr, DetectEngineCtx *de_ctx,
815815
return -1;
816816

817817
} else if (strncmp(name_array[name_idx], "flow:", 5) == 0) {
818-
pd->capids[pd->idx] =
818+
uint32_t varname_id =
819819
VarNameStoreRegister(name_array[name_idx] + 5, VAR_TYPE_FLOW_VAR);
820+
if (unlikely(varname_id == 0))
821+
return -1;
822+
pd->capids[pd->idx] = varname_id;
820823
pd->captypes[pd->idx] = VAR_TYPE_FLOW_VAR;
821824
pd->idx++;
822825

823826
} else if (strncmp(name_array[name_idx], "pkt:", 4) == 0) {
824-
pd->capids[pd->idx] =
827+
uint32_t varname_id =
825828
VarNameStoreRegister(name_array[name_idx] + 4, VAR_TYPE_PKT_VAR);
829+
if (unlikely(varname_id == 0))
830+
return -1;
831+
pd->capids[pd->idx] = varname_id;
826832
pd->captypes[pd->idx] = VAR_TYPE_PKT_VAR;
827833
SCLogDebug("id %u type %u", pd->capids[pd->idx], pd->captypes[pd->idx]);
828834
pd->idx++;
829835

830836
} else if (strncmp(name_array[name_idx], "alert:", 6) == 0) {
831-
pd->capids[pd->idx] =
837+
uint32_t varname_id =
832838
VarNameStoreRegister(name_array[name_idx] + 6, VAR_TYPE_ALERT_VAR);
839+
if (unlikely(varname_id == 0))
840+
return -1;
841+
pd->capids[pd->idx] = varname_id;
833842
pd->captypes[pd->idx] = VAR_TYPE_ALERT_VAR;
834843
pd->idx++;
835844

@@ -890,16 +899,25 @@ static int DetectPcreParseCapture(const char *regexstr, DetectEngineCtx *de_ctx,
890899
}
891900

892901
if (strcmp(type_str, "pkt") == 0) {
893-
pd->capids[pd->idx] = VarNameStoreRegister((char *)capture_str, VAR_TYPE_PKT_VAR);
902+
uint32_t varname_id = VarNameStoreRegister((char *)capture_str, VAR_TYPE_PKT_VAR);
903+
if (unlikely(varname_id == 0))
904+
return -1;
905+
pd->capids[pd->idx] = varname_id;
894906
pd->captypes[pd->idx] = VAR_TYPE_PKT_VAR;
895907
SCLogDebug("id %u type %u", pd->capids[pd->idx], pd->captypes[pd->idx]);
896908
pd->idx++;
897909
} else if (strcmp(type_str, "flow") == 0) {
898-
pd->capids[pd->idx] = VarNameStoreRegister((char *)capture_str, VAR_TYPE_FLOW_VAR);
910+
uint32_t varname_id = VarNameStoreRegister((char *)capture_str, VAR_TYPE_FLOW_VAR);
911+
if (unlikely(varname_id == 0))
912+
return -1;
913+
pd->capids[pd->idx] = varname_id;
899914
pd->captypes[pd->idx] = VAR_TYPE_FLOW_VAR;
900915
pd->idx++;
901916
} else if (strcmp(type_str, "alert") == 0) {
902-
pd->capids[pd->idx] = VarNameStoreRegister((char *)capture_str, VAR_TYPE_ALERT_VAR);
917+
uint32_t varname_id = VarNameStoreRegister((char *)capture_str, VAR_TYPE_ALERT_VAR);
918+
if (unlikely(varname_id == 0))
919+
return -1;
920+
pd->capids[pd->idx] = varname_id;
903921
pd->captypes[pd->idx] = VAR_TYPE_ALERT_VAR;
904922
pd->idx++;
905923
}

src/detect-pktvar.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ static int DetectPktvarSetup (DetectEngineCtx *de_ctx, Signature *s, const char
9494
size_t pcre2_len;
9595
uint8_t *content = NULL;
9696
uint16_t len = 0;
97-
97+
DetectPktvarData *cd = NULL;
9898
pcre2_match_data *match = NULL;
9999
int ret = DetectParsePcreExec(&parse_regex, &match, rawstr, 0, 0);
100100
if (ret != 3) {
@@ -138,7 +138,7 @@ static int DetectPktvarSetup (DetectEngineCtx *de_ctx, Signature *s, const char
138138
}
139139
pcre2_substring_free((PCRE2_UCHAR8 *)varcontent);
140140

141-
DetectPktvarData *cd = SCCalloc(1, sizeof(DetectPktvarData));
141+
cd = SCCalloc(1, sizeof(DetectPktvarData));
142142
if (unlikely(cd == NULL)) {
143143
pcre2_substring_free((PCRE2_UCHAR8 *)varname);
144144
SCFree(content);
@@ -147,7 +147,10 @@ static int DetectPktvarSetup (DetectEngineCtx *de_ctx, Signature *s, const char
147147

148148
cd->content = content;
149149
cd->content_len = len;
150-
cd->id = VarNameStoreRegister(varname, VAR_TYPE_PKT_VAR);
150+
uint32_t varname_id = VarNameStoreRegister(varname, VAR_TYPE_PKT_VAR);
151+
if (unlikely(varname_id == 0))
152+
goto error;
153+
cd->id = varname_id;
151154
pcre2_substring_free((PCRE2_UCHAR8 *)varname);
152155

153156
/* Okay so far so good, lets get this into a SigMatch
@@ -161,6 +164,7 @@ static int DetectPktvarSetup (DetectEngineCtx *de_ctx, Signature *s, const char
161164
return 0;
162165

163166
error:
167+
DetectPktvarFree(de_ctx, cd);
164168
if (match) {
165169
pcre2_match_data_free(match);
166170
}

src/detect-xbits.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,10 @@ static int DetectXbitParse(DetectEngineCtx *de_ctx,
368368
if (unlikely(cd == NULL))
369369
return -1;
370370

371-
cd->idx = VarNameStoreRegister(fb_name, var_type);
371+
uint32_t varname_id = VarNameStoreRegister(fb_name, var_type);
372+
if (unlikely(varname_id == 0))
373+
goto error;
374+
cd->idx = varname_id;
372375
cd->cmd = fb_cmd;
373376
cd->tracker = hb_dir;
374377
cd->type = var_type;

0 commit comments

Comments
 (0)