Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

12.1 KB
Raw Blame
#!/usr/bin/env bash
# Guest-side helper: derive CuaDriver's signed requirement and seed system TCC.
set -euo pipefail

TCC_DB="/Library/Application Support/com.apple.TCC/TCC.db"
APP_PATH="${CUA_TCC_APP_PATH:-/Applications/CuaDriverLocal.app}"
BINARY_PATH="${CUA_TCC_BINARY_PATH:-}"
EXPECTED_CLIENT="${CUA_TCC_EXPECTED_CLIENT:-com.trycua.driver.local}"
ALLOW_ADHOC="${CUA_TCC_ALLOW_ADHOC:-0}"
READ_SUDO_PASSWORD="${CUA_TCC_READ_SUDO_PASSWORD:-0}"

usage() {
  cat <<'USAGE'
Usage: seed-tcc-guest.sh [--app PATH] [--binary PATH]
                         [--expected-client ID] [--allow-adhoc]

Run inside a SIP-disabled Lume macOS VM. Seeds kTCCServiceAccessibility and
kTCCServiceScreenCapture into the system TCC database for the signed CuaDriver
app identity. The script refuses to run outside VirtualMac or with SIP enabled.

Pass --app /path/to/binary without --binary for a path-type test grant; also
pass --expected-client with that binary's canonical path. When --app points to
an app bundle, --binary may only name that bundle's executable.
USAGE
}

while (($#)); do
  case "$1" in
    --app)
      if (($# < 2)); then echo "--app requires a value" >&2; exit 2; fi
      APP_PATH="$2"; shift
      ;;
    --app=*) APP_PATH="${1#*=}" ;;
    --binary)
      if (($# < 2)); then echo "--binary requires a value" >&2; exit 2; fi
      BINARY_PATH="$2"; shift
      ;;
    --binary=*) BINARY_PATH="${1#*=}" ;;
    --expected-client)
      if (($# < 2)); then echo "--expected-client requires a value" >&2; exit 2; fi
      EXPECTED_CLIENT="$2"; shift
      ;;
    --expected-client=*) EXPECTED_CLIENT="${1#*=}" ;;
    --allow-adhoc) ALLOW_ADHOC=1 ;;
    -h|--help) usage; exit 0 ;;
    *) echo "unknown argument: $1" >&2; usage >&2; exit 2 ;;
  esac
  shift
done

fail() {
  echo "seed-tcc: $*" >&2
  exit 2
}

canonical_path() {
  local path="$1"
  local dir
  local base
  dir="$(/usr/bin/dirname "${path}")"
  base="$(/usr/bin/basename "${path}")"
  (cd "${dir}" && printf '%s/%s\n' "$(/bin/pwd -P)" "${base}")
}

bundle_executable_path() {
  local app="$1"
  local info_plist="${app}/Contents/Info.plist"
  local executable_name
  [[ -d "${app}" ]] || fail "app bundle is missing: ${app}"
  [[ -f "${info_plist}" ]] || fail "missing Info.plist at ${info_plist}"
  executable_name="$(/usr/libexec/PlistBuddy -c 'Print :CFBundleExecutable' "${info_plist}" 2>/dev/null || true)"
  [[ -n "${executable_name}" ]] || fail "CFBundleExecutable is missing from ${info_plist}"
  printf '%s/Contents/MacOS/%s\n' "${app}" "${executable_name}"
}

# Classify a designated requirement (the text after "designated => " in
# `codesign -d -r-`). Prints the accepted form and returns 0 when the
# requirement is certificate-backed; prints "rejected" and returns 1 otherwise.
#
# Accepted forms:
#   leaf-hash     A requirement that pins a certificate leaf or root SHA-1 hash,
#                 as produced by local self-signed or stable signing identities.
#   developer-id  The exact Apple-anchored Developer ID Application requirement
#                 that codesign derives for notarized releases:
#                 identifier "<id>" and anchor apple generic and the Developer
#                 ID intermediate and leaf markers and a 10-character team ID.
#                 When a second argument is given, the requirement identifier
#                 must equal it.
# Everything else, including ad hoc cdhash-only requirements, is rejected.
DEVELOPER_ID_REQUIREMENT_RE='^identifier "([A-Za-z0-9][A-Za-z0-9.-]*)" and anchor apple generic and certificate 1\[field\.1\.2\.840\.113635\.100\.6\.2\.6\] /\* exists \*/ and certificate leaf\[field\.1\.2\.840\.113635\.100\.6\.1\.13\] /\* exists \*/ and certificate leaf\[subject\.OU\] = ([A-Z0-9]{10}|"[A-Z0-9]{10}")$'
LEAF_HASH_REQUIREMENT_RE='certificate (leaf|root) = H"[[:xdigit:]]{40}"'

