Cosmetic fixes

This commit is contained in:
Luca Deri
2021-02-03 11:52:32 +01:00
parent 1f3ade5923
commit 6e87daab69
+56 -45
View File
@@ -1485,22 +1485,22 @@ static void ndpi_compile_rce_regex() {
&pcreErrorOffset, NULL); &pcreErrorOffset, NULL);
if(comp_rx[i]->compiled == NULL) { if(comp_rx[i]->compiled == NULL) {
#ifdef DEBUG #ifdef DEBUG
NDPI_LOG_ERR(ndpi_str, "ERROR: Could not compile '%s': %s\n", rce_regex[i], NDPI_LOG_ERR(ndpi_str, "ERROR: Could not compile '%s': %s\n", rce_regex[i],
pcreErrorStr); pcreErrorStr);
#endif #endif
continue; continue;
} }
comp_rx[i]->optimized = pcre_study(comp_rx[i]->compiled, 0, &pcreErrorStr); comp_rx[i]->optimized = pcre_study(comp_rx[i]->compiled, 0, &pcreErrorStr);
#ifdef DEBUG #ifdef DEBUG
if(pcreErrorStr != NULL) { if(pcreErrorStr != NULL) {
NDPI_LOG_ERR(ndpi_str, "ERROR: Could not study '%s': %s\n", rce_regex[i], NDPI_LOG_ERR(ndpi_str, "ERROR: Could not study '%s': %s\n", rce_regex[i],
pcreErrorStr); pcreErrorStr);
} }
#endif #endif
} }
free((void *)pcreErrorStr); free((void *)pcreErrorStr);
@@ -1525,7 +1525,7 @@ static int ndpi_is_rce_injection(char* query) {
if (pcreExecRet >= 0) { if (pcreExecRet >= 0) {
return 1; return 1;
} }
#ifdef DEBUG #ifdef DEBUG
else { else {
switch(pcreExecRet) { switch(pcreExecRet) {
case PCRE_ERROR_NOMATCH: case PCRE_ERROR_NOMATCH:
@@ -1551,7 +1551,7 @@ static int ndpi_is_rce_injection(char* query) {
break; break;
} }
} }
#endif #endif
} }
size_t ushlen = sizeof(ush_commands) / sizeof(ush_commands[0]); size_t ushlen = sizeof(ush_commands) / sizeof(ush_commands[0]);
@@ -1856,8 +1856,8 @@ void shell_sort_array(sorter_index_item_t arr[], int n) {
/* ******************************************************************** */ /* ******************************************************************** */
#ifdef FRAG_MAN #ifdef FRAG_MAN
void free_fragment(fragments_wrapper_t *frag) void free_fragment(fragments_wrapper_t *frag) {
{ /* /*
* *
typedef struct fragment_wrapper { typedef struct fragment_wrapper {
uint16_t id; uint16_t id;
@@ -1893,8 +1893,9 @@ void free_fragment(fragments_wrapper_t *frag)
} }
} }
uint8_t add_segment_to_buffer( struct ndpi_flow_struct *flow, struct ndpi_tcphdr const * tcph, uint32_t new_waited_seq) { /* ******************************************************************** */
uint8_t add_segment_to_buffer( struct ndpi_flow_struct *flow, struct ndpi_tcphdr const * tcph, uint32_t new_expected_seq) {
DBGINFO("[flow:%p], dir: %d, seq:%u, ack:%u, len: %ubytes",flow,flow->packet.packet_direction, ntohl(tcph->seq), ntohl(tcph->ack_seq), flow->packet.payload_packet_len) DBGINFO("[flow:%p], dir: %d, seq:%u, ack:%u, len: %ubytes",flow,flow->packet.packet_direction, ntohl(tcph->seq), ntohl(tcph->ack_seq), flow->packet.payload_packet_len)
if (flow->tcp_segments_management) { if (flow->tcp_segments_management) {
@@ -1904,7 +1905,7 @@ uint8_t add_segment_to_buffer( struct ndpi_flow_struct *flow, struct ndpi_tcphdr
if ( fragW->ct_frag == 0 ) { if ( fragW->ct_frag == 0 ) {
if (fragW->fragments_list) { free_fragment(fragW); } if (fragW->fragments_list) { free_fragment(fragW); }
// initialize the offset with the first fragment seq number // initialize the offset with the first fragment seq number
fragW->initial_offset= new_waited_seq; fragW->initial_offset= new_expected_seq;
DBGTRACER("initialized initial_offset: %u)",fragW->initial_offset) DBGTRACER("initialized initial_offset: %u)",fragW->initial_offset)
} }
@@ -1955,11 +1956,13 @@ uint8_t add_segment_to_buffer( struct ndpi_flow_struct *flow, struct ndpi_tcphdr
} }
return 0; return 0;
} }
//TODO: manage partial retrasmission //TODO: manage partial retrasmission
uint32_t reassembly_fragment(struct ndpi_flow_struct *const flow, struct ndpi_tcphdr const * tcph, uint8_t **ret_buffer, size_t *len_buffer) /* ******************************************************************** */
{
DBGTRACER("tcph:%p, ret_buffer:%p, len_buffer:%u",tcph,ret_buffer,len_buffer) uint32_t reassembly_fragment(struct ndpi_flow_struct *const flow, struct ndpi_tcphdr const * tcph, uint8_t **ret_buffer, size_t *len_buffer) {
DBGTRACER("tcph:%p, ret_buffer:%p, len_buffer:%u",tcph,ret_buffer,len_buffer);
// reassemble the fragments... // reassemble the fragments...
uint32_t ret_value=0; uint32_t ret_value=0;
@@ -1968,8 +1971,9 @@ uint32_t reassembly_fragment(struct ndpi_flow_struct *const flow, struct ndpi_tc
fragments_wrapper_t *fragW= &flow->tcp_segments_list[flow->packet.packet_direction]; fragments_wrapper_t *fragW= &flow->tcp_segments_list[flow->packet.packet_direction];
// phase 1: calculate the size and fill the indexes array // phase 1: calculate the size and fill the indexes array
DBGINFO("phase 1: init sorter, calculate the size of buffer to reassemble: %u items", fragW->ct_frag) DBGINFO("phase 1: init sorter, calculate the size of buffer to reassemble: %u items", fragW->ct_frag);
sorter_index_item_t *sorted_indexes= ndpi_calloc( fragW->ct_frag, sizeof(sorter_index_item_t) ); sorter_index_item_t *sorted_indexes= ndpi_calloc( fragW->ct_frag, sizeof(sorter_index_item_t) );
DBGPOINTER("sorted_indexes=> %p",sorted_indexes) DBGPOINTER("sorted_indexes=> %p",sorted_indexes)
if ( !sorted_indexes ) { if ( !sorted_indexes ) {
fprintf(stderr, "[%8u] Not enough memory to sort the %u segments \n", fprintf(stderr, "[%8u] Not enough memory to sort the %u segments \n",
@@ -1984,11 +1988,11 @@ uint32_t reassembly_fragment(struct ndpi_flow_struct *const flow, struct ndpi_tc
sorted_indexes[i].sort_value= item->offset; sorted_indexes[i].sort_value= item->offset;
sorted_indexes[i].item_index= i; sorted_indexes[i].item_index= i;
tot_length += item->len; tot_length += item->len;
DBGTRACER("segment (%d): len:%lu, offset: %u => partial buffer len: %lu", i, (long unsigned int)item->len, (unsigned int)item->offset, (long unsigned int)tot_length) DBGTRACER("segment (%d): len:%lu, offset: %u => partial buffer len: %lu", i, (long unsigned int)item->len, (unsigned int)item->offset, (long unsigned int)tot_length);
} }
// phase 2: sorts fragments and check fragments and sequences // phase 2: sorts fragments and check fragments and sequences
DBGINFO(" phase 2 sorting %d segments and checking",fragW->ct_frag) DBGINFO(" phase 2 sorting %d segments and checking",fragW->ct_frag);
if (fragW->ct_frag>1) shell_sort_array(sorted_indexes, fragW->ct_frag); if (fragW->ct_frag>1) shell_sort_array(sorted_indexes, fragW->ct_frag);
// checks // checks
@@ -1996,7 +2000,7 @@ uint32_t reassembly_fragment(struct ndpi_flow_struct *const flow, struct ndpi_tc
fragment_t *item = (fragment_t*)fragW->fragments_list[ sorted_indexes[i].item_index ]; fragment_t *item = (fragment_t*)fragW->fragments_list[ sorted_indexes[i].item_index ];
// 1: no segment offset can be > tot_length // 1: no segment offset can be > tot_length
DBGTRACER("checking %d/%d element: offset=%lu vs t_length=%lu",i,sorted_indexes[i].item_index,(unsigned long)item->offset,(unsigned long)tot_length) DBGTRACER("checking %d/%d element: offset=%lu vs t_length=%lu",i,sorted_indexes[i].item_index,(unsigned long)item->offset,(unsigned long)tot_length);
if ( (item->offset) > (uint32_t)tot_length ) { if ( (item->offset) > (uint32_t)tot_length ) {
// update the last index of elements to elaborate // update the last index of elements to elaborate
DBGINFO("stop processing at %d/%d element: len= %u; offset= %u",i,sorted_indexes[i].item_index,(unsigned)length,(unsigned)item->offset) DBGINFO("stop processing at %d/%d element: len= %u; offset= %u",i,sorted_indexes[i].item_index,(unsigned)length,(unsigned)item->offset)
@@ -2008,7 +2012,7 @@ uint32_t reassembly_fragment(struct ndpi_flow_struct *const flow, struct ndpi_tc
// 2: for every len(buffer) must exists a offset fragment // 2: for every len(buffer) must exists a offset fragment
else if ( (item->offset) != (uint32_t)length ) { else if ( (item->offset) != (uint32_t)length ) {
// update the last index of elements to elaborate // update the last index of elements to elaborate
DBGINFO("checking %d/%d element: stop processing! len: %u; n_offset: %u", i,sorted_indexes[i].item_index,(unsigned)length,(unsigned)item->offset) DBGINFO("checking %d/%d element: stop processing! len: %u; n_offset: %u", i,sorted_indexes[i].item_index,(unsigned)length,(unsigned)item->offset);
tot_length= length; tot_length= length;
ret_value= (fragW->initial_offset)+(item->offset); // set the first offset to wait for the next segment ret_value= (fragW->initial_offset)+(item->offset); // set the first offset to wait for the next segment
@@ -2022,9 +2026,11 @@ uint32_t reassembly_fragment(struct ndpi_flow_struct *const flow, struct ndpi_tc
last_item++; // index to number aligment last_item++; // index to number aligment
// phase 3: allocate memory and fill the buffer // phase 3: allocate memory and fill the buffer
DBGINFO("phase 3: allocate memory for %u items and fill the buffer tot: %lu", last_item, (unsigned long int)tot_length) DBGINFO("phase 3: allocate memory for %u items and fill the buffer tot: %lu", last_item, (unsigned long int)tot_length);
uint8_t *buffer= ndpi_calloc(tot_length, sizeof(uint8_t)); uint8_t *buffer= ndpi_calloc(tot_length, sizeof(uint8_t));
DBGPOINTER("buffer (len:%lu)=> %p",(unsigned long)tot_length,buffer)
DBGPOINTER("buffer (len:%lu)=> %p",(unsigned long)tot_length,buffer);
if ( !buffer ) { if ( !buffer ) {
fprintf(stderr, "[%8u] Not enough memory for buffer for %u segments \n", flow->packet_counter,last_item); fprintf(stderr, "[%8u] Not enough memory for buffer for %u segments \n", flow->packet_counter,last_item);
free_fragment(fragW); free_fragment(fragW);
@@ -2036,7 +2042,7 @@ uint32_t reassembly_fragment(struct ndpi_flow_struct *const flow, struct ndpi_tc
fragment_t *item = (fragment_t*)fragW->fragments_list[sorted_indexes[i].item_index]; fragment_t *item = (fragment_t*)fragW->fragments_list[sorted_indexes[i].item_index];
//uint_t blk_sz= (tot_length - item->offset) > item->len ) ? item->len : tot_length-item->offset //uint_t blk_sz= (tot_length - item->offset) > item->len ) ? item->len : tot_length-item->offset
DBGINFO("copying data item no:%u of len: %lu to buffer: %p (offset:%lu)", sorted_indexes[i].item_index, (unsigned long int)item->len, buffer, (unsigned long int)item->offset) DBGINFO("copying data item no:%u of len: %lu to buffer: %p (offset:%lu)", sorted_indexes[i].item_index, (unsigned long int)item->len, buffer, (unsigned long int)item->offset);
memcpy((void*)(buffer+item->offset),item->data, item->len); memcpy((void*)(buffer+item->offset),item->data, item->len);
// free memory item // free memory item
@@ -2047,13 +2053,13 @@ uint32_t reassembly_fragment(struct ndpi_flow_struct *const flow, struct ndpi_tc
} }
if ( last_item==fragW->ct_frag ) { if ( last_item==fragW->ct_frag ) {
DBGTRACER("all processed: free all memory!") DBGTRACER("all processed: free all memory!");
free_fragment(fragW); free_fragment(fragW);
} else { } else {
// phase 4: re-organize the other segments, updating the list // phase 4: re-organize the other segments, updating the list
fragment_t **fragW_old_list = fragW->fragments_list; fragment_t **fragW_old_list = fragW->fragments_list;
fragW->fragments_list= ndpi_calloc((fragW->ct_frag-last_item), sizeof(struct fragment_t*)); fragW->fragments_list= ndpi_calloc((fragW->ct_frag-last_item), sizeof(struct fragment_t*));
DBGPOINTER("old segments list: %p, new segments list: %p.",fragW_old_list,fragW->fragments_list) DBGPOINTER("old segments list: %p, new segments list: %p.",fragW_old_list, fragW->fragments_list);
if (!fragW->fragments_list) { if (!fragW->fragments_list) {
fprintf(stderr, "[%8u] Not enough memory for new segments list \n", flow->packet_counter); fprintf(stderr, "[%8u] Not enough memory for new segments list \n", flow->packet_counter);
free_fragment(fragW); free_fragment(fragW);
@@ -2066,20 +2072,20 @@ uint32_t reassembly_fragment(struct ndpi_flow_struct *const flow, struct ndpi_tc
for (uint i=last_item; i<fragW->ct_frag; i++) { for (uint i=last_item; i<fragW->ct_frag; i++) {
fragW->fragments_list[i-last_item] = (fragment_t*)fragW_old_list[sorted_indexes[i].item_index]; fragW->fragments_list[i-last_item] = (fragment_t*)fragW_old_list[sorted_indexes[i].item_index];
fragW->fragments_list[i-last_item]->offset-= tot_length; fragW->fragments_list[i-last_item]->offset-= tot_length;
DBGTRACER("moving the item (%p), index %u - to position %u of new segments list; new offset: %u.", fragW->fragments_list[i-last_item], sorted_indexes[i].item_index, i-last_item, fragW->fragments_list[i-last_item]->offset ) DBGTRACER("moving the item (%p), index %u - to position %u of new segments list; new offset: %u.", fragW->fragments_list[i-last_item], sorted_indexes[i].item_index, i-last_item, fragW->fragments_list[i-last_item]->offset );
} }
// update the fragments countes // update the fragments countes
fragW->ct_frag-= last_item; fragW->ct_frag-= last_item;
fragW->initial_offset+= tot_length; fragW->initial_offset+= tot_length;
DBGINFO("updated counter: %d and i_offset: %u.",(unsigned)fragW->ct_frag, (unsigned)fragW->initial_offset) DBGINFO("updated counter: %d and i_offset: %u.",(unsigned)fragW->ct_frag, (unsigned)fragW->initial_offset);
DBGPOINTER("freeing old segments list: %p ",fragW_old_list) DBGPOINTER("freeing old segments list: %p ",fragW_old_list)
ndpi_free(fragW_old_list); ndpi_free(fragW_old_list);
} }
if ( sorted_indexes) { if ( sorted_indexes) {
DBGPOINTER("freeing sorter indexes: %p ",sorted_indexes) DBGPOINTER("freeing sorter indexes: %p ",sorted_indexes);
ndpi_free(sorted_indexes); ndpi_free(sorted_indexes);
} }
@@ -2089,48 +2095,50 @@ uint32_t reassembly_fragment(struct ndpi_flow_struct *const flow, struct ndpi_tc
if(ret_buffer != NULL) { if(ret_buffer != NULL) {
*ret_buffer = (u_int8_t *) buffer; *ret_buffer = (u_int8_t *) buffer;
flow->must_free[flow->packet.packet_direction]=1; flow->must_free[flow->packet.packet_direction]=1;
DBGINFO("retrieved the buffer of segments (len:%lu) %p",*len_buffer,*ret_buffer) DBGINFO("retrieved the buffer of segments (len:%lu) %p",*len_buffer,*ret_buffer);
} else { } else {
DBGPOINTER("freeing buffer=> %p",buffer) DBGPOINTER("freeing buffer=> %p",buffer);
ndpi_free(buffer); ndpi_free(buffer);
} }
DBGINFO("returning: %d",ret_value) DBGINFO("returning: %d",ret_value);
return ret_value; return ret_value;
} }
uint8_t check_for_sequence( struct ndpi_flow_struct *flow, struct ndpi_tcphdr const * tcph) /* ******************************************************************** */
{
uint8_t check_for_sequence( struct ndpi_flow_struct *flow, struct ndpi_tcphdr const * tcph) {
uint8_t *ret_buffer=NULL; uint8_t *ret_buffer=NULL;
size_t len_buffer=0; size_t len_buffer=0;
DBGINFO("## sorted flags: %d/%d ",flow->not_sorted[0],flow->not_sorted[1]) DBGINFO("## sorted flags: %d/%d ",flow->not_sorted[0],flow->not_sorted[1]);
if ( flow->next_tcp_seq_nr[flow->packet.packet_direction] ) { if ( flow->next_tcp_seq_nr[flow->packet.packet_direction] ) {
uint32_t *trigger; uint32_t *trigger, expected;
uint8_t *not_sorted; uint8_t *not_sorted;
// use pointers to allow the modification // use pointers to allow the modification
not_sorted = &flow->not_sorted[flow->packet.packet_direction]; not_sorted = &flow->not_sorted[flow->packet.packet_direction];
trigger = &flow->trigger[flow->packet.packet_direction]; trigger = &flow->trigger[flow->packet.packet_direction];
DBGTRACER("dir:%d, trg:%u, next:%u", flow->packet.packet_direction,*trigger, flow->next_tcp_seq_nr[flow->packet.packet_direction]) DBGTRACER("dir:%d, trg:%u, next:%u", flow->packet.packet_direction,*trigger, flow->next_tcp_seq_nr[flow->packet.packet_direction]);
uint32_t waited= (*not_sorted && *trigger) ? ndpi_min(*trigger,flow->next_tcp_seq_nr[flow->packet.packet_direction]) : flow->next_tcp_seq_nr[flow->packet.packet_direction]; expected = (*not_sorted && *trigger) ? ndpi_min(*trigger,flow->next_tcp_seq_nr[flow->packet.packet_direction]) : flow->next_tcp_seq_nr[flow->packet.packet_direction];
if ( waited<(0xffffffff & ntohl(tcph->seq))) {
if ( expected< (0xffffffff & ntohl(tcph->seq))) {
// segment not in order... almost 1 has been skipped! add this fragment to buffer // segment not in order... almost 1 has been skipped! add this fragment to buffer
DBGINFO("received a segment (seq:%u) over the waited (next:%u)", (0xffffffff & ntohl(tcph->seq)), waited) DBGINFO("received a segment (seq:%u) over the expected (next:%u)", (0xffffffff & ntohl(tcph->seq)), expected);
if ( add_segment_to_buffer(flow, tcph, waited) ) { if ( add_segment_to_buffer(flow, tcph, expected) ) {
DBGTRACER("segment (seq:%u) bufferized, waiting for (next:%u)", (0xffffffff & ntohl(tcph->seq)), waited) DBGTRACER("segment (seq:%u) bufferized, waiting for (next:%u)", (0xffffffff & ntohl(tcph->seq)), expected);
// set flag a save the waited sequence number // set flag a save the expected sequence number
*not_sorted=1; *not_sorted=1;
*trigger= *trigger ? ndpi_min(flow->next_tcp_seq_nr[flow->packet.packet_direction],*trigger):flow->next_tcp_seq_nr[flow->packet.packet_direction]; *trigger= *trigger ? ndpi_min(flow->next_tcp_seq_nr[flow->packet.packet_direction],*trigger):flow->next_tcp_seq_nr[flow->packet.packet_direction];
DBGINFO("set flag and trigger[%d]: %u",flow->packet.packet_direction,*trigger) DBGINFO("set flag and trigger[%d]: %u",flow->packet.packet_direction,*trigger);
} }
return 1; return 1;
} else if (waited>(0xffffffff & ntohl(tcph->seq))) { } else if (expected>(0xffffffff & ntohl(tcph->seq))) {
DBGINFO("received a segment (seq:%u) minus than the waited (next:%u): retransmission!!", (0xffffffff & ntohl(tcph->seq)), flow->next_tcp_seq_nr[flow->packet.packet_direction]) DBGINFO("received a segment (seq:%u) minus than the expected (next:%u): retransmission!!", (0xffffffff & ntohl(tcph->seq)), flow->next_tcp_seq_nr[flow->packet.packet_direction]);
flow->packet.tcp_retransmission = 1; flow->packet.tcp_retransmission = 1;
@@ -2143,13 +2151,14 @@ uint8_t check_for_sequence( struct ndpi_flow_struct *flow, struct ndpi_tcphdr co
flow->packet.num_retried_bytes = (u_int16_t)(flow->next_tcp_seq_nr[flow->packet.packet_direction] - ntohl(tcph->seq)); flow->packet.num_retried_bytes = (u_int16_t)(flow->next_tcp_seq_nr[flow->packet.packet_direction] - ntohl(tcph->seq));
flow->packet.actual_payload_len = flow->packet.payload_packet_len - flow->packet.num_retried_bytes; flow->packet.actual_payload_len = flow->packet.payload_packet_len - flow->packet.num_retried_bytes;
flow->next_tcp_seq_nr[flow->packet.packet_direction] = ntohl(tcph->seq) + flow->packet.payload_packet_len; flow->next_tcp_seq_nr[flow->packet.packet_direction] = ntohl(tcph->seq) + flow->packet.payload_packet_len;
DBGINFO("partial_bytes:%u",flow->packet.num_retried_bytes) DBGINFO("partial_bytes:%u",flow->packet.num_retried_bytes);
//TODO: manage this!! //TODO: manage this!!
} }
} else { } else {
DBGTRACER("seq (%u) and waited (%u) matched! sorted flag: %d", (0xffffffff & ntohl(tcph->seq)), flow->next_tcp_seq_nr[flow->packet.packet_direction], *not_sorted) DBGTRACER("seq (%u) and expected (%u) matched! sorted flag: %d", (0xffffffff & ntohl(tcph->seq)), flow->next_tcp_seq_nr[flow->packet.packet_direction], *not_sorted);
if ( *not_sorted ) { if ( *not_sorted ) {
if ( add_segment_to_buffer(flow, tcph, 0) ) { if ( add_segment_to_buffer(flow, tcph, 0) ) {
*trigger= reassembly_fragment(flow,tcph,&ret_buffer,&len_buffer); *trigger= reassembly_fragment(flow,tcph,&ret_buffer,&len_buffer);
@@ -2164,10 +2173,12 @@ uint8_t check_for_sequence( struct ndpi_flow_struct *flow, struct ndpi_tcphdr co
} }
} }
} }
return 0; return 0;
} }
#endif // FRAG_MAN #endif // FRAG_MAN
/* ******************************************************************** */
#define ROR64(x,r) (((x)>>(r))|((x)<<(64-(r)))) #define ROR64(x,r) (((x)>>(r))|((x)<<(64-(r))))