exec_linux_test.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. //go:build linux
  2. // +build linux
  3. package daemon
  4. import (
  5. "context"
  6. "testing"
  7. "github.com/containerd/containerd/pkg/apparmor"
  8. containertypes "github.com/docker/docker/api/types/container"
  9. "github.com/docker/docker/container"
  10. "github.com/docker/docker/daemon/config"
  11. specs "github.com/opencontainers/runtime-spec/specs-go"
  12. "gotest.tools/v3/assert"
  13. )
  14. func TestExecSetPlatformOptAppArmor(t *testing.T) {
  15. appArmorEnabled := apparmor.HostSupports()
  16. tests := []struct {
  17. doc string
  18. privileged bool
  19. appArmorProfile string
  20. expectedProfile string
  21. }{
  22. {
  23. doc: "default options",
  24. expectedProfile: defaultAppArmorProfile,
  25. },
  26. {
  27. doc: "custom profile",
  28. appArmorProfile: "my-custom-profile",
  29. expectedProfile: "my-custom-profile",
  30. },
  31. {
  32. doc: "privileged container",
  33. privileged: true,
  34. expectedProfile: unconfinedAppArmorProfile,
  35. },
  36. {
  37. doc: "privileged container, custom profile",
  38. privileged: true,
  39. appArmorProfile: "my-custom-profile",
  40. expectedProfile: "my-custom-profile",
  41. // FIXME: execSetPlatformOpts prefers custom profiles over "privileged",
  42. // which looks like a bug (--privileged on the container should
  43. // disable apparmor, seccomp, and selinux); see the code at:
  44. // https://github.com/moby/moby/blob/46cdcd206c56172b95ba5c77b827a722dab426c5/daemon/exec_linux.go#L32-L40
  45. // expectedProfile: unconfinedAppArmorProfile,
  46. },
  47. }
  48. d := &Daemon{configStore: &config.Config{}}
  49. // Currently, `docker exec --privileged` inherits the Privileged configuration
  50. // of the container, and does not disable AppArmor.
  51. // See https://github.com/moby/moby/pull/31773#discussion_r105586900
  52. //
  53. // This behavior may change in future, but to verify the current behavior,
  54. // we run the test both with "exec" and "exec --privileged", which should
  55. // both give the same result.
  56. for _, execPrivileged := range []bool{false, true} {
  57. for _, tc := range tests {
  58. tc := tc
  59. doc := tc.doc
  60. if !appArmorEnabled {
  61. // no profile should be set if the host does not support AppArmor
  62. doc += " (apparmor disabled)"
  63. tc.expectedProfile = ""
  64. }
  65. if execPrivileged {
  66. doc += " (exec privileged)"
  67. }
  68. t.Run(doc, func(t *testing.T) {
  69. c := &container.Container{
  70. AppArmorProfile: tc.appArmorProfile,
  71. HostConfig: &containertypes.HostConfig{
  72. Privileged: tc.privileged,
  73. },
  74. }
  75. ec := &container.ExecConfig{Container: c, Privileged: execPrivileged}
  76. p := &specs.Process{}
  77. err := d.execSetPlatformOpt(context.Background(), ec, p)
  78. assert.NilError(t, err)
  79. assert.Equal(t, p.ApparmorProfile, tc.expectedProfile)
  80. })
  81. }
  82. }
  83. }