WireGuardKit: Only assign self.settingsGenerator upon success to set tunnel network settings to avoid inconsistent state

Signed-off-by: Andrej Mihajlov <and@mullvad.net>
This commit is contained in:
Andrej Mihajlov 2020-12-02 13:53:58 +01:00
parent 2e356d3d8f
commit b34625f511
1 changed files with 5 additions and 1 deletions

View File

@ -263,7 +263,6 @@ public class WireGuardAdapter {
let settingsGenerator = PacketTunnelSettingsGenerator(tunnelConfiguration: tunnelConfiguration, resolvedEndpoints: resolvedEndpoints) let settingsGenerator = PacketTunnelSettingsGenerator(tunnelConfiguration: tunnelConfiguration, resolvedEndpoints: resolvedEndpoints)
let networkSettings = settingsGenerator.generateNetworkSettings() let networkSettings = settingsGenerator.generateNetworkSettings()
self.settingsGenerator = settingsGenerator
var systemError: Error? var systemError: Error?
let condition = NSCondition() let condition = NSCondition()
@ -284,6 +283,11 @@ public class WireGuardAdapter {
if condition.wait(until: Date().addingTimeInterval(setTunnelNetworkSettingsTimeout)) { if condition.wait(until: Date().addingTimeInterval(setTunnelNetworkSettingsTimeout)) {
let returnError = systemError.map { WireGuardAdapterError.setNetworkSettings($0) } let returnError = systemError.map { WireGuardAdapterError.setNetworkSettings($0) }
// Only assign `settingsGenerator` when `setTunnelNetworkSettings` succeeded.
if returnError == nil {
self.settingsGenerator = settingsGenerator
}
completionHandler(settingsGenerator, returnError) completionHandler(settingsGenerator, returnError)
} else { } else {
completionHandler(nil, .setNetworkSettingsTimeout) completionHandler(nil, .setNetworkSettingsTimeout)