htp: simplify streaming buffer config

Use a single static config instead of the per profile config.
pull/8426/head
Victor Julien 4 years ago
parent c79c0ca347
commit d0f1507c83

@ -33,8 +33,7 @@
#include "util-streaming-buffer.h" #include "util-streaming-buffer.h"
#include "util-print.h" #include "util-print.h"
static StreamingBufferConfig default_cfg = { 3072, 1, STREAMING_BUFFER_REGION_GAP_DEFAULT, extern StreamingBufferConfig htp_sbcfg;
HTPCalloc, HTPRealloc, HTPFree };
/** /**
* \brief Append a chunk of body to the HtpBody struct * \brief Append a chunk of body to the HtpBody struct
@ -58,8 +57,7 @@ int HtpBodyAppendChunk(const HTPCfgDir *hcfg, HtpBody *body,
} }
if (body->sb == NULL) { if (body->sb == NULL) {
const StreamingBufferConfig *cfg = hcfg ? &hcfg->sbcfg : &default_cfg; body->sb = StreamingBufferInit(&htp_sbcfg);
body->sb = StreamingBufferInit(cfg);
if (body->sb == NULL) if (body->sb == NULL)
SCReturnInt(-1); SCReturnInt(-1);
} }
@ -70,7 +68,7 @@ int HtpBodyAppendChunk(const HTPCfgDir *hcfg, HtpBody *body,
SCReturnInt(-1); SCReturnInt(-1);
} }
if (StreamingBufferAppend(body->sb, &hcfg->sbcfg, &bd->sbseg, data, len) != 0) { if (StreamingBufferAppend(body->sb, &htp_sbcfg, &bd->sbseg, data, len) != 0) {
HTPFree(bd, sizeof(HtpBodyChunk)); HTPFree(bd, sizeof(HtpBodyChunk));
SCReturnInt(-1); SCReturnInt(-1);
} }
@ -138,7 +136,7 @@ void HtpBodyFree(const HTPCfgDir *hcfg, HtpBody *body)
} }
body->first = body->last = NULL; body->first = body->last = NULL;
StreamingBufferFree(body->sb, &hcfg->sbcfg); StreamingBufferFree(body->sb, &htp_sbcfg);
} }
/** /**
@ -187,7 +185,7 @@ void HtpBodyPrune(HtpState *state, HtpBody *body, int direction)
if (left_edge) { if (left_edge) {
SCLogDebug("sliding body to offset %"PRIu64, left_edge); SCLogDebug("sliding body to offset %"PRIu64, left_edge);
StreamingBufferSlideToOffset(body->sb, &cfg->sbcfg, left_edge); StreamingBufferSlideToOffset(body->sb, &htp_sbcfg, left_edge);
} }
SCLogDebug("pruning chunks of body %p", body); SCLogDebug("pruning chunks of body %p", body);

@ -29,6 +29,8 @@
#include "app-layer-htp-range.h" #include "app-layer-htp-range.h"
#include "util-validate.h" #include "util-validate.h"
extern StreamingBufferConfig htp_sbcfg;
/** /**
* \brief Open the file with "filename" and pass the first chunk * \brief Open the file with "filename" and pass the first chunk
* of data if any. * of data if any.
@ -50,27 +52,22 @@ int HTPFileOpen(HtpState *s, HtpTxUserData *tx, const uint8_t *filename, uint16_
int retval = 0; int retval = 0;
uint16_t flags = 0; uint16_t flags = 0;
FileContainer *files = NULL; FileContainer *files = NULL;
const StreamingBufferConfig *sbcfg = NULL;
SCLogDebug("data %p data_len %"PRIu32, data, data_len); SCLogDebug("data %p data_len %"PRIu32, data, data_len);
if (direction & STREAM_TOCLIENT) { if (direction & STREAM_TOCLIENT) {
files = &tx->files_tc; files = &tx->files_tc;
flags = FileFlowFlagsToFlags(tx->tx_data.file_flags, STREAM_TOCLIENT); flags = FileFlowFlagsToFlags(tx->tx_data.file_flags, STREAM_TOCLIENT);
sbcfg = &s->cfg->response.sbcfg;
// we shall not open a new file if there is a current one // we shall not open a new file if there is a current one
DEBUG_VALIDATE_BUG_ON(tx->file_range != NULL); DEBUG_VALIDATE_BUG_ON(tx->file_range != NULL);
} else { } else {
files = &tx->files_ts; files = &tx->files_ts;
flags = FileFlowFlagsToFlags(tx->tx_data.file_flags, STREAM_TOSERVER); flags = FileFlowFlagsToFlags(tx->tx_data.file_flags, STREAM_TOSERVER);
sbcfg = &s->cfg->request.sbcfg;
} }
if (FileOpenFileWithId(files, sbcfg, s->file_track_id++, if (FileOpenFileWithId(files, &htp_sbcfg, s->file_track_id++, filename, filename_len, data,
filename, filename_len, data_len, flags) != 0) {
data, data_len, flags) != 0)
{
retval = -1; retval = -1;
} }
@ -159,8 +156,8 @@ int HTPFileOpenWithRange(HtpState *s, HtpTxUserData *txud, const uint8_t *filena
FileContainer *files = &txud->files_tc; FileContainer *files = &txud->files_tc;
// we open a file for this specific range // we open a file for this specific range
if (FileOpenFileWithId(files, &s->cfg->response.sbcfg, s->file_track_id++, filename, if (FileOpenFileWithId(files, &htp_sbcfg, s->file_track_id++, filename, filename_len, data,
filename_len, data, data_len, flags) != 0) { data_len, flags) != 0) {
SCReturnInt(-1); SCReturnInt(-1);
} }
txud->tx_data.files_opened++; txud->tx_data.files_opened++;
@ -189,8 +186,8 @@ int HTPFileOpenWithRange(HtpState *s, HtpTxUserData *txud, const uint8_t *filena
SCReturnInt(0); SCReturnInt(0);
} }
DEBUG_VALIDATE_BUG_ON(htud->file_range); DEBUG_VALIDATE_BUG_ON(htud->file_range);
htud->file_range = HttpRangeContainerOpenFile(keyurl, keylen, s->f, &crparsed, htud->file_range = HttpRangeContainerOpenFile(keyurl, keylen, s->f, &crparsed, &htp_sbcfg,
&s->cfg->response.sbcfg, filename, filename_len, flags, data, data_len); filename, filename_len, flags, data, data_len);
SCFree(keyurl); SCFree(keyurl);
if (htud->file_range == NULL) { if (htud->file_range == NULL) {
SCReturnInt(-1); SCReturnInt(-1);
@ -219,14 +216,11 @@ int HTPFileStoreChunk(
int retval = 0; int retval = 0;
int result = 0; int result = 0;
FileContainer *files = NULL; FileContainer *files = NULL;
const StreamingBufferConfig *sbcfg = NULL;
if (direction & STREAM_TOCLIENT) { if (direction & STREAM_TOCLIENT) {
files = &tx->files_tc; files = &tx->files_tc;
sbcfg = &s->cfg->response.sbcfg;
} else { } else {
files = &tx->files_ts; files = &tx->files_ts;
sbcfg = &s->cfg->request.sbcfg;
} }
SCLogDebug("files %p data %p data_len %" PRIu32, files, data, data_len); SCLogDebug("files %p data %p data_len %" PRIu32, files, data, data_len);
@ -237,12 +231,12 @@ int HTPFileStoreChunk(
} }
if (tx->file_range != NULL) { if (tx->file_range != NULL) {
if (HttpRangeAppendData(sbcfg, tx->file_range, data, data_len) < 0) { if (HttpRangeAppendData(&htp_sbcfg, tx->file_range, data, data_len) < 0) {
SCLogDebug("Failed to append data"); SCLogDebug("Failed to append data");
} }
} }
result = FileAppendData(files, sbcfg, data, data_len); result = FileAppendData(files, &htp_sbcfg, data, data_len);
if (result == -1) { if (result == -1) {
SCLogDebug("appending data failed"); SCLogDebug("appending data failed");
retval = -1; retval = -1;
@ -311,14 +305,11 @@ int HTPFileClose(HtpState *s, HtpTxUserData *tx, const uint8_t *data, uint32_t d
int retval = 0; int retval = 0;
int result = 0; int result = 0;
FileContainer *files = NULL; FileContainer *files = NULL;
const StreamingBufferConfig *sbcfg = NULL;
if (direction & STREAM_TOCLIENT) { if (direction & STREAM_TOCLIENT) {
files = &tx->files_tc; files = &tx->files_tc;
sbcfg = &s->cfg->response.sbcfg;
} else { } else {
files = &tx->files_ts; files = &tx->files_ts;
sbcfg = &s->cfg->request.sbcfg;
} }
SCLogDebug("files %p data %p data_len %" PRIu32, files, data, data_len); SCLogDebug("files %p data %p data_len %" PRIu32, files, data, data_len);
@ -328,7 +319,7 @@ int HTPFileClose(HtpState *s, HtpTxUserData *tx, const uint8_t *data, uint32_t d
goto end; goto end;
} }
result = FileCloseFile(files, sbcfg, data, data_len, flags); result = FileCloseFile(files, &htp_sbcfg, data, data_len, flags);
if (result == -1) { if (result == -1) {
retval = -1; retval = -1;
} else if (result == -2) { } else if (result == -2) {
@ -337,7 +328,8 @@ int HTPFileClose(HtpState *s, HtpTxUserData *tx, const uint8_t *data, uint32_t d
SCLogDebug("result %u", result); SCLogDebug("result %u", result);
if (tx->file_range != NULL) { if (tx->file_range != NULL) {
bool added = HTPFileCloseHandleRange(sbcfg, files, flags, tx->file_range, data, data_len); bool added =
HTPFileCloseHandleRange(&htp_sbcfg, files, flags, tx->file_range, data, data_len);
if (added) { if (added) {
tx->tx_data.files_opened++; tx->tx_data.files_opened++;
} }

@ -93,6 +93,8 @@ static SCRadixTree *cfgtree;
/** List of HTP configurations. */ /** List of HTP configurations. */
static HTPCfgRec cfglist; static HTPCfgRec cfglist;
StreamingBufferConfig htp_sbcfg = STREAMING_BUFFER_CONFIG_INITIALIZER;
/** Limit to the number of libhtp messages that can be handled */ /** Limit to the number of libhtp messages that can be handled */
#define HTP_MAX_MESSAGES 512 #define HTP_MAX_MESSAGES 512
@ -369,12 +371,11 @@ static void HtpTxUserDataFree(HtpState *state, HtpTxUserData *htud)
DetectEngineStateFree(htud->tx_data.de_state); DetectEngineStateFree(htud->tx_data.de_state);
} }
if (htud->file_range) { if (htud->file_range) {
HTPFileCloseHandleRange( HTPFileCloseHandleRange(&htp_sbcfg, &htud->files_tc, 0, htud->file_range, NULL, 0);
&state->cfg->response.sbcfg, &htud->files_tc, 0, htud->file_range, NULL, 0);
HttpRangeFreeBlock(htud->file_range); HttpRangeFreeBlock(htud->file_range);
} }
FileContainerRecycle(&htud->files_ts, &state->cfg->request.sbcfg); FileContainerRecycle(&htud->files_ts, &htp_sbcfg);
FileContainerRecycle(&htud->files_tc, &state->cfg->response.sbcfg); FileContainerRecycle(&htud->files_tc, &htp_sbcfg);
HTPFree(htud, sizeof(HtpTxUserData)); HTPFree(htud, sizeof(HtpTxUserData));
} }
} }
@ -2552,18 +2553,6 @@ static void HTPConfigSetDefaultsPhase2(const char *name, HTPCfgRec *cfg_prec)
} }
htp_config_register_request_line(cfg_prec->cfg, HTPCallbackRequestLine); htp_config_register_request_line(cfg_prec->cfg, HTPCallbackRequestLine);
cfg_prec->request.sbcfg.buf_size =
cfg_prec->request.inspect_window ? cfg_prec->request.inspect_window : 256;
cfg_prec->request.sbcfg.Calloc = HTPCalloc;
cfg_prec->request.sbcfg.Realloc = HTPRealloc;
cfg_prec->request.sbcfg.Free = HTPFree;
cfg_prec->response.sbcfg.buf_size =
cfg_prec->response.inspect_window ? cfg_prec->response.inspect_window : 256;
cfg_prec->response.sbcfg.Calloc = HTPCalloc;
cfg_prec->response.sbcfg.Realloc = HTPRealloc;
cfg_prec->response.sbcfg.Free = HTPFree;
return; return;
} }
@ -2943,6 +2932,10 @@ void HTPConfigure(void)
cfglist.next = NULL; cfglist.next = NULL;
htp_sbcfg.Calloc = HTPCalloc;
htp_sbcfg.Realloc = HTPRealloc;
htp_sbcfg.Free = HTPFree;
cfgtree = SCRadixCreateRadixTree(NULL, NULL); cfgtree = SCRadixCreateRadixTree(NULL, NULL);
if (NULL == cfgtree) if (NULL == cfgtree)
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
@ -3026,17 +3019,14 @@ void AppLayerHtpPrintStats(void)
*/ */
static AppLayerGetFileState HTPGetTxFiles(void *state, void *txv, uint8_t direction) static AppLayerGetFileState HTPGetTxFiles(void *state, void *txv, uint8_t direction)
{ {
AppLayerGetFileState files = { .fc = NULL, .cfg = NULL }; AppLayerGetFileState files = { .fc = NULL, .cfg = &htp_sbcfg };
HtpState *s = state;
htp_tx_t *tx = (htp_tx_t *)txv; htp_tx_t *tx = (htp_tx_t *)txv;
HtpTxUserData *tx_ud = htp_tx_get_user_data(tx); HtpTxUserData *tx_ud = htp_tx_get_user_data(tx);
if (tx_ud) { if (tx_ud) {
if (direction & STREAM_TOCLIENT) { if (direction & STREAM_TOCLIENT) {
files.fc = &tx_ud->files_tc; files.fc = &tx_ud->files_tc;
files.cfg = &s->cfg->response.sbcfg;
} else { } else {
files.fc = &tx_ud->files_ts; files.fc = &tx_ud->files_ts;
files.cfg = &s->cfg->request.sbcfg;
} }
} }
return files; return files;

@ -149,7 +149,6 @@ typedef struct HTPCfgDir_ {
uint32_t body_limit; uint32_t body_limit;
uint32_t inspect_min_size; uint32_t inspect_min_size;
uint32_t inspect_window; uint32_t inspect_window;
StreamingBufferConfig sbcfg;
} HTPCfgDir; } HTPCfgDir;
/** Need a linked list in order to keep track of these */ /** Need a linked list in order to keep track of these */

Loading…
Cancel
Save