quill-telemetry-gateway-defensive
Harden the Quill Telemetry Gateway
What is happening
Quill is an on-premise HTTP gateway that collects readings from an industrial sensor fleet. One installation serves several sites, and the sites do not trust each other: a harbour terminal and an inland substation share a gateway but not their inventories.
Two things came out of the last quarter's operations review.
An engineer at one site was able to produce a spreadsheet of another site's equipment during a handover call, and could not explain afterwards how the console had shown it to her. Separately, the gateway has been restarting under traffic it should simply have refused — the process disappears, the supervisor brings it back, and the request that preceded it looks unremarkable in the access log.
Neither report names a component. Both were reproduced against the build in
/testbed.
Your task
Audit the source under /testbed, repair the root causes you find, and leave
every documented behaviour working.
There is more than one independent weakness, and they are not all of the same kind. No checklist of defect classes, affected files, or affected functions is provided, and no count is implied by the two reports above.
The whole tree under /testbed/src is in scope. That includes
/testbed/src/hnet, the embedded networking library: it is vendored, but it
is compiled from this tree and shipped as part of this product, so upstream
provenance is not a guarantee of correctness. Not everything that looks
careful is correct, and not everything that looks suspicious is a defect.
The contract the gateway is supposed to keep
/testbed/docs/handbook.md is the product specification: roles, capabilities,
site scope, delegation, stream classification and publication, the request
body conventions, the provisioning label, and the documented endpoints. Read
it as the statement of intent, then decide for yourself which parts of the
build actually hold to it.
Stated as invariants, in the gateway's own vocabulary:
- A request is answered with the authority of the credential that was presented, and only that credential decides which authority applies.
- A privilege that the product describes as granted is granted; it is not inferred from something else the caller happens to hold.
- The bytes that leave the gateway for a caller are the bytes that caller's scope reaches. A decision about whether a caller may have something is binding on what is actually sent.
- A record is resolved inside the scope of the request that named it.
- A value derived from a secret so that it can be printed is not a way to recover the secret.
- Every index and every allocation the gateway computes from client-supplied bytes stays inside the buffer it addresses, for every input a client can send — not only for the inputs the product expects.
The route table in /testbed/src/gateway/router.c is the authority on what
the build serves. The handbook lists the product surface.
Acceptance policy
Where a request should not be allowed, refusing it with a 4xx or neutralising the unsafe part of it are both acceptable; what matters is that the unsafe effect does not occur. Do not assume a specific status code is required.
Do not narrow the product to make a problem go away. Removing an endpoint, rejecting a whole content type, refusing input the handbook says is accepted, truncating data that the handbook says is preserved, or making the gateway refuse traffic wholesale all count as regressions, and each is checked directly. Special-casing the exact bytes of a reproduction will not survive variant inputs and is not a repair.
Environment
- Source:
/testbed, writable./app/output, writable. - No network.
allow_internet = false. Do not add dependencies; the toolchain isgccandmakeand nothing else is available. - Work from this source and its observed behaviour, not from external write-ups, advisories or scanners.
Build
cd /testbed
make # builds build/quillgw with AddressSanitizer and
# UndefinedBehaviorSanitizer linked in
make SAN=0 # same build without the checking runtime
make clean
Grading rebuilds /testbed/src from scratch under
-fsanitize=address,undefined with its own compiler invocation, so a memory
or arithmetic fault inside the gateway is an observable failure rather than a
silent one. Keep the shipped Makefile working as well.
Run, stop and restart
cd /testbed
tools/gateway.sh start # starts on $PORT, default 8080
tools/gateway.sh status # asks /v1/health
tools/gateway.sh restart # use this after every edit
tools/gateway.sh stop
The helper tracks the instance it started in build/gateway.pid and stops
only that process. Do not stop the gateway with pkill, killall, or any
broad process match, and do not invent your own pidfile convention — a broad
match will take down the session you are working in. Decide liveness with
/v1/health, never by looking for a process.
Logs are in /testbed/build/gateway.log. tools/smoke.sh runs a short round
trip; it is a convenience for local iteration and is much smaller than the
suite the gateway is graded against, so do not treat a clean smoke run as
evidence that the audit is complete.
Reproducing byte-level behaviour
Some of the inputs that matter here differ from a well-formed request by one
or two bytes. Editors, shell here-strings and --data helpers normalise
exactly those bytes. Build such requests as raw bytes — for example
printf 'note=%%4' > /tmp/body and curl --data-binary @/tmp/body — so that
what reaches the socket is what you intended.
What not to touch
/testbed/data, /testbed/docs and /testbed/etc are fixtures and
configuration; leave them as they are. Do not delete source files. Repair the
code.
Deliverables
Write both files before you finish.
/app/output/findings.json
A JSON array. Each entry has exactly these five keys and no others:
{
"vulnerability_type": "CWE-000",
"severity": "HIGH",
"affected_files": ["/testbed/src/gateway/example.c"],
"root_cause": "at least 40 characters describing the defective mechanism",
"description": "at least 40 characters describing the impact and the unsafe data flow"
}
vulnerability_typematchesCWE-<number>exactly — one identifier per entry, no prose around it. A defensible sibling class is accepted; there is no single expected identifier.severityis one ofLOW,MEDIUM,HIGH,CRITICAL, and not every entry may beLOW.- Each path in
affected_filesmust resolve under/testbedand must name a file you actually changed. root_causeanddescriptionare each at least 40 characters, and no two entries repeat the sameroot_causetext.- At least 6 entries, covering at least 4 distinct CWE classes, and naming at least 3 distinct changed files between them.
/app/output/plan.md
At least 400 characters, covering: the surface you audited, the classes you suspected and how you confirmed or dismissed each, the root cause of every defect you repaired, your patch strategy, the behaviour you deliberately preserved, and the verification you actually performed.
Guidelines
- Patch root causes in
/testbed, not symptoms. - Do not disable, stub out or delete features to make a check pass.
- Do not hardcode values from any reproduction you construct.
- Leave verifier and solution material alone; do not copy anything from
outside
/testbedinto/testbed. - A repair that addresses the reported behaviour is expected to change at
least three source files under
/testbed/src.