From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from gandalf.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by passt.top (Postfix) with ESMTPS id 11FD85A0275 for ; Thu, 6 Apr 2023 05:28:28 +0200 (CEST) Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4PsRm60XVLz4xFv; Thu, 6 Apr 2023 13:28:22 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=201602; t=1680751702; bh=I8FgwsSobCOL1AQkrrHmLzq7G8e1G1Nwv6JukmPIhDg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=QvR02CeYUuOekB/n6Lc0SiF0lYU/QqAPsyIYTPPIid5a1TUD2KfMeyHpnpEVjYw7t x8rB3GjNMsXoH5iWUWjsEzmMVVJ7EYBCvYZG1i3NPaNYsdHESb/DHdCS6aUTbpcOO2 h34cxNIAF48i6REZ1VFCGOlOS0AxTbxdXPlSoA6E= From: David Gibson To: passt-dev@passt.top, Stefano Brivio Subject: [PATCH v2 14/14] nstool: Enter holder's cwd when changing mount ns with nstool exec Date: Thu, 6 Apr 2023 13:28:19 +1000 Message-Id: <20230406032819.707441-15-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230406032819.707441-1-david@gibson.dropbear.id.au> References: <20230406032819.707441-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: WM236X7XCGBDE2P4Z5HT7O2SUY2WOWRV X-Message-ID-Hash: WM236X7XCGBDE2P4Z5HT7O2SUY2WOWRV X-MailFrom: dgibson@gandalf.ozlabs.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: David Gibson X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: 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 --- test/nstool.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/test/nstool.c b/test/nstool.c index ece566e..48f4b77 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) -- 2.39.2