create.go 13 KB

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