2014-02-20 08:50:10 +08:00
|
|
|
// +build linux
|
|
|
|
|
2014-06-05 06:47:57 +08:00
|
|
|
package namespaces
|
2014-02-19 09:52:06 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-03-04 13:46:49 +08:00
|
|
|
"os"
|
2014-03-27 16:00:18 +08:00
|
|
|
"runtime"
|
2014-05-01 08:55:15 +08:00
|
|
|
"strings"
|
2014-03-04 13:46:49 +08:00
|
|
|
"syscall"
|
|
|
|
|
2014-06-10 23:14:16 +08:00
|
|
|
"github.com/docker/libcontainer"
|
|
|
|
"github.com/docker/libcontainer/apparmor"
|
|
|
|
"github.com/docker/libcontainer/console"
|
|
|
|
"github.com/docker/libcontainer/label"
|
|
|
|
"github.com/docker/libcontainer/mount"
|
|
|
|
"github.com/docker/libcontainer/netlink"
|
|
|
|
"github.com/docker/libcontainer/network"
|
|
|
|
"github.com/docker/libcontainer/security/capabilities"
|
|
|
|
"github.com/docker/libcontainer/security/restrict"
|
|
|
|
"github.com/docker/libcontainer/utils"
|
2014-02-19 15:13:36 +08:00
|
|
|
"github.com/dotcloud/docker/pkg/system"
|
2014-02-25 05:52:56 +08:00
|
|
|
"github.com/dotcloud/docker/pkg/user"
|
2014-02-19 09:52:06 +08:00
|
|
|
)
|
|
|
|
|
2014-06-20 07:36:39 +08:00
|
|
|
// TODO(vishh): This is part of the libcontainer API and it does much more than just namespaces related work.
|
|
|
|
// Move this to libcontainer package.
|
2014-02-21 10:27:42 +08:00
|
|
|
// Init is the init process that first runs inside a new namespace to setup mounts, users, networking,
|
|
|
|
// and other options required for the new container.
|
2014-05-01 08:55:15 +08:00
|
|
|
func Init(container *libcontainer.Container, uncleanRootfs, consolePath string, syncPipe *SyncPipe, args []string) error {
|
2014-02-25 13:11:52 +08:00
|
|
|
rootfs, err := utils.ResolveRootfs(uncleanRootfs)
|
2014-02-20 06:33:25 +08:00
|
|
|
if err != nil {
|
2014-02-20 08:40:36 +08:00
|
|
|
return err
|
2014-02-20 06:33:25 +08:00
|
|
|
}
|
|
|
|
|
2014-05-01 08:55:15 +08:00
|
|
|
// clear the current processes env and replace it with the environment
|
|
|
|
// defined on the container
|
|
|
|
if err := LoadContainerEnvironment(container); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-02-21 06:12:08 +08:00
|
|
|
// We always read this as it is a way to sync with the parent as well
|
2014-02-22 14:58:30 +08:00
|
|
|
context, err := syncPipe.ReadFromParent()
|
2014-02-21 06:12:08 +08:00
|
|
|
if err != nil {
|
2014-02-22 14:58:30 +08:00
|
|
|
syncPipe.Close()
|
2014-02-21 06:12:08 +08:00
|
|
|
return err
|
2014-02-20 07:33:44 +08:00
|
|
|
}
|
2014-02-22 14:58:30 +08:00
|
|
|
syncPipe.Close()
|
|
|
|
|
2014-04-11 23:15:28 +08:00
|
|
|
if consolePath != "" {
|
|
|
|
if err := console.OpenAndDup(consolePath); err != nil {
|
|
|
|
return err
|
2014-02-21 10:05:40 +08:00
|
|
|
}
|
2014-02-19 09:52:06 +08:00
|
|
|
}
|
2014-02-19 15:13:36 +08:00
|
|
|
if _, err := system.Setsid(); err != nil {
|
2014-02-20 08:40:36 +08:00
|
|
|
return fmt.Errorf("setsid %s", err)
|
2014-02-19 09:52:06 +08:00
|
|
|
}
|
2014-04-11 23:15:28 +08:00
|
|
|
if consolePath != "" {
|
2014-02-21 10:05:40 +08:00
|
|
|
if err := system.Setctty(); err != nil {
|
|
|
|
return fmt.Errorf("setctty %s", err)
|
|
|
|
}
|
2014-02-19 09:52:06 +08:00
|
|
|
}
|
2014-03-04 13:46:49 +08:00
|
|
|
if err := setupNetwork(container, context); err != nil {
|
|
|
|
return fmt.Errorf("setup networking %s", err)
|
|
|
|
}
|
2014-05-17 15:06:29 +08:00
|
|
|
if err := setupRoute(container); err != nil {
|
|
|
|
return fmt.Errorf("setup route %s", err)
|
|
|
|
}
|
2014-04-01 22:03:29 +08:00
|
|
|
|
|
|
|
label.Init()
|
2014-05-01 08:18:07 +08:00
|
|
|
|
2014-06-24 02:09:36 +08:00
|
|
|
if err := mount.InitializeMountNamespace(rootfs, consolePath, libcontainer.GetInternalMountConfig(container)); err != nil {
|
2014-02-20 08:40:36 +08:00
|
|
|
return fmt.Errorf("setup mount namespace %s", err)
|
2014-02-19 09:52:06 +08:00
|
|
|
}
|
2014-05-06 02:12:25 +08:00
|
|
|
if container.Hostname != "" {
|
|
|
|
if err := system.Sethostname(container.Hostname); err != nil {
|
|
|
|
return fmt.Errorf("sethostname %s", err)
|
|
|
|
}
|
2014-02-19 09:52:06 +08:00
|
|
|
}
|
|
|
|
|
2014-03-19 04:49:16 +08:00
|
|
|
runtime.LockOSThread()
|
2014-04-18 07:47:27 +08:00
|
|
|
|
2014-05-02 10:09:12 +08:00
|
|
|
if err := apparmor.ApplyProfile(container.Context["apparmor_profile"]); err != nil {
|
|
|
|
return fmt.Errorf("set apparmor profile %s: %s", container.Context["apparmor_profile"], err)
|
|
|
|
}
|
|
|
|
if err := label.SetProcessLabel(container.Context["process_label"]); err != nil {
|
|
|
|
return fmt.Errorf("set process label %s", err)
|
|
|
|
}
|
2014-05-02 01:08:18 +08:00
|
|
|
if container.Context["restrictions"] != "" {
|
2014-05-19 15:51:20 +08:00
|
|
|
if err := restrict.Restrict("proc/sys", "proc/sysrq-trigger", "proc/irq", "proc/bus", "sys"); err != nil {
|
2014-05-01 09:00:42 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2014-05-14 17:49:06 +08:00
|
|
|
|
|
|
|
pdeathSignal, err := system.GetParentDeathSignal()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("get parent death signal %s", err)
|
|
|
|
}
|
|
|
|
|
2014-05-01 09:00:42 +08:00
|
|
|
if err := FinalizeNamespace(container); err != nil {
|
|
|
|
return fmt.Errorf("finalize namespace %s", err)
|
|
|
|
}
|
2014-05-14 17:49:06 +08:00
|
|
|
|
|
|
|
// FinalizeNamespace can change user/group which clears the parent death
|
|
|
|
// signal, so we restore it here.
|
|
|
|
if err := RestoreParentDeathSignal(pdeathSignal); err != nil {
|
|
|
|
return fmt.Errorf("restore parent death signal %s", err)
|
|
|
|
}
|
|
|
|
|
2014-03-06 03:59:31 +08:00
|
|
|
return system.Execv(args[0], args[0:], container.Env)
|
2014-02-22 14:58:30 +08:00
|
|
|
}
|
|
|
|
|
2014-05-14 17:49:06 +08:00
|
|
|
// RestoreParentDeathSignal sets the parent death signal to old.
|
|
|
|
func RestoreParentDeathSignal(old int) error {
|
|
|
|
if old == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
current, err := system.GetParentDeathSignal()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("get parent death signal %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if old == current {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := system.ParentDeathSignal(uintptr(old)); err != nil {
|
|
|
|
return fmt.Errorf("set parent death signal %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Signal self if parent is already dead. Does nothing if running in a new
|
|
|
|
// PID namespace, as Getppid will always return 0.
|
|
|
|
if syscall.Getppid() == 1 {
|
2014-05-28 04:38:24 +08:00
|
|
|
return syscall.Kill(syscall.Getpid(), syscall.SIGKILL)
|
2014-05-14 17:49:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-05-01 06:27:59 +08:00
|
|
|
// SetupUser changes the groups, gid, and uid for the user inside the container
|
|
|
|
func SetupUser(u string) error {
|
|
|
|
uid, gid, suppGids, err := user.GetUserGroupSupplementary(u, syscall.Getuid(), syscall.Getgid())
|
2014-04-29 06:46:03 +08:00
|
|
|
if err != nil {
|
2014-05-01 06:27:59 +08:00
|
|
|
return fmt.Errorf("get supplementary groups %s", err)
|
2014-04-29 06:46:03 +08:00
|
|
|
}
|
|
|
|
if err := system.Setgroups(suppGids); err != nil {
|
|
|
|
return fmt.Errorf("setgroups %s", err)
|
|
|
|
}
|
|
|
|
if err := system.Setgid(gid); err != nil {
|
|
|
|
return fmt.Errorf("setgid %s", err)
|
|
|
|
}
|
|
|
|
if err := system.Setuid(uid); err != nil {
|
|
|
|
return fmt.Errorf("setuid %s", err)
|
2014-02-19 09:52:06 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-02-20 14:43:40 +08:00
|
|
|
// setupVethNetwork uses the Network config if it is not nil to initialize
|
|
|
|
// the new veth interface inside the container for use by changing the name to eth0
|
|
|
|
// setting the MTU and IP address along with the default gateway
|
2014-06-20 07:36:39 +08:00
|
|
|
func setupNetwork(container *libcontainer.Container, context map[string]string) error {
|
2014-02-27 06:19:39 +08:00
|
|
|
for _, config := range container.Networks {
|
2014-02-22 14:20:15 +08:00
|
|
|
strategy, err := network.GetStrategy(config.Type)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-02-20 06:55:34 +08:00
|
|
|
}
|
2014-03-16 08:01:31 +08:00
|
|
|
|
2014-06-20 07:36:39 +08:00
|
|
|
err1 := strategy.Initialize(libcontainer.GetInternalNetworkSpec(config), context)
|
2014-03-16 08:01:31 +08:00
|
|
|
if err1 != nil {
|
|
|
|
return err1
|
|
|
|
}
|
2014-02-20 02:44:29 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2014-03-04 04:15:47 +08:00
|
|
|
|
2014-05-17 15:06:29 +08:00
|
|
|
func setupRoute(container *libcontainer.Container) error {
|
|
|
|
for _, config := range container.Routes {
|
|
|
|
if err := netlink.AddRoute(config.Destination, config.Source, config.Gateway, config.InterfaceName); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-05-01 08:18:07 +08:00
|
|
|
// FinalizeNamespace drops the caps, sets the correct user
|
2014-04-29 13:22:54 +08:00
|
|
|
// and working dir, and closes any leaky file descriptors
|
|
|
|
// before execing the command inside the namespace
|
2014-05-01 08:18:07 +08:00
|
|
|
func FinalizeNamespace(container *libcontainer.Container) error {
|
2014-06-13 21:56:58 +08:00
|
|
|
// Ensure that all non-standard fds we may have accidentally
|
|
|
|
// inherited are marked close-on-exec so they stay out of the
|
|
|
|
// container
|
|
|
|
if err := utils.CloseExecFrom(3); err != nil {
|
2014-04-29 13:22:54 +08:00
|
|
|
return fmt.Errorf("close open file descriptors %s", err)
|
|
|
|
}
|
2014-05-28 22:41:48 +08:00
|
|
|
|
|
|
|
// drop capabilities in bounding set before changing user
|
2014-06-20 07:36:39 +08:00
|
|
|
if err := capabilities.DropBoundingSet(&container.Capabilities); err != nil {
|
2014-05-28 22:41:48 +08:00
|
|
|
return fmt.Errorf("drop bounding set %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// preserve existing capabilities while we change users
|
|
|
|
if err := system.SetKeepCaps(); err != nil {
|
|
|
|
return fmt.Errorf("set keep caps %s", err)
|
|
|
|
}
|
|
|
|
|
2014-05-01 06:27:59 +08:00
|
|
|
if err := SetupUser(container.User); err != nil {
|
2014-03-04 04:15:47 +08:00
|
|
|
return fmt.Errorf("setup user %s", err)
|
|
|
|
}
|
2014-05-28 22:41:48 +08:00
|
|
|
|
|
|
|
if err := system.ClearKeepCaps(); err != nil {
|
|
|
|
return fmt.Errorf("clear keep caps %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// drop all other capabilities
|
2014-06-20 07:36:39 +08:00
|
|
|
if err := capabilities.DropCapabilities(&container.Capabilities); err != nil {
|
2014-05-28 22:41:48 +08:00
|
|
|
return fmt.Errorf("drop capabilities %s", err)
|
|
|
|
}
|
|
|
|
|
2014-03-04 04:15:47 +08:00
|
|
|
if container.WorkingDir != "" {
|
|
|
|
if err := system.Chdir(container.WorkingDir); err != nil {
|
|
|
|
return fmt.Errorf("chdir to %s %s", container.WorkingDir, err)
|
|
|
|
}
|
|
|
|
}
|
2014-06-13 21:56:58 +08:00
|
|
|
|
2014-03-04 04:15:47 +08:00
|
|
|
return nil
|
|
|
|
}
|
2014-05-01 08:55:15 +08:00
|
|
|
|
|
|
|
func LoadContainerEnvironment(container *libcontainer.Container) error {
|
|
|
|
os.Clearenv()
|
|
|
|
for _, pair := range container.Env {
|
|
|
|
p := strings.SplitN(pair, "=", 2)
|
2014-05-16 12:36:15 +08:00
|
|
|
if len(p) < 2 {
|
|
|
|
return fmt.Errorf("invalid environment '%v'", pair)
|
|
|
|
}
|
2014-05-01 08:55:15 +08:00
|
|
|
if err := os.Setenv(p[0], p[1]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|