2015-02-07 04:48:57 +08:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package libcontainer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"syscall"
|
|
|
|
|
2015-05-06 21:14:04 +08:00
|
|
|
"github.com/Sirupsen/logrus"
|
2015-06-22 10:29:59 +08:00
|
|
|
"github.com/opencontainers/runc/libcontainer/cgroups"
|
|
|
|
"github.com/opencontainers/runc/libcontainer/configs"
|
|
|
|
"github.com/opencontainers/runc/libcontainer/netlink"
|
|
|
|
"github.com/opencontainers/runc/libcontainer/seccomp"
|
|
|
|
"github.com/opencontainers/runc/libcontainer/system"
|
|
|
|
"github.com/opencontainers/runc/libcontainer/user"
|
|
|
|
"github.com/opencontainers/runc/libcontainer/utils"
|
2015-02-07 04:48:57 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type initType string
|
|
|
|
|
|
|
|
const (
|
2015-02-18 13:50:43 +08:00
|
|
|
initSetns initType = "setns"
|
|
|
|
initStandard initType = "standard"
|
2015-02-07 04:48:57 +08:00
|
|
|
)
|
|
|
|
|
2015-02-10 07:16:27 +08:00
|
|
|
type pid struct {
|
|
|
|
Pid int `json:"pid"`
|
|
|
|
}
|
|
|
|
|
2015-02-11 03:51:45 +08:00
|
|
|
// network is an internal struct used to setup container networks.
|
|
|
|
type network struct {
|
|
|
|
configs.Network
|
|
|
|
|
|
|
|
// TempVethPeerName is a unique tempory veth peer name that was placed into
|
|
|
|
// the container's namespace.
|
|
|
|
TempVethPeerName string `json:"temp_veth_peer_name"`
|
|
|
|
}
|
|
|
|
|
2015-02-14 08:06:17 +08:00
|
|
|
// initConfig is used for transferring parameters from Exec() to Init()
|
2015-02-07 04:48:57 +08:00
|
|
|
type initConfig struct {
|
2015-04-01 05:40:05 +08:00
|
|
|
Args []string `json:"args"`
|
|
|
|
Env []string `json:"env"`
|
|
|
|
Cwd string `json:"cwd"`
|
|
|
|
Capabilities []string `json:"capabilities"`
|
|
|
|
User string `json:"user"`
|
|
|
|
Config *configs.Config `json:"config"`
|
|
|
|
Console string `json:"console"`
|
|
|
|
Networks []*network `json:"network"`
|
|
|
|
PassedFilesCount int `json:"passed_files_count"`
|
2015-02-07 04:48:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type initer interface {
|
|
|
|
Init() error
|
|
|
|
}
|
|
|
|
|
|
|
|
func newContainerInit(t initType, pipe *os.File) (initer, error) {
|
|
|
|
var config *initConfig
|
|
|
|
if err := json.NewDecoder(pipe).Decode(&config); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-02-07 11:16:11 +08:00
|
|
|
if err := populateProcessEnvironment(config.Env); err != nil {
|
2015-02-07 04:48:57 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
switch t {
|
|
|
|
case initSetns:
|
|
|
|
return &linuxSetnsInit{
|
2015-02-10 05:11:57 +08:00
|
|
|
config: config,
|
2015-02-07 04:48:57 +08:00
|
|
|
}, nil
|
|
|
|
case initStandard:
|
|
|
|
return &linuxStandardInit{
|
2015-04-03 04:55:55 +08:00
|
|
|
parentPid: syscall.Getppid(),
|
|
|
|
config: config,
|
2015-02-07 04:48:57 +08:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("unknown init type %q", t)
|
|
|
|
}
|
|
|
|
|
|
|
|
// populateProcessEnvironment loads the provided environment variables into the
|
|
|
|
// current processes's environment.
|
|
|
|
func populateProcessEnvironment(env []string) error {
|
|
|
|
for _, pair := range env {
|
|
|
|
p := strings.SplitN(pair, "=", 2)
|
|
|
|
if len(p) < 2 {
|
|
|
|
return fmt.Errorf("invalid environment '%v'", pair)
|
|
|
|
}
|
|
|
|
if err := os.Setenv(p[0], p[1]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// finalizeNamespace drops the caps, sets the correct user
|
2015-02-14 08:06:17 +08:00
|
|
|
// and working dir, and closes any leaked file descriptors
|
2015-03-25 14:58:22 +08:00
|
|
|
// before executing the command inside the namespace
|
2015-02-10 05:11:57 +08:00
|
|
|
func finalizeNamespace(config *initConfig) error {
|
2015-04-01 05:40:05 +08:00
|
|
|
// Ensure that all unwanted fds we may have accidentally
|
2015-02-07 04:48:57 +08:00
|
|
|
// inherited are marked close-on-exec so they stay out of the
|
|
|
|
// container
|
2015-04-01 05:40:05 +08:00
|
|
|
if err := utils.CloseExecFrom(config.PassedFilesCount + 3); err != nil {
|
2015-02-07 04:48:57 +08:00
|
|
|
return err
|
|
|
|
}
|
2015-03-26 03:40:32 +08:00
|
|
|
|
|
|
|
capabilities := config.Config.Capabilities
|
|
|
|
if config.Capabilities != nil {
|
|
|
|
capabilities = config.Capabilities
|
|
|
|
}
|
|
|
|
w, err := newCapWhitelist(capabilities)
|
2015-02-10 07:38:28 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-02-07 04:48:57 +08:00
|
|
|
// drop capabilities in bounding set before changing user
|
2015-02-10 07:38:28 +08:00
|
|
|
if err := w.dropBoundingSet(); err != nil {
|
2015-02-07 04:48:57 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
// preserve existing capabilities while we change users
|
|
|
|
if err := system.SetKeepCaps(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := setupUser(config); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := system.ClearKeepCaps(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// drop all other capabilities
|
2015-02-10 07:38:28 +08:00
|
|
|
if err := w.drop(); err != nil {
|
2015-02-07 04:48:57 +08:00
|
|
|
return err
|
|
|
|
}
|
2015-02-10 05:11:57 +08:00
|
|
|
if config.Cwd != "" {
|
|
|
|
if err := syscall.Chdir(config.Cwd); err != nil {
|
|
|
|
return err
|
2015-02-07 04:48:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// joinExistingNamespaces gets all the namespace paths specified for the container and
|
|
|
|
// does a setns on the namespace fd so that the current process joins the namespace.
|
|
|
|
func joinExistingNamespaces(namespaces []configs.Namespace) error {
|
|
|
|
for _, ns := range namespaces {
|
|
|
|
if ns.Path != "" {
|
|
|
|
f, err := os.OpenFile(ns.Path, os.O_RDONLY, 0)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = system.Setns(f.Fd(), uintptr(ns.Syscall()))
|
|
|
|
f.Close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// setupUser changes the groups, gid, and uid for the user inside the container
|
2015-02-10 05:11:57 +08:00
|
|
|
func setupUser(config *initConfig) error {
|
2015-02-07 04:48:57 +08:00
|
|
|
// Set up defaults.
|
|
|
|
defaultExecUser := user.ExecUser{
|
|
|
|
Uid: syscall.Getuid(),
|
|
|
|
Gid: syscall.Getgid(),
|
|
|
|
Home: "/",
|
|
|
|
}
|
|
|
|
passwdPath, err := user.GetPasswdPath()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
groupPath, err := user.GetGroupPath()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
execUser, err := user.GetExecUserPath(config.User, &defaultExecUser, passwdPath, groupPath)
|
|
|
|
if err != nil {
|
2015-02-10 05:11:57 +08:00
|
|
|
return err
|
2015-02-07 04:48:57 +08:00
|
|
|
}
|
2015-05-01 07:02:31 +08:00
|
|
|
|
2015-06-13 00:40:42 +08:00
|
|
|
var addGroups []int
|
|
|
|
if len(config.Config.AdditionalGroups) > 0 {
|
|
|
|
addGroups, err = user.GetAdditionalGroupsPath(config.Config.AdditionalGroups, groupPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-01 07:02:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
suppGroups := append(execUser.Sgids, addGroups...)
|
2015-02-07 04:48:57 +08:00
|
|
|
if err := syscall.Setgroups(suppGroups); err != nil {
|
2015-02-10 05:11:57 +08:00
|
|
|
return err
|
2015-02-07 04:48:57 +08:00
|
|
|
}
|
2015-05-01 07:02:31 +08:00
|
|
|
|
2015-02-07 04:48:57 +08:00
|
|
|
if err := system.Setgid(execUser.Gid); err != nil {
|
2015-02-10 05:11:57 +08:00
|
|
|
return err
|
2015-02-07 04:48:57 +08:00
|
|
|
}
|
|
|
|
if err := system.Setuid(execUser.Uid); err != nil {
|
2015-02-10 05:11:57 +08:00
|
|
|
return err
|
2015-02-07 04:48:57 +08:00
|
|
|
}
|
|
|
|
// if we didn't get HOME already, set it based on the user's HOME
|
|
|
|
if envHome := os.Getenv("HOME"); envHome == "" {
|
|
|
|
if err := os.Setenv("HOME", execUser.Home); err != nil {
|
2015-02-10 05:11:57 +08:00
|
|
|
return err
|
2015-02-07 04:48:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-02-11 03:51:45 +08:00
|
|
|
// setupNetwork sets up and initializes any network interface inside the container.
|
|
|
|
func setupNetwork(config *initConfig) error {
|
2015-02-07 04:48:57 +08:00
|
|
|
for _, config := range config.Networks {
|
2015-02-10 07:16:27 +08:00
|
|
|
strategy, err := getStrategy(config.Type)
|
2015-02-07 04:48:57 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-02-11 03:51:45 +08:00
|
|
|
if err := strategy.initialize(config); err != nil {
|
|
|
|
return err
|
2015-02-07 04:48:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupRoute(config *configs.Config) error {
|
|
|
|
for _, config := range config.Routes {
|
|
|
|
if err := netlink.AddRoute(config.Destination, config.Source, config.Gateway, config.InterfaceName); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupRlimits(config *configs.Config) error {
|
|
|
|
for _, rlimit := range config.Rlimits {
|
|
|
|
l := &syscall.Rlimit{Max: rlimit.Hard, Cur: rlimit.Soft}
|
|
|
|
if err := syscall.Setrlimit(rlimit.Type, l); err != nil {
|
|
|
|
return fmt.Errorf("error setting rlimit type %v: %v", rlimit.Type, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2015-02-12 08:45:23 +08:00
|
|
|
|
2015-02-14 08:06:17 +08:00
|
|
|
// killCgroupProcesses freezes then iterates over all the processes inside the
|
2015-02-12 08:45:23 +08:00
|
|
|
// manager's cgroups sending a SIGKILL to each process then waiting for them to
|
|
|
|
// exit.
|
|
|
|
func killCgroupProcesses(m cgroups.Manager) error {
|
|
|
|
var procs []*os.Process
|
|
|
|
if err := m.Freeze(configs.Frozen); err != nil {
|
2015-05-06 21:14:04 +08:00
|
|
|
logrus.Warn(err)
|
2015-02-12 08:45:23 +08:00
|
|
|
}
|
|
|
|
pids, err := m.GetPids()
|
|
|
|
if err != nil {
|
|
|
|
m.Freeze(configs.Thawed)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, pid := range pids {
|
|
|
|
if p, err := os.FindProcess(pid); err == nil {
|
|
|
|
procs = append(procs, p)
|
|
|
|
if err := p.Kill(); err != nil {
|
2015-05-06 21:14:04 +08:00
|
|
|
logrus.Warn(err)
|
2015-02-12 08:45:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := m.Freeze(configs.Thawed); err != nil {
|
2015-05-06 21:14:04 +08:00
|
|
|
logrus.Warn(err)
|
2015-02-12 08:45:23 +08:00
|
|
|
}
|
|
|
|
for _, p := range procs {
|
|
|
|
if _, err := p.Wait(); err != nil {
|
2015-05-06 21:14:04 +08:00
|
|
|
logrus.Warn(err)
|
2015-02-12 08:45:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2015-04-16 08:39:12 +08:00
|
|
|
|
|
|
|
func finalizeSeccomp(config *initConfig) error {
|
2015-05-30 06:24:18 +08:00
|
|
|
if config.Config.Seccomp == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
context := seccomp.New()
|
|
|
|
for _, s := range config.Config.Seccomp.Syscalls {
|
|
|
|
ss := &seccomp.Syscall{
|
|
|
|
Value: uint32(s.Value),
|
|
|
|
Action: seccompAction(s.Action),
|
|
|
|
}
|
|
|
|
if len(s.Args) > 0 {
|
|
|
|
ss.Args = seccompArgs(s.Args)
|
2015-04-16 08:39:12 +08:00
|
|
|
}
|
2015-05-30 06:24:18 +08:00
|
|
|
context.Add(ss)
|
2015-04-16 08:39:12 +08:00
|
|
|
}
|
2015-05-30 06:24:18 +08:00
|
|
|
return context.Load()
|
|
|
|
}
|
2015-05-23 07:10:20 +08:00
|
|
|
|
2015-05-30 06:24:18 +08:00
|
|
|
func seccompAction(a configs.Action) seccomp.Action {
|
|
|
|
switch a {
|
|
|
|
case configs.Kill:
|
|
|
|
return seccomp.Kill
|
|
|
|
case configs.Trap:
|
|
|
|
return seccomp.Trap
|
|
|
|
case configs.Allow:
|
|
|
|
return seccomp.Allow
|
|
|
|
}
|
|
|
|
return seccomp.Error(syscall.Errno(int(a)))
|
|
|
|
}
|
|
|
|
|
|
|
|
func seccompArgs(args []*configs.Arg) seccomp.Args {
|
|
|
|
var sa []seccomp.Arg
|
|
|
|
for _, a := range args {
|
|
|
|
sa = append(sa, seccomp.Arg{
|
|
|
|
Index: uint32(a.Index),
|
|
|
|
Op: seccompOperator(a.Op),
|
|
|
|
Value: uint(a.Value),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return seccomp.Args{sa}
|
|
|
|
}
|
|
|
|
|
|
|
|
func seccompOperator(o configs.Operator) seccomp.Operator {
|
|
|
|
switch o {
|
|
|
|
case configs.EqualTo:
|
|
|
|
return seccomp.EqualTo
|
|
|
|
case configs.NotEqualTo:
|
|
|
|
return seccomp.NotEqualTo
|
|
|
|
case configs.GreatherThan:
|
|
|
|
return seccomp.GreatherThan
|
|
|
|
case configs.LessThan:
|
|
|
|
return seccomp.LessThan
|
|
|
|
case configs.MaskEqualTo:
|
|
|
|
return seccomp.MaskEqualTo
|
|
|
|
}
|
|
|
|
return 0
|
2015-04-16 08:39:12 +08:00
|
|
|
}
|