Skip to content

fix(update): recover a handoff store left readable by an interrupted write - #145085

Merged
steipete merged 5 commits into
mainfrom
steipete/handoff-lease-fail-safe
Sep 11, 2026
Merged

steipete merged 5 commits into
mainfrom
steipete/handoff-lease-fail-safe

Conversation

@steipete

Copy link
Copy Markdown
Contributor

Problem

Creating the managed-handoff lease store is not atomic:

  1. open creates the file — under the caller's umask, so 0644 with the common umask 022
  2. the table is written to disk
  3. only then does the store chmod it to 0600

Every reader checks the file mode before opening the database. So a crash anywhere in steps 1-2 leaves a world-readable file, and from then on withConfigWriteLock and withGatewayServiceOperationLock refuse with managed handoff lease file is unsafe — for every install root on the host, not just the interrupted one. The store is machine-global under /tmp/openclaw with no env override, so the only recovery was deleting the file by hand. openclaw doctor cannot help: its repair path writes config and dies on the same throw.

This is the other half of #144715. That PR made an uninitialized table readable, but the mode check runs first, so the interrupted-write case it was aimed at still refused. #144715 only helps hosts running umask 077; on the macOS and Linux default it never reached the new schema probe.

Fix

Repair the invariant the store already maintains rather than refusing it. repairPrivateFileMode restores 0600 on a path we own, then revalidates. The chmod also moves ahead of the table write, shrinking the window it exists for from a disk write to one syscall.

Excess bits are defense in depth here rather than a live exposure: assertPath enforces a 0700 owned directory on every read and every write, and nlink === 1, so no other user could traverse to the inode or hold a descriptor on it whatever the file's own mode said. Group/other write bits are still refused rather than repaired — chmod cannot revoke a descriptor, and integrity is the one thing the directory guarantee would not restore. Ownership, type and link count stay unrepairable and still refuse.

Evidence

Live before/after on Linux (Docker, umask 0022), through the real installed openclaw CLI, in one image — the BEFORE arm removes only the repair call from the shipped bundle:

host: Linux aarch64   node: v24.19.0   umask: 0022
cli:  /home/appuser/.local/bin/openclaw -> /app/openclaw.mjs
pre-fix artifact: removed repairPrivateFileMode

===== BEFORE (repair removed from the shipped bundle) =====
  store: mode 644  size 0  tables=[]
  exit=1
  error: managed handoff lease file is unsafe
  store mode after: 644
  config key removed: NO

===== AFTER (shipped bundle as built) =====
  store: mode 644  size 0  tables=[]
  exit=0
  store mode after: 600
  config key removed: YES

Local:

  • src/infra/update-managed-service-handoff-store-recovery.test.ts — new, and the first coverage these checks have had. Verified it fails on the original defect: with the repair reverted, the recovery case fails with the exact production error while the refusal cases keep passing, so they guard the boundary rather than the fix.
  • 6 handoff/custody suites + src/state/local-onboarding-state.test.ts: 72 passed, 2 skipped and 13 passed.
  • node scripts/check-changed.mjs — passed.
  • Three independent Codex review rounds. Two findings accepted and fixed: POSIX mode assertions now gated to non-Windows, and previously-writable files are no longer repaired. The third round argued a retained read descriptor could observe later rows; that premise does not hold given the enforced 0700 directory and single link, so it is answered by the comment on repairPrivateFileMode and locked in by a test asserting the directory check still refuses once it stops being private.

Known gap, deliberately not closed here

On a host with umask 002 an interrupted write leaves 0664, which this change refuses rather than repairs. Those hosts stay locked out. Refusing is the right call for write bits, but it means the recovery story is still incomplete, and it lands on the two gaps I did not touch:

  • The refusal says retain diagnostics and run openclaw triage manually, but triage only ever touches the lease at its own install root; it cannot inspect or clear the row blocking a different one. The advised recovery does not exist for this failure.
  • Nothing in Doctor inspects this store, so a corrupt one is undiagnosable by the tool users reach for — and unrunnable, since Doctor writes config.

