Skip to content

kyguard-broker: keys, journal, pairing, daemon (§4.1) - #9

Merged
Yoshiofthewire merged 13 commits into
mainfrom
kyguard/broker-daemon
Nov 25, 2026
Merged

Yoshiofthewire merged 13 commits into
mainfrom
kyguard/broker-daemon

Conversation

@Yoshiofthewire

@Yoshiofthewire Yoshiofthewire commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

In one line: kyguard-broker exists: pair creates the broker's keys on the host and pairs it, run holds the control channel; a hardened systemd unit ships with it.

What changed

  • kyguard-broker pair --token T --bootstrap-fingerprint HEX (or --bootstrap-bundle FILE) generates the TLS, hybrid signing and X-Wing keys locally into one 0600 key file, pairs over the pinned CA and hostname, verifies the bundle against the operator's fingerprint, and prints broker_id, sig_key_id, enc_key_id and cert_sha256 for the Custodian check (§4.1 steps 2–4, §3.2 rule 4).
  • The pairing lands in a bbolt journal (0600, exclusive file lock; a second process is refused). A key file left by a failed attempt is reused, never replaced.
  • kyguard-broker run sets RLIMIT_CORE=0 first, then holds the channel with backoff (1 s → 1 min). Nudges only wake the scheduler; they carry no content.
  • The broker calls setrlimit only when RLIMIT_CORE is not already zero, so it starts under the unit's syscall filter (Guide finding).
  • deploy/broker/kyguard-broker.service: LimitCORE=0, UMask=0077, ProtectSystem=strict, no capabilities, @system-service syscall filter.
  • TestNothingInTheServerDecrypts gains one exemption: internal/broker/keys.go openKeys, the broker rebuilding its own X-Wing key.

Review this

Safe to skip: cmd/broker/main.go flag wiring, docs.

Checked: full suite with -race and again with GODEBUG=fips140=on, on SQLite (k83) and PostgreSQL 17 (k83), exit 0 both, on this head's tree; go vet, gofmt, go mod tidy. Mutations: removing the openKeys exemption fails TestNothingInTheServerDecrypts; calling setrlimit with the limit already zero fails TestDisableCoreDumpsAcceptsAZeroLimit. Not checked: the systemd unit on a real host (it is not exercised in CI); the journal on non-Linux hosts.

Decided for you: one key file with four 32-byte seeds rather than one file per key; the pairing token is passed on the command line as the spec writes it (--token), which shows it in process listings for its 10-minute, one-time life.

You need to: nothing.

§13 checklist: Phase 1A "kyguard-broker: systemd unit, key files, RLIMIT_CORE=0, journal, pairing, … control channel" (broker side). Stacked on #8.

Detail

Tests (internal/broker/broker_test.go, cmd/broker/rlimit_test.go): pair prints every identity value and writes both files 0600; pairing twice is refused; a wrong bundle file fails and leaves the key file for the retry; exactly one of the two fingerprint flags; run connects once a Custodian-signed pool manifest lists the broker, takes a nudge, and holds the journal lock against a second process; a world-readable key file is refused; RLIMIT_CORE is 0 soft and hard.

Makefile builds kyguard-broker for Linux; .gitignore ignores the binary.

Review fixes (25 Nov, Guide "Startup failure")

  • Checked under the real unit with systemd 262 (systemd-run --user with every [Service] sandbox line of deploy/broker/kyguard-broker.service, Seccomp: 2 confirmed): the finding does not reproduce. Go's Getrlimit/Setrlimit use prlimit64, which systemd lists in @default, not @resources (@resources has setrlimit, getrlimit, ugetrlimit); systemd-analyze syscall-filter agrees, as does systemd's seccomp-util.c from v235 to v255. The broker passes the rlimit step and stops later on the missing journal, both with this PR's earlier pre-check and without it; the unit is unchanged.
  • RLIMIT_CORE=0 is set unconditionally again, as §4.1 says, and the mocked test is replaced by TestDisableCoreDumpsUnderTheUnit, which runs the startup step under the unit's real properties through systemd-run --user (it skips where there is no user manager: CI containers, K hosts) (f63ebc5, rebased).
Property Mutation Failing test
The broker starts under its unit's syscall filter add prlimit64 to the unit's deny line TestDisableCoreDumpsUnderTheUnit (under the unit: exit status 255; the service dies status=31/SYS)

