create.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. // +build linux,cgo
  2. package native
  3. import (
  4. "fmt"
  5. "strings"
  6. "syscall"
  7. "github.com/docker/docker/daemon/execdriver"
  8. "github.com/opencontainers/runc/libcontainer/apparmor"
  9. "github.com/opencontainers/runc/libcontainer/configs"
  10. "github.com/opencontainers/runc/libcontainer/devices"
  11. )
  12. // createContainer populates and configures the container type with the
  13. // data provided by the execdriver.Command
  14. func (d *Driver) createContainer(c *execdriver.Command, hooks execdriver.Hooks) (*configs.Config, error) {
  15. container := execdriver.InitContainer(c)
  16. if err := d.createIpc(container, c); err != nil {
  17. return nil, err
  18. }
  19. if err := d.createPid(container, c); err != nil {
  20. return nil, err
  21. }
  22. if err := d.createUTS(container, c); err != nil {
  23. return nil, err
  24. }
  25. if err := d.createNetwork(container, c, hooks); err != nil {
  26. return nil, err
  27. }
  28. if c.ProcessConfig.Privileged {
  29. if !container.Readonlyfs {
  30. // clear readonly for /sys
  31. for i := range container.Mounts {
  32. if container.Mounts[i].Destination == "/sys" {
  33. container.Mounts[i].Flags &= ^syscall.MS_RDONLY
  34. }
  35. }
  36. container.ReadonlyPaths = nil
  37. }
  38. // clear readonly for cgroup
  39. for i := range container.Mounts {
  40. if container.Mounts[i].Device == "cgroup" {
  41. container.Mounts[i].Flags &= ^syscall.MS_RDONLY
  42. }
  43. }
  44. container.MaskPaths = nil
  45. if err := d.setPrivileged(container); err != nil {
  46. return nil, err
  47. }
  48. } else {
  49. if err := d.setCapabilities(container, c); err != nil {
  50. return nil, err
  51. }
  52. }
  53. // add CAP_ prefix to all caps for new libcontainer update to match
  54. // the spec format.
  55. for i, s := range container.Capabilities {
  56. if !strings.HasPrefix(s, "CAP_") {
  57. container.Capabilities[i] = fmt.Sprintf("CAP_%s", s)
  58. }
  59. }
  60. container.AdditionalGroups = c.GroupAdd
  61. if c.AppArmorProfile != "" {
  62. container.AppArmorProfile = c.AppArmorProfile
  63. }
  64. if err := execdriver.SetupCgroups(container, c); err != nil {
  65. return nil, err
  66. }
  67. if container.Readonlyfs {
  68. for i := range container.Mounts {
  69. switch container.Mounts[i].Destination {
  70. case "/proc", "/dev", "/dev/pts":
  71. continue
  72. }
  73. container.Mounts[i].Flags |= syscall.MS_RDONLY
  74. }
  75. /* These paths must be remounted as r/o */
  76. container.ReadonlyPaths = append(container.ReadonlyPaths, "/dev")
  77. }
  78. if err := d.setupMounts(container, c); err != nil {
  79. return nil, err
  80. }
  81. d.setupLabels(container, c)
  82. d.setupRlimits(container, c)
  83. return container, nil
  84. }
  85. func (d *Driver) createNetwork(container *configs.Config, c *execdriver.Command, hooks execdriver.Hooks) error {
  86. if c.Network == nil {
  87. return nil
  88. }
  89. if c.Network.ContainerID != "" {
  90. d.Lock()
  91. active := d.activeContainers[c.Network.ContainerID]
  92. d.Unlock()
  93. if active == nil {
  94. return fmt.Errorf("%s is not a valid running container to join", c.Network.ContainerID)
  95. }
  96. state, err := active.State()
  97. if err != nil {
  98. return err
  99. }
  100. container.Namespaces.Add(configs.NEWNET, state.NamespacePaths[configs.NEWNET])
  101. return nil
  102. }
  103. if c.Network.NamespacePath != "" {
  104. container.Namespaces.Add(configs.NEWNET, c.Network.NamespacePath)
  105. return nil
  106. }
  107. // only set up prestart hook if the namespace path is not set (this should be
  108. // all cases *except* for --net=host shared networking)
  109. container.Hooks = &configs.Hooks{
  110. Prestart: []configs.Hook{
  111. configs.NewFunctionHook(func(s configs.HookState) error {
  112. if len(hooks.PreStart) > 0 {
  113. for _, fnHook := range hooks.PreStart {
  114. // A closed channel for OOM is returned here as it will be
  115. // non-blocking and return the correct result when read.
  116. chOOM := make(chan struct{})
  117. close(chOOM)
  118. if err := fnHook(&c.ProcessConfig, s.Pid, chOOM); err != nil {
  119. return err
  120. }
  121. }
  122. }
  123. return nil
  124. }),
  125. },
  126. }
  127. return nil
  128. }
  129. func (d *Driver) createIpc(container *configs.Config, c *execdriver.Command) error {
  130. if c.Ipc.HostIpc {
  131. container.Namespaces.Remove(configs.NEWIPC)
  132. return nil
  133. }
  134. if c.Ipc.ContainerID != "" {
  135. d.Lock()
  136. active := d.activeContainers[c.Ipc.ContainerID]
  137. d.Unlock()
  138. if active == nil {
  139. return fmt.Errorf("%s is not a valid running container to join", c.Ipc.ContainerID)
  140. }
  141. state, err := active.State()
  142. if err != nil {
  143. return err
  144. }
  145. container.Namespaces.Add(configs.NEWIPC, state.NamespacePaths[configs.NEWIPC])
  146. }
  147. return nil
  148. }
  149. func (d *Driver) createPid(container *configs.Config, c *execdriver.Command) error {
  150. if c.Pid.HostPid {
  151. container.Namespaces.Remove(configs.NEWPID)
  152. return nil
  153. }
  154. return nil
  155. }
  156. func (d *Driver) createUTS(container *configs.Config, c *execdriver.Command) error {
  157. if c.UTS.HostUTS {
  158. container.Namespaces.Remove(configs.NEWUTS)
  159. container.Hostname = ""
  160. return nil
  161. }
  162. return nil
  163. }
  164. func (d *Driver) setPrivileged(container *configs.Config) (err error) {
  165. container.Capabilities = execdriver.GetAllCapabilities()
  166. container.Cgroups.AllowAllDevices = true
  167. hostDevices, err := devices.HostDevices()
  168. if err != nil {
  169. return err
  170. }
  171. container.Devices = hostDevices
  172. if apparmor.IsEnabled() {
  173. container.AppArmorProfile = "unconfined"
  174. }
  175. return nil
  176. }
  177. func (d *Driver) setCapabilities(container *configs.Config, c *execdriver.Command) (err error) {
  178. container.Capabilities, err = execdriver.TweakCapabilities(container.Capabilities, c.CapAdd, c.CapDrop)
  179. return err
  180. }
  181. func (d *Driver) setupRlimits(container *configs.Config, c *execdriver.Command) {
  182. if c.Resources == nil {
  183. return
  184. }
  185. for _, rlimit := range c.Resources.Rlimits {
  186. container.Rlimits = append(container.Rlimits, configs.Rlimit{
  187. Type: rlimit.Type,
  188. Hard: rlimit.Hard,
  189. Soft: rlimit.Soft,
  190. })
  191. }
  192. }
  193. func (d *Driver) setupMounts(container *configs.Config, c *execdriver.Command) error {
  194. userMounts := make(map[string]struct{})
  195. for _, m := range c.Mounts {
  196. userMounts[m.Destination] = struct{}{}
  197. }
  198. // Filter out mounts that are overriden by user supplied mounts
  199. var defaultMounts []*configs.Mount
  200. _, mountDev := userMounts["/dev"]
  201. for _, m := range container.Mounts {
  202. if _, ok := userMounts[m.Destination]; !ok {
  203. if mountDev && strings.HasPrefix(m.Destination, "/dev/") {
  204. continue
  205. }
  206. defaultMounts = append(defaultMounts, m)
  207. }
  208. }
  209. container.Mounts = defaultMounts
  210. for _, m := range c.Mounts {
  211. flags := syscall.MS_BIND | syscall.MS_REC
  212. if !m.Writable {
  213. flags |= syscall.MS_RDONLY
  214. }
  215. if m.Slave {
  216. flags |= syscall.MS_SLAVE
  217. }
  218. container.Mounts = append(container.Mounts, &configs.Mount{
  219. Source: m.Source,
  220. Destination: m.Destination,
  221. Device: "bind",
  222. Flags: flags,
  223. })
  224. }
  225. return nil
  226. }
  227. func (d *Driver) setupLabels(container *configs.Config, c *execdriver.Command) {
  228. container.ProcessLabel = c.ProcessLabel
  229. container.MountLabel = c.MountLabel
  230. }