From 3908166f91485b1c8b784db6fbe304c3d5b0d7a8 Mon Sep 17 00:00:00 2001 From: Eric Leblond Date: Mon, 8 Feb 2021 21:46:26 +0100 Subject: [PATCH] stream: count realloc in memcap TCP memory cap was not taking into account the memory that can be used by realloc of Packet headers in TCP segments. --- src/stream-tcp-list.c | 6 +++--- src/stream-tcp-reassemble.c | 4 ++-- src/stream-tcp-reassemble.h | 1 + 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/stream-tcp-list.c b/src/stream-tcp-list.c index c7baca3218..5bced3ede2 100644 --- a/src/stream-tcp-list.c +++ b/src/stream-tcp-list.c @@ -595,9 +595,9 @@ static void StreamTcpSegmentAddPacketData( * need to check that this is sufficient and allocate more memory if * not. */ - if (GET_PKT_LEN(rp) - p->payload_len > seg->pcap_hdr_storage->alloclen) { - uint8_t *tmp_pkt_hdr = - SCRealloc(seg->pcap_hdr_storage->pkt_hdr, GET_PKT_LEN(rp) - p->payload_len); + if (seg->pcap_hdr_storage->pktlen > seg->pcap_hdr_storage->alloclen) { + uint8_t *tmp_pkt_hdr = StreamTcpReassembleRealloc(seg->pcap_hdr_storage->pkt_hdr, + seg->pcap_hdr_storage->alloclen, seg->pcap_hdr_storage->pktlen); if (tmp_pkt_hdr == NULL) { SCLogDebug("Failed to realloc"); seg->pcap_hdr_storage->ts.tv_sec = 0; diff --git a/src/stream-tcp-reassemble.c b/src/stream-tcp-reassemble.c index ae8f7a80ec..bf634f5bb6 100644 --- a/src/stream-tcp-reassemble.c +++ b/src/stream-tcp-reassemble.c @@ -224,7 +224,7 @@ static void *ReassembleCalloc(size_t n, size_t size) /* void *(*Realloc)(void *ptr, size_t orig_size, size_t size); */ -static void *ReassembleRealloc(void *optr, size_t orig_size, size_t size) +void *StreamTcpReassembleRealloc(void *optr, size_t orig_size, size_t size) { if (size > orig_size) { if (StreamTcpReassembleCheckMemcap(size - orig_size) == 0) @@ -484,7 +484,7 @@ static int StreamTcpReassemblyConfig(bool quiet) stream_config.sbcnf.buf_size = 2048; stream_config.sbcnf.Malloc = ReassembleMalloc; stream_config.sbcnf.Calloc = ReassembleCalloc; - stream_config.sbcnf.Realloc = ReassembleRealloc; + stream_config.sbcnf.Realloc = StreamTcpReassembleRealloc; stream_config.sbcnf.Free = ReassembleFree; return 0; diff --git a/src/stream-tcp-reassemble.h b/src/stream-tcp-reassemble.h index d62e27b045..e3ec460f28 100644 --- a/src/stream-tcp-reassemble.h +++ b/src/stream-tcp-reassemble.h @@ -85,6 +85,7 @@ void StreamTcpReassembleInitMemuse(void); int StreamTcpReassembleHandleSegment(ThreadVars *, TcpReassemblyThreadCtx *, TcpSession *, TcpStream *, Packet *, PacketQueueNoLock *); int StreamTcpReassembleInit(bool); void StreamTcpReassembleFree(bool); +void *StreamTcpReassembleRealloc(void *optr, size_t orig_size, size_t size); void StreamTcpReassembleRegisterTests(void); TcpReassemblyThreadCtx *StreamTcpReassembleInitThreadCtx(ThreadVars *tv); void StreamTcpReassembleFreeThreadCtx(TcpReassemblyThreadCtx *);