Skip to content

Rewrite nmap C→Rust (Windows) — Milestone 0: foundation - #1

Merged
kj299 merged 3 commits into
masterfrom
claude/nmap-c2rust-windows-o7bghe
Jul 9, 2026
Merged

kj299 merged 3 commits into
masterfrom
claude/nmap-c2rust-windows-o7bghe

Conversation

@kj299

@kj299 kj299 commented Jul 8, 2026

Copy link
Copy Markdown
Owner

What this is

The first milestone of a long, multi-milestone safety-first C→Rust rewrite of nmap targeting Windows, driven by the c2rust-port Porting Kit. The full plan (milestone ladder M0→M7 + a signature-DB-maintenance workstream) was agreed separately.

Prime directive: the Rust must be safer and more secure than the C, not merely equivalent. The C tree is the specification (and the differential oracle) — no C source is modified.

Milestone 0 — Foundation (this PR)

  • Vendored the kit into porting-kit/ (docs, runnable harnesses, skills) and symlinked the porting-kit-* skills into .claude/skills/ (integrity check clean; make -C porting-kit check-kit green).
  • Stood up nmap-rs/ — a Cargo workspace from the kit skeleton, encoding unsafe-isolation structurally:
    • crates/core — #[forbid(unsafe_code)]; where most ported logic will live.
    • crates/sys — the only crate permitted unsafe; every block audited (// SAFETY:). ~0 unsafe expected through the MVP.
    • crates/cli — thin; binary nmap-rs (coexists with C nmap during the port).
    • Workspace safety lints (overflow-checks, arithmetic_side_effects, cast_possible_truncation, undocumented_unsafe_blocks, missing_safety_doc); deny.toml; a NMAP_RS_TRACE env-gated phase logger scaffolded on day one.
  • Path-scoped CI (.github/workflows/nmap-rs-ci.yml) for the safety gates, and excluded nmap-rs/** + porting-kit/** from the C build.yml so neither pipeline triggers the other (kit LESSONS nmap C→Rust — M1: core::timing (RTT estimator + -T0..-T5 templates) #5). Fuzz + differential gates are wired but self-skip until Milestone 1 supplies their inputs.

Gates (all green locally)

cargo fmt --check · cargo clippy -D warnings · cargo test · unsafe-audit (0 undocumented) · make check-kit.

Notes

  • Kit finding for the retrospective: the kit skeleton's own placeholder code failed the workspace's own gates (clippy::arithmetic_side_effects on i + 1; not rustfmt-clean) — fixed here; a candidate LESSONS entry to feed upstream.
  • Target platform is x86_64-pc-windows-msvc; core builds/tests on the Linux CI runner every push regardless of target.

Next

Milestone 1 — the MVP vertical slice: unprivileged TCP connect scan + host discovery + normal/grepable/XML output, verified by same-binary differential against C nmap. Begins with porting-kit-kickoff (Phase 0 inventory + C-flaw scan + threat model) ∥ porting-kit-oracle.

🤖 Generated with Claude Code


Generated by Claude Code

claude added 3 commits July 8, 2026 23:23
Foundation for the safety-first C->Rust rewrite of nmap (Windows-targeted),
driven by the c2rust-port Porting Kit. No C source is modified; the C tree
becomes the differential oracle.

- Vendor the kit into porting-kit/ (docs, runnable harnesses, skills).
- Symlink porting-kit skills into .claude/skills/ (integrity check clean).
- Stand up nmap-rs/ Cargo workspace from the kit skeleton:
  - core (#![forbid(unsafe_code)]) / sys (only unsafe; audited) / cli (nmap-rs bin)
  - workspace safety lints (overflow-checks, arithmetic_side_effects,
    cast_possible_truncation, undocumented_unsafe_blocks, missing_safety_doc)
  - TRACE env-gated phase logger scaffolded day one (NMAP_RS_TRACE)
  - deny.toml for the supply-chain gate
- Wire path-scoped CI (nmap-rs-ci.yml) for the safety gates; exclude nmap-rs/**
  and porting-kit/** from the C build.yml so neither pipeline triggers the other
  (kit LESSONS #5).
- make check-kit green; cargo fmt/clippy/test green; unsafe-audit 0 undocumented.

Kit findings for the eventual retrospective: the kit skeleton's own placeholder
code failed the workspace's own gates (clippy arithmetic_side_effects on `i + 1`;
not rustfmt-clean) — fixed here; candidates for a LESSONS entry.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Claude-Session: https://claude.ai/code/session_01JfEzHqD7a7TKDuUs5GwZ2w
…=false

CI supply-chain gate failed: the kit's vendored deny.template.toml uses the
pre-0.18 key `wildcard-dependencies`, which current cargo-deny (0.19.9) rejects
(renamed to `wildcards`). Rewrote deny.toml to the v2 schema and set
`allow-wildcard-paths = true`.

That alone wasn't enough: cargo-deny's wildcard exemption for path deps does not
apply to *publishable* crates. These are application crates never published to
crates.io, so mark each `publish = false` — now the intra-workspace path deps
pass the `wildcards = "deny"` gate. Verified locally: `cargo deny check` →
advisories ok, bans ok, licenses ok, sources ok (exit 0).

Kit finding for the retrospective: porting-kit deny.template.toml is stale vs
cargo-deny 0.18+ (wildcard-dependencies -> wildcards) and its wildcards="deny"
default breaks a path-only workspace unless crates are publish=false.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Claude-Session: https://claude.ai/code/session_01JfEzHqD7a7TKDuUs5GwZ2w
Full C->Rust roadmap driven by the c2rust-port kit: M0 foundation (done)
through connect-scan MVP, async engine, service/OS detection, raw scanning,
signature-DB maintenance, and NSE. Status tracker reflects M0 complete / M1
current so the phase is not lost across sessions.

Co-Authored-By: Claude Fable 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01JfEzHqD7a7TKDuUs5GwZ2w
@kj299
kj299 marked this pull request as ready for review July 9, 2026 06:12
@kj299
kj299 merged commit 16f8ea1 into master Jul 9, 2026
26 checks passed
kj299 pushed a commit that referenced this pull request Jul 18, 2026
…(six gates)

The service-detection match engine — the Rust analog of ServiceProbeMatch::testMatch
/ ServiceProbe::testMatch. Given a probe's match/softmatch rules and a target
banner, decide the service. This is M3's highest-risk module: every byte matched is
attacker-chosen, so the banner is the #1 fuzz target.

The hybrid engine (the M3-1 spike's decision, now built):
- Linear default: regex::bytes (Unicode off) on raw bytes — the ~93.6% path, a
  finite automaton with a linear-time guarantee, so ReDoS is UNEXPRESSIBLE
  (strictly safer than the C's leashed PCRE2).
- Bounded-backtracking fallback: fancy-regex with an explicit backtrack_limit (the
  analog of nmap's match_limit=50000); exceeding it yields "no match", never a
  hang. It is &str-only, so a binary banner is matched through a latin-1 bijection
  (byte b <-> char U+00b) and captures are mapped back to bytes — exact because
  every backtracking pattern in the corpus is ASCII.

Result: all 12,171 shipped rules compile (100% coverage, 0 dropped) — the improved
pcre_translate (literal-[-in-class rewrite) erased the spike's ~9-pattern residual.

Degrade, not abort (DIVERGENCES.md):
- matcher-empty-match-drop: a rule whose regex can match "" is dropped with a
  warning (nmap fatal()s via PCRE2_INFO_MATCHEMPTY); would mislabel every port.
- a rule neither engine accepts is dropped with a warning (never fatal); never
  fires on the shipped DB, only hostile/custom --versiondb.

Six gates:
- ported: crates/core/src/matcher.rs (forbid(unsafe_code)), 11 unit tests incl.
  fancy-engine binary capture round-trip and first-match-wins ordering
- differential: tests/matcher_corpus.rs compiles all 12,171 real rules (100%,
  0 dropped), matches real ssh/ftp banners, total over hostile banners
- fuzzed: fuzz target matcher (compile arbitrary pattern + match arbitrary
  banner), 200k+ runs, 0 crashes; the (a+)+$ ReDoS seed does not hang
- sanitized: miri clean on all 11 unit tests
- unsafe-audited: 0 unsafe (regex/fancy-regex expose safe APIs)
- supply-chain: cargo audit + deny clean with the two new deps; corpus test pins

Version-string substitution ($1/$P/...) is the next module (core::versioninfo);
the matcher returns the raw capture groups it will consume.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Claude-Session: https://claude.ai/code/session_01JfEzHqD7a7TKDuUs5GwZ2w
kj299 pushed a commit that referenced this pull request Jul 19, 2026
…(six gates)

The service-detection match engine — the Rust analog of ServiceProbeMatch::testMatch
/ ServiceProbe::testMatch. Given a probe's match/softmatch rules and a target
banner, decide the service. This is M3's highest-risk module: every byte matched is
attacker-chosen, so the banner is the #1 fuzz target.

The hybrid engine (the M3-1 spike's decision, now built):
- Linear default: regex::bytes (Unicode off) on raw bytes — the ~93.6% path, a
  finite automaton with a linear-time guarantee, so ReDoS is UNEXPRESSIBLE
  (strictly safer than the C's leashed PCRE2).
- Bounded-backtracking fallback: fancy-regex with an explicit backtrack_limit (the
  analog of nmap's match_limit=50000); exceeding it yields "no match", never a
  hang. It is &str-only, so a binary banner is matched through a latin-1 bijection
  (byte b <-> char U+00b) and captures are mapped back to bytes — exact because
  every backtracking pattern in the corpus is ASCII.

Result: all 12,171 shipped rules compile (100% coverage, 0 dropped) — the improved
pcre_translate (literal-[-in-class rewrite) erased the spike's ~9-pattern residual.

Degrade, not abort (DIVERGENCES.md):
- matcher-empty-match-drop: a rule whose regex can match "" is dropped with a
  warning (nmap fatal()s via PCRE2_INFO_MATCHEMPTY); would mislabel every port.
- a rule neither engine accepts is dropped with a warning (never fatal); never
  fires on the shipped DB, only hostile/custom --versiondb.

Six gates:
- ported: crates/core/src/matcher.rs (forbid(unsafe_code)), 11 unit tests incl.
  fancy-engine binary capture round-trip and first-match-wins ordering
- differential: tests/matcher_corpus.rs compiles all 12,171 real rules (100%,
  0 dropped), matches real ssh/ftp banners, total over hostile banners
- fuzzed: fuzz target matcher (compile arbitrary pattern + match arbitrary
  banner), 200k+ runs, 0 crashes; the (a+)+$ ReDoS seed does not hang
- sanitized: miri clean on all 11 unit tests
- unsafe-audited: 0 unsafe (regex/fancy-regex expose safe APIs)
- supply-chain: cargo audit + deny clean with the two new deps; corpus test pins

Version-string substitution ($1/$P/...) is the next module (core::versioninfo);
the matcher returns the raw capture groups it will consume.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Claude-Session: https://claude.ai/code/session_01JfEzHqD7a7TKDuUs5GwZ2w
kj299 added a commit that referenced this pull request Jul 19, 2026
…23)

* M3 module 3/6 — core::matcher: hybrid service-detection regex engine (six gates)

The service-detection match engine — the Rust analog of ServiceProbeMatch::testMatch
/ ServiceProbe::testMatch. Given a probe's match/softmatch rules and a target
banner, decide the service. This is M3's highest-risk module: every byte matched is
attacker-chosen, so the banner is the #1 fuzz target.

The hybrid engine (the M3-1 spike's decision, now built):
- Linear default: regex::bytes (Unicode off) on raw bytes — the ~93.6% path, a
  finite automaton with a linear-time guarantee, so ReDoS is UNEXPRESSIBLE
  (strictly safer than the C's leashed PCRE2).
- Bounded-backtracking fallback: fancy-regex with an explicit backtrack_limit (the
  analog of nmap's match_limit=50000); exceeding it yields "no match", never a
  hang. It is &str-only, so a binary banner is matched through a latin-1 bijection
  (byte b <-> char U+00b) and captures are mapped back to bytes — exact because
  every backtracking pattern in the corpus is ASCII.

Result: all 12,171 shipped rules compile (100% coverage, 0 dropped) — the improved
pcre_translate (literal-[-in-class rewrite) erased the spike's ~9-pattern residual.

Degrade, not abort (DIVERGENCES.md):
- matcher-empty-match-drop: a rule whose regex can match "" is dropped with a
  warning (nmap fatal()s via PCRE2_INFO_MATCHEMPTY); would mislabel every port.
- a rule neither engine accepts is dropped with a warning (never fatal); never
  fires on the shipped DB, only hostile/custom --versiondb.

Six gates:
- ported: crates/core/src/matcher.rs (forbid(unsafe_code)), 11 unit tests incl.
  fancy-engine binary capture round-trip and first-match-wins ordering
- differential: tests/matcher_corpus.rs compiles all 12,171 real rules (100%,
  0 dropped), matches real ssh/ftp banners, total over hostile banners
- fuzzed: fuzz target matcher (compile arbitrary pattern + match arbitrary
  banner), 200k+ runs, 0 crashes; the (a+)+$ ReDoS seed does not hang
- sanitized: miri clean on all 11 unit tests
- unsafe-audited: 0 unsafe (regex/fancy-regex expose safe APIs)
- supply-chain: cargo audit + deny clean with the two new deps; corpus test pins

Version-string substitution ($1/$P/...) is the next module (core::versioninfo);
the matcher returns the raw capture groups it will consume.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Claude-Session: https://claude.ai/code/session_01JfEzHqD7a7TKDuUs5GwZ2w

* matcher: decode the latin-1 banner once per probe, not once per rule

Optimization (behavior-identical). The backtracking engine is &str-only, so a
binary banner is decoded to a latin-1 String before matching. The previous code
rebuilt that String inside every backtracking rule's captures/is_match; a probe
with many backtracking rules (the NULL probe has ~120) therefore re-decoded the
same banner once per rule.

CompiledProbe::test / matches now build the latin-1 view at most once per call
(lazily, only if a backtracking rule is reached) and share it across rules via
CompiledRule::captures_with / is_match_with. Linear rules never touch it.

Measured: ~10% faster on the worst case (8 KB non-matching banner walked against
the whole NULL probe: 12955 -> 11714 us/call); the win scales with banner size and
is never negative. No semantic change — the 12,171-rule corpus still compiles 100%,
all 11 unit tests pass, miri clean, fuzz 127k+ runs 0 crashes.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Claude-Session: https://claude.ai/code/session_01JfEzHqD7a7TKDuUs5GwZ2w

---------

Co-authored-by: Claude <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants