docker_cli_userns_test.go 4.1 KB

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