2021-08-23 13:14:53 +00:00
|
|
|
//go:build !windows
|
2016-02-18 07:57:42 +00:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2019-10-21 15:06:20 +00:00
|
|
|
"os/exec"
|
2016-02-18 07:57:42 +00:00
|
|
|
"path/filepath"
|
2016-12-28 22:00:32 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2022-10-09 15:55:13 +00:00
|
|
|
"syscall"
|
2019-09-09 21:06:12 +00:00
|
|
|
"testing"
|
2016-02-18 07:57:42 +00:00
|
|
|
|
2023-07-27 17:59:33 +00:00
|
|
|
"github.com/docker/docker/integration-cli/cli"
|
2020-02-07 13:39:24 +00:00
|
|
|
"gotest.tools/v3/assert"
|
2016-02-18 07:57:42 +00:00
|
|
|
)
|
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLICpSuite) TestCpToContainerWithPermissions(c *testing.T) {
|
2018-12-24 12:25:53 +00:00
|
|
|
testRequires(c, testEnv.IsLocalDaemon, DaemonIsLinux)
|
2016-11-14 13:37:08 +00:00
|
|
|
|
|
|
|
tmpDir := getTestDir(c, "test-cp-to-host-with-permissions")
|
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
|
|
|
|
makeTestContentInDir(c, tmpDir)
|
|
|
|
|
|
|
|
containerName := "permtest"
|
|
|
|
|
2023-07-27 17:59:33 +00:00
|
|
|
exc := cli.DockerCmd(c, "create", "--name", containerName, "busybox", "/bin/sh", "-c", "stat -c '%u %g %a' /permdirtest /permdirtest/permtest").ExitCode
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Equal(c, exc, 0)
|
2023-07-27 17:59:33 +00:00
|
|
|
defer cli.DockerCmd(c, "rm", "-f", containerName)
|
2016-11-14 13:37:08 +00:00
|
|
|
|
|
|
|
srcPath := cpPath(tmpDir, "permdirtest")
|
|
|
|
dstPath := containerCpPath(containerName, "/")
|
|
|
|
|
2019-10-21 15:06:20 +00:00
|
|
|
args := []string{"cp", "-a", srcPath, dstPath}
|
|
|
|
out, _, err := runCommandWithOutput(exec.Command(dockerBinary, args...))
|
|
|
|
assert.NilError(c, err, "output: %v", out)
|
|
|
|
|
|
|
|
out, err = startContainerGetOutput(c, containerName)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err, "output: %v", out)
|
|
|
|
assert.Equal(c, strings.TrimSpace(out), "2 2 700\n65534 65534 400", "output: %v", out)
|
2016-11-14 13:37:08 +00:00
|
|
|
}
|
|
|
|
|
2016-02-18 07:57:42 +00:00
|
|
|
// Check ownership is root, both in non-userns and userns enabled modes
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLICpSuite) TestCpCheckDestOwnership(c *testing.T) {
|
2018-12-24 12:25:53 +00:00
|
|
|
testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon)
|
2016-02-18 07:57:42 +00:00
|
|
|
tmpVolDir := getTestDir(c, "test-cp-tmpvol")
|
|
|
|
containerID := makeTestContainer(c,
|
|
|
|
testContainerOptions{volumes: []string{fmt.Sprintf("%s:/tmpvol", tmpVolDir)}})
|
|
|
|
|
|
|
|
tmpDir := getTestDir(c, "test-cp-to-check-ownership")
|
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
|
|
|
|
makeTestContentInDir(c, tmpDir)
|
|
|
|
|
|
|
|
srcPath := cpPath(tmpDir, "file1")
|
|
|
|
dstPath := containerCpPath(containerID, "/tmpvol", "file1")
|
|
|
|
|
2019-10-21 15:06:20 +00:00
|
|
|
assert.NilError(c, runDockerCp(c, srcPath, dstPath))
|
2016-02-18 07:57:42 +00:00
|
|
|
|
2022-10-09 15:55:13 +00:00
|
|
|
stat, err := os.Stat(filepath.Join(tmpVolDir, "file1"))
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2016-02-18 07:57:42 +00:00
|
|
|
uid, gid, err := getRootUIDGID()
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2022-10-09 15:55:13 +00:00
|
|
|
fi := stat.Sys().(*syscall.Stat_t)
|
|
|
|
assert.Equal(c, fi.Uid, uint32(uid), "Copied file not owned by container root UID")
|
|
|
|
assert.Equal(c, fi.Gid, uint32(gid), "Copied file not owned by container root GID")
|
2016-02-18 07:57:42 +00:00
|
|
|
}
|
2016-12-28 22:00:32 +00:00
|
|
|
|
|
|
|
func getRootUIDGID() (int, int, error) {
|
2018-01-15 14:28:10 +00:00
|
|
|
uidgid := strings.Split(filepath.Base(testEnv.DaemonInfo.DockerRootDir), ".")
|
2016-12-28 22:00:32 +00:00
|
|
|
if len(uidgid) == 1 {
|
2019-10-21 15:06:20 +00:00
|
|
|
// user namespace remapping is not turned on; return 0
|
2016-12-28 22:00:32 +00:00
|
|
|
return 0, 0, nil
|
|
|
|
}
|
|
|
|
uid, err := strconv.Atoi(uidgid[0])
|
|
|
|
if err != nil {
|
|
|
|
return 0, 0, err
|
|
|
|
}
|
|
|
|
gid, err := strconv.Atoi(uidgid[1])
|
|
|
|
if err != nil {
|
|
|
|
return 0, 0, err
|
|
|
|
}
|
|
|
|
return uid, gid, nil
|
|
|
|
}
|