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 48FDC5A026D for ; Tue, 4 Apr 2023 03:46:50 +0200 (CEST) Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4Pr9bj4qRxz4xFq; Tue, 4 Apr 2023 11:46:41 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=201602; t=1680572801; bh=7iG+Q0VG0juZVlfM2ItxVR1oPdzbIB3OyZ9i9fDExhM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YJJHlJYOQwwRnFBcoXj0g8J1L2nAsaNv4TkgsECrhXqOqsDAQ1qHpmQ0ujltbdVP+ oK32wLZGYmOjV50eEl/nuHxdss2tTibgbLgWKDythbqFaOp0MX88DxwIXXhk922f0p STIfPJCQB3WCF/6MAwpAsLDx1HV+TUtVvv9Sg768= From: David Gibson To: passt-dev@passt.top, Stefano Brivio Subject: [PATCH 14/14] nstool: Enter holder's cwd when changing mount ns with nstool exec Date: Tue, 4 Apr 2023 11:46:38 +1000 Message-Id: <20230404014638.3225556-15-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230404014638.3225556-1-david@gibson.dropbear.id.au> References: <20230404014638.3225556-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: 2VM7KEPLRV6PD2GIWEYADITBFZV35EVL X-Message-ID-Hash: 2VM7KEPLRV6PD2GIWEYADITBFZV35EVL 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 c07eabb..a1c31be 100644 --- a/test/nstool.c +++ b/test/nstool.c @@ -417,6 +417,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; @@ -441,7 +442,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); @@ -472,6 +473,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