aboutsummaryrefslogtreecommitdiff
path: root/example/ndpiReader.c
diff options
context:
space:
mode:
authortheirix <theirix@gmail.com>2016-04-12 22:04:47 +0300
committertheirix <theirix@gmail.com>2016-04-12 22:04:47 +0300
commit624f61193cd1d9d203a5d05f9accfaed19ccd53a (patch)
treeec73052cd681ec4b63bfbe5421fba779ed25e1c4 /example/ndpiReader.c
parent5a37ee99764b7d262676b0ca052075c9c559c01d (diff)
Allocate exact-size buffer with address sanitizer
Allows address sanitizer to detect buffer overflow by using special packet buffer when compiled with address sanitizer support (autodetected clang and gcc).
Diffstat (limited to 'example/ndpiReader.c')
-rw-r--r--example/ndpiReader.c30
1 files changed, 29 insertions, 1 deletions
diff --git a/example/ndpiReader.c b/example/ndpiReader.c
index 52fac6a65..a7b4bf418 100644
--- a/example/ndpiReader.c
+++ b/example/ndpiReader.c
@@ -51,6 +51,17 @@
#include <json.h>
#endif
+/* Check for buffer allocation errors if address-sanitizer is enabled */
+#if defined(__has_feature)
+#if __has_feature(address_sanitizer)
+#define NDPI_STRICT_BUFFER_CHECK 1
+#endif
+#endif
+/* Check for GCC */
+#ifdef __SANITIZE_ADDRESS__
+#define NDPI_STRICT_BUFFER_CHECK 1
+#endif
+
#define MAX_NUM_READER_THREADS 16
#define IDLE_SCAN_PERIOD 10 /* msec (use detection_tick_resolution = 1000) */
#define MAX_IDLE_TIME 30000
@@ -1940,11 +1951,28 @@ static void pcap_packet_callback(u_char *args,
thread_id, (unsigned long)ndpi_thread_info[thread_id].stats.raw_packet_count);
}
+#ifdef NDPI_STRICT_BUFFER_CHECK
+static void pcap_packet_callback_checked(u_char *args,
+ const struct pcap_pkthdr *header,
+ const u_char *packet) {
+ uint8_t *packet_checked = malloc(header->caplen);
+ memcpy(packet_checked, packet, header->caplen);
+ pcap_packet_callback(args, header, packet_checked);
+ free(packet_checked);
+}
+#else
+static void pcap_packet_callback_checked(u_char *args,
+ const struct pcap_pkthdr *header,
+ const u_char *packet) {
+ pcap_packet_callback(args, header, packet);
+}
+#endif
+
/* ******************************************************************** */
static void runPcapLoop(u_int16_t thread_id) {
if((!shutdown_app) && (ndpi_thread_info[thread_id]._pcap_handle != NULL))
- pcap_loop(ndpi_thread_info[thread_id]._pcap_handle, -1, &pcap_packet_callback, (u_char*)&thread_id);
+ pcap_loop(ndpi_thread_info[thread_id]._pcap_handle, -1, &pcap_packet_callback_checked, (u_char*)&thread_id);
}
/* ******************************************************************** */