defaults.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. package oci // import "github.com/docker/docker/oci"
  2. import (
  3. "runtime"
  4. "github.com/docker/docker/oci/caps"
  5. specs "github.com/opencontainers/runtime-spec/specs-go"
  6. )
  7. func iPtr(i int64) *int64 { return &i }
  8. const defaultUnixPathEnv = "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
  9. // DefaultPathEnv is unix style list of directories to search for
  10. // executables. Each directory is separated from the next by a colon
  11. // ':' character .
  12. // For Windows containers, an empty string is returned as the default
  13. // path will be set by the container, and Docker has no context of what the
  14. // default path should be.
  15. //
  16. // TODO(thaJeztah) align Windows default with BuildKit; see https://github.com/moby/buildkit/pull/1747
  17. // TODO(thaJeztah) use defaults from containerd (but align it with BuildKit; see https://github.com/moby/buildkit/pull/1747)
  18. func DefaultPathEnv(os string) string {
  19. if os == "windows" {
  20. return ""
  21. }
  22. return defaultUnixPathEnv
  23. }
  24. // DefaultSpec returns the default spec used by docker for the current Platform
  25. func DefaultSpec() specs.Spec {
  26. if runtime.GOOS == "windows" {
  27. return DefaultWindowsSpec()
  28. }
  29. return DefaultLinuxSpec()
  30. }
  31. // DefaultWindowsSpec create a default spec for running Windows containers
  32. func DefaultWindowsSpec() specs.Spec {
  33. return specs.Spec{
  34. Version: specs.Version,
  35. Windows: &specs.Windows{},
  36. Process: &specs.Process{},
  37. Root: &specs.Root{},
  38. }
  39. }
  40. // DefaultLinuxSpec create a default spec for running Linux containers
  41. func DefaultLinuxSpec() specs.Spec {
  42. return specs.Spec{
  43. Version: specs.Version,
  44. Process: &specs.Process{
  45. Capabilities: &specs.LinuxCapabilities{
  46. Bounding: caps.DefaultCapabilities(),
  47. Permitted: caps.DefaultCapabilities(),
  48. Effective: caps.DefaultCapabilities(),
  49. },
  50. },
  51. Root: &specs.Root{},
  52. Mounts: []specs.Mount{
  53. {
  54. Destination: "/proc",
  55. Type: "proc",
  56. Source: "proc",
  57. Options: []string{"nosuid", "noexec", "nodev"},
  58. },
  59. {
  60. Destination: "/dev",
  61. Type: "tmpfs",
  62. Source: "tmpfs",
  63. Options: []string{"nosuid", "strictatime", "mode=755", "size=65536k"},
  64. },
  65. {
  66. Destination: "/dev/pts",
  67. Type: "devpts",
  68. Source: "devpts",
  69. Options: []string{"nosuid", "noexec", "newinstance", "ptmxmode=0666", "mode=0620", "gid=5"},
  70. },
  71. {
  72. Destination: "/sys",
  73. Type: "sysfs",
  74. Source: "sysfs",
  75. Options: []string{"nosuid", "noexec", "nodev", "ro"},
  76. },
  77. {
  78. Destination: "/sys/fs/cgroup",
  79. Type: "cgroup",
  80. Source: "cgroup",
  81. Options: []string{"ro", "nosuid", "noexec", "nodev"},
  82. },
  83. {
  84. Destination: "/dev/mqueue",
  85. Type: "mqueue",
  86. Source: "mqueue",
  87. Options: []string{"nosuid", "noexec", "nodev"},
  88. },
  89. {
  90. Destination: "/dev/shm",
  91. Type: "tmpfs",
  92. Source: "shm",
  93. Options: []string{"nosuid", "noexec", "nodev", "mode=1777"},
  94. },
  95. },
  96. Linux: &specs.Linux{
  97. MaskedPaths: []string{
  98. "/proc/asound",
  99. "/proc/acpi",
  100. "/proc/kcore",
  101. "/proc/keys",
  102. "/proc/latency_stats",
  103. "/proc/timer_list",
  104. "/proc/timer_stats",
  105. "/proc/sched_debug",
  106. "/proc/scsi",
  107. "/sys/firmware",
  108. },
  109. ReadonlyPaths: []string{
  110. "/proc/bus",
  111. "/proc/fs",
  112. "/proc/irq",
  113. "/proc/sys",
  114. "/proc/sysrq-trigger",
  115. },
  116. Namespaces: []specs.LinuxNamespace{
  117. {Type: "mount"},
  118. {Type: "network"},
  119. {Type: "uts"},
  120. {Type: "pid"},
  121. {Type: "ipc"},
  122. },
  123. // Devices implicitly contains the following devices:
  124. // null, zero, full, random, urandom, tty, console, and ptmx.
  125. // ptmx is a bind mount or symlink of the container's ptmx.
  126. // See also: https://github.com/opencontainers/runtime-spec/blob/master/config-linux.md#default-devices
  127. Devices: []specs.LinuxDevice{},
  128. Resources: &specs.LinuxResources{
  129. Devices: []specs.LinuxDeviceCgroup{
  130. {
  131. Allow: false,
  132. Access: "rwm",
  133. },
  134. {
  135. Allow: true,
  136. Type: "c",
  137. Major: iPtr(1),
  138. Minor: iPtr(5),
  139. Access: "rwm",
  140. },
  141. {
  142. Allow: true,
  143. Type: "c",
  144. Major: iPtr(1),
  145. Minor: iPtr(3),
  146. Access: "rwm",
  147. },
  148. {
  149. Allow: true,
  150. Type: "c",
  151. Major: iPtr(1),
  152. Minor: iPtr(9),
  153. Access: "rwm",
  154. },
  155. {
  156. Allow: true,
  157. Type: "c",
  158. Major: iPtr(1),
  159. Minor: iPtr(8),
  160. Access: "rwm",
  161. },
  162. {
  163. Allow: true,
  164. Type: "c",
  165. Major: iPtr(5),
  166. Minor: iPtr(0),
  167. Access: "rwm",
  168. },
  169. {
  170. Allow: true,
  171. Type: "c",
  172. Major: iPtr(5),
  173. Minor: iPtr(1),
  174. Access: "rwm",
  175. },
  176. {
  177. Allow: false,
  178. Type: "c",
  179. Major: iPtr(10),
  180. Minor: iPtr(229),
  181. Access: "rwm",
  182. },
  183. },
  184. },
  185. },
  186. }
  187. }