X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/a42871173b94b0ca82c1db9b97c5da82fc3867cb..refs/heads/master:/Inject.cpp diff --git a/Inject.cpp b/Inject.cpp index 95610d2..a451b3d 100644 --- a/Inject.cpp +++ b/Inject.cpp @@ -1,5 +1,5 @@ -/* Cycript - Optimizing JavaScript Compiler/Runtime - * Copyright (C) 2009-2014 Jay Freeman (saurik) +/* Cycript - The Truly Universal Scripting Language + * Copyright (C) 2009-2016 Jay Freeman (saurik) */ /* GNU Affero General Public License, Version 3 {{{ */ @@ -28,6 +28,8 @@ #include "Pooling.hpp" #if defined(__APPLE__) && (defined(__i386__) || defined(__x86_64__)) +#include + #include #include @@ -53,7 +55,7 @@ Type_ *shift(Type_ *data, size_t size) { return reinterpret_cast(reinterpret_cast(data) + size); } -void InjectLibrary(int pid, int argc, const char *argv[]) { +void InjectLibrary(int pid, std::ostream &stream, int argc, const char *const argv[]) { auto cynject(LibraryFor(reinterpret_cast(&main))); auto slash(cynject.rfind('/')); _assert(slash != std::string::npos); @@ -65,7 +67,8 @@ void InjectLibrary(int pid, int argc, const char *argv[]) { off_t offset; _assert(csops(pid, CS_OPS_PIDOFFSET, &offset, sizeof(offset)) != -1); - char path[PATH_MAX]; + // XXX: implement a safe version of this + char path[4096]; int writ(proc_pidpath(pid, path, sizeof(path))); _assert(writ != 0); @@ -110,8 +113,32 @@ void InjectLibrary(int pid, int argc, const char *argv[]) { std::ostringstream inject; inject << cynject << " " << std::dec << pid << " " << library; - for (decltype(argc) i(0); i != argc; ++i) - inject << " " << argv[i]; + for (decltype(argc) i(0); i != argc; ++i) { + inject << " '"; + for (const char *arg(argv[i]); *arg != '\0'; ++arg) + if (*arg != '\'') + inject.put(*arg); + else + inject << "'\\''"; + inject << "'"; + } + + FILE *process(popen(inject.str().c_str(), "r")); + _assert(process != NULL); + + for (;;) { + char data[1024]; + auto writ(fread(data, 1, sizeof(data), process)); + stream.write(data, writ); + + if (writ == sizeof(data)) + continue; + _assert(!ferror(process)); + if (feof(process)) + break; + } - _assert(system(inject.str().c_str()) == 0); + auto status(pclose(process)); // XXX: _scope (sort of?) + _assert(status != -1); + _assert(status == 0); }