2015-01-26 19:05:29 +08:00
|
|
|
package nsenter
|
|
|
|
|
|
|
|
import (
|
2015-10-17 23:35:36 +08:00
|
|
|
"bytes"
|
2015-01-26 19:05:29 +08:00
|
|
|
"encoding/json"
|
2015-09-14 08:40:43 +08:00
|
|
|
"fmt"
|
2015-10-17 23:35:36 +08:00
|
|
|
"io"
|
2015-09-14 08:40:43 +08:00
|
|
|
"io/ioutil"
|
2015-01-26 19:05:29 +08:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
2015-10-17 23:35:36 +08:00
|
|
|
"syscall"
|
2015-01-26 19:05:29 +08:00
|
|
|
"testing"
|
2015-10-17 23:35:36 +08:00
|
|
|
|
|
|
|
"github.com/opencontainers/runc/libcontainer"
|
|
|
|
"github.com/vishvananda/netlink/nl"
|
2015-01-26 19:05:29 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type pid struct {
|
|
|
|
Pid int `json:"Pid"`
|
|
|
|
}
|
|
|
|
|
2015-09-14 08:40:43 +08:00
|
|
|
func TestNsenterValidPaths(t *testing.T) {
|
2015-01-26 19:05:29 +08:00
|
|
|
args := []string{"nsenter-exec"}
|
2015-10-17 23:35:36 +08:00
|
|
|
parent, child, err := newPipe()
|
2015-01-26 19:05:29 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to create pipe %v", err)
|
|
|
|
}
|
|
|
|
|
2015-09-14 08:40:43 +08:00
|
|
|
namespaces := []string{
|
|
|
|
// join pid ns of the current process
|
|
|
|
fmt.Sprintf("/proc/%d/ns/pid", os.Getpid()),
|
|
|
|
}
|
2015-01-26 19:05:29 +08:00
|
|
|
cmd := &exec.Cmd{
|
|
|
|
Path: os.Args[0],
|
|
|
|
Args: args,
|
2015-10-17 23:35:36 +08:00
|
|
|
ExtraFiles: []*os.File{child},
|
2015-09-14 08:40:43 +08:00
|
|
|
Env: []string{"_LIBCONTAINER_INITPIPE=3"},
|
|
|
|
Stdout: os.Stdout,
|
|
|
|
Stderr: os.Stderr,
|
2015-01-26 19:05:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := cmd.Start(); err != nil {
|
|
|
|
t.Fatalf("nsenter failed to start %v", err)
|
|
|
|
}
|
2015-09-14 08:40:43 +08:00
|
|
|
// write cloneFlags
|
2015-10-17 23:35:36 +08:00
|
|
|
r := nl.NewNetlinkRequest(int(libcontainer.InitMsg), 0)
|
|
|
|
r.AddData(&libcontainer.Int32msg{
|
2015-09-14 08:40:43 +08:00
|
|
|
Type: libcontainer.CloneFlagsAttr,
|
|
|
|
Value: uint32(syscall.CLONE_NEWNET),
|
|
|
|
})
|
|
|
|
r.AddData(&libcontainer.Bytemsg{
|
|
|
|
Type: libcontainer.NsPathsAttr,
|
|
|
|
Value: []byte(strings.Join(namespaces, ",")),
|
2015-10-17 23:35:36 +08:00
|
|
|
})
|
|
|
|
if _, err := io.Copy(parent, bytes.NewReader(r.Serialize())); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-09-14 08:40:43 +08:00
|
|
|
|
2015-10-17 23:35:36 +08:00
|
|
|
decoder := json.NewDecoder(parent)
|
2015-01-26 19:05:29 +08:00
|
|
|
var pid *pid
|
|
|
|
|
|
|
|
if err := decoder.Decode(&pid); err != nil {
|
2015-09-14 08:40:43 +08:00
|
|
|
dir, _ := ioutil.ReadDir(fmt.Sprintf("/proc/%d/ns", os.Getpid()))
|
|
|
|
for _, d := range dir {
|
|
|
|
t.Log(d.Name())
|
|
|
|
}
|
2015-01-26 19:05:29 +08:00
|
|
|
t.Fatalf("%v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := cmd.Wait(); err != nil {
|
|
|
|
t.Fatalf("nsenter exits with a non-zero exit status")
|
|
|
|
}
|
|
|
|
p, err := os.FindProcess(pid.Pid)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%v", err)
|
|
|
|
}
|
|
|
|
p.Wait()
|
|
|
|
}
|
|
|
|
|
2015-09-14 08:40:43 +08:00
|
|
|
func TestNsenterInvalidPaths(t *testing.T) {
|
2015-01-26 19:05:29 +08:00
|
|
|
args := []string{"nsenter-exec"}
|
2015-10-17 23:35:36 +08:00
|
|
|
parent, child, err := newPipe()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to create pipe %v", err)
|
|
|
|
}
|
2015-01-26 19:05:29 +08:00
|
|
|
|
2015-09-14 08:40:43 +08:00
|
|
|
namespaces := []string{
|
|
|
|
// join pid ns of the current process
|
|
|
|
fmt.Sprintf("/proc/%d/ns/pid", -1),
|
2015-10-17 23:35:36 +08:00
|
|
|
}
|
2015-01-26 19:05:29 +08:00
|
|
|
cmd := &exec.Cmd{
|
2015-10-17 23:35:36 +08:00
|
|
|
Path: os.Args[0],
|
|
|
|
Args: args,
|
|
|
|
ExtraFiles: []*os.File{child},
|
2015-09-14 08:40:43 +08:00
|
|
|
Env: []string{"_LIBCONTAINER_INITPIPE=3"},
|
2015-01-26 19:05:29 +08:00
|
|
|
}
|
|
|
|
|
2015-10-17 23:35:36 +08:00
|
|
|
if err := cmd.Start(); err != nil {
|
2015-09-14 08:40:43 +08:00
|
|
|
t.Fatal(err)
|
2015-10-17 23:35:36 +08:00
|
|
|
}
|
2015-09-14 08:40:43 +08:00
|
|
|
// write cloneFlags
|
2015-10-17 23:35:36 +08:00
|
|
|
r := nl.NewNetlinkRequest(int(libcontainer.InitMsg), 0)
|
|
|
|
r.AddData(&libcontainer.Int32msg{
|
2015-09-14 08:40:43 +08:00
|
|
|
Type: libcontainer.CloneFlagsAttr,
|
|
|
|
Value: uint32(syscall.CLONE_NEWNET),
|
|
|
|
})
|
|
|
|
r.AddData(&libcontainer.Bytemsg{
|
|
|
|
Type: libcontainer.NsPathsAttr,
|
|
|
|
Value: []byte(strings.Join(namespaces, ",")),
|
2015-10-17 23:35:36 +08:00
|
|
|
})
|
|
|
|
if _, err := io.Copy(parent, bytes.NewReader(r.Serialize())); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := cmd.Wait(); err == nil {
|
2015-09-14 08:40:43 +08:00
|
|
|
t.Fatalf("nsenter exits with a zero exit status")
|
2015-01-26 19:05:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
if strings.HasPrefix(os.Args[0], "nsenter-") {
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2015-10-17 23:35:36 +08:00
|
|
|
|
|
|
|
func newPipe() (parent *os.File, child *os.File, err error) {
|
|
|
|
fds, err := syscall.Socketpair(syscall.AF_LOCAL, syscall.SOCK_STREAM|syscall.SOCK_CLOEXEC, 0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return os.NewFile(uintptr(fds[1]), "parent"), os.NewFile(uintptr(fds[0]), "child"), nil
|
|
|
|
}
|