container_operations_unix.go 16 KB

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