seccomp_linux_test.go 5.5 KB

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