ipcmode_linux_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. package container // import "github.com/docker/docker/integration/container"
  2. import (
  3. "bufio"
  4. "context"
  5. "os"
  6. "regexp"
  7. "strings"
  8. "testing"
  9. "github.com/docker/docker/api/types"
  10. containertypes "github.com/docker/docker/api/types/container"
  11. "github.com/docker/docker/api/types/versions"
  12. "github.com/docker/docker/client"
  13. "github.com/docker/docker/integration/internal/container"
  14. "github.com/docker/docker/testutil/daemon"
  15. "github.com/docker/docker/testutil/request"
  16. "gotest.tools/v3/assert"
  17. is "gotest.tools/v3/assert/cmp"
  18. "gotest.tools/v3/fs"
  19. "gotest.tools/v3/skip"
  20. )
  21. // testIpcCheckDevExists checks whether a given mount (identified by its
  22. // major:minor pair from /proc/self/mountinfo) exists on the host system.
  23. //
  24. // The format of /proc/self/mountinfo is like:
  25. //
  26. // 29 23 0:24 / /dev/shm rw,nosuid,nodev shared:4 - tmpfs tmpfs rw
  27. // ^^^^\
  28. // - this is the minor:major we look for
  29. func testIpcCheckDevExists(mm string) (bool, error) {
  30. f, err := os.Open("/proc/self/mountinfo")
  31. if err != nil {
  32. return false, err
  33. }
  34. defer f.Close()
  35. s := bufio.NewScanner(f)
  36. for s.Scan() {
  37. fields := strings.Fields(s.Text())
  38. if len(fields) < 7 {
  39. continue
  40. }
  41. if fields[2] == mm {
  42. return true, nil
  43. }
  44. }
  45. return false, s.Err()
  46. }
  47. // testIpcNonePrivateShareable is a helper function to test "none",
  48. // "private" and "shareable" modes.
  49. func testIpcNonePrivateShareable(t *testing.T, mode string, mustBeMounted bool, mustBeShared bool) {
  50. defer setupTest(t)()
  51. cfg := containertypes.Config{
  52. Image: "busybox",
  53. Cmd: []string{"top"},
  54. }
  55. hostCfg := containertypes.HostConfig{
  56. IpcMode: containertypes.IpcMode(mode),
  57. }
  58. client := testEnv.APIClient()
  59. ctx := context.Background()
  60. resp, err := client.ContainerCreate(ctx, &cfg, &hostCfg, nil, nil, "")
  61. assert.NilError(t, err)
  62. assert.Check(t, is.Equal(len(resp.Warnings), 0))
  63. err = client.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{})
  64. assert.NilError(t, err)
  65. // get major:minor pair for /dev/shm from container's /proc/self/mountinfo
  66. cmd := "awk '($5 == \"/dev/shm\") {printf $3}' /proc/self/mountinfo"
  67. result, err := container.Exec(ctx, client, resp.ID, []string{"sh", "-c", cmd})
  68. assert.NilError(t, err)
  69. mm := result.Combined()
  70. if !mustBeMounted {
  71. assert.Check(t, is.Equal(mm, ""))
  72. // no more checks to perform
  73. return
  74. }
  75. assert.Check(t, is.Equal(true, regexp.MustCompile("^[0-9]+:[0-9]+$").MatchString(mm)))
  76. shared, err := testIpcCheckDevExists(mm)
  77. assert.NilError(t, err)
  78. t.Logf("[testIpcPrivateShareable] ipcmode: %v, ipcdev: %v, shared: %v, mustBeShared: %v\n", mode, mm, shared, mustBeShared)
  79. assert.Check(t, is.Equal(shared, mustBeShared))
  80. }
  81. // TestIpcModeNone checks the container "none" IPC mode
  82. // (--ipc none) works as expected. It makes sure there is no
  83. // /dev/shm mount inside the container.
  84. func TestIpcModeNone(t *testing.T) {
  85. skip.If(t, testEnv.IsRemoteDaemon)
  86. testIpcNonePrivateShareable(t, "none", false, false)
  87. }
  88. // TestAPIIpcModePrivate checks the container private IPC mode
  89. // (--ipc private) works as expected. It gets the minor:major pair
  90. // of /dev/shm mount from the container, and makes sure there is no
  91. // such pair on the host.
  92. func TestIpcModePrivate(t *testing.T) {
  93. skip.If(t, testEnv.IsRemoteDaemon)
  94. testIpcNonePrivateShareable(t, "private", true, false)
  95. }
  96. // TestAPIIpcModeShareable checks the container shareable IPC mode
  97. // (--ipc shareable) works as expected. It gets the minor:major pair
  98. // of /dev/shm mount from the container, and makes sure such pair
  99. // also exists on the host.
  100. func TestIpcModeShareable(t *testing.T) {
  101. skip.If(t, testEnv.IsRemoteDaemon)
  102. skip.If(t, testEnv.IsRootless, "cannot test /dev/shm in rootless")
  103. testIpcNonePrivateShareable(t, "shareable", true, true)
  104. }
  105. // testIpcContainer is a helper function to test --ipc container:NNN mode in various scenarios
  106. func testIpcContainer(t *testing.T, donorMode string, mustWork bool) {
  107. t.Helper()
  108. defer setupTest(t)()
  109. cfg := containertypes.Config{
  110. Image: "busybox",
  111. Cmd: []string{"top"},
  112. }
  113. hostCfg := containertypes.HostConfig{
  114. IpcMode: containertypes.IpcMode(donorMode),
  115. }
  116. ctx := context.Background()
  117. client := testEnv.APIClient()
  118. // create and start the "donor" container
  119. resp, err := client.ContainerCreate(ctx, &cfg, &hostCfg, nil, nil, "")
  120. assert.NilError(t, err)
  121. assert.Check(t, is.Equal(len(resp.Warnings), 0))
  122. name1 := resp.ID
  123. err = client.ContainerStart(ctx, name1, types.ContainerStartOptions{})
  124. assert.NilError(t, err)
  125. // create and start the second container
  126. hostCfg.IpcMode = containertypes.IpcMode("container:" + name1)
  127. resp, err = client.ContainerCreate(ctx, &cfg, &hostCfg, nil, nil, "")
  128. assert.NilError(t, err)
  129. assert.Check(t, is.Equal(len(resp.Warnings), 0))
  130. name2 := resp.ID
  131. err = client.ContainerStart(ctx, name2, types.ContainerStartOptions{})
  132. if !mustWork {
  133. // start should fail with a specific error
  134. assert.Check(t, is.ErrorContains(err, "non-shareable IPC"))
  135. // no more checks to perform here
  136. return
  137. }
  138. // start should succeed
  139. assert.NilError(t, err)
  140. // check that IPC is shared
  141. // 1. create a file in the first container
  142. _, err = container.Exec(ctx, client, name1, []string{"sh", "-c", "printf covfefe > /dev/shm/bar"})
  143. assert.NilError(t, err)
  144. // 2. check it's the same file in the second one
  145. result, err := container.Exec(ctx, client, name2, []string{"cat", "/dev/shm/bar"})
  146. assert.NilError(t, err)
  147. out := result.Combined()
  148. assert.Check(t, is.Equal(true, regexp.MustCompile("^covfefe$").MatchString(out)))
  149. }
  150. // TestAPIIpcModeShareableAndPrivate checks that
  151. // 1) a container created with --ipc container:ID can use IPC of another shareable container.
  152. // 2) a container created with --ipc container:ID can NOT use IPC of another private container.
  153. func TestAPIIpcModeShareableAndContainer(t *testing.T) {
  154. skip.If(t, testEnv.IsRemoteDaemon)
  155. testIpcContainer(t, "shareable", true)
  156. testIpcContainer(t, "private", false)
  157. }
  158. /* TestAPIIpcModeHost checks that a container created with --ipc host
  159. * can use IPC of the host system.
  160. */
  161. func TestAPIIpcModeHost(t *testing.T) {
  162. skip.If(t, testEnv.IsRemoteDaemon)
  163. skip.If(t, testEnv.IsUserNamespace)
  164. skip.If(t, testEnv.IsRootless, "cannot test /dev/shm in rootless")
  165. cfg := containertypes.Config{
  166. Image: "busybox",
  167. Cmd: []string{"top"},
  168. }
  169. hostCfg := containertypes.HostConfig{
  170. IpcMode: containertypes.IPCModeHost,
  171. }
  172. ctx := context.Background()
  173. client := testEnv.APIClient()
  174. resp, err := client.ContainerCreate(ctx, &cfg, &hostCfg, nil, nil, "")
  175. assert.NilError(t, err)
  176. assert.Check(t, is.Equal(len(resp.Warnings), 0))
  177. name := resp.ID
  178. err = client.ContainerStart(ctx, name, types.ContainerStartOptions{})
  179. assert.NilError(t, err)
  180. // check that IPC is shared
  181. // 1. create a file inside container
  182. _, err = container.Exec(ctx, client, name, []string{"sh", "-c", "printf covfefe > /dev/shm/." + name})
  183. assert.NilError(t, err)
  184. // 2. check it's the same on the host
  185. bytes, err := os.ReadFile("/dev/shm/." + name)
  186. assert.NilError(t, err)
  187. assert.Check(t, is.Equal("covfefe", string(bytes)))
  188. // 3. clean up
  189. _, err = container.Exec(ctx, client, name, []string{"rm", "-f", "/dev/shm/." + name})
  190. assert.NilError(t, err)
  191. }
  192. // testDaemonIpcPrivateShareable is a helper function to test "private" and "shareable" daemon default ipc modes.
  193. func testDaemonIpcPrivateShareable(t *testing.T, mustBeShared bool, arg ...string) {
  194. defer setupTest(t)()
  195. d := daemon.New(t)
  196. d.StartWithBusybox(t, arg...)
  197. defer d.Stop(t)
  198. c := d.NewClientT(t)
  199. cfg := containertypes.Config{
  200. Image: "busybox",
  201. Cmd: []string{"top"},
  202. }
  203. ctx := context.Background()
  204. resp, err := c.ContainerCreate(ctx, &cfg, &containertypes.HostConfig{}, nil, nil, "")
  205. assert.NilError(t, err)
  206. assert.Check(t, is.Equal(len(resp.Warnings), 0))
  207. err = c.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{})
  208. assert.NilError(t, err)
  209. // get major:minor pair for /dev/shm from container's /proc/self/mountinfo
  210. cmd := "awk '($5 == \"/dev/shm\") {printf $3}' /proc/self/mountinfo"
  211. result, err := container.Exec(ctx, c, resp.ID, []string{"sh", "-c", cmd})
  212. assert.NilError(t, err)
  213. mm := result.Combined()
  214. assert.Check(t, is.Equal(true, regexp.MustCompile("^[0-9]+:[0-9]+$").MatchString(mm)))
  215. shared, err := testIpcCheckDevExists(mm)
  216. assert.NilError(t, err)
  217. t.Logf("[testDaemonIpcPrivateShareable] ipcdev: %v, shared: %v, mustBeShared: %v\n", mm, shared, mustBeShared)
  218. assert.Check(t, is.Equal(shared, mustBeShared))
  219. }
  220. // TestDaemonIpcModeShareable checks that --default-ipc-mode shareable works as intended.
  221. func TestDaemonIpcModeShareable(t *testing.T) {
  222. skip.If(t, testEnv.IsRemoteDaemon)
  223. skip.If(t, testEnv.IsRootless, "cannot test /dev/shm in rootless")
  224. testDaemonIpcPrivateShareable(t, true, "--default-ipc-mode", "shareable")
  225. }
  226. // TestDaemonIpcModePrivate checks that --default-ipc-mode private works as intended.
  227. func TestDaemonIpcModePrivate(t *testing.T) {
  228. skip.If(t, testEnv.IsRemoteDaemon)
  229. testDaemonIpcPrivateShareable(t, false, "--default-ipc-mode", "private")
  230. }
  231. // used to check if an IpcMode given in config works as intended
  232. func testDaemonIpcFromConfig(t *testing.T, mode string, mustExist bool) {
  233. skip.If(t, testEnv.IsRootless, "cannot test /dev/shm in rootless")
  234. config := `{"default-ipc-mode": "` + mode + `"}`
  235. file := fs.NewFile(t, "test-daemon-ipc-config", fs.WithContent(config))
  236. defer file.Remove()
  237. testDaemonIpcPrivateShareable(t, mustExist, "--config-file", file.Path())
  238. }
  239. // TestDaemonIpcModePrivateFromConfig checks that "default-ipc-mode: private" config works as intended.
  240. func TestDaemonIpcModePrivateFromConfig(t *testing.T) {
  241. skip.If(t, testEnv.IsRemoteDaemon)
  242. testDaemonIpcFromConfig(t, "private", false)
  243. }
  244. // TestDaemonIpcModeShareableFromConfig checks that "default-ipc-mode: shareable" config works as intended.
  245. func TestDaemonIpcModeShareableFromConfig(t *testing.T) {
  246. skip.If(t, testEnv.IsRemoteDaemon)
  247. testDaemonIpcFromConfig(t, "shareable", true)
  248. }
  249. // TestIpcModeOlderClient checks that older client gets shareable IPC mode
  250. // by default, even when the daemon default is private.
  251. func TestIpcModeOlderClient(t *testing.T) {
  252. skip.If(t, versions.LessThan(testEnv.DaemonAPIVersion(), "1.40"), "requires a daemon with DefaultIpcMode: private")
  253. c := testEnv.APIClient()
  254. skip.If(t, versions.LessThan(c.ClientVersion(), "1.40"), "requires client API >= 1.40")
  255. t.Parallel()
  256. ctx := context.Background()
  257. // pre-check: default ipc mode in daemon is private
  258. cID := container.Create(ctx, t, c, container.WithAutoRemove)
  259. inspect, err := c.ContainerInspect(ctx, cID)
  260. assert.NilError(t, err)
  261. assert.Check(t, is.Equal(string(inspect.HostConfig.IpcMode), "private"))
  262. // main check: using older client creates "shareable" container
  263. c = request.NewAPIClient(t, client.WithVersion("1.39"))
  264. cID = container.Create(ctx, t, c, container.WithAutoRemove)
  265. inspect, err = c.ContainerInspect(ctx, cID)
  266. assert.NilError(t, err)
  267. assert.Check(t, is.Equal(string(inspect.HostConfig.IpcMode), "shareable"))
  268. }