@fuller-stack-dev — same area as #140339 / #144715, so flagging rather than quietly changing it. A Doctor-owned inspect/repair for this store looks like the right home for both.

…write

Creating the lease store is not atomic: open creates the file under the caller's
umask, the table is written, and only then does the store chmod it to 0600. A
crash anywhere in that window leaves a world-readable file, and with the common
umask 022 that is every crash in the window. Every later reader checks the mode
before opening, so one interrupted first write refused config mutation and native
service operations for every install root on the host until an operator deleted
the file by hand. #144715 made the uninitialized table readable, but the mode
check runs first and still refused.

Repair the mode the store itself maintains instead of refusing it, and set it
before the table write so the window is one syscall rather than a disk write.
Ownership, type and link count are not ours to repair and still refuse.
@clawsweeper

clawsweeper Bot commented Sep 11, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

ClawSweeper review complete

ClawSweeper finished reviewing this revision. The review result is being finalized.

View the workflow run.

@openclaw-barnacle openclaw-barnacle Bot added size: S maintainer Maintainer-authored PR labels Sep 11, 2026
@clawsweeper clawsweeper Bot added P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. labels Sep 11, 2026
@clawsweeper

clawsweeper Bot commented Sep 11, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed September 11, 2026, 2:46 PM ET / 18:46 UTC (Revision 4).

ClawSweeper review

What this changes

Restores private permissions on eligible interrupted-update handoff databases so configuration and service operations can resume, and adds recovery and refusal coverage.

Merge readiness

✅ Ready for maintainer review

The repair remains necessary on current main and is supported by real CLI before/after evidence. The earlier fixture finding is resolved, and this review found no blocking defect.

Priority: P2
Reviewed head: ca54a4b815e59628ae58c05fb798d2752273fb0e

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A focused, useful repair with direct runtime evidence, meaningful regression coverage, and no remaining actionable finding.
Proof confidence 🦞 diamond lobster (5/6) Sufficient (terminal): The supplied installed Linux CLI before/after trace exercises configuration locking through the changed database owner and shows 0644-to-0600 recovery with the requested config mutation succeeding; focused tests supplement the refusal boundary.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Verified Sufficient (terminal): The supplied installed Linux CLI before/after trace exercises configuration locking through the changed database owner and shows 0644-to-0600 recovery with the requested config mutation succeeding; focused tests supplement the refusal boundary.
Evidence reviewed 8 items Current main still needs the repair: The fetched main revision checks excess permission bits before opening the database and applies chmod only after table creation; it contains no permission-recovery call.
Latest release retains the old behavior: The source at the supplied v2026.9.4 release commit also refuses excess file permissions and chmods after table creation. The proposed recovery has therefore not shipped in that release.
Bounded repair and preserved authority checks: Repair requires an owned regular file with one link and no group/other write bits. Directory validation precedes repair. Captured-identity recovery follows the separate unchanged strict path; schema, lease rows, and transaction fencing are unchanged.
Findings None None.
Security None None.

How this fits together

The managed-update handoff database records update ownership and retained custody. Configuration writes and Gateway service operations consult it before changing protected resources.

flowchart TD
  A[Configuration or service operation] --> B[Inspect handoff database]
  B --> C{Safe ownership and permissions?}
  C -->|Eligible permission drift| D[Restore private permissions]
  C -->|Unsafe file| E[Refuse operation]
  C -->|Already private| F[Check retained custody]
  D --> F
  F -->|Resource available| G[Allow operation]
  F -->|Resource retained| E
Loading

Before merge

None.

Agent review details

Security

None.

PR surface

Source +33, Tests +134. Total +167 across 3 files.

View PR surface stats
Area Files Added Removed Net
Source 1 35 2 +33
Tests 2 135 1 +134
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 3 170 3 +167

