diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index f8b454e317e..010943f79a7 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -107,6 +107,7 @@ jobs: pcre: "--with-pcre2" maxminddb: "--with-maxminddb" msan: "--with-sanitizer" + cflags: "-fno-sanitize-recover=all" - compiler: "cc" os: macOS-latest pcre: "--with-pcre2" diff --git a/configure.ac b/configure.ac index 875fcd6ae1d..58a40f118f1 100644 --- a/configure.ac +++ b/configure.ac @@ -79,9 +79,9 @@ AS_IF([test "${with_sanitizer+set}" = set],[ NDPI_LDFLAGS="${NDPI_LDFLAGS} -fsanitize=address -fsanitize=undefined -fsanitize=leak" #Sanitizers should work on any compilers that we support (or that we test on CI, at least) #Exception: "-fsanitize=alignment" is not supported in gcc 4.9 - AX_CHECK_COMPILE_FLAG([-fno-sanitize=alignment], [ - NDPI_CFLAGS="${NDPI_CFLAGS} -fno-sanitize=alignment" - NDPI_LDFLAGS="${NDPI_LDFLAGS} -fno-sanitize=alignment" + AX_CHECK_COMPILE_FLAG([-fsanitize=alignment], [ + NDPI_CFLAGS="${NDPI_CFLAGS} -fsanitize=alignment" + NDPI_LDFLAGS="${NDPI_LDFLAGS} -fsanitize=alignment" ]) ]) @@ -302,7 +302,7 @@ echo "Setting API version to ${NDPI_API_VERSION}" AC_DEFINE_UNQUOTED(NDPI_GIT_RELEASE, "${GIT_RELEASE}", [GIT Release]) AC_DEFINE_UNQUOTED(NDPI_GIT_DATE, "${GIT_DATE}", [Last GIT change]) -NDPI_CFLAGS="-W -Wall -Wextra -Wno-address-of-packed-member ${NDPI_CFLAGS}" +NDPI_CFLAGS="-W -Wall -Wextra ${NDPI_CFLAGS}" dnl> MacOS brew.sh HOMEBREW_DIR=/opt/homebrew diff --git a/example/reader_util.h b/example/reader_util.h index 6def1a59735..2e83763dd40 100644 --- a/example/reader_util.h +++ b/example/reader_util.h @@ -285,7 +285,10 @@ typedef struct ndpi_flow_info { } bfcp; }; - ndpi_serializer ndpi_flow_serializer; + struct { + uint8_t _alignment[4]; + ndpi_serializer ndpi_flow_serializer; + }; char host_server_name[80]; /* Hostname/SNI */ char *server_hostname; diff --git a/src/include/ndpi_typedefs.h b/src/include/ndpi_typedefs.h index 22ad4bd241a..a4ed5e23661 100644 --- a/src/include/ndpi_typedefs.h +++ b/src/include/ndpi_typedefs.h @@ -684,8 +684,8 @@ struct ndpi_binary_bitmap_entry { typedef union { - u_int32_t ipv4; struct ndpi_in6_addr ipv6; + u_int32_t ipv4; } ndpi_ip_addr_t; @@ -2257,7 +2257,7 @@ typedef struct { typedef ndpi_ranking_epoch_entry ndpi_ranking_change; typedef struct { - u_int32_t epoch; + u_int64_t epoch; ndpi_ranking_epoch_entry *entries; } ndpi_ranking_epoch; diff --git a/src/lib/ndpi_analyze.c b/src/lib/ndpi_analyze.c index 94ddb2e80da..76c7465799c 100644 --- a/src/lib/ndpi_analyze.c +++ b/src/lib/ndpi_analyze.c @@ -2308,7 +2308,7 @@ void ndpi_print_ranking(ndpi_ranking *rank) { ndpi_ranking_epoch_entry *this_entries = (ndpi_ranking_epoch_entry*)&rank->epochs[i*epoch_len + sizeof(epoch->epoch)]; u_int32_t j; - fprintf(stdout, "\t[epoch %u @ %u]\n", i, epoch->epoch); + fprintf(stdout, "\t[epoch %u @ %llu]\n", i, (long long unsigned int)epoch->epoch); for(j=0; jheader.max_num_entries; j++) { fprintf(stdout, "\t\t[%2d] %u - %llu\n", j, @@ -2351,7 +2351,7 @@ u_int16_t ndpi_ranking_add_epoch(ndpi_ranking *rank, qsort(entries, num_epoch_entries, sizeof(ndpi_ranking_epoch_entry), _comp); - epoch_len = (sizeof(ndpi_ranking_epoch_entry) * rank->header.max_num_entries) + sizeof(u_int32_t /* epoch */); + epoch_len = (sizeof(ndpi_ranking_epoch_entry) * rank->header.max_num_entries) + sizeof(this_epoch->epoch); offset = epoch_len * rank->header.next_epoch_id; this_epoch = (ndpi_ranking_epoch*)&rank->epochs[offset]; diff --git a/src/lib/protocols/blizzard.c b/src/lib/protocols/blizzard.c index 550adccb68e..a2980f1cf0c 100644 --- a/src/lib/protocols/blizzard.c +++ b/src/lib/protocols/blizzard.c @@ -52,11 +52,14 @@ static void search_blizzard_tcp(struct ndpi_detection_module_struct* ndpi_struct } /* Pattern found on Hearthstone */ - if(packet->payload_packet_len >= 8 && - le32toh(*(uint32_t *)&packet->payload[4]) == (u_int32_t)(packet->payload_packet_len - 8)) { - NDPI_LOG_INFO(ndpi_struct, "Found Blizzard (Hearthstone)\n"); - ndpi_set_detected_protocol(ndpi_struct, flow, NDPI_PROTOCOL_BLIZZARD, NDPI_PROTOCOL_UNKNOWN, NDPI_CONFIDENCE_DPI); - return; + if(packet->payload_packet_len >= 8) { + uint32_t pdu_length; + memcpy(&pdu_length, &packet->payload[4], sizeof(pdu_length)); + if (le32toh(pdu_length) == (u_int32_t)(packet->payload_packet_len - 8)) { + NDPI_LOG_INFO(ndpi_struct, "Found Blizzard (Hearthstone)\n"); + ndpi_set_detected_protocol(ndpi_struct, flow, NDPI_PROTOCOL_BLIZZARD, NDPI_PROTOCOL_UNKNOWN, NDPI_CONFIDENCE_DPI); + return; + } } /* Pattern found on WoW */ diff --git a/src/lib/protocols/dns.c b/src/lib/protocols/dns.c index d139e9c11a5..f7ed45d6841 100644 --- a/src/lib/protocols/dns.c +++ b/src/lib/protocols/dns.c @@ -160,7 +160,12 @@ static u_int16_t checkDNSSubprotocol(u_int16_t sport, u_int16_t dport) { /* *********************************************** */ static u_int16_t get16(u_int *i, const u_int8_t *payload) { - u_int16_t v = *(u_int16_t*)&payload[*i]; + u_int16_t v; + + if (*i % 2 == 0) + v = *(u_int16_t*)&payload[*i]; + else + memcpy(&v, &payload[*i], sizeof(v)); (*i) += 2; @@ -383,7 +388,8 @@ static int process_answers(struct ndpi_detection_module_struct *ndpi_struct, } rsp_type = get16(&x, packet->payload); - rsp_ttl = ntohl(*((u_int32_t*)&packet->payload[x+2])); + memcpy(&rsp_ttl, &packet->payload[x+2], sizeof(rsp_ttl)); + rsp_ttl = ntohl(rsp_ttl); if(rsp_ttl == 0) ndpi_set_risk(ndpi_struct, flow, NDPI_MINOR_ISSUES, "DNS Record with zero TTL"); @@ -400,7 +406,9 @@ static int process_answers(struct ndpi_detection_module_struct *ndpi_struct, /* x points to the response "class" field */ if((x+12) <= packet->payload_packet_len) { - u_int32_t ttl = ntohl(*((u_int32_t*)&packet->payload[x+2])); + u_int32_t ttl; + memcpy(&ttl, &packet->payload[x+2], sizeof(ttl)); + ttl = ntohl(ttl); x += 6; data_len = get16(&x, packet->payload); diff --git a/src/lib/protocols/mudfish.c b/src/lib/protocols/mudfish.c index 74caeaf21b7..e3dc18aa656 100644 --- a/src/lib/protocols/mudfish.c +++ b/src/lib/protocols/mudfish.c @@ -77,12 +77,14 @@ static void ndpi_search_mudfish(struct ndpi_detection_module_struct *ndpi_struct } // Check discovery response - if (packet->payload_packet_len > 8 && - get_u_int32_t(packet->payload, 0) == htonl(0x554e2041) && - get_u_int32_t(packet->payload, 0) == htonl(0x465f494e)) - { - ndpi_int_mudfish_add_connection(ndpi_struct, flow); - return; + if (packet->payload_packet_len > 8) { + u_int32_t sig; + memcpy(&sig, &packet->payload[0], sizeof(sig)); + if (sig == htonl(0x554e2041) && sig == htonl(0x465f494e)) + { + ndpi_int_mudfish_add_connection(ndpi_struct, flow); + return; + } } NDPI_EXCLUDE_DISSECTOR(ndpi_struct, flow); diff --git a/src/lib/protocols/stun.c b/src/lib/protocols/stun.c index 67200b36c8e..8ec2a08bea6 100644 --- a/src/lib/protocols/stun.c +++ b/src/lib/protocols/stun.c @@ -398,10 +398,14 @@ int is_stun(struct ndpi_detection_module_struct *ndpi_struct, msg_type = ntohs(*((u_int16_t *)&payload[0])); msg_len = ntohs(*((u_int16_t *)&payload[2])); - magic_cookie = ntohl(*((u_int32_t *)&payload[4])); - transaction_id[0] = ntohl(*((u_int32_t *)&payload[8])); - transaction_id[1] = ntohl(*((u_int32_t *)&payload[12])); - transaction_id[2] = ntohl(*((u_int32_t *)&payload[16])); + memcpy(&magic_cookie, &payload[4], sizeof(magic_cookie)); + magic_cookie = ntohl(magic_cookie); + memcpy(&transaction_id[0], &payload[8], sizeof(transaction_id[0])); + transaction_id[0] = ntohl(transaction_id[0]); + memcpy(&transaction_id[1], &payload[12], sizeof(transaction_id[1])); + transaction_id[1] = ntohl(transaction_id[1]); + memcpy(&transaction_id[2], &payload[16], sizeof(transaction_id[2])); + transaction_id[2] = ntohl(transaction_id[2]); /* No magic_cookie on classic-stun */ /* Let's hope that we don't have anymore classic-stun over TCP */