fuzz: extend fuzzing coverage

This commit is contained in:
Nardi Ivan
2023-10-15 12:00:26 +02:00
committed by Ivan Nardi
parent b6179467eb
commit 16b4913be6
10 changed files with 131 additions and 5 deletions
+1
View File
@@ -80,6 +80,7 @@
/fuzz/fuzz_gcrypt_light
/fuzz/fuzz_gcrypt_aes
/fuzz/fuzz_gcrypt_gcm
/fuzz/fuzz_gcrypt_cipher
/fuzz/fuzz_ndpi_reader_payload_analyzer
/fuzz/fuzz_filecfg_protocols
/fuzz/fuzz_readerutils_workflow
+16 -1
View File
@@ -6,7 +6,7 @@ bin_PROGRAMS += fuzz_ds_patricia fuzz_ds_ahocorasick fuzz_ds_libcache fuzz_ds_tr
#Third party
bin_PROGRAMS += fuzz_libinjection fuzz_binaryfusefilter
#Internal crypto
bin_PROGRAMS += fuzz_gcrypt_light fuzz_gcrypt_aes fuzz_gcrypt_gcm
bin_PROGRAMS += fuzz_gcrypt_light fuzz_gcrypt_aes fuzz_gcrypt_gcm fuzz_gcrypt_cipher
#Configuration files
bin_PROGRAMS += fuzz_filecfg_protocols
#Reader utils
@@ -457,6 +457,21 @@ fuzz_gcrypt_gcm_LINK=$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \
$(LIBTOOLFLAGS) --mode=link $(CXX) @NDPI_CFLAGS@ $(AM_CXXFLAGS) $(CXXFLAGS) \
$(fuzz_gcrypt_gcm_LDFLAGS) @NDPI_LDFLAGS@ $(LDFLAGS) -o $@
fuzz_gcrypt_cipher_SOURCES = fuzz_gcrypt_cipher.cpp
fuzz_gcrypt_cipher_CXXFLAGS = @NDPI_CFLAGS@ $(CXXFLAGS) -I ../src/lib/third_party/include/
fuzz_gcrypt_cipher_CFLAGS = @NDPI_CFLAGS@ $(CXXFLAGS) -I ../src/lib/third_party/include/
fuzz_gcrypt_cipher_LDADD = ../src/lib/libndpi.a $(ADDITIONAL_LIBS)
fuzz_gcrypt_cipher_LDFLAGS = $(LIBS)
if HAS_FUZZLDFLAGS
fuzz_gcrypt_cipher_CXXFLAGS += $(LIB_FUZZING_ENGINE)
fuzz_gcrypt_cipher_CFLAGS += $(LIB_FUZZING_ENGINE)
fuzz_gcrypt_cipher_LDFLAGS += $(LIB_FUZZING_ENGINE)
endif
# force usage of CXX for linker
fuzz_gcrypt_cipher_LINK=$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \
$(LIBTOOLFLAGS) --mode=link $(CXX) @NDPI_CFLAGS@ $(AM_CXXFLAGS) $(CXXFLAGS) \
$(fuzz_gcrypt_cipher_LDFLAGS) @NDPI_LDFLAGS@ $(LDFLAGS) -o $@
fuzz_filecfg_protocols_SOURCES = fuzz_filecfg_protocols.c fuzz_common_code.c
fuzz_filecfg_protocols_CFLAGS = @NDPI_CFLAGS@ $(CXXFLAGS)
fuzz_filecfg_protocols_LDADD = ../src/lib/libndpi.a $(ADDITIONAL_LIBS)
+105
View File
@@ -0,0 +1,105 @@
#include <stdlib.h>
#include <stdint.h>
#include <assert.h>
#include "fuzzer/FuzzedDataProvider.h"
#define MBEDTLS_CHECK_RETURN_TYPICAL
#define MBEDTLS_INTERNAL_VALIDATE_RET( cond, ret ) do { } while( 0 )
#include "../src/lib/third_party/include/gcrypt/cipher.h"
#include "../src/lib/third_party/include/gcrypt/aes.h"
extern int force_no_aesni;
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
FuzzedDataProvider fuzzed_data(data, size);
int key_lens[] = { 128, 192, 256 };
int key_len, iv_len, rc_e, rc_d, input_length;
unsigned char *output, *decrypted;
size_t output_size, output_size2, decrypted_size;
mbedtls_cipher_type_t cipher_type;
/* TODO: GCM. This code/fuzzer doesn't work with GCM ciphers. Not sure why.. :( */
const char *cipher_names[] = { NULL, "", "AES-128-ECB", "AES-192-ECB", "AES-256-ECB",
/* "AES-128-GCM", "AES-192-GCM", "AES-256-GCM" */ };
const char *cipher_name;
mbedtls_cipher_context_t *ctx_e, *ctx_d;
/* No real memory allocations involved */
if(fuzzed_data.remaining_bytes() < 512) /* Some data */
return -1;
posix_memalign((void **)&ctx_e, 8, sizeof(mbedtls_cipher_context_t));
posix_memalign((void **)&ctx_d, 8, sizeof(mbedtls_cipher_context_t));
key_len = fuzzed_data.PickValueInArray(key_lens);
std::vector<unsigned char>key = fuzzed_data.ConsumeBytes<u_int8_t>(key_len / 8);
iv_len = fuzzed_data.ConsumeIntegralInRange(0, MBEDTLS_MAX_IV_LENGTH + 1);
std::vector<u_int8_t>iv = fuzzed_data.ConsumeBytes<uint8_t>(iv_len);
input_length = fuzzed_data.ConsumeIntegralInRange(16, 17);
std::vector<unsigned char>input = fuzzed_data.ConsumeBytes<u_int8_t>(input_length);
output = (unsigned char *)malloc(input_length);
decrypted = (unsigned char *)malloc(input_length);
mbedtls_cipher_list();
/* Random iteration */
cipher_type = static_cast<mbedtls_cipher_type_t>(fuzzed_data.ConsumeIntegralInRange(0, (int)MBEDTLS_CIPHER_AES_256_KWP) + 1);
mbedtls_cipher_info_from_type(cipher_type);
/* Real cipher used */
cipher_name = cipher_names[fuzzed_data.ConsumeIntegralInRange(0, (int)(sizeof(cipher_names) / sizeof(char *) - 1))];
mbedtls_cipher_init(ctx_e);
mbedtls_cipher_init(ctx_d);
ctx_e->cipher_info = mbedtls_cipher_info_from_string(cipher_name);
ctx_d->cipher_info = ctx_e->cipher_info;
mbedtls_cipher_info_get_mode(ctx_e->cipher_info);
mbedtls_cipher_info_get_type(ctx_e->cipher_info);
mbedtls_cipher_info_get_name(ctx_e->cipher_info);
mbedtls_cipher_info_has_variable_key_bitlen(ctx_e->cipher_info);
mbedtls_cipher_info_get_iv_size(ctx_e->cipher_info);
mbedtls_cipher_info_get_block_size(ctx_e->cipher_info);
mbedtls_cipher_get_cipher_mode(ctx_e);
mbedtls_cipher_info_get_key_bitlen(ctx_e->cipher_info);
posix_memalign((void **)&ctx_e->cipher_ctx, 8, sizeof(mbedtls_aes_context));
posix_memalign((void **)&ctx_d->cipher_ctx, 8, sizeof(mbedtls_aes_context));
rc_e = mbedtls_cipher_setkey(ctx_e, key.data(), key.size() * 8, MBEDTLS_ENCRYPT);
rc_d = mbedtls_cipher_setkey(ctx_d, key.data(), key.size() * 8, MBEDTLS_DECRYPT);
if(rc_e == 0 && rc_d == 0) {
rc_e = mbedtls_cipher_set_iv(ctx_e, iv.data(), iv.size());
rc_d = mbedtls_cipher_set_iv(ctx_d, iv.data(), iv.size());
if(rc_e == 0 && rc_d == 0) {
mbedtls_cipher_reset(ctx_e);
mbedtls_cipher_reset(ctx_d);
rc_e = mbedtls_cipher_update(ctx_e, input.data(), input.size(), output, &output_size);
if(rc_e == 0) {
rc_e = mbedtls_cipher_finish(ctx_e, NULL, &output_size2);
if(rc_e == 0) {
rc_d = mbedtls_cipher_update(ctx_d, output, output_size, decrypted, &decrypted_size);
if(rc_d == 0) {
rc_d = mbedtls_cipher_finish(ctx_d, NULL, &output_size2);
/* TODO: decryption doesn't work with no-aesni data path!
Note that with MASAN, aesni is always disabled */
#if 0
if(rc_d == 0) {
assert(input.size() == decrypted_size);
assert(memcmp(input.data(), decrypted, decrypted_size) == 0);
}
#endif
}
}
}
}
}
free(output);
free(decrypted);
free(ctx_e->cipher_ctx);
free(ctx_e);
free(ctx_d->cipher_ctx);
free(ctx_d);
return 0;
}
-1
View File
@@ -37,7 +37,6 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
aes_e_ctx = (mbedtls_aes_context *)malloc(sizeof(mbedtls_aes_context));
aes_d_ctx = (mbedtls_aes_context *)malloc(sizeof(mbedtls_aes_context));
/* Not sure if it is really necessary... */
force_no_aesni = 0;
if(fuzzed_data.ConsumeBool())
force_no_aesni = 1;
+3
View File
@@ -5,6 +5,7 @@
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
struct libinjection_sqli_state state;
char fingerprint[8];
/* No memory allocations involved */
@@ -21,6 +22,8 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
libinjection_sqli_init(&state, (char *)data, size, FLAG_QUOTE_DOUBLE | FLAG_SQL_MYSQL);
libinjection_is_sqli(&state);
libinjection_sqli((char *)data, size, fingerprint);
libinjection_xss((char *)data, size);
libinjection_version();
+1 -1
View File
@@ -141,7 +141,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
ndpi_free(workflow->ndpi_flows_root);
/* Free payload analyzer data, without printing */
if(enable_payload_analyzer)
ndpi_report_payload_stats(NULL);
ndpi_report_payload_stats(stdout);
#ifdef ENABLE_PAYLOAD_ANALYZER
ndpi_update_params(SPLT_PARAM_TYPE, "splt_param.txt");
@@ -0,0 +1,2 @@
[libfuzzer]
close_fd_mask=1
+1 -1
View File
@@ -5881,7 +5881,7 @@ static int fully_enc_heuristic(struct ndpi_detection_module_struct *ndpi_str,
cnt += 1;
cnt_consecutives += 1;
if(cnt_consecutives >= 20) { /* Ex4 */
return 0;;
return 0;
}
} else {
cnt_consecutives = 0;
-1
View File
@@ -2059,7 +2059,6 @@ const char* ndpi_risk2str(ndpi_risk_enum risk) {
case NDPI_TLS_ALPN_SNI_MISMATCH:
return("ALPN/SNI Mismatch");
break;
case NDPI_MALWARE_HOST_CONTACTED:
return("Client contacted a malware host");
+2
View File
@@ -70,6 +70,8 @@ static void aes_gen_tables( void )
#if defined(MBEDTLS_AESNI_C) && defined(MBEDTLS_HAVE_X86_64)
if( mbedtls_aesni_has_support( MBEDTLS_AESNI_AES ) )
aes_aesni_has_support = 1;
else
aes_aesni_has_support = 0;
#endif
/*