Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 55 additions & 2 deletions .github/workflows/zig-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ jobs:
echo "$RUNNER_TEMP/zig-x86_64-linux-${ZIG_VERSION}" >> "$GITHUB_PATH"

- name: Formatting
run: zig fmt --check build.zig src/root.zig src/wire src/dnssec src/stub src/cli tests
run: zig fmt --check build.zig src/root.zig src/wire src/dnssec src/stub src/cli src/interface/ffi tests

- name: Unit, RFC-vector, denial, replay and fuzz-corpus tests
- name: Unit, RFC-vector, denial, replay, fuzz-corpus and C ABI tests
run: zig build test --summary all

- name: ReleaseSafe build
Expand All @@ -51,3 +51,56 @@ jobs:
run: |
zig build fuzz-tool -Doptimize=ReleaseSafe
./zig-out/bin/dnssec-fuzz all 20 0x2026

# The Idris2 side of the ABI seam: typecheck the proofs, then run the
# Idris2 -> C ABI -> Zig end-to-end test. Idris2 is built from a pinned
# release commit (it has no checksummed binary release).
abi-seam:
runs-on: ubuntu-24.04
timeout-minutes: 45
permissions:
contents: read
env:
ZIG_VERSION: "0.17.0"
ZIG_SHA256: "1cbe9df9f27e6b78d14ccbca43b6703a404ef79ef1c463de901d7f088d4e2026"
IDRIS2_TAG: "v0.7.0"
IDRIS2_COMMIT: "27780073c8631826d846499840b3857d9b9a4fd5"
steps:
- uses: actions/checkout@v7.0.1

- name: Install Zig (checksum-verified)
run: |
set -euo pipefail
curl --proto '=https' --tlsv1.2 -fsSL -o "$RUNNER_TEMP/zig.tar.xz" \
"https://ziglang.org/download/${ZIG_VERSION}/zig-x86_64-linux-${ZIG_VERSION}.tar.xz"
echo "${ZIG_SHA256} $RUNNER_TEMP/zig.tar.xz" | sha256sum -c -
tar -xJf "$RUNNER_TEMP/zig.tar.xz" -C "$RUNNER_TEMP"
echo "$RUNNER_TEMP/zig-x86_64-linux-${ZIG_VERSION}" >> "$GITHUB_PATH"

- name: Build Idris2 from the pinned release commit
run: |
set -euo pipefail
sudo apt-get update -q
sudo apt-get install -y -q chezscheme libgmp-dev
git clone -q --depth 1 --branch "$IDRIS2_TAG" https://github.com/idris-lang/Idris2.git "$RUNNER_TEMP/idris2-src"
test "$(git -C "$RUNNER_TEMP/idris2-src" rev-parse HEAD)" = "$IDRIS2_COMMIT"
make -C "$RUNNER_TEMP/idris2-src" bootstrap SCHEME=chezscheme PREFIX="$RUNNER_TEMP/idris2" -j"$(nproc)"
make -C "$RUNNER_TEMP/idris2-src" install PREFIX="$RUNNER_TEMP/idris2"
echo "$RUNNER_TEMP/idris2/bin" >> "$GITHUB_PATH"

# `idris2 --typecheck` can exit 0 with an Error: line (see
# scripts/check-proofs.sh), so both are checked.
- name: Typecheck the Idris2 ABI model
run: |
set -euo pipefail
out="$(idris2 --typecheck src/interface/abi.ipkg 2>&1)" || { echo "$out"; exit 1; }
echo "$out"
if echo "$out" | grep -q '^Error'; then exit 1; fi

- name: Idris2 -> C ABI -> Zig end-to-end test
run: |
set -euo pipefail
zig build ffi
idris2 --build src/interface/E2e/abi-e2e.ipkg
LD_LIBRARY_PATH=zig-out/lib src/interface/E2e/build/exec/abi-e2e \
tests/testdata/crosscheck.traces tests/testdata/crosscheck-root.anchor
8 changes: 8 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ https://semver.org/spec/v2.0.0.html[Semantic Versioning].
recorded responses replayed offline, mutation attacks, fuzz targets with a
corpus; a cross-check against Unbound and ldns (`tests/crosscheck/`).
* `zig-ci` workflow; container image (`build/container/Containerfile`).
* C ABI (`zig build ffi`): `zig_dnssec.h` as the contract, a pure-Zig
`libzig_dnssec` checked against it at compile time, and an Idris2 model of
the interface (`src/interface/Abi/`) whose result type cannot carry an
answer for a bogus or indeterminate verdict; tests keep the three in step,
and an Idris2 end-to-end test (`just abi-e2e`) runs in CI.

