]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
ftp: truncate command data that is too long
authorJason Ish <jason.ish@oisf.net>
Wed, 6 Apr 2022 21:38:35 +0000 (15:38 -0600)
committerVictor Julien <vjulien@oisf.net>
Thu, 21 Apr 2022 05:37:58 +0000 (07:37 +0200)
FTP control commands will be buffered forever until a new line is seen,
this can lead to memory exhaustion in Suricata.

To fix, set an upper bound, 4096 bytes on the size of the command that
is saved in the transaction. The input continues to be parsed to find
the end of the command so the parser can continue to move onto the next
command.

The result is that the command data in the transaction is truncated,
which also shows up in the ftp transaction logs.

This value is configurable with the max-line-length field in the ftp
app-layer.protocols section.

As FTP doesn't have events at this time, add a new fields to eve-log
that specificy if the request, or the response has been truncated.

Ticket #5024

(cherry-picked from commit 433a0b8e3b0bf206069ebb94bc4cb71044572791)

src/app-layer-ftp.c
src/app-layer-ftp.h
src/output-json-ftp.c

index 29c12f16e7dd7d0ec71b6746bee474fc3d370c1c..63d5dc21fd83c392d4ae5c9a03829e6524bfba64 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 2007-2017 Open Information Security Foundation
+/* Copyright (C) 2007-2022 Open Information Security Foundation
  *
  * You can copy, redistribute or modify this Program under the terms of
  * the GNU General Public License version 2 as published by the Free
@@ -58,6 +58,7 @@
 #include "util-byte.h"
 #include "util-mem.h"
 #include "util-misc.h"
+#include "util-validate.h"
 
 #include "rust-ftp-mod-gen.h"
 
@@ -127,6 +128,7 @@ const FtpCommand FtpCommands[FTP_COMMAND_MAX + 1] = {
     { FTP_COMMAND_UNKNOWN, NULL, 0}
 };
 uint64_t ftp_config_memcap = 0;
+uint32_t ftp_max_line_len = 4096;
 
 SC_ATOMIC_DECLARE(uint64_t, ftp_memuse);
 SC_ATOMIC_DECLARE(uint64_t, ftp_memcap);
@@ -154,6 +156,14 @@ static void FTPParseMemcap(void)
 
     SC_ATOMIC_INIT(ftp_memuse);
     SC_ATOMIC_INIT(ftp_memcap);
+
+    if ((ConfGet("app-layer.protocols.ftp.max-line-length", &conf_val)) == 1) {
+        if (ParseSizeStringU32(conf_val, &ftp_max_line_len) < 0) {
+            SCLogError(SC_ERR_SIZE_PARSE, "Error parsing ftp.max-line-length from conf file - %s.",
+                    conf_val);
+        }
+        SCLogConfig("FTP max line length: %" PRIu32, ftp_max_line_len);
+    }
 }
 
 static void FTPIncrMemuse(uint64_t size)
@@ -355,6 +365,7 @@ static int FTPGetLineForDirection(FtpState *state, FtpLineState *line_state)
         /* we have seen the lf for the previous line.  Clear the parser
          * details to parse new line */
         line_state->current_line_lf_seen = 0;
