Browse code

replace pkg/symlink with github.com/moby/sys/symlink

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2020/10/30 08:54:10
Showing 22 changed files
... ...
@@ -6,7 +6,7 @@ import (
6 6
 	"strings"
7 7
 
8 8
 	"github.com/docker/docker/pkg/idtools"
9
-	"github.com/docker/docker/pkg/symlink"
9
+	"github.com/moby/sys/symlink"
10 10
 	lcUser "github.com/opencontainers/runc/libcontainer/user"
11 11
 	"github.com/pkg/errors"
12 12
 )
... ...
@@ -9,7 +9,7 @@ import (
9 9
 	"path/filepath"
10 10
 	"strings"
11 11
 
12
-	"github.com/docker/docker/pkg/symlink"
12
+	"github.com/moby/sys/symlink"
13 13
 	"github.com/pkg/errors"
14 14
 )
15 15
 
... ...
@@ -32,13 +32,13 @@ import (
32 32
 	"github.com/docker/docker/pkg/idtools"
33 33
 	"github.com/docker/docker/pkg/ioutils"
34 34
 	"github.com/docker/docker/pkg/signal"
35
-	"github.com/docker/docker/pkg/symlink"
36 35
 	"github.com/docker/docker/pkg/system"
37 36
 	"github.com/docker/docker/restartmanager"
38 37
 	"github.com/docker/docker/volume"
39 38
 	volumemounts "github.com/docker/docker/volume/mounts"
40 39
 	units "github.com/docker/go-units"
41 40
 	agentexec "github.com/docker/swarmkit/agent/exec"
41
+	"github.com/moby/sys/symlink"
42 42
 	"github.com/pkg/errors"
43 43
 	"github.com/sirupsen/logrus"
44 44
 )
... ...
@@ -22,8 +22,8 @@ import (
22 22
 	"github.com/docker/docker/pkg/progress"
23 23
 	"github.com/docker/docker/pkg/streamformatter"
24 24
 	"github.com/docker/docker/pkg/stringid"
25
-	"github.com/docker/docker/pkg/symlink"
26 25
 	"github.com/docker/docker/pkg/system"
26
+	"github.com/moby/sys/symlink"
27 27
 	digest "github.com/opencontainers/go-digest"
28 28
 	"github.com/sirupsen/logrus"
29 29
 )
... ...
@@ -6,7 +6,7 @@ import (
6 6
 
7 7
 	"github.com/containerd/continuity/driver"
8 8
 	"github.com/containerd/continuity/pathdriver"
9
-	"github.com/docker/docker/pkg/symlink"
9
+	"github.com/moby/sys/symlink"
10 10
 )
11 11
 
12 12
 // ContainerFS is that represents a root file system
