]> git.ipfire.org Git - thirdparty/snort3.git/commitdiff
Merge pull request #1395 in SNORT/snort3 from offload_mime_decode to master
authorRuss Combs (rucombs) <rucombs@cisco.com>
Fri, 26 Oct 2018 15:57:22 +0000 (11:57 -0400)
committerRuss Combs (rucombs) <rucombs@cisco.com>
Fri, 26 Oct 2018 15:57:22 +0000 (11:57 -0400)
Squashed commit of the following:

commit 3922aab0afd5e880353a70281d166a2d2516de6d
Author: Bhagya Tholpady <bbantwal@cisco.com>
Date:   Tue Oct 23 13:42:17 2018 -0400

    delete commented lines

commit 2cdc9ca78a9527636f566146802c6168d3509436
Author: Bhagya Tholpady <bbantwal@cisco.com>
Date:   Mon Oct 22 15:24:50 2018 -0400

    allocate buffer only once per mime context data

commit d1d11e4e669d38f0c06165e6e1af25c62f6c0704
Author: Bhagya Tholpady <bbantwal@cisco.com>
Date:   Fri Oct 19 14:34:23 2018 -0400

    changes per code review

commit 622759823ffb87f6fabe7f1a381204370463a4a7
Author: Bhagya Tholpady <bbantwal@cisco.com>
Date:   Wed Oct 17 23:45:25 2018 -0400

    mime: move the decode buffer used by mime attachments to mime context data

29 files changed:
src/detection/CMakeLists.txt
src/detection/context_switcher.cc
src/detection/detection_engine.cc
src/detection/ips_context.cc
src/detection/ips_context.h
src/detection/ips_context_data.cc [new file with mode: 0644]
src/detection/ips_context_data.h [new file with mode: 0644]
src/mime/CMakeLists.txt
src/mime/decode_b64.cc
src/mime/decode_b64.h
src/mime/decode_base.h
src/mime/decode_bit.cc
src/mime/decode_bit.h
src/mime/decode_buffer.cc
src/mime/decode_buffer.h
src/mime/decode_qp.cc
src/mime/decode_qp.h
src/mime/decode_uu.cc
src/mime/decode_uu.h
src/mime/file_mime_context_data.cc [new file with mode: 0644]
src/mime/file_mime_context_data.h [new file with mode: 0644]
src/mime/file_mime_decode.cc
src/mime/file_mime_decode.h
src/mime/file_mime_process.cc
src/service_inspectors/dce_rpc/dce_context_data.cc
src/service_inspectors/dce_rpc/dce_context_data.h
src/service_inspectors/gtp/gtp_inspect.cc
src/service_inspectors/http_inspect/http_context_data.cc
src/service_inspectors/http_inspect/http_context_data.h

