[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH for 6.2 12/49] bsd-user: implement path searching
From: |
Warner Losh |
Subject: |
[PATCH for 6.2 12/49] bsd-user: implement path searching |
Date: |
Sat, 7 Aug 2021 15:42:05 -0600 |
Use the PATH to find the executable given a bare argument.
Signed-off-by: Stacey Son <sson@FreeBSD.org>
Signed-off-by: Warner Losh <imp@bsdimp.com>
---
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;
}
+static bool is_there(const char *candidate)
+{
+ struct stat fin;
+
+ /* XXX work around access(2) false positives for superuser */
+ if (access(candidate, X_OK) == 0 && stat(candidate, &fin) == 0 &&
+ S_ISREG(fin.st_mode) && (getuid() != 0 ||
+ (fin.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) != 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 = strsep(&path, ":")) != NULL) {
+ if (*d == '\0') {
+ d = ".";
+ }
+ if (snprintf(retpath, rpsize, "%s/%s", d, filename) >= (int)rpsize) {
+ 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 = NULL, fullpath[PATH_MAX];
int retval, i;
bprm->p = TARGET_PAGE_SIZE * MAX_ARG_PAGES;
for (i = 0; i < MAX_ARG_PAGES; i++) { /* clear page-table */
bprm->page[i] = NULL;
}
- retval = open(filename, O_RDONLY);
+
+ if (strchr(filename, '/') != NULL) {
+ path = realpath(filename, fullpath);
+ if (path == NULL) {
+ /* Failed to resolve. */
+ return -1;
+ }
+ if (!is_there(path)) {
+ return -1;
+ }
+ retval = open(path, O_RDONLY);
+ bprm->fullpath = g_strdup(path);
+ } else {
+ p = getenv("PATH");
+ if (p == NULL) {
+ return -1;
+ }
+
+ path = g_strdup(p);
+ if (path == NULL) {
+ fprintf(stderr, "Out of memory\n");
+ return -1;
+ }
+
+ if (!find_in_path(path, filename, fullpath, sizeof(fullpath))) {
+ return -1;
+ }
+ retval = open(fullpath, O_RDONLY);
+ bprm->fullpath = g_strdup(fullpath);
+
+ g_free(path);
+ }
+
+ /* bprm->fullpath must be populated. */
+ if (bprm->fullpath == 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 */
};
void do_init_thread(struct target_pt_regs *regs, struct image_info *infop);
--
2.32.0
- [PATCH for 6.2 06/49] bsd-user: merge comments and guards from bsd-user fork, (continued)
- [PATCH for 6.2 06/49] bsd-user: merge comments and guards from bsd-user fork, Warner Losh, 2021/08/07
- [PATCH for 6.2 08/49] bsd-user: style nits: fix whitespace issues to be qemu standard, Warner Losh, 2021/08/07
- [PATCH for 6.2 09/49] bsd-user: add license, Warner Losh, 2021/08/07
- [PATCH for 6.2 10/49] bsd-user: pass the bsd_param into loader_exec, Warner Losh, 2021/08/07
- [PATCH for 6.2 11/49] bsd-user: Fix calculation of size to allocate, Warner Losh, 2021/08/07
- [PATCH for 6.2 12/49] bsd-user: implement path searching,
Warner Losh <=
- [PATCH for 6.2 13/49] bsd-user: Eliminate elf personality, Warner Losh, 2021/08/07
- [PATCH for 6.2 14/49] bsd-user: remove a.out support, Warner Losh, 2021/08/07
- [PATCH for 6.2 15/49] bsd-user: TARGET_NGROUPS unused in this file, remove, Warner Losh, 2021/08/07
- [PATCH for 6.2 16/49] bsd-user: elfload: simplify bswap a bit., Warner Losh, 2021/08/07