Skip to content

Commit b77671c

Browse files
authored
feat(mac): harden Peekaboo elevation cutovers (#124564)
* feat(mac): harden elevation cutovers * fix(mac): make elevation recovery transactional * fix(mac): revalidate elevation owner shutdown * fix(mac): quiesce elevation recovery owners * fix(mac): close elevation artifact and recovery races * fix(mac): use signed exclusive app renames * fix(mac): defer elevation hangup signals * fix(mac): restore receipts during elevation rollback * fix(mac): bind elevation receipts to both architectures * fix(mac): close elevation attestation races * fix(mac): bind elevation proof to final objects * fix(mac): keep elevation rollback independently trusted * fix(mac): validate every rollback generation * fix(mac): authenticate legacy elevation recovery * fix(mac): attest recursive Peekaboo sources * fix(mac): recover when elevation app is unavailable * fix(mac): preserve raced recovery custody * test(mac): preserve missing-tool coverage * test(mac): register recursive gitlink fixture * fix(mac): authenticate all elevation custody * fix(mac): bind elevation build and recovery inputs * fix(mac): persist elevation recovery transactions * fix(mac): durably publish elevation custody * fix(mac): complete elevation transaction durability
1 parent 87a0493 commit b77671c

9 files changed

Lines changed: 7803 additions & 253 deletions

File tree

‎apps/macos/README.md‎

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,29 +70,61 @@ operator command: it requires the OpenClaw Foundation signing identity and
7070
notarization credentials, and its archive is not a general-download artifact.
7171

7272
```bash
73-
scripts/mac-elevation-host.sh package
73+
scripts/mac-elevation-host.sh package \
74+
--peekaboo-source-commit <full-peekaboo-sha>
7475
cd dist/elevation-host
75-
shasum -a 256 -c "OpenClaw-<full-source-sha>-stable.zip.sha256"
76-
shasum -a 256 -c "OpenClaw-<full-source-sha>-stable-installer.sh.sha256"
77-
./OpenClaw-<full-source-sha>-stable-installer.sh install \
78-
--archive "OpenClaw-<full-source-sha>-stable.zip"
79-
./OpenClaw-<full-source-sha>-stable-installer.sh status
76+
export PREFIX="OpenClaw-<full-openclaw-sha>-Peekaboo-<full-peekaboo-sha>-stable"
77+
export INSTALLER_SHA256="<authenticated-installer-sha256>"
78+
export RECEIPT_SHA256="<authenticated-receipt-sha256>"
79+
[[ "$(shasum -a 256 "$PREFIX-installer.sh" | awk '{print $1}')" == "$INSTALLER_SHA256" ]] || exit 1
80+
shasum -a 256 -c "$PREFIX.zip.sha256"
81+
shasum -a 256 -c "$PREFIX-installer.sh.sha256"
82+
./"$PREFIX-installer.sh" verify \
83+
--archive "$PREFIX.zip" \
84+
--receipt "$PREFIX.json" \
85+
--receipt-sha256 "$RECEIPT_SHA256"
86+
./"$PREFIX-installer.sh" migration-plan \
87+
--migrate-launch-agent "$HOME/Library/LaunchAgents/ai.openclaw.node.plist"
88+
./"$PREFIX-installer.sh" install \
89+
--archive "$PREFIX.zip" \
90+
--receipt "$PREFIX.json" \
91+
--receipt-sha256 "$RECEIPT_SHA256" \
92+
--migrate-launch-agent "$HOME/Library/LaunchAgents/ai.openclaw.node.plist"
93+
./"$PREFIX-installer.sh" status --state-dir "<existing-state-dir>"
8094
```
8195

8296
The elevation package is ZIP-only, notarized and stapled, contains exactly
8397
`OpenClaw.app`, omits Apple Events entitlements, records an immutable receipt,
8498
and verifies a freshly extracted copy. The same source-addressed artifact set
85-
includes an executable installer copied from that exact Git commit plus separate
86-
archive and installer checksum files, so a target Mac does not need a source
87-
checkout. Transfer the complete set and verify both checksums before running the
88-
installer. Installation owns the separate
99+
includes a portable installer copied from that exact Git commit plus separate
100+
archive and installer checksum files. Transfer the archive, receipt, portable
101+
installer, and both checksums; the target Mac does not need a source checkout.
102+
The release operator must deliver the receipt SHA-256 through the authenticated
103+
handoff alongside the separately authenticated installer digest. `verify` uses
104+
that receipt digest to select the approved archive and then checks its signer,
105+
entitlements, architectures, and both source revisions. The portable installer
106+
is not covered by the app's code signature, so this explicit two-digest operator
107+
handoff remains part of the internal workflow's trust boundary.
108+
109+
Installation requires an existing app-readable remote Gateway config and a
110+
paired macOS node identity in the selected state directory. Use
111+
`migration-plan` before changing a CLI-managed node LaunchAgent. For a currently
112+
running background app with no LaunchAgent, use the explicit
113+
`--adopt-running-app` plan/install option instead. The installer copies no token
114+
or password: it preserves only the state and config ownership paths, then
115+
requires the same node identity to reconnect as `openclaw-macos/node` with the
116+
new app version and computer-use capabilities before committing. Installation owns the separate
89117
`ai.openclaw.mac.elevation-host` launchd job with `RunAtLoad` and `KeepAlive`.
90118
It refuses to replace or race the ordinary `ai.openclaw.mac` Launch at login
91119
job. `recover` restores the recorded prior bundle after a failed cutover;
92120
`uninstall` removes only the elevation job and preserves the app, state,
93121
Keychain, TCC, and recovery receipt. Installation exits successfully once the
94-
launchd-owned process is Bridge-ready; missing TCC remains a degraded `status`
95-
result until the required grants are present.
122+
launchd-owned process is both Bridge-ready and reconnected to the Gateway as the
123+
expected computer-use node. Missing TCC remains a degraded `status` result until
124+
the required grants are present. Managed upgrades use generation-unique plist and
125+
receipt backups. Recovery preserves the replaced app in a unique evidence directory
126+
and restores the prior install receipt, so `status` and a same-artifact reinstall
127+
remain valid after rollback.
96128

97129
## Signing behavior
98130

‎apps/macos/Sources/OpenClaw/AppLaunchPresentationPolicy.swift‎

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,145 @@
1+
import Darwin
12
import Foundation
23

4+
enum ElevationExclusiveRename {
5+
static let argument = "--elevation-rename-exclusive"
6+
7+
static func runIfRequested(arguments: [String] = CommandLine.arguments) -> Int32? {
8+
guard arguments.dropFirst().first == self.argument else { return nil }
9+
guard arguments.count == 4 else {
10+
fputs("OpenClaw elevation rename requires absolute source and destination paths\n", stderr)
11+
return 2
12+
}
13+
let source = arguments[2]
14+
let destination = arguments[3]
15+
guard source.hasPrefix("/"), destination.hasPrefix("/") else {
16+
fputs("OpenClaw elevation rename paths must be absolute\n", stderr)
17+
return 2
18+
}
19+
20+
let result = source.withCString { sourcePath in
21+
destination.withCString { destinationPath in
22+
renamex_np(sourcePath, destinationPath, UInt32(RENAME_EXCL))
23+
}
24+
}
25+
guard result == 0 else {
26+
let code = errno
27+
fputs("OpenClaw elevation rename failed: \(String(cString: strerror(code)))\n", stderr)
28+
return 1
29+
}
30+
return 0
31+
}
32+
}
33+
34+
enum ElevationFilesystemSync {
35+
static let fileArgument = "--elevation-sync-file"
36+
static let directoryArgument = "--elevation-sync-directory"
37+
static let treeArgument = "--elevation-sync-tree"
38+
39+
static func runIfRequested(arguments: [String] = CommandLine.arguments) -> Int32? {
40+
guard let argument = arguments.dropFirst().first,
41+
argument == self.fileArgument || argument == self.directoryArgument || argument == self.treeArgument
42+
else { return nil }
43+
guard arguments.count == 3 else {
44+
fputs("OpenClaw elevation sync requires one absolute path\n", stderr)
45+
return 2
46+
}
47+
let path = arguments[2]
48+
guard path.hasPrefix("/") else {
49+
fputs("OpenClaw elevation sync path must be absolute\n", stderr)
50+
return 2
51+
}
52+
if argument == self.treeArgument {
53+
return self.syncTree(path) ? 0 : 1
54+
}
55+
let expectsDirectory = argument == self.directoryArgument
56+
return self.syncPath(path, expectsDirectory: expectsDirectory) ? 0 : 1
57+
}
58+
59+
private static func syncPath(_ path: String, expectsDirectory: Bool) -> Bool {
60+
let descriptor = open(path, O_RDONLY | O_NOFOLLOW | (expectsDirectory ? O_DIRECTORY : 0))
61+
guard descriptor >= 0 else {
62+
fputs("OpenClaw elevation sync could not open path\n", stderr)
63+
return false
64+
}
65+
defer { close(descriptor) }
66+
var metadata = stat()
67+
guard fstat(descriptor, &metadata) == 0,
68+
expectsDirectory ? (metadata.st_mode & S_IFMT) == S_IFDIR : (metadata.st_mode & S_IFMT) == S_IFREG
69+
else {
70+
fputs("OpenClaw elevation sync path has an unsupported type\n", stderr)
71+
return false
72+
}
73+
if expectsDirectory {
74+
guard fsync(descriptor) == 0 else {
75+
fputs("OpenClaw elevation directory sync failed\n", stderr)
76+
return false
77+
}
78+
return true
79+
}
80+
guard fcntl(descriptor, F_FULLFSYNC) == 0 || fsync(descriptor) == 0 else {
81+
fputs("OpenClaw elevation file sync failed\n", stderr)
82+
return false
83+
}
84+
let parent = URL(fileURLWithPath: path).deletingLastPathComponent().path
85+
let parentDescriptor = open(parent, O_RDONLY | O_NOFOLLOW | O_DIRECTORY)
86+
guard parentDescriptor >= 0 else {
87+
fputs("OpenClaw elevation sync could not open parent directory\n", stderr)
88+
return false
89+
}
90+
defer { close(parentDescriptor) }
91+
guard fsync(parentDescriptor) == 0 else {
92+
fputs("OpenClaw elevation parent directory sync failed\n", stderr)
93+
return false
94+
}
95+
return true
96+
}
97+
98+
private static func syncTree(_ path: String) -> Bool {
99+
let root = URL(fileURLWithPath: path, isDirectory: true)
100+
let keys: [URLResourceKey] = [.isDirectoryKey, .isRegularFileKey, .isSymbolicLinkKey]
101+
var enumerationFailed = false
102+
guard let enumerator = FileManager.default.enumerator(
103+
at: root,
104+
includingPropertiesForKeys: keys,
105+
options: [],
106+
errorHandler: { _, _ in
107+
enumerationFailed = true
108+
return false
109+
})
110+
else {
111+
fputs("OpenClaw elevation sync could not enumerate tree\n", stderr)
112+
return false
113+
}
114+
var directories = [root]
115+
for case let url as URL in enumerator {
116+
guard let values = try? url.resourceValues(forKeys: Set(keys)) else {
117+
fputs("OpenClaw elevation sync could not inspect tree entry\n", stderr)
118+
return false
119+
}
120+
if values.isSymbolicLink == true {
121+
continue
122+
}
123+
if values.isDirectory == true {
124+
directories.append(url)
125+
} else if values.isRegularFile == true {
126+
guard self.syncPath(url.path, expectsDirectory: false) else { return false }
127+
} else {
128+
fputs("OpenClaw elevation sync tree contains an unsupported entry\n", stderr)
129+
return false
130+
}
131+
}
132+
guard !enumerationFailed else {
133+
fputs("OpenClaw elevation sync could not enumerate complete tree\n", stderr)
134+
return false
135+
}
136+
for directory in directories.reversed() {
137+
guard self.syncPath(directory.path, expectsDirectory: true) else { return false }
138+
}
139+
return true
140+
}
141+
}
142+
3143
struct AppLaunchRuntimePlan: Equatable {
4144
enum Mode: Equatable {
5145
case interactive

‎apps/macos/Sources/OpenClaw/MenuBar.swift‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,31 @@ import OSLog
99
import Security
1010
import SwiftUI
1111

12+
/// Routes private maintenance commands before SwiftUI constructs or activates the application.
1213
@main
14+
enum OpenClawProcessMain {
15+
static func main() {
16+
if let status = OpenClawProcessEntrypoint.run(arguments: CommandLine.arguments, launchApplication: {
17+
OpenClawApp.main()
18+
}) {
19+
Darwin.exit(status)
20+
}
21+
}
22+
}
23+
24+
enum OpenClawProcessEntrypoint {
25+
static func run(arguments: [String], launchApplication: () -> Void) -> Int32? {
26+
if let status = ElevationExclusiveRename.runIfRequested(arguments: arguments) {
27+
return status
28+
}
29+
if let status = ElevationFilesystemSync.runIfRequested(arguments: arguments) {
30+
return status
31+
}
32+
launchApplication()
33+
return nil
34+
}
35+
}
36+
1337
struct OpenClawApp: App {
1438
@NSApplicationDelegateAdaptor(AppDelegate.self) private var delegate
1539
@Environment(\.openWindow) private var openWindow

‎apps/macos/Tests/OpenClawIPCTests/AppLaunchPresentationPolicyTests.swift‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,76 @@ import Testing
44
@testable import OpenClaw
55

66
struct AppLaunchRuntimePlanTests {
7+
@Test func `elevation rename is exclusive and source preserving on conflict`() throws {
8+
let root = FileManager.default.temporaryDirectory
9+
.appendingPathComponent("openclaw-elevation-rename-\(UUID().uuidString)", isDirectory: true)
10+
defer { try? FileManager.default.removeItem(at: root) }
11+
try FileManager.default.createDirectory(at: root, withIntermediateDirectories: true)
12+
13+
let source = root.appendingPathComponent("source", isDirectory: true)
14+
let destination = root.appendingPathComponent("destination", isDirectory: true)
15+
try FileManager.default.createDirectory(at: source, withIntermediateDirectories: false)
16+
var applicationConstructed = false
17+
let moved = try #require(OpenClawProcessEntrypoint.run(
18+
arguments: ["OpenClaw", ElevationExclusiveRename.argument, source.path, destination.path],
19+
launchApplication: { applicationConstructed = true }))
20+
#expect(moved == 0)
21+
#expect(!applicationConstructed)
22+
#expect(!FileManager.default.fileExists(atPath: source.path))
23+
#expect(FileManager.default.fileExists(atPath: destination.path))
24+
25+
let conflictingSource = root.appendingPathComponent("conflicting-source", isDirectory: true)
26+
try FileManager.default.createDirectory(at: conflictingSource, withIntermediateDirectories: false)
27+
let rejected = try #require(OpenClawProcessEntrypoint.run(
28+
arguments: ["OpenClaw", ElevationExclusiveRename.argument, conflictingSource.path, destination.path],
29+
launchApplication: { applicationConstructed = true }))
30+
#expect(rejected != 0)
31+
#expect(!applicationConstructed)
32+
#expect(FileManager.default.fileExists(atPath: conflictingSource.path))
33+
#expect(FileManager.default.fileExists(atPath: destination.path))
34+
}
35+
36+
@Test func `elevation filesystem sync is native and rejects symlinks`() throws {
37+
let root = FileManager.default.temporaryDirectory
38+
.appendingPathComponent("openclaw-elevation-sync-\(UUID().uuidString)", isDirectory: true)
39+
defer { try? FileManager.default.removeItem(at: root) }
40+
try FileManager.default.createDirectory(at: root, withIntermediateDirectories: true)
41+
let receipt = root.appendingPathComponent("receipt.json")
42+
try Data("{}\n".utf8).write(to: receipt)
43+
var applicationConstructed = false
44+
let fileStatus = try #require(OpenClawProcessEntrypoint.run(
45+
arguments: ["OpenClaw", ElevationFilesystemSync.fileArgument, receipt.path],
46+
launchApplication: { applicationConstructed = true }))
47+
#expect(fileStatus == 0)
48+
let directoryStatus = try #require(OpenClawProcessEntrypoint.run(
49+
arguments: ["OpenClaw", ElevationFilesystemSync.directoryArgument, root.path],
50+
launchApplication: { applicationConstructed = true }))
51+
#expect(directoryStatus == 0)
52+
let nested = root.appendingPathComponent("nested", isDirectory: true)
53+
try FileManager.default.createDirectory(at: nested, withIntermediateDirectories: false)
54+
try Data("payload\n".utf8).write(to: nested.appendingPathComponent("payload.txt"))
55+
let treeStatus = try #require(OpenClawProcessEntrypoint.run(
56+
arguments: ["OpenClaw", ElevationFilesystemSync.treeArgument, root.path],
57+
launchApplication: { applicationConstructed = true }))
58+
#expect(treeStatus == 0)
59+
let unreadable = root.appendingPathComponent("unreadable", isDirectory: true)
60+
try FileManager.default.createDirectory(at: unreadable, withIntermediateDirectories: false)
61+
try Data("hidden\n".utf8).write(to: unreadable.appendingPathComponent("hidden.txt"))
62+
try FileManager.default.setAttributes([.posixPermissions: 0], ofItemAtPath: unreadable.path)
63+
let incompleteTreeStatus = try #require(OpenClawProcessEntrypoint.run(
64+
arguments: ["OpenClaw", ElevationFilesystemSync.treeArgument, root.path],
65+
launchApplication: { applicationConstructed = true }))
66+
try FileManager.default.setAttributes([.posixPermissions: 0o700], ofItemAtPath: unreadable.path)
67+
#expect(incompleteTreeStatus != 0)
68+
let symlink = root.appendingPathComponent("receipt-link.json")
69+
try FileManager.default.createSymbolicLink(at: symlink, withDestinationURL: receipt)
70+
let symlinkStatus = try #require(OpenClawProcessEntrypoint.run(
71+
arguments: ["OpenClaw", ElevationFilesystemSync.fileArgument, symlink.path],
72+
launchApplication: { applicationConstructed = true }))
73+
#expect(symlinkStatus != 0)
74+
#expect(!applicationConstructed)
75+
}
76+
777
@Test func `normal launches allow automatic presentation`() {
878
let policy = AppLaunchRuntimePlan(arguments: ["OpenClaw"])
979

0 commit comments

Comments
 (0)