Skip to content

process: add execve #56496

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

Closed
wants to merge 4 commits into from
Closed
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
9 changes: 9 additions & 0 deletions doc/api/diagnostics_channel.md
Original file line number Diff line number Diff line change
@@ -1319,6 +1319,14 @@ added: v16.18.0

Emitted when a new process is created.

`execve`

* `execPath` {string}
* `args` {string\[]}
* `env` {string\[]}

Emitted when [`process.execve()`][] is invoked.

#### Worker Thread

<!-- YAML
@@ -1348,5 +1356,6 @@ Emitted when a new thread is created.
[`end` event]: #endevent
[`error` event]: #errorevent
[`net.Server.listen()`]: net.md#serverlisten
[`process.execve()`]: process.md#processexecvefile-args-env
[`start` event]: #startevent
[context loss]: async_context.md#troubleshooting-context-loss
30 changes: 28 additions & 2 deletions doc/api/process.md
Original file line number Diff line number Diff line change
@@ -2516,8 +2516,7 @@ if (process.getuid) {
}
```
This function is only available on POSIX platforms (i.e. not Windows or
Android).
This function not available on Windows.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this change related to this PR? Could this land separately, and should we update the YAML history section?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was already like that but docs were not updated so I did it here.

## `process.hasUncaughtExceptionCaptureCallback()`
@@ -3343,6 +3342,33 @@ In custom builds from non-release versions of the source tree, only the
`name` property may be present. The additional properties should not be
relied upon to exist.
## `process.execve(file[, args[, env]])`
<!-- YAML
added: REPLACEME
-->
> Stability: 1 - Experimental
* `file` {string} The name or path of the executable file to run.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit... not in this PR but as a separate follow on we should likely align this with the fs module handling of paths so that file can be represented using a Buffer or URL also.

* `args` {string\[]} List of string arguments. No argument can contain a null-byte (`\u0000`).
* `env` {Object} Environment key-value pairs.
No key or value can contain a null-byte (`\u0000`).
**Default:** `process.env`.
Replaces the current process with a new process.
This is achieved by using the `execve` POSIX function and therefore no memory or other
resources from the current process are preserved, except for the standard input,
standard output and standard error file descriptor.
All other resources are discarded by the system when the processes are swapped, without triggering
any exit or close events and without running any cleanup handler.
This function will never return, unless an error occurred.
This function is only available on POSIX platforms (i.e. not Windows or Android).
## `process.report`
<!-- YAML
1 change: 1 addition & 0 deletions lib/internal/bootstrap/node.js
Original file line number Diff line number Diff line change
@@ -177,6 +177,7 @@ const rawMethods = internalBinding('process_methods');
process.availableMemory = rawMethods.availableMemory;
process.kill = wrapped.kill;
process.exit = wrapped.exit;
process.execve = wrapped.execve;
process.ref = perThreadSetup.ref;
process.unref = perThreadSetup.unref;

60 changes: 60 additions & 0 deletions lib/internal/process/per_thread.js
Original file line number Diff line number Diff line change
@@ -16,6 +16,7 @@ const {
FunctionPrototypeCall,
NumberMAX_SAFE_INTEGER,
ObjectDefineProperty,
ObjectEntries,
ObjectFreeze,
ReflectApply,
RegExpPrototypeExec,
@@ -24,6 +25,7 @@ const {
SetPrototypeEntries,
SetPrototypeValues,
StringPrototypeEndsWith,
StringPrototypeIncludes,
StringPrototypeReplace,
StringPrototypeSlice,
Symbol,
@@ -34,20 +36,27 @@ const {
const {
ErrnoException,
codes: {
ERR_FEATURE_UNAVAILABLE_ON_PLATFORM,
ERR_INVALID_ARG_TYPE,
ERR_INVALID_ARG_VALUE,
ERR_OPERATION_FAILED,
ERR_OUT_OF_RANGE,
ERR_UNKNOWN_SIGNAL,
ERR_WORKER_UNSUPPORTED_OPERATION,
},
} = require('internal/errors');
const { emitExperimentalWarning } = require('internal/util');
const format = require('internal/util/inspect').format;
const {
validateArray,
validateNumber,
validateObject,
validateString,
} = require('internal/validators');

const dc = require('diagnostics_channel');
const execveDiagnosticChannel = dc.channel('process.execve');

const constants = internalBinding('constants').os.signals;

let getValidatedPath; // We need to lazy load it because of the circular dependency.
@@ -103,6 +112,7 @@ function wrapProcessMethods(binding) {
rss,
resourceUsage: _resourceUsage,
loadEnvFile: _loadEnvFile,
execve: _execve,
} = binding;

function _rawDebug(...args) {
@@ -269,6 +279,55 @@ function wrapProcessMethods(binding) {
return true;
}

function execve(execPath, args, env) {
emitExperimentalWarning('process.execve');

const { isMainThread } = require('internal/worker');

if (!isMainThread) {
throw new ERR_WORKER_UNSUPPORTED_OPERATION('Calling process.execve');
} else if (process.platform === 'win32') {
throw new ERR_FEATURE_UNAVAILABLE_ON_PLATFORM('process.execve');
}

validateString(execPath, 'execPath');
validateArray(args, 'args');

for (let i = 0; i < args.length; i++) {
const arg = args[i];
if (typeof arg !== 'string' || StringPrototypeIncludes(arg, '\u0000')) {
throw new ERR_INVALID_ARG_VALUE(`args[${i}]`, arg, 'must be a string without null bytes');
}
}

const envArray = [];
if (env !== undefined) {
validateObject(env, 'env');

for (const { 0: key, 1: value } of ObjectEntries(env)) {
if (
typeof key !== 'string' ||
typeof value !== 'string' ||
StringPrototypeIncludes(key, '\u0000') ||
StringPrototypeIncludes(value, '\u0000')
) {
throw new ERR_INVALID_ARG_VALUE(
'env', env, 'must be an object with string keys and values without null bytes',
);
} else {
ArrayPrototypePush(envArray, `${key}=${value}`);
}
}
}

if (execveDiagnosticChannel.hasSubscribers) {
execveDiagnosticChannel.publish({ execPath, args, env: envArray });
}

// Perform the system call
_execve(execPath, args, envArray);
}

const resourceValues = new Float64Array(16);
function resourceUsage() {
_resourceUsage(resourceValues);
@@ -314,6 +373,7 @@ function wrapProcessMethods(binding) {
memoryUsage,
kill,
exit,
execve,
loadEnvFile,
};
}
5 changes: 5 additions & 0 deletions src/node_errors.h
Original file line number Diff line number Diff line change
@@ -13,6 +13,11 @@
#include <sstream>

namespace node {
// This forward declaration is required to have the method
// available in error messages.
namespace errors {
const char* errno_string(int errorno);
}

enum ErrorHandlingMode { CONTEXTIFY_ERROR, FATAL_ERROR, MODULE_ERROR };
void AppendExceptionLine(Environment* env,
99 changes: 99 additions & 0 deletions src/node_process_methods.cc
Original file line number Diff line number Diff line change
@@ -27,12 +27,14 @@
#if defined(_MSC_VER)
#include <direct.h>
#include <io.h>
#include <process.h>
#define umask _umask
typedef int mode_t;
#else
#include <pthread.h>
#include <sys/resource.h> // getrlimit, setrlimit
#include <termios.h> // tcgetattr, tcsetattr
#include <unistd.h>
#endif

namespace node {
@@ -495,6 +497,95 @@ static void ReallyExit(const FunctionCallbackInfo<Value>& args) {
env->Exit(code);
}

#ifdef __POSIX__
inline int persist_standard_stream(int fd) {
int flags = fcntl(fd, F_GETFD, 0);

if (flags < 0) {
return flags;
}

flags &= ~FD_CLOEXEC;
return fcntl(fd, F_SETFD, flags);
}

static void Execve(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
Isolate* isolate = env->isolate();
Local<Context> context = env->context();

THROW_IF_INSUFFICIENT_PERMISSIONS(
env, permission::PermissionScope::kChildProcess, "");

CHECK(args[0]->IsString());
CHECK(args[1]->IsArray());
CHECK(args[2]->IsArray());

Local<Array> argv_array = args[1].As<Array>();
Local<Array> envp_array = args[2].As<Array>();

// Copy arguments and environment
Utf8Value executable(isolate, args[0]);
std::vector<std::string> argv_strings(argv_array->Length());
std::vector<std::string> envp_strings(envp_array->Length());
std::vector<char*> argv(argv_array->Length() + 1);
std::vector<char*> envp(envp_array->Length() + 1);

for (unsigned int i = 0; i < argv_array->Length(); i++) {
Local<Value> str;
if (!argv_array->Get(context, i).ToLocal(&str)) {
THROW_ERR_INVALID_ARG_VALUE(env, "Failed to deserialize argument.");
return;
}

argv_strings[i] = Utf8Value(isolate, str).ToString();
argv[i] = argv_strings[i].data();
}
argv[argv_array->Length()] = nullptr;

for (unsigned int i = 0; i < envp_array->Length(); i++) {
Local<Value> str;
if (!envp_array->Get(context, i).ToLocal(&str)) {
THROW_ERR_INVALID_ARG_VALUE(
env, "Failed to deserialize environment variable.");
return;
}

envp_strings[i] = Utf8Value(isolate, str).ToString();
envp[i] = envp_strings[i].data();
}

envp[envp_array->Length()] = nullptr;

// Set stdin, stdout and stderr to be non-close-on-exec
// so that the new process will inherit it.
if (persist_standard_stream(0) < 0 || persist_standard_stream(1) < 0 ||
persist_standard_stream(2) < 0) {
env->ThrowErrnoException(errno, "fcntl");
return;
}

// Perform the execve operation.
RunAtExit(env);
execve(*executable, argv.data(), envp.data());

// If it returns, it means that the execve operation failed.
// In that case we abort the process.
auto error_message = std::string("process.execve failed with error code ") +
errors::errno_string(errno);

// Abort the process
Local<v8::Value> exception =
ErrnoException(isolate, errno, "execve", *executable);
Local<v8::Message> message = v8::Exception::CreateMessage(isolate, exception);

std::string info = FormatErrorMessage(
isolate, context, error_message.c_str(), message, true);
FPrintF(stderr, "%s\n", info);
ABORT();
}
#endif

static void LoadEnvFile(const v8::FunctionCallbackInfo<v8::Value>& args) {
Environment* env = Environment::GetCurrent(args);
std::string path = ".env";
@@ -687,6 +778,10 @@ static void CreatePerIsolateProperties(IsolateData* isolate_data,
SetMethodNoSideEffect(isolate, target, "cwd", Cwd);
SetMethod(isolate, target, "dlopen", binding::DLOpen);
SetMethod(isolate, target, "reallyExit", ReallyExit);

#ifdef __POSIX__
SetMethod(isolate, target, "execve", Execve);
#endif
SetMethodNoSideEffect(isolate, target, "uptime", Uptime);
SetMethod(isolate, target, "patchProcessObject", PatchProcessObject);

@@ -730,6 +825,10 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(Cwd);
registry->Register(binding::DLOpen);
registry->Register(ReallyExit);

#ifdef __POSIX__
registry->Register(Execve);
#endif
registry->Register(Uptime);
registry->Register(PatchProcessObject);

26 changes: 26 additions & 0 deletions test/parallel/test-process-execve-abort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict';

const { skip, isWindows } = require('../common');
const { ok } = require('assert');
const { spawnSync } = require('child_process');
const { isMainThread } = require('worker_threads');

if (!isMainThread) {
skip('process.execve is not available in Workers');
} else if (isWindows) {
skip('process.execve is not available in Windows');
}

if (process.argv[2] === 'child') {
process.execve(
process.execPath + '_non_existing',
[__filename, 'replaced'],
{ ...process.env, EXECVE_A: 'FIRST', EXECVE_B: 'SECOND', CWD: process.cwd() }
);
} else {
const child = spawnSync(`${process.execPath}`, [`${__filename}`, 'child']);
const stderr = child.stderr.toString();

ok(stderr.includes('process.execve failed with error code ENOENT'), stderr);
ok(stderr.includes('execve (node:internal/process/per_thread'), stderr);
}
18 changes: 18 additions & 0 deletions test/parallel/test-process-execve-on-exit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

const { mustNotCall, skip, isWindows } = require('../common');
const { strictEqual } = require('assert');
const { isMainThread } = require('worker_threads');

if (!isMainThread) {
skip('process.execve is not available in Workers');
} else if (isWindows) {
skip('process.execve is not available in Windows');
}

if (process.argv[2] === 'replaced') {
strictEqual(process.argv[2], 'replaced');
} else {
process.on('exit', mustNotCall());
process.execve(process.execPath, [process.execPath, __filename, 'replaced'], process.env);
}
Loading