From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by passt.top (Postfix) with ESMTP id 2D0DE5A0265 for ; Thu, 13 Oct 2022 04:17:17 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1665627436; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=VssmGcWRWHKc9gEb1uOYQa1kAC57ihUreLJM7/V9R54=; b=dIeN/INrgNK8ODenaZDj46qss5NzvklJvYOk2NPLHQlTpkqKxAT4qc6ns7d42Wqnj3dUl9 peWXfezQ9CkWPXUTBuPr0UXFFUyUSP2iLs8AP71VkPjtkpoTmKx6iPYlRe6Sz+elFeSSw5 RtDKEsopqRW6B9BzUesIKXOmMEQQafA= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-178-NCPuOCpKMWqg7k1HOulXAg-1; Wed, 12 Oct 2022 22:17:13 -0400 X-MC-Unique: NCPuOCpKMWqg7k1HOulXAg-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id E816C86F130; Thu, 13 Oct 2022 02:17:06 +0000 (UTC) Received: from maya.cloud.tilaa.com (ovpn-208-3.brq.redhat.com [10.40.208.3]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 529CC1121333; Thu, 13 Oct 2022 02:17:04 +0000 (UTC) Date: Thu, 13 Oct 2022 04:16:59 +0200 From: Stefano Brivio To: David Gibson Subject: Re: [PATCH 02/10] pasta: More general way of starting spawned shell as a login shell Message-ID: <20221013041659.02ab4ec7@elisabeth> In-Reply-To: <20221011054018.1449506-3-david@gibson.dropbear.id.au> References: <20221011054018.1449506-1-david@gibson.dropbear.id.au> <20221011054018.1449506-3-david@gibson.dropbear.id.au> Organization: Red Hat MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Message-ID-Hash: F24UUPKFITJLXSUWH42ROZYRSRNTXO7J X-Message-ID-Hash: F24UUPKFITJLXSUWH42ROZYRSRNTXO7J X-MailFrom: sbrivio@redhat.com 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: passt-dev@passt.top X-Mailman-Version: 3.3.3 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: Just nits here: On Tue, 11 Oct 2022 16:40:10 +1100 David Gibson wrote: > When invoked so as to spawn a shell, pasta checks explicitly for the > shell being bash and if so, adds a "-l" option to make it a login shell. > This is not ideal, since this is a bash specific option and requires pasta > to know about specific shell variants. > > There's a general convention for starting a login shell, which is to > prepend a "-" to argv[0]. Use this approach instead, so we don't need bash > specific logic. Hah, I didn't know that was the meaning. > Signed-off-by: David Gibson > --- > pasta.c | 32 ++++++++++++++++++++------------ > 1 file changed, 20 insertions(+), 12 deletions(-) > > diff --git a/pasta.c b/pasta.c > index 1dd8267..7c3acef 100644 > --- a/pasta.c > +++ b/pasta.c > @@ -148,10 +148,12 @@ void pasta_open_ns(struct ctx *c, const char *netns) > > /** > * struct pasta_setup_ns_arg - Argument for pasta_setup_ns() > + * @exe: Executable to run > * @argv: Command and arguments to run > */ > struct pasta_setup_ns_arg { > - char **argv; > + const char *exe; > + char *const *argv; > }; > > /** > @@ -162,12 +164,13 @@ struct pasta_setup_ns_arg { > */ > static int pasta_setup_ns(void *arg) > { > - struct pasta_setup_ns_arg *a = (struct pasta_setup_ns_arg *)arg; > + const struct pasta_setup_ns_arg *a > + = (const struct pasta_setup_ns_arg *)arg; At this point the assignment could be split onto another line. > > FWRITE("/proc/sys/net/ipv4/ping_group_range", "0 0", > "Cannot set ping_group_range, ICMP requests might fail"); > > - execvp(a->argv[0], a->argv); > + execvp(a->exe, a->argv); > > perror("execvp"); > exit(EXIT_FAILURE); > @@ -182,26 +185,31 @@ static int pasta_setup_ns(void *arg) > void pasta_start_ns(struct ctx *c, int argc, char *argv[]) > { > struct pasta_setup_ns_arg arg = { > + .exe = argv[0], > .argv = argv, > }; > - char *shell = getenv("SHELL"); > - char *sh_argv[] = { shell, NULL }; > - char *bash_argv[] = { shell, "-l", NULL }; > + char *sh_argv[] = { NULL, NULL }; > char ns_fn_stack[NS_FN_STACK_SIZE]; If you respin, it would be nice to have the usual ordering here (sh_argv[] after ns_fn_stack). > + char sh_arg0[PATH_MAX + 1]; > > c->foreground = 1; > if (!c->debug) > c->quiet = 1; > > - if (!shell) > - shell = "/bin/sh"; > > if (argc == 0) { > - if (strstr(shell, "/bash")) { > - arg.argv = bash_argv; > - } else { > - arg.argv = sh_argv; > + arg.exe = getenv("SHELL"); > + if (!arg.exe) > + arg.exe = "/bin/sh"; > + > + if ((size_t)snprintf(sh_arg0, sizeof(sh_arg0), > + "-%s", arg.exe) >= sizeof(sh_arg0)) { This is completely specified and looks safe, but it also looks more complicated than it actually is, at a glance. Maybe a separate length check before snprintf() would make it look more natural. Not a strong preference though. > + err("$SHELL is too long (%u bytes)", > + strlen(arg.exe)); > + exit(EXIT_FAILURE); > } > + sh_argv[0] = sh_arg0; > + arg.argv = sh_argv; > } > > pasta_child_pid = clone(pasta_setup_ns, -- Stefano