seccomp_linux_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. //go:build linux && seccomp
  2. // +build linux,seccomp
  3. package daemon // import "github.com/docker/docker/daemon"
  4. import (
  5. "testing"
  6. coci "github.com/containerd/containerd/oci"
  7. config "github.com/docker/docker/api/types/container"
  8. "github.com/docker/docker/container"
  9. dconfig "github.com/docker/docker/daemon/config"
  10. doci "github.com/docker/docker/oci"
  11. "github.com/docker/docker/profiles/seccomp"
  12. specs "github.com/opencontainers/runtime-spec/specs-go"
  13. "gotest.tools/v3/assert"
  14. )
  15. func TestWithSeccomp(t *testing.T) {
  16. type expected struct {
  17. daemon *Daemon
  18. c *container.Container
  19. inSpec coci.Spec
  20. outSpec coci.Spec
  21. err string
  22. comment string
  23. }
  24. for _, x := range []expected{
  25. {
  26. comment: "unconfined seccompProfile runs unconfined",
  27. daemon: &Daemon{
  28. seccompEnabled: true,
  29. },
  30. c: &container.Container{
  31. SeccompProfile: dconfig.SeccompProfileUnconfined,
  32. HostConfig: &config.HostConfig{
  33. Privileged: false,
  34. },
  35. },
  36. inSpec: doci.DefaultLinuxSpec(),
  37. outSpec: doci.DefaultLinuxSpec(),
  38. },
  39. {
  40. comment: "privileged container w/ custom profile runs unconfined",
  41. daemon: &Daemon{
  42. seccompEnabled: true,
  43. },
  44. c: &container.Container{
  45. SeccompProfile: "{ \"defaultAction\": \"SCMP_ACT_LOG\" }",
  46. HostConfig: &config.HostConfig{
  47. Privileged: true,
  48. },
  49. },
  50. inSpec: doci.DefaultLinuxSpec(),
  51. outSpec: doci.DefaultLinuxSpec(),
  52. },
  53. {
  54. comment: "privileged container w/ default runs unconfined",
  55. daemon: &Daemon{
  56. seccompEnabled: true,
  57. },
  58. c: &container.Container{
  59. SeccompProfile: "",
  60. HostConfig: &config.HostConfig{
  61. Privileged: true,
  62. },
  63. },
  64. inSpec: doci.DefaultLinuxSpec(),
  65. outSpec: doci.DefaultLinuxSpec(),
  66. },
  67. {
  68. comment: "privileged container w/ daemon profile runs unconfined",
  69. daemon: &Daemon{
  70. seccompEnabled: true,
  71. seccompProfile: []byte("{ \"defaultAction\": \"SCMP_ACT_ERRNO\" }"),
  72. },
  73. c: &container.Container{
  74. SeccompProfile: "",
  75. HostConfig: &config.HostConfig{
  76. Privileged: true,
  77. },
  78. },
  79. inSpec: doci.DefaultLinuxSpec(),
  80. outSpec: doci.DefaultLinuxSpec(),
  81. },
  82. {
  83. comment: "custom profile when seccomp is disabled returns error",
  84. daemon: &Daemon{
  85. seccompEnabled: false,
  86. },
  87. c: &container.Container{
  88. SeccompProfile: "{ \"defaultAction\": \"SCMP_ACT_ERRNO\" }",
  89. HostConfig: &config.HostConfig{
  90. Privileged: false,
  91. },
  92. },
  93. inSpec: doci.DefaultLinuxSpec(),
  94. outSpec: doci.DefaultLinuxSpec(),
  95. err: "seccomp is not enabled in your kernel, cannot run a custom seccomp profile",
  96. },
  97. {
  98. comment: "empty profile name loads default profile",
  99. daemon: &Daemon{
  100. seccompEnabled: true,
  101. },
  102. c: &container.Container{
  103. SeccompProfile: "",
  104. HostConfig: &config.HostConfig{
  105. Privileged: false,
  106. },
  107. },
  108. inSpec: doci.DefaultLinuxSpec(),
  109. outSpec: func() coci.Spec {
  110. s := doci.DefaultLinuxSpec()
  111. profile, _ := seccomp.GetDefaultProfile(&s)
  112. s.Linux.Seccomp = profile
  113. return s
  114. }(),
  115. },
  116. {
  117. comment: "load container's profile",
  118. daemon: &Daemon{
  119. seccompEnabled: true,
  120. },
  121. c: &container.Container{
  122. SeccompProfile: "{ \"defaultAction\": \"SCMP_ACT_ERRNO\" }",
  123. HostConfig: &config.HostConfig{
  124. Privileged: false,
  125. },
  126. },
  127. inSpec: doci.DefaultLinuxSpec(),
  128. outSpec: func() coci.Spec {
  129. s := doci.DefaultLinuxSpec()
  130. profile := &specs.LinuxSeccomp{
  131. DefaultAction: specs.LinuxSeccompAction("SCMP_ACT_ERRNO"),
  132. }
  133. s.Linux.Seccomp = profile
  134. return s
  135. }(),
  136. },
  137. {
  138. comment: "load daemon's profile",
  139. daemon: &Daemon{
  140. seccompEnabled: true,
  141. seccompProfile: []byte("{ \"defaultAction\": \"SCMP_ACT_ERRNO\" }"),
  142. },
  143. c: &container.Container{
  144. SeccompProfile: "",
  145. HostConfig: &config.HostConfig{
  146. Privileged: false,
  147. },
  148. },
  149. inSpec: doci.DefaultLinuxSpec(),
  150. outSpec: func() coci.Spec {
  151. s := doci.DefaultLinuxSpec()
  152. profile := &specs.LinuxSeccomp{
  153. DefaultAction: specs.LinuxSeccompAction("SCMP_ACT_ERRNO"),
  154. }
  155. s.Linux.Seccomp = profile
  156. return s
  157. }(),
  158. },
  159. {
  160. comment: "load prioritise container profile over daemon's",
  161. daemon: &Daemon{
  162. seccompEnabled: true,
  163. seccompProfile: []byte("{ \"defaultAction\": \"SCMP_ACT_ERRNO\" }"),
  164. },
  165. c: &container.Container{
  166. SeccompProfile: "{ \"defaultAction\": \"SCMP_ACT_LOG\" }",
  167. HostConfig: &config.HostConfig{
  168. Privileged: false,
  169. },
  170. },
  171. inSpec: doci.DefaultLinuxSpec(),
  172. outSpec: func() coci.Spec {
  173. s := doci.DefaultLinuxSpec()
  174. profile := &specs.LinuxSeccomp{
  175. DefaultAction: specs.LinuxSeccompAction("SCMP_ACT_LOG"),
  176. }
  177. s.Linux.Seccomp = profile
  178. return s
  179. }(),
  180. },
  181. } {
  182. t.Run(x.comment, func(t *testing.T) {
  183. opts := WithSeccomp(x.daemon, x.c)
  184. err := opts(nil, nil, nil, &x.inSpec)
  185. assert.DeepEqual(t, x.inSpec, x.outSpec)
  186. if x.err != "" {
  187. assert.Error(t, err, x.err)
  188. } else {
  189. assert.NilError(t, err)
  190. }
  191. })
  192. }
  193. }