+        state->current_line_truncated = false;
         if (line_state->current_line_db == 1) {
             line_state->current_line_db = 0;
             FTPFree(line_state->db, line_state->db_len);
@@ -382,20 +393,27 @@ static int FTPGetLineForDirection(FtpState *state, FtpLineState *line_state)
             line_state->current_line_db = 1;
             memcpy(line_state->db, state->input, state->input_len);
             line_state->db_len = state->input_len;
-        } else {
-            ptmp = FTPRealloc(line_state->db, line_state->db_len,
-                             (line_state->db_len + state->input_len));
-            if (ptmp == NULL) {
-                FTPFree(line_state->db, line_state->db_len);
-                line_state->db = NULL;
-                line_state->db_len = 0;
-                return -1;
+        } else if (!state->current_line_truncated) {
+            int32_t input_len = state->input_len;
+            if (line_state->db_len + input_len > ftp_max_line_len) {
+                input_len = ftp_max_line_len - line_state->db_len;
+                DEBUG_VALIDATE_BUG_ON(input_len < 0);
+                state->current_line_truncated = true;
             }
-            line_state->db = ptmp;
+            if (input_len > 0) {
+                ptmp = FTPRealloc(
+                        line_state->db, line_state->db_len, (line_state->db_len + input_len));
+                if (ptmp == NULL) {
+                    FTPFree(line_state->db, line_state->db_len);
+                    line_state->db = NULL;
+                    line_state->db_len = 0;
+                    return -1;
+                }
+                line_state->db = ptmp;
 
-            memcpy(line_state->db + line_state->db_len,
-                   state->input, state->input_len);
-            line_state->db_len += state->input_len;
+                memcpy(line_state->db + line_state->db_len, state->input, input_len);
+                line_state->db_len += input_len;
+            }
         }
         state->input += state->input_len;
         state->input_len = 0;
@@ -406,27 +424,35 @@ static int FTPGetLineForDirection(FtpState *state, FtpLineState *line_state)
         line_state->current_line_lf_seen = 1;
 
         if (line_state->current_line_db == 1) {
-            ptmp = FTPRealloc(line_state->db, line_state->db_len,
-                             (line_state->db_len + (lf_idx + 1 - state->input)));
-            if (ptmp == NULL) {
-                FTPFree(line_state->db, line_state->db_len);
-                line_state->db = NULL;
-                line_state->db_len = 0;
-                return -1;
-            }
-            line_state->db = ptmp;
+            if (!state->current_line_truncated) {
+                int32_t input_len = lf_idx + 1 - state->input;
+                if (line_state->db_len + input_len > ftp_max_line_len) {
+                    input_len = ftp_max_line_len - line_state->db_len;
+                    DEBUG_VALIDATE_BUG_ON(input_len < 0);
+                    state->current_line_truncated = true;
+                }
+                if (input_len > 0) {
+                    ptmp = FTPRealloc(
+                            line_state->db, line_state->db_len, (line_state->db_len + input_len));
+                    if (ptmp == NULL) {
+                        FTPFree(line_state->db, line_state->db_len);
+                        line_state->db = NULL;
+                        line_state->db_len = 0;
+                        return -1;
+                    }
+                    line_state->db = ptmp;
 
-            memcpy(line_state->db + line_state->db_len,
-                   state->input, (lf_idx + 1 - state->input));
-            line_state->db_len += (lf_idx + 1 - state->input);
+                    memcpy(line_state->db + line_state->db_len, state->input, input_len);
+                    line_state->db_len += input_len;
 
-            if (line_state->db_len > 1 &&
-                line_state->db[line_state->db_len - 2] == 0x0D) {
-                line_state->db_len -= 2;
-                state->current_line_delimiter_len = 2;
-            } else {
-                line_state->db_len -= 1;
-                state->current_line_delimiter_len = 1;
+                    if (line_state->db_len > 1 && line_state->db[line_state->db_len - 2] == 0x0D) {
+                        line_state->db_len -= 2;
+                        state->current_line_delimiter_len = 2;
+                    } else {
+                        line_state->db_len -= 1;
+                        state->current_line_delimiter_len = 1;
+                    }
+                }
             }
 
             state->current_line = line_state->db;
@@ -434,7 +460,12 @@ static int FTPGetLineForDirection(FtpState *state, FtpLineState *line_state)
 
         } else {
             state->current_line = state->input;
-            state->current_line_len = lf_idx - state->input;
+            if (lf_idx - state->input > ftp_max_line_len) {
+                state->current_line_len = ftp_max_line_len;
+                state->current_line_truncated = true;
+            } else {
+                state->current_line_len = lf_idx - state->input;
+            }
 
             if (state->input != lf_idx &&
                 *(lf_idx - 1) == 0x0D) {
@@ -592,6 +623,7 @@ static int FTPParseRequest(Flow *f, void *ftp_state,
 
         tx->command_descriptor = cmd_descriptor;
         tx->request_length = CopyCommandLine(&tx->request, state->current_line, state->current_line_len);
+        tx->request_truncated = state->current_line_truncated;
 
         switch (state->command) {
             case FTP_COMMAND_EPRT:
@@ -804,6 +836,7 @@ static int FTPParseResponse(Flow *f, void *ftp_state, AppLayerParserState *pstat
             FTPString *response = FTPStringAlloc();
             if (likely(response)) {
                 response->len = CopyCommandLine(&response->str, state->current_line, state->current_line_len);
+                response->truncated = state->current_line_truncated;
                 TAILQ_INSERT_TAIL(&tx->response_list, response, next);
             }
         }
index c5ccab6e16d0d1b610250325d8b75ba40cb62050..879af61b2f63eb12041b3bd48bdf9b00461181d7 100644 (file)
@@ -119,13 +119,14 @@ typedef struct FtpLineState_ {
     uint8_t *db;
     uint32_t db_len;
     uint8_t current_line_db;
-    /** we have see LF for the currently parsed line */
+    /** we have seen LF for the currently parsed line */
     uint8_t current_line_lf_seen;
 } FtpLineState;
 
 typedef struct FTPString_ {
     uint8_t *str;
     uint32_t len;
+    bool truncated;
     TAILQ_ENTRY(FTPString_) next;
 } FTPString;
 
@@ -142,6 +143,7 @@ typedef struct FTPTransaction_  {
     /* for the request */
     uint32_t request_length;
     uint8_t *request;
+    bool request_truncated;
 
     /* for the command description */
     const FtpCommand *command_descriptor;
@@ -177,6 +179,7 @@ typedef struct FtpState_ {
     /** length of the line in current_line.  Doesn't include the delimiter */
     uint32_t current_line_len;
     uint8_t current_line_delimiter_len;
+    bool current_line_truncated;
 
     /* 0 for toserver, 1 for toclient */
     FtpLineState line_state[2];
index 1f8933166aa9693ec4d9f4b54d176f4140b6ab5d..e40634a804b007d976f8d6549697706aeb38c63a 100644 (file)
@@ -93,6 +93,9 @@ static json_t *JsonFTPLogCommand(Flow *f, FTPTransaction *tx)
     } else {
         json_object_set_new(cjs, "command_data", json_string(NULL));
     }
+    json_object_set_new(cjs, "command_truncated", json_boolean(tx->request_truncated));
+
+    bool reply_truncated = false;
 
     if (!TAILQ_EMPTY(&tx->response_list)) {
         FTPString *response;
@@ -106,6 +109,9 @@ static json_t *JsonFTPLogCommand(Flow *f, FTPTransaction *tx)
             } else if (response->len > UINT16_MAX) {
                 length = UINT16_MAX;
             }
+            if (!reply_truncated && response->truncated) {
+                reply_truncated = true;
+            }
             while ((pos = JsonGetNextLineFromBuffer((const char *)where, length)) != UINT16_MAX) {
                 uint16_t offset = 0;
                 /* Try to find a completion code for this line */
@@ -144,6 +150,7 @@ static json_t *JsonFTPLogCommand(Flow *f, FTPTransaction *tx)
 
     json_object_set_new(cjs, "reply_received",
             json_string((char *)(tx->done ? "yes" : "no")));
+    json_object_set_new(cjs, "reply_truncated", json_boolean(reply_truncated));
 
     return cjs;
 }