seccomp_linux_test.go 5.2 KB

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