Skip to content

Commit

Permalink
Fix some errors found by fuzzers
Browse files Browse the repository at this point in the history
Fix compilation on Windows.
"dirent.h" has been taken from https://github.com/tronkko/dirent/
  • Loading branch information
IvanNardi committed Sep 9, 2023
1 parent 6397745 commit 57fb3e4
Show file tree
Hide file tree
Showing 8 changed files with 1,313 additions and 3 deletions.
15 changes: 15 additions & 0 deletions src/lib/ndpi_bitmap64.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ bool ndpi_bitmap64_compress(ndpi_bitmap64 *_b) {
ndpi_bitmap64_t *b = (ndpi_bitmap64_t*)_b;
u_int32_t i;

if(!b)
return(false);

if(b->num_used_entries > 0) {
if(b->num_used_entries > 1)
qsort(b->entries, b->num_used_entries,
Expand Down Expand Up @@ -122,6 +125,9 @@ bool ndpi_bitmap64_compress(ndpi_bitmap64 *_b) {
bool ndpi_bitmap64_set(ndpi_bitmap64 *_b, u_int64_t value) {
ndpi_bitmap64_t *b = (ndpi_bitmap64_t*)_b;

if(!b)
return(false);

if(b->is_compressed) {
/*
We need to discard the filter and start over as this
Expand Down Expand Up @@ -155,6 +161,9 @@ bool ndpi_bitmap64_set(ndpi_bitmap64 *_b, u_int64_t value) {
bool ndpi_bitmap64_isset(ndpi_bitmap64 *_b, u_int64_t value) {
ndpi_bitmap64_t *b = (ndpi_bitmap64_t*)_b;

if(!b)
return(false);

if(!b->is_compressed) ndpi_bitmap64_compress(b);

return(binary_fuse16_contain(value, &b->bitmap));
Expand All @@ -165,6 +174,9 @@ bool ndpi_bitmap64_isset(ndpi_bitmap64 *_b, u_int64_t value) {
void ndpi_bitmap64_free(ndpi_bitmap64 *_b) {
ndpi_bitmap64_t *b = (ndpi_bitmap64_t*)_b;

if(!b)
return;

if(b->entries) ndpi_free(b->entries);

if(b->is_compressed)
Expand All @@ -178,5 +190,8 @@ void ndpi_bitmap64_free(ndpi_bitmap64 *_b) {
u_int32_t ndpi_bitmap64_size(ndpi_bitmap64 *_b) {
ndpi_bitmap64_t *b = (ndpi_bitmap64_t*)_b;

if(!b)
return(0);

return(sizeof(ndpi_bitmap64) + binary_fuse16_size_in_bytes(&b->bitmap));
}
7 changes: 7 additions & 0 deletions src/lib/ndpi_domain_classify.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ ndpi_domain_classify* ndpi_domain_classify_alloc() {
void ndpi_domain_classify_free(ndpi_domain_classify *s) {
u_int32_t i;

if(!s)
return;

for(i=0; i<MAX_NUM_NDPI_DOMAIN_CLASSIFICATIONS; i++) {
if(s->classes[i].domains != NULL) {
ndpi_bitmap64_free(s->classes[i].domains);
Expand Down Expand Up @@ -86,6 +89,8 @@ bool ndpi_domain_classify_add(ndpi_domain_classify *s,
} else if(s->classes[i].class_id == 0) {
s->classes[i].class_id = class_id;
s->classes[i].domains = ndpi_bitmap64_alloc();
if(!s->classes[i].domains)
s->classes[i].class_id = 0;
break;
}
}
Expand Down Expand Up @@ -113,6 +118,8 @@ u_int32_t ndpi_domain_classify_add_domains(ndpi_domain_classify *s,
} else if(s->classes[i].class_id == 0) {
s->classes[i].class_id = class_id;
s->classes[i].domains = ndpi_bitmap64_alloc();
if(!s->classes[i].domains)
s->classes[i].class_id = 0;
break;
}
}
Expand Down
14 changes: 13 additions & 1 deletion src/lib/ndpi_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2669,7 +2669,8 @@ void ndpi_debug_printf(unsigned int proto, struct ndpi_detection_module_struct *

void set_ndpi_debug_function(struct ndpi_detection_module_struct *ndpi_str, ndpi_debug_function_ptr ndpi_debug_printf) {
#ifdef NDPI_ENABLE_DEBUG_MESSAGES
ndpi_str->ndpi_debug_printf = ndpi_debug_printf;
if(ndpi_str)
ndpi_str->ndpi_debug_printf = ndpi_debug_printf;
#endif
}

Expand Down Expand Up @@ -3002,7 +3003,15 @@ struct ndpi_detection_module_struct *ndpi_init_detection_module(ndpi_init_prefs
ac_automata_name(ndpi_str->custom_categories.hostnames_shadow.ac_automa, "ccat_sh", 0);
#else
ndpi_str->custom_categories.sc_hostnames = ndpi_domain_classify_alloc();
if(!ndpi_str->custom_categories.sc_hostnames) {
ndpi_exit_detection_module(ndpi_str);
return(NULL);
}
ndpi_str->custom_categories.sc_hostnames_shadow = ndpi_domain_classify_alloc();
if(!ndpi_str->custom_categories.sc_hostnames_shadow) {
ndpi_exit_detection_module(ndpi_str);
return(NULL);
}
#endif

ndpi_str->custom_categories.ipAddresses = ndpi_patricia_new(32 /* IPv4 */);
Expand Down Expand Up @@ -6887,6 +6896,9 @@ int ndpi_load_hostname_category(struct ndpi_detection_module_struct *ndpi_str,
(AC_AUTOMATA_t *)ndpi_str->custom_categories.hostnames_shadow.ac_automa,
name_to_add,category,category, 0, 0, 1); /* at_end */
#else
if(ndpi_str->custom_categories.sc_hostnames_shadow == NULL)
return(-1);

return(ndpi_domain_classify_add(ndpi_str->custom_categories.sc_hostnames_shadow,
(u_int16_t)category, (char*)name_to_add) ? 0 : -1);
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/lib/third_party/include/binaryfusefilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ static inline bool binary_fuse16_allocate(uint32_t size,
filter->ArrayLength =
(filter->SegmentCount + arity - 1) * filter->SegmentLength;
filter->SegmentCountLength = filter->SegmentCount * filter->SegmentLength;
filter->Fingerprints = (uint16_t*)ndpi_malloc(filter->ArrayLength * sizeof(uint16_t));
filter->Fingerprints = (uint16_t*)ndpi_calloc(filter->ArrayLength, sizeof(uint16_t));
return filter->Fingerprints != NULL;
}

Expand Down
30 changes: 30 additions & 0 deletions tests/cfgs/default/result/dns2tcp_tunnel.pcap.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Guessed flow protos: 0

DPI Packets (TCP): 6 (6.00 pkts/flow)
Confidence DPI : 1 (flows)
Num dissector calls: 1 (1.00 diss/flow)
LRU cache ookla: 0/0/0 (insert/search/found)
LRU cache bittorrent: 0/0/0 (insert/search/found)
LRU cache zoom: 0/0/0 (insert/search/found)
LRU cache stun: 0/0/0 (insert/search/found)
LRU cache tls_cert: 0/2/0 (insert/search/found)
LRU cache mining: 0/0/0 (insert/search/found)
LRU cache msteams: 0/0/0 (insert/search/found)
LRU cache stun_zoom: 0/0/0 (insert/search/found)
Automa host: 0/0 (search/found)
Automa domain: 0/0 (search/found)
Automa tls cert: 0/0 (search/found)
Automa risk mask: 0/0 (search/found)
Automa common alpns: 2/2 (search/found)
Patricia risk mask: 2/0 (search/found)
Patricia risk: 0/0 (search/found)
Patricia protocols: 2/0 (search/found)

TLS 50 8960 1

JA3 Host Stats:
IP Address # JA3C
1 192.168.20.211 1


1 TCP 192.168.20.211:44404 <-> 1.1.1.1:443 [proto: 91/TLS][IP: 0/Unknown][Encrypted][Confidence: DPI][DPI packets: 6][cat: Web/5][22 pkts/2595 bytes <-> 28 pkts/6365 bytes][Goodput ratio: 52/74][8.11 sec][(Advertised) ALPNs: h2;http/1.1][TLS Supported Versions: TLSv1.3;TLSv1.2][bytes ratio: -0.421 (Download)][IAT c2s/s2c min/avg/max/stddev: 0/0 289/346 3093/3078 792/900][Pkt Len c2s/s2c min/avg/max/stddev: 56/62 118/227 317/1644 68/386][Risk: ** Missing SNI TLS Extn **** ALPN/SNI Mismatch **][Risk Score: 100][TLSv1.3][JA3C: 547df21d727c7b3a5dcb59aa0fd97c2c][JA3S: eb1d94daa7e0344597e756a1fb6e7054][Firefox][Cipher: TLS_AES_128_GCM_SHA256][Plen Bins: 26,0,11,26,0,3,14,0,7,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,0,3]
5 changes: 4 additions & 1 deletion windows/nDPI.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,15 @@
<ClCompile Include="..\src\lib\ndpi_bitmap.c" />
<ClCompile Include="..\src\lib\ndpi_classify.c" />
<ClCompile Include="..\src\lib\ndpi_community_id.c" />
<ClCompile Include="..\src\lib\ndpi_domain_classify.c" />
<ClCompile Include="..\src\lib\ndpi_geoip.c" />
<ClCompile Include="..\src\lib\ndpi_main.c" />
<ClCompile Include="..\src\lib\ndpi_filter.c" />
<ClCompile Include="..\src\lib\ndpi_memory.c" />
<ClCompile Include="..\src\lib\ndpi_serializer.c" />
<ClCompile Include="..\src\lib\ndpi_utils.c" />
<ClCompile Include="..\src\lib\ndpi_binary_bitmap.c" />
<ClCompile Include="..\src\lib\ndpi_hash.c" />
<ClCompile Include="..\src\lib\ndpi_domain_classify.c" />
<ClCompile Include="..\src\lib\protocols\activision.c" />
<ClCompile Include="..\src\lib\protocols\afp.c" />
<ClCompile Include="..\src\lib\protocols\ajp.c" />
Expand Down Expand Up @@ -377,6 +379,7 @@
</ClInclude>
<ClInclude Include="src\ndpi_config.h" />
<ClInclude Include="src\ndpi_define.h" />
<ClInclude Include="src\dirent.h" />
</ItemGroup>
<ItemGroup>
<None Include="..\.github\workflows\build-msbuild.yml" />
Expand Down
4 changes: 4 additions & 0 deletions windows/nDPI.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@
<ClCompile Include="..\src\lib\ndpi_memory.c" />
<ClCompile Include="..\src\lib\ndpi_serializer.c" />
<ClCompile Include="..\src\lib\ndpi_utils.c" />
<ClCompile Include="..\src\lib\ndpi_binary_bitmap.c" />
<ClCompile Include="..\src\lib\ndpi_hash.c" />
<ClCompile Include="..\src\lib\ndpi_domain_classify.c" />
<ClCompile Include="..\src\lib\protocols\ajp.c" />
<ClCompile Include="..\src\lib\protocols\amazon_video.c" />
<ClCompile Include="..\src\lib\protocols\among_us.c" />
Expand Down Expand Up @@ -245,6 +248,7 @@
<ClInclude Include="..\src\include\ndpi_win32.h" />
<ClInclude Include="arpa\inet.h" />
<ClInclude Include="src\getopt.h" />
<ClInclude Include="src\dirent.h" />
<ClInclude Include="..\src\include\ndpi_encryption.h" />
<ClInclude Include="..\src\include\ndpi_main.h" />
<ClInclude Include="..\src\include\ndpi_utils.h" />
Expand Down
Loading

0 comments on commit 57fb3e4

Please sign in to comment.