Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gen/refactor: transform and misc related changes for improved transform support #12502

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

jlucovsky
Copy link
Contributor

Continuation of #12427

A set of changes from #12425 that improve transform support

Describe changes:

  • Detection engine thread context pointer now available to individual transform functions
  • Detect engine context available to transform free function
  • Defer memory release of keyword hash table until engine context has been reed
  • Make packet/tx_id available earlier
  • Refactor transform setup/apply pattern from 2 to 1 function: InspectionBufferSetupAndApplyTransforms

Updates:

  • Restored g_buffer_type_hash to not rely on the de_ctx as there is not a dependency for this global data structure.

Provide values to any of the below to override the defaults.

  • To use an LibHTP, Suricata-Verify or Suricata-Update pull request,
    link to the pull request in the respective _BRANCH variable.
  • Leave unused overrides blank or remove.

SV_REPO=
SV_BRANCH=
SU_REPO=
SU_BRANCH=
LIBHTP_REPO=
LIBHTP_BRANCH=

jlucovsky and others added 6 commits January 29, 2025 09:18
This commit makes the detection engine thread context available for
transforms to use. The Lua transform requires this value.

Issue: 2290
Issue: 2290

This commit extends the hash table logic with an alternate free function
that provides the detection engine context.

Users that wish to use the next functionality must use the
HashListTableInitWithCtx function when initializing the hash table.
Using this interface will result in the hash table "free with context"
function (new) being used instead.
Issue: 2290

Defer freeing the keyword hash table until the engine context has
been freed. This eliminates a double-free from occurring.

For the unittests ONLY, clear the keyword_hash to prevent a double
free attempt.
So transforms can access them through DetectEngineThreadCtx
git grep -A 1 -w InspectionBufferSetup shows numbers cases of the pattern:
    - InspectionBufferSetup
    - InspectionBufferApplyTransforms

Refactor the implementations of those functions into
InspectionBufferSetupAndApplyTransforms to reduce function call count.

Issuer: 2290
Copy link

codecov bot commented Jan 29, 2025

Codecov Report

Attention: Patch coverage is 92.07547% with 21 lines in your changes missing coverage. Please review.

Project coverage is 80.56%. Comparing base (cfbf8fd) to head (1161082).

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #12502   +/-   ##
=======================================
  Coverage   80.56%   80.56%           
=======================================
  Files         925      925           
  Lines      259292   259322   +30     
=======================================
+ Hits       208906   208935   +29     
- Misses      50386    50387    +1     
Flag Coverage Δ
fuzzcorpus 56.14% <79.05%> (-0.01%) ⬇️
livemode 19.41% <18.57%> (+0.01%) ⬆️
pcap 44.22% <49.01%> (+0.02%) ⬆️
suricata-verify 63.39% <88.14%> (+<0.01%) ⬆️
unittests 58.45% <41.13%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

@suricata-qa
Copy link

Information: QA ran without warnings.

Pipeline 24425

@@ -1291,7 +1291,7 @@ typedef struct SigTableElmt_ {
uint8_t flags, File *, const Signature *, const SigMatchCtx *);

/** InspectionBuffer transformation callback */
void (*Transform)(InspectionBuffer *, void *context);
void (*Transform)(DetectEngineThreadCtx *, InspectionBuffer *, void *context);
Copy link
Contributor

Choose a reason for hiding this comment

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

This requires to bump up SC_PLUGIN_API_VERSION

Copy link
Member

Choose a reason for hiding this comment

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

This requires to bump up SC_PLUGIN_API_VERSION

Why? Its currently at 8, and should stay that way until 8 is released.

Copy link
Contributor

Choose a reason for hiding this comment

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

It should be modified every time the API gets modified so that runtime suricata and plugins can ensure compatibility

Maybe the current 8 value is not the best fit since 8 is not released...

Copy link
Member

Choose a reason for hiding this comment

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

Our compatibility promises are for a major version. So 8 is fine, as 8 is not released there is no promise that its stable until 8 is released.

Copy link
Contributor

Choose a reason for hiding this comment

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

There is no promise, but we can do it

Copy link
Member

@jasonish jasonish Jan 29, 2025

Choose a reason for hiding this comment

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

We'll need to work on the versioning, so I think its out of scope for this PR.

