summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorQuentin Rameau <quinq@fifth.space>2018-03-11 14:47:45 +0100
committerA. Wilcox <AWilcox@Wilcox-Tech.com>2023-05-05 21:21:38 -0500
commit66e214d186bf50c647b8516c1f4a1a7cd40d5ea3 (patch)
treebd0d4d97d998ec3337b740f2f9e9ad2f1ccb8f6a
parentb1e554e906de3486d5ee82903cb61d4bfc6d4fab (diff)
downloadmusl-66e214d186bf50c647b8516c1f4a1a7cd40d5ea3.tar.gz
musl-66e214d186bf50c647b8516c1f4a1a7cd40d5ea3.tar.bz2
musl-66e214d186bf50c647b8516c1f4a1a7cd40d5ea3.tar.xz
musl-66e214d186bf50c647b8516c1f4a1a7cd40d5ea3.zip
Continue trying execution with "/bin/sh" for execlp and execvp
As Rick stated, this isn't a clean solution because argv can be arbirtary long and overflow the stack. I post it here in case you'd find it useful anyway.
-rw-r--r--src/process/execlp.c10
-rw-r--r--src/process/execsh.c18
-rw-r--r--src/process/execvp.c8
3 files changed, 34 insertions, 2 deletions
diff --git a/src/process/execlp.c b/src/process/execlp.c
index 5eed886e..f6da398b 100644
--- a/src/process/execlp.c
+++ b/src/process/execlp.c
@@ -1,6 +1,9 @@
#include <unistd.h>
+#include <errno.h>
#include <stdarg.h>
+extern int __execsh(const char *, char *const []);
+
int execlp(const char *file, const char *argv0, ...)
{
int argc;
@@ -17,6 +20,11 @@ int execlp(const char *file, const char *argv0, ...)
argv[i] = va_arg(ap, char *);
argv[i] = NULL;
va_end(ap);
- return execvp(file, argv);
+ execvp(file, argv);
+ if (errno == ENOEXEC) {
+ errno = 0;
+ return __execsh(file, argv);
+ }
+ return -1;
}
}
diff --git a/src/process/execsh.c b/src/process/execsh.c
new file mode 100644
index 00000000..180bb2aa
--- /dev/null
+++ b/src/process/execsh.c
@@ -0,0 +1,18 @@
+#include <unistd.h>
+#include <errno.h>
+#include "libc.h"
+
+int
+__execsh(const char *file, char *const argv[])
+{
+ int i, argc;
+ char **p;
+
+ for (argc=1, p=(char **)argv; *p; ++argc, ++p);
+
+ char *nargv[argc+1];
+ nargv[0] = (char *)file;
+ for (i=0; i<argc; ++i)
+ nargv[i+1] = argv[i];
+ return execv("/bin/sh", nargv);
+}
diff --git a/src/process/execvp.c b/src/process/execvp.c
index ef3b9dd5..5e6255f7 100644
--- a/src/process/execvp.c
+++ b/src/process/execvp.c
@@ -5,6 +5,7 @@
#include <limits.h>
extern char **__environ;
+extern int __execsh(const char *, char *const []);
int __execvpe(const char *file, char *const argv[], char *const envp[])
{
@@ -54,7 +55,12 @@ int __execvpe(const char *file, char *const argv[], char *const envp[])
int execvp(const char *file, char *const argv[])
{
- return __execvpe(file, argv, __environ);
+ __execvpe(file, argv, __environ);
+ if (errno == ENOEXEC) {
+ errno = 0;
+ return __execsh(file, argv);
+ }
+ return -1;
}
weak_alias(__execvpe, execvpe);