[lxc-devel] [lxd/master] shared/util: Fix relative paths in HostPathFollow

stgraber on Github lxc-bot at linuxcontainers.org
Tue Feb 18 18:56:07 UTC 2020


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 354 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20200218/f95288ea/attachment.bin>
-------------- next part --------------
From 2803e499ea4a15a20f304498446b662543f5d8d1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Tue, 18 Feb 2020 13:55:53 -0500
Subject: [PATCH] shared/util: Fix relative paths in HostPathFollow
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: St├ęphane Graber <stgraber at ubuntu.com>
---
 shared/util.go | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/shared/util.go b/shared/util.go
index ae9da6d281..4813aa62f4 100644
--- a/shared/util.go
+++ b/shared/util.go
@@ -126,6 +126,22 @@ func HostPathFollow(path string) string {
 		return path
 	}
 
+	// Handle relative paths
+	if path[0] != os.PathSeparator {
+		// Use the cwd of the parent as snap-confine alters our own cwd on launch
+		ppid := os.Getppid()
+		if ppid < 1 {
+			return path
+		}
+
+		pwd, err := os.Readlink(fmt.Sprintf("/proc/%d/cwd", ppid))
+		if err != nil {
+			return path
+		}
+
+		path = filepath.Clean(strings.Join([]string{pwd, path}, string(os.PathSeparator)))
+	}
+
 	// Rely on "readlink -m" to do the right thing.
 	for {
 		target, err := RunCommand("readlink", "-m", path)


More information about the lxc-devel mailing list