Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/datasets.c
Original file line number Diff line number Diff line change
Expand Up @@ -1373,7 +1373,7 @@ static int DatasetOpSerialized(Dataset *set, const char *string, DatasetOpFunc D

switch (set->type) {
case DATASET_TYPE_STRING: {
uint32_t decoded_size = SCBase64DecodeBufferSize(strlen(string));
uint32_t decoded_size = SCBase64DecodeBufferSize((uint32_t)strlen(string));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comes from unix socket

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we impose some limit?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding one as there is also the stack allocation below

uint8_t decoded[decoded_size];
uint32_t num_decoded = SCBase64Decode(
(const uint8_t *)string, strlen(string), SCBase64ModeStrict, decoded);
Expand Down
23 changes: 13 additions & 10 deletions src/detect-bsize.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,37 +40,38 @@
/*prototypes*/
static int DetectBsizeSetup (DetectEngineCtx *, Signature *, const char *);
static void DetectBsizeFree (DetectEngineCtx *, void *);
static int SigParseGetMaxBsize(const DetectU64Data *bsz);
static int SigParseGetMaxBsize(const DetectU64Data *bsz, uint64_t *bsize);
#ifdef UNITTESTS
static void DetectBsizeRegisterTests (void);
#endif

bool DetectBsizeValidateContentCallback(Signature *s, const SignatureInitDataBuffer *b)
{
int bsize = -1;
uint64_t bsize;
int max_bsize = 0;
const DetectU64Data *bsz;
for (const SigMatch *sm = b->head; sm != NULL; sm = sm->next) {
if (sm->type == DETECT_BSIZE) {
bsz = (const DetectU64Data *)sm->ctx;
bsize = SigParseGetMaxBsize(bsz);
max_bsize = SigParseGetMaxBsize(bsz, &bsize);
break;
}
}

if (bsize == -1) {
if (max_bsize == -1) {
return true;
}

uint64_t needed;
if (bsize >= 0) {
if (max_bsize == 0 && bsize >= 0) {
int len, offset;
SigParseRequiredContentSize(s, bsize, b->head, &len, &offset);
SCLogDebug("bsize: %d; len: %d; offset: %d [%s]", bsize, len, offset, s->sig_str);
needed = len;
if (len > bsize) {
if ((uint64_t)len > bsize) {
goto value_error;
}
if ((len + offset) > bsize) {
if ((uint64_t)(len + offset) > bsize) {
needed += offset;
goto value_error;
}
Expand Down Expand Up @@ -157,14 +158,16 @@ int DetectBsizeMatch(const SigMatchCtx *ctx, const uint64_t buffer_size, bool eo
return 0;
}

static int SigParseGetMaxBsize(const DetectU64Data *bsz)
static int SigParseGetMaxBsize(const DetectU64Data *bsz, uint64_t *bsize)
{
switch (bsz->mode) {
case DETECT_UINT_LT:
case DETECT_UINT_EQ:
return bsz->arg1;
*bsize = bsz->arg1;
SCReturnInt(0);
case DETECT_UINT_RA:
return bsz->arg2;
*bsize = bsz->arg2;
SCReturnInt(0);
case DETECT_UINT_GT:
default:
SCReturnInt(-2);
Expand Down
2 changes: 1 addition & 1 deletion src/detect-byte-extract.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ int DetectByteExtractDoMatch(DetectEngineThreadCtx *det_ctx, const SigMatchData

ptr += extbytes;

det_ctx->buffer_offset = ptr - payload;
det_ctx->buffer_offset = (uint32_t)(ptr - payload);

*value = val;
SCLogDebug("extracted value is %"PRIu64, val);
Expand Down
2 changes: 1 addition & 1 deletion src/detect-bytejump.c
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ bool DetectBytejumpDoMatch(DetectEngineThreadCtx *det_ctx, const Signature *s,

/* Adjust the detection context to the jump location. */
DEBUG_VALIDATE_BUG_ON(jumpptr < payload);
det_ctx->buffer_offset = jumpptr - payload;
det_ctx->buffer_offset = (uint32_t)(jumpptr - payload);

SCReturnBool(true);
}
Expand Down
2 changes: 1 addition & 1 deletion src/detect-bytemath.c
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ int DetectByteMathDoMatch(DetectEngineThreadCtx *det_ctx, const DetectByteMathDa
break;
}

det_ctx->buffer_offset = ptr - payload;
det_ctx->buffer_offset = (uint32_t)(ptr - payload);

if (data->flags & DETECT_BYTEMATH_FLAG_BITMASK) {
val &= data->bitmask_val;
Expand Down
2 changes: 1 addition & 1 deletion src/detect-bytetest.c
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ static DetectBytetestData *DetectBytetestParse(
data->neg_op = true;
op_ptr = &args[1][1];
while (isspace((char)*op_ptr) || (*op_ptr == ',')) op_ptr++;
op_offset = op_ptr - &args[1][0];
op_offset = (uint32_t)(op_ptr - &args[1][0]);
} else {
data->neg_op = false;
}
Expand Down
11 changes: 5 additions & 6 deletions src/detect-content.c
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ void DetectContentFree(DetectEngineCtx *de_ctx, void *ptr)
* - Negated content values are checked but not accumulated for the required size.
*/
void SigParseRequiredContentSize(
const Signature *s, const int max_size, const SigMatch *sm, int *len, int *offset)
const Signature *s, const uint64_t max_size, const SigMatch *sm, int *len, int *offset)
{
int max_offset = 0, total_len = 0;
bool first = true;
Expand All @@ -428,7 +428,7 @@ void SigParseRequiredContentSize(
if (cd->flags & DETECT_CONTENT_NEGATED) {
/* Check if distance/within cause max to be exceeded */
int check = total_len + cd->distance + cd->within;
if (max_size < check) {
if (max_size < (uint64_t)check) {
*len = check;
return;
}
Expand Down Expand Up @@ -457,12 +457,11 @@ bool DetectContentPMATCHValidateCallback(const Signature *s)
return true;
}

int max_right_edge_i = SigParseGetMaxDsize(s);
if (max_right_edge_i < 0) {
uint16_t max_right_edge_i;
if (SigParseGetMaxDsize(s, &max_right_edge_i) < 0) {
return true;
}

uint32_t max_right_edge = (uint32_t)max_right_edge_i;
uint32_t max_right_edge = max_right_edge_i;

int min_dsize_required = SigParseMaxRequiredDsize(s);
if (min_dsize_required >= 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/detect-content.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ void DetectContentPropagateLimits(Signature *s);

void DetectContentPatternPrettyPrint(const DetectContentData *cd, char *str, size_t str_len);
void SigParseRequiredContentSize(
const Signature *s, const int max, const SigMatch *sm, int *len, int *offset);
const Signature *s, const uint64_t max, const SigMatch *sm, int *len, int *offset);
int DetectContentConvertToNocase(DetectEngineCtx *de_ctx, DetectContentData *cd);

#endif /* SURICATA_DETECT_CONTENT_H */
16 changes: 9 additions & 7 deletions src/detect-dsize.c
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ static bool PrefilterDsizeIsPrefilterable(const Signature *s)
* \param s signature to get dsize value from
* \retval depth or negative value
*/
int SigParseGetMaxDsize(const Signature *s)
int SigParseGetMaxDsize(const Signature *s, uint16_t *dsize)
{
if (s->flags & SIG_FLAG_DSIZE && s->init_data->dsize_sm != NULL) {
const DetectU16Data *dd = (const DetectU16Data *)s->init_data->dsize_sm->ctx;
Expand All @@ -217,9 +217,11 @@ int SigParseGetMaxDsize(const Signature *s)
case DETECT_UINT_LT:
case DETECT_UINT_EQ:
case DETECT_UINT_NE:
return dd->arg1;
*dsize = dd->arg1;
SCReturnInt(0);
case DETECT_UINT_RA:
return dd->arg2;
*dsize = dd->arg2;
SCReturnInt(0);
case DETECT_UINT_GT:
default:
SCReturnInt(-2);
Expand Down Expand Up @@ -293,8 +295,8 @@ int SigParseMaxRequiredDsize(const Signature *s)
SCReturnInt(-1);
}

const int dsize = SigParseGetMaxDsize(s);
if (dsize < 0) {
uint16_t dsize;
if (SigParseGetMaxDsize(s, &dsize) < 0) {
/* nothing to do */
SCReturnInt(-1);
}
Expand Down Expand Up @@ -328,8 +330,8 @@ void SigParseApplyDsizeToContent(Signature *s)
if (s->flags & SIG_FLAG_DSIZE) {
SigParseSetDsizePair(s);

int dsize = SigParseGetMaxDsize(s);
if (dsize < 0) {
uint16_t dsize;
if (SigParseGetMaxDsize(s, &dsize) < 0) {
/* nothing to do */
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/detect-dsize.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
void DetectDsizeRegister (void);

int SigParseMaxRequiredDsize(const Signature *s);
int SigParseGetMaxDsize(const Signature *s);
int SigParseGetMaxDsize(const Signature *s, uint16_t *dsize);
void SigParseSetDsizePair(Signature *s);
void SigParseApplyDsizeToContent(Signature *s);

Expand Down
2 changes: 1 addition & 1 deletion src/detect-engine-analyzer.c
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ void SetupEngineAnalysis(DetectEngineCtx *de_ctx, bool *fp_analysis, bool *rule_
}

ea->file_prefix = NULL;
int cfg_prefix_len = strlen(de_ctx->config_prefix);
size_t cfg_prefix_len = strlen(de_ctx->config_prefix);
if (cfg_prefix_len > 0) {
/* length of prefix + NULL + "." */
ea->file_prefix = SCCalloc(1, cfg_prefix_len + 1 + 1);
Expand Down
64 changes: 50 additions & 14 deletions src/detect-engine-content-inspection.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,10 @@ static int DetectEngineContentInspectionInternal(DetectEngineThreadCtx *det_ctx,
int distance = cd->distance;
if (cd->flags & DETECT_CONTENT_DISTANCE) {
if (cd->flags & DETECT_CONTENT_DISTANCE_VAR) {
distance = det_ctx->byte_values[cd->distance];
if (det_ctx->byte_values[cd->distance] > UINT32_MAX) {
goto no_match;
}
distance = (uint32_t)det_ctx->byte_values[cd->distance];
}
if (distance < 0 && (uint32_t)(abs(distance)) > offset)
offset = 0;
Expand All @@ -170,7 +173,12 @@ static int DetectEngineContentInspectionInternal(DetectEngineThreadCtx *det_ctx,
if (cd->flags & DETECT_CONTENT_WITHIN) {
if (cd->flags & DETECT_CONTENT_WITHIN_VAR) {
if ((int32_t)depth > (int32_t)(prev_buffer_offset + det_ctx->byte_values[cd->within] + distance)) {
depth = prev_buffer_offset + det_ctx->byte_values[cd->within] + distance;
if (prev_buffer_offset + det_ctx->byte_values[cd->within] + distance >
UINT32_MAX) {
goto no_match;
}
depth = (uint32_t)(prev_buffer_offset +
det_ctx->byte_values[cd->within] + distance);
}
} else {
if ((int32_t)depth > (int32_t)(prev_buffer_offset + cd->within + distance)) {
Expand All @@ -194,7 +202,10 @@ static int DetectEngineContentInspectionInternal(DetectEngineThreadCtx *det_ctx,

if (cd->flags & DETECT_CONTENT_DEPTH_VAR) {
if ((det_ctx->byte_values[cd->depth] + prev_buffer_offset) < depth) {
depth = prev_buffer_offset + det_ctx->byte_values[cd->depth];
if (prev_buffer_offset + det_ctx->byte_values[cd->depth] > UINT32_MAX) {
goto no_match;
}
depth = (uint32_t)(prev_buffer_offset + det_ctx->byte_values[cd->depth]);
}
} else {
if (cd->depth != 0) {
Expand All @@ -207,8 +218,12 @@ static int DetectEngineContentInspectionInternal(DetectEngineThreadCtx *det_ctx,
}

if (cd->flags & DETECT_CONTENT_OFFSET_VAR) {
if (det_ctx->byte_values[cd->offset] > offset)
offset = det_ctx->byte_values[cd->offset];
if (det_ctx->byte_values[cd->offset] > offset) {
if (det_ctx->byte_values[cd->offset] > UINT32_MAX) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how would this happen? Can we avoid it from getting set? It seems painful to put all these checks here if we can avoid it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think with a rule using byte_extract with 8 bytes..?

goto no_match;
}
offset = (uint32_t)det_ctx->byte_values[cd->offset];
}
} else {
if (cd->offset > offset) {
offset = cd->offset;
Expand All @@ -218,7 +233,10 @@ static int DetectEngineContentInspectionInternal(DetectEngineThreadCtx *det_ctx,
} else { /* implied no relative matches */
/* set depth */
if (cd->flags & DETECT_CONTENT_DEPTH_VAR) {
depth = det_ctx->byte_values[cd->depth];
if (det_ctx->byte_values[cd->depth] > UINT32_MAX) {
goto no_match;
}
depth = (uint32_t)det_ctx->byte_values[cd->depth];
} else {
if (cd->depth != 0) {
depth = cd->depth;
Expand All @@ -236,9 +254,12 @@ static int DetectEngineContentInspectionInternal(DetectEngineThreadCtx *det_ctx,
}

/* set offset */
if (cd->flags & DETECT_CONTENT_OFFSET_VAR)
offset = det_ctx->byte_values[cd->offset];
else
if (cd->flags & DETECT_CONTENT_OFFSET_VAR) {
if (det_ctx->byte_values[cd->offset] > UINT32_MAX) {
goto no_match;
}
offset = (uint32_t)det_ctx->byte_values[cd->offset];
} else
offset = cd->offset;
prev_buffer_offset = 0;
}
Expand Down Expand Up @@ -493,13 +514,19 @@ static int DetectEngineContentInspectionInternal(DetectEngineThreadCtx *det_ctx,
uint64_t value = btd->value;
int32_t nbytes = btd->nbytes;
if (btflags & DETECT_BYTETEST_OFFSET_VAR) {
offset = det_ctx->byte_values[offset];
if (det_ctx->byte_values[offset] > UINT32_MAX) {
goto no_match;
}
offset = (uint32_t)det_ctx->byte_values[offset];
}
if (btflags & DETECT_BYTETEST_VALUE_VAR) {
value = det_ctx->byte_values[value];
}
if (btflags & DETECT_BYTETEST_NBYTES_VAR) {
nbytes = det_ctx->byte_values[nbytes];
if (det_ctx->byte_values[nbytes] > INT32_MAX) {
goto no_match;
}
nbytes = (int32_t)det_ctx->byte_values[nbytes];
}

/* if we have dce enabled we will have to use the endianness
Expand All @@ -525,11 +552,17 @@ static int DetectEngineContentInspectionInternal(DetectEngineThreadCtx *det_ctx,
int32_t nbytes;

if (bjflags & DETECT_BYTEJUMP_OFFSET_VAR) {
offset = det_ctx->byte_values[offset];
if (det_ctx->byte_values[offset] > UINT32_MAX) {
goto no_match;
}
offset = (uint32_t)det_ctx->byte_values[offset];
}

if (bjflags & DETECT_BYTEJUMP_NBYTES_VAR) {
nbytes = det_ctx->byte_values[bjd->nbytes];
if (det_ctx->byte_values[bjd->nbytes] > INT32_MAX) {
goto no_match;
}
nbytes = (int32_t)det_ctx->byte_values[bjd->nbytes];
} else {
nbytes = bjd->nbytes;
}
Expand Down Expand Up @@ -757,8 +790,11 @@ bool DetectEngineContentInspectionBuffer(DetectEngineCtx *de_ctx, DetectEngineTh

det_ctx->buffer_offset = 0;

if (b->inspect_offset > UINT32_MAX) {
return false;
}
int r = DetectEngineContentInspectionInternal(det_ctx, &ctx, s, smd, p, f, b->inspect,
b->inspect_len, b->inspect_offset, b->flags, inspection_mode);
b->inspect_len, (uint32_t)b->inspect_offset, b->flags, inspection_mode);
#ifdef UNITTESTS
ut_inspection_recursion_counter = ctx.recursion.count;
#endif
Expand Down
Loading
Loading