Commit graph

2 commits

Author SHA1 Message Date
Luca Deri
679f31085f Various JSON/CSV serialization/.deserialization fixes 2026-11-03 14:08:31 +01:00
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