]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
frames: enable only used frames 8429/head
authorVictor Julien <vjulien@oisf.net>
Mon, 23 Jan 2023 18:41:45 +0000 (19:41 +0100)
committerVictor Julien <vjulien@oisf.net>
Tue, 24 Jan 2023 08:44:02 +0000 (09:44 +0100)
Enable only frames that are actually used.

Ticket: #4979.

src/app-layer-frames.c
src/app-layer-frames.h
src/detect-frame.c
src/output-json-frame.c
src/suricata.c

index bebb4ad50002ec7261b3cf659222729fbd7c5b63..4b1ddde6b1030d14b877c85c1d3a85261e4d83a2 100644 (file)
 #include "app-layer-frames.h"
 #include "app-layer-parser.h"
 
+struct FrameConfig {
+    SC_ATOMIC_DECLARE(uint64_t, types);
+};
+static struct FrameConfig frame_config[ALPROTO_MAX];
+
+void FrameConfigInit(void)
+{
+    for (AppProto p = 0; p < ALPROTO_MAX; p++) {
+        SC_ATOMIC_INIT(frame_config[p].types);
+    }
+}
+
+void FrameConfigEnableAll(void)
+{
+    const uint64_t bits = UINT64_MAX;
+    for (AppProto p = 0; p < ALPROTO_MAX; p++) {
+        struct FrameConfig *fc = &frame_config[p];
+        SC_ATOMIC_OR(fc->types, bits);
+    }
+}
+
+void FrameConfigEnable(const AppProto p, const uint8_t type)
+{
+    const uint64_t bits = BIT_U64(type);
+    struct FrameConfig *fc = &frame_config[p];
+    SC_ATOMIC_OR(fc->types, bits);
+}
+
+static inline bool FrameConfigTypeIsEnabled(const AppProto p, const uint8_t type)
+{
+    struct FrameConfig *fc = &frame_config[p];
+    const uint64_t bits = BIT_U64(type);
+    const bool enabled = (SC_ATOMIC_GET(fc->types) & bits) != 0;
+    return enabled;
+}
+
 static void FrameDebug(const char *prefix, const Frames *frames, const Frame *frame)
 {
 #ifdef DEBUG
@@ -383,7 +419,10 @@ Frame *AppLayerFrameNewByPointer(Flow *f, const StreamSlice *stream_slice,
     SCLogDebug("frame_start:%p stream_slice->input:%p stream_slice->offset:%" PRIu64, frame_start,
             stream_slice->input, stream_slice->offset);
 
-    /* workarounds for many (unit|fuzz)tests not handling TCP data properly */
+    if (!(FrameConfigTypeIsEnabled(f->alproto, frame_type)))
+        return NULL;
+
+        /* workarounds for many (unit|fuzz)tests not handling TCP data properly */
 #if defined(UNITTESTS) || defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)
     if (f->proto == IPPROTO_TCP && f->protoctx == NULL)
         return NULL;
@@ -430,6 +469,9 @@ static Frame *AppLayerFrameUdp(Flow *f, const StreamSlice *stream_slice,
 {
     BUG_ON(f->proto != IPPROTO_UDP);
 
+    if (!(FrameConfigTypeIsEnabled(f->alproto, frame_type)))
+        return NULL;
+
     FramesContainer *frames_container = AppLayerFramesSetupContainer(f);
     if (frames_container == NULL)
         return NULL;
@@ -453,7 +495,10 @@ static Frame *AppLayerFrameUdp(Flow *f, const StreamSlice *stream_slice,
 Frame *AppLayerFrameNewByRelativeOffset(Flow *f, const StreamSlice *stream_slice,
         const uint32_t frame_start_rel, const int64_t len, int dir, uint8_t frame_type)
 {
-    /* workarounds for many (unit|fuzz)tests not handling TCP data properly */
+    if (!(FrameConfigTypeIsEnabled(f->alproto, frame_type)))
+        return NULL;
+
+        /* workarounds for many (unit|fuzz)tests not handling TCP data properly */
 #if defined(UNITTESTS) || defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)
     if (f->proto == IPPROTO_TCP && f->protoctx == NULL)
         return NULL;
@@ -510,7 +555,10 @@ void AppLayerFrameDump(Flow *f)
 Frame *AppLayerFrameNewByAbsoluteOffset(Flow *f, const StreamSlice *stream_slice,
         const uint64_t frame_start, const int64_t len, int dir, uint8_t frame_type)
 {
-    /* workarounds for many (unit|fuzz)tests not handling TCP data properly */
+    if (!(FrameConfigTypeIsEnabled(f->alproto, frame_type)))
+        return NULL;
+
+        /* workarounds for many (unit|fuzz)tests not handling TCP data properly */
 #if defined(UNITTESTS) || defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)
     if (f->proto == IPPROTO_TCP && f->protoctx == NULL)
         return NULL;
index 98ddefede7eed7e296737308448ea2c10c5d2319..ea24e1b5dcff4189e68a11d8775c085120520001 100644 (file)
@@ -26,7 +26,8 @@
 
 #include "rust.h"
 
-#define FRAME_STREAM_TYPE 255
+/** max 63 to fit the 64 bit per protocol space */
+#define FRAME_STREAM_TYPE 63
 /** always the first frame to be created. TODO but what about protocol upgrades? */
 #define FRAME_STREAM_ID 1
 
@@ -102,4 +103,8 @@ void AppLayerFramesSlide(Flow *f, const uint32_t slide, const uint8_t direction)
 FramesContainer *AppLayerFramesGetContainer(Flow *f);
 FramesContainer *AppLayerFramesSetupContainer(Flow *f);
 
+void FrameConfigInit(void);
+void FrameConfigEnableAll(void);
+void FrameConfigEnable(const AppProto p, const uint8_t type);
+
 #endif
index 8df5df9366f2d9e17dcbd3b46ddcedb898fdf509..c74c145a078831488449f42e3222b54732569c0a 100644 (file)
@@ -141,6 +141,7 @@ static int DetectFrameSetup(DetectEngineCtx *de_ctx, Signature *s, const char *s
     if (DetectBufferSetActiveList(s, buffer_id) < 0)
         return -1;
 
+    FrameConfigEnable(keyword_alproto, frame_type);
     return 0;
 }
 
index 68ecd95d48dcf0cf5f070d28c445cd2ab43354fb..d23ba92ac380183bb7199c89ae3ba2e79d6d0ff2 100644 (file)
@@ -489,6 +489,8 @@ static OutputInitResult JsonFrameLogInitCtxSub(ConfNode *conf, OutputCtx *parent
     output_ctx->data = json_output_ctx;
     output_ctx->DeInit = JsonFrameLogDeInitCtxSub;
 
+    FrameConfigEnableAll();
+
     result.ctx = output_ctx;
     result.ok = true;
     return result;
index 4828a893864d264efbf2cc55b98331a0d7a58e11..ba7e8e5f2e9108735ed5f1e94fdda0af3166471d 100644 (file)
@@ -336,6 +336,7 @@ void GlobalsInitPreConfig(void)
     SupportFastPatternForSigMatchTypes();
     SCThresholdConfGlobalInit();
     SCProtoNameInit();
+    FrameConfigInit();
 }
 
 static void GlobalsDestroy(SCInstance *suri)