From 1a3ade90371f7e9490e8f3be0aff83f2e178c327 Mon Sep 17 00:00:00 2001 From: David Gibson Date: Thu, 6 Apr 2023 13:28:19 +1000 Subject: nstool: Enter holder's cwd when changing mount ns with nstool exec If we enter a mount namespace with nstool exec our working directory will be changed to / in the new mount ns. This is surprising if we haven't actually altered any mounts yet in the new ns. Instead, change the working directory to match that of the holder process in this situation. Signed-off-by: David Gibson Signed-off-by: Stefano Brivio --- test/nstool.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/test/nstool.c b/test/nstool.c index c7f2b25..e6d7d37 100644 --- a/test/nstool.c +++ b/test/nstool.c @@ -420,6 +420,7 @@ static void cmd_exec(int argc, char *argv[]) const struct ns_type *nst; int ctlfd, flags, opt, rc; const char *const *xargs; + struct holder_info info; bool keepcaps = false; struct ucred peercred; const char *exe; @@ -444,7 +445,7 @@ static void cmd_exec(int argc, char *argv[]) sockpath = argv[optind]; - ctlfd = connect_ctl(sockpath, false, NULL, &peercred); + ctlfd = connect_ctl(sockpath, false, &info, &peercred); flags = detect_namespaces(peercred.pid); @@ -475,6 +476,15 @@ static void cmd_exec(int argc, char *argv[]) nst->name, strerror(errno)); } + /* If we've entered a mount ns, our cwd has changed to /. + * Switch to the cwd of the holder, which is probably less + * surprising. */ + if (flags & CLONE_NEWNS) { + rc = chdir(info.cwd); + if (rc < 0) + die("chdir(\"%s\"): %s\n", info.cwd, strerror(errno)); + } + /* Fork to properly enter PID namespace */ xpid = fork(); if (xpid < 0) -- cgit v1.2.3