[lxc-devel] [lxd/master] Fix a panic in 'remote add'

sean-jc on Github lxc-bot at linuxcontainers.org
Wed Jun 8 15:09:13 UTC 2016


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 805 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20160608/b8f4e9d5/attachment.bin>
-------------- next part --------------
From a834084fd1b9a492e1321effc71283ca7a0564a6 Mon Sep 17 00:00:00 2001
From: Sean Christopherson <sean.j.christopherson at intel.com>
Date: Wed, 8 Jun 2016 07:59:17 -0700
Subject: [PATCH] Fix a panic in 'remote add'

Use strings.TrimPrefix to strip 'unix://' from the URL/path in both
remoteCmd.addServer() and connectViaUnix.  This resolves the direct
cause of the panic and makes the handling of relative paths consistent
in both flows.

Add a check for IsDir when determining whether HTTPS or UNIX should
be assumed.  The UNIX scheme requires a full path to the UNIX socket,
i.e. a directory can never be a valid LXD server.

Fixes #2089

Signed-off-by: Sean Christopherson <sean.j.christopherson at intel.com>
---
 client.go     |  6 +-----
 lxc/remote.go | 16 +++-------------
 2 files changed, 4 insertions(+), 18 deletions(-)

diff --git a/client.go b/client.go
index 59c80ad..3f3e8bf 100644
--- a/client.go
+++ b/client.go
@@ -255,11 +255,7 @@ func connectViaUnix(c *Client, remote *RemoteConfig) error {
 		//   unix:///path/to/socket
 		//   unix:/path/to/socket
 		//   unix:path/to/socket
-		path := strings.TrimPrefix(remote.Addr, "unix:")
-		if strings.HasPrefix(path, "///") {
-			// translate unix:///path/to, to just "/path/to"
-			path = path[2:]
-		}
+		path := strings.TrimPrefix(strings.TrimPrefix(remote.Addr, "unix:"), "//")
 		raddr, err := net.ResolveUnixAddr("unix", path)
 		if err != nil {
 			return nil, err
diff --git a/lxc/remote.go b/lxc/remote.go
index df75e8a..a159ce5 100644
--- a/lxc/remote.go
+++ b/lxc/remote.go
@@ -126,7 +126,7 @@ func (c *remoteCmd) addServer(config *lxd.Config, server string, addr string, ac
 	} else if addr[0] == '/' {
 		rScheme = "unix"
 	} else {
-		if !shared.PathExists(addr) {
+		if !shared.PathExists(addr) || shared.IsDir(addr) {
 			rScheme = "https"
 		} else {
 			rScheme = "unix"
@@ -148,17 +148,7 @@ func (c *remoteCmd) addServer(config *lxd.Config, server string, addr string, ac
 	}
 
 	if rScheme == "unix" {
-		if addr[0:5] == "unix:" {
-			if addr[0:7] == "unix://" {
-				if len(addr) > 8 {
-					rHost = addr[8:]
-				} else {
-					rHost = ""
-				}
-			} else {
-				rHost = addr[6:]
-			}
-		}
+		rHost = strings.TrimPrefix(strings.TrimPrefix(addr, "unix:"), "//")
 		rPort = ""
 	}
 
@@ -181,7 +171,7 @@ func (c *remoteCmd) addServer(config *lxd.Config, server string, addr string, ac
 		return err
 	}
 
-	if len(addr) > 5 && addr[0:5] == "unix:" {
+	if strings.HasPrefix(addr, "unix:") {
 		// NewClient succeeded so there was a lxd there (we fingered
 		// it) so just accept it
 		return nil


More information about the lxc-devel mailing list