classify_designated_requirement() {
  local requirement="$1"
  local expected_identifier="${2:-}"
  if [[ "${requirement}" =~ ${LEAF_HASH_REQUIREMENT_RE} ]]; then
    echo "leaf-hash"
    return 0
  fi
  if [[ "${requirement}" =~ ${DEVELOPER_ID_REQUIREMENT_RE} ]]; then
    if [[ -z "${expected_identifier}" || "${BASH_REMATCH[1]}" == "${expected_identifier}" ]]; then
      echo "developer-id"
      return 0
    fi
  fi
  echo "rejected"
  return 1
}

sql_escape() {
  /usr/bin/sed "s/'/''/g" <<< "$1"
}

run_sudo() {
  if [[ "$(/usr/bin/id -u)" -eq 0 ]]; then
    "$@"
  elif [[ -n "${SUDO_PASSWORD_CACHE:-}" ]]; then
    printf '%s\n' "${SUDO_PASSWORD_CACHE}" | /usr/bin/sudo -S -p '' "$@"
  else
    /usr/bin/sudo -n "$@"
  fi
}

# Tests source this script with CUA_TCC_SEED_LIB_ONLY=1 to exercise the
# classifier without a VM. Executing it in that mode is refused.
if [[ "${CUA_TCC_SEED_LIB_ONLY:-0}" == 1 ]]; then
  if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then
    echo "CUA_TCC_SEED_LIB_ONLY is valid only when this script is sourced" >&2
    exit 2
  fi
  return 0
fi

MODEL="$(/usr/sbin/sysctl -n hw.model 2>/dev/null || true)"
[[ "${MODEL}" == VirtualMac* ]] || fail "refusing to edit TCC outside a Lume/VirtualMac guest (hw.model=${MODEL:-unknown})"

SIP_STATUS="$(/usr/bin/csrutil status 2>&1 || true)"
[[ "${SIP_STATUS}" == *"System Integrity Protection status: disabled."* ]] \
  || [[ "${SIP_STATUS}" == *"status: disabled"* ]] \
  || fail "system TCC.db is SIP-protected; expected SIP disabled, got: ${SIP_STATUS}"

[[ -n "${EXPECTED_CLIENT}" ]] || fail "--expected-client must not be empty"

CLIENT_TYPE=1
CLIENT=""
BUNDLE_ID=""
SIGN_TARGET=""
if [[ -n "${APP_PATH}" && -d "${APP_PATH}" ]]; then
  INFO_PLIST="${APP_PATH}/Contents/Info.plist"
  APP_EXECUTABLE_PATH="$(bundle_executable_path "${APP_PATH}")"
  if [[ -n "${BINARY_PATH}" ]]; then
    [[ -x "${BINARY_PATH}" ]] || fail "CuaDriver executable is missing or not executable: ${BINARY_PATH}"
    RESOLVED_BINARY_PATH="$(canonical_path "${BINARY_PATH}")"
    RESOLVED_APP_EXECUTABLE_PATH="$(canonical_path "${APP_EXECUTABLE_PATH}")"
    [[ "${RESOLVED_BINARY_PATH}" == "${RESOLVED_APP_EXECUTABLE_PATH}" ]] \
      || fail "--binary must match ${APP_PATH}'s CFBundleExecutable (${APP_EXECUTABLE_PATH}); got ${BINARY_PATH}"
  else
    BINARY_PATH="${APP_EXECUTABLE_PATH}"
    [[ -x "${BINARY_PATH}" ]] || fail "CuaDriver executable is missing or not executable: ${BINARY_PATH}"
  fi
  BUNDLE_ID="$(/usr/libexec/PlistBuddy -c 'Print :CFBundleIdentifier' "${INFO_PLIST}" 2>/dev/null || true)"
  [[ -n "${BUNDLE_ID}" ]] || fail "CFBundleIdentifier is missing from ${INFO_PLIST}"
  CLIENT_TYPE=0
  CLIENT="${BUNDLE_ID}"
  SIGN_TARGET="${APP_PATH}"