13 13
deleted file mode 100644
... ...
@@ -1,191 +0,0 @@
1
-
2
-                                 Apache License
3
-                           Version 2.0, January 2004
4
-                        http://www.apache.org/licenses/
5
-
6
-   TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
-
8
-   1. Definitions.
9
-
10
-      "License" shall mean the terms and conditions for use, reproduction,
11
-      and distribution as defined by Sections 1 through 9 of this document.
12
-
13
-      "Licensor" shall mean the copyright owner or entity authorized by
14
-      the copyright owner that is granting the License.
15
-
16
-      "Legal Entity" shall mean the union of the acting entity and all
17
-      other entities that control, are controlled by, or are under common
18
-      control with that entity. For the purposes of this definition,
19
-      "control" means (i) the power, direct or indirect, to cause the
20
-      direction or management of such entity, whether by contract or
21
-      otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
-      outstanding shares, or (iii) beneficial ownership of such entity.
23
-
24
-      "You" (or "Your") shall mean an individual or Legal Entity
25
-      exercising permissions granted by this License.
26
-
27
-      "Source" form shall mean the preferred form for making modifications,
28
-      including but not limited to software source code, documentation
29
-      source, and configuration files.
30
-
31
-      "Object" form shall mean any form resulting from mechanical
32
-      transformation or translation of a Source form, including but
33
-      not limited to compiled object code, generated documentation,
34
-      and conversions to other media types.
35
-
36
-      "Work" shall mean the work of authorship, whether in Source or
37
-      Object form, made available under the License, as indicated by a
38
-      copyright notice that is included in or attached to the work
39
-      (an example is provided in the Appendix below).
40
-
41
-      "Derivative Works" shall mean any work, whether in Source or Object
42
-      form, that is based on (or derived from) the Work and for which the
43
-      editorial revisions, annotations, elaborations, or other modifications
44
-      represent, as a whole, an original work of authorship. For the purposes
45
-      of this License, Derivative Works shall not include works that remain
46
-      separable from, or merely link (or bind by name) to the interfaces of,
47
-      the Work and Derivative Works thereof.
48
-
49
-      "Contribution" shall mean any work of authorship, including
50
-      the original version of the Work and any modifications or additions
51
-      to that Work or Derivative Works thereof, that is intentionally
52
-      submitted to Licensor for inclusion in the Work by the copyright owner
53
-      or by an individual or Legal Entity authorized to submit on behalf of
54
-      the copyright owner. For the purposes of this definition, "submitted"
55
-      means any form of electronic, verbal, or written communication sent
56
-      to the Licensor or its representatives, including but not limited to
57
-      communication on electronic mailing lists, source code control systems,
58
-      and issue tracking systems that are managed by, or on behalf of, the
59
-      Licensor for the purpose of discussing and improving the Work, but
60
-      excluding communication that is conspicuously marked or otherwise
61
-      designated in writing by the copyright owner as "Not a Contribution."
62
-
63
-      "Contributor" shall mean Licensor and any individual or Legal Entity
64
-      on behalf of whom a Contribution has been received by Licensor and
65
-      subsequently incorporated within the Work.
66
-
67
-   2. Grant of Copyright License. Subject to the terms and conditions of
68
-      this License, each Contributor hereby grants to You a perpetual,
69
-      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
-      copyright license to reproduce, prepare Derivative Works of,
71
-      publicly display, publicly perform, sublicense, and distribute the
72
-      Work and such Derivative Works in Source or Object form.
73
-
74
-   3. Grant of Patent License. Subject to the terms and conditions of
75
-      this License, each Contributor hereby grants to You a perpetual,
76
-      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
-      (except as stated in this section) patent license to make, have made,
78
-      use, offer to sell, sell, import, and otherwise transfer the Work,
79
-      where such license applies only to those patent claims licensable
80
-      by such Contributor that are necessarily infringed by their
81
-      Contribution(s) alone or by combination of their Contribution(s)
82
-      with the Work to which such Contribution(s) was submitted. If You
83
-      institute patent litigation against any entity (including a
84
-      cross-claim or counterclaim in a lawsuit) alleging that the Work
85
-      or a Contribution incorporated within the Work constitutes direct
86
-      or contributory patent infringement, then any patent licenses
87
-      granted to You under this License for that Work shall terminate
88
-      as of the date such litigation is filed.
89
-
90
-   4. Redistribution. You may reproduce and distribute copies of the
91
-      Work or Derivative Works thereof in any medium, with or without
92
-      modifications, and in Source or Object form, provided that You
93
-      meet the following conditions:
94
-
95
-      (a) You must give any other recipients of the Work or
96
-          Derivative Works a copy of this License; and
97
-
98
-      (b) You must cause any modified files to carry prominent notices
99
-          stating that You changed the files; and
100
-
101
-      (c) You must retain, in the Source form of any Derivative Works
102
-          that You distribute, all copyright, patent, trademark, and
103
-          attribution notices from the Source form of the Work,
104
-          excluding those notices that do not pertain to any part of
105
-          the Derivative Works; and
106
-
107
-      (d) If the Work includes a "NOTICE" text file as part of its
108
-          distribution, then any Derivative Works that You distribute must
109
-          include a readable copy of the attribution notices contained
110
-          within such NOTICE file, excluding those notices that do not
111
-          pertain to any part of the Derivative Works, in at least one
112
-          of the following places: within a NOTICE text file distributed
113
-          as part of the Derivative Works; within the Source form or
114
-          documentation, if provided along with the Derivative Works; or,
115
-          within a display generated by the Derivative Works, if and
116
-          wherever such third-party notices normally appear. The contents
117
-          of the NOTICE file are for informational purposes only and
118
-          do not modify the License. You may add Your own attribution
119
-          notices within Derivative Works that You distribute, alongside
120
-          or as an addendum to the NOTICE text from the Work, provided
121
-          that such additional attribution notices cannot be construed
122
-          as modifying the License.
123
-
124
-      You may add Your own copyright statement to Your modifications and
125
-      may provide additional or different license terms and conditions
126
-      for use, reproduction, or distribution of Your modifications, or
127
-      for any such Derivative Works as a whole, provided Your use,
128
-      reproduction, and distribution of the Work otherwise complies with
129
-      the conditions stated in this License.
130
-
131
-   5. Submission of Contributions. Unless You explicitly state otherwise,
132
-      any Contribution intentionally submitted for inclusion in the Work
133
-      by You to the Licensor shall be under the terms and conditions of
134
-      this License, without any additional terms or conditions.
135
-      Notwithstanding the above, nothing herein shall supersede or modify
136
-      the terms of any separate license agreement you may have executed
137
-      with Licensor regarding such Contributions.
138
-
139
-   6. Trademarks. This License does not grant permission to use the trade
140
-      names, trademarks, service marks, or product names of the Licensor,
141
-      except as required for reasonable and customary use in describing the
142
-      origin of the Work and reproducing the content of the NOTICE file.
143
-
144
-   7. Disclaimer of Warranty. Unless required by applicable law or
145
-      agreed to in writing, Licensor provides the Work (and each
146
-      Contributor provides its Contributions) on an "AS IS" BASIS,
147
-      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
-      implied, including, without limitation, any warranties or conditions
149
-      of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
-      PARTICULAR PURPOSE. You are solely responsible for determining the
151
-      appropriateness of using or redistributing the Work and assume any
152
-      risks associated with Your exercise of permissions under this License.
153
-
154
-   8. Limitation of Liability. In no event and under no legal theory,
155
-      whether in tort (including negligence), contract, or otherwise,
156
-      unless required by applicable law (such as deliberate and grossly
157
-      negligent acts) or agreed to in writing, shall any Contributor be
158
-      liable to You for damages, including any direct, indirect, special,
159
-      incidental, or consequential damages of any character arising as a
160
-      result of this License or out of the use or inability to use the
161
-      Work (including but not limited to damages for loss of goodwill,
162
-      work stoppage, computer failure or malfunction, or any and all
163
-      other commercial damages or losses), even if such Contributor
164
-      has been advised of the possibility of such damages.
165
-
166
-   9. Accepting Warranty or Additional Liability. While redistributing
167
-      the Work or Derivative Works thereof, You may choose to offer,
168
-      and charge a fee for, acceptance of support, warranty, indemnity,
169
-      or other liability obligations and/or rights consistent with this
170
-      License. However, in accepting such obligations, You may act only
171
-      on Your own behalf and on Your sole responsibility, not on behalf
172
-      of any other Contributor, and only if You agree to indemnify,
173
-      defend, and hold each Contributor harmless for any liability
174
-      incurred by, or claims asserted against, such Contributor by reason
175
-      of your accepting any such warranty or additional liability.
176
-
177
-   END OF TERMS AND CONDITIONS
178
-
179
-   Copyright 2014-2018 Docker, Inc.
180
-
181
-   Licensed under the Apache License, Version 2.0 (the "License");
182
-   you may not use this file except in compliance with the License.
183
-   You may obtain a copy of the License at
184
-
185
-       http://www.apache.org/licenses/LICENSE-2.0
186
-
187
-   Unless required by applicable law or agreed to in writing, software
188
-   distributed under the License is distributed on an "AS IS" BASIS,
189
-   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
190
-   See the License for the specific language governing permissions and
191
-   limitations under the License.
192 1
deleted file mode 100644
... ...
@@ -1,27 +0,0 @@
1
-Copyright (c) 2014-2018 The Docker & Go Authors. All rights reserved.
2
-
3
-Redistribution and use in source and binary forms, with or without
4
-modification, are permitted provided that the following conditions are
5
-met:
6
-
7
-   * Redistributions of source code must retain the above copyright
8
-notice, this list of conditions and the following disclaimer.
9
-   * Redistributions in binary form must reproduce the above
10
-copyright notice, this list of conditions and the following disclaimer
11
-in the documentation and/or other materials provided with the
12
-distribution.
13
-   * Neither the name of Google Inc. nor the names of its
14
-contributors may be used to endorse or promote products derived from
15
-this software without specific prior written permission.
16
-
17
-THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18
-"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19
-LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20
-A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21
-OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22
-SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23
-LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24
-DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25
-THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
-(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27
-OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 1
deleted file mode 100644
... ...
@@ -1,6 +0,0 @@
1
-Package symlink implements EvalSymlinksInScope which is an extension of filepath.EvalSymlinks,
2
-as well as a Windows long-path aware version of filepath.EvalSymlinks
3
-from the [Go standard library](https://golang.org/pkg/path/filepath).
4
-
5
-The code from filepath.EvalSymlinks has been adapted in fs.go.
6
-Please read the LICENSE.BSD file that governs fs.go and LICENSE.APACHE for fs_test.go.
7 1
new file mode 100644
... ...
@@ -0,0 +1,12 @@
0
+package symlink // import "github.com/docker/docker/pkg/symlink"
1
+
2
+import "github.com/moby/sys/symlink"
3
+
4
+var (
5
+	// EvalSymlinks is deprecated and moved to github.com/moby/sys/symlink
6
+	// Deprecated: use github.com/moby/sys/symlink.EvalSymlinks instead
7
+	EvalSymlinks = symlink.EvalSymlinks
8
+	// FollowSymlinkInScope is deprecated and moved to github.com/moby/sys/symlink
9
+	// Deprecated: use github.com/moby/sys/symlink.FollowSymlinkInScope instead
10
+	FollowSymlinkInScope = symlink.FollowSymlinkInScope
11
+)
0 12
deleted file mode 100644
... ...
@@ -1,142 +0,0 @@
1
-// Copyright 2012 The Go Authors. All rights reserved.
2
-// Use of this source code is governed by a BSD-style
3
-// license that can be found in the LICENSE.BSD file.
4
-
5
-// This code is a modified version of path/filepath/symlink.go from the Go standard library.
6
-
7
-package symlink // import "github.com/docker/docker/pkg/symlink"
8
-
9
-import (
10
-	"bytes"
11
-	"errors"
12
-	"os"
13
-	"path/filepath"
14
-	"strings"
15
-)
16
-
17
-// FollowSymlinkInScope is a wrapper around evalSymlinksInScope that returns an
18
-// absolute path. This function handles paths in a platform-agnostic manner.
19
-func FollowSymlinkInScope(path, root string) (string, error) {
20
-	path, err := filepath.Abs(filepath.FromSlash(path))
21
-	if err != nil {
22
-		return "", err
23
-	}
24
-	root, err = filepath.Abs(filepath.FromSlash(root))
25
-	if err != nil {
26
-		return "", err
27
-	}
28
-	return evalSymlinksInScope(path, root)
29
-}
30
-
31
-// evalSymlinksInScope will evaluate symlinks in `path` within a scope `root` and return
32
-// a result guaranteed to be contained within the scope `root`, at the time of the call.
33
-// Symlinks in `root` are not evaluated and left as-is.
34
-// Errors encountered while attempting to evaluate symlinks in path will be returned.
35
-// Non-existing paths are valid and do not constitute an error.
36
-// `path` has to contain `root` as a prefix, or else an error will be returned.
37
-// Trying to break out from `root` does not constitute an error.
38
-//
39
-// Example:
40
-//   If /foo/bar -> /outside,
41
-//   FollowSymlinkInScope("/foo/bar", "/foo") == "/foo/outside" instead of "/outside"
42
-//
43
-// IMPORTANT: it is the caller's responsibility to call evalSymlinksInScope *after* relevant symlinks
44
-// are created and not to create subsequently, additional symlinks that could potentially make a
45
-// previously-safe path, unsafe. Example: if /foo/bar does not exist, evalSymlinksInScope("/foo/bar", "/foo")
46
-// would return "/foo/bar". If one makes /foo/bar a symlink to /baz subsequently, then "/foo/bar" should
47
-// no longer be considered safely contained in "/foo".
48
-func evalSymlinksInScope(path, root string) (string, error) {
49
-	root = filepath.Clean(root)
50
-	if path == root {
51
-		return path, nil
52
-	}
53
-	if !strings.HasPrefix(path, root) {
54
-		return "", errors.New("evalSymlinksInScope: " + path + " is not in " + root)
55
-	}
56
-	const maxIter = 255
57
-	originalPath := path
58
-	// given root of "/a" and path of "/a/b/../../c" we want path to be "/b/../../c"
59
-	path = path[len(root):]
60
-	if root == string(filepath.Separator) {
61
-		path = string(filepath.Separator) + path
62
-	}
63
-	if !strings.HasPrefix(path, string(filepath.Separator)) {
64
-		return "", errors.New("evalSymlinksInScope: " + path + " is not in " + root)
65
-	}
66
-	path = filepath.Clean(path)
67
-	// consume path by taking each frontmost path element,
68
-	// expanding it if it's a symlink, and appending it to b
69
-	var b bytes.Buffer
70
-	// b here will always be considered to be the "current absolute path inside
71
-	// root" when we append paths to it, we also append a slash and use
72
-	// filepath.Clean after the loop to trim the trailing slash
73
-	for n := 0; path != ""; n++ {
74
-		if n > maxIter {
75
-			return "", errors.New("evalSymlinksInScope: too many links in " + originalPath)
76
-		}
77
-
78
-		// find next path component, p
79
-		i := strings.IndexRune(path, filepath.Separator)
80
-		var p string
81
-		if i == -1 {
82
-			p, path = path, ""
83
-		} else {
84
-			p, path = path[:i], path[i+1:]
85
-		}
86
-
87
-		if p == "" {
88
-			continue
89
-		}
90
-
91
-		// this takes a b.String() like "b/../" and a p like "c" and turns it
92
-		// into "/b/../c" which then gets filepath.Cleaned into "/c" and then
93
-		// root gets prepended and we Clean again (to remove any trailing slash
94
-		// if the first Clean gave us just "/")
95
-		cleanP := filepath.Clean(string(filepath.Separator) + b.String() + p)
96
-		if isDriveOrRoot(cleanP) {
97
-			// never Lstat "/" itself, or drive letters on Windows
98
-			b.Reset()
99
-			continue
100
-		}
101
-		fullP := filepath.Clean(root + cleanP)
102
-
103
-		fi, err := os.Lstat(fullP)
104
-		if os.IsNotExist(err) {
105
-			// if p does not exist, accept it
106
-			b.WriteString(p)
107
-			b.WriteRune(filepath.Separator)
108
-			continue
109
-		}
110
-		if err != nil {
111
-			return "", err
112
-		}
113
-		if fi.Mode()&os.ModeSymlink == 0 {
114
-			b.WriteString(p)
115
-			b.WriteRune(filepath.Separator)
116
-			continue
117
-		}
118
-
119
-		// it's a symlink, put it at the front of path
120
-		dest, err := os.Readlink(fullP)
121
-		if err != nil {
122
-			return "", err
123
-		}
124
-		if isAbs(dest) {
125
-			b.Reset()
126
-		}
127
-		path = dest + string(filepath.Separator) + path
128
-	}
129
-
130
-	// see note above on "fullP := ..." for why this is double-cleaned and
131
-	// what's happening here
132
-	return filepath.Clean(root + filepath.Clean(string(filepath.Separator)+b.String())), nil
133
-}
134
-
135
-// EvalSymlinks returns the path name after the evaluation of any symbolic
136
-// links.
137
-// If path is relative the result will be relative to the current directory,
138
-// unless one of the components is an absolute symbolic link.
139
-// This version has been updated to support long paths prepended with `\\?\`.
140
-func EvalSymlinks(path string) (string, error) {
141
-	return evalSymlinks(path)
142
-}
143 1
deleted file mode 100644
... ...
@@ -1,17 +0,0 @@
1
-// +build !windows
2
-
3
-package symlink // import "github.com/docker/docker/pkg/symlink"
4
-
5
-import (
6
-	"path/filepath"
7
-)
8
-
9
-func evalSymlinks(path string) (string, error) {
10
-	return filepath.EvalSymlinks(path)
11
-}
12
-
13
-func isDriveOrRoot(p string) bool {
14
-	return p == string(filepath.Separator)
15
-}
16
-
17
-var isAbs = filepath.IsAbs
18 1
deleted file mode 100644
... ...
@@ -1,407 +0,0 @@
1
-// +build !windows
2
-
3
-// Licensed under the Apache License, Version 2.0; See LICENSE.APACHE
4
-
5
-package symlink // import "github.com/docker/docker/pkg/symlink"
6
-
7
-import (
8
-	"fmt"
9
-	"io/ioutil"
10
-	"os"
11
-	"path/filepath"
12
-	"testing"
13
-)
14
-
15
-// TODO Windows: This needs some serious work to port to Windows. For now,
16
-// turning off testing in this package.
17
-
18
-type dirOrLink struct {
19
-	path   string
20
-	target string
21
-}
22
-
23
-func makeFs(tmpdir string, fs []dirOrLink) error {
24
-	for _, s := range fs {
25
-		s.path = filepath.Join(tmpdir, s.path)
26
-		if s.target == "" {
27
-			os.MkdirAll(s.path, 0755)
28
-			continue
29
-		}
30
-		if err := os.MkdirAll(filepath.Dir(s.path), 0755); err != nil {
31
-			return err
32
-		}
33
-		if err := os.Symlink(s.target, s.path); err != nil && !os.IsExist(err) {
34
-			return err
35
-		}
36
-	}
37
-	return nil
38
-}
39
-
40
-func testSymlink(tmpdir, path, expected, scope string) error {
41
-	rewrite, err := FollowSymlinkInScope(filepath.Join(tmpdir, path), filepath.Join(tmpdir, scope))
42
-	if err != nil {
43
-		return err
44
-	}
45
-	expected, err = filepath.Abs(filepath.Join(tmpdir, expected))
46
-	if err != nil {
47
-		return err
48
-	}
49
-	if expected != rewrite {
50
-		return fmt.Errorf("Expected %q got %q", expected, rewrite)
51
-	}
52
-	return nil
53
-}
54
-
55
-func TestFollowSymlinkAbsolute(t *testing.T) {
56
-	tmpdir, err := ioutil.TempDir("", "TestFollowSymlinkAbsolute")
57
-	if err != nil {
58
-		t.Fatal(err)
59
-	}
60
-	defer os.RemoveAll(tmpdir)
61
-	if err := makeFs(tmpdir, []dirOrLink{{path: "testdata/fs/a/d", target: "/b"}}); err != nil {
62
-		t.Fatal(err)
63
-	}
64
-	if err := testSymlink(tmpdir, "testdata/fs/a/d/c/data", "testdata/b/c/data", "testdata"); err != nil {
65
-		t.Fatal(err)
66
-	}
67
-}
68
-
69
-func TestFollowSymlinkRelativePath(t *testing.T) {
70
-	tmpdir, err := ioutil.TempDir("", "TestFollowSymlinkRelativePath")
71
-	if err != nil {
72
-		t.Fatal(err)
73
-	}
74
-	defer os.RemoveAll(tmpdir)
75
-	if err := makeFs(tmpdir, []dirOrLink{{path: "testdata/fs/i", target: "a"}}); err != nil {
76
-		t.Fatal(err)
77
-	}
78
-	if err := testSymlink(tmpdir, "testdata/fs/i", "testdata/fs/a", "testdata"); err != nil {
79
-		t.Fatal(err)
80
-	}
81
-}
82
-
83
-func TestFollowSymlinkSkipSymlinksOutsideScope(t *testing.T) {
84
-	tmpdir, err := ioutil.TempDir("", "TestFollowSymlinkSkipSymlinksOutsideScope")
85
-	if err != nil {
86
-		t.Fatal(err)
87
-	}
88
-	defer os.RemoveAll(tmpdir)
89
-	if err := makeFs(tmpdir, []dirOrLink{
90
-		{path: "linkdir", target: "realdir"},
91
-		{path: "linkdir/foo/bar"},
92
-	}); err != nil {
93
-		t.Fatal(err)
94
-	}
95
-	if err := testSymlink(tmpdir, "linkdir/foo/bar", "linkdir/foo/bar", "linkdir/foo"); err != nil {
96
-		t.Fatal(err)
97
-	}
98
-}
99
-
100
-func TestFollowSymlinkInvalidScopePathPair(t *testing.T) {
101
-	if _, err := FollowSymlinkInScope("toto", "testdata"); err == nil {
102
-		t.Fatal("expected an error")
103
-	}
104
-}
105
-
106
-func TestFollowSymlinkLastLink(t *testing.T) {
107
-	tmpdir, err := ioutil.TempDir("", "TestFollowSymlinkLastLink")
108
-	if err != nil {
109
-		t.Fatal(err)
110
-	}
111
-	defer os.RemoveAll(tmpdir)
112
-	if err := makeFs(tmpdir, []dirOrLink{{path: "testdata/fs/a/d", target: "/b"}}); err != nil {
113
-		t.Fatal(err)
114
-	}
115
-	if err := testSymlink(tmpdir, "testdata/fs/a/d", "testdata/b", "testdata"); err != nil {
116
-		t.Fatal(err)
117
-	}
118
-}
119
-
120
-func TestFollowSymlinkRelativeLinkChangeScope(t *testing.T) {
121
-	tmpdir, err := ioutil.TempDir("", "TestFollowSymlinkRelativeLinkChangeScope")
122
-	if err != nil {
123
-		t.Fatal(err)
124
-	}
125
-	defer os.RemoveAll(tmpdir)
126
-	if err := makeFs(tmpdir, []dirOrLink{{path: "testdata/fs/a/e", target: "../b"}}); err != nil {
127
-		t.Fatal(err)
128
-	}
129
-	if err := testSymlink(tmpdir, "testdata/fs/a/e/c/data", "testdata/fs/b/c/data", "testdata"); err != nil {
130
-		t.Fatal(err)
131
-	}
132
-	// avoid letting allowing symlink e lead us to ../b
133
-	// normalize to the "testdata/fs/a"
134
-	if err := testSymlink(tmpdir, "testdata/fs/a/e", "testdata/fs/a/b", "testdata/fs/a"); err != nil {
135
-		t.Fatal(err)
136
-	}
137
-}
138
-
139
-func TestFollowSymlinkDeepRelativeLinkChangeScope(t *testing.T) {
140
-	tmpdir, err := ioutil.TempDir("", "TestFollowSymlinkDeepRelativeLinkChangeScope")
141
-	if err != nil {
142
-		t.Fatal(err)
143
-	}
144
-	defer os.RemoveAll(tmpdir)
145
-
146
-	if err := makeFs(tmpdir, []dirOrLink{{path: "testdata/fs/a/f", target: "../../../../test"}}); err != nil {
147
-		t.Fatal(err)
148
-	}
149
-	// avoid letting symlink f lead us out of the "testdata" scope
150
-	// we don't normalize because symlink f is in scope and there is no
151
-	// information leak
152
-	if err := testSymlink(tmpdir, "testdata/fs/a/f", "testdata/test", "testdata"); err != nil {
153
-		t.Fatal(err)
154
-	}
155
-	// avoid letting symlink f lead us out of the "testdata/fs" scope
156
-	// we don't normalize because symlink f is in scope and there is no
157
-	// information leak
158
-	if err := testSymlink(tmpdir, "testdata/fs/a/f", "testdata/fs/test", "testdata/fs"); err != nil {
159
-		t.Fatal(err)
160
-	}
161
-}
162
-
163
-func TestFollowSymlinkRelativeLinkChain(t *testing.T) {
164
-	tmpdir, err := ioutil.TempDir("", "TestFollowSymlinkRelativeLinkChain")
165
-	if err != nil {
166
-		t.Fatal(err)
167
-	}
168
-	defer os.RemoveAll(tmpdir)
169
-
170
-	// avoid letting symlink g (pointed at by symlink h) take out of scope
171
-	// TODO: we should probably normalize to scope here because ../[....]/root
172
-	// is out of scope and we leak information
173
-	if err := makeFs(tmpdir, []dirOrLink{
174
-		{path: "testdata/fs/b/h", target: "../g"},
175
-		{path: "testdata/fs/g", target: "../../../../../../../../../../../../root"},
176
-	}); err != nil {
177
-		t.Fatal(err)
178
-	}
179
-	if err := testSymlink(tmpdir, "testdata/fs/b/h", "testdata/root", "testdata"); err != nil {
180
-		t.Fatal(err)
181
-	}
182
-}
183
-
184
-func TestFollowSymlinkBreakoutPath(t *testing.T) {
185
-	tmpdir, err := ioutil.TempDir("", "TestFollowSymlinkBreakoutPath")
186
-	if err != nil {
187
-		t.Fatal(err)
188
-	}
189
-	defer os.RemoveAll(tmpdir)
190
-
191
-	// avoid letting symlink -> ../directory/file escape from scope
192
-	// normalize to "testdata/fs/j"
193
-	if err := makeFs(tmpdir, []dirOrLink{{path: "testdata/fs/j/k", target: "../i/a"}}); err != nil {
194
-		t.Fatal(err)
195
-	}
196
-	if err := testSymlink(tmpdir, "testdata/fs/j/k", "testdata/fs/j/i/a", "testdata/fs/j"); err != nil {
197
-		t.Fatal(err)
198
-	}
199
-}
200
-
201
-func TestFollowSymlinkToRoot(t *testing.T) {
202
-	tmpdir, err := ioutil.TempDir("", "TestFollowSymlinkToRoot")
203
-	if err != nil {
204
-		t.Fatal(err)
205
-	}
206
-	defer os.RemoveAll(tmpdir)
207
-
208
-	// make sure we don't allow escaping to /
209
-	// normalize to dir
210
-	if err := makeFs(tmpdir, []dirOrLink{{path: "foo", target: "/"}}); err != nil {
211
-		t.Fatal(err)
212
-	}
213
-	if err := testSymlink(tmpdir, "foo", "", ""); err != nil {
214
-		t.Fatal(err)
215
-	}
216
-}
217
-
218
-func TestFollowSymlinkSlashDotdot(t *testing.T) {
219
-	tmpdir, err := ioutil.TempDir("", "TestFollowSymlinkSlashDotdot")
220
-	if err != nil {
221
-		t.Fatal(err)
222
-	}
223
-	defer os.RemoveAll(tmpdir)
224
-	tmpdir = filepath.Join(tmpdir, "dir", "subdir")
225
-
226
-	// make sure we don't allow escaping to /
227
-	// normalize to dir
228
-	if err := makeFs(tmpdir, []dirOrLink{{path: "foo", target: "/../../"}}); err != nil {
229
-		t.Fatal(err)
230
-	}
231
-	if err := testSymlink(tmpdir, "foo", "", ""); err != nil {
232
-		t.Fatal(err)
233
-	}
234
-}
235
-
236
-func TestFollowSymlinkDotdot(t *testing.T) {
237
-	tmpdir, err := ioutil.TempDir("", "TestFollowSymlinkDotdot")
238
-	if err != nil {
239
-		t.Fatal(err)
240
-	}
241
-	defer os.RemoveAll(tmpdir)
242
-	tmpdir = filepath.Join(tmpdir, "dir", "subdir")
243
-
244
-	// make sure we stay in scope without leaking information
245
-	// this also checks for escaping to /
246
-	// normalize to dir
247
-	if err := makeFs(tmpdir, []dirOrLink{{path: "foo", target: "../../"}}); err != nil {
248
-		t.Fatal(err)
249
-	}
250
-	if err := testSymlink(tmpdir, "foo", "", ""); err != nil {
251
-		t.Fatal(err)
252
-	}
253
-}
254
-
255
-func TestFollowSymlinkRelativePath2(t *testing.T) {
256
-	tmpdir, err := ioutil.TempDir("", "TestFollowSymlinkRelativePath2")
257
-	if err != nil {
258
-		t.Fatal(err)
259
-	}
260
-	defer os.RemoveAll(tmpdir)
261
-
262
-	if err := makeFs(tmpdir, []dirOrLink{{path: "bar/foo", target: "baz/target"}}); err != nil {
263
-		t.Fatal(err)
264
-	}
265
-	if err := testSymlink(tmpdir, "bar/foo", "bar/baz/target", ""); err != nil {
266
-		t.Fatal(err)
267
-	}
268
-}
269
-
270
-func TestFollowSymlinkScopeLink(t *testing.T) {
271
-	tmpdir, err := ioutil.TempDir("", "TestFollowSymlinkScopeLink")
272
-	if err != nil {
273
-		t.Fatal(err)
274
-	}
275
-	defer os.RemoveAll(tmpdir)
276
-
277
-	if err := makeFs(tmpdir, []dirOrLink{
278
-		{path: "root2"},
279
-		{path: "root", target: "root2"},
280
-		{path: "root2/foo", target: "../bar"},
281
-	}); err != nil {
282
-		t.Fatal(err)
283
-	}
284
-	if err := testSymlink(tmpdir, "root/foo", "root/bar", "root"); err != nil {
285
-		t.Fatal(err)
286
-	}
287
-}
288
-
289
-func TestFollowSymlinkRootScope(t *testing.T) {
290
-	tmpdir, err := ioutil.TempDir("", "TestFollowSymlinkRootScope")
291
-	if err != nil {
292
-		t.Fatal(err)
293
-	}
294
-	defer os.RemoveAll(tmpdir)
295
-
296
-	expected, err := filepath.EvalSymlinks(tmpdir)
297
-	if err != nil {
298
-		t.Fatal(err)
299
-	}
300
-	rewrite, err := FollowSymlinkInScope(tmpdir, "/")
301
-	if err != nil {
302
-		t.Fatal(err)
303
-	}
304
-	if rewrite != expected {
305
-		t.Fatalf("expected %q got %q", expected, rewrite)
306
-	}
307
-}
308
-
309
-func TestFollowSymlinkEmpty(t *testing.T) {
310
-	res, err := FollowSymlinkInScope("", "")
311
-	if err != nil {
312
-		t.Fatal(err)
313
-	}
314
-	wd, err := os.Getwd()
315
-	if err != nil {
316
-		t.Fatal(err)
317
-	}
318
-	if res != wd {
319
-		t.Fatalf("expected %q got %q", wd, res)
320
-	}
321
-}
322
-
323
-func TestFollowSymlinkCircular(t *testing.T) {
324
-	tmpdir, err := ioutil.TempDir("", "TestFollowSymlinkCircular")
325
-	if err != nil {
326
-		t.Fatal(err)
327
-	}
328
-	defer os.RemoveAll(tmpdir)
329
-
330
-	if err := makeFs(tmpdir, []dirOrLink{{path: "root/foo", target: "foo"}}); err != nil {
331
-		t.Fatal(err)
332
-	}
333
-	if err := testSymlink(tmpdir, "root/foo", "", "root"); err == nil {
334
-		t.Fatal("expected an error for foo -> foo")
335
-	}
336
-
337
-	if err := makeFs(tmpdir, []dirOrLink{
338
-		{path: "root/bar", target: "baz"},
339
-		{path: "root/baz", target: "../bak"},
340
-		{path: "root/bak", target: "/bar"},
341
-	}); err != nil {
342
-		t.Fatal(err)
343
-	}
344
-	if err := testSymlink(tmpdir, "root/foo", "", "root"); err == nil {
345
-		t.Fatal("expected an error for bar -> baz -> bak -> bar")
346
-	}
347
-}
348
-
349
-func TestFollowSymlinkComplexChainWithTargetPathsContainingLinks(t *testing.T) {
350
-	tmpdir, err := ioutil.TempDir("", "TestFollowSymlinkComplexChainWithTargetPathsContainingLinks")
351
-	if err != nil {
352
-		t.Fatal(err)
353
-	}
354
-	defer os.RemoveAll(tmpdir)
355
-
356
-	if err := makeFs(tmpdir, []dirOrLink{
357
-		{path: "root2"},
358
-		{path: "root", target: "root2"},
359
-		{path: "root/a", target: "r/s"},
360
-		{path: "root/r", target: "../root/t"},
361
-		{path: "root/root/t/s/b", target: "/../u"},
362
-		{path: "root/u/c", target: "."},
363
-		{path: "root/u/x/y", target: "../v"},
364
-		{path: "root/u/v", target: "/../w"},
365
-	}); err != nil {
366
-		t.Fatal(err)
367
-	}
368
-	if err := testSymlink(tmpdir, "root/a/b/c/x/y/z", "root/w/z", "root"); err != nil {
369
-		t.Fatal(err)
370
-	}
371
-}
372
-
373
-func TestFollowSymlinkBreakoutNonExistent(t *testing.T) {
374
-	tmpdir, err := ioutil.TempDir("", "TestFollowSymlinkBreakoutNonExistent")
375
-	if err != nil {
376
-		t.Fatal(err)
377
-	}
378
-	defer os.RemoveAll(tmpdir)
379
-
380
-	if err := makeFs(tmpdir, []dirOrLink{
381
-		{path: "root/slash", target: "/"},
382
-		{path: "root/sym", target: "/idontexist/../slash"},
383
-	}); err != nil {
384
-		t.Fatal(err)
385
-	}
386
-	if err := testSymlink(tmpdir, "root/sym/file", "root/file", "root"); err != nil {
387
-		t.Fatal(err)
388
-	}
389
-}
390
-
391
-func TestFollowSymlinkNoLexicalCleaning(t *testing.T) {
392
-	tmpdir, err := ioutil.TempDir("", "TestFollowSymlinkNoLexicalCleaning")
393
-	if err != nil {
394
-		t.Fatal(err)
395
-	}
396
-	defer os.RemoveAll(tmpdir)
397
-
398
-	if err := makeFs(tmpdir, []dirOrLink{
399
-		{path: "root/sym", target: "/foo/bar"},
400
-		{path: "root/hello", target: "/sym/../baz"},
401
-	}); err != nil {
402
-		t.Fatal(err)
403
-	}
404
-	if err := testSymlink(tmpdir, "root/hello", "root/foo/baz", "root"); err != nil {
405
-		t.Fatal(err)
406
-	}
407
-}
408 1
deleted file mode 100644
... ...
@@ -1,185 +0,0 @@
1
-package symlink // import "github.com/docker/docker/pkg/symlink"
2
-
3
-import (
4
-	"bytes"
5
-	"errors"
6
-	"os"
7
-	"path/filepath"
8
-	"strings"
9
-
10
-	"golang.org/x/sys/windows"
11
-)
12
-
13
-func toShort(path string) (string, error) {
14
-	p, err := windows.UTF16FromString(path)
15
-	if err != nil {
16
-		return "", err
17
-	}
18
-	b := p // GetShortPathName says we can reuse buffer
19
-	n, err := windows.GetShortPathName(&p[0], &b[0], uint32(len(b)))
20
-	if err != nil {
21
-		return "", err
22
-	}
23
-	if n > uint32(len(b)) {
24
-		b = make([]uint16, n)
25
-		if _, err = windows.GetShortPathName(&p[0], &b[0], uint32(len(b))); err != nil {
26
-			return "", err
27
-		}
28
-	}
29
-	return windows.UTF16ToString(b), nil
30
-}
31
-
32
-func toLong(path string) (string, error) {
33
-	p, err := windows.UTF16FromString(path)
34
-	if err != nil {
35
-		return "", err
36
-	}
37
-	b := p // GetLongPathName says we can reuse buffer
38
-	n, err := windows.GetLongPathName(&p[0], &b[0], uint32(len(b)))
39
-	if err != nil {
40
-		return "", err
41
-	}
42
-	if n > uint32(len(b)) {
43
-		b = make([]uint16, n)
44
-		n, err = windows.GetLongPathName(&p[0], &b[0], uint32(len(b)))
45
-		if err != nil {
46
-			return "", err
47
-		}
48
-	}
49
-	b = b[:n]
50
-	return windows.UTF16ToString(b), nil
51
-}
52
-
53
-func evalSymlinks(path string) (string, error) {
54
-	path, err := walkSymlinks(path)
55
-	if err != nil {
56
-		return "", err
57
-	}
58
-
59
-	p, err := toShort(path)
60
-	if err != nil {
61
-		return "", err
62
-	}
63
-	p, err = toLong(p)
64
-	if err != nil {
65
-		return "", err
66
-	}
67
-	// windows.GetLongPathName does not change the case of the drive letter,
68
-	// but the result of EvalSymlinks must be unique, so we have
69
-	// EvalSymlinks(`c:\a`) == EvalSymlinks(`C:\a`).
70
-	// Make drive letter upper case.
71
-	if len(p) >= 2 && p[1] == ':' && 'a' <= p[0] && p[0] <= 'z' {
72
-		p = string(p[0]+'A'-'a') + p[1:]
73
-	} else if len(p) >= 6 && p[5] == ':' && 'a' <= p[4] && p[4] <= 'z' {
74
-		p = p[:3] + string(p[4]+'A'-'a') + p[5:]
75
-	}
76
-	return filepath.Clean(p), nil
77
-}
78
-
79
-const (
80
-	utf8RuneSelf   = 0x80
81
-	longPathPrefix = `\\?\`
82
-)
83
-
84
-func walkSymlinks(path string) (string, error) {
85
-	const maxIter = 255
86
-	originalPath := path
87
-	// consume path by taking each frontmost path element,
88
-	// expanding it if it's a symlink, and appending it to b
89
-	var b bytes.Buffer
90
-	for n := 0; path != ""; n++ {
91
-		if n > maxIter {
92
-			return "", errors.New("EvalSymlinks: too many links in " + originalPath)
93
-		}
94
-
95
-		// A path beginning with `\\?\` represents the root, so automatically
96
-		// skip that part and begin processing the next segment.
97
-		if strings.HasPrefix(path, longPathPrefix) {
98
-			b.WriteString(longPathPrefix)
99
-			path = path[4:]
100
-			continue
101
-		}
102
-
103
-		// find next path component, p
104
-		var i = -1
105
-		for j, c := range path {
106
-			if c < utf8RuneSelf && os.IsPathSeparator(uint8(c)) {
107
-				i = j
108
-				break
109
-			}
110
-		}
111
-		var p string
112
-		if i == -1 {
113
-			p, path = path, ""
114
-		} else {
115
-			p, path = path[:i], path[i+1:]
116
-		}
117
-
118
-		if p == "" {
119
-			if b.Len() == 0 {
120
-				// must be absolute path
121
-				b.WriteRune(filepath.Separator)
122
-			}
123
-			continue
124
-		}
125
-
126
-		// If this is the first segment after the long path prefix, accept the
127
-		// current segment as a volume root or UNC share and move on to the next.
128
-		if b.String() == longPathPrefix {
129
-			b.WriteString(p)
130
-			b.WriteRune(filepath.Separator)
131
-			continue
132
-		}
133
-
134
-		fi, err := os.Lstat(b.String() + p)
135
-		if err != nil {
136
-			return "", err
137
-		}
138
-		if fi.Mode()&os.ModeSymlink == 0 {
139
-			b.WriteString(p)
140
-			if path != "" || (b.Len() == 2 && len(p) == 2 && p[1] == ':') {
141
-				b.WriteRune(filepath.Separator)
142
-			}
143
-			continue
144
-		}
145
-
146
-		// it's a symlink, put it at the front of path
147
-		dest, err := os.Readlink(b.String() + p)
148
-		if err != nil {
149
-			return "", err
150
-		}
151
-		if isAbs(dest) {
152
-			b.Reset()
153
-		}
154
-		path = dest + string(filepath.Separator) + path
155
-	}
156
-	return filepath.Clean(b.String()), nil
157
-}
158
-
159
-func isDriveOrRoot(p string) bool {
160
-	if p == string(filepath.Separator) {
161
-		return true
162
-	}
163
-
164
-	length := len(p)
165
-	if length >= 2 {
166
-		if p[length-1] == ':' && (('a' <= p[length-2] && p[length-2] <= 'z') || ('A' <= p[length-2] && p[length-2] <= 'Z')) {
167
-			return true
168
-		}
169
-	}
170
-	return false
171
-}
172
-
173
-// isAbs is a platform-specific wrapper for filepath.IsAbs. On Windows,
174
-// golang filepath.IsAbs does not consider a path \windows\system32 as absolute
175
-// as it doesn't start with a drive-letter/colon combination. However, in
176
-// docker we need to verify things such as WORKDIR /windows/system32 in
177
-// a Dockerfile (which gets translated to \windows\system32 when being processed
178
-// by the daemon. This SHOULD be treated as absolute from a docker processing
179
-// perspective.
180
-func isAbs(path string) bool {
181
-	if filepath.IsAbs(path) || strings.HasPrefix(path, string(os.PathSeparator)) {
182
-		return true
183
-	}
184
-	return false
185
-}
... ...
@@ -9,12 +9,12 @@ github.com/Microsoft/opengcs                        a10967154e143a36014584a6f664
9 9
 github.com/moby/locker                              281af2d563954745bea9d1487c965f24d30742fe # v1.0.1
10 10
 github.com/moby/term                                7f0af18e79f2784809e9cef63d0df5aa2c79d76e
11 11
 
12
-# Note that this dependency uses submodules, providing the github.com/moby/sys/mount
13
-# and github.com/moby/sys/mountinfo modules. Our vendoring tool (vndr) currently
14
-# does not support submodules / vendoring sub-paths, so we vendor the top-level
15
-# moby/sys repository (which contains both) and pick the most recent tag, which
16
-# could be either `mountinfo/vX.Y.Z` or `mount/vX.Y.Z`.
17
-github.com/moby/sys                                 9a75fe61baf4b9788826b48b0518abecffb79b16 # mountinfo v0.4.0
12
+# Note that this dependency uses submodules, providing the github.com/moby/sys/mount,
13
+# github.com/moby/sys/mountinfo, and github.com/moby/sys/symlink modules. Our vendoring
14
+# tool (vndr) currently does not support submodules / vendoring sub-paths, so we vendor
15
+# the top-level moby/sys repository (which contains both) and pick the most recent tag,
16
+# which could be either `mountinfo/vX.Y.Z`, `mount/vX.Y.Z`, or `symlink/vX.Y.Z`.
17
+github.com/moby/sys                                 1bc8673b57550ddf85262eb0fed0aac651a37dab # symlink/v0.1.0
18 18
 
19 19
 github.com/creack/pty                               3a6a957789163cacdfe0e291617a1c8e80612c11 # v1.1.9
20 20
 github.com/sirupsen/logrus                          6699a89a232f3db797f2e280639854bbc4b89725 # v1.7.0
21 21
new file mode 100644
... ...
@@ -0,0 +1,191 @@
0
+
1
+                                 Apache License
2
+                           Version 2.0, January 2004
3
+                        http://www.apache.org/licenses/
4
+
5
+   TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+   1. Definitions.
8
+
9
+      "License" shall mean the terms and conditions for use, reproduction,
10
+      and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+      "Licensor" shall mean the copyright owner or entity authorized by
13
+      the copyright owner that is granting the License.
14
+
15
+      "Legal Entity" shall mean the union of the acting entity and all
16
+      other entities that control, are controlled by, or are under common
17
+      control with that entity. For the purposes of this definition,
18
+      "control" means (i) the power, direct or indirect, to cause the
19
+      direction or management of such entity, whether by contract or
20
+      otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+      outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+      "You" (or "Your") shall mean an individual or Legal Entity
24
+      exercising permissions granted by this License.
25
+
26
+      "Source" form shall mean the preferred form for making modifications,
27
+      including but not limited to software source code, documentation
28
+      source, and configuration files.
29
+
30
+      "Object" form shall mean any form resulting from mechanical
31
+      transformation or translation of a Source form, including but
32
+      not limited to compiled object code, generated documentation,
33
+      and conversions to other media types.
34
+
35
+      "Work" shall mean the work of authorship, whether in Source or
36
+      Object form, made available under the License, as indicated by a
37
+      copyright notice that is included in or attached to the work
38
+      (an example is provided in the Appendix below).
39
+
40
+      "Derivative Works" shall mean any work, whether in Source or Object
41
+      form, that is based on (or derived from) the Work and for which the
42
+      editorial revisions, annotations, elaborations, or other modifications
43
+      represent, as a whole, an original work of authorship. For the purposes
44
+      of this License, Derivative Works shall not include works that remain
45
+      separable from, or merely link (or bind by name) to the interfaces of,
46
+      the Work and Derivative Works thereof.
47
+
48
+      "Contribution" shall mean any work of authorship, including
49
+      the original version of the Work and any modifications or additions
50
+      to that Work or Derivative Works thereof, that is intentionally
51
+      submitted to Licensor for inclusion in the Work by the copyright owner
52
+      or by an individual or Legal Entity authorized to submit on behalf of
53
+      the copyright owner. For the purposes of this definition, "submitted"
54
+      means any form of electronic, verbal, or written communication sent
55
+      to the Licensor or its representatives, including but not limited to
56
+      communication on electronic mailing lists, source code control systems,
57
+      and issue tracking systems that are managed by, or on behalf of, the
58
+      Licensor for the purpose of discussing and improving the Work, but
59
+      excluding communication that is conspicuously marked or otherwise
60
+      designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+      "Contributor" shall mean Licensor and any individual or Legal Entity
63
+      on behalf of whom a Contribution has been received by Licensor and
64
+      subsequently incorporated within the Work.
65
+
66
+   2. Grant of Copyright License. Subject to the terms and conditions of
67
+      this License, each Contributor hereby grants to You a perpetual,
68
+      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+      copyright license to reproduce, prepare Derivative Works of,
70
+      publicly display, publicly perform, sublicense, and distribute the
71
+      Work and such Derivative Works in Source or Object form.
72
+
73
+   3. Grant of Patent License. Subject to the terms and conditions of
74
+      this License, each Contributor hereby grants to You a perpetual,
75
+      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+      (except as stated in this section) patent license to make, have made,
77
+      use, offer to sell, sell, import, and otherwise transfer the Work,
78
+      where such license applies only to those patent claims licensable
79
+      by such Contributor that are necessarily infringed by their
80
+      Contribution(s) alone or by combination of their Contribution(s)
81
+      with the Work to which such Contribution(s) was submitted. If You
82
+      institute patent litigation against any entity (including a
83
+      cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+      or a Contribution incorporated within the Work constitutes direct
85
+      or contributory patent infringement, then any patent licenses
86
+      granted to You under this License for that Work shall terminate
87
+      as of the date such litigation is filed.
88
+
89
+   4. Redistribution. You may reproduce and distribute copies of the
90
+      Work or Derivative Works thereof in any medium, with or without
91
+      modifications, and in Source or Object form, provided that You
92
+      meet the following conditions:
93
+
94
+      (a) You must give any other recipients of the Work or
95
+          Derivative Works a copy of this License; and
96
+
97
+      (b) You must cause any modified files to carry prominent notices
98
+          stating that You changed the files; and
99
+
100
+      (c) You must retain, in the Source form of any Derivative Works
101
+          that You distribute, all copyright, patent, trademark, and
102
+          attribution notices from the Source form of the Work,
103
+          excluding those notices that do not pertain to any part of
104
+          the Derivative Works; and
105
+
106
+      (d) If the Work includes a "NOTICE" text file as part of its
107
+          distribution, then any Derivative Works that You distribute must
108
+          include a readable copy of the attribution notices contained
109
+          within such NOTICE file, excluding those notices that do not
110
+          pertain to any part of the Derivative Works, in at least one
111
+          of the following places: within a NOTICE text file distributed
112
+          as part of the Derivative Works; within the Source form or
113
+          documentation, if provided along with the Derivative Works; or,
114
+          within a display generated by the Derivative Works, if and
115
+          wherever such third-party notices normally appear. The contents
116
+          of the NOTICE file are for informational purposes only and
117
+          do not modify the License. You may add Your own attribution
118
+          notices within Derivative Works that You distribute, alongside
119
+          or as an addendum to the NOTICE text from the Work, provided
120
+          that such additional attribution notices cannot be construed
121
+          as modifying the License.
122
+
123
+      You may add Your own copyright statement to Your modifications and
124
+      may provide additional or different license terms and conditions
125
+      for use, reproduction, or distribution of Your modifications, or
126
+      for any such Derivative Works as a whole, provided Your use,
127
+      reproduction, and distribution of the Work otherwise complies with
128
+      the conditions stated in this License.
129
+
130
+   5. Submission of Contributions. Unless You explicitly state otherwise,
131
+      any Contribution intentionally submitted for inclusion in the Work
132
+      by You to the Licensor shall be under the terms and conditions of
133
+      this License, without any additional terms or conditions.
134
+      Notwithstanding the above, nothing herein shall supersede or modify
135
+      the terms of any separate license agreement you may have executed
136
+      with Licensor regarding such Contributions.
137
+
138
+   6. Trademarks. This License does not grant permission to use the trade
139
+      names, trademarks, service marks, or product names of the Licensor,
140
+      except as required for reasonable and customary use in describing the
141
+      origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+   7. Disclaimer of Warranty. Unless required by applicable law or
144
+      agreed to in writing, Licensor provides the Work (and each
145
+      Contributor provides its Contributions) on an "AS IS" BASIS,
146
+      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+      implied, including, without limitation, any warranties or conditions
148
+      of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+      PARTICULAR PURPOSE. You are solely responsible for determining the
150
+      appropriateness of using or redistributing the Work and assume any
151
+      risks associated with Your exercise of permissions under this License.
152
+
153
+   8. Limitation of Liability. In no event and under no legal theory,
154
+      whether in tort (including negligence), contract, or otherwise,
155
+      unless required by applicable law (such as deliberate and grossly
156
+      negligent acts) or agreed to in writing, shall any Contributor be
157
+      liable to You for damages, including any direct, indirect, special,
158
+      incidental, or consequential damages of any character arising as a
159
+      result of this License or out of the use or inability to use the
160
+      Work (including but not limited to damages for loss of goodwill,
161
+      work stoppage, computer failure or malfunction, or any and all
162
+      other commercial damages or losses), even if such Contributor
163
+      has been advised of the possibility of such damages.
164
+
165
+   9. Accepting Warranty or Additional Liability. While redistributing
166
+      the Work or Derivative Works thereof, You may choose to offer,
167
+      and charge a fee for, acceptance of support, warranty, indemnity,
168
+      or other liability obligations and/or rights consistent with this
169
+      License. However, in accepting such obligations, You may act only
170
+      on Your own behalf and on Your sole responsibility, not on behalf
171
+      of any other Contributor, and only if You agree to indemnify,
172
+      defend, and hold each Contributor harmless for any liability
173
+      incurred by, or claims asserted against, such Contributor by reason
174
+      of your accepting any such warranty or additional liability.
175
+
176
+   END OF TERMS AND CONDITIONS
177
+
178
+   Copyright 2014-2018 Docker, Inc.
179
+
180
+   Licensed under the Apache License, Version 2.0 (the "License");
181
+   you may not use this file except in compliance with the License.
182
+   You may obtain a copy of the License at
183
+
184
+       http://www.apache.org/licenses/LICENSE-2.0
185
+
186
+   Unless required by applicable law or agreed to in writing, software
187
+   distributed under the License is distributed on an "AS IS" BASIS,
188
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
189
+   See the License for the specific language governing permissions and
190
+   limitations under the License.
0 191
new file mode 100644
... ...
@@ -0,0 +1,27 @@
0
+Copyright (c) 2014-2018 The Docker & Go Authors. All rights reserved.
1
+
2
+Redistribution and use in source and binary forms, with or without
3
+modification, are permitted provided that the following conditions are
4
+met:
5
+
6
+   * Redistributions of source code must retain the above copyright
7
+notice, this list of conditions and the following disclaimer.
8
+   * Redistributions in binary form must reproduce the above
9
+copyright notice, this list of conditions and the following disclaimer
10
+in the documentation and/or other materials provided with the
11
+distribution.
12
+   * Neither the name of Google Inc. nor the names of its
13
+contributors may be used to endorse or promote products derived from
14
+this software without specific prior written permission.
15
+
16
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20
+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0 27
new file mode 100644
... ...
@@ -0,0 +1,6 @@
0
+Package symlink implements EvalSymlinksInScope which is an extension of filepath.EvalSymlinks,
1
+as well as a Windows long-path aware version of filepath.EvalSymlinks
2
+from the [Go standard library](https://golang.org/pkg/path/filepath).
3
+
4
+The code from filepath.EvalSymlinks has been adapted in fs.go.
5
+Please read the LICENSE.BSD file that governs fs.go and LICENSE.APACHE for fs_test.go.
0 6
new file mode 100644
... ...
@@ -0,0 +1,4 @@
0
+// Package symlink implements EvalSymlinksInScope which is an extension of
1
+// filepath.EvalSymlinks, as well as a Windows long-path aware version of
2
+// filepath.EvalSymlinks from the Go standard library (https://golang.org/pkg/path/filepath).
3
+package symlink
0 4
new file mode 100644
... ...
@@ -0,0 +1,142 @@
0
+// Copyright 2012 The Go Authors. All rights reserved.
1
+// Use of this source code is governed by a BSD-style
2
+// license that can be found in the LICENSE.BSD file.
3
+
4
+// This code is a modified version of path/filepath/symlink.go from the Go standard library.
5
+
6
+package symlink
7
+
8
+import (
9
+	"bytes"
10
+	"errors"
11
+	"os"
12
+	"path/filepath"
13
+	"strings"
14
+)
15
+
16
+// FollowSymlinkInScope is a wrapper around evalSymlinksInScope that returns an
17
+// absolute path. This function handles paths in a platform-agnostic manner.
18
+func FollowSymlinkInScope(path, root string) (string, error) {
19
+	path, err := filepath.Abs(filepath.FromSlash(path))
20
+	if err != nil {
21
+		return "", err
22
+	}
23
+	root, err = filepath.Abs(filepath.FromSlash(root))
24
+	if err != nil {
25
+		return "", err
26
+	}
27
+	return evalSymlinksInScope(path, root)
28
+}
29
+
30
+// evalSymlinksInScope will evaluate symlinks in `path` within a scope `root` and return
31
+// a result guaranteed to be contained within the scope `root`, at the time of the call.
32
+// Symlinks in `root` are not evaluated and left as-is.
33
+// Errors encountered while attempting to evaluate symlinks in path will be returned.
34
+// Non-existing paths are valid and do not constitute an error.
35
+// `path` has to contain `root` as a prefix, or else an error will be returned.
36
+// Trying to break out from `root` does not constitute an error.
37
+//
38
+// Example:
39
+//   If /foo/bar -> /outside,
40
+//   FollowSymlinkInScope("/foo/bar", "/foo") == "/foo/outside" instead of "/outside"
41
+//
42
+// IMPORTANT: it is the caller's responsibility to call evalSymlinksInScope *after* relevant symlinks
43
+// are created and not to create subsequently, additional symlinks that could potentially make a
44
+// previously-safe path, unsafe. Example: if /foo/bar does not exist, evalSymlinksInScope("/foo/bar", "/foo")
45
+// would return "/foo/bar". If one makes /foo/bar a symlink to /baz subsequently, then "/foo/bar" should
46
+// no longer be considered safely contained in "/foo".
47
+func evalSymlinksInScope(path, root string) (string, error) {
48
+	root = filepath.Clean(root)
49
+	if path == root {
50
+		return path, nil
51
+	}
52
+	if !strings.HasPrefix(path, root) {
53
+		return "", errors.New("evalSymlinksInScope: " + path + " is not in " + root)
54
+	}
55
+	const maxIter = 255
56
+	originalPath := path
57
+	// given root of "/a" and path of "/a/b/../../c" we want path to be "/b/../../c"
58
+	path = path[len(root):]
59
+	if root == string(filepath.Separator) {
60
+		path = string(filepath.Separator) + path
61
+	}
62
+	if !strings.HasPrefix(path, string(filepath.Separator)) {
63
+		return "", errors.New("evalSymlinksInScope: " + path + " is not in " + root)
64
+	}
65
+	path = filepath.Clean(path)
66
+	// consume path by taking each frontmost path element,
67
+	// expanding it if it's a symlink, and appending it to b
68
+	var b bytes.Buffer
69
+	// b here will always be considered to be the "current absolute path inside
70
+	// root" when we append paths to it, we also append a slash and use
71
+	// filepath.Clean after the loop to trim the trailing slash
72
+	for n := 0; path != ""; n++ {
73
+		if n > maxIter {
74
+			return "", errors.New("evalSymlinksInScope: too many links in " + originalPath)
75
+		}
76
+
77
+		// find next path component, p
78
+		i := strings.IndexRune(path, filepath.Separator)
79
+		var p string
80
+		if i == -1 {
81
+			p, path = path, ""
82
+		} else {
83
+			p, path = path[:i], path[i+1:]
84
+		}
85
+
86
+		if p == "" {
87
+			continue
88
+		}
89
+
90
+		// this takes a b.String() like "b/../" and a p like "c" and turns it
91
+		// into "/b/../c" which then gets filepath.Cleaned into "/c" and then
92
+		// root gets prepended and we Clean again (to remove any trailing slash
93
+		// if the first Clean gave us just "/")
94
+		cleanP := filepath.Clean(string(filepath.Separator) + b.String() + p)
95
+		if isDriveOrRoot(cleanP) {
96
+			// never Lstat "/" itself, or drive letters on Windows
97
+			b.Reset()
98
+			continue
99
+		}
100
+		fullP := filepath.Clean(root + cleanP)
101
+
102
+		fi, err := os.Lstat(fullP)
103
+		if os.IsNotExist(err) {
104
+			// if p does not exist, accept it
105
+			b.WriteString(p)
106
+			b.WriteRune(filepath.Separator)
107
+			continue
108
+		}
109
+		if err != nil {
110
+			return "", err
111
+		}
112
+		if fi.Mode()&os.ModeSymlink == 0 {
113
+			b.WriteString(p)
114
+			b.WriteRune(filepath.Separator)
115
+			continue
116
+		}
117
+
118
+		// it's a symlink, put it at the front of path
119
+		dest, err := os.Readlink(fullP)
120
+		if err != nil {
121
+			return "", err
122
+		}
123
+		if isAbs(dest) {
124
+			b.Reset()
125
+		}
126
+		path = dest + string(filepath.Separator) + path
127
+	}
128
+
129
+	// see note above on "fullP := ..." for why this is double-cleaned and
130
+	// what's happening here
131
+	return filepath.Clean(root + filepath.Clean(string(filepath.Separator)+b.String())), nil
132
+}
133
+
134
+// EvalSymlinks returns the path name after the evaluation of any symbolic
135
+// links.
136
+// If path is relative the result will be relative to the current directory,
137
+// unless one of the components is an absolute symbolic link.
138
+// This version has been updated to support long paths prepended with `\\?\`.
139
+func EvalSymlinks(path string) (string, error) {
140
+	return evalSymlinks(path)
141
+}
0 142
new file mode 100644
... ...
@@ -0,0 +1,17 @@
0
+// +build !windows
1
+
2
+package symlink
3
+
4
+import (
5
+	"path/filepath"
6
+)
7
+
8
+func evalSymlinks(path string) (string, error) {
9
+	return filepath.EvalSymlinks(path)
10
+}
11
+
12
+func isDriveOrRoot(p string) bool {
13
+	return p == string(filepath.Separator)
14
+}
15
+
16
+var isAbs = filepath.IsAbs
0 17
new file mode 100644
... ...
@@ -0,0 +1,185 @@
0
+package symlink
1
+
2
+import (
3
+	"bytes"
4
+	"errors"
5
+	"os"
6
+	"path/filepath"
7
+	"strings"
8
+
9
+	"golang.org/x/sys/windows"
10
+)
11
+
12
+func toShort(path string) (string, error) {
13
+	p, err := windows.UTF16FromString(path)
14
+	if err != nil {
15
+		return "", err
16
+	}
17
+	b := p // GetShortPathName says we can reuse buffer
18
+	n, err := windows.GetShortPathName(&p[0], &b[0], uint32(len(b)))
19
+	if err != nil {
20
+		return "", err
21
+	}
22
+	if n > uint32(len(b)) {
23
+		b = make([]uint16, n)
24
+		if _, err = windows.GetShortPathName(&p[0], &b[0], uint32(len(b))); err != nil {
25
+			return "", err
26
+		}
27
+	}
28
+	return windows.UTF16ToString(b), nil
29
+}
30
+
31
+func toLong(path string) (string, error) {
32
+	p, err := windows.UTF16FromString(path)
33
+	if err != nil {
34
+		return "", err
35
+	}
36
+	b := p // GetLongPathName says we can reuse buffer
37
+	n, err := windows.GetLongPathName(&p[0], &b[0], uint32(len(b)))
38
+	if err != nil {
39
+		return "", err
40
+	}
41
+	if n > uint32(len(b)) {
42
+		b = make([]uint16, n)
43
+		n, err = windows.GetLongPathName(&p[0], &b[0], uint32(len(b)))
44
+		if err != nil {
45
+			return "", err
46
+		}
47
+	}
48
+	b = b[:n]
49
+	return windows.UTF16ToString(b), nil
50
+}
51
+
52
+func evalSymlinks(path string) (string, error) {
53
+	path, err := walkSymlinks(path)
54
+	if err != nil {
55
+		return "", err
56
+	}
57
+
58
+	p, err := toShort(path)
59
+	if err != nil {
60
+		return "", err
61
+	}
62
+	p, err = toLong(p)
63
+	if err != nil {
64
+		return "", err
65
+	}
66
+	// windows.GetLongPathName does not change the case of the drive letter,
67
+	// but the result of EvalSymlinks must be unique, so we have
68
+	// EvalSymlinks(`c:\a`) == EvalSymlinks(`C:\a`).
69
+	// Make drive letter upper case.
70
+	if len(p) >= 2 && p[1] == ':' && 'a' <= p[0] && p[0] <= 'z' {
71
+		p = string(p[0]+'A'-'a') + p[1:]
72
+	} else if len(p) >= 6 && p[5] == ':' && 'a' <= p[4] && p[4] <= 'z' {
73
+		p = p[:3] + string(p[4]+'A'-'a') + p[5:]
74
+	}
75
+	return filepath.Clean(p), nil
76
+}
77
+
78
+const (
79
+	utf8RuneSelf   = 0x80
80
+	longPathPrefix = `\\?\`
81
+)
82
+
83
+func walkSymlinks(path string) (string, error) {
84
+	const maxIter = 255
85
+	originalPath := path
86
+	// consume path by taking each frontmost path element,
87
+	// expanding it if it's a symlink, and appending it to b
88
+	var b bytes.Buffer
89
+	for n := 0; path != ""; n++ {
90
+		if n > maxIter {
91
+			return "", errors.New("EvalSymlinks: too many links in " + originalPath)
92
+		}
93
+
94
+		// A path beginning with `\\?\` represents the root, so automatically
95
+		// skip that part and begin processing the next segment.
96
+		if strings.HasPrefix(path, longPathPrefix) {
97
+			b.WriteString(longPathPrefix)
98
+			path = path[4:]
99
+			continue
100
+		}
101
+
102
+		// find next path component, p
103
+		var i = -1
104
+		for j, c := range path {
105
+			if c < utf8RuneSelf && os.IsPathSeparator(uint8(c)) {
106
+				i = j
107
+				break
108
+			}
109
+		}
110
+		var p string
111
+		if i == -1 {
112
+			p, path = path, ""
113
+		} else {
114
+			p, path = path[:i], path[i+1:]
115
+		}
116
+
117
+		if p == "" {
118
+			if b.Len() == 0 {
119
+				// must be absolute path
120
+				b.WriteRune(filepath.Separator)
121
+			}
122
+			continue
123
+		}
124
+
125
+		// If this is the first segment after the long path prefix, accept the
126
+		// current segment as a volume root or UNC share and move on to the next.
127
+		if b.String() == longPathPrefix {
128
+			b.WriteString(p)
129
+			b.WriteRune(filepath.Separator)
130
+			continue
131
+		}
132
+
133
+		fi, err := os.Lstat(b.String() + p)
134
+		if err != nil {
135
+			return "", err
136
+		}
137
+		if fi.Mode()&os.ModeSymlink == 0 {
138
+			b.WriteString(p)
139
+			if path != "" || (b.Len() == 2 && len(p) == 2 && p[1] == ':') {
140
+				b.WriteRune(filepath.Separator)
141
+			}
142
+			continue
143
+		}
144
+
145
+		// it's a symlink, put it at the front of path
146
+		dest, err := os.Readlink(b.String() + p)
147
+		if err != nil {
148
+			return "", err
149
+		}
150
+		if isAbs(dest) {
151
+			b.Reset()
152
+		}
153
+		path = dest + string(filepath.Separator) + path
154
+	}
155
+	return filepath.Clean(b.String()), nil
156
+}
157
+
158
+func isDriveOrRoot(p string) bool {
159
+	if p == string(filepath.Separator) {
160
+		return true
161
+	}
162
+
163
+	length := len(p)
164
+	if length >= 2 {
165
+		if p[length-1] == ':' && (('a' <= p[length-2] && p[length-2] <= 'z') || ('A' <= p[length-2] && p[length-2] <= 'Z')) {
166
+			return true
167
+		}
168
+	}
169
+	return false
170
+}
171
+
172
+// isAbs is a platform-specific wrapper for filepath.IsAbs. On Windows,
173
+// golang filepath.IsAbs does not consider a path \windows\system32 as absolute
174
+// as it doesn't start with a drive-letter/colon combination. However, in
175
+// docker we need to verify things such as WORKDIR /windows/system32 in
176
+// a Dockerfile (which gets translated to \windows\system32 when being processed
177
+// by the daemon. This SHOULD be treated as absolute from a docker processing
178
+// perspective.
179
+func isAbs(path string) bool {
180
+	if filepath.IsAbs(path) || strings.HasPrefix(path, string(os.PathSeparator)) {
181
+		return true
182
+	}
183
+	return false
184
+}
0 185
new file mode 100644
... ...
@@ -0,0 +1,5 @@
0
+module github.com/moby/sys/symlink
1
+
2
+go 1.13
3
+
4
+require golang.org/x/sys v0.0.0-20200922070232-aee5d888a860