Amebis / eduVPN

Windows eduVPN Client
GNU General Public License v3.0
41 stars 16 forks source link

TunnelFailoverTest: int overflow leads to ArgumentOutOfRangeException #244

Closed bbrauns closed 5 months ago

bbrauns commented 5 months ago

Hey everyone,

My workstation is on Windows 11 and the client unexpectedly crashed with an ArgumentOutOfRangeException. Windows EventView showed:

Anwendung: eduVPN.Client.exe
Frameworkversion: v4.0.30319
Beschreibung: Der Prozess wurde aufgrund einer unbehandelten Ausnahme beendet.
Ausnahmeinformationen: System.ArgumentOutOfRangeException
   bei System.Net.IPAddress..ctor(Int64)
   bei eduVPN.ViewModels.VPN.Session.<.ctor>b__77_1()
   bei System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   bei System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   bei System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
   bei System.Threading.ThreadHelper.ThreadStart()

The issue seems to be that casting tunnelAddress.Address to int can result in an overflow. See image below. Aside from that IPAddress.Adress is marked as obsolete and should be replaced by GetAddressBytes [1].

image

I am not familiar enough with the calculation of IP addresses and subnet masks, but replacing line 444 with something like the following should help:

 var addressBytes = tunnelAddress.GetAddressBytes();
 var unicastMaskBytes = unicast.IPv4Mask.GetAddressBytes();
 var maskBytes = new byte[] { 0, 0, 0, 1 };
 for (var i = 0; i < addressBytes.Length; i++)
 {
     addressBytes[i] = (byte)(addressBytes[i] & unicastMaskBytes[i]);
 }
 for (var i = 0; i < addressBytes.Length; i++)
 {
     addressBytes[i] = (byte)(addressBytes[i] | maskBytes[i]);
 }

 gateway = new IPAddress(addressBytes).ToString();

[1] https://learn.microsoft.com/en-us/dotnet/api/system.net.ipaddress.getaddressbytes?view=net-8.0

sklemer1 commented 5 months ago

The exception occured for IP 10.45.84.132/26.

rozmansi commented 5 months ago

Thanks for reporting this. The problem is sign bit 1 from 132 in 10.45.84.132. (calculation was done in host byte order, so the last component of IP became MSB).