From cdff060b635a5c17659abaaa676fc41d50b922b2 Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Fri, 8 Aug 2014 11:11:23 -0700 Subject: [PATCH] Nsenter checks for argv 0 before running Signed-off-by: Michael Crosby --- namespaces/execin.go | 17 +++++++++++++---- namespaces/nsenter/nsenter.c | 26 +++++--------------------- 2 files changed, 18 insertions(+), 25 deletions(-) diff --git a/namespaces/execin.go b/namespaces/execin.go index 054d3685..2ac9dba7 100644 --- a/namespaces/execin.go +++ b/namespaces/execin.go @@ -6,6 +6,7 @@ import ( "io" "os" "os/exec" + "path/filepath" "strconv" "syscall" @@ -15,19 +16,27 @@ import ( "github.com/docker/libcontainer/system" ) +// ExecIn reexec's the initPath with the argv 0 rewrite to "nsenter" so that it is able to run the +// setns code in a single threaded environment joining the existing containers' namespaces. func ExecIn(container *libcontainer.Config, state *libcontainer.State, userArgs []string, initPath string, stdin io.Reader, stdout, stderr io.Writer, console string, startCallback func(*exec.Cmd)) (int, error) { - args := []string{"--nspid", strconv.Itoa(state.InitPid)} + args := []string{"nsenter", "--nspid", strconv.Itoa(state.InitPid)} if console != "" { args = append(args, "--console", console) } - args = append(args, "nsenter", "--") - args = append(args, userArgs...) + cmd := &exec.Cmd{ + Path: initPath, + Args: append(args, append([]string{"--"}, userArgs...)...), + } - cmd := exec.Command(initPath, args...) + if filepath.Base(initPath) == initPath { + if lp, err := exec.LookPath(initPath); err == nil { + cmd.Path = lp + } + } pipe, err := syncpipe.NewSyncPipe() if err != nil { diff --git a/namespaces/nsenter/nsenter.c b/namespaces/nsenter/nsenter.c index 9afe2f01..3e15db7c 100644 --- a/namespaces/nsenter/nsenter.c +++ b/namespaces/nsenter/nsenter.c @@ -71,7 +71,7 @@ int setns(int fd, int nstype) void print_usage() { fprintf(stderr, - " nsenter --nspid -- cmd1 arg1 arg2...\n"); + "nsenter --nspid --console -- cmd1 arg1 arg2...\n"); } void nsenter() @@ -80,22 +80,10 @@ void nsenter() char **argv; get_args(&argc, &argv); - // Ignore if this is not for us. - if (argc < 4) { - return; - } - - int found_nsenter = 0; - for (c = 0; c < argc; ++c) { - if (strcmp(argv[c], kNsEnter) == 0) { - found_nsenter = 1; - break; - } - } - - if (!found_nsenter) { - return; - } + // check argv 0 to ensure that we are supposed to setns + if (strcmp(argv[0], kNsEnter) != 0) { + return; + } static const struct option longopts[] = { {"nspid", required_argument, NULL, 'n'}, @@ -117,10 +105,6 @@ void nsenter() } } - if (strcmp(argv[optind], kNsEnter) != 0) { - return; - } - if (init_pid_str == NULL) { print_usage(); exit(1);