Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tunneld: Fix possible FD leaks during connection establish #1369

Merged
merged 1 commit into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 25 additions & 18 deletions pymobiledevice3/remote/remote_service_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,18 @@ def set_language(self, language: str) -> None:

async def connect(self) -> None:
await self.service.connect()
self.peer_info = await self.service.receive_response()
self.udid = self.peer_info['Properties']['UniqueDeviceID']
self.product_type = self.peer_info['Properties']['ProductType']
try:
self.lockdown = create_using_remote(self.start_lockdown_service('com.apple.mobile.lockdown.remote.trusted'))
except InvalidServiceError:
self.lockdown = create_using_remote(
self.start_lockdown_service('com.apple.mobile.lockdown.remote.untrusted'))
self.all_values = self.lockdown.all_values
self.peer_info = await self.service.receive_response()
self.udid = self.peer_info['Properties']['UniqueDeviceID']
self.product_type = self.peer_info['Properties']['ProductType']
try:
self.lockdown = create_using_remote(self.start_lockdown_service('com.apple.mobile.lockdown.remote.trusted'))
except InvalidServiceError:
self.lockdown = create_using_remote(
self.start_lockdown_service('com.apple.mobile.lockdown.remote.untrusted'))
self.all_values = self.lockdown.all_values
except Exception: # noqa: E722
await self.close()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reraise exception

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! :)


def get_value(self, domain: Optional[str] = None, key: Optional[str] = None) -> Any:
return self.lockdown.get_value(domain, key)
Expand All @@ -75,16 +78,20 @@ def start_lockdown_service_without_checkin(self, name: str) -> ServiceConnection

def start_lockdown_service(self, name: str, include_escrow_bag: bool = False) -> ServiceConnection:
service = self.start_lockdown_service_without_checkin(name)
checkin = {'Label': 'pymobiledevice3', 'ProtocolVersion': '2', 'Request': 'RSDCheckin'}
if include_escrow_bag:
pairing_record = get_local_pairing_record(get_remote_pairing_record_filename(self.udid), get_home_folder())
checkin['EscrowBag'] = base64.b64decode(pairing_record['remote_unlock_host_key'])
response = service.send_recv_plist(checkin)
if response['Request'] != 'RSDCheckin':
raise PyMobileDevice3Exception(f'Invalid response for RSDCheckIn: {response}. Expected "RSDCheckIn"')
response = service.recv_plist()
if response['Request'] != 'StartService':
raise PyMobileDevice3Exception(f'Invalid response for RSDCheckIn: {response}. Expected "ServiceService"')
try:
checkin = {'Label': 'pymobiledevice3', 'ProtocolVersion': '2', 'Request': 'RSDCheckin'}
if include_escrow_bag:
pairing_record = get_local_pairing_record(get_remote_pairing_record_filename(self.udid), get_home_folder())
checkin['EscrowBag'] = base64.b64decode(pairing_record['remote_unlock_host_key'])
response = service.send_recv_plist(checkin)
if response['Request'] != 'RSDCheckin':
raise PyMobileDevice3Exception(f'Invalid response for RSDCheckIn: {response}. Expected "RSDCheckIn"')
response = service.recv_plist()
if response['Request'] != 'StartService':
raise PyMobileDevice3Exception(f'Invalid response for RSDCheckIn: {response}. Expected "ServiceService"')
except Exception: # noqa: E722
service.close()
raise
return service

async def aio_start_lockdown_service(
Expand Down
6 changes: 5 additions & 1 deletion pymobiledevice3/remote/remotexpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ async def __aexit__(self, exc_type, exc_val, exc_tb) -> None:

async def connect(self) -> None:
self._reader, self._writer = await asyncio.open_connection(self.address[0], self.address[1])
await self._do_handshake()
try:
await self._do_handshake()
except Exception: # noqa: E722
await self.close()
raise

async def close(self) -> None:
if self._writer is None:
Expand Down
2 changes: 2 additions & 0 deletions pymobiledevice3/remote/tunnel_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,8 @@ async def create_core_device_tunnel_service_using_rsd(
service = CoreDeviceTunnelService(rsd)
try:
await service.connect(autopair=autopair)
except Exception: # noqa: E722
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This except block should be below RemotePairingCompletedError, and also reraise exception

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! :)

await service.close()
except RemotePairingCompletedError:
# The connection must be reestablished upon pairing is completed
await service.close()
Expand Down
Loading