Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor how ASLR and implement rz_sys_execv using RzSubprocess #4202

Merged
merged 4 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file removed librz/include/rz_util/rz_queue.h
Empty file.
4 changes: 2 additions & 2 deletions librz/include/rz_util/rz_sys.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ RZ_API int rz_sys_pipe_close(int fd);
#define rz_sys_pipe_close close
#endif
#if !HAVE_EXECV || (__UNIX__ && HAVE_EXECV && HAVE_PIPE && !HAVE_PIPE2)
RZ_API int rz_sys_execv(const char *pathname, char *const argv[]);
RZ_API int rz_sys_execv(RZ_NONNULL const char *pathname, RZ_NONNULL char *const argv[]);
#else
#define rz_sys_execv execv
#endif
Expand Down Expand Up @@ -106,7 +106,7 @@ RZ_API int rz_sys_clearenv(void);
RZ_API char *rz_sys_whoami(char *buf);
RZ_API char *rz_sys_getdir(void);
RZ_API bool rz_sys_chdir(RZ_NONNULL const char *s);
RZ_API bool rz_sys_aslr(int val);
RZ_API bool rz_sys_aslr(bool enable);
RZ_API void *rz_sys_dlopen(RZ_NULLABLE const char *libname);
RZ_API void *rz_sys_dlsym(void *handler, const char *name);
RZ_API int rz_sys_dlclose(void *handler);
Expand Down
6 changes: 5 additions & 1 deletion librz/main/rz-run.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ RZ_API int rz_main_rz_run(int argc, const char **argv) {
rz_run_tty();
return 0;
#else
eprintf("Not supported\n");
RZ_LOG_ERROR("Not supported\n");
return 1;
#endif
}
Expand All @@ -49,6 +49,10 @@ RZ_API int rz_main_rz_run(int argc, const char **argv) {
bool noMoreDirectives = false;
int directiveIndex = 0;
p = rz_run_new(NULL);
if (!p) {
RZ_LOG_ERROR("Failed to create new RzRunProfile\n");
return 1;
}
for (i = *file ? 1 : 2; i < argc; i++) {
if (!strcmp(argv[i], "--")) {
noMoreDirectives = true;
Expand Down
76 changes: 10 additions & 66 deletions librz/socket/run.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,8 @@
#include <errno.h>
#if defined(__sun)
#include <sys/filio.h>
#endif
#if __linux__ && !__ANDROID__
#include <sys/personality.h>
#endif
#if defined(__FreeBSD__) || defined(__DragonFly__)
#if HAVE_DECL_PROCCTL_ASLR_CTL
#include <sys/procctl.h>
#endif
#include <sys/sysctl.h>
#endif
#endif
#endif /* __sun */
#endif /* __UNIX__ */
#ifdef _MSC_VER
#include <direct.h> // to compile chdir in msvc windows
#include <process.h> // to compile execv in msvc windows
Expand Down Expand Up @@ -292,44 +283,6 @@ static bool parse_bool(const char *value) {
!rz_str_casecmp(value, "1");
}

// TODO: move into rz_util? rz_run_... ? with the rest of funcs?
static void setASLR(RzRunProfile *r, int enabled) {
#if __linux__
rz_sys_aslr(enabled);
#if HAVE_DECL_ADDR_NO_RANDOMIZE && !__ANDROID__
if (personality(ADDR_NO_RANDOMIZE) == -1) {
#endif
rz_sys_aslr(0);
#if HAVE_DECL_ADDR_NO_RANDOMIZE && !__ANDROID__
}
#endif
#elif __APPLE__
// TOO OLD setenv ("DYLD_NO_PIE", "1", 1);
// disable this because its
const char *argv0 = r->_system ? r->_system
: r->_program ? r->_program
: r->_args[0] ? r->_args[0]
: "/path/to/exec";
RZ_LOG_WARN("rz-run: to disable aslr patch mach0.hdr.flags with:\n"
"rizin -qwnc 'wx 000000 @ 0x18' %s\n",
argv0);
// f MH_PIE=0x00200000; wB-MH_PIE @ 24\n");
// for osxver>=10.7
// "unset the MH_PIE bit in an already linked executable" with --no-pie flag of the script
// the right way is to disable the aslr bit in the spawn call
#elif __FreeBSD__ || __NetBSD__ || __DragonFly__
rz_sys_aslr(enabled);
#if HAVE_DECL_PROCCTL_ASLR_CTL
int disabled = PROC_ASLR_FORCE_DISABLE;
if (procctl(P_PID, getpid(), PROC_ASLR_CTL, &disabled) == -1) {
rz_sys_aslr(0);
}
#endif
#else
// not supported for this platform
#endif
}

#if __APPLE__
#else
#if HAVE_OPENPTY && HAVE_FORKPTY && HAVE_LOGIN_TTY
Expand Down Expand Up @@ -906,7 +859,7 @@ RZ_API int rz_run_config_env(RzRunProfile *p) {
return 1;
}
if (p->_aslr != -1) {
setASLR(p, p->_aslr);
rz_sys_aslr(p->_aslr);
}
#if __UNIX__
set_limit(p->_docore, RLIMIT_CORE, RLIM_INFINITY);
Expand Down Expand Up @@ -1154,23 +1107,23 @@ RZ_API int rz_run_config_env(RzRunProfile *p) {

// NOTE: return value is like in unix return code (0 = ok, 1 = not ok)
RZ_API int rz_run_start(RzRunProfile *p) {
#if HAVE_EXECVE
if (p->_execve) {
exit(rz_sys_execv(p->_program, (char *const *)p->_args));
}
#endif
#if __APPLE__ && HAVE_FORK
posix_spawnattr_t attr = { 0 };
pid_t pid = -1;
int ret;
posix_spawnattr_init(&attr);
if (p->_args[0]) {
char **envp = rz_sys_get_environ();
ut32 spflags = 0; // POSIX_SPAWN_START_SUSPENDED;
spflags |= POSIX_SPAWN_SETEXEC;
if (p->_aslr == 0) {
short spflags = POSIX_SPAWN_SETEXEC;

// https://opensource.apple.com/source/gdb/gdb-2831/src/gdb/macosx/macosx-nat-inferior.c.auto.html
if (p->_aslr != -1 && p->_aslr) {
#define _POSIX_SPAWN_DISABLE_ASLR 0x0100
spflags |= _POSIX_SPAWN_DISABLE_ASLR;
#undef _POSIX_SPAWN_DISABLE_ASLR
}
(void)posix_spawnattr_setflags(&attr, spflags);
if (p->_bits) {
Expand All @@ -1187,14 +1140,9 @@ RZ_API int rz_run_start(RzRunProfile *p) {
posix_spawnattr_setbinpref_np(
&attr, 1, &cpu, &copied);
}
ret = posix_spawnp(&pid, p->_args[0],
NULL, &attr, p->_args, envp);
switch (ret) {
case 0:
break;
default:
ret = posix_spawnp(&pid, p->_args[0], NULL, &attr, p->_args, envp);
if (ret) {
RZ_LOG_ERROR("rz-run: posix_spawnp: %s\n", strerror(ret));
break;
}
exit(ret);
}
Expand Down Expand Up @@ -1328,14 +1276,10 @@ RZ_API int rz_run_start(RzRunProfile *p) {
}
}
setsid();
#if HAVE_EXECVE
exit(rz_sys_execv(p->_program, (char *const *)p->_args));
#endif
#endif
}
#if HAVE_EXECVE
exit(rz_sys_execv(p->_program, (char *const *)p->_args));
#endif
}
if (p->_runlib) {
if (!p->_runlib_fcn) {
Expand Down
Loading
Loading