docker_cli_userns_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // +build !windows
  2. package main
  3. import (
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "os/exec"
  8. "path"
  9. "path/filepath"
  10. "strconv"
  11. "strings"
  12. "github.com/docker/docker/pkg/integration"
  13. "github.com/docker/docker/pkg/integration/checker"
  14. "github.com/docker/docker/pkg/stringid"
  15. "github.com/docker/docker/pkg/system"
  16. "github.com/go-check/check"
  17. )
  18. // user namespaces test: run daemon with remapped root setting
  19. // 1. validate uid/gid maps are set properly
  20. // 2. verify that files created are owned by remapped root
  21. func (s *DockerDaemonSuite) TestDaemonUserNamespaceRootSetting(c *check.C) {
  22. testRequires(c, DaemonIsLinux, SameHostDaemon, UserNamespaceInKernel)
  23. s.d.StartWithBusybox(c, "--userns-remap", "default")
  24. tmpDir, err := ioutil.TempDir("", "userns")
  25. c.Assert(err, checker.IsNil)
  26. defer os.RemoveAll(tmpDir)
  27. // Set a non-existent path
  28. tmpDirNotExists := path.Join(os.TempDir(), "userns"+stringid.GenerateRandomID())
  29. defer os.RemoveAll(tmpDirNotExists)
  30. // we need to find the uid and gid of the remapped root from the daemon's root dir info
  31. uidgid := strings.Split(filepath.Base(s.d.Root), ".")
  32. c.Assert(uidgid, checker.HasLen, 2, check.Commentf("Should have gotten uid/gid strings from root dirname: %s", filepath.Base(s.d.Root)))
  33. uid, err := strconv.Atoi(uidgid[0])
  34. c.Assert(err, checker.IsNil, check.Commentf("Can't parse uid"))
  35. gid, err := strconv.Atoi(uidgid[1])
  36. c.Assert(err, checker.IsNil, check.Commentf("Can't parse gid"))
  37. // writable by the remapped root UID/GID pair
  38. c.Assert(os.Chown(tmpDir, uid, gid), checker.IsNil)
  39. out, err := s.d.Cmd("run", "-d", "--name", "userns", "-v", tmpDir+":/goofy", "-v", tmpDirNotExists+":/donald", "busybox", "sh", "-c", "touch /goofy/testfile; top")
  40. c.Assert(err, checker.IsNil, check.Commentf("Output: %s", out))
  41. user := s.findUser(c, "userns")
  42. c.Assert(uidgid[0], checker.Equals, user)
  43. // check that the created directory is owned by remapped uid:gid
  44. statNotExists, err := system.Stat(tmpDirNotExists)
  45. c.Assert(err, checker.IsNil)
  46. c.Assert(statNotExists.UID(), checker.Equals, uint32(uid), check.Commentf("Created directory not owned by remapped root UID"))
  47. c.Assert(statNotExists.GID(), checker.Equals, uint32(gid), check.Commentf("Created directory not owned by remapped root GID"))
  48. pid, err := s.d.Cmd("inspect", "--format={{.State.Pid}}", "userns")
  49. c.Assert(err, checker.IsNil, check.Commentf("Could not inspect running container: out: %q", pid))
  50. // check the uid and gid maps for the PID to ensure root is remapped
  51. // (cmd = cat /proc/<pid>/uid_map | grep -E '0\s+9999\s+1')
  52. out, rc1, err := integration.RunCommandPipelineWithOutput(
  53. exec.Command("cat", "/proc/"+strings.TrimSpace(pid)+"/uid_map"),
  54. exec.Command("grep", "-E", fmt.Sprintf("0[[:space:]]+%d[[:space:]]+", uid)))
  55. c.Assert(rc1, checker.Equals, 0, check.Commentf("Didn't match uid_map: output: %s", out))
  56. out, rc2, err := integration.RunCommandPipelineWithOutput(
  57. exec.Command("cat", "/proc/"+strings.TrimSpace(pid)+"/gid_map"),
  58. exec.Command("grep", "-E", fmt.Sprintf("0[[:space:]]+%d[[:space:]]+", gid)))
  59. c.Assert(rc2, checker.Equals, 0, check.Commentf("Didn't match gid_map: output: %s", out))
  60. // check that the touched file is owned by remapped uid:gid
  61. stat, err := system.Stat(filepath.Join(tmpDir, "testfile"))
  62. c.Assert(err, checker.IsNil)
  63. c.Assert(stat.UID(), checker.Equals, uint32(uid), check.Commentf("Touched file not owned by remapped root UID"))
  64. c.Assert(stat.GID(), checker.Equals, uint32(gid), check.Commentf("Touched file not owned by remapped root GID"))
  65. // use host usernamespace
  66. out, err = s.d.Cmd("run", "-d", "--name", "userns_skip", "--userns", "host", "busybox", "sh", "-c", "touch /goofy/testfile; top")
  67. c.Assert(err, checker.IsNil, check.Commentf("Output: %s", out))
  68. user = s.findUser(c, "userns_skip")
  69. // userns are skipped, user is root
  70. c.Assert(user, checker.Equals, "root")
  71. }
  72. // findUser finds the uid or name of the user of the first process that runs in a container
  73. func (s *DockerDaemonSuite) findUser(c *check.C, container string) string {
  74. out, err := s.d.Cmd("top", container)
  75. c.Assert(err, checker.IsNil, check.Commentf("Output: %s", out))
  76. rows := strings.Split(out, "\n")
  77. if len(rows) < 2 {
  78. // No process rows founds
  79. c.FailNow()
  80. }
  81. return strings.Fields(rows[1])[0]
  82. }