index 9e08575cde373d6731ff9bcb32e21c560d70cce4..02d3f262940c1b7514200a951f146db96e8f59bd 100644 (file)
@@ -6,6 +6,7 @@ set (DETECTION_INCLUDES
     detection_util.h
     detect_trace.h
     ips_context.h
+    ips_context_data.h
     regex_offload.h
     rule_option_types.h
     rules.h
@@ -32,6 +33,7 @@ add_library (detection OBJECT
     fp_utils.cc
     fp_utils.h
     ips_context.cc
+    ips_context_data.cc
     pattern_match_data.h
     pcrm.cc
     pcrm.h
index c5887f326dc035848fba90159d822d64d827e756..d9bd750de2fbe21cc388c9f57ceeb2676756707e 100644 (file)
@@ -32,6 +32,7 @@
 
 #include "detect_trace.h"
 #include "ips_context.h"
+#include "ips_context_data.h"
 
 #ifdef UNIT_TEST
 #include "catch/snort_catch.h"
index c8a6c210e97ebe4fa70acbeae3377649b96d6a7e..377be8da6d3366b9b13fc029d205a7fd721fc4ea 100644 (file)
@@ -50,6 +50,7 @@
 #include "fp_config.h"
 #include "fp_detect.h"
 #include "ips_context.h"
+#include "ips_context_data.h"
 #include "regex_offload.h"
 
 static THREAD_LOCAL RegexOffload* offloader = nullptr;
index 75c3f6fb192290f4a7d39ada7734d52b096f34f2..194cf63721bbd0ecb10b63b2762dce5648894c0f 100644 (file)
@@ -25,6 +25,8 @@
 #include "ips_context.h"
 
 #include <cassert>
+#include "detection/detection_engine.h"
+#include "detection/ips_context_data.h"
 #include "events/event_queue.h"
 #include "events/sfeventq.h"
 #include "main/snort_config.h"
 
 using namespace snort;
 
-//--------------------------------------------------------------------------
-// context data
-//--------------------------------------------------------------------------
-
-// ips_id is not a member of context data so that
-// tests (and only tests) can reset the id
-static unsigned ips_id = 0;
-
-// Only 5 inspectors currently use the ips context data.
-// FIXIT-L This limit should to be updated if any more inspectors/modules use it.
-constexpr unsigned max_ips_id = 32;
-
-unsigned IpsContextData::get_ips_id()
-{ 
-    ++ips_id;
-    assert( ips_id < max_ips_id );
-    return ips_id; 
-}
-
 //--------------------------------------------------------------------------
 // context methods
 //--------------------------------------------------------------------------
@@ -151,17 +134,7 @@ public:
 };
 
 int TestData::count = 0;
-
-TEST_CASE("IpsContextData id", "[IpsContextData]")
-{
-    ips_id = 0;
-
-    auto id1 = IpsContextData::get_ips_id();
-    auto id2 = IpsContextData::get_ips_id();
-    CHECK(id1 != id2);
-
-    CHECK(max_ips_id > id2 );
-}
+static unsigned ips_id = 0;
 
 TEST_CASE("IpsContext basic", "[IpsContext]")
 {
index 519c6401aa7a3b7bebfef33d54924e08ac519a01..6d518168b4103ff413b830e3d58735697aa4c229 100644 (file)
@@ -39,6 +39,7 @@ struct SF_EVENTQ;
 
 namespace snort
 {
+class IpsContextData;
 struct SnortConfig;
 struct Replacement
 {
@@ -52,18 +53,6 @@ struct FlowSnapshot
     SnortProtocolId proto_id;
 };
 
-class SO_PUBLIC IpsContextData
-{
-public:
-    virtual ~IpsContextData() = default;
-
-    static unsigned get_ips_id();
-    virtual void clear() {}
-
-protected:
-    IpsContextData() = default;
-};
-
 class SO_PUBLIC IpsContext
 {
 public:
@@ -126,6 +115,9 @@ public:
     std::vector<Replacement> rpl;
 
     static const unsigned buf_size = Codec::PKT_MAX;
+    // Only 5 inspectors currently use the ips context data.
+    // FIXIT-L This limit should to be updated if any more inspectors/modules use it.
+    static constexpr unsigned max_ips_id = 32;
 
 private:
     FlowSnapshot flow;
diff --git a/src/detection/ips_context_data.cc b/src/detection/ips_context_data.cc
new file mode 100644 (file)
index 0000000..44e38a5
--- /dev/null
@@ -0,0 +1,70 @@
+//--------------------------------------------------------------------------
+// Copyright (C) 2018-2018 Cisco and/or its affiliates. All rights reserved.
+//
+// This program is free software; you can redistribute it and/or modify it
+// under the terms of the GNU General Public License Version 2 as published
+// by the Free Software Foundation.  You may not use, modify or distribute
+// this program under any other version of the GNU General Public License.
+//
+// This program is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+//--------------------------------------------------------------------------
+
+// ips_context_data.cc author Russ Combs <rucombs@cisco.com>
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "ips_context_data.h"
+
+#include <cassert>
+#include "detection/ips_context.h"
+
+#ifdef UNIT_TEST
+#include "catch/snort_catch.h"
+#endif
+
+using namespace snort;
+
+//--------------------------------------------------------------------------
+// context data
+//--------------------------------------------------------------------------
+
+// ips_id is not a member of context data so that
+// tests (and only tests) can reset the id
+static unsigned ips_id = 0;
+static unsigned max_id = IpsContext::max_ips_id;
+
+unsigned IpsContextData::get_ips_id()
+{ 
+    ++ips_id;
+    assert( ips_id < max_id );
+    return ips_id; 
+}
+
+//--------------------------------------------------------------------------
+// unit tests
+//--------------------------------------------------------------------------
+
+#ifdef UNIT_TEST
+
+TEST_CASE("IpsContextData id", "[IpsContextData]")
+{
+    ips_id = 0;
+
+    auto id1 = IpsContextData::get_ips_id();
+    auto id2 = IpsContextData::get_ips_id();
+    CHECK(id1 != id2);
+
+    CHECK(max_id > id2 );
+}
+
+#endif
+
diff --git a/src/detection/ips_context_data.h b/src/detection/ips_context_data.h
new file mode 100644 (file)
index 0000000..5b79df8
--- /dev/null
@@ -0,0 +1,54 @@
+//--------------------------------------------------------------------------
+// Copyright (C) 2018-2018 Cisco and/or its affiliates. All rights reserved.
+//
+// This program is free software; you can redistribute it and/or modify it
+// under the terms of the GNU General Public License Version 2 as published
+// by the Free Software Foundation.  You may not use, modify or distribute
+// this program under any other version of the GNU General Public License.
+//
+// This program is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+//--------------------------------------------------------------------------
+
+// ips_context_data.h author Russ Combs <rucombs@cisco.com>
+
+#ifndef IPS_CONTEXT_DATA_H
+#define IPS_CONTEXT_DATA_H
+
+#include "main/snort_types.h"
+
+#include "detection/detection_engine.h"
+
+namespace snort
+{
+class SO_PUBLIC IpsContextData
+{
+public:
+    virtual ~IpsContextData() = default;
+
+    static unsigned get_ips_id();
+    template<typename T>
+    static T* get(unsigned ips_id)
+    {
+        T* data = (T*)DetectionEngine::get_data(ips_id);
+        if ( ! data )
+        {
+            data = new T;
+            DetectionEngine::set_data(ips_id, data);
+        }
+        return data;
+    }
+    virtual void clear() {}
+
+protected:
+    IpsContextData() = default;
+};
+}
+#endif
+
index 4deecbaa4d9ed7adb296a7a2e6bae85407052d61..713be80a8de0db48b6bb5c1e68d6284dfd1bf455 100644 (file)
@@ -3,6 +3,7 @@ set( MIME_INCLUDES
     decode_b64.h
     decode_base.h
     file_mime_config.h
+    file_mime_context_data.h
     file_mime_decode.h 
     file_mime_log.h 
     file_mime_paf.h 
@@ -12,6 +13,7 @@ set( MIME_INCLUDES
 add_library ( mime OBJECT
     ${MIME_INCLUDES}
     file_mime_config.cc 
+    file_mime_context_data.cc 
     file_mime_decode.cc 
     file_mime_log.cc 
     file_mime_paf.cc 
index f4b92b625f1d19fe08ff9a9733d728b7fbef2a0d..281d54d6e921b8f7c591982629bd54d741e1149f 100644 (file)
@@ -34,12 +34,12 @@ void B64Decode::reset_decode_state()
     buffer->reset_saved();
 }
 
-DecodeResult B64Decode::decode_data(const uint8_t* start, const uint8_t* end)
+DecodeResult B64Decode::decode_data(const uint8_t* start, const uint8_t* end, uint8_t* decode_buf)
 {
     uint32_t act_encode_size = 0, act_decode_size = 0;
     uint32_t i = 0;
 
-    if (!buffer->check_restore_buffer())
+    if (!buffer->check_restore_buffer() || !decode_buf)
     {
         reset_decode_state();
         return DECODE_EXCEEDED;
@@ -69,7 +69,7 @@ DecodeResult B64Decode::decode_data(const uint8_t* start, const uint8_t* end)
         buffer->reset_saved();
 
     if (snort::sf_base64decode(buffer->get_encode_buff(), act_encode_size,
-        buffer->get_decode_buff(), buffer->get_decode_avail(), &act_decode_size) != 0)
+        decode_buf, buffer->get_decode_avail(), &act_decode_size) != 0)
     {
         reset_decode_state();
         return DECODE_FAIL;
@@ -81,7 +81,7 @@ DecodeResult B64Decode::decode_data(const uint8_t* start, const uint8_t* end)
     }
 
     decoded_bytes = act_decode_size;
-    decodePtr = buffer->get_decode_buff();
+    decodePtr = decode_buf;
     buffer->update_buffer(act_encode_size, act_decode_size);
     decode_bytes_read = buffer->get_decode_bytes_read();
     return DECODE_SUCCESS;
index 2415f4ff89bc61e0836b15a411705442032dda7f..345b5fc928f3eeac91887a0dc0c0e10fcc59285d 100644 (file)
@@ -32,7 +32,7 @@ public:
     ~B64Decode() override;
 
     // Main function to decode file data
-    DecodeResult decode_data(const uint8_t* start, const uint8_t* end) override;
+    DecodeResult decode_data(const uint8_t* start, const uint8_t* end, uint8_t* decode_buf) override;
 
     void reset_decode_state() override;
 
index d3149ff8c1989007c31daaa68796c95320881075..73364d37d1c5b16b31376f09ef68e9b104cad1d2 100644 (file)
@@ -38,7 +38,7 @@ public:
     virtual ~DataDecode() = default;
 
     // Main function to decode file data
-    virtual DecodeResult decode_data(const uint8_t* start, const uint8_t* end) = 0;
+    virtual DecodeResult decode_data(const uint8_t* start, const uint8_t* end, uint8_t* decode_buf) = 0;
 
     // Retrieve the decoded data the previous decode_data() call
     int get_decoded_data(const uint8_t** buf,  uint32_t* size);
index 18f68ce38c0e3878396ceb167994ede26403dc16..5a1da2e572fe5be6fae95fe8916da035b649e458 100644 (file)
@@ -29,7 +29,7 @@ void BitDecode::reset_decode_state()
     reset_decoded_bytes();
 }
 
-DecodeResult BitDecode::decode_data(const uint8_t* start, const uint8_t* end)
+DecodeResult BitDecode::decode_data(const uint8_t* start, const uint8_t* end, uint8_t*)
 {
     uint32_t bytes_avail = 0;
     uint32_t act_size = end - start;
index b5bb054153a8b68830bb56e2a3999093defa2a2d..8369c7dba7c8aa78c4f2823f4a749b00bd27fedd 100644 (file)
@@ -30,7 +30,7 @@ public:
     BitDecode(int max_depth, int detect_depth);
 
     // Main function to decode file data
-    DecodeResult decode_data(const uint8_t* start, const uint8_t* end) override;
+    DecodeResult decode_data(const uint8_t* start, const uint8_t* end, uint8_t* decode_buf) override;
 
     void reset_decode_state() override;
 
index 7f01ffb7c75c39eb19343dbd4b630ed8f9f0758c..f1af6c1dbd1ec43272b4bd54a47fc20db66bbb79 100644 (file)
@@ -42,7 +42,7 @@ bool DecodeBuffer::check_restore_buffer()
     uint32_t encode_avail =  get_encode_avail();
 
     if (encode_avail ==0 || decode_avail ==0 ||
-        (!encodeBuf) || (!decodeBuf))
+        (!encodeBuf))
     {
         return false;
     }
@@ -111,15 +111,12 @@ DecodeBuffer::DecodeBuffer(int max_depth)
         return;
 
     encodeBuf = (uint8_t*)snort_alloc(buf_size);
-    decodeBuf = (uint8_t*)snort_alloc(buf_size);
 }
 
 DecodeBuffer::~DecodeBuffer()
 {
     if (encodeBuf)
         snort_free(encodeBuf);
-    if (decodeBuf)
-        snort_free(decodeBuf);
 }
 
 void DecodeBuffer::update_buffer(uint32_t act_encode_size, uint32_t act_decode_size)
index 4bf936c594cc304e3287b0394f3e5e877d9c02e7..5f8580b65669d98aa64d818e2e6d246e4b6c6405 100644 (file)
@@ -40,7 +40,6 @@ public:
     void update_buffer(uint32_t act_encode_size, uint32_t act_decode_size);
 
     void reset_saved();
-    uint8_t* get_decode_buff() { return decodeBuf; }
     uint8_t* get_encode_buff() { return encodeBuf; }
     uint32_t get_decode_bytes_read() { return decode_bytes_read; }
     uint32_t get_decode_avail();
@@ -52,7 +51,6 @@ private:
     uint32_t prev_encoded_bytes;
     uint8_t* prev_encoded_buf;
     uint8_t* encodeBuf = nullptr;
-    uint8_t* decodeBuf = nullptr;
     uint32_t encode_bytes_read;
     uint32_t decode_bytes_read;
     int code_depth;
index 8daf8968bb11978599e3d4085384a426fd382787..7b96c60cd722fdb654bf978dfb0c406a08f67474 100644 (file)
@@ -37,11 +37,11 @@ void QPDecode::reset_decode_state()
     buffer->reset_saved();
 }
 
-DecodeResult QPDecode::decode_data(const uint8_t* start, const uint8_t* end)
+DecodeResult QPDecode::decode_data(const uint8_t* start, const uint8_t* end, uint8_t *decode_buf)
 {
     uint32_t act_encode_size = 0, act_decode_size = 0, bytes_read = 0;
 
-    if (!buffer->check_restore_buffer())
+    if (!buffer->check_restore_buffer() || !decode_buf)
     {
         reset_decode_state();
         return DECODE_EXCEEDED;
@@ -59,8 +59,8 @@ DecodeResult QPDecode::decode_data(const uint8_t* start, const uint8_t* end)
     act_encode_size = act_encode_size + buffer->get_prev_encoded_bytes();
 
     if (sf_qpdecode((char *)buffer->get_encode_buff(), act_encode_size,
-        (char *)buffer->get_decode_buff(), buffer->get_decode_avail(),
-        &bytes_read, &act_decode_size) != 0)
+        (char *)decode_buf, buffer->get_decode_avail(), &bytes_read,
+        &act_decode_size) != 0)
     {
         reset_decode_state();
         return DECODE_FAIL;
@@ -80,7 +80,7 @@ DecodeResult QPDecode::decode_data(const uint8_t* start, const uint8_t* end)
         buffer->reset_saved();
 
     decoded_bytes = act_decode_size;
-    decodePtr = buffer->get_decode_buff();
+    decodePtr = decode_buf;
     buffer->update_buffer(act_encode_size, act_decode_size);
     decode_bytes_read = buffer->get_decode_bytes_read();
     return DECODE_SUCCESS;
index 0a471374681a437f050ba5e1403824141525d313..77a173cd2dc49cc9d96ada226e42b5d593fb39dd 100644 (file)
@@ -31,7 +31,7 @@ public:
     ~QPDecode() override;
 
     // Main function to decode file data
-    DecodeResult decode_data(const uint8_t* start, const uint8_t* end) override;
+    DecodeResult decode_data(const uint8_t* start, const uint8_t* end, uint8_t* decode_buf) override;
 
     void reset_decode_state() override;
 
index d4fdecf7e358659fe53422536cc6f3003bebf2c8..3ce600dd7b7f9f1e831ecad965eebec5fd098cb3 100644 (file)
@@ -43,11 +43,11 @@ void UUDecode::reset_decode_state()
     begin_found = end_found = false;
 }
 
-DecodeResult UUDecode::decode_data(const uint8_t* start, const uint8_t* end)
+DecodeResult UUDecode::decode_data(const uint8_t* start, const uint8_t* end, uint8_t* decode_buf)
 {
     uint32_t act_encode_size = 0, act_decode_size = 0, bytes_read = 0;
 
-    if (!buffer->check_restore_buffer())
+    if (!buffer->check_restore_buffer() || !decode_buf)
     {
         reset_decode_state();
         return DECODE_EXCEEDED;
@@ -74,7 +74,7 @@ DecodeResult UUDecode::decode_data(const uint8_t* start, const uint8_t* end)
 
     act_encode_size = act_encode_size + buffer->get_prev_encoded_bytes();
 
-    if (sf_uudecode(buffer->get_encode_buff(), act_encode_size, buffer->get_decode_buff(),
+    if (sf_uudecode(buffer->get_encode_buff(), act_encode_size, decode_buf,
             buffer->get_decode_avail(), &bytes_read, &act_decode_size,
             &(begin_found), &(end_found)) != 0)
     {
@@ -105,7 +105,7 @@ DecodeResult UUDecode::decode_data(const uint8_t* start, const uint8_t* end)
         buffer->reset_saved();
 
     decoded_bytes = act_decode_size;
-    decodePtr = buffer->get_decode_buff();
+    decodePtr = decode_buf;
     buffer->update_buffer(act_encode_size, act_decode_size);
     decode_bytes_read = buffer->get_decode_bytes_read();
     return DECODE_SUCCESS;
index 857542bd2591af18d44494e9fcd1ed2cc8596bcf..6d510bba1afaac55d9ab052b2d34e9ce4fce5bfe 100644 (file)
@@ -31,7 +31,7 @@ public:
     ~UUDecode() override;
 
     // Main function to decode file data
-    DecodeResult decode_data(const uint8_t* start, const uint8_t* end) override;
+    DecodeResult decode_data(const uint8_t* start, const uint8_t* end, uint8_t* decode_buf) override;
 
     void reset_decode_state() override;
 
diff --git a/src/mime/file_mime_context_data.cc b/src/mime/file_mime_context_data.cc
new file mode 100644 (file)
index 0000000..4e6b7c5
--- /dev/null
@@ -0,0 +1,53 @@
+//--------------------------------------------------------------------------
+// Copyright (C) 2018-2018 Cisco and/or its affiliates. All rights reserved.
+//
+// This program is free software; you can redistribute it and/or modify it
+// under the terms of the GNU General Public License Version 2 as published
+// by the Free Software Foundation.  You may not use, modify or distribute
+// this program under any other version of the GNU General Public License.
+//
+// This program is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+//--------------------------------------------------------------------------
+// file_mime_context_data.cc author Bhagya Tholpady <bbantwal@cisco.com>
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "file_mime_context_data.h"
+
+#include "detection/detection_engine.h"
+#include "utils/util.h"
+
+using namespace snort;
+
+#define MAX_DEPTH       65536
+unsigned MimeDecodeContextData::mime_ips_id = 0;
+
+MimeDecodeContextData::MimeDecodeContextData()
+{
+    decode_buf = (uint8_t*)snort_alloc(MAX_DEPTH);
+}
+MimeDecodeContextData::~MimeDecodeContextData()
+{
+    snort_free(decode_buf);
+    decode_buf = nullptr;
+}
+
+void MimeDecodeContextData::init()
+{ mime_ips_id = IpsContextData::get_ips_id(); }
+
+uint8_t* MimeDecodeContextData::get_decode_buf()
+{
+    MimeDecodeContextData* data = IpsContextData::get<MimeDecodeContextData>(mime_ips_id);
+
+    return data->decode_buf;
+}
+
diff --git a/src/mime/file_mime_context_data.h b/src/mime/file_mime_context_data.h
new file mode 100644 (file)
index 0000000..6ca7ed9
--- /dev/null
@@ -0,0 +1,40 @@
+//--------------------------------------------------------------------------
+// Copyright (C) 2018-2018 Cisco and/or its affiliates. All rights reserved.
+//
+// This program is free software; you can redistribute it and/or modify it
+// under the terms of the GNU General Public License Version 2 as published
+// by the Free Software Foundation.  You may not use, modify or distribute
+// this program under any other version of the GNU General Public License.
+//
+// This program is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+//--------------------------------------------------------------------------
+// file_mime_context_data.h author Bhagya Tholpady <bbantwal@cisco.com>
+
+#ifndef FILE_MIME_CONTEXT_DATA_H
+#define FILE_MIME_CONTEXT_DATA_H
+
+#include "detection/ips_context_data.h"
+
+class MimeDecodeContextData : public snort::IpsContextData
+{
+public:
+    MimeDecodeContextData();
+    ~MimeDecodeContextData() override;
+
+    static unsigned mime_ips_id;
+
+    uint8_t* decode_buf = nullptr;
+
+    static void init();
+    static uint8_t* get_decode_buf();
+};
+
+#endif
+
index 17afe3e0ccbb0b6cc71e368d4e6f2522c1d9e142..ec6f1a589feba3822a226da93ef998efeb19d21e 100644 (file)
@@ -16,7 +16,7 @@
 // with this program; if not, write to the Free Software Foundation, Inc.,
 // 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 //--------------------------------------------------------------------------
-// file_mime_decode.cc author Bhagyashree Bantwal <bbantwal@sourcefire.com>
+// file_mime_decode.cc author Bhagya Tholpady <bbantwal@cisco.com>
 
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #include "decode_bit.h"
 #include "decode_qp.h"
 #include "decode_uu.h"
+#include "file_mime_context_data.h"
 
 using namespace snort;
 
+void MimeDecode::init()
+{ MimeDecodeContextData::init(); }
+
 void MimeDecode::reset_decoded_bytes()
 {
     if (decoder)
@@ -66,7 +70,7 @@ void MimeDecode::process_decode_type(const char* start, int length, bool cnt_xf,
                 if (mime_stats)
                     mime_stats->b64_attachments++;
                 decoder = new B64Decode(config->get_max_depth(config->get_b64_depth()),
-                    config->get_b64_depth());
+                        config->get_b64_depth());
                 return;
             }
         }
@@ -81,7 +85,7 @@ void MimeDecode::process_decode_type(const char* start, int length, bool cnt_xf,
                 if (mime_stats)
                     mime_stats->qp_attachments++;
                 decoder = new QPDecode(config->get_max_depth(config->get_qp_depth()),
-                    config->get_qp_depth());
+                        config->get_qp_depth());
                 return;
             }
         }
@@ -96,7 +100,7 @@ void MimeDecode::process_decode_type(const char* start, int length, bool cnt_xf,
                 if (mime_stats)
                     mime_stats->uu_attachments++;
                 decoder = new UUDecode(config->get_max_depth(config->get_uu_depth()),
-                    config->get_uu_depth());
+                        config->get_uu_depth());
                 return;
             }
         }
@@ -115,7 +119,8 @@ void MimeDecode::process_decode_type(const char* start, int length, bool cnt_xf,
 
 DecodeResult MimeDecode::decode_data(const uint8_t* start, const uint8_t* end)
 {
-    return (decoder ? decoder->decode_data(start,end) : DECODE_SUCCESS);
+    uint8_t* decode_buf = MimeDecodeContextData::get_decode_buf();
+    return (decoder ? decoder->decode_data(start,end, decode_buf) : DECODE_SUCCESS);
 }
 
 int MimeDecode::get_detection_depth()
index 87b484e5f207251ec384cd180fa9baec4f2e60c2..e2ade2a4116dacf2900c334ea17edc4f4ea23017 100644 (file)
@@ -15,7 +15,7 @@
 // with this program; if not, write to the Free Software Foundation, Inc.,
 // 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 //--------------------------------------------------------------------------
-// sf_email_attach_decode.h author Bhagyashree Bantwal <bbantwal@cisco.com>
+// file_mime_decode.h author Bhagya Bantwal <bbantwal@cisco.com>
 
 #ifndef FILE_MIME_DECODE_H
 #define FILE_MIME_DECODE_H
@@ -75,6 +75,8 @@ public:
 
     DecodeType get_decode_type();
 
+    static void init();
+
 private:
     DecodeType decode_type = DECODE_NONE;
     snort::DecodeConfig* config;
index a454800e9d99bf15096e5cde4a02c92e6f7d0223..6eec1b185f297a0be95fbd4284d34b73b5bf0fb5 100644 (file)
@@ -689,6 +689,8 @@ void MimeSession::init()
 {
     const MimeToken* tmp;
 
+    MimeDecode::init();
+
     /* Header search */
     mime_hdr_search_mpse = new SearchTool;
     if (mime_hdr_search_mpse == nullptr)
index 82206335b67f857a8bbf11385a58a26d23300f4b..12d912bb000adcbfa435ccfe924c8e0cc4b89083 100644 (file)
@@ -117,13 +117,7 @@ void DceContextData::set_current_ropts(DCE2_SsnData* sd)
     if ( !ips_id )
         return;
 
-    DceContextData* dcd = (DceContextData*)DetectionEngine::get_data(ips_id);
-
-    if ( !dcd )
-    {
-        dcd = new DceContextData;
-        DetectionEngine::set_data(ips_id, dcd);
-    }
+    DceContextData* dcd = IpsContextData::get<DceContextData>(ips_id);
 
     if ( !dcd->current_ropts )
     {
index 8b0ed07734167731c3d865c5310ff0af21b377f1..e4c227a005226b5e17650fffdf3f6ad92a4dbe1f 100644 (file)
@@ -20,7 +20,7 @@
 #ifndef DCE_CONTEXT_DATA_H
 #define DCE_CONTEXT_DATA_H
 
-#include "detection/ips_context.h"
+#include "detection/ips_context_data.h"
 #include "dce_utils.h"
 
 struct DCE2_Roptions;
index 56c02e98e76f53e3e7fa0e67c72b0d1a7c5982fe..a6b3b239e28d21ba5c04ed2d8e6f65f4ee3e2ed6 100644 (file)
@@ -26,7 +26,7 @@
 #include "gtp_inspect.h"
 
 #include "detection/detection_engine.h"
-#include "detection/ips_context.h"
+#include "detection/ips_context_data.h"
 #include "managers/inspector_manager.h"
 #include "profiler/profiler.h"
 #include "protocols/packet.h"
@@ -87,13 +87,8 @@ public:
 
 GTP_IEData* get_infos()
 {
-    GtpContextData* gcd = (GtpContextData*)DetectionEngine::get_data(ips_id);
+    GtpContextData* gcd = IpsContextData::get<GtpContextData>(ips_id);
 
-    if ( !gcd )
-    {
-        gcd = new GtpContextData;
-        DetectionEngine::set_data(ips_id, gcd);
-    }
     return gcd->gtp_ies;
 }
 
index 8cde97e6852052fcc9544d1a6cd09ee3e570d6ed..ca8b3e203de73082ba3a299ceabfa19f883bcbf1 100644 (file)
@@ -44,13 +44,7 @@ HttpMsgSection* HttpContextData::get_snapshot(const Packet* p)
 
 void HttpContextData::save_snapshot(HttpMsgSection* section)
 {
-    HttpContextData* hcd = (HttpContextData*)DetectionEngine::get_data(HttpContextData::ips_id);
-
-    if ( !hcd )
-    {
-        hcd = new HttpContextData;
-        DetectionEngine::set_data(HttpContextData::ips_id, hcd);
-    }
+    HttpContextData* hcd = IpsContextData::get<HttpContextData>(HttpContextData::ips_id);
 
     hcd->current_section = section;
     section->add_ips_context(DetectionEngine::get_context());
index 9396d0eed4d362a55cb4f1957fbf5766266dd68c..52841710ac96081f5355385a1e4a09d0a8a8282b 100644 (file)
@@ -20,7 +20,7 @@
 #ifndef HTTP_CONTEXT_DATA_H
 #define HTTP_CONTEXT_DATA_H
 
-#include "detection/ips_context.h"
+#include "detection/ips_context_data.h"
 
 class HttpMsgSection;