container_operations_unix.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. //go:build linux || freebsd
  2. // +build linux freebsd
  3. package daemon // import "github.com/docker/docker/daemon"
  4. import (
  5. "fmt"
  6. "os"
  7. "path/filepath"
  8. "strconv"
  9. "syscall"
  10. "github.com/docker/docker/container"
  11. "github.com/docker/docker/daemon/links"
  12. "github.com/docker/docker/errdefs"
  13. "github.com/docker/docker/libnetwork"
  14. "github.com/docker/docker/pkg/idtools"
  15. "github.com/docker/docker/pkg/stringid"
  16. "github.com/docker/docker/pkg/system"
  17. "github.com/docker/docker/runconfig"
  18. "github.com/moby/sys/mount"
  19. "github.com/opencontainers/selinux/go-selinux/label"
  20. "github.com/pkg/errors"
  21. "github.com/sirupsen/logrus"
  22. "golang.org/x/sys/unix"
  23. )
  24. func (daemon *Daemon) setupLinkedContainers(container *container.Container) ([]string, error) {
  25. var env []string
  26. children := daemon.children(container)
  27. bridgeSettings := container.NetworkSettings.Networks[runconfig.DefaultDaemonNetworkMode().NetworkName()]
  28. if bridgeSettings == nil || bridgeSettings.EndpointSettings == nil {
  29. return nil, nil
  30. }
  31. for linkAlias, child := range children {
  32. if !child.IsRunning() {
  33. return nil, fmt.Errorf("Cannot link to a non running container: %s AS %s", child.Name, linkAlias)
  34. }
  35. childBridgeSettings := child.NetworkSettings.Networks[runconfig.DefaultDaemonNetworkMode().NetworkName()]
  36. if childBridgeSettings == nil || childBridgeSettings.EndpointSettings == nil {
  37. return nil, fmt.Errorf("container %s not attached to default bridge network", child.ID)
  38. }
  39. link := links.NewLink(
  40. bridgeSettings.IPAddress,
  41. childBridgeSettings.IPAddress,
  42. linkAlias,
  43. child.Config.Env,
  44. child.Config.ExposedPorts,
  45. )
  46. env = append(env, link.ToEnv()...)
  47. }
  48. return env, nil
  49. }
  50. func (daemon *Daemon) getIpcContainer(id string) (*container.Container, error) {
  51. errMsg := "can't join IPC of container " + id
  52. // Check the container exists
  53. ctr, err := daemon.GetContainer(id)
  54. if err != nil {
  55. return nil, errors.Wrap(err, errMsg)
  56. }
  57. // Check the container is running and not restarting
  58. if err := daemon.checkContainer(ctr, containerIsRunning, containerIsNotRestarting); err != nil {
  59. return nil, errors.Wrap(err, errMsg)
  60. }
  61. // Check the container ipc is shareable
  62. if st, err := os.Stat(ctr.ShmPath); err != nil || !st.IsDir() {
  63. if err == nil || os.IsNotExist(err) {
  64. return nil, errors.New(errMsg + ": non-shareable IPC (hint: use IpcMode:shareable for the donor container)")
  65. }
  66. // stat() failed?
  67. return nil, errors.Wrap(err, errMsg+": unexpected error from stat "+ctr.ShmPath)
  68. }
  69. return ctr, nil
  70. }
  71. func (daemon *Daemon) getPidContainer(ctr *container.Container) (*container.Container, error) {
  72. containerID := ctr.HostConfig.PidMode.Container()
  73. ctr, err := daemon.GetContainer(containerID)
  74. if err != nil {
  75. return nil, errors.Wrapf(err, "cannot join PID of a non running container: %s", containerID)
  76. }
  77. return ctr, daemon.checkContainer(ctr, containerIsRunning, containerIsNotRestarting)
  78. }
  79. func containerIsRunning(c *container.Container) error {
  80. if !c.IsRunning() {
  81. return errdefs.Conflict(errors.Errorf("container %s is not running", c.ID))
  82. }
  83. return nil
  84. }
  85. func containerIsNotRestarting(c *container.Container) error {
  86. if c.IsRestarting() {
  87. return errContainerIsRestarting(c.ID)
  88. }
  89. return nil
  90. }
  91. func (daemon *Daemon) setupIpcDirs(c *container.Container) error {
  92. ipcMode := c.HostConfig.IpcMode
  93. switch {
  94. case ipcMode.IsContainer():
  95. ic, err := daemon.getIpcContainer(ipcMode.Container())
  96. if err != nil {
  97. return err
  98. }
  99. c.ShmPath = ic.ShmPath
  100. case ipcMode.IsHost():
  101. if _, err := os.Stat("/dev/shm"); err != nil {
  102. return fmt.Errorf("/dev/shm is not mounted, but must be for --ipc=host")
  103. }
  104. c.ShmPath = "/dev/shm"
  105. case ipcMode.IsPrivate(), ipcMode.IsNone():
  106. // c.ShmPath will/should not be used, so make it empty.
  107. // Container's /dev/shm mount comes from OCI spec.
  108. c.ShmPath = ""
  109. case ipcMode.IsEmpty():
  110. // A container was created by an older version of the daemon.
  111. // The default behavior used to be what is now called "shareable".
  112. fallthrough
  113. case ipcMode.IsShareable():
  114. rootIDs := daemon.idMapping.RootPair()
  115. if !c.HasMountFor("/dev/shm") {
  116. shmPath, err := c.ShmResourcePath()
  117. if err != nil {
  118. return err
  119. }
  120. if err := idtools.MkdirAllAndChown(shmPath, 0700, rootIDs); err != nil {
  121. return err
  122. }
  123. shmproperty := "mode=1777,size=" + strconv.FormatInt(c.HostConfig.ShmSize, 10)
  124. if err := unix.Mount("shm", shmPath, "tmpfs", uintptr(unix.MS_NOEXEC|unix.MS_NOSUID|unix.MS_NODEV), label.FormatMountLabel(shmproperty, c.GetMountLabel())); err != nil {
  125. return fmt.Errorf("mounting shm tmpfs: %s", err)
  126. }
  127. if err := os.Chown(shmPath, rootIDs.UID, rootIDs.GID); err != nil {
  128. return err
  129. }
  130. c.ShmPath = shmPath
  131. }
  132. default:
  133. return fmt.Errorf("invalid IPC mode: %v", ipcMode)
  134. }
  135. return nil
  136. }
  137. func (daemon *Daemon) setupSecretDir(c *container.Container) (setupErr error) {
  138. if len(c.SecretReferences) == 0 && len(c.ConfigReferences) == 0 {
  139. return nil
  140. }
  141. if err := daemon.createSecretsDir(c); err != nil {
  142. return err
  143. }
  144. defer func() {
  145. if setupErr != nil {
  146. daemon.cleanupSecretDir(c)
  147. }
  148. }()
  149. if c.DependencyStore == nil {
  150. return fmt.Errorf("secret store is not initialized")
  151. }
  152. // retrieve possible remapped range start for root UID, GID
  153. rootIDs := daemon.idMapping.RootPair()
  154. for _, s := range c.SecretReferences {
  155. // TODO (ehazlett): use type switch when more are supported
  156. if s.File == nil {
  157. logrus.Error("secret target type is not a file target")
  158. continue
  159. }
  160. // secrets are created in the SecretMountPath on the host, at a
  161. // single level
  162. fPath, err := c.SecretFilePath(*s)
  163. if err != nil {
  164. return errors.Wrap(err, "error getting secret file path")
  165. }
  166. if err := idtools.MkdirAllAndChown(filepath.Dir(fPath), 0700, rootIDs); err != nil {
  167. return errors.Wrap(err, "error creating secret mount path")
  168. }
  169. logrus.WithFields(logrus.Fields{
  170. "name": s.File.Name,
  171. "path": fPath,
  172. }).Debug("injecting secret")
  173. secret, err := c.DependencyStore.Secrets().Get(s.SecretID)
  174. if err != nil {
  175. return errors.Wrap(err, "unable to get secret from secret store")
  176. }
  177. if err := os.WriteFile(fPath, secret.Spec.Data, s.File.Mode); err != nil {
  178. return errors.Wrap(err, "error injecting secret")
  179. }
  180. uid, err := strconv.Atoi(s.File.UID)
  181. if err != nil {
  182. return err
  183. }
  184. gid, err := strconv.Atoi(s.File.GID)
  185. if err != nil {
  186. return err
  187. }
  188. if err := os.Chown(fPath, rootIDs.UID+uid, rootIDs.GID+gid); err != nil {
  189. return errors.Wrap(err, "error setting ownership for secret")
  190. }
  191. if err := os.Chmod(fPath, s.File.Mode); err != nil {
  192. return errors.Wrap(err, "error setting file mode for secret")
  193. }
  194. }
  195. for _, configRef := range c.ConfigReferences {
  196. // TODO (ehazlett): use type switch when more are supported
  197. if configRef.File == nil {
  198. // Runtime configs are not mounted into the container, but they're
  199. // a valid type of config so we should not error when we encounter
  200. // one.
  201. if configRef.Runtime == nil {
  202. logrus.Error("config target type is not a file or runtime target")
  203. }
  204. // However, in any case, this isn't a file config, so we have no
  205. // further work to do
  206. continue
  207. }
  208. fPath, err := c.ConfigFilePath(*configRef)
  209. if err != nil {
  210. return errors.Wrap(err, "error getting config file path for container")
  211. }
  212. if err := idtools.MkdirAllAndChown(filepath.Dir(fPath), 0700, rootIDs); err != nil {
  213. return errors.Wrap(err, "error creating config mount path")
  214. }
  215. logrus.WithFields(logrus.Fields{
  216. "name": configRef.File.Name,
  217. "path": fPath,
  218. }).Debug("injecting config")
  219. config, err := c.DependencyStore.Configs().Get(configRef.ConfigID)
  220. if err != nil {
  221. return errors.Wrap(err, "unable to get config from config store")
  222. }
  223. if err := os.WriteFile(fPath, config.Spec.Data, configRef.File.Mode); err != nil {
  224. return errors.Wrap(err, "error injecting config")
  225. }
  226. uid, err := strconv.Atoi(configRef.File.UID)
  227. if err != nil {
  228. return err
  229. }
  230. gid, err := strconv.Atoi(configRef.File.GID)
  231. if err != nil {
  232. return err
  233. }
  234. if err := os.Chown(fPath, rootIDs.UID+uid, rootIDs.GID+gid); err != nil {
  235. return errors.Wrap(err, "error setting ownership for config")
  236. }
  237. if err := os.Chmod(fPath, configRef.File.Mode); err != nil {
  238. return errors.Wrap(err, "error setting file mode for config")
  239. }
  240. }
  241. return daemon.remountSecretDir(c)
  242. }
  243. // createSecretsDir is used to create a dir suitable for storing container secrets.
  244. // In practice this is using a tmpfs mount and is used for both "configs" and "secrets"
  245. func (daemon *Daemon) createSecretsDir(c *container.Container) error {
  246. // retrieve possible remapped range start for root UID, GID
  247. rootIDs := daemon.idMapping.RootPair()
  248. dir, err := c.SecretMountPath()
  249. if err != nil {
  250. return errors.Wrap(err, "error getting container secrets dir")
  251. }
  252. // create tmpfs
  253. if err := idtools.MkdirAllAndChown(dir, 0700, rootIDs); err != nil {
  254. return errors.Wrap(err, "error creating secret local mount path")
  255. }
  256. tmpfsOwnership := fmt.Sprintf("uid=%d,gid=%d", rootIDs.UID, rootIDs.GID)
  257. if err := mount.Mount("tmpfs", dir, "tmpfs", "nodev,nosuid,noexec,"+tmpfsOwnership); err != nil {
  258. return errors.Wrap(err, "unable to setup secret mount")
  259. }
  260. return nil
  261. }
  262. func (daemon *Daemon) remountSecretDir(c *container.Container) error {
  263. dir, err := c.SecretMountPath()
  264. if err != nil {
  265. return errors.Wrap(err, "error getting container secrets path")
  266. }
  267. if err := label.Relabel(dir, c.MountLabel, false); err != nil {
  268. logrus.WithError(err).WithField("dir", dir).Warn("Error while attempting to set selinux label")
  269. }
  270. rootIDs := daemon.idMapping.RootPair()
  271. tmpfsOwnership := fmt.Sprintf("uid=%d,gid=%d", rootIDs.UID, rootIDs.GID)
  272. // remount secrets ro
  273. if err := mount.Mount("tmpfs", dir, "tmpfs", "remount,ro,"+tmpfsOwnership); err != nil {
  274. return errors.Wrap(err, "unable to remount dir as readonly")
  275. }
  276. return nil
  277. }
  278. func (daemon *Daemon) cleanupSecretDir(c *container.Container) {
  279. dir, err := c.SecretMountPath()
  280. if err != nil {
  281. logrus.WithError(err).WithField("container", c.ID).Warn("error getting secrets mount path for container")
  282. }
  283. if err := mount.RecursiveUnmount(dir); err != nil {
  284. logrus.WithField("dir", dir).WithError(err).Warn("Error while attempting to unmount dir, this may prevent removal of container.")
  285. }
  286. if err := os.RemoveAll(dir); err != nil {
  287. logrus.WithField("dir", dir).WithError(err).Error("Error removing dir.")
  288. }
  289. }
  290. func killProcessDirectly(container *container.Container) error {
  291. pid := container.GetPID()
  292. if pid == 0 {
  293. // Ensure that we don't kill ourselves
  294. return nil
  295. }
  296. if err := unix.Kill(pid, syscall.SIGKILL); err != nil {
  297. if err != unix.ESRCH {
  298. return errdefs.System(err)
  299. }
  300. err = errNoSuchProcess{pid, syscall.SIGKILL}
  301. logrus.WithError(err).WithField("container", container.ID).Debug("no such process")
  302. return err
  303. }
  304. // In case there were some exceptions(e.g., state of zombie and D)
  305. if system.IsProcessAlive(pid) {
  306. // Since we can not kill a zombie pid, add zombie check here
  307. isZombie, err := system.IsProcessZombie(pid)
  308. // TODO(thaJeztah) should we ignore os.IsNotExist() here? ("/proc/<pid>/stat" will be gone if the process exited)
  309. if err != nil {
  310. logrus.WithError(err).WithField("container", container.ID).Warn("Container state is invalid")
  311. return err
  312. }
  313. if isZombie {
  314. return errdefs.System(errors.Errorf("container %s PID %d is zombie and can not be killed. Use the --init option when creating containers to run an init inside the container that forwards signals and reaps processes", stringid.TruncateID(container.ID), pid))
  315. }
  316. }
  317. return nil
  318. }
  319. func isLinkable(child *container.Container) bool {
  320. // A container is linkable only if it belongs to the default network
  321. _, ok := child.NetworkSettings.Networks[runconfig.DefaultDaemonNetworkMode().NetworkName()]
  322. return ok
  323. }
  324. func enableIPOnPredefinedNetwork() bool {
  325. return false
  326. }
  327. // serviceDiscoveryOnDefaultNetwork indicates if service discovery is supported on the default network
  328. func serviceDiscoveryOnDefaultNetwork() bool {
  329. return false
  330. }
  331. func (daemon *Daemon) setupPathsAndSandboxOptions(container *container.Container, sboxOptions *[]libnetwork.SandboxOption) error {
  332. var err error
  333. // Set the correct paths for /etc/hosts and /etc/resolv.conf, based on the
  334. // networking-mode of the container. Note that containers with "container"
  335. // networking are already handled in "initializeNetworking()" before we reach
  336. // this function, so do not have to be accounted for here.
  337. switch {
  338. case container.HostConfig.NetworkMode.IsHost():
  339. // In host-mode networking, the container does not have its own networking
  340. // namespace, so both `/etc/hosts` and `/etc/resolv.conf` should be the same
  341. // as on the host itself. The container gets a copy of these files.
  342. *sboxOptions = append(
  343. *sboxOptions,
  344. libnetwork.OptionOriginHostsPath("/etc/hosts"),
  345. libnetwork.OptionOriginResolvConfPath("/etc/resolv.conf"),
  346. )
  347. case container.HostConfig.NetworkMode.IsUserDefined():
  348. // The container uses a user-defined network. We use the embedded DNS
  349. // server for container name resolution and to act as a DNS forwarder
  350. // for external DNS resolution.
  351. // We parse the DNS server(s) that are defined in /etc/resolv.conf on
  352. // the host, which may be a local DNS server (for example, if DNSMasq or
  353. // systemd-resolvd are in use). The embedded DNS server forwards DNS
  354. // resolution to the DNS server configured on the host, which in itself
  355. // may act as a forwarder for external DNS servers.
  356. // If systemd-resolvd is used, the "upstream" DNS servers can be found in
  357. // /run/systemd/resolve/resolv.conf. We do not query those DNS servers
  358. // directly, as they can be dynamically reconfigured.
  359. *sboxOptions = append(
  360. *sboxOptions,
  361. libnetwork.OptionOriginResolvConfPath("/etc/resolv.conf"),
  362. )
  363. default:
  364. // For other situations, such as the default bridge network, container
  365. // discovery / name resolution is handled through /etc/hosts, and no
  366. // embedded DNS server is available. Without the embedded DNS, we
  367. // cannot use local DNS servers on the host (for example, if DNSMasq or
  368. // systemd-resolvd is used). If systemd-resolvd is used, we try to
  369. // determine the external DNS servers that are used on the host.
  370. // This situation is not ideal, because DNS servers configured in the
  371. // container are not updated after the container is created, but the
  372. // DNS servers on the host can be dynamically updated.
  373. //
  374. // Copy the host's resolv.conf for the container (/run/systemd/resolve/resolv.conf or /etc/resolv.conf)
  375. *sboxOptions = append(
  376. *sboxOptions,
  377. libnetwork.OptionOriginResolvConfPath(daemon.configStore.GetResolvConf()),
  378. )
  379. }
  380. container.HostsPath, err = container.GetRootResourcePath("hosts")
  381. if err != nil {
  382. return err
  383. }
  384. *sboxOptions = append(*sboxOptions, libnetwork.OptionHostsPath(container.HostsPath))
  385. container.ResolvConfPath, err = container.GetRootResourcePath("resolv.conf")
  386. if err != nil {
  387. return err
  388. }
  389. *sboxOptions = append(*sboxOptions, libnetwork.OptionResolvConfPath(container.ResolvConfPath))
  390. return nil
  391. }
  392. func (daemon *Daemon) initializeNetworkingPaths(container *container.Container, nc *container.Container) error {
  393. container.HostnamePath = nc.HostnamePath
  394. container.HostsPath = nc.HostsPath
  395. container.ResolvConfPath = nc.ResolvConfPath
  396. return nil
  397. }
  398. func (daemon *Daemon) setupContainerMountsRoot(c *container.Container) error {
  399. // get the root mount path so we can make it unbindable
  400. p, err := c.MountsResourcePath("")
  401. if err != nil {
  402. return err
  403. }
  404. return idtools.MkdirAllAndChown(p, 0710, idtools.Identity{UID: idtools.CurrentIdentity().UID, GID: daemon.IdentityMapping().RootPair().GID})
  405. }