From 6238f1550b82162a2e9e5bb178c458684de9a68d Mon Sep 17 00:00:00 2001 From: Brian Luong Date: Fri, 18 Sep 2020 07:10:25 -0700 Subject: [PATCH] Handle panic when invalid IP entered in sshd (#296) --- ssh.go | 42 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/ssh.go b/ssh.go index 03e8422..9e409bc 100644 --- a/ssh.go +++ b/ssh.go @@ -463,7 +463,12 @@ func sshQueryLighthouse(ifce *Interface, fs interface{}, a []string, w sshd.Stri return w.WriteLine("No vpn ip was provided") } - vpnIp := ip2int(net.ParseIP(a[0])) + parsedIp := net.ParseIP(a[0]) + if parsedIp == nil { + return w.WriteLine(fmt.Sprintf("The provided vpn ip could not be parsed: %s", a[0])) + } + + vpnIp := ip2int(parsedIp) if vpnIp == 0 { return w.WriteLine(fmt.Sprintf("The provided vpn ip could not be parsed: %s", a[0])) } @@ -483,7 +488,12 @@ func sshCloseTunnel(ifce *Interface, fs interface{}, a []string, w sshd.StringWr return w.WriteLine("No vpn ip was provided") } - vpnIp := ip2int(net.ParseIP(a[0])) + parsedIp := net.ParseIP(a[0]) + if parsedIp == nil { + return w.WriteLine(fmt.Sprintf("The provided vpn ip could not be parsed: %s", a[0])) + } + + vpnIp := ip2int(parsedIp) if vpnIp == 0 { return w.WriteLine(fmt.Sprintf("The provided vpn ip could not be parsed: %s", a[0])) } @@ -521,7 +531,12 @@ func sshCreateTunnel(ifce *Interface, fs interface{}, a []string, w sshd.StringW return w.WriteLine("No vpn ip was provided") } - vpnIp := ip2int(net.ParseIP(a[0])) + parsedIp := net.ParseIP(a[0]) + if parsedIp == nil { + return w.WriteLine(fmt.Sprintf("The provided vpn ip could not be parsed: %s", a[0])) + } + + vpnIp := ip2int(parsedIp) if vpnIp == 0 { return w.WriteLine(fmt.Sprintf("The provided vpn ip could not be parsed: %s", a[0])) } @@ -573,7 +588,12 @@ func sshChangeRemote(ifce *Interface, fs interface{}, a []string, w sshd.StringW return w.WriteLine("Address could not be parsed") } - vpnIp := ip2int(net.ParseIP(a[0])) + parsedIp := net.ParseIP(a[0]) + if parsedIp == nil { + return w.WriteLine(fmt.Sprintf("The provided vpn ip could not be parsed: %s", a[0])) + } + + vpnIp := ip2int(parsedIp) if vpnIp == 0 { return w.WriteLine(fmt.Sprintf("The provided vpn ip could not be parsed: %s", a[0])) } @@ -649,7 +669,12 @@ func sshPrintCert(ifce *Interface, fs interface{}, a []string, w sshd.StringWrit cert := ifce.certState.certificate if len(a) > 0 { - vpnIp := ip2int(net.ParseIP(a[0])) + parsedIp := net.ParseIP(a[0]) + if parsedIp == nil { + return w.WriteLine(fmt.Sprintf("The provided vpn ip could not be parsed: %s", a[0])) + } + + vpnIp := ip2int(parsedIp) if vpnIp == 0 { return w.WriteLine(fmt.Sprintf("The provided vpn ip could not be parsed: %s", a[0])) } @@ -696,7 +721,12 @@ func sshPrintTunnel(ifce *Interface, fs interface{}, a []string, w sshd.StringWr return w.WriteLine("No vpn ip was provided") } - vpnIp := ip2int(net.ParseIP(a[0])) + parsedIp := net.ParseIP(a[0]) + if parsedIp == nil { + return w.WriteLine(fmt.Sprintf("The provided vpn ip could not be parsed: %s", a[0])) + } + + vpnIp := ip2int(parsedIp) if vpnIp == 0 { return w.WriteLine(fmt.Sprintf("The provided vpn ip could not be parsed: %s", a[0])) }