2019-04-10 10:59:33 -04:00
|
|
|
package specconv // import "github.com/docker/docker/rootless/specconv"
|
2018-10-15 03:52:53 -04:00
|
|
|
|
|
|
|
import (
|
2021-01-19 00:12:43 -05:00
|
|
|
"os"
|
|
|
|
"path"
|
2018-10-15 03:52:53 -04:00
|
|
|
"strconv"
|
2021-03-23 05:11:35 -04:00
|
|
|
"strings"
|
2018-10-15 03:52:53 -04:00
|
|
|
|
2019-08-05 10:37:47 -04:00
|
|
|
specs "github.com/opencontainers/runtime-spec/specs-go"
|
2021-03-23 05:11:35 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2018-10-15 03:52:53 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// ToRootless converts spec to be compatible with "rootless" runc.
|
2020-02-10 00:37:22 -05:00
|
|
|
// * Remove non-supported cgroups
|
2018-10-15 03:52:53 -04:00
|
|
|
// * Fix up OOMScoreAdj
|
2021-01-19 00:12:43 -05:00
|
|
|
// * Fix up /proc if --pid=host
|
2020-02-10 00:37:22 -05:00
|
|
|
//
|
|
|
|
// v2Controllers should be non-nil only if running with v2 and systemd.
|
|
|
|
func ToRootless(spec *specs.Spec, v2Controllers []string) error {
|
|
|
|
return toRootless(spec, v2Controllers, getCurrentOOMScoreAdj())
|
2018-10-15 03:52:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func getCurrentOOMScoreAdj() int {
|
2021-08-24 06:10:50 -04:00
|
|
|
b, err := os.ReadFile("/proc/self/oom_score_adj")
|
2018-10-15 03:52:53 -04:00
|
|
|
if err != nil {
|
2021-03-23 05:11:35 -04:00
|
|
|
logrus.WithError(err).Warn("failed to read /proc/self/oom_score_adj")
|
2018-10-15 03:52:53 -04:00
|
|
|
return 0
|
|
|
|
}
|
2021-03-23 05:11:35 -04:00
|
|
|
s := string(b)
|
|
|
|
i, err := strconv.Atoi(strings.TrimSpace(s))
|
2018-10-15 03:52:53 -04:00
|
|
|
if err != nil {
|
2021-03-23 05:11:35 -04:00
|
|
|
logrus.WithError(err).Warnf("failed to parse /proc/self/oom_score_adj (%q)", s)
|
2018-10-15 03:52:53 -04:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2020-02-10 00:37:22 -05:00
|
|
|
func toRootless(spec *specs.Spec, v2Controllers []string, currentOOMScoreAdj int) error {
|
|
|
|
if len(v2Controllers) == 0 {
|
|
|
|
// Remove cgroup settings.
|
|
|
|
spec.Linux.Resources = nil
|
|
|
|
spec.Linux.CgroupsPath = ""
|
|
|
|
} else {
|
|
|
|
if spec.Linux.Resources != nil {
|
|
|
|
m := make(map[string]struct{})
|
|
|
|
for _, s := range v2Controllers {
|
|
|
|
m[s] = struct{}{}
|
|
|
|
}
|
|
|
|
// Remove devices: https://github.com/containers/crun/issues/255
|
|
|
|
spec.Linux.Resources.Devices = nil
|
|
|
|
if _, ok := m["memory"]; !ok {
|
|
|
|
spec.Linux.Resources.Memory = nil
|
|
|
|
}
|
|
|
|
if _, ok := m["cpu"]; !ok {
|
|
|
|
spec.Linux.Resources.CPU = nil
|
|
|
|
}
|
|
|
|
if _, ok := m["cpuset"]; !ok {
|
|
|
|
if spec.Linux.Resources.CPU != nil {
|
|
|
|
spec.Linux.Resources.CPU.Cpus = ""
|
|
|
|
spec.Linux.Resources.CPU.Mems = ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if _, ok := m["pids"]; !ok {
|
|
|
|
spec.Linux.Resources.Pids = nil
|
|
|
|
}
|
|
|
|
if _, ok := m["io"]; !ok {
|
|
|
|
spec.Linux.Resources.BlockIO = nil
|
|
|
|
}
|
|
|
|
if _, ok := m["rdma"]; !ok {
|
|
|
|
spec.Linux.Resources.Rdma = nil
|
|
|
|
}
|
|
|
|
spec.Linux.Resources.HugepageLimits = nil
|
|
|
|
spec.Linux.Resources.Network = nil
|
|
|
|
}
|
|
|
|
}
|
2018-10-15 03:52:53 -04:00
|
|
|
|
|
|
|
if spec.Process.OOMScoreAdj != nil && *spec.Process.OOMScoreAdj < currentOOMScoreAdj {
|
|
|
|
*spec.Process.OOMScoreAdj = currentOOMScoreAdj
|
|
|
|
}
|
2021-01-19 00:12:43 -05:00
|
|
|
|
|
|
|
// Fix up /proc if --pid=host
|
|
|
|
pidHost, err := isPidHost(spec)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !pidHost {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return bindMountHostProcfs(spec)
|
|
|
|
}
|
|
|
|
|
|
|
|
func isPidHost(spec *specs.Spec) (bool, error) {
|
|
|
|
for _, ns := range spec.Linux.Namespaces {
|
|
|
|
if ns.Type == specs.PIDNamespace {
|
|
|
|
if ns.Path == "" {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
pidNS, err := os.Readlink(ns.Path)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
selfPidNS, err := os.Readlink("/proc/self/ns/pid")
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return pidNS == selfPidNS, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func bindMountHostProcfs(spec *specs.Spec) error {
|
|
|
|
// Replace procfs mount with rbind
|
|
|
|
// https://github.com/containers/podman/blob/v3.0.0-rc1/pkg/specgen/generate/oci.go#L248-L257
|
|
|
|
for i, m := range spec.Mounts {
|
|
|
|
if path.Clean(m.Destination) == "/proc" {
|
|
|
|
newM := specs.Mount{
|
|
|
|
Destination: "/proc",
|
|
|
|
Type: "bind",
|
|
|
|
Source: "/proc",
|
|
|
|
Options: []string{"rbind", "nosuid", "noexec", "nodev"},
|
|
|
|
}
|
|
|
|
spec.Mounts[i] = newM
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove ReadonlyPaths for /proc/*
|
|
|
|
newROP := spec.Linux.ReadonlyPaths[:0]
|
|
|
|
for _, s := range spec.Linux.ReadonlyPaths {
|
|
|
|
s = path.Clean(s)
|
|
|
|
if !strings.HasPrefix(s, "/proc/") {
|
|
|
|
newROP = append(newROP, s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
spec.Linux.ReadonlyPaths = newROP
|
|
|
|
|
2018-10-15 03:52:53 -04:00
|
|
|
return nil
|
|
|
|
}
|