detect/byte: cache byte values in tx state#15175
Conversation
- Add detection-state storage/retrieval for byte keyword values. - Restore cached values before content inspection and update cache after extraction/math. - Clear cached values on tx state reset and free them during state destruction. Issue: 7801
Update documentation to reflect that variables produced by byte_extract and byte_math can now be used across different buffers and direction boundaries. The per-transaction cache ensures produced values persist when the engine switches between buffers or evaluates bidirectional rules. - differences-from-snort: replace same-buffer limitation with cross-buffer support description - payload-keywords: add cross-buffer notes and example to byte_extract and byte_math sections - devguide/transactions: add per-transaction byte value cache section describing the save/restore mechanism Issue: 7801
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #15175 +/- ##
==========================================
+ Coverage 82.71% 82.72% +0.01%
==========================================
Files 993 993
Lines 271737 271795 +58
==========================================
+ Hits 224772 224852 +80
+ Misses 46965 46943 -22
Flags with carried forward coverage won't be shown. Click here to find out more. 🚀 New features to boost your workflow:
|
|
WARNING:
Pipeline = 30831 |
|
Question about tickets and scope :
You said
Is it a simple fixup to add ? Or a more complete one
You said
Did you file a ticket ? I do not see one linked to https://redmine.openinfosecfoundation.org/issues/7801 |
| * (by DetectEngineStateFree or DetectEngineThreadCtxDeinit). */ | ||
| uint64_t *tmp = det_ctx->byte_values; | ||
| det_ctx->byte_values = state->byte_values; | ||
| state->byte_values = tmp; |
There was a problem hiding this comment.
So, this is incorrect even if it is memory-safe
See OISF/suricata-verify#3021 latest commit test 7801-10
We have 2 txs that should match because the value in file.data is > than the value extracted in the uri
But we have only one match
Debug-print in bytetest shows that we use 88 for both txs !
This is because this pointers swap is not reset.
So, after one packet which calls DetectEngineContentInspectionRestoreByteValues
detctx has now the tx0 values, tx0 has now tx1 values, tx1 has now the dectx values
So, if we see another later packet which calls DetectEngineContentInspectionRestoreByteValues, we do not use the right values for the right tx
There was a problem hiding this comment.
Good catch -- the swap doesn't work for mult. packets. A memcpy would, however, work and i'm looking at that.
There was a problem hiding this comment.
Thanks for the test case -- I'll add case 10 to my s-v PR.
There was a problem hiding this comment.
I closed OISF/suricata-verify#3021 as you took my test 10 in your own PR
|
|
Continued in #15195 |
Continuation of #15085
Add per-transaction byte value caching so that variables produced by byte_extract and byte_math persist across buffer and direction boundaries.
Without this change, bidirectional (=>) rules that produce a byte variable in a toserver buffer and consume it in a toclient buffer fail when HTTP/1.1 pipelining creates multiple transactions. All TXs share the same det_ctx->byte_values, and a later TX's toserver inspection clobbers values before an earlier TX's toclient inspection runs.
The fix saves byte values to the per-TX DetectEngineState after extraction, restores them before each content inspection pass, clears them when reseting a TX and frees the memory when DetectEngineState is destroyed, transaction is reset, or the DetectEngineThreadCtx is uninitialized.
Link to ticket: https://redmine.openinfosecfoundation.org/issues/7801
Describe changes:
Updates:
Provide values to any of the below to override the defaults.
link to the pull request in the respective
_BRANCHvariable.SV_REPO=
SV_BRANCH=OISF/suricata-verify#2968
SU_REPO=
SU_BRANCH=