From 02e7e3c23b29002267a89ae74e51c2285bb27da1 Mon Sep 17 00:00:00 2001 From: Ivan Nardi <12729895+IvanNardi@users.noreply.github.com> Date: Thu, 1 Dec 2022 12:21:04 +0100 Subject: [PATCH] Add a new fuzzer for QUIC (#1800) QUIC packets are encrypted/obfuscated; that means that we need to decrypt them before parsing the real (TLS) message. Fuzzing is not effective here, since a random buffer is hardly a valid encrypted QUIC packet. Add a new fuzzer, testing *decrypted* QUIC packets. Add a basic corpus. Fix a few bugs already found by this fuzzer. --- .github/workflows/cifuzz.yml | 2 +- .gitignore | 2 + fuzz/Makefile.am | 38 ++++- .../corpus/fuzz_quic_get_crypto_data/q046.bin | Bin 0 -> 1324 bytes .../corpus/fuzz_quic_get_crypto_data/q050.bin | Bin 0 -> 1319 bytes .../v1_chaos_protection.bin | Bin 0 -> 1219 bytes .../v1_chaos_protection_b.bin | Bin 0 -> 1219 bytes .../fuzz_quic_get_crypto_data/v1_doq.bin | Bin 0 -> 1210 bytes .../v1_only_first_fragment.bin | Bin 0 -> 1190 bytes .../v1_only_second_fragment.bin | Bin 0 -> 1190 bytes fuzz/corpus/fuzz_quic_get_crypto_data/v2.bin | Bin 0 -> 1210 bytes fuzz/fuzz_quic_get_crypto_data.c | 136 ++++++++++++++++++ src/lib/protocols/quic.c | 35 ++--- 13 files changed, 194 insertions(+), 19 deletions(-) create mode 100644 fuzz/corpus/fuzz_quic_get_crypto_data/q046.bin create mode 100644 fuzz/corpus/fuzz_quic_get_crypto_data/q050.bin create mode 100644 fuzz/corpus/fuzz_quic_get_crypto_data/v1_chaos_protection.bin create mode 100644 fuzz/corpus/fuzz_quic_get_crypto_data/v1_chaos_protection_b.bin create mode 100644 fuzz/corpus/fuzz_quic_get_crypto_data/v1_doq.bin create mode 100644 fuzz/corpus/fuzz_quic_get_crypto_data/v1_only_first_fragment.bin create mode 100644 fuzz/corpus/fuzz_quic_get_crypto_data/v1_only_second_fragment.bin create mode 100644 fuzz/corpus/fuzz_quic_get_crypto_data/v2.bin create mode 100644 fuzz/fuzz_quic_get_crypto_data.c diff --git a/.github/workflows/cifuzz.yml b/.github/workflows/cifuzz.yml index c74c81096..a4b7ddbe9 100644 --- a/.github/workflows/cifuzz.yml +++ b/.github/workflows/cifuzz.yml @@ -18,7 +18,7 @@ jobs: uses: google/oss-fuzz/infra/cifuzz/actions/run_fuzzers@master with: oss-fuzz-project-name: 'ndpi' - fuzz-seconds: 600 + fuzz-seconds: 900 dry-run: false sanitizer: ${{ matrix.sanitizer }} - name: Check Crash (fails when a crash is detected) diff --git a/.gitignore b/.gitignore index 21f2a04d2..4565885d2 100644 --- a/.gitignore +++ b/.gitignore @@ -54,6 +54,8 @@ /fuzz/fuzz_ndpi_reader_with_main /fuzz/fuzz_process_packet /fuzz/fuzz_process_packet_with_main +/fuzz/fuzz_quic_get_crypto_data +/fuzz/fuzz_quic_get_crypto_data_with_main /influxdb/Makefile /install-sh /libndpi.pc diff --git a/fuzz/Makefile.am b/fuzz/Makefile.am index ed680892b..2e80ec205 100644 --- a/fuzz/Makefile.am +++ b/fuzz/Makefile.am @@ -1,4 +1,4 @@ -bin_PROGRAMS = fuzz_process_packet fuzz_process_packet_with_main fuzz_ndpi_reader fuzz_ndpi_reader_with_main +bin_PROGRAMS = fuzz_process_packet fuzz_process_packet_with_main fuzz_ndpi_reader fuzz_ndpi_reader_with_main fuzz_quic_get_crypto_data fuzz_quic_get_crypto_data_with_main fuzz_process_packet_SOURCES = fuzz_process_packet.c fuzz_process_packet_CFLAGS = @@ -44,9 +44,45 @@ fuzz_ndpi_reader_with_main_LINK=$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS $(LIBTOOLFLAGS) --mode=link $(CXX) @NDPI_CFLAGS@ $(AM_CXXFLAGS) $(CXXFLAGS) \ $(fuzz_ndpi_reader_with_main_LDFLAGS) @NDPI_LDFLAGS@ $(LDFLAGS) -o $@ +fuzz_quic_get_crypto_data_SOURCES = fuzz_quic_get_crypto_data.c +fuzz_quic_get_crypto_data_CFLAGS = -I../example/ +fuzz_quic_get_crypto_data_LDADD = ../example/libndpiReader.a ../src/lib/libndpi.a +fuzz_quic_get_crypto_data_LDFLAGS = $(PCAP_LIB) $(ADDITIONAL_LIBS) $(LIBS) +if HAS_FUZZLDFLAGS +fuzz_quic_get_crypto_data_CFLAGS += $(LIB_FUZZING_ENGINE) +fuzz_quic_get_crypto_data_LDFLAGS += $(LIB_FUZZING_ENGINE) +endif +# force usage of CXX for linker +fuzz_quic_get_crypto_data_LINK=$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \ + $(LIBTOOLFLAGS) --mode=link $(CXX) @NDPI_CFLAGS@ $(AM_CXXFLAGS) $(CXXFLAGS) \ + $(fuzz_quic_get_crypto_data_LDFLAGS) @NDPI_LDFLAGS@ $(LDFLAGS) -o $@ + +fuzz_quic_get_crypto_data_with_main_SOURCES = fuzz_quic_get_crypto_data.c +fuzz_quic_get_crypto_data_with_main_CFLAGS = -I../example/ -DBUILD_MAIN +fuzz_quic_get_crypto_data_with_main_LDADD = ../src/lib/libndpi.a +fuzz_quic_get_crypto_data_with_main_LDFLAGS = ../example/libndpiReader.a $(PCAP_LIB) $(ADDITIONAL_LIBS) $(LIBS) +# force usage of CXX for linker +fuzz_quic_get_crypto_data_with_main_LINK=$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \ + $(LIBTOOLFLAGS) --mode=link $(CXX) @NDPI_CFLAGS@ $(AM_CXXFLAGS) $(CXXFLAGS) \ + $(fuzz_quic_get_crypto_data_with_main_LDFLAGS) @NDPI_LDFLAGS@ $(LDFLAGS) -o $@ + + # required for Google oss-fuzz # see https://github.com/google/oss-fuzz/tree/master/projects/ndpi testpcaps := $(wildcard ../tests/pcap/*.pcap*) fuzz_ndpi_reader_seed_corpus.zip: $(testpcaps) zip -r fuzz_ndpi_reader_seed_corpus.zip $(testpcaps) + +files_corpus_fuzz_quic_get_crypto_data := $(wildcard corpus/fuzz_quic_get_crypto_data/*) + +fuzz_quic_get_crypto_data_seed_corpus.zip: $(files_corpus_fuzz_quic_get_crypto_data) + zip -r fuzz_quic_get_crypto_data_seed_corpus.zip $(files_corpus_fuzz_quic_get_crypto_data) + +corpus: fuzz_quic_get_crypto_data_seed_corpus.zip + +distdir: + find . -type d | xargs -I'{}' mkdir -p '$(distdir)/{}' + find . -type f -name '*.c' \ + -o -name '*.am' \ + -o -name '*.bin' | xargs -I'{}' cp '{}' '$(distdir)/{}' diff --git a/fuzz/corpus/fuzz_quic_get_crypto_data/q046.bin b/fuzz/corpus/fuzz_quic_get_crypto_data/q046.bin new file mode 100644 index 0000000000000000000000000000000000000000..679ef82458bd45302223ab49ed6588d5c9a13551 GIT binary patch literal 1324 zcmWGEFfm)g$il$F;Oyb!FU7#X5a8&-u#J&{A=uB8;V6g>@n-l7=KC`kGBGfOxdt(q z0%>RGU5JT^(IAfpn;&r^^%|9qjDsvIIzn0O=J#I>5!(WerH&*TZK6 zkoI&A_SpiYf%*fkfaF7+g1-Q1Ur(3dA3!?T*(dl9koI4&eaO&cRUH zKOlq$#CHw~kpa@4K_MXuAikS(xC)RC2GSZpT6Yu-!4N3ONvzaM&(BZKN!3fv&-IB5 z$e7}lX`JCTsVrZ$;9u=x_b<~s3`DX;!nqZmX4IB1Y@E0+v%7t&{Cw8BRnC9b2)&xV z_)B3#CP%_`buGPj%M*UyJa{t6VWP>5AF7I<)o)lf3zjrSC_dhmvQR+U`k`e`j^s_1 zTuzg|=GZ`BmSB9*m@sLQ)Qh^e4Q-1=ntkfNa&C90 zA-`RaWzOrs>Dgk3f!U5B!qmVLn9UgEfY|Q&Lqo=Xuk_w)OnM1Ue%|~_QqvC-9EnQKHA`##AJ zG%z(_e9@RNX_C~7y0;B&i$t3y#w#6vYXAkZ;t#*G=B-TBi|F;{y7^=6vGY+?j;_J( zKxaZ-t(TaWQk0*Wq6hVjf^$)RUTTTHg{hu_o{6D_v7VvDTJKLi`MDSN3FY1Jjs7JR z49tQI5vB%~azJ|eI)?coTbw1ep8a{^snxW0`icWT%BRLHea`tz?MqorQ+W2H&Tli1 zA7bH|G(|*|H>c2M{lm_?fmEX3dxhksBpPLxBDvz~-=(k3j-x zuMC3}1A};Fekq&7I(7~QHU?P+ar!$4w~cIkN-FnG)`iHpx%l)g{pZHyzwFYQ<;>&@TiS8QC1NS?BPa-@TR1 zA%>rUnJL4V!I(jfL4`qxR!1ifod*v$oF8l}2(NBO8MWBLky8 zGxGx58!kHM4qCc%Uz~Pf!R|*^s!hMzs=s(dc%S^>E&mRviA|VMm`Ru!s2pejn?sw! z+8fIQDnc178It)KIG7w#7+D$^N)4``d8^RjwoZ%B;voBGu}SIgo%dhZx9R{3OTz~S zh6V=rAQqr@#)kjJw|PK}RaUdQKr}l`LjurPFv$ugnVDPo<(Ft7prF3upqAOI|gzyYGmz{JQ1 E08YnLjQ{`u literal 0 HcmV?d00001 diff --git a/fuzz/corpus/fuzz_quic_get_crypto_data/v1_doq.bin b/fuzz/corpus/fuzz_quic_get_crypto_data/v1_doq.bin new file mode 100644 index 0000000000000000000000000000000000000000..e1c7d9cbb9f818895b4a03c56b35a04e55b72e6f GIT binary patch literal 1210 zcmZQ%U|?WlaCpebz;KtD+4<~aml~bD%#3#)B`Nf`yFK~#y?XB}vDtx*aWjt|5H|v< z6=oD>5@rS}U&mm~;LhO6AkN^(a!^W?<3zl=@!2b>vqTU5@jiUy=C*Q{&`OK@@hRrydr!J^w&}Cp^1gaPXqaiRF0>dT*08@2BaR2}S literal 0 HcmV?d00001 diff --git a/fuzz/corpus/fuzz_quic_get_crypto_data/v1_only_first_fragment.bin b/fuzz/corpus/fuzz_quic_get_crypto_data/v1_only_first_fragment.bin new file mode 100644 index 0000000000000000000000000000000000000000..d865a5578ce92269b926abe9f859c80c0b8d9e99 GIT binary patch literal 1190 zcmZQ%U|?WlaGA%*z^2B`Y&&5B!*Q{f2CP0O);Z7ruD_dmgW8^rSCXA9JHuBV|NIT8 zR+v$kNtl_Df%Q9sF@rmUD}y+LBg>cEyDCc@PAN;4Z+4m2!L{KstM3fIzx((6vs2%2 zQ}2_W)*7{z)!iD47uK(8?X`HE#lKM8Z1S3_3&zR+w^jLENn;RTC}7BA%`nzAHet;u z1d~9vF_;Y`fovl%8%P4#hF~_3G}bk;1hGLRP}~444kUr%7GQB831pjt+2%|c##}(f znRz9tMfn8`+6+LREX*v7OpHJ_8v_f_XR-{ItemVY4GIhl zEG!KI3=AAhPK=B!4Gg6Q%uKEeIDtGimIg+KWOks~|Nnf99Ndgt{2b9+HqAUe_tcT- YD4_5wb{}(>4S}OzGz3ONfXom80NsvghX4Qo literal 0 HcmV?d00001 diff --git a/fuzz/corpus/fuzz_quic_get_crypto_data/v1_only_second_fragment.bin b/fuzz/corpus/fuzz_quic_get_crypto_data/v1_only_second_fragment.bin new file mode 100644 index 0000000000000000000000000000000000000000..59aa1fd6e10210c1d391abb6fd6c9877fcbe2b64 GIT binary patch literal 1190 zcmZQ%U|?W#ndjIOI11<-0tbLW|DWle6qECd28O~2M&_(d3=FyqOpJ^`iBT{b0;3@S G3jqKadwod& literal 0 HcmV?d00001 diff --git a/fuzz/corpus/fuzz_quic_get_crypto_data/v2.bin b/fuzz/corpus/fuzz_quic_get_crypto_data/v2.bin new file mode 100644 index 0000000000000000000000000000000000000000..86e5d2bc4bb1e6906c43d96492cabde73f5ab8ec GIT binary patch literal 1210 zcmX>SFsp!#!Lfvqfw6#@d67h#F-OO0nIC&^2-I+DoKr~A&+k*T`oRAs`q$&Bvwkoz zun98?GYK;Tl{+#RGq^LjGKe!cvNS%|I8wCxRkj_kkWY$GrmNCMf0U^b96)-|#Ou|XtI+yE>NB!S`Yyi#fGZshxt tDeu|=1_p-zO!uUioL@9B6h<&IXKi9&&}Cp^WCTi#g3%Bd4FR%3001X}W)A=W literal 0 HcmV?d00001 diff --git a/fuzz/fuzz_quic_get_crypto_data.c b/fuzz/fuzz_quic_get_crypto_data.c new file mode 100644 index 000000000..3e16446cd --- /dev/null +++ b/fuzz/fuzz_quic_get_crypto_data.c @@ -0,0 +1,136 @@ +#include "ndpi_api.h" + +#include +#include + +struct ndpi_detection_module_struct *ndpi_info_mod = NULL; +struct ndpi_flow_struct *flow = NULL; + +extern const uint8_t *get_crypto_data(struct ndpi_detection_module_struct *ndpi_struct, + struct ndpi_flow_struct *flow, + uint32_t version, + u_int8_t *clear_payload, uint32_t clear_payload_len, + uint64_t *crypto_data_len); +extern void process_tls(struct ndpi_detection_module_struct *ndpi_struct, + struct ndpi_flow_struct *flow, + const u_int8_t *crypto_data, uint32_t crypto_data_len, + uint32_t version); +extern void process_chlo(struct ndpi_detection_module_struct *ndpi_struct, + struct ndpi_flow_struct *flow, + const u_int8_t *crypto_data, uint32_t crypto_data_len); +extern int is_version_with_tls(uint32_t version); + + +int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) { + const u_int8_t *crypto_data; + uint64_t crypto_data_len; + u_int32_t first_int, version = 0; + + if(ndpi_info_mod == NULL) { + ndpi_info_mod = ndpi_init_detection_module(ndpi_enable_ja3_plus); + NDPI_PROTOCOL_BITMASK all; + NDPI_BITMASK_SET_ALL(all); + ndpi_set_protocol_detection_bitmask2(ndpi_info_mod, &all); +#if 0 + NDPI_PROTOCOL_BITMASK debug_bitmask; + NDPI_BITMASK_SET_ALL(debug_bitmask); + ndpi_set_log_level(ndpi_info_mod, 4); + ndpi_set_debug_bitmask(ndpi_info_mod, debug_bitmask); +#endif + ndpi_finalize_initialization(ndpi_info_mod); + + flow = ndpi_calloc(1, SIZEOF_FLOW_STRUCT); + } + + if(Size < 4) + return 0; + + first_int = ntohl(*(u_int32_t *)Data); + if((first_int % 4) == 0) + version = 0x00000001; /* v1 */ + else if((first_int % 4) == 1) + version = 0x51303530; /* Q050 */ + else if((first_int % 4) == 2) + version = 0x51303436; /* Q046 */ + else if((first_int % 4) == 3) + version = 0x709A50C4; /* v2 */ + + memset(flow, '\0', sizeof(*flow)); + flow->detected_protocol_stack[0] = NDPI_PROTOCOL_QUIC; + flow->l4_proto = IPPROTO_UDP; + + crypto_data = get_crypto_data(ndpi_info_mod, flow, version, (u_int8_t *)Data + 4, Size - 4, &crypto_data_len); + + if(crypto_data) { + if(!is_version_with_tls(version)) { + process_chlo(ndpi_info_mod, flow, crypto_data, crypto_data_len); + } else { + process_tls(ndpi_info_mod, flow, crypto_data, crypto_data_len, version); + } + } + + ndpi_free_flow_data(flow); + + return 0; +} + +#ifdef BUILD_MAIN +int main(int argc, char ** argv) +{ + FILE * data_file; + long data_file_size; + uint8_t * data_buffer; + int test_retval; + + if (argc != 2) { + fprintf(stderr, "usage: %s: [data-file]\n", + (argc > 0 ? argv[0] : "fuzz_quic_get_crypto_data")); + return 1; + } + + data_file = fopen(argv[1], "r"); + if (data_file == NULL) { + perror("fopen failed"); + return 1; + } + + if (fseek(data_file, 0, SEEK_END) != 0) { + perror("fseek(SEEK_END) failed"); + fclose(data_file); + return 1; + } + + data_file_size = ftell(data_file); + if (data_file_size < 0) { + perror("ftell failed"); + fclose(data_file); + return 1; + } + + if (fseek(data_file, 0, SEEK_SET) != 0) { + perror("fseek(0, SEEK_SET) failed"); + fclose(data_file); + return 1; + } + + data_buffer = malloc(data_file_size); + if (data_buffer == NULL) { + perror("malloc failed"); + fclose(data_file); + return 1; + } + + if (fread(data_buffer, sizeof(*data_buffer), data_file_size, data_file) != (size_t)data_file_size) { + perror("fread failed"); + fclose(data_file); + free(data_buffer); + return 1; + } + + test_retval = LLVMFuzzerTestOneInput(data_buffer, data_file_size); + fclose(data_file); + free(data_buffer); + + return test_retval; +} +#endif diff --git a/src/lib/protocols/quic.c b/src/lib/protocols/quic.c index 433bc0261..7f405b433 100644 --- a/src/lib/protocols/quic.c +++ b/src/lib/protocols/quic.c @@ -172,7 +172,7 @@ static int is_version_with_encrypted_header(uint32_t version) ((version & 0xFFFFFF00) == 0x51303500) /* Q05X */ || ((version & 0xFFFFFF00) == 0x54303500) /* T05X */; } -static int is_version_with_tls(uint32_t version) +int is_version_with_tls(uint32_t version) { return is_version_quic(version) || ((version & 0xFFFFFF00) == 0x54303500) /* T05X */; @@ -1126,11 +1126,11 @@ static const uint8_t *get_reassembled_crypto_data(struct ndpi_detection_module_s return NULL; } -static const uint8_t *get_crypto_data(struct ndpi_detection_module_struct *ndpi_struct, - struct ndpi_flow_struct *flow, - uint32_t version, - u_int8_t *clear_payload, uint32_t clear_payload_len, - uint64_t *crypto_data_len) +const uint8_t *get_crypto_data(struct ndpi_detection_module_struct *ndpi_struct, + struct ndpi_flow_struct *flow, + uint32_t version, + u_int8_t *clear_payload, uint32_t clear_payload_len, + uint64_t *crypto_data_len) { const u_int8_t *crypto_data = NULL; uint32_t counter; @@ -1222,12 +1222,12 @@ static const uint8_t *get_crypto_data(struct ndpi_detection_module_struct *ndpi_ case 0x06: NDPI_LOG_DBG2(ndpi_struct, "CRYPTO frame\n"); counter += 1; - if(counter > clear_payload_len || - counter + quic_len_buffer_still_required(clear_payload[counter]) > clear_payload_len) + if(counter >= clear_payload_len || + counter + quic_len_buffer_still_required(clear_payload[counter]) >= clear_payload_len) return NULL; counter += quic_len(&clear_payload[counter], &frag_offset); - if(counter > clear_payload_len || - counter + quic_len_buffer_still_required(clear_payload[counter]) > clear_payload_len) + if(counter >= clear_payload_len || + counter + quic_len_buffer_still_required(clear_payload[counter]) >= clear_payload_len) return NULL; counter += quic_len(&clear_payload[counter], &frag_len); if(frag_len + counter > clear_payload_len) { @@ -1306,10 +1306,11 @@ static uint8_t *get_clear_payload(struct ndpi_detection_module_struct *ndpi_stru return clear_payload; } -static void process_tls(struct ndpi_detection_module_struct *ndpi_struct, - struct ndpi_flow_struct *flow, - const u_int8_t *crypto_data, uint32_t crypto_data_len, - uint32_t version) + +void process_tls(struct ndpi_detection_module_struct *ndpi_struct, + struct ndpi_flow_struct *flow, + const u_int8_t *crypto_data, uint32_t crypto_data_len, + uint32_t version) { struct ndpi_packet_struct *packet = &ndpi_struct->packet; @@ -1341,9 +1342,9 @@ static void process_tls(struct ndpi_detection_module_struct *ndpi_struct, ndpi_set_detected_protocol(ndpi_struct, flow, NDPI_PROTOCOL_DOH_DOT, NDPI_PROTOCOL_QUIC, NDPI_CONFIDENCE_DPI); } } -static void process_chlo(struct ndpi_detection_module_struct *ndpi_struct, - struct ndpi_flow_struct *flow, - const u_int8_t *crypto_data, uint32_t crypto_data_len) +void process_chlo(struct ndpi_detection_module_struct *ndpi_struct, + struct ndpi_flow_struct *flow, + const u_int8_t *crypto_data, uint32_t crypto_data_len) { const uint8_t *tag; uint32_t i;