docker_cli_userns_test.go 3.9 KB

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