Files
VirtualBuddy/VirtualCore/Source/Utilities/HostSettingsApprovalStore.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

61 lines
2.6 KiB
Swift

import Foundation
import LocalAuthentication
import Security
/// Remembers which setting records the host user approved, in the host app's Keychain rather than in any bundle.
///
/// Items are per-user, device-local and in the host app's default Keychain access group, so nothing
/// that can only write to a bundle can approve what it wrote.
struct HostSettingsApprovalStore {
var service = "codes.rambo.VirtualBuddy.settings-approvals"
private func item(_ account: String) -> [String: Any] {
[
kSecClass as String: kSecClassGenericPassword,
kSecUseDataProtectionKeychain as String: true,
kSecAttrSynchronizable as String: false,
kSecAttrService as String: service,
kSecAttrAccount as String: account
]
}
func digest(for account: String) throws -> Data? {
var query = item(account)
let context = LAContext()
context.interactionNotAllowed = true
query[kSecUseAuthenticationContext as String] = context
query[kSecReturnData as String] = true
query[kSecMatchLimit as String] = kSecMatchLimitOne
var result: CFTypeRef?
let status = SecItemCopyMatching(query as CFDictionary, &result)
if status == errSecItemNotFound { return nil }
try check(status)
guard let digest = result as? Data else { throw Failure("The saved settings approval is invalid.") }
return digest
}
func record(_ digest: Data, for account: String) throws {
let value = [kSecValueData as String: digest]
var status = SecItemUpdate(item(account) as CFDictionary, value as CFDictionary)
if status == errSecItemNotFound {
var new = item(account)
new[kSecAttrAccessible as String] = kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly
new[kSecAttrLabel as String] = "VirtualBuddy settings approval"
new[kSecValueData as String] = digest
status = SecItemAdd(new as CFDictionary, nil)
// A concurrent approval of the same bundle added the item first. The latest approval wins.
if status == errSecDuplicateItem {
status = SecItemUpdate(item(account) as CFDictionary, value as CFDictionary)
}
}
try check(status)
}
private func check(_ status: OSStatus) throws {
guard status == errSecSuccess else {
let detail = SecCopyErrorMessageString(status, nil) as String? ?? "OSStatus \(status)"
throw Failure("Unable to access the saved settings approval. \(detail)")
}
}
}