mounts_linux_test.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. package container // import "github.com/docker/docker/integration/container"
  2. import (
  3. "context"
  4. "fmt"
  5. "path/filepath"
  6. "testing"
  7. "github.com/docker/docker/api/types"
  8. "github.com/docker/docker/api/types/container"
  9. "github.com/docker/docker/api/types/mount"
  10. "github.com/docker/docker/api/types/network"
  11. "github.com/docker/docker/client"
  12. "github.com/docker/docker/internal/test/request"
  13. "github.com/docker/docker/pkg/system"
  14. "github.com/gotestyourself/gotestyourself/assert"
  15. is "github.com/gotestyourself/gotestyourself/assert/cmp"
  16. "github.com/gotestyourself/gotestyourself/fs"
  17. "github.com/gotestyourself/gotestyourself/skip"
  18. )
  19. func TestContainerNetworkMountsNoChown(t *testing.T) {
  20. // chown only applies to Linux bind mounted volumes; must be same host to verify
  21. skip.If(t, testEnv.DaemonInfo.OSType != "linux" || testEnv.IsRemoteDaemon())
  22. defer setupTest(t)()
  23. ctx := context.Background()
  24. tmpDir := fs.NewDir(t, "network-file-mounts", fs.WithMode(0755), fs.WithFile("nwfile", "network file bind mount", fs.WithMode(0644)))
  25. defer tmpDir.Remove()
  26. tmpNWFileMount := tmpDir.Join("nwfile")
  27. config := container.Config{
  28. Image: "busybox",
  29. }
  30. hostConfig := container.HostConfig{
  31. Mounts: []mount.Mount{
  32. {
  33. Type: "bind",
  34. Source: tmpNWFileMount,
  35. Target: "/etc/resolv.conf",
  36. },
  37. {
  38. Type: "bind",
  39. Source: tmpNWFileMount,
  40. Target: "/etc/hostname",
  41. },
  42. {
  43. Type: "bind",
  44. Source: tmpNWFileMount,
  45. Target: "/etc/hosts",
  46. },
  47. },
  48. }
  49. cli, err := client.NewEnvClient()
  50. assert.NilError(t, err)
  51. defer cli.Close()
  52. ctrCreate, err := cli.ContainerCreate(ctx, &config, &hostConfig, &network.NetworkingConfig{}, "")
  53. assert.NilError(t, err)
  54. // container will exit immediately because of no tty, but we only need the start sequence to test the condition
  55. err = cli.ContainerStart(ctx, ctrCreate.ID, types.ContainerStartOptions{})
  56. assert.NilError(t, err)
  57. // Check that host-located bind mount network file did not change ownership when the container was started
  58. // Note: If the user specifies a mountpath from the host, we should not be
  59. // attempting to chown files outside the daemon's metadata directory
  60. // (represented by `daemon.repository` at init time).
  61. // This forces users who want to use user namespaces to handle the
  62. // ownership needs of any external files mounted as network files
  63. // (/etc/resolv.conf, /etc/hosts, /etc/hostname) separately from the
  64. // daemon. In all other volume/bind mount situations we have taken this
  65. // same line--we don't chown host file content.
  66. // See GitHub PR 34224 for details.
  67. statT, err := system.Stat(tmpNWFileMount)
  68. assert.NilError(t, err)
  69. assert.Check(t, is.Equal(uint32(0), statT.UID()), "bind mounted network file should not change ownership from root")
  70. }
  71. func TestMountDaemonRoot(t *testing.T) {
  72. skip.If(t, testEnv.DaemonInfo.OSType != "linux" || testEnv.IsRemoteDaemon())
  73. t.Parallel()
  74. client := request.NewAPIClient(t)
  75. ctx := context.Background()
  76. info, err := client.Info(ctx)
  77. if err != nil {
  78. t.Fatal(err)
  79. }
  80. for _, test := range []struct {
  81. desc string
  82. propagation mount.Propagation
  83. expected mount.Propagation
  84. }{
  85. {
  86. desc: "default",
  87. propagation: "",
  88. expected: mount.PropagationRSlave,
  89. },
  90. {
  91. desc: "private",
  92. propagation: mount.PropagationPrivate,
  93. },
  94. {
  95. desc: "rprivate",
  96. propagation: mount.PropagationRPrivate,
  97. },
  98. {
  99. desc: "slave",
  100. propagation: mount.PropagationSlave,
  101. },
  102. {
  103. desc: "rslave",
  104. propagation: mount.PropagationRSlave,
  105. expected: mount.PropagationRSlave,
  106. },
  107. {
  108. desc: "shared",
  109. propagation: mount.PropagationShared,
  110. },
  111. {
  112. desc: "rshared",
  113. propagation: mount.PropagationRShared,
  114. expected: mount.PropagationRShared,
  115. },
  116. } {
  117. t.Run(test.desc, func(t *testing.T) {
  118. test := test
  119. t.Parallel()
  120. propagationSpec := fmt.Sprintf(":%s", test.propagation)
  121. if test.propagation == "" {
  122. propagationSpec = ""
  123. }
  124. bindSpecRoot := info.DockerRootDir + ":" + "/foo" + propagationSpec
  125. bindSpecSub := filepath.Join(info.DockerRootDir, "containers") + ":/foo" + propagationSpec
  126. for name, hc := range map[string]*container.HostConfig{
  127. "bind root": {Binds: []string{bindSpecRoot}},
  128. "bind subpath": {Binds: []string{bindSpecSub}},
  129. "mount root": {
  130. Mounts: []mount.Mount{
  131. {
  132. Type: mount.TypeBind,
  133. Source: info.DockerRootDir,
  134. Target: "/foo",
  135. BindOptions: &mount.BindOptions{Propagation: test.propagation},
  136. },
  137. },
  138. },
  139. "mount subpath": {
  140. Mounts: []mount.Mount{
  141. {
  142. Type: mount.TypeBind,
  143. Source: filepath.Join(info.DockerRootDir, "containers"),
  144. Target: "/foo",
  145. BindOptions: &mount.BindOptions{Propagation: test.propagation},
  146. },
  147. },
  148. },
  149. } {
  150. t.Run(name, func(t *testing.T) {
  151. hc := hc
  152. t.Parallel()
  153. c, err := client.ContainerCreate(ctx, &container.Config{
  154. Image: "busybox",
  155. Cmd: []string{"true"},
  156. }, hc, nil, "")
  157. if err != nil {
  158. if test.expected != "" {
  159. t.Fatal(err)
  160. }
  161. // expected an error, so this is ok and should not continue
  162. return
  163. }
  164. if test.expected == "" {
  165. t.Fatal("expected create to fail")
  166. }
  167. defer func() {
  168. if err := client.ContainerRemove(ctx, c.ID, types.ContainerRemoveOptions{Force: true}); err != nil {
  169. panic(err)
  170. }
  171. }()
  172. inspect, err := client.ContainerInspect(ctx, c.ID)
  173. if err != nil {
  174. t.Fatal(err)
  175. }
  176. if len(inspect.Mounts) != 1 {
  177. t.Fatalf("unexpected number of mounts: %+v", inspect.Mounts)
  178. }
  179. m := inspect.Mounts[0]
  180. if m.Propagation != test.expected {
  181. t.Fatalf("got unexpected propagation mode, expected %q, got: %v", test.expected, m.Propagation)
  182. }
  183. })
  184. }
  185. })
  186. }
  187. }