Skip to content
This repository has been archived by the owner on Jan 3, 2024. It is now read-only.

Commit

Permalink
boundary-less payload for 0.12.18
Browse files Browse the repository at this point in the history
  • Loading branch information
igorklopov committed May 27, 2017
1 parent 8a8deb5 commit 7263fad
Showing 1 changed file with 52 additions and 49 deletions.
101 changes: 52 additions & 49 deletions patches/node.v0.12.18.patch
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@

--- node/src/node.js
+++ node/src/node.js
@@ -65,10 +65,55 @@
@@ -65,10 +65,57 @@
// There are various modes that Node can run in. The most common two
// are running from a script and running the REPL - but there are a few
// others like the debugger or running --eval arguments. Here we decide
Expand All @@ -211,12 +211,14 @@
+ var fs = NativeModule.require('fs');
+ var vm = NativeModule.require('vm');
+ function readPrelude (fd) {
+ var PAYLOAD_POSITION = process.env.PKG_PAYLOAD_POSITION | 0;
+ var PAYLOAD_SIZE = process.env.PKG_PAYLOAD_SIZE | 0;
+ var PRELUDE_POSITION = process.env.PKG_PRELUDE_POSITION | 0;
+ var PRELUDE_SIZE = process.env.PKG_PRELUDE_SIZE | 0;
+ var PAYLOAD_POSITION = process.env.PKG_PAYLOAD_POSITION | 0;
+ delete process.env.PKG_PAYLOAD_POSITION;
+ delete process.env.PKG_PAYLOAD_SIZE;
+ delete process.env.PKG_PRELUDE_POSITION;
+ delete process.env.PKG_PRELUDE_SIZE;
+ delete process.env.PKG_PAYLOAD_POSITION;
+ if (!PRELUDE_POSITION) {
+ // no prelude - remove entrypoint from argv[1]
+ process.argv.splice(1, 1);
Expand All @@ -235,8 +237,8 @@
+ }
+ var s = new vm.Script(prelude, { filename: 'pkg/prelude/bootstrap.js' });
+ var fn = s.runInThisContext();
+ return fn(process, NativeModule.require, console, fd,
+ PAYLOAD_POSITION, PRELUDE_POSITION - PAYLOAD_POSITION - 16);
+ return fn(process, NativeModule.require,
+ console, fd, PAYLOAD_POSITION, PAYLOAD_SIZE);
+ }
+ (function () {
+ var fd = fs.openSync(process.execPath, 'r');
Expand Down Expand Up @@ -333,7 +335,7 @@
TryCatch& try_catch) {
--- node/src/node_main.cc
+++ node/src/node_main.cc
@@ -19,10 +19,291 @@
@@ -19,10 +19,292 @@
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.

Expand All @@ -342,8 +344,6 @@
+#include <string.h>
+#include "uv.h"
+
+#define BOUNDARY 4096
+
+uint16_t read16(uint8_t* buffer, uint32_t pos) {
+ buffer = &buffer[pos];
+ uint16_t* buffer16 = (uint16_t*) buffer;
Expand Down Expand Up @@ -441,38 +441,30 @@
+ exit(1);
+}
+
+bool GetSentryPosition(FILE* file, int start, uint32_t s1,
+bool FindNextBlock(FILE* file, int start, uint32_t s1,
+ uint32_t s12, uint32_t s3, int* pposition, int* psize
+) {
+ int read;
+ uint32_t sentry, length;
+ uint8_t probe[4096];
+ uint32_t* psentry;
+
+ if (fseek(file, start, SEEK_SET) != 0) return false;
+
+ while (true) {
+ read = static_cast<int>(fread(&sentry, 1, sizeof(sentry), file));
+ if (read != sizeof(sentry)) return false;
+ if (sentry != s1) {
+ fseek(file, BOUNDARY - 4, SEEK_CUR);
+ continue;
+ }
+ fread(&length, 1, sizeof(length), file);
+ if ((sentry^length) != s12) {
+ fseek(file, BOUNDARY - 8, SEEK_CUR);
+ continue;
+ }
+ fread(&sentry, 1, sizeof(sentry), file);
+ if (sentry != s3) {
+ fseek(file, BOUNDARY - 12, SEEK_CUR);
+ continue;
+ }
+ break;
+ read = static_cast<int>(fread(&probe, 1, sizeof(probe), file));
+
+ for (int i = 0; i < read - 16; i += 1) {
+ psentry = (uint32_t*) (probe + i);
+ if (*psentry != s1) continue;
+ psentry += 1;
+ if (((*psentry)^s1) != s12) continue;
+ psentry += 1;
+ if (*psentry != s3) continue;
+ psentry += 1;
+ *pposition = start + i + 16;
+ *psize = *psentry;
+ return true;
+ }
+
+ fread(&length, 1, sizeof(length), file);
+ *pposition = ftell(file);
+ *psize = static_cast<int>(length);
+ return true;
+ return false;
+}
+
+
Expand Down Expand Up @@ -507,39 +499,50 @@
+ exit(1);
+ }
+
+ char env[64];
+ int position = (FindMeatEnd(file) / BOUNDARY) * BOUNDARY; int size;
+ int position = FindMeatEnd(file);
+ int size;
+ char* bakery = NULL;
+ char env[64];
+
+ if (GetSentryPosition(file, position, 0x4818c4df,
+ if (FindNextBlock(file, position, 0x4818c4df,
+ 0x32dbc2af, 0x56558a76, &position, &size)
+ ) {
+ bakery = static_cast<char*>(malloc(size));
+ int read;
+
+ for (int i = 0; i < size;) {
+ read = static_cast<int>(fread(&bakery[i], 1, size - i, file));
+ if (ferror(file) != 0) {
+ if (size) {
+ if (fseek(file, position, SEEK_SET) != 0) {
+ fprintf(stderr, "Pkg: Error reading from file.\n");
+ fclose(file);
+ exit(1);
+ }
+ i += read;
+ }
+
+ position -= 16; // align back to boundary
+ bakery = static_cast<char*>(malloc(size));
+ int read;
+
+ for (int i = 0; i < size;) {
+ read = static_cast<int>(fread(&bakery[i], 1, size - i, file));
+ if (ferror(file) != 0) {
+ fprintf(stderr, "Pkg: Error reading from file.\n");
+ fclose(file);
+ exit(1);
+ }
+ i += read;
+ }
+
+ position += size;
+ }
+ }
+
+ if (GetSentryPosition(file, position, 0x75148eba,
+ if (FindNextBlock(file, position, 0x75148eba,
+ 0x1aa9270e, 0x2e20c08d, &position, &size)
+ ) {
+ sprintf(env, "%d", position);
+ setenv("PKG_PAYLOAD_POSITION", env, 1);
+ sprintf(env, "%d", size);
+ setenv("PKG_PAYLOAD_SIZE", env, 1);
+
+ position -= 16; // align back to boundary
+ position += size;
+ }
+
+ if (GetSentryPosition(file, position, 0x26e0c928,
+ if (FindNextBlock(file, position, 0x26e0c928,
+ 0x6713e24e, 0x3ea13ccf, &position, &size)
+ ) {
+ sprintf(env, "%d", position);
Expand Down Expand Up @@ -625,7 +628,7 @@
// Convert argv to to UTF8
char** argv = new char*[argc];
for (int i = 0; i < argc; i++) {
@@ -55,13 +336,13 @@
@@ -55,13 +337,13 @@
fprintf(stderr, "Could not convert arguments to utf8.");
exit(1);
}
Expand Down

0 comments on commit 7263fad

Please sign in to comment.