Skip to content

Commit

Permalink
app-layer/smtp: Improve RSET handling
Browse files Browse the repository at this point in the history
This commit improves how the parser handles the `RSET` command.
Termination of the transaction occurs when the `RSET` ack is seen (reply
code 250).

Bug: #3592
  • Loading branch information
jlucovsky authored and victorjulien committed Apr 28, 2020
1 parent 554b0e4 commit 5b130bd
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/app-layer-smtp.c
Expand Up @@ -101,6 +101,7 @@
#define SMTP_COMMAND_DATA_MODE 4
/* All other commands are represented by this var */
#define SMTP_COMMAND_OTHER_CMD 5
#define SMTP_COMMAND_RSET 6

/* Different EHLO extensions. Not used now. */
#define SMTP_EHLO_EXTENSION_PIPELINING
Expand Down Expand Up @@ -880,6 +881,13 @@ static void SetMimeEvents(SMTPState *state)
}
}

static inline void SMTPTransactionComplete(SMTPState *state)
{
DEBUG_VALIDATE_BUG_ON(state->curr_tx == NULL);
if (state->curr_tx)
state->curr_tx->done = 1;
}

/**
* \retval 0 ok
* \retval -1 error
Expand Down Expand Up @@ -916,7 +924,7 @@ static int SMTPProcessCommandDATA(SMTPState *state, Flow *f,
/* Generate decoder events */
SetMimeEvents(state);
}
state->curr_tx->done = 1;
SMTPTransactionComplete(state);
SCLogDebug("marked tx as done");
} else if (smtp_config.raw_extraction) {
// message not over, store the line. This is a substitution of
Expand Down Expand Up @@ -1031,7 +1039,7 @@ static int SMTPProcessReply(SMTPState *state, Flow *f,
/* we are entering STARRTTLS data mode */
state->parser_state |= SMTP_PARSER_STATE_COMMAND_DATA_MODE;
AppLayerRequestProtocolTLSUpgrade(f);
state->curr_tx->done = 1;
SMTPTransactionComplete(state);
} else {
/* decoder event */
SMTPSetEvent(state, SMTP_DECODER_EVENT_TLS_REJECTED);
Expand Down Expand Up @@ -1063,6 +1071,12 @@ static int SMTPProcessReply(SMTPState *state, Flow *f,
}
}

if (state->cmds_idx < state->cmds_buffer_len && state->cmds[state->cmds_idx] == SMTP_COMMAND_RSET) {
if (reply_code == SMTP_REPLY_250) {
SMTPTransactionComplete(state);
}
}

/* if we have matched all the buffered commands, reset the cnt and index */
if (state->cmds_idx == state->cmds_cnt) {
state->cmds_cnt = 0;
Expand Down Expand Up @@ -1321,7 +1335,7 @@ static int SMTPProcessRequest(SMTPState *state, Flow *f,
SCMemcmpLowercase("rset", state->current_line, 4) == 0) {
// Resets chunk index in case of connection reuse
state->bdat_chunk_idx = 0;
state->curr_tx->done = 1;
state->current_command = SMTP_COMMAND_RSET;
} else {
state->current_command = SMTP_COMMAND_OTHER_CMD;
}
Expand Down

0 comments on commit 5b130bd

Please sign in to comment.