← All posts / Security Engineering 2026-11-28 · 3 min read

Auditing C/C++ Network Daemons: Wire Invariants, Unsigned Underflow, and Standalone ASan Harnesses

A practical methodology for finding pre-auth memory corruption in parser-heavy network services by checking mathematical invariants at trust boundaries and verifying with minimal ASan/UBSan harnesses.

C/C++Memory SafetyASanProtocol ParsersSystems

Long-lived C and C++ network daemons rarely fail because a programmer forgot strncpy exists. They fail at state transitions and arithmetic boundaries where two independently reasonable assumptions quietly contradict each other.

In practice, reviewing parser-heavy network services—VPN terminators, RPC dispatchers, binary serialization verifiers, or packet decoders—comes down to auditing invariants systematically from first principles rather than scanning for banned function names.

1. The Three Invariants That Break Most Often

Unsigned Length Subtraction Before Bounds Checks

Consider a packet parser checking a fixed header length HH against a size_t or uint32_t payload length LL:

int parse_frame(const uint8_t *buf, size_t total_len) {
    /* Assumption: caller already checked header size (not always true) */
    size_t payload_len = total_len - HEADER_BYTES;
    if (payload_len > MAX_PAYLOAD) {
        return -1;
    }
    memcpy(out, buf + HEADER_BYTES, payload_len);
    return 0;
}

When total_len < HEADER_BYTES, unsigned subtraction wraps L−H(mod264)L - H \pmod{2^{64}} (or 2322^{32}). Because payload_len becomes huge, the > check may still reject it—unless payload_len is first converted to a signed int, masked, rounded up for alignment, or subtracted on the opposite side of a comparison like pos + len > end where integer overflow wraps around.

The clean algebraic invariant is always addition-free, subtraction-free comparison:

if (total_len < HEADER_BYTES || (size_t)(end - cur) < field_len) {
    return -1;
}

Parallel Arrays & Asymmetric Offsets in Binary Formats

Binary serialization formats (such as table/union encodings) often store type tags in one vector and offsets/values in a second vector. If a verifier validates each element individually without asserting that vector lengths match—or assumes that struct elements inside a union contain an extra vtable/table indirection level when they are actually stored inline—bounds validation diverges from the decoder’s actual pointer arithmetic.

In google/flatbuffers#9506, for instance, enforcing structural offset checks for struct values inside unions and rejecting asymmetric union type/value vectors prevents verifier-accepted buffers from reading out of bounds during traversal.

Stale Callbacks & Pointer Ownership Across Disconnects

Event-driven daemons juggle socket lifetimes, TLS contexts, and queued write buffers across asynchronous libevent/epoll callbacks. A recurring failure pattern occurs when:

  1. A connection enters a write-queued state and registers an event callback (EV_WRITE).
  2. An error or peer disconnect tears down the TLS object (tls = NULL) or frees the parent state.
  3. A pending queue flush still invokes send_queued() with an active callback handle pointing into cleared or freed state.

Whenever a state object owns multiple asynchronous handles, teardown must clear every registered interest bit and verify non-null invariants at the sink—not just along the happy disconnect path.

2. Why Fuzzers Miss Reachable Bugs

Even heavily fuzzed repositories leave blind spots:

  • Fuzz targets only wrap top-level entrypoints. Auxiliary subcommands, secondary parsers, optional flags, or legacy framing handlers often have zero fuzz harness coverage.
  • State machine depth. A bug gated behind a specific handshake order, non-default capability advertisement, or disconnect sequence may require millions of blind mutations unless triggered structurally.
  • Unverified assumptions between siblings. When maintainers patch vulnerability AA in function parse_v1(), the sibling handler parse_v2() or shared utility helper frequently stays unpatched.

3. Standalone Differential Harnesses Over Heavy Builds

Before drafting a minimal patch, I verify every candidate against the real upstream source—never synthetic pseudocode—using standalone -fsanitize=address,undefined harnesses:

clang -O1 -g -fsanitize=address,undefined -fno-omit-frame-pointer \
  harness.c upstream_module.c -o verify_bug

A good verification harness proves three properties deterministically:

  1. Unpatched failure: Exact ASan/UBSan trace on minimal malformed input (exact byte length, offset, and fault site).
  2. Patched rejection: Same malformed input returns a clean error code with zero sanitizer findings (exit 0).
  3. Functional non-regression: Valid protocol inputs continue parsing identically before and after the diff.

Keeping diffs narrow, checking git diff --check, and avoiding speculative rewrites makes maintainer review fast—and ensures fixes hold cleanly across stable release branches.