Skip to content

Commit 6f34536

Browse files
authored
Merge pull request #3323 from apostasie/b-dev-cp
Rewrite cp
2 parents 3cf6903 + 47d6d0a commit 6f34536

File tree

6 files changed

+1746
-562
lines changed

6 files changed

+1746
-562
lines changed
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package container
18+
19+
import (
20+
"fmt"
21+
"os"
22+
"path/filepath"
23+
"testing"
24+
25+
"gotest.tools/v3/assert"
26+
"gotest.tools/v3/icmd"
27+
28+
"github.com/containerd/nerdctl/v2/pkg/containerutil"
29+
"github.com/containerd/nerdctl/v2/pkg/rootlessutil"
30+
"github.com/containerd/nerdctl/v2/pkg/testutil"
31+
)
32+
33+
// This is a separate set of tests for cp specifically meant to test corner or extreme cases that do not fit in the normal testing rig
34+
// because of their complexity
35+
36+
func TestCopyAcid(t *testing.T) {
37+
t.Parallel()
38+
39+
t.Run("Travelling along volumes w/o read-only", func(t *testing.T) {
40+
t.Parallel()
41+
testID := testutil.Identifier(t)
42+
tempDir := t.TempDir()
43+
base := testutil.NewBase(t)
44+
base.Dir = tempDir
45+
46+
sourceFile := filepath.Join(tempDir, "hostfile")
47+
sourceFileContent := []byte(testID)
48+
49+
roContainer := testID + "-ro"
50+
rwContainer := testID + "-rw"
51+
52+
setup := func() {
53+
base.Cmd("volume", "create", testID+"-1-ro").AssertOK()
54+
base.Cmd("volume", "create", testID+"-2-rw").AssertOK()
55+
base.Cmd("volume", "create", testID+"-3-rw").AssertOK()
56+
base.Cmd("run", "-d", "-w", containerCwd, "--name", roContainer, "--read-only",
57+
"-v", fmt.Sprintf("%s:%s:ro", testID+"-1-ro", "/vol1/dir1/ro"),
58+
"-v", fmt.Sprintf("%s:%s", testID+"-2-rw", "/vol2/dir2/rw"),
59+
testutil.CommonImage, "sleep", "Inf",
60+
).AssertOK()
61+
base.Cmd("run", "-d", "-w", containerCwd, "--name", rwContainer,
62+
"-v", fmt.Sprintf("%s:%s:ro", testID+"-1-ro", "/vol1/dir1/ro"),
63+
"-v", fmt.Sprintf("%s:%s", testID+"-3-rw", "/vol3/dir3/rw"),
64+
testutil.CommonImage, "sleep", "Inf",
65+
).AssertOK()
66+
67+
base.Cmd("exec", rwContainer, "sh", "-euxc", "cd /vol3/dir3/rw; ln -s ../../../ relativelinktoroot").AssertOK()
68+
base.Cmd("exec", rwContainer, "sh", "-euxc", "cd /vol3/dir3/rw; ln -s / absolutelinktoroot").AssertOK()
69+
base.Cmd("exec", roContainer, "sh", "-euxc", "cd /vol2/dir2/rw; ln -s ../../../ relativelinktoroot").AssertOK()
70+
base.Cmd("exec", roContainer, "sh", "-euxc", "cd /vol2/dir2/rw; ln -s / absolutelinktoroot").AssertOK()
71+
// Create file on the host
72+
err := os.WriteFile(sourceFile, sourceFileContent, filePerm)
73+
assert.NilError(t, err)
74+
}
75+
76+
tearDown := func() {
77+
base.Cmd("rm", "-f", roContainer).Run()
78+
base.Cmd("rm", "-f", rwContainer).Run()
79+
base.Cmd("volume", "rm", testID+"-1-ro").Run()
80+
base.Cmd("volume", "rm", testID+"-2-rw").Run()
81+
base.Cmd("volume", "rm", testID+"-3-rw").Run()
82+
}
83+
84+
t.Cleanup(tearDown)
85+
tearDown()
86+
87+
setup()
88+
89+
expectedErr := containerutil.ErrTargetIsReadOnly.Error()
90+
if testutil.GetTarget() == testutil.Docker {
91+
expectedErr = ""
92+
}
93+
94+
t.Run("Cannot copy into a read-only root", func(t *testing.T) {
95+
t.Parallel()
96+
97+
base.Cmd("cp", sourceFile, roContainer+":/").Assert(icmd.Expected{
98+
ExitCode: 1,
99+
Err: expectedErr,
100+
})
101+
})
102+
103+
t.Run("Cannot copy into a read-only mount, in a rw container", func(t *testing.T) {
104+
t.Parallel()
105+
106+
base.Cmd("cp", sourceFile, rwContainer+":/vol1/dir1/ro").Assert(icmd.Expected{
107+
ExitCode: 1,
108+
Err: expectedErr,
109+
})
110+
})
111+
112+
t.Run("Can copy into a read-write mount in a read-only container", func(t *testing.T) {
113+
t.Parallel()
114+
115+
base.Cmd("cp", sourceFile, roContainer+":/vol2/dir2/rw").Assert(icmd.Expected{
116+
ExitCode: 0,
117+
})
118+
})
119+
120+
t.Run("Traverse read-only locations to a read-write location", func(t *testing.T) {
121+
t.Parallel()
122+
123+
base.Cmd("cp", sourceFile, roContainer+":/vol1/dir1/ro/../../../vol2/dir2/rw").Assert(icmd.Expected{
124+
ExitCode: 0,
125+
})
126+
})
127+
128+
t.Run("Follow an absolute symlink inside a read-write mount to a read-only root", func(t *testing.T) {
129+
t.Parallel()
130+
131+
base.Cmd("cp", sourceFile, roContainer+":/vol2/dir2/rw/absolutelinktoroot").Assert(icmd.Expected{
132+
ExitCode: 1,
133+
Err: expectedErr,
134+
})
135+
})
136+
137+
t.Run("Follow am absolute symlink inside a read-write mount to a read-only mount", func(t *testing.T) {
138+
t.Parallel()
139+
140+
base.Cmd("cp", sourceFile, rwContainer+":/vol3/dir3/rw/absolutelinktoroot/vol1/dir1/ro").Assert(icmd.Expected{
141+
ExitCode: 1,
142+
Err: expectedErr,
143+
})
144+
})
145+
146+
t.Run("Follow a relative symlink inside a read-write location to a read-only root", func(t *testing.T) {
147+
t.Parallel()
148+
149+
base.Cmd("cp", sourceFile, roContainer+":/vol2/dir2/rw/relativelinktoroot").Assert(icmd.Expected{
150+
ExitCode: 1,
151+
Err: expectedErr,
152+
})
153+
})
154+
155+
t.Run("Follow a relative symlink inside a read-write location to a read-only mount", func(t *testing.T) {
156+
t.Parallel()
157+
158+
base.Cmd("cp", sourceFile, rwContainer+":/vol3/dir3/rw/relativelinktoroot/vol1/dir1/ro").Assert(icmd.Expected{
159+
ExitCode: 1,
160+
Err: expectedErr,
161+
})
162+
})
163+
164+
t.Run("Cannot copy into a HOST read-only location", func(t *testing.T) {
165+
t.Parallel()
166+
167+
// Root will just ignore the 000 permission on the host directory.
168+
if !rootlessutil.IsRootless() {
169+
t.Skip("This test does not work rootful")
170+
}
171+
172+
err := os.MkdirAll(filepath.Join(tempDir, "rotest"), 0o000)
173+
assert.NilError(t, err)
174+
base.Cmd("cp", roContainer+":/etc/issue", filepath.Join(tempDir, "rotest")).Assert(icmd.Expected{
175+
ExitCode: 1,
176+
Err: expectedErr,
177+
})
178+
})
179+
180+
})
181+
}

cmd/nerdctl/container/container_cp_linux.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,16 +115,16 @@ func processCpOptions(cmd *cobra.Command, args []string) (types.ContainerCpOptio
115115
}
116116

117117
container2host := srcSpec.Container != nil
118-
var container string
118+
var containerReq string
119119
if container2host {
120-
container = *srcSpec.Container
120+
containerReq = *srcSpec.Container
121121
} else {
122-
container = *destSpec.Container
122+
containerReq = *destSpec.Container
123123
}
124124
return types.ContainerCpOptions{
125125
GOptions: globalOptions,
126126
Container2Host: container2host,
127-
ContainerReq: container,
127+
ContainerReq: containerReq,
128128
DestPath: destSpec.Path,
129129
SrcPath: srcSpec.Path,
130130
FollowSymLink: flagL,

0 commit comments

Comments
 (0)