Skip to content

Computer use CLI for AI agents

fix(sandbox): confine host key writes and bound guest probe replies (#673)

Lahfir2a63c28b0a15
diff --git a/providers/macos/provider.sh b/providers/macos/provider.sh
index e0a3a0cd3..358c81b63 100644
--- a/providers/macos/provider.sh
+++ b/providers/macos/provider.sh
@@ -581,6 +581,25 @@ guest_transport() {
"$@"
}
+PROBE_REPLY_LIMIT=65536
+
+# The guest decides how much a probe prints, so the host reads at most PROBE_REPLY_LIMIT bytes of the reply and
+# closes the pipe on the rest. Returns the ssh exit status.
+guest_probe() {
+ {
+ reply_status=$(
+ {
+ {
+ probe_ssh_status=0
+ guest_transport ssh "$@" 2>/dev/null 3>&- 4>&- || probe_ssh_status=$?
+ printf '%s\n' "$probe_ssh_status" >&3
+ } | head -c "$PROBE_REPLY_LIMIT" >&4
+ } 3>&1
+ )
+ } 4>&1
+ return "${reply_status:-255}"
+}
+
detach_guest_disk() {
disk=$1
for dev in $(hdiutil info 2>/dev/null | awk -v img="$disk" '$1 == "image-path" { path = $0; sub(/^[^:]*:[[:space:]]*/, "", path); found = (path == img) } found && $1 ~ /^\/dev\/disk/ { print $1 }'); do
@@ -588,17 +607,47 @@ detach_guest_disk() {
done
}
-guest_ssh_dir() {
- attach=$1
- mounts=$2
- printf '%s\n' "$attach" | awk -F '\t' '$3 ~ /^\// { print $3 }' > "$mounts"
- while IFS= read -r mount_point; do
- if [ -f "$mount_point/private/etc/ssh/ssh_host_ed25519_key" ]; then
- printf '%s\n' "$mount_point"
- return 0
- fi
- done < "$mounts"
- return 1
+# The guest chooses every path on its disk, so the keys are written only through directories and files that are
+# opened without following a symlink and that sit on the mounted volume. Exits 2 when the host key is not on it.
+write_guest_host_key() {
+ perl -e '
+ BEGIN { $SIG{__DIE__} = sub { exit 1 } }
+ use strict;
+ use Fcntl qw(:mode O_WRONLY O_CREAT O_NOFOLLOW O_NONBLOCK);
+ my ($volume_root, @sources) = @ARGV;
+ my @contents;
+ for my $source (@sources) {
+ open(my $in, "<", $source) or exit 1;
+ my $bytes = do { local $/; <$in> };
+ defined $bytes or exit 1;
+ push @contents, $bytes;
+ }
+ chdir $volume_root or exit 1;
+ my @root = stat "." or exit 1;
+ my $volume = $root[0];
+ for my $part (qw(private etc ssh)) {
+ my @seen = lstat $part or exit($!{ENOENT} ? 2 : 1);
+ $seen[0] == $volume or exit 2;
+ S_ISDIR($seen[2]) or exit 1;
+ chdir $part or exit 1;
+ my @entered = stat "." or exit 1;
+ $entered[0] == $seen[0] && $entered[1] == $seen[1] or exit 1;
+ }
+ my @writes;
+ for my $target (["ssh_host_ed25519_key", 0], ["ssh_host_ed25519_key.pub", O_CREAT]) {
+ my ($name, $create) = @$target;
+ sysopen(my $out, $name, O_WRONLY | O_NOFOLLOW | O_NONBLOCK | $create, 0644) or exit($!{ENOENT} ? 2 : 1);
+ my @held = stat $out or exit 1;
+ S_ISREG($held[2]) && $held[0] == $volume or exit 1;
+ push @writes, [$out, shift @contents];
+ }
+ for my $write (@writes) {
+ my ($out, $bytes) = @$write;
+ truncate($out, 0) or exit 1;
+ (syswrite($out, $bytes) // -1) == length($bytes) or exit 1;
+ close($out) or exit 1;
+ }
+ ' "$1" "$2" "$3"
}
rotate_host_key() {
@@ -616,18 +665,18 @@ rotate_host_key() {
rm -rf "$tmp"
return 1
fi
- if ! dir=$(guest_ssh_dir "$attach" "$tmp/mounts"); then
- detach_guest_disk "$disk"
- rm -rf "$tmp"
- return 2
- fi
- if ! cat "$tmp/key" > "$dir/private/etc/ssh/ssh_host_ed25519_key" || \
- ! cat "$tmp/key.pub" > "$dir/private/etc/ssh/ssh_host_ed25519_key.pub"; then
- detach_guest_disk "$disk"
+ printf '%s\n' "$attach" | awk -F '\t' '$3 ~ /^\// { print $3 }' > "$tmp/mounts"
+ write_status=2
+ while IFS= read -r mount_point; do
+ write_status=0
+ write_guest_host_key "$mount_point" "$tmp/key" "$tmp/key.pub" || write_status=$?
+ [ "$write_status" -eq 2 ] || break
+ done < "$tmp/mounts"
+ detach_guest_disk "$disk"
+ if [ "$write_status" -ne 0 ]; then
rm -rf "$tmp"
- return 1
+ return "$write_status"
fi
- detach_guest_disk "$disk"
if ! cp "$tmp/key.pub" "$pub"; then
rm -rf "$tmp"
return 1
@@ -713,7 +762,7 @@ check_guest_setup() {
fi
target=$user@$ip
probe_status=0
- permissions=$(guest_transport ssh "$target" 'if [ ! -x ~/.local/bin/agent-desktop ]; then exit 3; fi; ~/.local/bin/agent-desktop permissions' 2>/dev/null) || probe_status=$?
+ permissions=$(guest_probe "$target" 'if [ ! -x ~/.local/bin/agent-desktop ]; then exit 3; fi; ~/.local/bin/agent-desktop permissions') || probe_status=$?
if [ "$probe_status" -eq 255 ]; then
guest_not_answering "$name" "$user"
return 0
@@ -941,11 +990,11 @@ do_exec() {
host_helper_hash=$(printf '%s\n' "$host_helper_hash_line" | awk '{ print tolower($1) }')
target=$user@$ip
probe_status=0
- guest_hash_line=$(guest_transport ssh "$target" 'shasum -a 1 ~/.local/bin/agent-desktop' 2>/dev/null) || probe_status=$?
+ guest_hash_line=$(guest_probe "$target" 'shasum -a 1 ~/.local/bin/agent-desktop') || probe_status=$?
if [ "$probe_status" -eq 255 ]; then remote_login_failure "$name"; fi
guest_hash=$(printf '%s\n' "$guest_hash_line" | awk '{ print tolower($1) }')
probe_status=0
- guest_helper_hash_line=$(guest_transport ssh "$target" "shasum -a 1 ~/.local/bin/$HELPER_NAME" 2>/dev/null) || probe_status=$?
+ guest_helper_hash_line=$(guest_probe "$target" "shasum -a 1 ~/.local/bin/$HELPER_NAME") || probe_status=$?
if [ "$probe_status" -eq 255 ]; then remote_login_failure "$name"; fi
guest_helper_hash=$(printf '%s\n' "$guest_helper_hash_line" | awk '{ print tolower($1) }')
if ! sync_guest_file "$target" "$AD_BINARY" agent-desktop "$host_hash" "$guest_hash"; then
diff --git a/providers/macos/test-provider.sh b/providers/macos/test-provider.sh
index 67a0024c2..d39d433f3 100644
--- a/providers/macos/test-provider.sh
+++ b/providers/macos/test-provider.sh
@@ -66,10 +66,13 @@ fake hdiutil <<'FAKE'
[ "${FAKE_HDIUTIL:-ok}" = ok ] || exit 1
case ${1:-} in
attach)
- mkdir -p "$FAKE_VOLUME/private/etc/ssh"
- : > "$FAKE_VOLUME/private/etc/ssh/ssh_host_ed25519_key"
- : > "$FAKE_VOLUME/private/etc/ssh/ssh_host_ed25519_key.pub"
- printf '/dev/disk99\tFAKE\t%s\n' "$FAKE_VOLUME"
+ if [ ! -e "$FAKE_VOLUME" ]; then
+ mkdir -p "$FAKE_VOLUME/private/etc/ssh"
+ : > "$FAKE_VOLUME/private/etc/ssh/ssh_host_ed25519_key"
+ : > "$FAKE_VOLUME/private/etc/ssh/ssh_host_ed25519_key.pub"
+ fi
+ mkdir -p "$FAKE_VOLUME.preboot"
+ printf '/dev/disk99s1\tFAKE\t%s\n/dev/disk99s2\tFAKE\t%s\n' "$FAKE_VOLUME.preboot" "$FAKE_VOLUME"
;;
detach) printf 'hdiutil %s\n' "$*" >> "$FAKE_LOG" ;;
info) printf 'image-path : %s\n/dev/disk99\n' "$AD_SANDBOX_DIR/vm/Disk.img" ;;
@@ -124,6 +127,15 @@ fake ssh <<'FAKE'
printf 'ssh %s\n' "$*" >> "$FAKE_LOG"
for remote do :; done
[ "${FAKE_SSH:-ok}" != unreachable ] || exit 255
+[ "${FAKE_SSH:-ok}" != no-binary ] || exit 3
+if [ -n "${FAKE_PROBE_FLOOD:-}" ]; then
+ case $remote in
+ *permissions*|*'shasum -a 1'*)
+ head -c 33554432 /dev/zero | tr '\0' 'x' && : > "$FAKE_PROBE_FLOOD"
+ exit
+ ;;
+ esac
+fi
case $remote in
*permissions*) printf '{"data":{"accessibility":{"state":"%s"}}}\n' "${FAKE_ACCESSIBILITY:-granted}" ;;
*'shasum -a 1'*) printf '%s file\n' "${FAKE_GUEST_SHA:-stale}" ;;
@@ -235,10 +247,28 @@ grep -q "vmhost run $tmp/sandboxes/demo/vm\$" "$FAKE_LOG" || fail 'a set-up sand
grep -q '^192.0.2.2 ' "$tmp/sandboxes/demo/known_hosts" || fail 'start did not pin the sandbox host key'
grep -q -- '-o StrictHostKeyChecking=yes' "$FAKE_LOG" || fail 'a pinned sandbox did not verify the host key it pinned'
grep -q 'hdiutil detach /dev/disk99' "$FAKE_LOG" || fail 'the guest disk stayed mounted after the host key was written'
+grep -q '^private key' "$FAKE_VOLUME/private/etc/ssh/ssh_host_ed25519_key" && grep -q '^public key' "$FAKE_VOLUME/private/etc/ssh/ssh_host_ed25519_key.pub" || fail 'the new host key did not reach the guest disk'
put demo stopped
provider demo FAKE_HDIUTIL=broken start demo
expect 'a guest disk that cannot be opened' 1 code ACTION_FAILED
[ ! -f "$tmp/sandboxes/demo/vm/vmhost.pid" ] || fail 'a sandbox without its own host key was booted'
+
+for link in private private/etc private/etc/ssh private/etc/ssh/ssh_host_ed25519_key private/etc/ssh/ssh_host_ed25519_key.pub; do
+ rm -rf "$FAKE_VOLUME" "$tmp/host"
+ mkdir -p "$FAKE_VOLUME/private/etc/ssh" "$tmp/host"
+ printf 'host file\n' > "$FAKE_VOLUME/private/etc/ssh/ssh_host_ed25519_key"
+ printf 'host file\n' > "$FAKE_VOLUME/private/etc/ssh/ssh_host_ed25519_key.pub"
+ mv "$FAKE_VOLUME/$link" "$tmp/host/target"
+ ln -s "$tmp/host/target" "$FAKE_VOLUME/$link"
+ put demo stopped
+ : > "$FAKE_LOG"
+ provider demo start demo
+ expect "a guest disk whose $link is a link to the host" 1 code ACTION_FAILED
+ [ -z "$(find "$tmp/host" -type f -exec grep -L '^host file$' {} +)" ] || fail "a host file behind the guest link $link was overwritten"
+ [ ! -f "$tmp/sandboxes/demo/vm/vmhost.pid" ] || fail "a sandbox whose $link is a link to the host was booted"
+ grep -q 'hdiutil detach /dev/disk99' "$FAKE_LOG" || fail "the guest disk stayed mounted after the link at $link was refused"
+done
+rm -rf "$FAKE_VOLUME" "$tmp/host"
put demo stopped
provider demo AD_SET_WINDOW=on 'AD_SET_TITLE=Review build' start demo
vm_pid=$(cat "$tmp/sandboxes/demo/vm/vmhost.pid")
@@ -270,6 +300,9 @@ expect 'accessibility not granted' 0 state needs_setup
: > "$tmp/sandboxes/demo/setup_done"
provider demo FAKE_SSH=unreachable status demo
expect 'a guest that does not answer yet' 0 state working
+provider demo FAKE_SSH=no-binary status demo
+expect 'a guest without agent-desktop' 0 state needs_setup
+field message | grep -q 'agent-desktop is not installed in the guest' || fail 'the probe lost the exit status that reports a missing guest binary'
: > "$FAKE_LOG"
provider demo exec demo -- type "two words" 'it'"'"'s $HOME `id`' '' --
@@ -289,6 +322,14 @@ provider demo FAKE_GUEST_NOISE=300000 FAKE_GUEST_EXIT=7 exec demo -- version
[ "$(wc -c < "$tmp/err")" -le 65536 ] || fail 'guest stderr reached the host unbounded'
provider demo FAKE_SSH=unreachable exec demo -- version
expect 'exec with no Remote Login' 125 code ACTION_FAILED
+provider demo "FAKE_PROBE_FLOOD=$tmp/flooded" status demo
+expect 'status with a guest that floods its reply' 0 state needs_setup
+[ ! -e "$tmp/flooded" ] || fail 'a status probe accepted the whole guest reply'
+: > "$tmp/sandboxes/demo/setup_done"
+provider demo "FAKE_PROBE_FLOOD=$tmp/flooded" exec demo -- version
+[ "$status" = 0 ] || fail "exec with a guest that floods its reply exited $status"
+[ "$(cat "$tmp/out")" = '[version]' ] || fail 'a guest that floods its reply lost the exec output'
+[ ! -e "$tmp/flooded" ] || fail 'an exec probe accepted the whole guest reply'
# create: every bad combination of settings is refused before anything is touched.
refuse() {