2020-02-22 18:39:33 +00:00
|
|
|
// +build !windows
|
|
|
|
|
2019-03-08 22:47:44 +00:00
|
|
|
package rootless
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
2021-03-02 08:57:40 +00:00
|
|
|
"strconv"
|
2019-03-08 22:47:44 +00:00
|
|
|
"strings"
|
|
|
|
|
2021-03-02 08:57:40 +00:00
|
|
|
"github.com/opencontainers/runc/libcontainer/cgroups"
|
2019-03-08 22:47:44 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/rootless-containers/rootlesskit/pkg/child"
|
|
|
|
"github.com/rootless-containers/rootlesskit/pkg/copyup/tmpfssymlink"
|
|
|
|
"github.com/rootless-containers/rootlesskit/pkg/network/slirp4netns"
|
|
|
|
"github.com/rootless-containers/rootlesskit/pkg/parent"
|
2019-05-29 06:01:38 +00:00
|
|
|
portbuiltin "github.com/rootless-containers/rootlesskit/pkg/port/builtin"
|
2019-03-08 22:47:44 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2021-03-02 08:57:40 +00:00
|
|
|
"golang.org/x/sys/unix"
|
2019-03-08 22:47:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-03-02 08:57:40 +00:00
|
|
|
pipeFD = "_K3S_ROOTLESS_FD"
|
|
|
|
childEnv = "_K3S_ROOTLESS_SOCK"
|
|
|
|
evacuateCgroup2Env = "_K3S_ROOTLESS_EVACUATE_CGROUP2" // boolean
|
|
|
|
Sock = ""
|
2019-03-08 22:47:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func Rootless(stateDir string) error {
|
|
|
|
defer func() {
|
|
|
|
os.Unsetenv(pipeFD)
|
|
|
|
os.Unsetenv(childEnv)
|
|
|
|
}()
|
|
|
|
|
|
|
|
hasFD := os.Getenv(pipeFD) != ""
|
|
|
|
hasChildEnv := os.Getenv(childEnv) != ""
|
|
|
|
|
|
|
|
if hasFD {
|
|
|
|
logrus.Debug("Running rootless child")
|
|
|
|
childOpt, err := createChildOpt()
|
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := child.Child(*childOpt); err != nil {
|
2020-08-18 23:44:10 +00:00
|
|
|
logrus.Fatalf("child died: %v", err)
|
2019-03-08 22:47:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if hasChildEnv {
|
|
|
|
Sock = os.Getenv(childEnv)
|
|
|
|
logrus.Debug("Running rootless process")
|
|
|
|
return setupMounts(stateDir)
|
|
|
|
}
|
|
|
|
|
|
|
|
logrus.Debug("Running rootless parent")
|
2020-11-24 08:43:48 +00:00
|
|
|
if err := validateSysctl(); err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
2019-03-08 22:47:44 +00:00
|
|
|
parentOpt, err := createParentOpt(filepath.Join(stateDir, "rootless"))
|
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
os.Setenv(childEnv, filepath.Join(parentOpt.StateDir, parent.StateFileAPISock))
|
2021-03-02 08:57:40 +00:00
|
|
|
if parentOpt.EvacuateCgroup2 != "" {
|
|
|
|
os.Setenv(evacuateCgroup2Env, "1")
|
|
|
|
}
|
2019-03-08 22:47:44 +00:00
|
|
|
if err := parent.Parent(*parentOpt); err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
os.Exit(0)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-24 08:43:48 +00:00
|
|
|
func validateSysctl() error {
|
|
|
|
expected := map[string]string{
|
|
|
|
// kernel.unprivileged_userns_clone needs to be 1 to allow userns on some distros.
|
|
|
|
"kernel.unprivileged_userns_clone": "1",
|
|
|
|
|
|
|
|
// net.ipv4.ip_forward should not need to be 1 in the parent namespace.
|
|
|
|
// However, the current k3s implementation has a bug that requires net.ipv4.ip_forward=1
|
|
|
|
// https://github.com/rancher/k3s/issues/2420#issuecomment-715051120
|
|
|
|
"net.ipv4.ip_forward": "1",
|
|
|
|
}
|
|
|
|
for key, expectedValue := range expected {
|
|
|
|
if actualValue, err := readSysctl(key); err == nil {
|
|
|
|
if expectedValue != actualValue {
|
|
|
|
return errors.Errorf("expected sysctl value %q to be %q, got %q; try adding \"%s=%s\" to /etc/sysctl.conf and running `sudo sysctl --system`",
|
|
|
|
key, expectedValue, actualValue, key, expectedValue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func readSysctl(key string) (string, error) {
|
|
|
|
p := "/proc/sys/" + strings.ReplaceAll(key, ".", "/")
|
|
|
|
b, err := ioutil.ReadFile(p)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return strings.TrimSpace(string(b)), nil
|
|
|
|
}
|
|
|
|
|
2019-03-08 22:47:44 +00:00
|
|
|
func parseCIDR(s string) (*net.IPNet, error) {
|
|
|
|
if s == "" {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
ip, ipnet, err := net.ParseCIDR(s)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !ip.Equal(ipnet.IP) {
|
|
|
|
return nil, errors.Errorf("cidr must be like 10.0.2.0/24, not like 10.0.2.100/24")
|
|
|
|
}
|
|
|
|
return ipnet, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func createParentOpt(stateDir string) (*parent.Opt, error) {
|
|
|
|
if err := os.MkdirAll(stateDir, 0755); err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed to mkdir %s", stateDir)
|
|
|
|
}
|
|
|
|
|
|
|
|
stateDir, err := ioutil.TempDir("", "rootless")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
opt := &parent.Opt{
|
2021-03-02 08:57:40 +00:00
|
|
|
StateDir: stateDir,
|
|
|
|
CreatePIDNS: true,
|
|
|
|
CreateCgroupNS: true,
|
|
|
|
CreateUTSNS: true,
|
|
|
|
CreateIPCNS: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
selfCgroupMap, err := cgroups.ParseCgroupFile("/proc/self/cgroup")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if selfCgroup2 := selfCgroupMap[""]; selfCgroup2 == "" {
|
|
|
|
logrus.Warnf("enabling cgroup2 is highly recommended, see https://rootlesscontaine.rs/getting-started/common/cgroup2/")
|
|
|
|
} else {
|
|
|
|
selfCgroup2Dir := filepath.Join("/sys/fs/cgroup", selfCgroup2)
|
|
|
|
if unix.Access(selfCgroup2Dir, unix.W_OK) == nil {
|
|
|
|
opt.EvacuateCgroup2 = "k3s_evac"
|
|
|
|
} else {
|
|
|
|
logrus.Warn("cannot set cgroup2 evacuation, make sure to run k3s as a systemd unit")
|
|
|
|
}
|
2019-03-08 22:47:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mtu := 0
|
|
|
|
ipnet, err := parseCIDR("10.41.0.0/16")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
disableHostLoopback := true
|
|
|
|
binary := "slirp4netns"
|
|
|
|
if _, err := exec.LookPath(binary); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-08-05 09:11:24 +00:00
|
|
|
debugWriter := &logrusDebugWriter{}
|
2021-03-02 08:13:01 +00:00
|
|
|
opt.NetworkDriver, err = slirp4netns.NewParentDriver(debugWriter, binary, mtu, ipnet, "tap0", disableHostLoopback, "", false, false, false)
|
2020-08-05 09:11:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
opt.PortDriver, err = portbuiltin.NewParentDriver(debugWriter, stateDir)
|
2019-03-08 22:47:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
opt.PipeFDEnvKey = pipeFD
|
|
|
|
|
|
|
|
return opt, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type logrusDebugWriter struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *logrusDebugWriter) Write(p []byte) (int, error) {
|
|
|
|
s := strings.TrimSuffix(string(p), "\n")
|
|
|
|
logrus.Debug(s)
|
|
|
|
return len(p), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func createChildOpt() (*child.Opt, error) {
|
|
|
|
opt := &child.Opt{}
|
|
|
|
opt.TargetCmd = os.Args
|
|
|
|
opt.PipeFDEnvKey = pipeFD
|
|
|
|
opt.NetworkDriver = slirp4netns.NewChildDriver()
|
2019-05-29 06:01:38 +00:00
|
|
|
opt.PortDriver = portbuiltin.NewChildDriver(&logrusDebugWriter{})
|
2021-01-22 13:56:27 +00:00
|
|
|
opt.CopyUpDirs = []string{"/etc", "/var/run", "/run", "/var/lib"}
|
2019-03-08 22:47:44 +00:00
|
|
|
opt.CopyUpDriver = tmpfssymlink.NewChildDriver()
|
2020-01-22 17:53:24 +00:00
|
|
|
opt.MountProcfs = true
|
|
|
|
opt.Reaper = true
|
2021-03-02 08:57:40 +00:00
|
|
|
if v := os.Getenv(evacuateCgroup2Env); v != "" {
|
|
|
|
var err error
|
|
|
|
opt.EvacuateCgroup2, err = strconv.ParseBool(v)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2019-03-08 22:47:44 +00:00
|
|
|
return opt, nil
|
|
|
|
}
|