container_operations_unix.go 16 KB

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