Skip to content

Run a virtual iPhone on an Apple Silicon Mac.

Swift

Harden ramdisk mounts and enforce strict iproxy UDID isolation

Lakrd9c93ec703a5
diff --git a/scripts/cfw_install.sh b/scripts/cfw_install.sh
index 77d05a11..70872577 100755
--- a/scripts/cfw_install.sh
+++ b/scripts/cfw_install.sh
@@ -134,11 +134,23 @@ ldid_sign() {
# Detach a DMG mountpoint if currently mounted, ignore errors
safe_detach() {
local mnt="$1"
- if mount | grep -q "$mnt"; then
+ if mount | grep -Fq " on $mnt "; then
sudo hdiutil detach -force "$mnt" 2>/dev/null || true
fi
}
+assert_mount_under_vm() {
+ local mnt="$1" label="${2:-mountpoint}"
+ local abs_vm abs_mnt
+
+ abs_vm="$(cd "$VM_DIR" && pwd -P)"
+ abs_mnt="$(cd "$mnt" && pwd -P)"
+ case "$abs_mnt/" in
+ "$abs_vm/"*) ;;
+ *) die "Unsafe ${label}: ${abs_mnt} (must be inside ${abs_vm})" ;;
+ esac
+}
+
# Mount device filesystem, tolerate already-mounted
remote_mount() {
local dev="$1" mnt="$2" opts="${3:-rw}"
@@ -249,11 +261,13 @@ fi
safe_detach "$MNT_SYSOS"
safe_detach "$MNT_APPOS"
mkdir -p "$MNT_SYSOS" "$MNT_APPOS"
+assert_mount_under_vm "$MNT_SYSOS" "SystemOS mountpoint"
+assert_mount_under_vm "$MNT_APPOS" "AppOS mountpoint"
echo " Mounting SystemOS..."
-sudo hdiutil attach -mountpoint "$MNT_SYSOS" "$SYSOS_DMG" -owners off
+sudo hdiutil attach -mountpoint "$MNT_SYSOS" "$SYSOS_DMG" -nobrowse -owners off
echo " Mounting AppOS..."
-sudo hdiutil attach -mountpoint "$MNT_APPOS" "$APPOS_DMG" -owners off
+sudo hdiutil attach -mountpoint "$MNT_APPOS" "$APPOS_DMG" -nobrowse -owners off
# Mount device rootfs (tolerate already-mounted)
echo " Mounting device rootfs rw..."
diff --git a/scripts/cfw_install_dev.sh b/scripts/cfw_install_dev.sh
index ee2eda32..aeb20b79 100755
--- a/scripts/cfw_install_dev.sh
+++ b/scripts/cfw_install_dev.sh
@@ -107,11 +107,23 @@ ldid_sign_ent() {
# Detach a DMG mountpoint if currently mounted, ignore errors
safe_detach() {
local mnt="$1"
- if mount | grep -q "$mnt"; then
+ if mount | grep -Fq " on $mnt "; then
sudo hdiutil detach -force "$mnt" 2>/dev/null || true
fi
}
+assert_mount_under_vm() {
+ local mnt="$1" label="${2:-mountpoint}"
+ local abs_vm abs_mnt
+
+ abs_vm="$(cd "$VM_DIR" && pwd -P)"
+ abs_mnt="$(cd "$mnt" && pwd -P)"
+ case "$abs_mnt/" in
+ "$abs_vm/"*) ;;
+ *) die "Unsafe ${label}: ${abs_mnt} (must be inside ${abs_vm})" ;;
+ esac
+}
+
# Mount device filesystem, tolerate already-mounted
remote_mount() {
local dev="$1" mnt="$2" opts="${3:-rw}"
@@ -235,11 +247,13 @@ fi
safe_detach "$MNT_SYSOS"
safe_detach "$MNT_APPOS"
mkdir -p "$MNT_SYSOS" "$MNT_APPOS"
+assert_mount_under_vm "$MNT_SYSOS" "SystemOS mountpoint"
+assert_mount_under_vm "$MNT_APPOS" "AppOS mountpoint"
echo " Mounting SystemOS..."
-sudo hdiutil attach -mountpoint "$MNT_SYSOS" "$SYSOS_DMG" -owners off
+sudo hdiutil attach -mountpoint "$MNT_SYSOS" "$SYSOS_DMG" -nobrowse -owners off
echo " Mounting AppOS..."
-sudo hdiutil attach -mountpoint "$MNT_APPOS" "$APPOS_DMG" -owners off
+sudo hdiutil attach -mountpoint "$MNT_APPOS" "$APPOS_DMG" -nobrowse -owners off
# Mount device rootfs (tolerate already-mounted)
echo " Mounting device rootfs rw..."
diff --git a/scripts/ramdisk_build.py b/scripts/ramdisk_build.py
index 269ceacc..01a05a8a 100755
--- a/scripts/ramdisk_build.py
+++ b/scripts/ramdisk_build.py
@@ -178,6 +178,18 @@ def run_sudo(cmd, **kwargs):
return run(["sudo", *cmd], **kwargs)
+def ensure_path_within_vm(path, vm_dir, label):
+ """Fail if path escapes vm_dir."""
+ vm_real = os.path.realpath(vm_dir)
+ path_real = os.path.realpath(path)
+ if path_real == vm_real or path_real.startswith(vm_real + os.sep):
+ return
+ print(f"[-] {label} must be inside VM dir")
+ print(f" VM dir: {vm_real}")
+ print(f" Path: {path_real}")
+ sys.exit(1)
+
+
def check_prerequisites():
"""Verify required host tools are available."""
missing = []
@@ -383,6 +395,7 @@ def build_ramdisk(restore_dir, im4m_path, vm_dir, input_dir, output_dir, temp_di
capture_output=True,
)
+ ensure_path_within_vm(mountpoint, vm_dir, "Ramdisk mountpoint")
os.makedirs(mountpoint, exist_ok=True)
try:
@@ -395,6 +408,7 @@ def build_ramdisk(restore_dir, im4m_path, vm_dir, input_dir, output_dir, temp_di
"-mountpoint",
mountpoint,
ramdisk_raw,
+ "-nobrowse",
"-owners",
"off",
]
@@ -433,6 +447,7 @@ def build_ramdisk(restore_dir, im4m_path, vm_dir, input_dir, output_dir, temp_di
"-mountpoint",
mountpoint,
ramdisk_custom,
+ "-nobrowse",
"-owners",
"off",
]
@@ -550,6 +565,8 @@ def main():
# Create temp and output directories
temp_dir = os.path.join(vm_dir, TEMP_DIR)
output_dir = os.path.join(vm_dir, OUTPUT_DIR)
+ ensure_path_within_vm(temp_dir, vm_dir, "Temp directory")
+ ensure_path_within_vm(output_dir, vm_dir, "Output directory")
for d in (temp_dir, output_dir):
if os.path.exists(d):
shutil.rmtree(d)
diff --git a/scripts/setup_machine.sh b/scripts/setup_machine.sh
index cd3e51fa..45469481 100755
--- a/scripts/setup_machine.sh
+++ b/scripts/setup_machine.sh
@@ -38,7 +38,7 @@ RAMDISK_SSH_PORT="${RAMDISK_SSH_PORT:-}"
RAMDISK_SSH_USER="${RAMDISK_SSH_USER:-root}"
RAMDISK_SSH_PASS="${RAMDISK_SSH_PASS:-alpine}"
IPROXY_UDID="${IPROXY_UDID:-}"
-IPROXY_DEVICE_WAIT_TIMEOUT="${IPROXY_DEVICE_WAIT_TIMEOUT:-45}"
+IPROXY_DEVICE_WAIT_TIMEOUT="${IPROXY_DEVICE_WAIT_TIMEOUT:-90}"
IPROXY_DEVICE_WAIT_INTERVAL="${IPROXY_DEVICE_WAIT_INTERVAL:-1}"
RAMDISK_SSH_PORT_EXPLICIT=0
if [[ -n "$RAMDISK_SSH_PORT" ]]; then
@@ -193,9 +193,8 @@ try_resolve_iproxy_target_udid() {
fi
if (( ${#usbmux_udids[@]} == 1 )); then
- IPROXY_TARGET_UDID="${usbmux_udids[1]}"
- IPROXY_RESOLVE_REASON="single_fallback"
- return 0
+ IPROXY_RESOLVE_REASON="single_mismatch"
+ return 3
fi
IPROXY_RESOLVE_REASON="ambiguous"
@@ -229,21 +228,23 @@ wait_for_iproxy_target_udid() {
ecid_match)
echo "[+] iproxy target UDID matched ECID substring: ${IPROXY_TARGET_UDID}"
;;
- single_fallback)
- echo "[!] Restore UDID (${DEVICE_UDID}) is not visible in ramdisk USBMux enumeration."
- echo "[!] Falling back to the only available USBMux ID: ${IPROXY_TARGET_UDID}"
- ;;
esac
return
fi
rc=$?
if (( waited == 0 || waited % 5 == 0 )); then
- if [[ "$rc" == "2" ]]; then
- echo " waiting for USBMux disambiguation... ${waited}s elapsed"
- else
- echo " waiting for USBMux device... ${waited}s elapsed"
- fi
+ case "$rc" in
+ 2)
+ echo " waiting for USBMux disambiguation... ${waited}s elapsed"
+ ;;
+ 3)
+ echo " waiting for restore UDID/ECID match (strict mode)... ${waited}s elapsed"
+ ;;
+ *)
+ echo " waiting for USBMux device... ${waited}s elapsed"
+ ;;
+ esac
fi
sleep "$interval"
@@ -253,6 +254,9 @@ wait_for_iproxy_target_udid() {
echo "[-] Timed out resolving iproxy target UDID after ${timeout}s."
echo "[-] USBMux IDs currently visible:"
print_usbmux_udids
+ if [[ "$IPROXY_RESOLVE_REASON" == "single_mismatch" ]]; then
+ die "Only non-matching USBMux device was visible. Strict identity isolation is enabled; wait for restore UDID/ECID or set IPROXY_UDID explicitly."
+ fi
if [[ "$IPROXY_RESOLVE_REASON" == "ambiguous" ]]; then
die "Multiple USBMux devices detected and none uniquely matched restore UDID/ECID. Set IPROXY_UDID explicitly."
fi
@@ -782,6 +786,15 @@ wait_for_ramdisk_ssh() {
echo "[*] Waiting for ramdisk SSH on ${RAMDISK_SSH_USER}@127.0.0.1:${RAMDISK_SSH_PORT} (timeout=${RAMDISK_SSH_TIMEOUT}s)..."
while (( waited < RAMDISK_SSH_TIMEOUT )); do
+ if [[ -n "$IPROXY_PID" ]] && ! kill -0 "$IPROXY_PID" 2>/dev/null; then
+ echo "[-] iproxy process exited while waiting for ramdisk SSH."
+ if [[ -n "$IPROXY_LOG" ]]; then
+ echo "[-] iproxy log tail:"
+ tail -n 40 "$IPROXY_LOG" 2>/dev/null || true
+ fi
+ die "iproxy exited before ramdisk SSH became ready."
+ fi
+
if [[ -f "$DFU_LOG" ]] && grep -Eiq 'panic|kernel panic|stackshot succeeded|panic\.apple\.com' "$DFU_LOG"; then
echo "[-] Detected panic markers in boot_dfu log while waiting for ramdisk SSH."
echo "[-] boot_dfu log tail:"