==== Fixed

Expand All @@ -36,6 +41,9 @@ https://semver.org/spec/v2.0.0.html[Semantic Versioning].
==== Changed

* Toolchain pin: Zig 0.16.0 → 0.17.0 (`mise.lock`).
* `src/interface/`: the template's example ABI/FFI seam is replaced by the
zig-dnssec C ABI; its separate `ffi/build.zig` is gone (the root
`build.zig` builds and tests the seam).

=== Inherited from rsr-template-repo at mint (2026-10-13)

Expand Down
15 changes: 10 additions & 5 deletions Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,9 @@ clean-all: clean
# TEST & QUALITY
# ═══════════════════════════════════════════════════════════════════════════════

# Run every detected language's tests: `zig build test` covers src/interface/ffi
# (unit tests in src/main.zig, test/integration_test.zig). It compiles only after
# `just repo-init` has filled the template tokens, so an un-initialised template
# fails here loudly instead of reporting a pass over nothing.
# Run every detected language's tests: `zig build test` (library, C ABI and the
# Idris2/header consistency checks in src/interface/ffi/test/) and
# `idris2 --typecheck src/interface/abi.ipkg` (the seam's proofs).
test: provision::test

# Run tests with verbose output
Expand All @@ -145,9 +144,15 @@ test-smoke:
@echo "FAIL: 'just test-smoke' is not wired yet — no smoke check ran. Edit the 'test-smoke' recipe." >&2; exit 1

# Run end-to-end tests (full pipeline: build → run → verify)
e2e:
e2e: abi-e2e

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Update the E2E validator for the relocated FFI build

The changed e2e recipe still runs tests/e2e.sh, whose template-instantiation test invokes scripts/validate-template.sh; that validator unconditionally requires the now-deleted src/interface/ffi/build.zig and test/integration_test.zig and exits immediately on the first missing file. Consequently every just e2e run fails after the new ABI test succeeds. Update the validator and the instantiation build check to recognize the root build.zig and replacement ABI tests.

Useful? React with 👍 / 👎.

bash tests/e2e.sh

# Idris2 -> C ABI -> Zig validator, over the recorded fixtures. Needs zig and idris2.
abi-e2e:
zig build ffi
idris2 --build src/interface/E2e/abi-e2e.ipkg
LD_LIBRARY_PATH=zig-out/lib src/interface/E2e/build/exec/abi-e2e tests/testdata/crosscheck.traces tests/testdata/crosscheck-root.anchor

# Run aspect tests (cross-cutting concern validation)
aspect:
# Aspect tests validate architectural invariants:
Expand Down
13 changes: 12 additions & 1 deletion README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ $ ./zig-out/bin/dnssec-stub --doh https://cloudflare-dns.com/dns-query dnssec-fa
verdict=bogus reason=dnskey_not_authenticated at=dnssec-failed.org. type=dnskey
----

=== From C, Idris2 and other languages

`zig build ffi` builds `libzig_dnssec` (static and shared, pure Zig, no libc) and
installs `zig_dnssec.h`. The header is the contract: a verdict whose security is never
`SECURE` unless the call succeeded, and answer text only for `SECURE` and `INSECURE`.
The caller supplies the transport as a callback. An Idris2 model of the interface in
`src/interface/Abi/` states the same rule as a type. See
link:src/interface/README.adoc[`src/interface/README.adoc`].

[[ai-install]]
== AI-Assisted Installation (Recommended)

Expand Down Expand Up @@ -121,7 +130,9 @@ is sent anywhere. Running the tests needs no network. Only the live examples abo