else
  [[ "${APP_PATH}" != *.app ]] || fail "app bundle is missing: ${APP_PATH}"
  [[ -z "${BINARY_PATH}" ]] \
    || fail "--binary requires --app to point at an app bundle; omit --binary and pass the executable path as --app for a path-type grant"
  BINARY_PATH="${APP_PATH}"
  [[ -x "${BINARY_PATH}" ]] || fail "CuaDriver executable is missing or not executable: ${BINARY_PATH}"
  CLIENT="$(canonical_path "${BINARY_PATH}")"
  SIGN_TARGET="${CLIENT}"
fi

VERIFY_INFO=""
if ! VERIFY_INFO="$(/usr/bin/codesign --verify --deep --strict "${SIGN_TARGET}" 2>&1)"; then
  fail "codesign verification failed for ${SIGN_TARGET}: ${VERIFY_INFO}"
fi

CODESIGN_INFO=""
if ! CODESIGN_INFO="$(/usr/bin/codesign -dvvv "${SIGN_TARGET}" 2>&1)"; then
  fail "could not read codesign metadata from ${SIGN_TARGET}: ${CODESIGN_INFO}"
fi
SIGNING_IDENTIFIER="$(/usr/bin/awk -F= '/^Identifier=/ { print $2; exit }' <<< "${CODESIGN_INFO}")"
[[ -n "${SIGNING_IDENTIFIER}" ]] || fail "could not read signing identifier from ${SIGN_TARGET}"

[[ "${CLIENT}" == "${EXPECTED_CLIENT}" ]] \
  || fail "TCC client mismatch: expected ${EXPECTED_CLIENT}, resolved ${CLIENT}"
if [[ "${CLIENT_TYPE}" == 0 ]]; then
  [[ "${SIGNING_IDENTIFIER}" == "${EXPECTED_CLIENT}" ]] \
    || fail "codesign identifier mismatch: expected ${EXPECTED_CLIENT}, got ${SIGNING_IDENTIFIER}"
fi

REQUIREMENT_INFO=""
if ! REQUIREMENT_INFO="$(/usr/bin/codesign -d -r- "${SIGN_TARGET}" 2>&1)"; then
  fail "could not read designated requirement from ${SIGN_TARGET}: ${REQUIREMENT_INFO}"
fi
REQUIREMENT="$(/usr/bin/awk -F 'designated => ' '/^(# )?designated =>/ { print $2; exit }' <<< "${REQUIREMENT_INFO}")"
[[ -n "${REQUIREMENT}" ]] || fail "could not read designated requirement from ${SIGN_TARGET}"
if [[ "${ALLOW_ADHOC}" != 1 ]]; then
  if printf '%s\n' "${CODESIGN_INFO}" | /usr/bin/grep -q '^Signature=adhoc$'; then
    fail "${SIGN_TARGET} has an ad hoc signature; rerun install-local with --require-stable-signing, or use --allow-adhoc only for an explicitly authorized one-build grant"
  fi
  if ! REQUIREMENT_KIND="$(classify_designated_requirement "${REQUIREMENT}" "${SIGNING_IDENTIFIER}")"; then
    fail "${SIGN_TARGET} is not signed with a certificate-backed identity; expected its designated requirement to bind a certificate leaf or root hash, or to be an Apple-anchored Developer ID requirement for ${SIGNING_IDENTIFIER} (got: ${REQUIREMENT})"
  fi
  echo "seed-tcc: designated requirement accepted as ${REQUIREMENT_KIND}"
fi

CSREQ_TMPDIR="$(/usr/bin/mktemp -d /tmp/cua-driver-tcc-csreq.XXXXXX)"
cleanup() { /bin/rm -rf "${CSREQ_TMPDIR}"; }
trap cleanup EXIT
printf '%s\n' "${REQUIREMENT}" > "${CSREQ_TMPDIR}/requirement.txt"
if ! /usr/bin/csreq -r "${CSREQ_TMPDIR}/requirement.txt" -b "${CSREQ_TMPDIR}/csreq.bin" >/dev/null; then
  fail "csreq failed for ${SIGN_TARGET}'s designated requirement"
