[lxc-devel] [lxd/master] lxc/file: Follow symlinks on individual file transfers

stgraber on Github lxc-bot at linuxcontainers.org
Mon Feb 10 20:59:37 UTC 2020


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 301 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20200210/a5b9087c/attachment.bin>
-------------- next part --------------
From 844c70cfb5942a1dea97a3282b21b5819cfd0633 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Mon, 10 Feb 2020 15:58:38 -0500
Subject: [PATCH 1/2] shared: Add 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 | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/shared/util.go b/shared/util.go
index 43b11b495c..ffaa750b9d 100644
--- a/shared/util.go
+++ b/shared/util.go
@@ -106,6 +106,23 @@ func IsUnixSocket(path string) bool {
 	return (stat.Mode() & os.ModeSocket) == os.ModeSocket
 }
 
+// HostPathFollow takes a valid path (from HostPath) and resolves it
+// all the way to its target or to the last which can be resolved.
+func HostPathFollow(path string) string {
+	for {
+		target, err := os.Readlink(path)
+		if err != nil {
+			return path
+		}
+
+		if target == path {
+			return path
+		}
+
+		path = HostPath(target)
+	}
+}
+
 // HostPath returns the host path for the provided path
 // On a normal system, this does nothing
 // When inside of a snap environment, returns the real path

From 0a33b7065266b1bd9b5c6c8e7297eec273fbdbcb Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Mon, 10 Feb 2020 15:59:10 -0500
Subject: [PATCH 2/2] lxc/file: Follow symlinks on individual file transfers
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>
---
 lxc/file.go | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/lxc/file.go b/lxc/file.go
index 83c5255f1c..aa7167a84b 100644
--- a/lxc/file.go
+++ b/lxc/file.go
@@ -535,6 +535,9 @@ func (c *cmdFilePush) Run(cmd *cobra.Command, args []string) error {
 		if f == "-" {
 			file = os.Stdin
 		} else {
+			// Follow symlinks within the snap environment.
+			f = shared.HostPathFollow(f)
+
 			file, err = os.Open(f)
 			if err != nil {
 				return err


More information about the lxc-devel mailing list