exec_linux_test.go 2.6 KB

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