From: Jeff Lucovsky Date: Thu, 11 Jun 2020 13:07:43 +0000 (-0400) Subject: detect: Add transform validation api X-Git-Tag: suricata-6.0.0-beta1~290 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=197a593078781adaaec12aaac61696deff2f08ba;p=thirdparty%2Fsuricata.git detect: Add transform validation api This commit extends the API with a function that validates arguments against the transforms for the SM list (if any). --- diff --git a/src/detect-engine.c b/src/detect-engine.c index 326060bf0c..b984e587ca 100644 --- a/src/detect-engine.c +++ b/src/detect-engine.c @@ -1161,6 +1161,49 @@ void InspectionBufferCopy(InspectionBuffer *buffer, uint8_t *buf, uint32_t buf_l } } +/** \brief Check content byte array compatibility with transforms + * + * The "content" array is presented to the transforms so that each + * transform may validate that it's compatible with the transform. + * + * When a transform indicates the byte array is incompatible, none of the + * subsequent transforms, if any, are invoked. This means the first positive + * validation result terminates the loop. + * + * \param de_ctx Detection engine context. + * \param sm_list The SM list id. + * \param content The byte array being validated + * \param namestr returns the name of the transform that is incompatible with + * content. + * + * \retval true (false) If any of the transforms indicate the byte array is + * (is not) compatible. + **/ +bool DetectBufferTypeValidateTransform(DetectEngineCtx *de_ctx, int sm_list, + const uint8_t *content, uint16_t content_len, const char **namestr) +{ + const DetectBufferType *dbt = DetectBufferTypeGetById(de_ctx, sm_list); + BUG_ON(dbt == NULL); + + for (int i = 0; i < dbt->transforms.cnt; i++) { + const TransformData *t = &dbt->transforms.transforms[i]; + if (!sigmatch_table[t->transform].TransformValidate) + continue; + + if (sigmatch_table[t->transform].TransformValidate(content, content_len, t->options)) { + continue; + } + + if (namestr) { + *namestr = sigmatch_table[t->transform].name; + } + + return false; + } + + return true; +} + void InspectionBufferApplyTransforms(InspectionBuffer *buffer, const DetectEngineTransforms *transforms) { diff --git a/src/detect-engine.h b/src/detect-engine.h index 6ef796134d..a8c1e71795 100644 --- a/src/detect-engine.h +++ b/src/detect-engine.h @@ -35,6 +35,8 @@ void InspectionBufferCheckAndExpand(InspectionBuffer *buffer, uint32_t min_size) void InspectionBufferCopy(InspectionBuffer *buffer, uint8_t *buf, uint32_t buf_len); void InspectionBufferApplyTransforms(InspectionBuffer *buffer, const DetectEngineTransforms *transforms); +bool DetectBufferTypeValidateTransform(DetectEngineCtx *de_ctx, int sm_list, + const uint8_t *content, uint16_t content_len, const char **namestr); void InspectionBufferClean(DetectEngineThreadCtx *det_ctx); InspectionBuffer *InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id); InspectionBuffer *InspectionBufferMultipleForListGet(InspectionBufferMultipleForList *fb, uint32_t local_id);