Review metrics

None.

Technical review

Best possible solution:

Keep permission recovery in the existing database owner while preserving strict refusal of writable, foreign-owned, malformed, or retained-custody state.

Do we have a high-confidence way to reproduce the issue?

Yes: current-main source rejects an owned 0644 tableless handoff store before configuration or service callbacks run; the supplied CLI transcript demonstrates that failure and recovery. This read-only review did not execute tests or application code.

Is this the best way to solve the issue?

Yes: restoring the existing permission invariant at the shared database owner repairs both callers without a second recovery implementation or a schema change.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against b3ffe8760d73.

Labels

Label justifications:

  • P2: Repairs configuration and service-operation lockout after a specific interrupted database-creation window.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🦞 diamond lobster and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (terminal): The supplied installed Linux CLI before/after trace exercises configuration locking through the changed database owner and shows 0644-to-0600 recovery with the requested config mutation succeeding; focused tests supplement the refusal boundary.
  • proof: sufficient: Contributor real behavior proof is sufficient. The supplied installed Linux CLI before/after trace exercises configuration locking through the changed database owner and shows 0644-to-0600 recovery with the requested config mutation succeeding; focused tests supplement the refusal boundary.

Evidence

What I checked:

  • Current main still needs the repair: The fetched main revision checks excess permission bits before opening the database and applies chmod only after table creation; it contains no permission-recovery call. (src/infra/update-managed-service-handoff-database.ts:118, b3ffe8760d73)
  • Latest release retains the old behavior: The source at the supplied v2026.9.4 release commit also refuses excess file permissions and chmods after table creation. The proposed recovery has therefore not shipped in that release. (src/infra/update-managed-service-handoff-database.ts:42, 3a9d69db306c)
  • Bounded repair and preserved authority checks: Repair requires an owned regular file with one link and no group/other write bits. Directory validation precedes repair. Captured-identity recovery follows the separate unchanged strict path; schema, lease rows, and transaction fencing are unchanged. (src/infra/update-managed-service-handoff-database.ts:55, ca54a4b815e5)
  • Connected regression coverage: The reviewed tests exercise configuration and service lock admission, in-place permission recovery, and refusal of writable or foreign-owned stores. The previous unsafe-mode fixture now uses 0660, resolving the earlier finding. (src/daemon/service-operation-lock.retained-custody.test.ts:423, ca54a4b815e5)
  • Real installed CLI recovery evidence: The supplied PR body records Linux aarch64, Node 24.19.0, Docker, and umask 0022. With the repair removed, a tableless 0644 store blocks the config operation; with the built repair, the CLI exits zero, removes the requested config key, and leaves mode 0600. This directly exercises the changed database owner through configuration locking. Source snapshot identity: d2e01c8c9916fe25cc4fcd4ab41276b8523d67f48bf811f8ccfeacd5f5774bc4.
  • Review continuity: The three currently introduced files are unchanged since the previous completed review revision. The historical install-test helper change is absent from the pinned introduction delta and is not attributed to this PR's current scope. (ca54a4b815e5)

Likely related people:

  • fuller-stack-dev: Suggested for follow-up; no historical authorship or introduction is verified. (role: unverified routing candidate; confidence: low)
  • unknown: The claimed source-line change could not be verified from bounded local history. (role: source history unknown; confidence: low)

Rating scale

Score Internal tier Crab rank Meaning
6/6 S 🦀 challenger crab Exceptional readiness
5/6 A 🦞 diamond lobster Very strong readiness
4/6 B 🐚 platinum hermit Good normal PR; ordinary maintainer review
3/6 C 🦐 gold shrimp Useful, but confidence is limited
2/6 D 🦪 silver shellfish Proof or implementation needs work
1/6 F 🧂 unranked krab Not merge-ready
N/A NA 🌊 off-meta tidepool Rating does not apply

Overall follows the weaker of proof and patch quality.
Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

Workflow

  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

History

