From 6a7c8c2a2592e8bef585e72f1c31021179113baf Mon Sep 17 00:00:00 2001 From: Chris Abernethy Date: Thu, 25 Jun 2015 08:29:44 -0400 Subject: [PATCH] Additional validation on virtualbox-hostonly-cidr Check that the CIDR provided for a virtualbox host only CIDR is specified as a host IP and netmask, e.g., 192.168.100.1/24, and not a network IP and netmask, e.g., 192.168.100.0/24. This will help prevent confusion like #1383 Signed-off-by: Chris Abernethy Signed-off-by: David Gageot --- drivers/virtualbox/virtualbox.go | 21 +++++++++++++++++---- drivers/virtualbox/virtualbox_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/drivers/virtualbox/virtualbox.go b/drivers/virtualbox/virtualbox.go index 25e5ad3826..b0c149823e 100644 --- a/drivers/virtualbox/virtualbox.go +++ b/drivers/virtualbox/virtualbox.go @@ -45,6 +45,7 @@ const ( var ( ErrUnableToGenerateRandomIP = errors.New("unable to generate random IP") ErrMustEnableVTX = errors.New("This computer doesn't have VT-X/AMD-v enabled. Enabling it in the BIOS is mandatory.") + ErrNetworkAddrCidr = errors.New("host-only cidr must be specified with a host address, not a network address") ) type Driver struct { @@ -658,19 +659,17 @@ func (d *Driver) setupHostOnlyNetwork(machineName string) error { hostOnlyCIDR = defaultHostOnlyCIDR } - ip, network, err := net.ParseCIDR(hostOnlyCIDR) - + ip, network, err := parseAndValidateCIDR(hostOnlyCIDR) if err != nil { return err } - nAddr := network.IP.To4() - dhcpAddr, err := getRandomIPinSubnet(network.IP) if err != nil { return err } + nAddr := network.IP.To4() lowerDHCPIP := net.IPv4(nAddr[0], nAddr[1], nAddr[2], byte(100)) upperDHCPIP := net.IPv4(nAddr[0], nAddr[1], nAddr[2], byte(254)) @@ -695,6 +694,20 @@ func (d *Driver) setupHostOnlyNetwork(machineName string) error { "--cableconnected2", "on") } +func parseAndValidateCIDR(hostOnlyCIDR string) (net.IP, *net.IPNet, error) { + ip, network, err := net.ParseCIDR(hostOnlyCIDR) + if err != nil { + return nil, nil, err + } + + networkAddress := network.IP.To4() + if ip.Equal(networkAddress) { + return nil, nil, ErrNetworkAddrCidr + } + + return ip, network, nil +} + // createDiskImage makes a disk image at dest with the given size in MB. If r is // not nil, it will be read as a raw disk image to convert from. func createDiskImage(dest string, size int, r io.Reader) error { diff --git a/drivers/virtualbox/virtualbox_test.go b/drivers/virtualbox/virtualbox_test.go index 3fb2c7e60d..eb42fa0079 100644 --- a/drivers/virtualbox/virtualbox_test.go +++ b/drivers/virtualbox/virtualbox_test.go @@ -152,6 +152,31 @@ func TestGetIPErrors(t *testing.T) { } } +func TestParseValidCIDR(t *testing.T) { + ip, network, err := parseAndValidateCIDR("192.168.100.1/24") + + assert.Equal(t, "192.168.100.1", ip.String()) + assert.Equal(t, "192.168.100.0", network.IP.String()) + assert.Equal(t, "ffffff00", network.Mask.String()) + assert.NoError(t, err) +} + +func TestInvalidCIDR(t *testing.T) { + ip, network, err := parseAndValidateCIDR("192.168.100.1") + + assert.EqualError(t, err, "invalid CIDR address: 192.168.100.1") + assert.Nil(t, ip) + assert.Nil(t, network) +} + +func TestInvalidNetworkIpCIDR(t *testing.T) { + ip, network, err := parseAndValidateCIDR("192.168.100.0/24") + + assert.Equal(t, ErrNetworkAddrCidr, err) + assert.Nil(t, ip) + assert.Nil(t, network) +} + func newTestDriver(name string) *Driver { return NewDriver(name, "") }