Files
VirtualBuddy/VirtualCore/Source/SavedSession/VMBundleDuplicator.swift
Guilherme Rambo 4181d09e17 Guest<>host port and Unix socket forwarding (#824)
* Custom socket tunnels

* Improve tunnel configuration UI

* Tweak tunnel editing UI to be less confusing

* Create tunnel socket parent path if it doesn't already exist

* Security: Harden tunnel approval verification

* Security: harden tunnel approval UI against tampered strings, use only digest for validating approvals

* Security: refuse to start a TCP listener when something already answers at its address; replace existing socket when appropriate

* Fix newly added tunnel that replaces an existing listener being blocked by address in use

* Security: make tunnel status text from the other side safe to display

Status and failure text is chosen by the other side of a tunnel and shown
next to the tunnel in settings. Remove invisible, reordering and
line-breaking characters from it and limit its length.

Co-Authored-By: Claude Mythos 5.2 <noreply@anthropic.com>

* Security: require a trusted owner for every folder enclosing a tunnel socket

Each component of a listener's path, as written and as resolved through
links, has to belong to this account or to a system account. Set the
socket's permissions without following links.

Co-Authored-By: Claude Mythos 5.2 <noreply@anthropic.com>

* Security: check who owns a Unix socket tunnel destination

Apply the path ownership check to destinations, and require the service
that accepts the connection to run as this account or a system account.

Co-Authored-By: Claude Mythos 5.2 <noreply@anthropic.com>

* Security: give each tunnel its own share of a virtual machine's connections

Raise the limit to 64 connections per virtual machine with at most 32 for
one tunnel, so holding one tunnel's connections open can't starve the
others. Pause a listener that runs out of descriptors or memory instead
of stopping it for good.

Co-Authored-By: Claude Mythos 5.2 <noreply@anthropic.com>

* Security: keep the reason a tunnel failed on the host from the guest

The guest is only told that a tunnel isn't available on the host.

Co-Authored-By: Claude Mythos 5.2 <noreply@anthropic.com>

* Update service tunnels audit report with the status of F5 to F10

Notes what was fixed for each finding and what was left as is.

Co-Authored-By: Claude Mythos 5.2 <noreply@anthropic.com>

* Support managed preference restrictions on tunnels

* Remove audit file

* Tunnels UI tweaks

* Rewrite service tunnels documentation

---------

Co-authored-by: Claude Mythos 5.2 <noreply@anthropic.com>
2026-11-25 18:17:16 -03:00

74 lines
3.8 KiB
Swift

import Foundation
import OSLog
/// Duplicates a virtual machine bundle so that the copy is either complete or doesn't exist.
///
/// The copy is built under a hidden name next to its destination, finished there, and only then moved into place.
/// A saved session is copied along with everything else, so the copy resumes independently from the original.
/// Temporary data from operations that were in progress on the original is never copied, with one exception:
/// a saved session that was interrupted after it was resumed keeps that fact in the copy. The copy's disks may be newer than
/// the session, so resuming the copy has to ask first, exactly like the original.
///
/// Settings that the user approved for the original are approved again for the copy, because duplicating is something
/// the user does in VirtualBuddy. A bundle copied by any other means has to have those settings reviewed.
struct VMBundleDuplicator: Sendable {
typealias ApprovalCarrier = @Sendable (_ original: VBVirtualMachine, _ copy: VBVirtualMachine) throws -> Void
let fileSystem: SavedSessionFileSystem
let carryApprovals: ApprovalCarrier
private let logger = Logger(subsystem: VirtualCoreConstants.subsystemName, category: "VMBundleDuplicator")
init(fileSystem: SavedSessionFileSystem = DefaultSavedSessionFileSystem(),
carryApprovals: @escaping ApprovalCarrier = { try VBVirtualMachine.carryApprovedSettings(from: $0, to: $1) }) {
self.fileSystem = fileSystem
self.carryApprovals = carryApprovals
}
/// - Returns: The copy, with a new library identity but the same guest identity as the original.
func duplicate(bundleAt sourceURL: URL, to destinationURL: URL) throws -> VBVirtualMachine {
let workURL = destinationURL
.deletingLastPathComponent()
.appending(path: ".duplicating-\(UUID().uuidString).\(VBVirtualMachine.bundleExtension)", directoryHint: .isDirectory)
do {
/// Operations that were interrupted before the virtual machine could have run are finished (rolled back) on the original first,
/// so that the copy never starts from files that are halfway through being replaced. What's left is only
/// the record of a session that was resumed, which the copy has to keep.
try SavedSessionStorage(bundleURL: sourceURL, fileSystem: fileSystem).recover()
try fileSystem.copy(from: sourceURL, to: workURL)
for url in try fileSystem.contentsOfDirectory(at: workURL) {
let name = url.lastPathComponent
guard SavedSessionLayout.isTransientName(name), name != SavedSessionLayout.transactionName else { continue }
try fileSystem.remove(url)
}
var copy = try VBVirtualMachine(bundleURL: workURL, isNewInstall: false, createIfNeeded: false)
copy.uuid = UUID()
try copy.saveMetadata()
copy.bundleURL.creationDate = .now
try fileSystem.move(from: workURL, to: destinationURL, replacingExisting: false)
} catch {
try? fileSystem.remove(workURL)
throw error
}
let duplicate = try VBVirtualMachine(bundleURL: destinationURL, isNewInstall: false, createIfNeeded: false)
/// The copy is complete and usable at this point. Without its approvals it only asks for them to be reviewed,
/// which is not worth failing the duplication for.
do {
let original = try VBVirtualMachine(bundleURL: sourceURL, isNewInstall: false, createIfNeeded: false)
try carryApprovals(original, duplicate)
} catch {
logger.error("Unable to carry approved settings over to \(destinationURL.lastPathComponent, privacy: .public): \(error.localizedDescription, privacy: .public)")
}
return duplicate
}
}