docker_cli_userns_test.go 3.5 KB

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