binary-telemetry-codec-defensive
Binary Telemetry Codec security review
You are performing an authorized local defensive review of a C telemetry batch codec. The source code is available under /testbed and is writable. Patch the application source so that the verifier can rebuild it and exercise the command-line tool safely.
Application context
The codec ingests compact binary telemetry batches from edge collectors. A batch contains a file header followed by one or more binary frames. Each frame identifies a tenant, metric stream, sequence number, and payload. The command-line tool has two public modes:
./telemetry_codec inspect <batch-file>
./telemetry_codec ingest <batch-file> <state-dir>
./telemetry_codec verify-state <state-dir>
inspect validates and summarizes a batch without writing state. ingest validates a batch and appends accepted records to the state directory. Ingest also maintains a compact checkpoint.tsv file recording the latest accepted sequence for each metric stream, a compact manifest_index.tsv side index with one non-sensitive summary row per committed frame (tenant, metric, stream id, sequence number, and payload length), and a compact digest_index.tsv side index with one non-sensitive checksum row per committed decoded payload (tenant, metric, stream id, sequence number, and an 8-hex-character payload digest). The digest index is also the persistent authority for dedupe/audit frames, so malformed digest-index rows must be rejected before an ingest writes any state.
Persistent state file schemas are part of the public contract and must be preserved exactly. records.tsv rows are tenant<TAB>metric<TAB>sequence<TAB>payload_length<TAB>decoded_payload, with decoded payload bytes written after the fourth tab. checkpoint.tsv rows are stream_id<TAB>latest_sequence. manifest_index.tsv rows are tenant<TAB>metric<TAB>stream_id<TAB>sequence<TAB>payload_length. Current digest_index.tsv rows are tenant<TAB>metric<TAB>stream_id<TAB>sequence<TAB>digest, while legacy four-column digest rows are handled only by the migration rule below.
verify-state performs the same persistent-state recovery and consistency checks that ingest must perform before accepting new input, then prints a compact JSON status summary without ingesting a batch. The state directory may contain crash leftovers from a previous ingest: records.tsv.txn, manifest_index.tsv.txn, digest_index.tsv.txn, checkpoint.tsv.txn, and commit.marker. If all four .txn files and the marker are present, recovery must finalize that transaction before validation. If only a partial transaction is present, recovery must roll back the transaction leftovers and preserve canonical state. After recovery, canonical records.tsv, manifest_index.tsv, digest_index.tsv, and checkpoint.tsv must be cross-checked as one state snapshot before verify-state reports success or ingest accepts a new batch.
To rebuild after editing source:
cd /testbed && make clean && make
The verifier may build with additional sanitizer flags. Your fix must be source changes under /testbed, not a wrapper script.
Normal behavior to preserve
Preserve the public CLI and file contracts:
inspectvalidates and summarizes a batch without writing state.ingestvalidates a batch, appends accepted records, and updates checkpoints plus side indexes.verify-staterecovers and validates persistent state, then returns JSON containingstatus,records, andstreams.- Valid version-1, version-2, and version-3 batches must continue to work. Version 2 carries a manifest section. Version 3 carries 16-bit manifest and dictionary section lengths before frame bytes; either section may be empty. Raw version-3 frames remain valid when no dictionary is needed.
- Persistent TSV schemas are fixed by the Application context section. Do not change row ordering, column meaning, side-index semantics, or the command output shape for valid inputs.
- Stream IDs are compact table indexes
0through15. A tenant or metric identifier is non-empty, at most 31 bytes, and limited to ASCII letters, digits, dots, underscores, and hyphens. - Decoded payloads are compact TSV-safe samples: at most 64 KiB, printable ASCII bytes
0x20through0x7e, after any raw, RLE, or dictionary decoding. - Batches declare
1through128frames. The declared count is authoritative; undeclared trailing bytes are malformed. - Supported frame flags are dedupe/audit, RLE payload, and version-3 dictionary payload. RLE uses complete non-zero
(run_length, byte_value)pairs. Dictionary-encoded frame payloads are 16-bit dictionary indexes. RLE and dictionary payload encodings are mutually exclusive. - Dedupe/audit frames are still decoded, authorized, replay-checked, and payload-checked. A digest match suppresses only duplicate record/index/digest rows for the same tenant, metric, stream, and decoded-payload digest; checkpoint advancement still occurs. New scoped digests commit normally.
- Version-2 manifests are authoritative. Manifest length is capped at 2048 bytes, entry count at 32, and entries bind tenant, metric, stream, and inclusive sequence range. Scopes for the same tenant, metric, and stream must not overlap; adjacent non-overlapping ranges are valid.
- Version-3 dictionaries are authoritative. Dictionary length is capped at 4096 bytes, entry count at 32, values at 4 KiB, and entries bind tenant, metric, stream, sequence range, and decoded value. Same-scope ranges must not overlap. A dictionary frame must resolve to a matching in-scope entry. If a batch has both manifest and dictionary authority, both must approve a frame before commit.
- Per-stream sequence numbers are strictly increasing within a batch and across existing checkpoint state.
- Persistent state is valid only when
records.tsv,manifest_index.tsv,digest_index.tsv, andcheckpoint.tsvagree as one snapshot. Existing state must be recovered and cross-checked beforeverify-statesucceeds oringestaccepts new input. - Legacy four-column digest rows may be migrated to the current five-column format only when existing records and manifest-index rows identify exactly one matching committed record.
- Complete transaction leftovers (
records.tsv.txn,manifest_index.tsv.txn,digest_index.tsv.txn,checkpoint.tsv.txn, andcommit.marker) must be finalized before validation. Partial transaction leftovers must be rolled back without changing canonical state. ingestis all-or-nothing across records, checkpoint, manifest index, and digest index. Rejected input must not partially mutate state or leave temporary files behind. Malformed input returns a non-zero exit status with an actionable error.- No service is started; this is a command-line codec task.
Security and correctness expectations
Review the source end to end. Treat both batch bytes and pre-existing state files as untrusted. Fix root causes that let malformed input, inconsistent authority data, or inconsistent persistent state escape the documented contract, corrupt memory, corrupt state, or change later ingest decisions.
Fix the underlying source-level issues without dropping the public commands, changing the batch format for valid inputs, hardcoding verifier cases, or replacing the parser with a stub.
Hard constraints
- Work only under
/testbed. - Do not edit verifier files, solution files, or environment metadata.
- Do not require internet access at runtime.
- Do not remove
inspectoringest. - Do not remove
verify-state. - Do not hard-code verifier values, filenames, markers, or request strings.
- Preserve the public command-line contracts unless an input violates documented safety boundaries.
- Keep individual shell commands reasonably small; prefer incremental source edits over pasting multiple full files in one terminal command.
Audit findings report
In addition to source changes under /testbed, write a brief audit summary to /app/output/findings.json as a JSON array, one object per distinct vulnerability, with these keys:
vulnerability_type: the CWE identifierseverity:"LOW"|"MEDIUM"|"HIGH"|"CRITICAL"title: short descriptive titledescription: the attack vector and impactaffected_files: list of source file paths under/testbedroot_cause: why the original code was unsafefix_applied: what you changed and why it closes the hole
Also maintain /app/output/plan.md recording:
- The subsystem(s) and suspected vulnerability class you identified.
- The root cause you traced for each.
- The fix approach and why it addresses the root cause.
- How you verified the fix, including both unsafe inputs and legitimate batches.
The findings report does not replace the source fix; the patched source under /testbed is the primary deliverable.