create.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. // +build linux,cgo
  2. package native
  3. import (
  4. "fmt"
  5. "path/filepath"
  6. "strings"
  7. "syscall"
  8. "github.com/docker/docker/daemon/execdriver"
  9. derr "github.com/docker/docker/errors"
  10. "github.com/docker/docker/pkg/mount"
  11. "github.com/docker/docker/volume"
  12. "github.com/opencontainers/runc/libcontainer/apparmor"
  13. "github.com/opencontainers/runc/libcontainer/configs"
  14. "github.com/opencontainers/runc/libcontainer/devices"
  15. )
  16. // createContainer populates and configures the container type with the
  17. // data provided by the execdriver.Command
  18. func (d *Driver) createContainer(c *execdriver.Command, hooks execdriver.Hooks) (container *configs.Config, err error) {
  19. container = execdriver.InitContainer(c)
  20. if err := d.createIpc(container, c); err != nil {
  21. return nil, err
  22. }
  23. if err := d.createPid(container, c); err != nil {
  24. return nil, err
  25. }
  26. if err := d.createUTS(container, c); err != nil {
  27. return nil, err
  28. }
  29. if err := d.setupRemappedRoot(container, c); err != nil {
  30. return nil, err
  31. }
  32. if err := d.createNetwork(container, c, hooks); err != nil {
  33. return nil, err
  34. }
  35. if c.ProcessConfig.Privileged {
  36. if !container.Readonlyfs {
  37. // clear readonly for /sys
  38. for i := range container.Mounts {
  39. if container.Mounts[i].Destination == "/sys" {
  40. container.Mounts[i].Flags &= ^syscall.MS_RDONLY
  41. }
  42. }
  43. container.ReadonlyPaths = nil
  44. }
  45. // clear readonly for cgroup
  46. for i := range container.Mounts {
  47. if container.Mounts[i].Device == "cgroup" {
  48. container.Mounts[i].Flags &= ^syscall.MS_RDONLY
  49. }
  50. }
  51. container.MaskPaths = nil
  52. if err := d.setPrivileged(container); err != nil {
  53. return nil, err
  54. }
  55. } else {
  56. if err := d.setCapabilities(container, c); err != nil {
  57. return nil, err
  58. }
  59. }
  60. // add CAP_ prefix to all caps for new libcontainer update to match
  61. // the spec format.
  62. for i, s := range container.Capabilities {
  63. if !strings.HasPrefix(s, "CAP_") {
  64. container.Capabilities[i] = fmt.Sprintf("CAP_%s", s)
  65. }
  66. }
  67. container.AdditionalGroups = c.GroupAdd
  68. if c.AppArmorProfile != "" {
  69. container.AppArmorProfile = c.AppArmorProfile
  70. }
  71. if c.SeccompProfile != "" {
  72. container.Seccomp, err = loadSeccompProfile(c.SeccompProfile)
  73. if err != nil {
  74. return nil, err
  75. }
  76. }
  77. if err := execdriver.SetupCgroups(container, c); err != nil {
  78. return nil, err
  79. }
  80. container.OomScoreAdj = c.OomScoreAdj
  81. if container.Readonlyfs {
  82. for i := range container.Mounts {
  83. switch container.Mounts[i].Destination {
  84. case "/proc", "/dev", "/dev/pts":
  85. continue
  86. }
  87. container.Mounts[i].Flags |= syscall.MS_RDONLY
  88. }
  89. /* These paths must be remounted as r/o */
  90. container.ReadonlyPaths = append(container.ReadonlyPaths, "/dev")
  91. }
  92. if err := d.setupMounts(container, c); err != nil {
  93. return nil, err
  94. }
  95. d.setupLabels(container, c)
  96. d.setupRlimits(container, c)
  97. return container, nil
  98. }
  99. func (d *Driver) createNetwork(container *configs.Config, c *execdriver.Command, hooks execdriver.Hooks) error {
  100. if c.Network == nil {
  101. return nil
  102. }
  103. if c.Network.ContainerID != "" {
  104. d.Lock()
  105. active := d.activeContainers[c.Network.ContainerID]
  106. d.Unlock()
  107. if active == nil {
  108. return fmt.Errorf("%s is not a valid running container to join", c.Network.ContainerID)
  109. }
  110. state, err := active.State()
  111. if err != nil {
  112. return err
  113. }
  114. container.Namespaces.Add(configs.NEWNET, state.NamespacePaths[configs.NEWNET])
  115. return nil
  116. }
  117. if c.Network.NamespacePath != "" {
  118. container.Namespaces.Add(configs.NEWNET, c.Network.NamespacePath)
  119. return nil
  120. }
  121. // only set up prestart hook if the namespace path is not set (this should be
  122. // all cases *except* for --net=host shared networking)
  123. container.Hooks = &configs.Hooks{
  124. Prestart: []configs.Hook{
  125. configs.NewFunctionHook(func(s configs.HookState) error {
  126. if len(hooks.PreStart) > 0 {
  127. for _, fnHook := range hooks.PreStart {
  128. // A closed channel for OOM is returned here as it will be
  129. // non-blocking and return the correct result when read.
  130. chOOM := make(chan struct{})
  131. close(chOOM)
  132. if err := fnHook(&c.ProcessConfig, s.Pid, chOOM); err != nil {
  133. return err
  134. }
  135. }
  136. }
  137. return nil
  138. }),
  139. },
  140. }
  141. return nil
  142. }
  143. func (d *Driver) createIpc(container *configs.Config, c *execdriver.Command) error {
  144. if c.Ipc.HostIpc {
  145. container.Namespaces.Remove(configs.NEWIPC)
  146. return nil
  147. }
  148. if c.Ipc.ContainerID != "" {
  149. d.Lock()
  150. active := d.activeContainers[c.Ipc.ContainerID]
  151. d.Unlock()
  152. if active == nil {
  153. return fmt.Errorf("%s is not a valid running container to join", c.Ipc.ContainerID)
  154. }
  155. state, err := active.State()
  156. if err != nil {
  157. return err
  158. }
  159. container.Namespaces.Add(configs.NEWIPC, state.NamespacePaths[configs.NEWIPC])
  160. }
  161. return nil
  162. }
  163. func (d *Driver) createPid(container *configs.Config, c *execdriver.Command) error {
  164. if c.Pid.HostPid {
  165. container.Namespaces.Remove(configs.NEWPID)
  166. return nil
  167. }
  168. return nil
  169. }
  170. func (d *Driver) createUTS(container *configs.Config, c *execdriver.Command) error {
  171. if c.UTS.HostUTS {
  172. container.Namespaces.Remove(configs.NEWUTS)
  173. container.Hostname = ""
  174. return nil
  175. }
  176. return nil
  177. }
  178. func (d *Driver) setupRemappedRoot(container *configs.Config, c *execdriver.Command) error {
  179. if c.RemappedRoot.UID == 0 {
  180. container.Namespaces.Remove(configs.NEWUSER)
  181. return nil
  182. }
  183. // convert the Docker daemon id map to the libcontainer variant of the same struct
  184. // this keeps us from having to import libcontainer code across Docker client + daemon packages
  185. cuidMaps := []configs.IDMap{}
  186. cgidMaps := []configs.IDMap{}
  187. for _, idMap := range c.UIDMapping {
  188. cuidMaps = append(cuidMaps, configs.IDMap(idMap))
  189. }
  190. for _, idMap := range c.GIDMapping {
  191. cgidMaps = append(cgidMaps, configs.IDMap(idMap))
  192. }
  193. container.UidMappings = cuidMaps
  194. container.GidMappings = cgidMaps
  195. for _, node := range container.Devices {
  196. node.Uid = uint32(c.RemappedRoot.UID)
  197. node.Gid = uint32(c.RemappedRoot.GID)
  198. }
  199. // TODO: until a kernel/mount solution exists for handling remount in a user namespace,
  200. // we must clear the readonly flag for the cgroups mount (@mrunalp concurs)
  201. for i := range container.Mounts {
  202. if container.Mounts[i].Device == "cgroup" {
  203. container.Mounts[i].Flags &= ^syscall.MS_RDONLY
  204. }
  205. }
  206. return nil
  207. }
  208. func (d *Driver) setPrivileged(container *configs.Config) (err error) {
  209. container.Capabilities = execdriver.GetAllCapabilities()
  210. container.Cgroups.AllowAllDevices = true
  211. hostDevices, err := devices.HostDevices()
  212. if err != nil {
  213. return err
  214. }
  215. container.Devices = hostDevices
  216. if apparmor.IsEnabled() {
  217. container.AppArmorProfile = "unconfined"
  218. }
  219. return nil
  220. }
  221. func (d *Driver) setCapabilities(container *configs.Config, c *execdriver.Command) (err error) {
  222. container.Capabilities, err = execdriver.TweakCapabilities(container.Capabilities, c.CapAdd, c.CapDrop)
  223. return err
  224. }
  225. func (d *Driver) setupRlimits(container *configs.Config, c *execdriver.Command) {
  226. if c.Resources == nil {
  227. return
  228. }
  229. for _, rlimit := range c.Resources.Rlimits {
  230. container.Rlimits = append(container.Rlimits, configs.Rlimit{
  231. Type: rlimit.Type,
  232. Hard: rlimit.Hard,
  233. Soft: rlimit.Soft,
  234. })
  235. }
  236. }
  237. // If rootfs mount propagation is RPRIVATE, that means all the volumes are
  238. // going to be private anyway. There is no need to apply per volume
  239. // propagation on top. This is just an optimzation so that cost of per volume
  240. // propagation is paid only if user decides to make some volume non-private
  241. // which will force rootfs mount propagation to be non RPRIVATE.
  242. func checkResetVolumePropagation(container *configs.Config) {
  243. if container.RootPropagation != mount.RPRIVATE {
  244. return
  245. }
  246. for _, m := range container.Mounts {
  247. m.PropagationFlags = nil
  248. }
  249. }
  250. func getMountInfo(mountinfo []*mount.Info, dir string) *mount.Info {
  251. for _, m := range mountinfo {
  252. if m.Mountpoint == dir {
  253. return m
  254. }
  255. }
  256. return nil
  257. }
  258. // Get the source mount point of directory passed in as argument. Also return
  259. // optional fields.
  260. func getSourceMount(source string) (string, string, error) {
  261. // Ensure any symlinks are resolved.
  262. sourcePath, err := filepath.EvalSymlinks(source)
  263. if err != nil {
  264. return "", "", err
  265. }
  266. mountinfos, err := mount.GetMounts()
  267. if err != nil {
  268. return "", "", err
  269. }
  270. mountinfo := getMountInfo(mountinfos, sourcePath)
  271. if mountinfo != nil {
  272. return sourcePath, mountinfo.Optional, nil
  273. }
  274. path := sourcePath
  275. for {
  276. path = filepath.Dir(path)
  277. mountinfo = getMountInfo(mountinfos, path)
  278. if mountinfo != nil {
  279. return path, mountinfo.Optional, nil
  280. }
  281. if path == "/" {
  282. break
  283. }
  284. }
  285. // If we are here, we did not find parent mount. Something is wrong.
  286. return "", "", fmt.Errorf("Could not find source mount of %s", source)
  287. }
  288. // Ensure mount point on which path is mouted, is shared.
  289. func ensureShared(path string) error {
  290. sharedMount := false
  291. sourceMount, optionalOpts, err := getSourceMount(path)
  292. if err != nil {
  293. return err
  294. }
  295. // Make sure source mount point is shared.
  296. optsSplit := strings.Split(optionalOpts, " ")
  297. for _, opt := range optsSplit {
  298. if strings.HasPrefix(opt, "shared:") {
  299. sharedMount = true
  300. break
  301. }
  302. }
  303. if !sharedMount {
  304. return fmt.Errorf("Path %s is mounted on %s but it is not a shared mount.", path, sourceMount)
  305. }
  306. return nil
  307. }
  308. // Ensure mount point on which path is mounted, is either shared or slave.
  309. func ensureSharedOrSlave(path string) error {
  310. sharedMount := false
  311. slaveMount := false
  312. sourceMount, optionalOpts, err := getSourceMount(path)
  313. if err != nil {
  314. return err
  315. }
  316. // Make sure source mount point is shared.
  317. optsSplit := strings.Split(optionalOpts, " ")
  318. for _, opt := range optsSplit {
  319. if strings.HasPrefix(opt, "shared:") {
  320. sharedMount = true
  321. break
  322. } else if strings.HasPrefix(opt, "master:") {
  323. slaveMount = true
  324. break
  325. }
  326. }
  327. if !sharedMount && !slaveMount {
  328. return fmt.Errorf("Path %s is mounted on %s but it is not a shared or slave mount.", path, sourceMount)
  329. }
  330. return nil
  331. }
  332. func (d *Driver) setupMounts(container *configs.Config, c *execdriver.Command) error {
  333. userMounts := make(map[string]struct{})
  334. for _, m := range c.Mounts {
  335. userMounts[m.Destination] = struct{}{}
  336. }
  337. // Filter out mounts that are overriden by user supplied mounts
  338. var defaultMounts []*configs.Mount
  339. _, mountDev := userMounts["/dev"]
  340. for _, m := range container.Mounts {
  341. if _, ok := userMounts[m.Destination]; !ok {
  342. if mountDev && strings.HasPrefix(m.Destination, "/dev/") {
  343. container.Devices = nil
  344. continue
  345. }
  346. defaultMounts = append(defaultMounts, m)
  347. }
  348. }
  349. container.Mounts = defaultMounts
  350. mountPropagationMap := map[string]int{
  351. "private": mount.PRIVATE,
  352. "rprivate": mount.RPRIVATE,
  353. "shared": mount.SHARED,
  354. "rshared": mount.RSHARED,
  355. "slave": mount.SLAVE,
  356. "rslave": mount.RSLAVE,
  357. }
  358. for _, m := range c.Mounts {
  359. for _, cm := range container.Mounts {
  360. if cm.Destination == m.Destination {
  361. return derr.ErrorCodeMountDup.WithArgs(m.Destination)
  362. }
  363. }
  364. if m.Source == "tmpfs" {
  365. var (
  366. data = "size=65536k"
  367. flags = syscall.MS_NOEXEC | syscall.MS_NOSUID | syscall.MS_NODEV
  368. err error
  369. )
  370. fulldest := filepath.Join(c.Rootfs, m.Destination)
  371. if m.Data != "" {
  372. flags, data, err = mount.ParseTmpfsOptions(m.Data)
  373. if err != nil {
  374. return err
  375. }
  376. }
  377. container.Mounts = append(container.Mounts, &configs.Mount{
  378. Source: m.Source,
  379. Destination: m.Destination,
  380. Data: data,
  381. Device: "tmpfs",
  382. Flags: flags,
  383. PremountCmds: genTmpfsPremountCmd(c.TmpDir, fulldest, m.Destination),
  384. PostmountCmds: genTmpfsPostmountCmd(c.TmpDir, fulldest, m.Destination),
  385. PropagationFlags: []int{mountPropagationMap[volume.DefaultPropagationMode]},
  386. })
  387. continue
  388. }
  389. flags := syscall.MS_BIND | syscall.MS_REC
  390. var pFlag int
  391. if !m.Writable {
  392. flags |= syscall.MS_RDONLY
  393. }
  394. // Determine property of RootPropagation based on volume
  395. // properties. If a volume is shared, then keep root propagtion
  396. // shared. This should work for slave and private volumes too.
  397. //
  398. // For slave volumes, it can be either [r]shared/[r]slave.
  399. //
  400. // For private volumes any root propagation value should work.
  401. pFlag = mountPropagationMap[m.Propagation]
  402. if pFlag == mount.SHARED || pFlag == mount.RSHARED {
  403. if err := ensureShared(m.Source); err != nil {
  404. return err
  405. }
  406. rootpg := container.RootPropagation
  407. if rootpg != mount.SHARED && rootpg != mount.RSHARED {
  408. execdriver.SetRootPropagation(container, mount.SHARED)
  409. }
  410. } else if pFlag == mount.SLAVE || pFlag == mount.RSLAVE {
  411. if err := ensureSharedOrSlave(m.Source); err != nil {
  412. return err
  413. }
  414. rootpg := container.RootPropagation
  415. if rootpg != mount.SHARED && rootpg != mount.RSHARED && rootpg != mount.SLAVE && rootpg != mount.RSLAVE {
  416. execdriver.SetRootPropagation(container, mount.RSLAVE)
  417. }
  418. }
  419. mount := &configs.Mount{
  420. Source: m.Source,
  421. Destination: m.Destination,
  422. Device: "bind",
  423. Flags: flags,
  424. }
  425. if pFlag != 0 {
  426. mount.PropagationFlags = []int{pFlag}
  427. }
  428. container.Mounts = append(container.Mounts, mount)
  429. }
  430. checkResetVolumePropagation(container)
  431. return nil
  432. }
  433. func (d *Driver) setupLabels(container *configs.Config, c *execdriver.Command) {
  434. container.ProcessLabel = c.ProcessLabel
  435. container.MountLabel = c.MountLabel
  436. }