From nobody Fri Dec 19 07:47:23 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1628372998771184.03205431661627; Sat, 7 Aug 2021 14:49:58 -0700 (PDT) Received: from localhost ([::1]:34668 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mCUCb-0000wh-GQ for importer@patchew.org; Sat, 07 Aug 2021 17:49:57 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:43334) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mCU5x-0006be-DP for qemu-devel@nongnu.org; Sat, 07 Aug 2021 17:43:05 -0400 Received: from mail-io1-xd42.google.com ([2607:f8b0:4864:20::d42]:43945) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mCU5v-0004oA-U1 for qemu-devel@nongnu.org; Sat, 07 Aug 2021 17:43:05 -0400 Received: by mail-io1-xd42.google.com with SMTP id y1so19935003iod.10 for ; Sat, 07 Aug 2021 14:43:03 -0700 (PDT) Received: from dune.bsdimp.com (50-253-99-174-static.hfc.comcastbusiness.net. [50.253.99.174]) by smtp.gmail.com with ESMTPSA id a17sm8062827ios.36.2021.08.07.14.43.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 07 Aug 2021 14:43:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bsdimp-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=/m+LrsTqQXACGZ2lhzaNittRoT4FuKArZPmmbyQaTCo=; b=d3/wHM+rSJyrWFYXEth2fvEu1tE2SHhDyZv+GqG8iMOQSvXW4DyLe0AR2vwg1pxEse FMC84+I5DKoy7mmiDw6Lh36WaoDY1jXMZeW1tSWXRZgB1pJhYGm35TmFcTpcckh7sCDQ 9ju2TXwSLrNLlHR03+NDafkNs1Uvb1skSaaQLONnpmckXSHjqxFuKoHNsZ+OgF/AWvwO VOOH1JEf8TusgI1TQnYuiCbRkjoHGLuYRXIkSMzZaP155zhzrb+BcwpdVcWTPd/vJbIc RH+k/GJRpy9p+icjzqza0fZEJrOqEUfAn2lDnXDWDhRinKcqTkJinSC2eKKaMV2HdwB5 5jhA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=/m+LrsTqQXACGZ2lhzaNittRoT4FuKArZPmmbyQaTCo=; b=ENC8GL9iF63RBBawE2GyXSWTEepBwsIxpowGErYH8/SGRcak38xMJ9RvlyDs3ILput 5vhuJOdTBQGvZm5rtwXO6jc9u7RaA7Kse04eq8ULkBUD0tTEES9BlObAWbLBknL7FCwF 7lH95TKIY+MjhGH7gHtqyqtmnHQ+rQB+OGtvw4nZJjsgGFc9IbNaiOI5zf9vBiwX8m5m H77uz4DFQ7SzgSZqziJs44UEQv2OtXGGHbt/jqVgrv3ZzvnYYVCv1Mw5q/J6KpqdixNu DPSdrDcjaAHey8Dgv+ZJpfQBR1YgZvDt8lx1et/ZtNdEKhIgRuTvliAizM1IBQXJnSYO O6NA== X-Gm-Message-State: AOAM531gXjMyn1LAJfTO+lNJe30NvVYTqKDSA/TXQTpCZ3mOKwHwhj5e G9DNSpdFbjoLoiTWhwgHmz4lFWPPICL0jZcl X-Google-Smtp-Source: ABdhPJwOTRpJToYwZvjyX3WrkCAnELNDMpg33YH1yX5aXWf1wIzP1ErTsbJ4tY3MthvXx9BNYPWnfQ== X-Received: by 2002:a05:6e02:190d:: with SMTP id w13mr100884ilu.53.1628372582772; Sat, 07 Aug 2021 14:43:02 -0700 (PDT) From: Warner Losh To: qemu-devel@nongnu.org Subject: [PATCH for 6.2 12/49] bsd-user: implement path searching Date: Sat, 7 Aug 2021 15:42:05 -0600 Message-Id: <20210807214242.82385-13-imp@bsdimp.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210807214242.82385-1-imp@bsdimp.com> References: <20210807214242.82385-1-imp@bsdimp.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: none client-ip=2607:f8b0:4864:20::d42; envelope-from=imp@bsdimp.com; helo=mail-io1-xd42.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_NONE=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kevans@freebsd.org, Stacey Son , Warner Losh Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1628372999956100001 Content-Type: text/plain; charset="utf-8" Use the PATH to find the executable given a bare argument. Signed-off-by: Stacey Son Signed-off-by: Warner Losh --- bsd-user/bsdload.c | 73 +++++++++++++++++++++++++++++++++++++++++++++- bsd-user/qemu.h | 3 +- 2 files changed, 74 insertions(+), 2 deletions(-) diff --git a/bsd-user/bsdload.c b/bsd-user/bsdload.c index 379015c744..f8030d72bc 100644 --- a/bsd-user/bsdload.c +++ b/bsd-user/bsdload.c @@ -139,17 +139,88 @@ abi_ulong loader_build_argptr(int envc, int argc, abi= _ulong sp, return sp; } =20 +static bool is_there(const char *candidate) +{ + struct stat fin; + + /* XXX work around access(2) false positives for superuser */ + if (access(candidate, X_OK) =3D=3D 0 && stat(candidate, &fin) =3D=3D 0= && + S_ISREG(fin.st_mode) && (getuid() !=3D 0 || + (fin.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) !=3D 0)) { + return true; + } + + return false; +} + +static bool find_in_path(char *path, const char *filename, char *retpath, + size_t rpsize) +{ + const char *d; + + while ((d =3D strsep(&path, ":")) !=3D NULL) { + if (*d =3D=3D '\0') { + d =3D "."; + } + if (snprintf(retpath, rpsize, "%s/%s", d, filename) >=3D (int)rpsi= ze) { + continue; + } + if (is_there((const char *)retpath)) { + return true; + } + } + return false; +} + int loader_exec(const char *filename, char **argv, char **envp, struct target_pt_regs *regs, struct image_info *infop, struct bsd_binprm *bprm) { + char *p, *path =3D NULL, fullpath[PATH_MAX]; int retval, i; =20 bprm->p =3D TARGET_PAGE_SIZE * MAX_ARG_PAGES; for (i =3D 0; i < MAX_ARG_PAGES; i++) { /* clear page-table */ bprm->page[i] =3D NULL; } - retval =3D open(filename, O_RDONLY); + + if (strchr(filename, '/') !=3D NULL) { + path =3D realpath(filename, fullpath); + if (path =3D=3D NULL) { + /* Failed to resolve. */ + return -1; + } + if (!is_there(path)) { + return -1; + } + retval =3D open(path, O_RDONLY); + bprm->fullpath =3D g_strdup(path); + } else { + p =3D getenv("PATH"); + if (p =3D=3D NULL) { + return -1; + } + + path =3D g_strdup(p); + if (path =3D=3D NULL) { + fprintf(stderr, "Out of memory\n"); + return -1; + } + + if (!find_in_path(path, filename, fullpath, sizeof(fullpath))) { + return -1; + } + retval =3D open(fullpath, O_RDONLY); + bprm->fullpath =3D g_strdup(fullpath); + + g_free(path); + } + + /* bprm->fullpath must be populated. */ + if (bprm->fullpath =3D=3D NULL) { + fprintf(stderr, "Out of memory\n"); + return -1; + } if (retval < 0) { return retval; } diff --git a/bsd-user/qemu.h b/bsd-user/qemu.h index 5237e35f9c..6b601ce4b5 100644 --- a/bsd-user/qemu.h +++ b/bsd-user/qemu.h @@ -124,7 +124,8 @@ struct bsd_binprm { int argc, envc; char **argv; char **envp; - char *filename; /* Name of binary */ + char *filename; /* (Given) Name of binary */ + char *fullpath; /* Full path of binary */ }; =20 void do_init_thread(struct target_pt_regs *regs, struct image_info *infop); --=20 2.32.0