defaults.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. package oci
  2. import (
  3. "os"
  4. "runtime"
  5. "github.com/opencontainers/runtime-spec/specs-go"
  6. )
  7. func iPtr(i int64) *int64 { return &i }
  8. func u32Ptr(i int64) *uint32 { u := uint32(i); return &u }
  9. func fmPtr(i int64) *os.FileMode { fm := os.FileMode(i); return &fm }
  10. func defaultCapabilities() []string {
  11. return []string{
  12. "CAP_CHOWN",
  13. "CAP_DAC_OVERRIDE",
  14. "CAP_FSETID",
  15. "CAP_FOWNER",
  16. "CAP_MKNOD",
  17. "CAP_NET_RAW",
  18. "CAP_SETGID",
  19. "CAP_SETUID",
  20. "CAP_SETFCAP",
  21. "CAP_SETPCAP",
  22. "CAP_NET_BIND_SERVICE",
  23. "CAP_SYS_CHROOT",
  24. "CAP_KILL",
  25. "CAP_AUDIT_WRITE",
  26. }
  27. }
  28. // DefaultSpec returns the default spec used by docker for the current Platform
  29. func DefaultSpec() specs.Spec {
  30. return DefaultOSSpec(runtime.GOOS)
  31. }
  32. // DefaultOSSpec returns the spec for a given OS
  33. func DefaultOSSpec(osName string) specs.Spec {
  34. if osName == "windows" {
  35. return DefaultWindowsSpec()
  36. }
  37. return DefaultLinuxSpec()
  38. }
  39. // DefaultWindowsSpec create a default spec for running Windows containers
  40. func DefaultWindowsSpec() specs.Spec {
  41. return specs.Spec{
  42. Version: specs.Version,
  43. Windows: &specs.Windows{},
  44. Process: &specs.Process{},
  45. Root: &specs.Root{},
  46. }
  47. }
  48. // DefaultLinuxSpec create a default spec for running Linux containers
  49. func DefaultLinuxSpec() specs.Spec {
  50. s := specs.Spec{
  51. Version: specs.Version,
  52. Process: &specs.Process{
  53. Capabilities: &specs.LinuxCapabilities{
  54. Bounding: defaultCapabilities(),
  55. Permitted: defaultCapabilities(),
  56. Inheritable: defaultCapabilities(),
  57. Effective: defaultCapabilities(),
  58. },
  59. },
  60. Root: &specs.Root{},
  61. }
  62. s.Mounts = []specs.Mount{
  63. {
  64. Destination: "/proc",
  65. Type: "proc",
  66. Source: "proc",
  67. Options: []string{"nosuid", "noexec", "nodev"},
  68. },
  69. {
  70. Destination: "/dev",
  71. Type: "tmpfs",
  72. Source: "tmpfs",
  73. Options: []string{"nosuid", "strictatime", "mode=755", "size=65536k"},
  74. },
  75. {
  76. Destination: "/dev/pts",
  77. Type: "devpts",
  78. Source: "devpts",
  79. Options: []string{"nosuid", "noexec", "newinstance", "ptmxmode=0666", "mode=0620", "gid=5"},
  80. },
  81. {
  82. Destination: "/sys",
  83. Type: "sysfs",
  84. Source: "sysfs",
  85. Options: []string{"nosuid", "noexec", "nodev", "ro"},
  86. },
  87. {
  88. Destination: "/sys/fs/cgroup",
  89. Type: "cgroup",
  90. Source: "cgroup",
  91. Options: []string{"ro", "nosuid", "noexec", "nodev"},
  92. },
  93. {
  94. Destination: "/dev/mqueue",
  95. Type: "mqueue",
  96. Source: "mqueue",
  97. Options: []string{"nosuid", "noexec", "nodev"},
  98. },
  99. {
  100. Destination: "/dev/shm",
  101. Type: "tmpfs",
  102. Source: "shm",
  103. Options: []string{"nosuid", "noexec", "nodev", "mode=1777"},
  104. },
  105. }
  106. s.Linux = &specs.Linux{
  107. MaskedPaths: []string{
  108. "/proc/kcore",
  109. "/proc/latency_stats",
  110. "/proc/timer_list",
  111. "/proc/timer_stats",
  112. "/proc/sched_debug",
  113. "/proc/scsi",
  114. "/sys/firmware",
  115. },
  116. ReadonlyPaths: []string{
  117. "/proc/asound",
  118. "/proc/bus",
  119. "/proc/fs",
  120. "/proc/irq",
  121. "/proc/sys",
  122. "/proc/sysrq-trigger",
  123. },
  124. Namespaces: []specs.LinuxNamespace{
  125. {Type: "mount"},
  126. {Type: "network"},
  127. {Type: "uts"},
  128. {Type: "pid"},
  129. {Type: "ipc"},
  130. },
  131. // Devices implicitly contains the following devices:
  132. // null, zero, full, random, urandom, tty, console, and ptmx.
  133. // ptmx is a bind mount or symlink of the container's ptmx.
  134. // See also: https://github.com/opencontainers/runtime-spec/blob/master/config-linux.md#default-devices
  135. Devices: []specs.LinuxDevice{},
  136. Resources: &specs.LinuxResources{
  137. Devices: []specs.LinuxDeviceCgroup{
  138. {
  139. Allow: false,
  140. Access: "rwm",
  141. },
  142. {
  143. Allow: true,
  144. Type: "c",
  145. Major: iPtr(1),
  146. Minor: iPtr(5),
  147. Access: "rwm",
  148. },
  149. {
  150. Allow: true,
  151. Type: "c",
  152. Major: iPtr(1),
  153. Minor: iPtr(3),
  154. Access: "rwm",
  155. },
  156. {
  157. Allow: true,
  158. Type: "c",
  159. Major: iPtr(1),
  160. Minor: iPtr(9),
  161. Access: "rwm",
  162. },
  163. {
  164. Allow: true,
  165. Type: "c",
  166. Major: iPtr(1),
  167. Minor: iPtr(8),
  168. Access: "rwm",
  169. },
  170. {
  171. Allow: true,
  172. Type: "c",
  173. Major: iPtr(5),
  174. Minor: iPtr(0),
  175. Access: "rwm",
  176. },
  177. {
  178. Allow: true,
  179. Type: "c",
  180. Major: iPtr(5),
  181. Minor: iPtr(1),
  182. Access: "rwm",
  183. },
  184. {
  185. Allow: false,
  186. Type: "c",
  187. Major: iPtr(10),
  188. Minor: iPtr(229),
  189. Access: "rwm",
  190. },
  191. },
  192. },
  193. }
  194. // For LCOW support, populate a blank Windows spec
  195. if runtime.GOOS == "windows" {
  196. s.Windows = &specs.Windows{}
  197. }
  198. return s
  199. }