@@ -116,14 +116,14 @@ u_int16_t max_pattern_len = 8;
116116
117117/* *********************************************************** */
118118
119- void ndpi_analyze_payload (struct ndpi_flow_info * flow ,
120- u_int8_t src_to_dst_direction ,
121- u_int8_t * payload ,
122- u_int16_t payload_len ,
123- u_int32_t packet_id ) {
124- struct payload_stats * ret ;
125- struct flow_id_stats * f ;
126- struct packet_id_stats * p ;
119+ int ndpi_analyze_payload (struct ndpi_flow_info * flow ,
120+ u_int8_t src_to_dst_direction ,
121+ u_int8_t * payload ,
122+ u_int16_t payload_len ,
123+ u_int32_t packet_id ) {
124+ struct payload_stats * ret , * ret_found ;
125+ struct flow_id_stats * f , * f_found ;
126+ struct packet_id_stats * p , * p_found ;
127127
128128#ifdef DEBUG_PAYLOAD
129129 u_int16_t i ;
@@ -135,11 +135,11 @@ void ndpi_analyze_payload(struct ndpi_flow_info *flow,
135135 HASH_FIND (hh , pstats , payload , payload_len , ret );
136136 if (ret == NULL ) {
137137 if ((ret = (struct payload_stats * )ndpi_calloc (1 , sizeof (struct payload_stats ))) == NULL )
138- return ; /* OOM */
138+ return -1 ; /* OOM */
139139
140140 if ((ret -> pattern = (u_int8_t * )ndpi_malloc (payload_len )) == NULL ) {
141141 ndpi_free (ret );
142- return ;
142+ return -1 ;
143143 }
144144
145145 memcpy (ret -> pattern , payload , payload_len );
@@ -148,6 +148,13 @@ void ndpi_analyze_payload(struct ndpi_flow_info *flow,
148148
149149 HASH_ADD (hh , pstats , pattern [0 ], payload_len , ret );
150150
151+ HASH_FIND (hh , pstats , payload , payload_len , ret_found );
152+ if (ret_found == NULL ) { /* The insertion failed (because of a memory allocation error) */
153+ ndpi_free (ret -> pattern );
154+ ndpi_free (ret );
155+ return -1 ;
156+ }
157+
151158#ifdef DEBUG_PAYLOAD
152159 printf ("Added element [total: %u]\n" , HASH_COUNT (pstats ));
153160#endif
@@ -159,20 +166,32 @@ void ndpi_analyze_payload(struct ndpi_flow_info *flow,
159166 HASH_FIND_INT (ret -> flows , & flow -> flow_id , f );
160167 if (f == NULL ) {
161168 if ((f = (struct flow_id_stats * )ndpi_calloc (1 , sizeof (struct flow_id_stats ))) == NULL )
162- return ; /* OOM */
169+ return -1 ; /* OOM */
163170
164171 f -> flow_id = flow -> flow_id ;
165172 HASH_ADD_INT (ret -> flows , flow_id , f );
173+
174+ HASH_FIND_INT (ret -> flows , & flow -> flow_id , f_found );
175+ if (f_found == NULL ) { /* The insertion failed (because of a memory allocation error) */
176+ ndpi_free (f );
177+ return -1 ;
178+ }
166179 }
167180
168181 HASH_FIND_INT (ret -> packets , & packet_id , p );
169182 if (p == NULL ) {
170183 if ((p = (struct packet_id_stats * )ndpi_calloc (1 , sizeof (struct packet_id_stats ))) == NULL )
171- return ; /* OOM */
184+ return -1 ; /* OOM */
172185 p -> packet_id = packet_id ;
173186
174187 HASH_ADD_INT (ret -> packets , packet_id , p );
188+
189+ HASH_FIND_INT (ret -> packets , & packet_id , p_found );
190+ if (p_found == NULL ) { /* The insertion failed (because of a memory allocation error) */
191+ ndpi_free (p );
192+ }
175193 }
194+ return 0 ;
176195}
177196
178197/* *********************************************************** */
@@ -199,7 +218,12 @@ void ndpi_payload_analyzer(struct ndpi_flow_info *flow,
199218 for (i = 0 ; i < scan_len ; i ++ ) {
200219 for (j = min_pattern_len ; j <= max_pattern_len ; j ++ ) {
201220 if ((i + j ) < payload_len ) {
202- ndpi_analyze_payload (flow , src_to_dst_direction , & payload [i ], j , packet_id );
221+ if (ndpi_analyze_payload (flow , src_to_dst_direction , & payload [i ], j , packet_id ) == -1 ) {
222+ #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
223+ /* Avoid too much logging while fuzzing */
224+ LOG (NDPI_LOG_ERROR , "Error ndpi_analyze_payload (allocation failure)\n" );
225+ #endif
226+ }
203227 }
204228 }
205229 }
@@ -960,6 +984,12 @@ static struct ndpi_flow_info *get_ndpi_flow_info(struct ndpi_workflow * workflow
960984 if (enable_flow_stats ) {
961985 newflow -> entropy = ndpi_calloc (1 , sizeof (struct ndpi_entropy ));
962986 newflow -> last_entropy = ndpi_calloc (1 , sizeof (struct ndpi_entropy ));
987+ if (!newflow -> entropy || !newflow -> last_entropy ) {
988+ ndpi_tdelete (newflow , & workflow -> ndpi_flows_root [idx ], ndpi_workflow_node_cmp );
989+ ndpi_flow_info_free_data (newflow );
990+ ndpi_free (newflow );
991+ return (NULL );
992+ }
963993 newflow -> entropy -> src2dst_pkt_len [newflow -> entropy -> src2dst_pkt_count ] = l4_data_len ;
964994 newflow -> entropy -> src2dst_pkt_time [newflow -> entropy -> src2dst_pkt_count ] = when ;
965995 if (newflow -> entropy -> src2dst_pkt_count == 0 ) {
0 commit comments