mirror of
https://github.com/insidegui/VirtualBuddy.git
synced 2026-12-02 00:59:45 +08:00
74 lines
16 KiB
Markdown
74 lines
16 KiB
Markdown
# The security of Xcode debugging in VirtualBuddy
|
||
|
||
With VirtualBuddy, you can set a breakpoint in Xcode on your Mac and inspect your app as it runs inside a macOS virtual machine. Xcode builds the app on the host. VirtualBuddy delivers the built app to the VM you selected, where macOS checks its signature and its permission to be debugged. Your source tree can stay on the host. The debugger connects through the VM’s virtual hardware, so Remote Login and shared folders can both be turned off.
|
||
|
||
You decide which project and VM to use together. **Create Scheme** saves a personal Xcode scheme and records your approval on the host Mac. On subsequent runs, VirtualBuddy uses that record to select the destination. Your team can continue using its shared scheme, and you can switch between running locally and running in the VM by choosing a scheme in Xcode.
|
||
|
||
An App Store Connect key is optional. If your app needs a development profile that covers the VM, you can register the VM yourself through Apple Developer. If you choose to register it from VirtualBuddy, the private key stays on your Mac. You authorize its use with Touch ID or your account password and separately confirm the registration before it consumes a team device slot.
|
||
|
||
## What creating a scheme authorizes
|
||
|
||
Setup starts with a particular VM. You choose an Xcode project or workspace and the app scheme to use. If Xcode needs **Custom LLDB Commands**, the sheet offers an explicit **Enable** button. This experimental preference applies to Xcode across projects and takes effect after relaunch. Creating the VM scheme is a separate action.
|
||
|
||
VirtualBuddy reads the selected scheme, or constructs one from the app target when Xcode generates schemes automatically. It writes the VM-specific copy under `xcuserdata` in the project or workspace. The copy’s Run action invokes the bundled `vbdebug` helper and tells LLDB to load a Python module shipped with VirtualBuddy. Both paths go through links in the user’s Application Support directory that VirtualBuddy refreshes when it moves or updates. Setup reads scheme and workspace XML with external entity loading disabled and writes the new file atomically. It replaces a file of the same name only if that file already specifies the same VM and target in a VirtualBuddy binding. [Scheme generation](../Packages/MacDebugHost/Sources/MacDebugHost/DebugScheme.swift)
|
||
|
||
The scheme carries an identifier. The approval to start a VM lives in a separate record in the host user’s login Keychain. VirtualBuddy authenticates the scheme identifier, destination UUID, target identifier, original scheme location and record version with HMAC-SHA256 using a 256-bit key in the Keychain. When the helper requests a run, VirtualBuddy’s broker verifies the record and takes the destination UUID from it. Exactly one VM in the library must match. You can rename the scheme and keep its approval because the lookup uses the scheme identifier. Editing the VM name or UUID in the XML cannot redirect that approval. [Scheme approvals](../Packages/MacDebugHost/Sources/MacDebugHost/DebugBindings.swift) · [Destination selection](../VirtualUI/Source/Session/VirtualMachineSessionUIManager.swift)
|
||
|
||
## From Xcode to the guest
|
||
|
||
The scheme’s helper asks VirtualBuddy to prepare a run. VirtualBuddy works with the guest to install the app, then hands LLDB a separate connection for breakpoints, memory inspection and stepping.
|
||
|
||
| Connection | What crosses it | How VirtualBuddy selects the peer |
|
||
| --- | --- | --- |
|
||
| Scheme helper ↔ VirtualBuddy | Build locations, enabled Run options and the prepared launch details | Both processes check the local user and the other process’s code signature. |
|
||
| VirtualBuddy ↔ VirtualBuddyGuest | The host sends control requests and the built app archive. The guest returns requested system images and, when needed, its provisioning device identifier (UDID). | The host opens the selected VM’s Virtio socket device. The guest service associates each operation with the current guest session and run. |
|
||
| LLDB ↔ guest debugger | The live debugger protocol | LLDB connects to a per-run, same-user Unix socket on the host. VirtualBuddy relays it over a dedicated Virtio connection. The guest accepts the host’s Virtio context ID, and the host checks the run UUID returned by the guest. |
|
||
|
||
The host broker creates its Unix sockets with mode `0600`. It checks that the enclosing directory belongs to the current user and grants access only to that user. On the control connection, macOS supplies a kernel audit token for the connected process. Both the helper and the broker use it to verify the peer against VirtualBuddy’s designated code-signing requirement before exchanging build information. The broker accepts up to 16 clients at once and gives each 10 seconds to send its request. [Peer verification](../Packages/MacDebugProtocol/Sources/MacDebugProtocol/DebugPeer.swift) · [Run broker](../Packages/MacDebugHost/Sources/MacDebugHost/DebugBroker.swift)
|
||
|
||
For each run, the helper checks that Xcode built a macOS app and an executable inside it. The broker checks the absolute local paths and the executable’s location within the standardized bundle path. The guest receives the app archive and the arguments and environment variables enabled in the scheme’s Run action. The helper expands named Xcode build settings in those values. The guest passes the resulting values as arguments to Apple’s debugger, placing the app executable and its arguments after the debugger’s end-of-options marker. This lets you pass a path containing spaces or a literal shell character as app data. [Run options](../Packages/MacDebugHost/Sources/MacDebugHost/DebugSchemeOptions.swift) · [Guest launch](../Packages/MacDebugTarget/Sources/MacDebugTarget/TargetDebugRun.swift)
|
||
|
||
VirtualBuddy can start or resume the approved VM. It waits for the current VirtualBuddyGuest app in a logged-in desktop session and allows one active debug run per VM. File transfers carry a session identifier, an operation identifier and a debugging purpose. Each receiver assigns its own destination to the expected transfer. The guest waits for the full archive before opening it. [Guest debugging service](../VirtualWormhole/Source/Services/Debugging/DebugGuestService.swift)
|
||
|
||
## Preserving the app’s signing identity
|
||
|
||
A provisioning profile authorizes a signed app to use particular capabilities, such as a provisioned app group. The app and its extensions can each have their own profile. VirtualBuddy looks for embedded profiles throughout the built product, accepting regular profile files and skipping symlinked directories. A build with no embedded profiles skips host provisioning entirely. For a profiled build, VirtualBuddy asks the guest for the provisioning UDID reported by System Information and checks whether the embedded profiles are current and cover that Mac.
|
||
|
||
When a replacement is needed, VirtualBuddy looks in its local profile cache and Xcode’s profile directories. You can also supply profiles with **Import Profiles…**. A replacement must cover the device, Mac platform, signing team, signing certificate, app identifier and restricted entitlements claimed by that component. VirtualBuddy checks the profile’s expiration and CMS signature, evaluates its certificate chain against Apple roots, and requires the signer’s Mac provisioning profile signing extension. These checks apply to profiles obtained through Xcode as well as those selected by hand. [Profile selection and validation](../Packages/MacDebugHost/Sources/MacDebugHost/DevelopmentProfile.swift)
|
||
|
||
With automatic signing, VirtualBuddy can ask Xcode to refresh missing profiles. It exports a temporary archive of the app you just built with the debugging export method and `-allowProvisioningUpdates`. It runs `/usr/bin/xcodebuild` with a fixed system search path and a small set of environment variables that excludes the scheme’s `DEVELOPER_DIR`. The Xcode installation selected by `xcode-select` supplies the tools, and the account signed in to Xcode handles provisioning.
|
||
|
||
VirtualBuddy makes signing changes in a temporary copy. Before replacing a profile, it checks the copy against the profile bytes and code hash it inspected. It re-signs affected components from the inside out with their original identities, checks that their identifiers and entitlements match, then verifies the complete signature, including nested code and all architectures. If Xcode’s incremental build changed just the sealed profile, VirtualBuddy can reseal the copy. Other signature damage requires a fresh build. The development signing identity is used on the host. The guest receives the resulting signed app and its embedded profiles. [Staged signing and profile refresh](../Packages/MacDebugHost/Sources/MacDebugHost/DebugProvisioning.swift)
|
||
|
||
The guest makes its own launch decision. It requires one top-level app with an arm64 executable inside its bundle and checks its minimum macOS version. It validates the signature and nested code, reads `com.apple.security.get-task-allow` from the app’s signed entitlements, and requires it to be true. It also asks the guest’s own `/usr/bin/profiles` to validate each embedded profile. Only after these checks does it install the app under the guest user’s Application Support directory, atomically replacing an earlier build at the same location.
|
||
|
||
The process that inspects the app runs inside the VM. VirtualBuddyGuest locates `debugserver` in the guest’s Command Line Tools or Xcode installation and verifies its Apple signature. It gives `debugserver` an already-connected file descriptor and the validated executable to launch. The host side of the connection uses VirtualBuddy’s existing virtualization entitlement. [Guest installation and signature checks](../Packages/MacDebugTarget/Sources/MacDebugTarget/TargetDebugTools.swift) · [Host entitlements](../VirtualBuddy/Config/Entitlements/VirtualBuddy.entitlements)
|
||
|
||
## Using an App Store Connect key
|
||
|
||
If the VM needs registration, the provisioning window shows its UDID, the signing team and the affected bundle identifiers. You can copy the UDID and complete registration in Apple Developer. To perform the same operation from VirtualBuddy, you can import a team’s P-256 `.p8` key with its key ID and issuer ID. Apple assigns the key its permissions according to the role your team chooses. VirtualBuddy’s client uses just two device operations, lookup and registration. [Apple’s team key permissions](https://developer.apple.com/documentation/appstoreconnectapi/creating-api-keys-for-app-store-connect-api)
|
||
|
||
VirtualBuddy stores one credential per signing team in its app’s access group in the host’s data protection Keychain. The item is bound to that Mac and available only when it is unlocked (`kSecAttrAccessibleWhenUnlockedThisDeviceOnly`). Reading it also requires user presence. A separate Keychain item identifies the configured team, so Settings can list teams without reading their private keys. The provisioning window first accesses the key when you click **Check Registration**. macOS asks for Touch ID or your password, and one authentication context covers the lookup and any registration you approve in that panel. [Key storage and authentication](../Packages/AppStoreConnectDevices/Sources/AppStoreConnectDevices/DeviceRegistrationKeys.swift) · [Provisioning window](../VirtualUI/Source/Library/XcodeProvisioningController.swift)
|
||
|
||
For each request, VirtualBuddy uses the private key on the host to sign a JSON Web Token with ES256. The token names the issuer and key ID, identifies App Store Connect as its audience and expires after five minutes. VirtualBuddy sends this token as the bearer credential over HTTPS directly to Apple’s `api.appstoreconnect.apple.com/v1/devices` endpoint. The client uses an ephemeral URL session with automatic cookies and credential storage turned off. It refuses HTTP redirects so each request ends at the intended service.
|
||
|
||
Lookup requests specify the exact UDID and Mac platform. If Apple has no matching device, **Register Virtual Machine…** asks you to confirm the VM name and signing team and explains the membership-year device slot it will use. The client checks again before sending the name, UDID and platform to Apple. It verifies the platform and UDID in Apple’s response and directs disabled registrations to a team administrator. **Remove Key** in Developer settings deletes the local credential and its team marker. Team-wide key revocation is a separate action in App Store Connect. [Registration client](../Packages/AppStoreConnectDevices/Sources/AppStoreConnectDevices/MacDeviceRegistration.swift) · [Apple’s device registration limits](https://developer.apple.com/help/account/devices/devices-overview/)
|
||
|
||
## Preparing system symbols on the host
|
||
|
||
To show a stack frame inside a macOS framework, LLDB needs system libraries that match the guest build. On a cache miss, VirtualBuddy requests the guest’s dynamic loader (`dyld`) and its shared cache files. It validates the manifest’s architecture, names, file count and sizes, checks available disk space, verifies each received byte count and matches the primary cache header’s UUID to the manifest.
|
||
|
||
Extraction runs in a separate helper process under a deny-by-default sandbox. In addition to the macOS system sandbox rules, the profile grants read access to the code and staged images needed for extraction, confines extraction writes to the output directory and denies XPC service lookups. Before loading Xcode’s extractor, the helper verifies its Apple signature and its identifier, `com.apple.dsc_extractor`.
|
||
|
||
VirtualBuddy accepts only regular files and directories in the extracted tree, installs the standalone guest loader and checks for the expected system library before publishing the cache. It discards partial preparations. Completed entries are reusable across VMs with the same OS build, architecture and cache UUID. In **Settings → Developer**, you can see how much disk space each entry uses and remove it once the last run using it ends. [Symbol cache and extraction sandbox](../Packages/MacDebugHost/Sources/MacDebugHost/DebugSymbolCache.swift)
|
||
|
||
## Ending a run and controlling future access
|
||
|
||
LLDB claims its launch details by renaming a handoff file in the private runtime directory. It accepts details up to 90 seconds old and deletes the claimed file after reading it. The per-run relay also waits up to 90 seconds for a same-user connection and removes its socket name once connected. When the debugger disconnects, VirtualBuddy closes the relay and releases the run. The guest stops its debugger and removes temporary staging after outstanding transfers finish. The validated app stays installed in the guest user’s debugging directory for the next run. [LLDB handoff](../Packages/MacDebugHost/Sources/MacDebugHost/Resources/mac_debug.py) · [Guest debugger connection](../VirtualWormhole/Source/Services/Debugging/DebugVirtioSocket.swift)
|
||
|
||
By default, VirtualBuddy enables Developer Features when Xcode is installed. Once you choose a setting in **VirtualBuddy Settings → Developer**, VirtualBuddy remembers your choice. Turning it off closes the host broker’s listener. VirtualBuddy checks the setting when accepting a run and again immediately before requesting launch in the guest. The same setting controls scheme creation, saving a team key and initiating device lookups or registration.
|
||
|
||
Administrators can enforce this policy by setting `DisableDeveloperFeatures` to `true` in the `codes.rambo.VirtualBuddy` managed preference domain. The policy locks the switch off and blocks new runs even from previously approved schemes. Already attached sessions can finish in Xcode. Removing the policy restores the user’s saved choice. [Managed preferences](ManagedPreferences.md)
|
||
|
||
This paper covers the [VirtualBuddy implementation from `3522f65` through `aabca53`](https://github.com/insidegui/VirtualBuddy/compare/20c10d88cab660aff5a57729d2611968ab4f6add...aabca5342d1b024b3929de35bb3d9982ed104811).
|