For example, if we ever did an 8.1 release, we wouldn't consider that compatible with 8.0. So we'd want something like:

  • 800000 # 8.0
  • 800100 # 8.1
    Or were you thinking just an ever increasing int?

Patch releases should not be considered as they should not break the API. I'd still not consider pre-release git snapshot versions either.

As the library matures we'll end up in a position where any change to any public function signature will require an update to this, which could be a lot of commits during a major development cycle.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think you forget to take into account the people using git master (like after 8 release before 9)

We'll need to work on the versioning

indeed

so I think its out of scope for this PR.

But a change in SC_PLUGIN_API_VERSION is required by this PR

As the library matures we'll end up in a position where any change to any public function signature will require an update to this, which could be a lot of commits during a major development cycle.

I think we are there

Copy link
Member

Choose a reason for hiding this comment

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

In git master we make no promises until it is released as a final 8.0.0 release. Ppl relying on a dev branch will have to accept we can still break things.

Copy link
Contributor

Choose a reason for hiding this comment

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

I understand that.

Even bumping SC_PLUGIN_API_VERSION, this PR still "breaks things", but it will give a better error message instead of a segfault.

This is to get the good habit for post 8 release

Copy link
Contributor

Choose a reason for hiding this comment

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

I understand that.

Even bumping SC_PLUGIN_API_VERSION, this PR still "breaks things", but it will give a better error message instead of a segfault.

This is to get the good habit for post 8 release

@@ -977,12 +978,18 @@ static void DetectBufferTypeFreeFunc(void *data)
sigmatch_table[map->transforms.transforms[i].transform].name);
continue;
}
sigmatch_table[map->transforms.transforms[i].transform].Free(NULL, map->transforms.transforms[i].options);
sigmatch_table[map->transforms.transforms[i].transform].Free(
de_ctx, map->transforms.transforms[i].options);
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks ok but feels painful

Why will we need this de_ctx when freeing ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The Luaxform will need the de_ctx to unregister the thread ctx (see #12425 detect-transform-luaxform.c, function DetectTransformLuaxformFree)

Copy link
Contributor

Choose a reason for hiding this comment

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

What is this thread ctx ?

Copy link
Contributor

Choose a reason for hiding this comment

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

Another option would be to have sigmatch_table having 2 different function pointers Free and
FreeCtx (not sure it is better, but requires less modifications)

@@ -2606,6 +2606,9 @@ DetectEngineCtx *DetectEngineCtxInitWithPrefix(const char *prefix, uint32_t tena
static void DetectEngineCtxFreeThreadKeywordData(DetectEngineCtx *de_ctx)
{
HashListTableFree(de_ctx->keyword_hash);
#if UNITTESTS
de_ctx->keyword_hash = NULL;
Copy link
Contributor

Choose a reason for hiding this comment

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

Double free Happens only in unit tests, right ? Just double checking

Copy link
Contributor

Choose a reason for hiding this comment

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

And why was CI green if there was this double free ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, this is a unit test only situation.

The double free would occur in DetectUnregsiterThreadCtxFuncs iff keyword hash wasn't nullified.

Copy link
Contributor

Choose a reason for hiding this comment

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

And why was CI green if there was this double free ?

@@ -113,7 +113,6 @@ void DetectEngineFrameMpmRegister(DetectEngineCtx *de_ctx, const char *name, int
const DetectBufferMpmRegistry *mpm_reg, int list_id),
AppProto alproto, uint8_t type);


Copy link
Contributor

Choose a reason for hiding this comment

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

Does not deserve its own commit :-p

InspectionBufferSetup(det_ctx, list_id, buffer, data, data_len);
InspectionBufferApplyTransforms(det_ctx, buffer, transforms);
InspectionBufferSetupAndApplyTransforms(
det_ctx, list_id, buffer, data, data_len, transforms);
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you use DetectHelperGetData to save even more lines ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes -- I think that pattern could be used in several places but is probably best done in a separate PR, imo

Copy link
Contributor

Choose a reason for hiding this comment

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

So, why do you need this commit ? if it "is probably best done in a separate PR"

Copy link
Contributor

Choose a reason for hiding this comment

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

So, why do you need this commit ? if it "is probably best done in a separate PR"

Copy link
Contributor

@catenacyber catenacyber left a comment

Choose a reason for hiding this comment

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

Needs the SC_PLUGIN_API_VERSION bump

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

5 participants