Review history (3 earlier review cycles)
  • reviewed 2026-09-11T16:58:46.892Z sha d61c1be :: needs changes before merge. :: [P2] Update the existing unsafe-mode fixture to use writable permissions
  • reviewed 2026-09-11T17:44:54.799Z sha faa970e :: needs maintainer review before merge. :: none
  • reviewed 2026-09-11T18:00:47.696Z sha e72fe62 :: needs maintainer review before merge. :: none

The daemon retained-custody suite asserted that unsafe-mode storage refuses
without repairing it, using a group-readable 0640 file. That is exactly the
drift this change repairs, so point the refusal case at 0660 instead, where
a descriptor another user may already hold keeps the store unrepairable, and
assert the repaired mode admits both the config and service lock owners.
@openclaw-barnacle openclaw-barnacle Bot added the gateway Gateway runtime label Sep 11, 2026
@clawsweeper clawsweeper Bot added rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. and removed rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. labels Sep 11, 2026
install.test.ts crossed the 1000-line cap when #143905 added an authority
case, so every pull request merging main now fails lint on a file it never
touched. expectFields asserts named fields and touches none of this suite's
mocks, so it belongs in the existing sibling helper module rather than here.
@steipete

Copy link
Copy Markdown
Contributor Author

Also carries an unrelated red-main repair, because it blocks every pull request in the repo.

c06f21ddfad (#143905) added an authority case to src/cli/daemon-cli/install.test.ts, taking it from 1104 to 1126 raw lines — 1001 counted against a 1000 cap. Main's push runs do not include the lint stripe, so it only surfaces on PRs, where check-lint-core-1 now fails on a file the PR never touched. Reproduced locally on a clean merge of main, linting only that file:

src/cli/daemon-cli/install.test.ts:1126:4: error eslint(max-lines):
  File has too many lines (1001). help: Maximum allowed is 1000.

Fixed by shrinking rather than suppressing: expectFields asserts named fields and touches none of that suite's mocks, so it moved into the existing sibling install.test-helpers.ts, which is where the other shared fixtures already live. No test was removed or weakened — 42/42 still pass, and lint is clean on both files.

@openclaw-barnacle openclaw-barnacle Bot added the cli CLI command changes label Sep 11, 2026
…e-fail-safe

# Conflicts:
#	src/cli/daemon-cli/install.test.ts
@openclaw-barnacle openclaw-barnacle Bot removed the cli CLI command changes label Sep 11, 2026
@steipete
steipete merged commit b7693bf into main Sep 11, 2026
181 checks passed
@steipete
steipete deleted the steipete/handoff-lease-fail-safe branch September 11, 2026 18:55
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Sep 12, 2026
…write (openclaw#145085)

fix(update): recover a handoff store left readable by an interrupted write

Creating the lease store is not atomic: open creates the file under the caller's
umask, the table is written, and only then does the store chmod it to 0600. A
crash anywhere in that window leaves a world-readable file, and with the common
umask 022 that is every crash in the window. Every later reader checks the mode
before opening, so one interrupted first write refused config mutation and native
service operations for every install root on the host until an operator deleted
the file by hand. openclaw#144715 made the uninitialized table readable, but the mode
check runs first and still refused.

Repair the mode the store itself maintains instead of refusing it, and set it
before the table write so the window is one syscall rather than a disk write.
Excess bits are defense in depth here: assertPath enforces a 0700 owned directory
on every read and write, and a single link, so no other user could reach the inode
whatever the file's own mode said. Group and other write bits stay unrepairable,
because chmod cannot revoke a descriptor another user already holds. Ownership,
type and link count are likewise not ours to repair and still refuse.

Proven on Linux at umask 0022 through the installed CLI: before the repair a
config write exits 1 with "managed handoff lease file is unsafe" and the store
stays 0644; after, it exits 0 and the store is restored to 0600.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gateway Gateway runtime maintainer Maintainer-authored PR P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: S status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant