ipcmode_linux_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. //
  29. //nolint:dupword
  30. func testIpcCheckDevExists(mm string) (bool, error) {
  31. f, err := os.Open("/proc/self/mountinfo")
  32. if err != nil {
  33. return false, err
  34. }
  35. defer f.Close()
  36. s := bufio.NewScanner(f)
  37. for s.Scan() {
  38. fields := strings.Fields(s.Text())
  39. if len(fields) < 7 {
  40. continue
  41. }
  42. if fields[2] == mm {
  43. return true, nil
  44. }
  45. }
  46. return false, s.Err()
  47. }
  48. // testIpcNonePrivateShareable is a helper function to test "none",
  49. // "private" and "shareable" modes.
  50. func testIpcNonePrivateShareable(t *testing.T, mode string, mustBeMounted bool, mustBeShared bool) {
  51. ctx := setupTest(t)
  52. cfg := containertypes.Config{
  53. Image: "busybox",
  54. Cmd: []string{"top"},
  55. }
  56. hostCfg := containertypes.HostConfig{
  57. IpcMode: containertypes.IpcMode(mode),
  58. }
  59. apiClient := testEnv.APIClient()
  60. resp, err := apiClient.ContainerCreate(ctx, &cfg, &hostCfg, nil, nil, "")
  61. assert.NilError(t, err)
  62. assert.Check(t, is.Equal(len(resp.Warnings), 0))
  63. err = apiClient.ContainerStart(ctx, resp.ID, containertypes.StartOptions{})
  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, apiClient, 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, "no support for --ipc=shareable 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. ctx := 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. apiClient := testEnv.APIClient()
  117. // create and start the "donor" container
  118. resp, err := apiClient.ContainerCreate(ctx, &cfg, &hostCfg, nil, nil, "")
  119. assert.NilError(t, err)
  120. assert.Check(t, is.Equal(len(resp.Warnings), 0))
  121. name1 := resp.ID
  122. err = apiClient.ContainerStart(ctx, name1, containertypes.StartOptions{})
  123. assert.NilError(t, err)
  124. // create and start the second container
  125. hostCfg.IpcMode = containertypes.IpcMode("container:" + name1)
  126. resp, err = apiClient.ContainerCreate(ctx, &cfg, &hostCfg, nil, nil, "")
  127. assert.NilError(t, err)
  128. assert.Check(t, is.Equal(len(resp.Warnings), 0))
  129. name2 := resp.ID
  130. err = apiClient.ContainerStart(ctx, name2, containertypes.StartOptions{})
  131. if !mustWork {
  132. // start should fail with a specific error
  133. assert.Check(t, is.ErrorContains(err, "non-shareable IPC"))
  134. // no more checks to perform here
  135. return
  136. }
  137. // start should succeed
  138. assert.NilError(t, err)
  139. // check that IPC is shared
  140. // 1. create a file in the first container
  141. _, err = container.Exec(ctx, apiClient, name1, []string{"sh", "-c", "printf covfefe > /dev/shm/bar"})
  142. assert.NilError(t, err)
  143. // 2. check it's the same file in the second one
  144. result, err := container.Exec(ctx, apiClient, name2, []string{"cat", "/dev/shm/bar"})
  145. assert.NilError(t, err)
  146. out := result.Combined()
  147. assert.Check(t, is.Equal(true, regexp.MustCompile("^covfefe$").MatchString(out)))
  148. }
  149. // TestAPIIpcModeShareableAndPrivate checks that
  150. // 1) a container created with --ipc container:ID can use IPC of another shareable container.
  151. // 2) a container created with --ipc container:ID can NOT use IPC of another private container.
  152. func TestAPIIpcModeShareableAndContainer(t *testing.T) {
  153. skip.If(t, testEnv.IsRemoteDaemon)
  154. testIpcContainer(t, "shareable", true)
  155. testIpcContainer(t, "private", false)
  156. }
  157. /* TestAPIIpcModeHost checks that a container created with --ipc host
  158. * can use IPC of the host system.
  159. */
  160. func TestAPIIpcModeHost(t *testing.T) {
  161. skip.If(t, testEnv.IsRemoteDaemon)
  162. skip.If(t, testEnv.IsUserNamespace)
  163. cfg := containertypes.Config{
  164. Image: "busybox",
  165. Cmd: []string{"top"},
  166. }
  167. hostCfg := containertypes.HostConfig{
  168. IpcMode: containertypes.IPCModeHost,
  169. }
  170. ctx := testutil.StartSpan(baseContext, t)
  171. apiClient := testEnv.APIClient()
  172. resp, err := apiClient.ContainerCreate(ctx, &cfg, &hostCfg, nil, nil, "")
  173. assert.NilError(t, err)
  174. assert.Check(t, is.Equal(len(resp.Warnings), 0))
  175. name := resp.ID
  176. err = apiClient.ContainerStart(ctx, name, containertypes.StartOptions{})
  177. assert.NilError(t, err)
  178. // check that IPC is shared
  179. // 1. create a file inside container
  180. _, err = container.Exec(ctx, apiClient, name, []string{"sh", "-c", "printf covfefe > /dev/shm/." + name})
  181. assert.NilError(t, err)
  182. // 2. check it's the same on the host
  183. bytes, err := os.ReadFile("/dev/shm/." + name)
  184. assert.NilError(t, err)
  185. assert.Check(t, is.Equal("covfefe", string(bytes)))
  186. // 3. clean up
  187. _, err = container.Exec(ctx, apiClient, name, []string{"rm", "-f", "/dev/shm/." + name})
  188. assert.NilError(t, err)
  189. }
  190. // testDaemonIpcPrivateShareable is a helper function to test "private" and "shareable" daemon default ipc modes.
  191. func testDaemonIpcPrivateShareable(t *testing.T, mustBeShared bool, arg ...string) {
  192. ctx := setupTest(t)
  193. d := daemon.New(t)
  194. d.StartWithBusybox(ctx, t, arg...)
  195. defer d.Stop(t)
  196. c := d.NewClientT(t)
  197. cfg := containertypes.Config{
  198. Image: "busybox",
  199. Cmd: []string{"top"},
  200. }
  201. resp, err := c.ContainerCreate(ctx, &cfg, &containertypes.HostConfig{}, nil, nil, "")
  202. assert.NilError(t, err)
  203. assert.Check(t, is.Equal(len(resp.Warnings), 0))
  204. err = c.ContainerStart(ctx, resp.ID, containertypes.StartOptions{})
  205. assert.NilError(t, err)
  206. // get major:minor pair for /dev/shm from container's /proc/self/mountinfo
  207. cmd := "awk '($5 == \"/dev/shm\") {printf $3}' /proc/self/mountinfo"
  208. result, err := container.Exec(ctx, c, resp.ID, []string{"sh", "-c", cmd})
  209. assert.NilError(t, err)
  210. mm := result.Combined()
  211. assert.Check(t, is.Equal(true, regexp.MustCompile("^[0-9]+:[0-9]+$").MatchString(mm)))
  212. shared, err := testIpcCheckDevExists(mm)
  213. assert.NilError(t, err)
  214. t.Logf("[testDaemonIpcPrivateShareable] ipcdev: %v, shared: %v, mustBeShared: %v\n", mm, shared, mustBeShared)
  215. assert.Check(t, is.Equal(shared, mustBeShared))
  216. }
  217. // TestDaemonIpcModeShareable checks that --default-ipc-mode shareable works as intended.
  218. func TestDaemonIpcModeShareable(t *testing.T) {
  219. skip.If(t, testEnv.IsRemoteDaemon)
  220. skip.If(t, testEnv.IsRootless, "no support for --ipc=shareable in rootless")
  221. testDaemonIpcPrivateShareable(t, true, "--default-ipc-mode", "shareable")
  222. }
  223. // TestDaemonIpcModePrivate checks that --default-ipc-mode private works as intended.
  224. func TestDaemonIpcModePrivate(t *testing.T) {
  225. skip.If(t, testEnv.IsRemoteDaemon)
  226. testDaemonIpcPrivateShareable(t, false, "--default-ipc-mode", "private")
  227. }
  228. // used to check if an IpcMode given in config works as intended
  229. func testDaemonIpcFromConfig(t *testing.T, mode string, mustExist bool) {
  230. config := `{"default-ipc-mode": "` + mode + `"}`
  231. // WithMode is needed for rootless
  232. file := fs.NewFile(t, "test-daemon-ipc-config", fs.WithContent(config), fs.WithMode(0o644))
  233. defer file.Remove()
  234. testDaemonIpcPrivateShareable(t, mustExist, "--config-file", file.Path())
  235. }
  236. // TestDaemonIpcModePrivateFromConfig checks that "default-ipc-mode: private" config works as intended.
  237. func TestDaemonIpcModePrivateFromConfig(t *testing.T) {
  238. skip.If(t, testEnv.IsRemoteDaemon)
  239. testDaemonIpcFromConfig(t, "private", false)
  240. }
  241. // TestDaemonIpcModeShareableFromConfig checks that "default-ipc-mode: shareable" config works as intended.
  242. func TestDaemonIpcModeShareableFromConfig(t *testing.T) {
  243. skip.If(t, testEnv.IsRemoteDaemon)
  244. skip.If(t, testEnv.IsRootless, "no support for --ipc=shareable in rootless")
  245. testDaemonIpcFromConfig(t, "shareable", true)
  246. }
  247. // TestIpcModeOlderClient checks that older client gets shareable IPC mode
  248. // by default, even when the daemon default is private.
  249. func TestIpcModeOlderClient(t *testing.T) {
  250. apiClient := testEnv.APIClient()
  251. skip.If(t, versions.LessThan(apiClient.ClientVersion(), "1.40"), "requires client API >= 1.40")
  252. t.Parallel()
  253. ctx := testutil.StartSpan(baseContext, t)
  254. // pre-check: default ipc mode in daemon is private
  255. cID := container.Create(ctx, t, apiClient, container.WithAutoRemove)
  256. inspect, err := apiClient.ContainerInspect(ctx, cID)
  257. assert.NilError(t, err)
  258. assert.Check(t, is.Equal(string(inspect.HostConfig.IpcMode), "private"))
  259. // main check: using older client creates "shareable" container
  260. apiClient = request.NewAPIClient(t, client.WithVersion("1.39"))
  261. cID = container.Create(ctx, t, apiClient, container.WithAutoRemove)
  262. inspect, err = apiClient.ContainerInspect(ctx, cID)
  263. assert.NilError(t, err)
  264. assert.Check(t, is.Equal(string(inspect.HostConfig.IpcMode), "shareable"))
  265. }