Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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
14 changes: 14 additions & 0 deletions scripts/openclaw-config-guard.py
Original file line number Diff line number Diff line change
Expand Up @@ -3461,6 +3461,20 @@ def _transition(
raise GuardError(exc.code, exc.path, detail) from exc
raise GuardError("transition-failed", opened.config_path, detail) from exc

if _is_mutable_dir_posture(opened, identity):
# Unlock is idempotent, mirroring the already-locked short-circuits in
# the lock branch above. A config already in the exact mutable posture
# is the unlock target: on some platforms (DGX Spark/Station, macOS) an
# in-sandbox OpenClaw reconciler re-permissions the config back to
# sandbox-owned mutable *after* a host lock returns, and a freshly
# onboarded or snapshot-restored sandbox boots mutable before the lock
# settles. Requiring the locked posture there rejected a legitimate
# `shields down` with config-not-locked even though the config already
# holds the mutable target posture. Verify the exact mutable posture
# and treat the transition as a no-op instead of failing.
pair = _snapshot_raw_pair(opened)
_verify_mutable_posture(opened, pair, identity)
return
Comment on lines +3464 to +3477

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The directory check happens before _snapshot_raw_pair(), but this tree is already sandbox-writable. If a sandbox process changes the parent or config directory during capture, _verify_mutable_files() still passes and this branch returns success without rechecking the directory. I reproduced _transition("unlock", ...) succeeding with final config-directory mode 0700. Capture the pair, then validate the full mutable posture (for example _verify_mutable_posture(opened, pair, identity)) and add a directory-drift regression.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Addressed at exact signed/Verified head 32c1b9698. The already-mutable unlock path now captures with _snapshot_raw_pair() and then calls _verify_mutable_posture(opened, pair, identity) before returning. The regression mutates the config directory to 0700 during capture and requires fail-closed config-not-mutable; the companion rejected-mode coverage also proves a 0600 file remains unchanged. The focused guard suite passes 45/45 and exact-range hooks pass. Leaving this thread open for your re-review.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Exact head update: aec2516 is now current-main synced and GitHub Verified; the feature diff is unchanged. The full mutable-posture revalidation and directory-drift regression remain in place. Exact-head guard tests pass 45/45 under the repository Python 3.11 environment, exact-range hooks pass, and the fresh WSL, macOS, CLI, CodeQL, and E2E gates are green. Leaving the thread open for your re-review.

pair = _snapshot_pair(opened)
_verify_locked_posture(opened, pair, identity, allow_blocking_flags=True)
snapshots: list[FileSnapshot] = []
Expand Down
31 changes: 20 additions & 11 deletions test/openclaw-config-guard.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,17 @@
module._pid1_effective_uid = lambda: identity.root_uid + 1
if failure == "startup-owner":
module.os.getppid = lambda: 1
if failure == "pair-race":
if failure in {"pair-race", "mutable-dir-drift"}:
original_snapshot = module._snapshot_file
raced = False
def race_pair(opened, name):
global raced
snapshot = original_snapshot(opened, name)
if name == "openclaw.json" and not raced:
raced = True
if failure == "mutable-dir-drift":
os.chmod(config_dir, 0o700)
return snapshot
updated = b'{"gateway":{"port":19001}}\n'
with open(os.path.join(config_dir, "openclaw.json"), "wb") as stream:
stream.write(updated)
Expand Down Expand Up @@ -336,6 +339,10 @@
return fs.lstatSync(filePath).mode & 0o7777;
}

function fileIdentity(filePath: string): [number, Buffer] {
return [fs.statSync(filePath).ino, fs.readFileSync(filePath)];
Comment thread
github-advanced-security[bot] marked this conversation as resolved.
Fixed
}

function setUserXattr(filePath: string, value: string): boolean {
return (
spawnSync(
Expand Down Expand Up @@ -396,10 +403,7 @@
const { root, configDir, configPath, hashPath } = fixture();
const first = runGuard("lock", configDir);
expect(first.status, JSON.stringify(first.lines)).toBe(0);
const configInode = fs.statSync(configPath).ino;
const hashInode = fs.statSync(hashPath).ino;
const configBytes = fs.readFileSync(configPath);
const hashBytes = fs.readFileSync(hashPath);
const fileIdentities = [configPath, hashPath].map(fileIdentity);
expect(mode(root)).toBe(0o1775);

const second = runGuard("lock", configDir);
Expand All @@ -408,10 +412,17 @@
expect(mode(configDir)).toBe(0o755);
expect(mode(configPath)).toBe(0o444);
expect(mode(hashPath)).toBe(0o444);
expect(fs.statSync(configPath).ino).toBe(configInode);
expect(fs.statSync(hashPath).ino).toBe(hashInode);
expect(fs.readFileSync(configPath)).toEqual(configBytes);
expect(fs.readFileSync(hashPath)).toEqual(hashBytes);
expect([configPath, hashPath].map(fileIdentity)).toEqual(fileIdentities);
});
it("unlocks idempotently when the config already holds the mutable posture (#7430)", () => {
const { configDir, configPath, hashPath } = fixture();
const fileIdentities = [configPath, hashPath].map(fileIdentity);
const r = runGuard("unlock", configDir);
expect(r.status, JSON.stringify(r.lines)).toBe(0);
expect([mode(configDir), mode(configPath), mode(hashPath)]).toEqual([0o2770, 0o660, 0o660]);
expect([configPath, hashPath].map(fileIdentity)).toEqual(fileIdentities);
const drift = runGuard("unlock", configDir, "mutable-dir-drift");
expect(drift.lines).toContainEqual(expect.objectContaining({ code: "config-not-mutable" }));
});

it("fresh-replaces both files on lock and unlock while preserving bytes, times, and xattrs", () => {
Expand Down Expand Up @@ -561,9 +572,7 @@

it("retries config and hash as one pair when a writer interleaves their capture", () => {
const { configDir, configPath, hashPath } = fixture();

const result = runGuard("lock", configDir, "pair-race");

expect(result.status).toBe(0);
const updated = Buffer.from('{"gateway":{"port":19001}}\n');
expect(fs.readFileSync(configPath)).toEqual(updated);
Expand Down
Loading