defaults.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. } else if osName == "solaris" {
  37. return DefaultSolarisSpec()
  38. } else {
  39. return DefaultLinuxSpec()
  40. }
  41. }
  42. // DefaultWindowsSpec create a default spec for running Windows containers
  43. func DefaultWindowsSpec() specs.Spec {
  44. return specs.Spec{
  45. Version: specs.Version,
  46. Windows: &specs.Windows{},
  47. Process: &specs.Process{},
  48. Root: &specs.Root{},
  49. }
  50. }
  51. // DefaultSolarisSpec create a default spec for running Solaris containers
  52. func DefaultSolarisSpec() specs.Spec {
  53. s := specs.Spec{
  54. Version: "0.6.0",
  55. }
  56. s.Solaris = &specs.Solaris{}
  57. return s
  58. }
  59. // DefaultLinuxSpec create a default spec for running Linux containers
  60. func DefaultLinuxSpec() specs.Spec {
  61. s := specs.Spec{
  62. Version: specs.Version,
  63. Process: &specs.Process{},
  64. Root: &specs.Root{},
  65. }
  66. s.Mounts = []specs.Mount{
  67. {
  68. Destination: "/proc",
  69. Type: "proc",
  70. Source: "proc",
  71. Options: []string{"nosuid", "noexec", "nodev"},
  72. },
  73. {
  74. Destination: "/dev",
  75. Type: "tmpfs",
  76. Source: "tmpfs",
  77. Options: []string{"nosuid", "strictatime", "mode=755", "size=65536k"},
  78. },
  79. {
  80. Destination: "/dev/pts",
  81. Type: "devpts",
  82. Source: "devpts",
  83. Options: []string{"nosuid", "noexec", "newinstance", "ptmxmode=0666", "mode=0620", "gid=5"},
  84. },
  85. {
  86. Destination: "/sys",
  87. Type: "sysfs",
  88. Source: "sysfs",
  89. Options: []string{"nosuid", "noexec", "nodev", "ro"},
  90. },
  91. {
  92. Destination: "/sys/fs/cgroup",
  93. Type: "cgroup",
  94. Source: "cgroup",
  95. Options: []string{"ro", "nosuid", "noexec", "nodev"},
  96. },
  97. {
  98. Destination: "/dev/mqueue",
  99. Type: "mqueue",
  100. Source: "mqueue",
  101. Options: []string{"nosuid", "noexec", "nodev"},
  102. },
  103. {
  104. Destination: "/dev/shm",
  105. Type: "tmpfs",
  106. Source: "shm",
  107. Options: []string{"nosuid", "noexec", "nodev", "mode=1777"},
  108. },
  109. }
  110. s.Process = &specs.Process{
  111. Capabilities: &specs.LinuxCapabilities{
  112. Bounding: defaultCapabilities(),
  113. Permitted: defaultCapabilities(),
  114. Inheritable: defaultCapabilities(),
  115. Effective: defaultCapabilities(),
  116. },
  117. }
  118. s.Linux = &specs.Linux{
  119. MaskedPaths: []string{
  120. "/proc/kcore",
  121. "/proc/latency_stats",
  122. "/proc/timer_list",
  123. "/proc/timer_stats",
  124. "/proc/sched_debug",
  125. },
  126. ReadonlyPaths: []string{
  127. "/proc/asound",
  128. "/proc/bus",
  129. "/proc/fs",
  130. "/proc/irq",
  131. "/proc/sys",
  132. "/proc/sysrq-trigger",
  133. },
  134. Namespaces: []specs.LinuxNamespace{
  135. {Type: "mount"},
  136. {Type: "network"},
  137. {Type: "uts"},
  138. {Type: "pid"},
  139. {Type: "ipc"},
  140. },
  141. // Devices implicitly contains the following devices:
  142. // null, zero, full, random, urandom, tty, console, and ptmx.
  143. // ptmx is a bind mount or symlink of the container's ptmx.
  144. // See also: https://github.com/opencontainers/runtime-spec/blob/master/config-linux.md#default-devices
  145. Devices: []specs.LinuxDevice{},
  146. Resources: &specs.LinuxResources{
  147. Devices: []specs.LinuxDeviceCgroup{
  148. {
  149. Allow: false,
  150. Access: "rwm",
  151. },
  152. {
  153. Allow: true,
  154. Type: "c",
  155. Major: iPtr(1),
  156. Minor: iPtr(5),
  157. Access: "rwm",
  158. },
  159. {
  160. Allow: true,
  161. Type: "c",
  162. Major: iPtr(1),
  163. Minor: iPtr(3),
  164. Access: "rwm",
  165. },
  166. {
  167. Allow: true,
  168. Type: "c",
  169. Major: iPtr(1),
  170. Minor: iPtr(9),
  171. Access: "rwm",
  172. },
  173. {
  174. Allow: true,
  175. Type: "c",
  176. Major: iPtr(1),
  177. Minor: iPtr(8),
  178. Access: "rwm",
  179. },
  180. {
  181. Allow: true,
  182. Type: "c",
  183. Major: iPtr(5),
  184. Minor: iPtr(0),
  185. Access: "rwm",
  186. },
  187. {
  188. Allow: true,
  189. Type: "c",
  190. Major: iPtr(5),
  191. Minor: iPtr(1),
  192. Access: "rwm",
  193. },
  194. {
  195. Allow: false,
  196. Type: "c",
  197. Major: iPtr(10),
  198. Minor: iPtr(229),
  199. Access: "rwm",
  200. },
  201. },
  202. },
  203. }
  204. // For LCOW support, populate a blank Windows spec
  205. if runtime.GOOS == "windows" {
  206. s.Windows = &specs.Windows{}
  207. }
  208. // For LCOW support, don't mask /sys/firmware
  209. if runtime.GOOS != "windows" {
  210. s.Linux.MaskedPaths = append(s.Linux.MaskedPaths, "/sys/firmware")
  211. }
  212. return s
  213. }