[source,console]
----
zig build test # unit, RFC vector, denial, replay and fuzz smoke tests
zig build test # unit, RFC vector, denial, replay, fuzz smoke and C ABI tests
idris2 --typecheck src/interface/abi.ipkg # proofs of the Idris2 ABI model
just abi-e2e # Idris2 -> C ABI -> Zig end to end
zig build fuzz --fuzz=500K -Doptimize=ReleaseSafe # coverage-guided fuzzing
zig build fuzz-tool -Doptimize=ReleaseSafe && ./zig-out/bin/dnssec-fuzz all 60
tests/crosscheck/make-zones.sh && tests/crosscheck/servers.sh start && tests/crosscheck/run.sh
Expand Down
78 changes: 78 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,82 @@ pub fn build(b: *std.Build) void {
});
const fuzz_install = b.addInstallArtifact(fuzz_exe, .{});
b.step("fuzz-tool", "Build the standalone mutation fuzzer (use -Doptimize=ReleaseSafe)").dependOn(&fuzz_install.step);

// ---- C ABI seam (src/interface/) ---------------------------------------
// The header is the contract: translate-c turns it into the module the
// Zig implementation is type-checked against.
const header_path = b.path("src/interface/ffi/include/zig_dnssec.h");
const header_mod = b.addTranslateC(.{
.root_source_file = header_path,
.target = target,
.optimize = optimize,
.link_libc = false,
}).createModule();

const ffi_static = b.addLibrary(.{
.name = "zig_dnssec",
.linkage = .static,
.root_module = ffiModule(b, target, optimize, mod, header_mod, null),
});
const ffi_shared = b.addLibrary(.{
.name = "zig_dnssec",
.linkage = .dynamic,
.root_module = ffiModule(b, target, optimize, mod, header_mod, true),
});
const ffi_step = b.step("ffi", "Build the C ABI (static and shared libzig_dnssec) and install the header");
ffi_step.dependOn(&b.addInstallArtifact(ffi_static, .{}).step);
ffi_step.dependOn(&b.addInstallArtifact(ffi_shared, .{}).step);
ffi_step.dependOn(&b.addInstallFileWithDir(header_path, .header, "zig_dnssec.h").step);

// C-ABI behaviour: calls through the header's declarations into the
// library, plus allocator-checked calls of the same code.
const ffi_tests = b.addTest(.{ .root_module = b.createModule(.{
.root_source_file = b.path("src/interface/ffi/test/abi_test.zig"),
.target = target,
.optimize = optimize,
.imports = &.{
.{ .name = "ffi", .module = ffiModule(b, target, optimize, mod, header_mod, null) },
.{ .name = "zig_dnssec_h", .module = header_mod },
.{ .name = "dnssec", .module = mod },
},
}) });
ffi_tests.root_module.addAnonymousImport("crosscheck.traces", .{ .root_source_file = b.path("tests/testdata/crosscheck.traces") });
ffi_tests.root_module.addAnonymousImport("crosscheck-root.anchor", .{ .root_source_file = b.path("tests/testdata/crosscheck-root.anchor") });
test_step.dependOn(&b.addRunArtifact(ffi_tests).step);

// The Idris2 model and the header must agree: codes, names and layout.
const seam_tests = b.addTest(.{ .root_module = b.createModule(.{
.root_source_file = b.path("src/interface/ffi/test/seam_test.zig"),
.target = target,
.optimize = optimize,
.imports = &.{
.{ .name = "ffi", .module = ffiModule(b, target, optimize, mod, header_mod, null) },
.{ .name = "zig_dnssec_h", .module = header_mod },
.{ .name = "dnssec", .module = mod },
},
}) });
seam_tests.root_module.addAnonymousImport("Types.idr", .{ .root_source_file = b.path("src/interface/Abi/Types.idr") });
seam_tests.root_module.addAnonymousImport("Layout.idr", .{ .root_source_file = b.path("src/interface/Abi/Layout.idr") });
seam_tests.root_module.addAnonymousImport("Foreign.idr", .{ .root_source_file = b.path("src/interface/Abi/Foreign.idr") });
test_step.dependOn(&b.addRunArtifact(seam_tests).step);
}