fi
CSREQ_HEX="$(/usr/bin/od -An -tx1 -v "${CSREQ_TMPDIR}/csreq.bin" | /usr/bin/tr -d ' \n')"
[[ -n "${CSREQ_HEX}" ]] || fail "csreq produced an empty blob"

if [[ "$(/usr/bin/id -u)" -ne 0 ]]; then
  if /usr/bin/sudo -n -v 2>/dev/null; then
    true
  elif [[ "${READ_SUDO_PASSWORD}" == 1 ]]; then
    if IFS= read -r SUDO_PASSWORD_CACHE || [[ -n "${SUDO_PASSWORD_CACHE:-}" ]]; then
      true
    else
      fail "sudo password was not provided on stdin"
    fi
    printf '%s\n' "${SUDO_PASSWORD_CACHE}" | /usr/bin/sudo -S -p '' -v >/dev/null
  else
    fail "non-interactive sudo preflight failed (no active sudo timestamp or NOPASSWD rule); pass the password through the host wrapper, configure narrowly scoped NOPASSWD sudo for this disposable guest, or run as root"
  fi
fi

[[ -f "${TCC_DB}" ]] || fail "system TCC.db does not exist at ${TCC_DB}"
# tccd keeps TCC.db open and writes to it at any time, so a bare sqlite3 call
# can fail at once with "database is locked". Wait up to 30 s for the lock
# instead, and take the write lock when the transaction starts.
TCC_SQLITE=(/usr/bin/sqlite3 -cmd ".timeout 30000")
ACCESS_COLUMNS="$(run_sudo "${TCC_SQLITE[@]}" "${TCC_DB}" 'PRAGMA table_info(access);')"
CLIENT_SQL="$(sql_escape "${CLIENT}")"

if ! /usr/bin/grep -q '|auth_value|' <<< "${ACCESS_COLUMNS}"; then
  fail "unsupported TCC access schema; expected modern auth_value column"
fi

SQL="
BEGIN IMMEDIATE;
INSERT OR REPLACE INTO access(
  service,client,client_type,auth_value,auth_reason,auth_version,
  csreq,flags,indirect_object_identifier_type,
  indirect_object_identifier,indirect_object_code_identity,last_modified
) VALUES
  ('kTCCServiceAccessibility','${CLIENT_SQL}',${CLIENT_TYPE},2,2,1,
   X'${CSREQ_HEX}',0,0,'UNUSED',NULL,strftime('%s','now')),
  ('kTCCServiceScreenCapture','${CLIENT_SQL}',${CLIENT_TYPE},2,2,1,
   X'${CSREQ_HEX}',0,0,'UNUSED',NULL,strftime('%s','now'));
COMMIT;
"
VERIFY_SQL="SELECT count(*) FROM access WHERE client='${CLIENT_SQL}' AND client_type=${CLIENT_TYPE} AND auth_value=2 AND service IN ('kTCCServiceAccessibility','kTCCServiceScreenCapture');"

run_sudo "${TCC_SQLITE[@]}" "${TCC_DB}" "${SQL}"
ROW_COUNT="$(run_sudo "${TCC_SQLITE[@]}" "${TCC_DB}" "${VERIFY_SQL}")"
[[ "${ROW_COUNT}" == 2 ]] || fail "expected two granted rows for ${CLIENT}, found ${ROW_COUNT}"

if run_sudo /usr/bin/pgrep -x tccd >/dev/null 2>&1; then
  run_sudo /usr/bin/killall tccd >/dev/null \
    || fail "seeded rows for ${CLIENT}, but failed to restart tccd; restart the VM or tccd before trusting permission status"
  TCCD_RESTARTED=1
else
  TCCD_RESTARTED=0
  echo "seed-tcc: warning: tccd was not running; no TCC cache process was restarted" >&2
fi

echo "seeded kTCCServiceAccessibility and kTCCServiceScreenCapture for ${CLIENT} (client_type=${CLIENT_TYPE})"
echo "restart CuaDriverLocal.app before checking permissions if it was already running"
echo "model: ${MODEL}"
echo "sip: ${SIP_STATUS}"
echo "csreq_bytes: $(( ${#CSREQ_HEX} / 2 ))"
echo "tccd_restarted: ${TCCD_RESTARTED}"