2015-04-23 23:16:57 +00:00
|
|
|
/* radare - LGPL - Copyright 2014-2015 - pancake */
|
2014-07-09 00:24:31 +00:00
|
|
|
|
|
|
|
/* this helper api is here because it depends on r_util and r_socket */
|
|
|
|
/* we should find a better place for it. r_io? */
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <r_util.h>
|
|
|
|
#include <r_socket.h>
|
|
|
|
#if __APPLE__
|
|
|
|
#include <spawn.h>
|
|
|
|
#include <sys/ptrace.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <mach/exception_types.h>
|
|
|
|
#include <mach/mach_init.h>
|
|
|
|
#include <mach/mach_port.h>
|
|
|
|
#include <mach/mach_traps.h>
|
|
|
|
#include <mach/task.h>
|
|
|
|
#include <mach/task_info.h>
|
|
|
|
#include <mach/thread_act.h>
|
|
|
|
#include <mach/thread_info.h>
|
|
|
|
#include <mach/vm_map.h>
|
|
|
|
#include <mach-o/loader.h>
|
|
|
|
#include <mach-o/nlist.h>
|
|
|
|
#endif
|
|
|
|
#if __UNIX__
|
|
|
|
#include <sys/resource.h>
|
|
|
|
#include <signal.h>
|
2014-11-10 02:06:41 +00:00
|
|
|
#include <grp.h>
|
2015-03-26 14:19:12 +00:00
|
|
|
#include <errno.h>
|
2014-07-09 00:25:43 +00:00
|
|
|
#if __linux__ && !__ANDROID__
|
2014-07-09 00:24:31 +00:00
|
|
|
#include <sys/personality.h>
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
R_API RRunProfile *r_run_new(const char *str) {
|
2014-07-09 01:48:47 +00:00
|
|
|
RRunProfile *p = R_NEW (RRunProfile);
|
|
|
|
if (p) {
|
|
|
|
r_run_reset (p);
|
|
|
|
if (str) r_run_parsefile (p, str);
|
|
|
|
}
|
2014-07-09 00:24:31 +00:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2014-07-09 01:48:47 +00:00
|
|
|
R_API void r_run_reset(RRunProfile *p) {
|
|
|
|
memset (p, 0, sizeof (RRunProfile));
|
|
|
|
p->_aslr = -1;
|
|
|
|
}
|
|
|
|
|
2014-07-09 00:24:31 +00:00
|
|
|
R_API int r_run_parse(RRunProfile *pf, const char *profile) {
|
|
|
|
char *p, *o, *str = strdup (profile);
|
|
|
|
for (o = p = str; (o = strchr (p, '\n')); p = o) {
|
2014-09-11 01:05:59 +00:00
|
|
|
*o++ = 0;
|
2014-07-09 00:24:31 +00:00
|
|
|
r_run_parseline (pf, p);
|
|
|
|
}
|
|
|
|
free (str);
|
2014-09-11 01:05:59 +00:00
|
|
|
return 1;
|
2014-07-09 00:24:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
R_API void r_run_free (RRunProfile *r) {
|
|
|
|
//int i;
|
|
|
|
//for (i=0;i<R_RUN_PROFILE_NARGS;i++)
|
|
|
|
// free (r->_args[i]);
|
|
|
|
free (r->_system);
|
|
|
|
free (r->_program);
|
|
|
|
free (r->_stdin);
|
|
|
|
free (r->_stdout);
|
|
|
|
free (r->_stderr);
|
|
|
|
free (r->_chgdir);
|
|
|
|
free (r->_chroot);
|
|
|
|
free (r->_libpath);
|
|
|
|
free (r->_preload);
|
|
|
|
free (r);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if __UNIX__
|
|
|
|
static void set_limit(int n, int a, ut64 b) {
|
|
|
|
if (n) {
|
|
|
|
struct rlimit cl = {b, b};
|
|
|
|
setrlimit (RLIMIT_CORE, &cl);
|
|
|
|
} else {
|
|
|
|
struct rlimit cl = {0, 0};
|
|
|
|
setrlimit (a, &cl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static char *getstr(const char *src) {
|
|
|
|
int len;
|
|
|
|
char *ret = NULL;
|
|
|
|
|
|
|
|
switch (*src) {
|
|
|
|
case '\'':
|
|
|
|
ret = strdup (src+1);
|
|
|
|
if (ret) {
|
|
|
|
len = strlen (ret);
|
|
|
|
if (len>0) {
|
|
|
|
len--;
|
|
|
|
if (ret[len]=='\'') {
|
|
|
|
ret[len] = 0;
|
|
|
|
return ret;
|
|
|
|
} else eprintf ("Missing \"\n");
|
|
|
|
}
|
|
|
|
free (ret);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
case '"':
|
|
|
|
ret = strdup (src+1);
|
|
|
|
if (ret) {
|
|
|
|
len = strlen (ret);
|
|
|
|
if (len>0) {
|
|
|
|
len--;
|
|
|
|
if (ret[len]=='"') {
|
|
|
|
ret[len] = 0;
|
|
|
|
r_str_unescape (ret);
|
|
|
|
return ret;
|
|
|
|
} else eprintf ("Missing \"\n");
|
|
|
|
}
|
|
|
|
free (ret);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
case '@':
|
2014-10-23 01:56:10 +00:00
|
|
|
{
|
|
|
|
char *pat = strchr (src+1, '@');
|
|
|
|
if (pat) {
|
|
|
|
*pat++ = 0;
|
|
|
|
int i, rep = atoi (src+1);
|
|
|
|
int len = strlen (pat);
|
2014-10-23 02:45:01 +00:00
|
|
|
if (rep>0) {
|
|
|
|
char *buf = malloc (rep);
|
|
|
|
for(i=0;i<rep;i++) {
|
|
|
|
buf[i] = pat[i%len];
|
|
|
|
}
|
|
|
|
return buf;
|
2014-10-23 01:56:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// slurp file
|
|
|
|
return r_file_slurp (src+1, NULL);
|
|
|
|
}
|
2014-10-20 11:39:37 +00:00
|
|
|
case '!':
|
|
|
|
return r_str_trim_tail (r_sys_cmd_str (src+1, NULL, NULL));
|
2014-07-09 00:24:31 +00:00
|
|
|
case ':':
|
2014-10-28 15:45:19 +00:00
|
|
|
if (src[1]=='!') {
|
|
|
|
ret = r_str_trim_tail (r_sys_cmd_str (src+1, NULL, NULL));
|
|
|
|
} else {
|
|
|
|
ret = strdup (src);
|
|
|
|
}
|
2014-07-09 00:24:31 +00:00
|
|
|
len = r_hex_str2bin (src+1, (ut8*)ret);
|
|
|
|
if (len>0) {
|
|
|
|
ret[len] = 0;
|
|
|
|
return ret;
|
|
|
|
} else {
|
|
|
|
eprintf ("Invalid hexpair string\n");
|
|
|
|
free (ret);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
r_str_unescape ((ret = strdup (src)));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int parseBool (const char *e) {
|
|
|
|
return (strcmp (e, "yes")?
|
|
|
|
(strcmp (e, "true")?
|
|
|
|
(strcmp (e, "1")?
|
|
|
|
0: 1): 1): 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: move into r_util? r_run_... ? with the rest of funcs?
|
|
|
|
static void setASLR(int enabled) {
|
|
|
|
#if __linux__
|
|
|
|
#define RVAS "/proc/sys/kernel/randomize_va_space"
|
|
|
|
if (enabled) {
|
|
|
|
system ("echo 2 > "RVAS);
|
|
|
|
} else {
|
2014-09-12 00:37:49 +00:00
|
|
|
#if __ANDROID__
|
|
|
|
system ("echo 0 > "RVAS);
|
|
|
|
#else
|
2015-04-25 02:24:20 +00:00
|
|
|
#ifdef ADDR_NO_RANDOMIZE
|
2014-07-09 00:24:31 +00:00
|
|
|
if (personality (ADDR_NO_RANDOMIZE) == -1)
|
2015-04-25 02:24:20 +00:00
|
|
|
#endif
|
2014-07-09 00:24:31 +00:00
|
|
|
system ("echo 0 > "RVAS);
|
2014-07-09 00:25:43 +00:00
|
|
|
#endif
|
2014-07-09 00:24:31 +00:00
|
|
|
}
|
|
|
|
#elif __APPLE__
|
2015-08-16 15:45:23 +00:00
|
|
|
// TOO OLD setenv ("DYLD_NO_PIE", "1", 1);
|
|
|
|
// disable this because its
|
|
|
|
//eprintf ("Patch mach0.hdr.flags with:\n"
|
|
|
|
// "f MH_PIE=0x00200000; wB-MH_PIE @ 24\n");
|
2014-07-09 00:24:31 +00:00
|
|
|
// for osxver>=10.7
|
|
|
|
// "unset the MH_PIE bit in an already linked executable" with --no-pie flag of the script
|
2015-08-16 15:45:23 +00:00
|
|
|
// the right way is to disable the aslr bit in the spawn call
|
2014-07-09 00:24:31 +00:00
|
|
|
#else
|
|
|
|
// not supported for this platform
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-07-09 03:05:22 +00:00
|
|
|
R_API int r_run_parsefile (RRunProfile *p, const char *b) {
|
2014-07-09 01:48:47 +00:00
|
|
|
int ret;
|
|
|
|
char *s = r_file_slurp (b, NULL);
|
|
|
|
if (s) {
|
|
|
|
ret = r_run_parse (p, s);
|
|
|
|
free (s);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-07-09 00:24:31 +00:00
|
|
|
R_API int r_run_parseline (RRunProfile *p, char *b) {
|
|
|
|
int must_free = R_FALSE;
|
|
|
|
char *e = strchr (b, '=');
|
|
|
|
if (!e) return 0;
|
|
|
|
if (*b=='#') return 0;
|
|
|
|
*e++ = 0;
|
|
|
|
if (*e=='$') {
|
|
|
|
must_free = R_TRUE;
|
|
|
|
e = r_sys_getenv (e);
|
|
|
|
}
|
|
|
|
if (e == NULL) return 0;
|
|
|
|
if (!strcmp (b, "program")) p->_args[0] = p->_program = strdup (e);
|
|
|
|
else if (!strcmp (b, "system")) p->_system = strdup (e);
|
|
|
|
else if (!strcmp (b, "aslr")) p->_aslr = parseBool (e);
|
2014-11-22 02:00:31 +00:00
|
|
|
else if (!strcmp (b, "pid")) p->_pid = atoi (e);
|
2015-02-18 00:10:19 +00:00
|
|
|
else if (!strcmp (b, "pidfile")) p->_pidfile = strdup (e);
|
2014-07-09 00:24:31 +00:00
|
|
|
else if (!strcmp (b, "connect")) p->_connect = strdup (e);
|
|
|
|
else if (!strcmp (b, "listen")) p->_listen = strdup (e);
|
|
|
|
else if (!strcmp (b, "stdout")) p->_stdout = strdup (e);
|
|
|
|
else if (!strcmp (b, "stdio")) {
|
|
|
|
p->_stdout = p->_stderr = p->_stdin = strdup (e);
|
|
|
|
} else if (!strcmp (b, "stdin")) p->_stdin = strdup (e);
|
|
|
|
else if (!strcmp (b, "stderr")) p->_stderr = strdup (e);
|
|
|
|
else if (!strcmp (b, "input")) p->_input = strdup (e);
|
|
|
|
else if (!strcmp (b, "chdir")) p->_chgdir = strdup (e);
|
|
|
|
else if (!strcmp (b, "core")) p->_docore = parseBool (e);
|
2015-09-12 17:36:15 +00:00
|
|
|
else if (!strcmp (b, "fork")) p->_dofork = parseBool (e);
|
2014-07-09 00:24:31 +00:00
|
|
|
else if (!strcmp (b, "sleep")) p->_r2sleep = atoi (e);
|
|
|
|
else if (!strcmp (b, "maxstack")) p->_maxstack = atoi (e);
|
|
|
|
else if (!strcmp (b, "maxproc")) p->_maxproc = atoi (e);
|
|
|
|
else if (!strcmp (b, "maxfd")) p->_maxfd = atoi (e);
|
|
|
|
else if (!strcmp (b, "bits")) p->_bits = atoi (e);
|
|
|
|
else if (!strcmp (b, "chroot")) p->_chroot = strdup (e);
|
|
|
|
else if (!strcmp (b, "libpath")) p->_libpath = strdup (e);
|
|
|
|
else if (!strcmp (b, "preload")) p->_preload = strdup (e);
|
|
|
|
else if (!strcmp (b, "r2preload")) p->_r2preload = parseBool (e);
|
2015-06-02 01:54:26 +00:00
|
|
|
else if (!strcmp (b, "r2preweb")) r_sys_setenv ("RARUN2_WEB", "yes");
|
2014-07-09 00:24:31 +00:00
|
|
|
else if (!strcmp (b, "setuid")) p->_setuid = strdup (e);
|
|
|
|
else if (!strcmp (b, "seteuid")) p->_seteuid = strdup (e);
|
|
|
|
else if (!strcmp (b, "setgid")) p->_setgid = strdup (e);
|
|
|
|
else if (!strcmp (b, "setegid")) p->_setegid = strdup (e);
|
2015-03-26 14:19:12 +00:00
|
|
|
else if (!strcmp (b, "nice")) p->_nice = atoi (e);
|
2014-07-09 00:24:31 +00:00
|
|
|
else if (!memcmp (b, "arg", 3)) {
|
|
|
|
int n = atoi (b+3);
|
|
|
|
if (n>=0 && n<R_RUN_PROFILE_NARGS) {
|
|
|
|
p->_args[n] = getstr (e);
|
|
|
|
} else eprintf ("Out of bounds args index: %d\n", n);
|
|
|
|
} else if (!strcmp (b, "timeout")) {
|
|
|
|
p->_timeout = atoi (e);
|
|
|
|
} else if (!strcmp (b, "envfile")) {
|
|
|
|
char *p, buf[1024];
|
|
|
|
FILE *fd = fopen (e, "r");
|
|
|
|
if (!fd) {
|
|
|
|
eprintf ("Cannot open '%s'\n", e);
|
|
|
|
if (must_free == R_TRUE)
|
|
|
|
free (e);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
for (;;) {
|
|
|
|
fgets (buf, sizeof (buf)-1, fd);
|
|
|
|
if (feof (fd)) break;
|
|
|
|
p = strchr (buf, '=');
|
|
|
|
if (p) {
|
|
|
|
*p = 0;
|
|
|
|
r_sys_setenv (buf, p+1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose (fd);
|
|
|
|
} else if (!strcmp (b, "unsetenv")) {
|
|
|
|
r_sys_setenv (e, NULL);
|
|
|
|
} else if (!strcmp (b, "setenv")) {
|
|
|
|
char *v = strchr (e, '=');
|
|
|
|
if (v) {
|
|
|
|
*v++ = 0;
|
|
|
|
r_sys_setenv (e, v);
|
|
|
|
}
|
2015-08-28 22:15:41 +00:00
|
|
|
} else if (!strcmp(b, "clearenv")) {
|
|
|
|
r_sys_clearenv ();
|
2014-07-09 00:24:31 +00:00
|
|
|
}
|
|
|
|
if (must_free == R_TRUE)
|
|
|
|
free (e);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
R_API const char *r_run_help() {
|
|
|
|
return
|
|
|
|
"program=/bin/ls\n"
|
|
|
|
"arg1=/bin\n"
|
|
|
|
"# arg2=hello\n"
|
|
|
|
"# arg3=\"hello\\nworld\"\n"
|
|
|
|
"# arg4=:048490184058104849\n"
|
2014-10-28 15:45:19 +00:00
|
|
|
"# arg5=:!ragg2 -p n50 -d 10:0x8048123\n"
|
|
|
|
"# arg6=@arg.txt\n"
|
|
|
|
"# arg7=@300@ABCD # 300 chars filled with ABCD pattern\n"
|
2014-07-09 00:24:31 +00:00
|
|
|
"# system=r2 -\n"
|
|
|
|
"# aslr=no\n"
|
|
|
|
"setenv=FOO=BAR\n"
|
|
|
|
"# unsetenv=FOO\n"
|
2015-08-28 22:15:41 +00:00
|
|
|
"# clearenv=true\n"
|
2014-07-09 00:24:31 +00:00
|
|
|
"# envfile=environ.txt\n"
|
|
|
|
"timeout=3\n"
|
|
|
|
"# connect=localhost:8080\n"
|
|
|
|
"# listen=8080\n"
|
2015-09-12 17:36:15 +00:00
|
|
|
"# fork=true\n"
|
2014-07-09 00:24:31 +00:00
|
|
|
"# bits=32\n"
|
2014-11-22 02:00:31 +00:00
|
|
|
"# pid=0\n"
|
2015-02-18 00:10:19 +00:00
|
|
|
"# pidfile=/tmp/foo.pid\n"
|
2014-07-09 00:24:31 +00:00
|
|
|
"# #sleep=0\n"
|
|
|
|
"# #maxfd=0\n"
|
|
|
|
"# #maxproc=0\n"
|
|
|
|
"# #maxstack=0\n"
|
|
|
|
"# #core=false\n"
|
|
|
|
"# #stdio=blah.txt\n"
|
|
|
|
"# #stderr=foo.txt\n"
|
|
|
|
"# stdout=foo.txt\n"
|
|
|
|
"# stdin=input.txt\n"
|
|
|
|
"# input=input.txt\n"
|
|
|
|
"# chdir=/\n"
|
|
|
|
"# chroot=/mnt/chroot\n"
|
|
|
|
"# libpath=$PWD:/tmp/lib\n"
|
|
|
|
"# r2preload=yes\n"
|
|
|
|
"# preload=/lib/libfoo.so\n"
|
|
|
|
"# setuid=2000\n"
|
|
|
|
"# seteuid=2000\n"
|
|
|
|
"# setgid=2001\n"
|
2015-03-26 14:19:12 +00:00
|
|
|
"# setegid=2001\n"
|
|
|
|
"# nice=5\n";
|
2014-07-09 00:24:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if __UNIX__
|
|
|
|
static void parseinput (char *s) {
|
|
|
|
if (!*s) return;
|
|
|
|
while (*s++) {
|
|
|
|
if (s[0]=='\\' && s[1]=='n') {
|
|
|
|
*s = '\n';
|
2015-02-11 20:50:37 +00:00
|
|
|
memmove (s+1, s+2, strlen (s+2));
|
2014-07-09 00:24:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
R_API int r_run_start(RRunProfile *p) {
|
2014-07-09 00:25:43 +00:00
|
|
|
#if __APPLE__
|
2014-07-09 00:24:31 +00:00
|
|
|
posix_spawnattr_t attr = {0};
|
|
|
|
pid_t pid = -1;
|
2014-07-09 00:25:43 +00:00
|
|
|
#endif
|
2014-07-09 00:24:31 +00:00
|
|
|
int ret;
|
|
|
|
if (!p->_program && !p->_system) {
|
|
|
|
printf ("No program or system rule defined\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (p->_stdin) {
|
|
|
|
int f = open (p->_stdin, O_RDONLY);
|
|
|
|
if (f < 0)
|
|
|
|
return 1;
|
|
|
|
close (0);
|
|
|
|
dup2 (f, 0);
|
|
|
|
}
|
|
|
|
if (p->_stdout) {
|
|
|
|
int f = open (p->_stdout, O_WRONLY);
|
|
|
|
if (f < 0)
|
|
|
|
return 1;
|
|
|
|
close (1);
|
|
|
|
dup2 (f, 1);
|
|
|
|
}
|
|
|
|
if (p->_stderr) {
|
|
|
|
int f = open (p->_stderr, O_WRONLY);
|
|
|
|
if (f < 0)
|
|
|
|
return 1;
|
|
|
|
close (2);
|
|
|
|
dup2 (f, 2);
|
|
|
|
}
|
|
|
|
if (p->_aslr != -1)
|
|
|
|
setASLR (p->_aslr);
|
|
|
|
#if __UNIX__
|
|
|
|
set_limit (p->_docore, RLIMIT_CORE, RLIM_INFINITY);
|
|
|
|
if (p->_maxfd)
|
|
|
|
set_limit (p->_maxfd, RLIMIT_NOFILE, p->_maxfd);
|
|
|
|
#ifdef RLIMIT_NPROC
|
|
|
|
if (p->_maxproc)
|
|
|
|
set_limit (p->_maxproc, RLIMIT_NPROC, p->_maxproc);
|
|
|
|
#endif
|
|
|
|
if (p->_maxstack)
|
|
|
|
set_limit (p->_maxstack, RLIMIT_STACK, p->_maxstack);
|
|
|
|
#else
|
|
|
|
if (p->_docore || p->_maxfd || p->_maxproc || p->_maxstack)
|
|
|
|
eprintf ("Warning: setrlimits not supported for this platform\n");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (p->_connect) {
|
|
|
|
char *q = strchr (p->_connect, ':');
|
|
|
|
if (q) {
|
|
|
|
RSocket *fd = r_socket_new (0);
|
|
|
|
*q = 0;
|
|
|
|
if (!r_socket_connect_tcp (fd, p->_connect, q+1, 30)) {
|
|
|
|
eprintf ("Cannot connect\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
eprintf ("connected\n");
|
|
|
|
close (0);
|
|
|
|
close (1);
|
|
|
|
close (2);
|
|
|
|
dup2 (fd->fd, 0);
|
|
|
|
dup2 (fd->fd, 1);
|
|
|
|
dup2 (fd->fd, 2);
|
|
|
|
} else {
|
|
|
|
eprintf ("Invalid format for connect. missing ':'\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (p->_listen) {
|
|
|
|
RSocket *child, *fd = r_socket_new (0);
|
|
|
|
if (!r_socket_listen (fd, p->_listen, NULL)) {
|
|
|
|
eprintf ("rarun2: cannot listen\n");
|
|
|
|
r_socket_free (fd);
|
|
|
|
return 1;
|
|
|
|
}
|
2015-09-12 17:36:15 +00:00
|
|
|
while (R_TRUE) {
|
|
|
|
child = r_socket_accept (fd);
|
|
|
|
if (child) {
|
|
|
|
int is_child = R_TRUE;
|
|
|
|
|
|
|
|
if (p->_dofork && !p->_dodebug) {
|
|
|
|
pid_t child_pid = r_sys_fork ();
|
|
|
|
if (child_pid == -1) {
|
|
|
|
eprintf("rarun2: cannot fork\n");
|
|
|
|
r_socket_free (child);
|
|
|
|
r_socket_free (fd);
|
|
|
|
return 1;
|
|
|
|
} else if (child_pid != 0){
|
|
|
|
// parent code
|
|
|
|
is_child = R_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_child) {
|
|
|
|
r_socket_close_fd (fd);
|
|
|
|
eprintf ("connected\n");
|
|
|
|
close (0);
|
|
|
|
close (1);
|
|
|
|
close (2);
|
|
|
|
dup2 (child->fd, 0);
|
|
|
|
dup2 (child->fd, 1);
|
|
|
|
dup2 (child->fd, 2);
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
r_socket_close_fd (child);
|
|
|
|
}
|
|
|
|
}
|
2014-07-09 00:24:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (p->_r2sleep != 0) {
|
|
|
|
r_sys_sleep (p->_r2sleep);
|
|
|
|
}
|
|
|
|
if (p->_chgdir) {
|
|
|
|
ret = chdir (p->_chgdir);
|
|
|
|
if (ret < 0)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (p->_chroot) {
|
|
|
|
ret = chdir (p->_chroot);
|
|
|
|
if (ret < 0)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#if __UNIX__
|
|
|
|
if (p->_chroot) {
|
|
|
|
if (chroot (p->_chroot)) {
|
|
|
|
eprintf ("rarun2: cannot chroot\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2014-07-09 14:04:32 +00:00
|
|
|
chdir("/");
|
2014-07-09 00:24:31 +00:00
|
|
|
}
|
|
|
|
if (p->_setuid) {
|
2014-11-10 02:06:41 +00:00
|
|
|
ret = setgroups(0, NULL);
|
|
|
|
if (ret < 0)
|
|
|
|
return 1;
|
2014-07-09 00:24:31 +00:00
|
|
|
ret = setuid (atoi (p->_setuid));
|
|
|
|
if (ret < 0)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (p->_seteuid) {
|
|
|
|
ret = seteuid (atoi (p->_seteuid));
|
|
|
|
if (ret < 0)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (p->_setgid) {
|
|
|
|
ret = setgid (atoi (p->_setgid));
|
|
|
|
if (ret < 0)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (p->_input) {
|
|
|
|
int f2[2];
|
|
|
|
pipe (f2);
|
|
|
|
close (0);
|
|
|
|
dup2 (f2[0], 0);
|
|
|
|
parseinput (p->_input);
|
|
|
|
write (f2[1], p->_input, strlen (p->_input));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (p->_r2preload) {
|
|
|
|
if (p->_preload) {
|
|
|
|
eprintf ("WARNING: Only one library can be opened at a time\n");
|
|
|
|
}
|
|
|
|
p->_preload = R2_LIBDIR"/libr2."R_LIB_EXT;
|
|
|
|
}
|
|
|
|
if (p->_libpath) {
|
|
|
|
#if __WINDOWS__
|
|
|
|
eprintf ("rarun2: libpath unsupported for this platform\n");
|
|
|
|
#elif __HAIKU__
|
|
|
|
r_sys_setenv ("LIBRARY_PATH", p->_libpath);
|
|
|
|
#elif __APPLE__
|
|
|
|
r_sys_setenv ("DYLD_LIBRARY_PATH", p->_libpath);
|
|
|
|
#else
|
|
|
|
r_sys_setenv ("LD_LIBRARY_PATH", p->_libpath);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
if (p->_preload) {
|
|
|
|
#if __APPLE__
|
|
|
|
// 10.6
|
|
|
|
r_sys_setenv ("DYLD_PRELOAD", p->_preload);
|
|
|
|
r_sys_setenv ("DYLD_INSERT_LIBRARIES", p->_preload);
|
|
|
|
// 10.8
|
|
|
|
r_sys_setenv ("DYLD_FORCE_FLAT_NAMESPACE", "1");
|
|
|
|
#else
|
|
|
|
r_sys_setenv ("LD_PRELOAD", p->_preload);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
if (p->_timeout) {
|
|
|
|
#if __UNIX__
|
|
|
|
int mypid = getpid ();
|
2015-04-11 03:00:05 +00:00
|
|
|
if (!r_sys_fork ()) {
|
2014-07-09 00:24:31 +00:00
|
|
|
sleep (p->_timeout);
|
|
|
|
if (!kill (mypid, 0))
|
|
|
|
eprintf ("\nrarun2: Interrupted by timeout\n");
|
|
|
|
kill (mypid, SIGKILL);
|
|
|
|
exit (0);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
eprintf ("timeout not supported for this platform\n");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#if __APPLE__
|
|
|
|
posix_spawnattr_init (&attr);
|
|
|
|
if (p->_args[0]) {
|
2014-09-09 10:10:55 +00:00
|
|
|
char **envp = r_sys_get_environ();
|
2014-07-09 00:24:31 +00:00
|
|
|
ut32 spflags = 0; //POSIX_SPAWN_START_SUSPENDED;
|
|
|
|
spflags |= POSIX_SPAWN_SETEXEC;
|
|
|
|
if (p->_aslr == 0) {
|
|
|
|
#define _POSIX_SPAWN_DISABLE_ASLR 0x0100
|
|
|
|
spflags |= _POSIX_SPAWN_DISABLE_ASLR;
|
|
|
|
}
|
|
|
|
(void)posix_spawnattr_setflags (&attr, spflags);
|
2014-07-09 01:31:04 +00:00
|
|
|
if (p->_bits) {
|
|
|
|
size_t copied = 1;
|
|
|
|
cpu_type_t cpu;
|
|
|
|
#if __i386__ || __x86_64__
|
|
|
|
cpu = CPU_TYPE_I386;
|
|
|
|
if (p->_bits == 64)
|
|
|
|
cpu |= CPU_ARCH_ABI64;
|
|
|
|
#else
|
|
|
|
cpu = CPU_TYPE_ANY;
|
|
|
|
#endif
|
|
|
|
posix_spawnattr_setbinpref_np (
|
|
|
|
&attr, 1, &cpu, &copied);
|
|
|
|
}
|
2014-07-09 00:24:31 +00:00
|
|
|
ret = posix_spawnp (&pid, p->_args[0],
|
2014-09-09 10:10:55 +00:00
|
|
|
NULL, &attr, p->_args, envp);
|
2014-07-09 00:24:31 +00:00
|
|
|
switch (ret) {
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
case 22:
|
|
|
|
eprintf ("posix_spawnp: Invalid argument\n");
|
|
|
|
break;
|
|
|
|
case 86:
|
|
|
|
eprintf ("posix_spawnp: Unsupported architecture\n");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
eprintf ("posix_spawnp: unknown error %d\n", ret);
|
|
|
|
perror ("posix_spawnp");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
exit (ret);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (p->_system) {
|
2014-11-22 02:00:31 +00:00
|
|
|
if (p->_pid) {
|
|
|
|
eprintf ("PID: Cannot determine pid with 'system' directive. Use 'program'.\n");
|
|
|
|
}
|
2014-07-09 00:24:31 +00:00
|
|
|
exit (r_sys_cmd (p->_system));
|
|
|
|
}
|
|
|
|
if (p->_program) {
|
|
|
|
if (!r_file_exists (p->_program)) {
|
2014-11-18 10:19:04 +00:00
|
|
|
char *progpath = r_file_path (p->_program);
|
|
|
|
if (progpath && *progpath) {
|
|
|
|
free (p->_program);
|
|
|
|
p->_program = progpath;
|
|
|
|
} else {
|
2014-11-19 09:54:08 +00:00
|
|
|
free (progpath);
|
2014-11-18 10:19:04 +00:00
|
|
|
eprintf ("rarun2: %s: file not found\n", p->_program);
|
|
|
|
return 1;
|
|
|
|
}
|
2014-07-09 00:24:31 +00:00
|
|
|
}
|
2014-11-23 10:07:29 +00:00
|
|
|
#if __UNIX__
|
2014-10-23 02:45:01 +00:00
|
|
|
// XXX HACK close all non-tty fds
|
|
|
|
{ int i; for (i=3; i<10; i++) close (i); }
|
2014-07-09 00:24:31 +00:00
|
|
|
// TODO: use posix_spawn
|
2014-11-22 02:00:31 +00:00
|
|
|
if (p->_setgid) {
|
|
|
|
ret = setgid (atoi (p->_setgid));
|
|
|
|
if (ret < 0)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (p->_pid) {
|
|
|
|
eprintf ("PID: %d\n", getpid ());
|
|
|
|
}
|
2015-02-18 00:10:19 +00:00
|
|
|
if (p->_pidfile) {
|
|
|
|
char pidstr[32];
|
|
|
|
snprintf (pidstr, sizeof (pidstr), "%d\n", getpid ());
|
2015-04-03 02:04:46 +00:00
|
|
|
r_file_dump (p->_pidfile,
|
|
|
|
(const ut8*)pidstr,
|
|
|
|
strlen (pidstr), 0);
|
2015-02-18 00:10:19 +00:00
|
|
|
}
|
2014-11-23 10:07:29 +00:00
|
|
|
#endif
|
2015-03-26 14:19:12 +00:00
|
|
|
|
2015-04-23 23:16:57 +00:00
|
|
|
if (p->_nice) {
|
|
|
|
#if __UNIX__ && !defined(__HAIKU__)
|
|
|
|
if (nice (p->_nice) == -1) {
|
|
|
|
return 1;
|
|
|
|
}
|
2015-03-26 14:19:12 +00:00
|
|
|
#else
|
2015-04-23 23:16:57 +00:00
|
|
|
eprintf ("nice not supported for this platform\n");
|
2015-03-26 14:19:12 +00:00
|
|
|
#endif
|
2015-04-23 23:16:57 +00:00
|
|
|
}
|
2014-07-21 22:24:37 +00:00
|
|
|
exit (execv (p->_program, (char* const*)p->_args));
|
2014-07-09 00:24:31 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|