ipcmode_linux_test.go 11 KB

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