fn ffiModule(
b: *std.Build,
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
dnssec: *std.Build.Module,
header: *std.Build.Module,
pic: ?bool,
) *std.Build.Module {
return b.createModule(.{
.root_source_file = b.path("src/interface/ffi/src/main.zig"),
.target = target,
.optimize = optimize,
.pic = pic,
.imports = &.{
.{ .name = "dnssec", .module = dnssec },
.{ .name = "zig_dnssec_h", .module = header },
},
});
}
6 changes: 5 additions & 1 deletion docs/AI_INSTALLATION_GUIDE.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ cd zig-dnssec
=== 3. Install the toolchain

The toolchain is pinned in `mise.toml` / `mise.lock` (Zig 0.17.0, just, shellcheck).
Idris2 0.7.0, which checks the ABI model in `src/interface/Abi/`, is not a mise tool:
it comes from pack or a source build (`just doctor` reports whether it is present).

[source,bash]
----
Expand All @@ -48,7 +50,9 @@ SHA-256 against `mise.lock` (or its minisign signature) before use.

[source,bash]
----
zig build test --summary all # unit, RFC-vector, denial, replay, fuzz-corpus tests
zig build test --summary all # unit, RFC-vector, denial, replay, fuzz-corpus, C ABI tests
just test # the above plus the Idris2 typecheck
just abi-e2e # Idris2 -> C ABI -> Zig end to end
zig build -Doptimize=ReleaseSafe # library + dnssec-stub
just validate # repository-standard checks
----
Expand Down
19 changes: 19 additions & 0 deletions docs/architecture/THREAT-MODEL.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,25 @@ Every item here is a known limitation, not a hidden one.
as malformed; trailing zero octets are tolerated.
* A response must answer exactly the question asked, with `QR=1` and opcode QUERY.

== The C ABI (`src/interface/`)

* *What C callers are trusted for* — what any C API trusts: NUL-terminated strings, a
verdict pointer to a whole `zig_dnssec_verdict`, and `text_cap` writable bytes at
`text`. Breaking that is undefined behaviour. The library itself checks NULL
pointers, unknown policy bits, meta-type and zero query types, and malformed names,
anchors and traces, and reports each as a status with `security` NONE.
* *The transport callback is untrusted* — its responses go through the same strict
parser and validation as DoH responses, and a reported length beyond the buffer is a
failed exchange. A failure of the first query is `ZIG_DNSSEC_ERR_QUERY_FAILED`; a
failure of a later DS or DNSKEY fetch is a verdict (`bogus` or `indeterminate`).
* *The caller supplies the time* — the library never reads the clock, so a wrong `now`
gives wrong signature-validity decisions.
* No global state; each call allocates with Zig's thread-safe general-purpose allocator
and frees everything before it returns. The shared object exports the six header
functions and nothing else, and links no libc.
* The Idris2 wrapper reads the verdict struct little-endian and is tested on x86_64
Linux only.

== Trust-anchor handling assumptions

* Anchors are *static configuration*: DS and/or DNSKEY records in presentation format
Expand Down
61 changes: 48 additions & 13 deletions docs/reports/VERIFICATION.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
= zig-dnssec: what has been verified, and what has not
:toc:

Status as of 2026-10-13, Zig 0.17.0. Every claim below is tagged with the estate's
five statuses and names the artefact that discharges it. Nothing is tagged PROVEN:
there is no machine-checked proof in this repository yet.
Status as of 2026-10-13, Zig 0.17.0, Idris2 0.7.0. Every claim below is tagged with the
estate's five statuses and names the artefact that discharges it. The PROVEN rows are
about the C ABI seam (`src/interface/`) and are checked by Idris2; nothing about the
validator's own logic is proved yet.

[cols="1,4",options="header"]
|===
| Status | Meaning here
| PROVEN | discharged by a machine-checked proof (none yet)
| PROVEN | discharged by a machine-checked proof (Idris2 typecheck of `src/interface/abi.ipkg`)
| TESTED | discharged by a test or run that is in the repository and reproducible
| ASSUMED | relied on without being checked here
| DESIGNED | built and reasoned about, but not exercised by a test
Expand Down Expand Up @@ -74,12 +75,41 @@ there is no machine-checked proof in this repository yet.
| Failures are never reported as insecure
| TESTED (for the cases above) / DESIGNED (in general)
| the mutation tests assert `bogus`; the type of `Verdict` makes the answer unreachable
from failure arms. A general proof is OPEN (see the estate note below).
from failure arms, and at the C ABI seam the Idris2 model makes it unrepresentable
(PROVEN row above). A proof about the validator's own decisions is OPEN (see the
estate note below).

