nDPI/fuzz/fuzz_deserialization.cpp
Ivan Nardi da1995058f
Merge commit from fork
The TLV deserializer (`ndpi_serializer.c`) could be driven into out-of-bounds
reads, an adjacent-heap info leak, a NULL-pointer crash and an infinite loop
by a malformed/truncated byte stream. These paths are reachable whenever an
embedder deserializes untrusted input (e.g. a collector reading nDPI TLV
records off a socket); nDPI itself only serializes, so no in-tree packet path
is affected.

Reported issues (GHSA-mmp7-3vgp-v8mg):
  1. `ndpi_deserialize_get_single_size()`: return type widened u_int16_t -> int
     so the -2 truncation sentinel survives for string/block/list types.
  2. `ndpi_deserialize_get_single_size()`: bounds-check each fixed-width numeric
     field (uint8..double) against the bytes remaining before returning a size.
  3. `ndpi_deserialize_get_single_string_size()`: guard `offset > buffer.size`
     before computing buff_diff, so it can no longer underflow.
  4. 'expected' accumulators widened u_int16_t -> u_int32_t across the
     deserialize paths, so two field sizes can no longer wrap mod 2^16.
  5. `ndpi_deserialize_clone_item()`: re-check the source holds 'expected' bytes
     before the memcpy, closing the heap over-read / info leak.

Additional issues found while fuzzing the fixed code (new `fuzz_deserialization`
target, see below), all in `ndpi_deserialize_clone_all()` on untrusted input:
  6. Infinite-loop DoS: the return value of `ndpi_deserialize_next()` was
     ignored, so a record that cannot advance the cursor looped forever.
     `clone_all()` now validates the whole record is present before reading it
     and breaks when `next()` reports an error.
  7. NULL / out-of-bounds string reads: `ndpi_deserialize_single_string()` now
     bounds the on-wire length to the buffer (returns {NULL,0} otherwise), and
     `ndpi_deserialize_key_string()` verifies the key subtype is a string before
     reading it (the guard `ndpi_deserialize_value_string()` already had).
  8. `atoi(NULL)` crash: ndpi_is_number() now treats NULL/empty as "not a
     number", so a NULL key can no longer reach `atoi()`.
  9. `atoi()` over-read on a numeric key: the `ndpi_serialize_binary_*()` helpers
      called `atoi()` on a counted, non-NUL-terminated key, reading past its
      length when the buffer continued with more digits. A bounded `ndpi_atoi_n()`
      now converts exactly key_len digits.

A new libFuzzer harness (`fuzz/fuzz_deserialization.cpp`) feeds untrusted bytes
directly to ndpi_init_deserializer_buf() + the clone/iterate APIs, with a seed
corpus reproducing every issue above.

Original report by Mike Myers (Apple).
2026-10-16 10:59:50 +02:00

123 lines
4.3 KiB
C++

#include "ndpi_api.h"
#include "fuzz_common_code.h"
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "fuzzer/FuzzedDataProvider.h"
/* This fuzzer feeds *untrusted, attacker-controlled* bytes directly to the
* nDPI TLV deserializer public API (ndpi_init_deserializer_buf() + the
* deserialize/clone/iterate functions).
*
* This is exactly the usage pattern of an embedder that receives nDPI TLV
* records from the network (e.g. a collector reading them off a socket).
*/
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
/* To allow memory allocation failures */
fuzz_set_alloc_callbacks_and_seed(size);
/* The deserializer needs at least a 2-byte header (version + format) */
if(size < 2)
return 0;
/* ndpi_init_deserializer_buf() does NOT copy the input: it stores the
* pointer and reads through it in place. Copy into an exactly-sized heap
* buffer so that any read past the logical end lands in an ASan redzone
* and is reported, instead of silently hitting valid adjacent memory.
*
* Use libc malloc() (not ndpi_malloc()) on purpose: this models the bytes
* an embedder received off the network, which do not come from nDPI's
* allocator, and keeps the input buffer present regardless of the
* allocation-failure injection above (so the truncated-input defects
* reproduce deterministically). */
u_int8_t *buf = (u_int8_t *) malloc(size);
if(buf == NULL)
return 0;
memcpy(buf, data, size);
/* Force a valid version byte so the version gate doesn't reject most inputs;
* everything after it (format + TLV records) stays attacker-controlled. */
buf[0] = 1;
ndpi_deserializer deserializer;
ndpi_serializer serializer;
int rc;
/* 1) Clone the current item into a TLV serializer (ndpi_deserialize_clone_item) */
if(ndpi_init_deserializer_buf(&deserializer, buf, size) == 0) {
if(ndpi_init_serializer(&serializer, ndpi_serialization_format_tlv) == 0) {
ndpi_deserialize_clone_item(&deserializer, &serializer);
ndpi_term_serializer(&serializer);
}
}
/* 2) Clone the whole stream into a JSON serializer (ndpi_deserialize_clone_all) */
if(ndpi_init_deserializer_buf(&deserializer, buf, size) == 0) {
if(ndpi_init_serializer(&serializer, ndpi_serialization_format_json) == 0) {
ndpi_deserialize_clone_all(&deserializer, &serializer);
ndpi_term_serializer(&serializer);
}
}
/* 3) Walk the stream with the public iteration API, reading keys/values
* according to the advertised types (exercises ndpi_deserialize_next()
* and the single_string / single value readers). Bounded to avoid long
* loops on adversarial inputs. */
if(ndpi_init_deserializer_buf(&deserializer, buf, size) == 0) {
ndpi_serialization_type kt, et;
unsigned int guard = 0;
while(guard++ < 1024 &&
(et = ndpi_deserialize_get_item_type(&deserializer, &kt)) != ndpi_serialization_unknown) {
/* Read the key */
if(kt == ndpi_serialization_string) {
ndpi_string ks;
ndpi_deserialize_key_string(&deserializer, &ks);
} else {
u_int32_t k32;
ndpi_deserialize_key_uint32(&deserializer, &k32);
}
/* Read the value according to its type */
switch(et) {
case ndpi_serialization_uint8:
case ndpi_serialization_uint16:
case ndpi_serialization_uint32: {
u_int32_t v; ndpi_deserialize_value_uint32(&deserializer, &v); break;
}
case ndpi_serialization_uint64: {
u_int64_t v; ndpi_deserialize_value_uint64(&deserializer, &v); break;
}
case ndpi_serialization_int8:
case ndpi_serialization_int16:
case ndpi_serialization_int32: {
int32_t v; ndpi_deserialize_value_int32(&deserializer, &v); break;
}
case ndpi_serialization_int64: {
int64_t v; ndpi_deserialize_value_int64(&deserializer, &v); break;
}
case ndpi_serialization_float: {
float v; ndpi_deserialize_value_float(&deserializer, &v); break;
}
case ndpi_serialization_string:
case ndpi_serialization_start_of_block:
case ndpi_serialization_start_of_list: {
ndpi_string vs; ndpi_deserialize_value_string(&deserializer, &vs); break;
}
default:
break;
}
rc = ndpi_deserialize_next(&deserializer);
if(rc <= 0)
break;
}
}
free(buf);
return 0;
}