docker_cli_userns_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //go:build !windows
  2. // +build !windows
  3. package main
  4. import (
  5. "fmt"
  6. "os"
  7. "os/exec"
  8. "path"
  9. "path/filepath"
  10. "strconv"
  11. "strings"
  12. "syscall"
  13. "testing"
  14. "github.com/docker/docker/pkg/stringid"
  15. "gotest.tools/v3/assert"
  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 *testing.T) {
  21. testRequires(c, UserNamespaceInKernel)
  22. s.d.StartWithBusybox(c, "--userns-remap", "default")
  23. tmpDir, err := os.MkdirTemp("", "userns")
  24. assert.NilError(c, err)
  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. assert.Equal(c, len(uidgid), 2, fmt.Sprintf("Should have gotten uid/gid strings from root dirname: %s", filepath.Base(s.d.Root)))
  32. uid, err := strconv.Atoi(uidgid[0])
  33. assert.NilError(c, err, "Can't parse uid")
  34. gid, err := strconv.Atoi(uidgid[1])
  35. assert.NilError(c, err, "Can't parse gid")
  36. // writable by the remapped root UID/GID pair
  37. assert.NilError(c, os.Chown(tmpDir, uid, gid))
  38. out, err := s.d.Cmd("run", "-d", "--name", "userns", "-v", tmpDir+":/goofy", "-v", tmpDirNotExists+":/donald", "busybox", "sh", "-c", "touch /goofy/testfile; exec top")
  39. assert.NilError(c, err, "Output: %s", out)
  40. user := s.findUser(c, "userns")
  41. assert.Equal(c, uidgid[0], user)
  42. // check that the created directory is owned by remapped uid:gid
  43. statNotExists, err := os.Stat(tmpDirNotExists)
  44. assert.NilError(c, err)
  45. fi := statNotExists.Sys().(*syscall.Stat_t)
  46. assert.Equal(c, fi.Uid, uint32(uid), "Created directory not owned by remapped root UID")
  47. assert.Equal(c, fi.Gid, uint32(gid), "Created directory not owned by remapped root GID")
  48. pid, err := s.d.Cmd("inspect", "--format={{.State.Pid}}", "userns")
  49. assert.Assert(c, err == nil, "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. _, err = RunCommandPipelineWithOutput(
  53. exec.Command("cat", "/proc/"+strings.TrimSpace(pid)+"/uid_map"),
  54. exec.Command("grep", "-E", fmt.Sprintf("0[[:space:]]+%d[[:space:]]+", uid)))
  55. assert.NilError(c, err)
  56. _, err = RunCommandPipelineWithOutput(
  57. exec.Command("cat", "/proc/"+strings.TrimSpace(pid)+"/gid_map"),
  58. exec.Command("grep", "-E", fmt.Sprintf("0[[:space:]]+%d[[:space:]]+", gid)))
  59. assert.NilError(c, err)
  60. // check that the touched file is owned by remapped uid:gid
  61. stat, err := os.Stat(filepath.Join(tmpDir, "testfile"))
  62. assert.NilError(c, err)
  63. fi = stat.Sys().(*syscall.Stat_t)
  64. assert.Equal(c, fi.Uid, uint32(uid), "Touched file not owned by remapped root UID")
  65. assert.Equal(c, fi.Gid, uint32(gid), "Touched file not owned by remapped root GID")
  66. // use host usernamespace
  67. out, err = s.d.Cmd("run", "-d", "--name", "userns_skip", "--userns", "host", "busybox", "sh", "-c", "touch /goofy/testfile; exec top")
  68. assert.Assert(c, err == nil, "Output: %s", out)
  69. user = s.findUser(c, "userns_skip")
  70. // userns are skipped, user is root
  71. assert.Equal(c, user, "root")
  72. }
  73. // findUser finds the uid or name of the user of the first process that runs in a container
  74. func (s *DockerDaemonSuite) findUser(c *testing.T, container string) string {
  75. out, err := s.d.Cmd("top", container)
  76. assert.Assert(c, err == nil, "Output: %s", out)
  77. rows := strings.Split(out, "\n")
  78. if len(rows) < 2 {
  79. // No process rows founds
  80. c.FailNow()
  81. }
  82. return strings.Fields(rows[1])[0]
  83. }