From 197a593078781adaaec12aaac61696deff2f08ba Mon Sep 17 00:00:00 2001 From: Jeff Lucovsky Date: Thu, 11 Jun 2020 09:07:43 -0400 Subject: [PATCH] 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). --- src/detect-engine.c | 43 +++++++++++++++++++++++++++++++++++++++++++ src/detect-engine.h | 2 ++ 2 files changed, 45 insertions(+) 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); -- 2.47.2