Not checked: the system manager with User=kyguard-broker and /var/lib (needs root); the test's skip path in CI.

Fix found in round 3 (25 Nov): identity certificates outlived the journal mapping

  • Journal.Identity parsed both certificates straight from bbolt's value bytes, and x509.ParseCertificate keeps its input. Run holds the identity across reconnects. Once the journal grew and bbolt remapped it, the next dial read unmapped memory, and the daemon crashed (SIGSEGV, seen while testing Broker-local rotation scheduler (§3.4, §4.4) #12's reconnect fix). Both certificates are now cloned (b57aba2).
Property Mutation Failing test
The identity outlives the journal mapping parse b.Get(keyCert) without cloning TestIdentityOutlivesTheJournalMapping (SIGSEGV)

Checks (stack tip 96c38fd): full suite -race and GODEBUG=fips140=on, SQLite (k83, exit 0) and PostgreSQL 17 (see #16); Docker connector suites -race on Major under kslot (30 tests, 0 skipped, exit 0); go vet and the broker, store and brokerproto tests at every merged branch head; gofmt.

Round 4 (25 Nov): main merged up

Checks: at #8 ec97af5 the full suite with -race and GODEBUG=fips140=on passed on SQLite (Major under kslot, exit 0) and on PostgreSQL 17 (Major under kslot with its own postgres:17-alpine container, exit 0). At the top of the stack (#16, same tree as a350929) PostgreSQL 17 passed with -race + FIPS on k82 (exit 0). gofmt and go vet (also -tags docker) are clean at every branch head. The mutation proofs ran on throwaway copies.

Round 4b (25 Nov)

Checks (round 4b): at #8 f212849 and at #16 bdb9adf, the full suite with -race and GODEBUG=fips140=on passed on SQLite (Major under kslot; every K host was busy) and on PostgreSQL 17 (Major under kslot, own postgres:17-alpine container), all exit 0. #16's last head 5ef228b changes only PutTx and its test since bdb9adf: internal/store and internal/api -race passed on SQLite (k82), and the PutTx test passed 3 × 20 rounds on PostgreSQL 17. gofmt and go vet (also -tags docker) are clean at every branch head. The mutations ran on throwaway copies.

🤖 Generated with Claude Code

@Yoshiofthewire

Yoshiofthewire commented Nov 25, 2026 •

Copy link
Copy Markdown
Member Author

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪

🧪 PR contains tests

⚡ No major issues detected


Advisory only: this comment never gates the pull request.
gpt-6.1-sol using pr-agent on behalf of Yoshi

@Yoshiofthewire

Yoshiofthewire commented Nov 25, 2026 •

Copy link
Copy Markdown
Member Author

Security review - cleared

Detail withheld: this repository is public, and posting an unfixed finding here would be public disclosure. The full report was posted to the hand-off board at https://myslop.urlxl.us/f/pr-reviewer-busnes-app-kyguard-server-9, where it is readable by the operator and expires seven days after the last post.


gpt-6.1-sol using security-audit on behalf of Yoshi

@Yoshiofthewire

Yoshiofthewire commented Nov 25, 2026 •

Copy link
Copy Markdown
Member Author

Autonomous review - 1/1 personas cleared

  • security: cleared

Merging remains a human decision; this bot never approves.


gpt-6.1-sol using security-audit on behalf of Yoshi

Yoshiofthewire added a commit that referenced this pull request Nov 25, 2026
The systemd unit sets LimitCORE=0 and filters out @resources, so the
broker's own setrlimit(RLIMIT_CORE) call failed and the unit restarted
forever (PR Reviewer Guide on #9). The broker now checks the limit first
and calls setrlimit only when it is not already 0, soft and hard.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Yoshiofthewire added a commit that referenced this pull request Nov 25, 2026
The systemd unit sets LimitCORE=0 and filters out @resources, so the
broker's own setrlimit(RLIMIT_CORE) call failed and the unit restarted
forever (PR Reviewer Guide on #9). The broker now checks the limit first
and calls setrlimit only when it is not already 0, soft and hard.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Yoshiofthewire added a commit that referenced this pull request Nov 25, 2026
The systemd unit sets LimitCORE=0 and filters out @resources, so the
broker's own setrlimit(RLIMIT_CORE) call failed and the unit restarted
forever (PR Reviewer Guide on #9). The broker now checks the limit first
and calls setrlimit only when it is not already 0, soft and hard.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Yoshiofthewire

Copy link
Copy Markdown
Member Author

PR Reviewer Guide Startup failure: fixed in f72fcb8. disableCoreDumps reads RLIMIT_CORE first and calls setrlimit only when it is not already 0/0, so under the unit (LimitCORE=0, @resources filtered) the broker starts. Test TestDisableCoreDumpsAcceptsAZeroLimit fails if setrlimit is called with the limit already zero. Not checked: a boot under real systemd.

— rowan (Claude Opus 5.5)

Yoshiofthewire and others added 4 commits November 25, 2026 07:24
kyguard-broker pair generates the TLS, hybrid signing and X-Wing keys on
the host into one 0600 key file, pairs against the pinned CA hash and
listener hostname, verifies the bundle against the operator's
fingerprint (or bundle file) and records the pairing in a bbolt journal
held under its exclusive lock. It prints broker_id, sig_key_id,
enc_key_id and the certificate hash for the Custodian check.

kyguard-broker run sets RLIMIT_CORE=0 and holds the control channel,
reconnecting with backoff; nudges only wake the scheduler. A hardened
systemd unit ships in deploy/broker.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The systemd unit sets LimitCORE=0 and filters out @resources, so the
broker's own setrlimit(RLIMIT_CORE) call failed and the unit restarted
forever (PR Reviewer Guide on #9). The broker now checks the limit first
and calls setrlimit only when it is not already 0, soft and hard.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
On Linux Go's Getrlimit and Setrlimit both issue prlimit64, which
systemd puts in @default (v235 through v262); `~@resources` denies only
the setrlimit syscall. Run under the unit's [Service] sandbox through
systemd-run on systemd 262, the broker sets the limit and goes on to open
its journal, with or without the getrlimit pre-check of f72fcb8. That
pre-check guarded against a failure that does not happen, so it goes,
and the mocked test with it: the broker sets RLIMIT_CORE=0 at every start
as §4.1 says.

TestDisableCoreDumpsUnderTheUnit runs the startup step under the shipped
unit's properties via systemd-run --user (skipped without a user
manager). Denying prlimit64 in the unit makes it fail with SIGSYS.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Yoshiofthewire

Copy link
Copy Markdown
Member Author

Note

🤖 Claude Opus 5.5 responding on behalf of Yoshiofthewire

PR Reviewer Guide, "Startup failure": this does not reproduce under the real unit. Go's Getrlimit/Setrlimit call prlimit64, and systemd puts prlimit64 in @default, not @resources; systemd-analyze syscall-filter @resources lists only setrlimit, getrlimit and ugetrlimit. Running the broker under every [Service] sandbox line with systemd 262 gets past the rlimit step. In ea0fd8f the mocked test is replaced by TestDisableCoreDumpsUnderTheUnit, which runs the startup step under the unit's real filter and fails if prlimit64 is denied. The unit itself is unchanged.

Yoshiofthewire and others added 5 commits November 25, 2026 07:50
systemd 255's user manager (GitHub's ubuntu runners) cannot drop the
capability bounding set without CAP_SETPCAP, so the unit test exited 218
in CI. Its process holds no capabilities, and the test checks the
syscall filter, so the property is left out like User= and Group=.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
90d3433 didn't help: GitHub's runner still exits 218 (EXIT_CAPABILITIES)
because the unit's Protect*/PrivateDevices settings also drop capabilities,
which that user manager can't do. 218 is a systemd setup failure before the
broker runs, so the test now skips on it and keeps the whole unit,
CapabilityBoundingSet= included. On Major it still runs, and denying
prlimit64 in the unit still fails it (exit 255).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
x509.ParseCertificate keeps its input, and bbolt's value bytes are only
valid inside the transaction. Run holds the identity across reconnects,
so once the journal grew and bbolt remapped it, the next dial read freed
memory and the daemon crashed. Identity now clones both certificates.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Yoshiofthewire
Yoshiofthewire changed the base branch from kyguard/broker-pairing to main November 25, 2026 22:06
@Yoshiofthewire

Copy link
Copy Markdown
Member Author

Merger (saltmarsh-merge): closing and reopening right away so CodeQL runs now that the base is main. The branch is unchanged.

@Yoshiofthewire
Yoshiofthewire merged commit d9e4833 into main Nov 25, 2026
27 checks passed
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.

1 participant