runtime_unix.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. //go:build !windows
  2. // +build !windows
  3. package daemon
  4. import (
  5. "fmt"
  6. "os"
  7. "os/exec"
  8. "path/filepath"
  9. "strings"
  10. v2runcoptions "github.com/containerd/containerd/runtime/v2/runc/options"
  11. "github.com/containerd/containerd/runtime/v2/shim"
  12. "github.com/docker/docker/api/types"
  13. "github.com/docker/docker/daemon/config"
  14. "github.com/docker/docker/errdefs"
  15. "github.com/docker/docker/pkg/ioutils"
  16. "github.com/pkg/errors"
  17. "github.com/sirupsen/logrus"
  18. )
  19. const (
  20. defaultRuntimeName = "runc"
  21. linuxShimV2 = "io.containerd.runc.v2"
  22. )
  23. func configureRuntimes(conf *config.Config) {
  24. if conf.DefaultRuntime == "" {
  25. conf.DefaultRuntime = config.StockRuntimeName
  26. }
  27. if conf.Runtimes == nil {
  28. conf.Runtimes = make(map[string]types.Runtime)
  29. }
  30. conf.Runtimes[config.LinuxV2RuntimeName] = types.Runtime{Path: defaultRuntimeName, Shim: defaultV2ShimConfig(conf, defaultRuntimeName)}
  31. conf.Runtimes[config.StockRuntimeName] = conf.Runtimes[config.LinuxV2RuntimeName]
  32. }
  33. func defaultV2ShimConfig(conf *config.Config, runtimePath string) *types.ShimConfig {
  34. return &types.ShimConfig{
  35. Binary: linuxShimV2,
  36. Opts: &v2runcoptions.Options{
  37. BinaryName: runtimePath,
  38. Root: filepath.Join(conf.ExecRoot, "runtime-"+defaultRuntimeName),
  39. SystemdCgroup: UsingSystemd(conf),
  40. NoPivotRoot: os.Getenv("DOCKER_RAMDISK") != "",
  41. },
  42. }
  43. }
  44. func (daemon *Daemon) loadRuntimes() error {
  45. return daemon.initRuntimes(daemon.configStore.Runtimes)
  46. }
  47. func (daemon *Daemon) initRuntimes(runtimes map[string]types.Runtime) (err error) {
  48. runtimeDir := filepath.Join(daemon.configStore.Root, "runtimes")
  49. // Remove old temp directory if any
  50. os.RemoveAll(runtimeDir + "-old")
  51. tmpDir, err := ioutils.TempDir(daemon.configStore.Root, "gen-runtimes")
  52. if err != nil {
  53. return errors.Wrap(err, "failed to get temp dir to generate runtime scripts")
  54. }
  55. defer func() {
  56. if err != nil {
  57. if err1 := os.RemoveAll(tmpDir); err1 != nil {
  58. logrus.WithError(err1).WithField("dir", tmpDir).
  59. Warn("failed to remove tmp dir")
  60. }
  61. return
  62. }
  63. if err = os.Rename(runtimeDir, runtimeDir+"-old"); err != nil {
  64. return
  65. }
  66. if err = os.Rename(tmpDir, runtimeDir); err != nil {
  67. err = errors.Wrap(err, "failed to setup runtimes dir, new containers may not start")
  68. return
  69. }
  70. if err = os.RemoveAll(runtimeDir + "-old"); err != nil {
  71. logrus.WithError(err).WithField("dir", tmpDir).
  72. Warn("failed to remove old runtimes dir")
  73. }
  74. }()
  75. for name, rt := range runtimes {
  76. if len(rt.Args) > 0 {
  77. script := filepath.Join(tmpDir, name)
  78. content := fmt.Sprintf("#!/bin/sh\n%s %s $@\n", rt.Path, strings.Join(rt.Args, " "))
  79. if err := os.WriteFile(script, []byte(content), 0700); err != nil {
  80. return err
  81. }
  82. }
  83. if rt.Shim == nil {
  84. rt.Shim = defaultV2ShimConfig(daemon.configStore, rt.Path)
  85. }
  86. }
  87. return nil
  88. }
  89. // rewriteRuntimePath is used for runtimes which have custom arguments supplied.
  90. // This is needed because the containerd API only calls the OCI runtime binary, there is no options for extra arguments.
  91. // To support this case, the daemon wraps the specified runtime in a script that passes through those arguments.
  92. func (daemon *Daemon) rewriteRuntimePath(name, p string, args []string) (string, error) {
  93. if len(args) == 0 {
  94. return p, nil
  95. }
  96. // Check that the runtime path actually exists here so that we can return a well known error.
  97. if _, err := exec.LookPath(p); err != nil {
  98. return "", errors.Wrap(err, "error while looking up the specified runtime path")
  99. }
  100. return filepath.Join(daemon.configStore.Root, "runtimes", name), nil
  101. }
  102. func (daemon *Daemon) getRuntime(name string) (*types.Runtime, error) {
  103. rt := daemon.configStore.GetRuntime(name)
  104. if rt == nil {
  105. if !isPermissibleC8dRuntimeName(name) {
  106. return nil, errdefs.InvalidParameter(errors.Errorf("unknown or invalid runtime name: %s", name))
  107. }
  108. return &types.Runtime{Shim: &types.ShimConfig{Binary: name}}, nil
  109. }
  110. if len(rt.Args) > 0 {
  111. p, err := daemon.rewriteRuntimePath(name, rt.Path, rt.Args)
  112. if err != nil {
  113. return nil, err
  114. }
  115. rt.Path = p
  116. rt.Args = nil
  117. }
  118. if rt.Shim == nil {
  119. rt.Shim = defaultV2ShimConfig(daemon.configStore, rt.Path)
  120. }
  121. return rt, nil
  122. }
  123. // isPermissibleC8dRuntimeName tests whether name is safe to pass into
  124. // containerd as a runtime name, and whether the name is well-formed.
  125. // It does not check if the runtime is installed.
  126. //
  127. // A runtime name containing slash characters is interpreted by containerd as
  128. // the path to a runtime binary. If we allowed this, anyone with Engine API
  129. // access could get containerd to execute an arbitrary binary as root. Although
  130. // Engine API access is already equivalent to root on the host, the runtime name
  131. // has not historically been a vector to run arbitrary code as root so users are
  132. // not expecting it to become one.
  133. //
  134. // This restriction is not configurable. There are viable workarounds for
  135. // legitimate use cases: administrators and runtime developers can make runtimes
  136. // available for use with Docker by installing them onto PATH following the
  137. // [binary naming convention] for containerd Runtime v2.
  138. //
  139. // [binary naming convention]: https://github.com/containerd/containerd/blob/main/runtime/v2/README.md#binary-naming
  140. func isPermissibleC8dRuntimeName(name string) bool {
  141. // containerd uses a rather permissive test to validate runtime names:
  142. //
  143. // - Any name for which filepath.IsAbs(name) is interpreted as the absolute
  144. // path to a shim binary. We want to block this behaviour.
  145. // - Any name which contains at least one '.' character and no '/' characters
  146. // and does not begin with a '.' character is a valid runtime name. The shim
  147. // binary name is derived from the final two components of the name and
  148. // searched for on the PATH. The name "a.." is technically valid per
  149. // containerd's implementation: it would resolve to a binary named
  150. // "containerd-shim---".
  151. //
  152. // https://github.com/containerd/containerd/blob/11ded166c15f92450958078cd13c6d87131ec563/runtime/v2/manager.go#L297-L317
  153. // https://github.com/containerd/containerd/blob/11ded166c15f92450958078cd13c6d87131ec563/runtime/v2/shim/util.go#L83-L93
  154. return !filepath.IsAbs(name) && !strings.ContainsRune(name, '/') && shim.BinaryName(name) != ""
  155. }