| Resource limits (fetches, signatures, failures per RRset, NSEC3 hashes)
| TESTED
| `tests/replay.zig` budget cases; `tests/denial.zig` hash budget

| C ABI seam, Idris2 model: every status, security and reason code round-trips; code 0
decodes to no verdict; the verdict struct's fields are aligned, in order, not
overlapping, and its size is a multiple of its alignment; an `Outcome` for `Bogus` or
`Indeterminate` cannot hold an answer, and `answerOf` cannot be applied to one
| PROVEN
| `src/interface/Abi/Types.idr`, `Layout.idr`, `Foreign.idr` (no `believe_me` or
`assert_total`; `%default total`); `idris2 --typecheck src/interface/abi.ipkg`

| The Idris2 model describes the real interface: codes, field offsets and sizes, struct
size, `%foreign` parameter kinds agree with the C header as the C compiler sees it; the
Zig exports have exactly the header's types; every validator reason has a header code
| TESTED (model ↔ header) / checked at compile time (Zig ↔ header)
| `src/interface/ffi/test/seam_test.zig`; `exportChecked` and `reasonCode` in
`src/interface/ffi/src/main.zig`. Eleven deliberate mismatches, one of each kind, were
each caught when this was written

| C ABI fail-closed rules: security NONE on every non-OK status, no answer text for
bogus verdicts (all 51 recorded bogus scenarios), unknown policy bits and meta-types
rejected, buffer sizing, callback transport, no leaks, allocation failure at every point
| TESTED
| `src/interface/ffi/test/abi_test.zig` (through the header's declarations)

| Idris2 -> C ABI -> Zig end to end
| TESTED
| `just abi-e2e` (`src/interface/E2e/Main.idr`, 9 checks over recorded responses);
`abi-seam` job in `.github/workflows/zig-ci.yml`

| Live validation against the real root
| TESTED (manually, 2026-10-13)
| `dnssec-stub` over Cloudflare DoH through an HTTP CONNECT proxy: `example.com A`
Expand All @@ -95,7 +125,8 @@ there is no machine-checked proof in this repository yet.
| Container build and test
| TESTED
| `build/container/Containerfile --target test` built on Wolfi with the
checksum-verified Zig 0.17.0 tarball; all 88 tests passed inside it
checksum-verified Zig 0.17.0 tarball; all 110 tests (C ABI tests included) passed
inside it

| `crosscheck` and `runtime` container targets
| DESIGNED
Expand Down Expand Up @@ -172,12 +203,16 @@ include::FUZZ-RESULTS.adoc[]
* `drill -S` reported success for the DNSKEY RRset of a zone whose signatures had
expired a month earlier. Not investigated further; noted for whoever reviews ldns.

== Estate note: where a proof would go
== Estate note: what is proved, and where the next proof would go

The guarantee "answer data is only reachable with secure evidence or a proven-insecure
reason" is currently enforced by the shape of a Zig tagged union and by tests. It is the
kind of invariant the estate's epistemic-types work is meant to state: a `secure` answer
as knowledge, `insecure` as justified non-knowledge, and the absence of any path from
`bogus` to an answer. A small Idris2 model of `Verdict` at the ABI seam, with that
property as a type, is a short and legitimate next step; it would move the row
"Failures are never reported as insecure" from TESTED/DESIGNED towards PROVEN.
reason" is the kind of invariant the estate's epistemic-types work is meant to state: a
`secure` answer as knowledge, `insecure` as justified absence of authentication, and no
path from `bogus` to an answer. It is now stated as a type at the ABI seam
(`src/interface/Abi/`): an Idris2 caller cannot receive answer data with a bogus or
indeterminate verdict, even if the library wrote answer text for one (the FFI
marshalling and the library's memory safety are assumed). Inside the library the same
guarantee rests on the shape of a Zig tagged union and on tests. The next step towards
PROVEN is a model of the validator's decision procedure (the chain walk and the denial
proofs) in which "secure" can only be built from verified signatures up to an anchor;
that is a larger piece of work and is not started.
Loading
Loading