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

fix: ensure program name is part of child process arguments #577

Merged
merged 1 commit into from
Feb 2, 2023
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
23 changes: 12 additions & 11 deletions src/unix/pty.cc
Original file line number Diff line number Diff line change
Expand Up @@ -178,18 +178,23 @@ NAN_METHOD(PtyFork) {
bool closeFDs = Nan::To<bool>(info[8]).FromJust();
bool explicitlyCloseFDs = closeFDs && !HAVE_POSIX_SPAWN_CLOEXEC_DEFAULT;

// helperPath
Nan::Utf8String helper_path_(info[11]);
char *helper_path = strdup(*helper_path_);

// args
v8::Local<v8::Array> argv_ = v8::Local<v8::Array>::Cast(info[1]);

const int EXTRA_ARGS = 5;
const int EXTRA_ARGS = 6;
int argc = argv_->Length();
int argl = argc + EXTRA_ARGS + 1;
char **argv = new char*[argl];
argv[0] = strdup(*cwd_);
argv[1] = strdup(std::to_string(uid).c_str());
argv[2] = strdup(std::to_string(gid).c_str());
argv[3] = strdup(explicitlyCloseFDs ? "1": "0");
argv[4] = strdup(*file);
argv[0] = strdup(helper_path);
argv[1] = strdup(*cwd_);
argv[2] = strdup(std::to_string(uid).c_str());
argv[3] = strdup(std::to_string(gid).c_str());
argv[4] = strdup(explicitlyCloseFDs ? "1": "0");
argv[5] = strdup(*file);
argv[argl - 1] = NULL;
for (int i = 0; i < argc; i++) {
Nan::Utf8String arg(Nan::Get(argv_, i).ToLocalChecked());
Expand Down Expand Up @@ -241,10 +246,6 @@ NAN_METHOD(PtyFork) {
cfsetispeed(term, B38400);
cfsetospeed(term, B38400);

// helperPath
Nan::Utf8String helper_path_(info[11]);
char *helper_path = strdup(*helper_path_);

sigset_t newmask, oldmask;
int flags = POSIX_SPAWN_USEVFORK;

Expand Down Expand Up @@ -287,7 +288,7 @@ NAN_METHOD(PtyFork) {

{ // suppresses "jump bypasses variable initialization" errors
pid_t pid;
auto error = posix_spawn(&pid, helper_path, &acts, &attrs, argv, env);
auto error = posix_spawn(&pid, argv[0], &acts, &attrs, argv, env);

close(comms_pipe[1]);

Expand Down
12 changes: 6 additions & 6 deletions src/unix/spawn-helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ int main (int argc, char** argv) {
close(open(slave_path, O_RDWR));
#endif

char *cwd = argv[0];
int uid = std::stoi(argv[1]);
int gid = std::stoi(argv[2]);
bool closeFDs = std::stoi(argv[3]);
char *file = argv[4];
argv = &argv[4];
char *cwd = argv[1];
int uid = std::stoi(argv[2]);
int gid = std::stoi(argv[3]);
bool closeFDs = std::stoi(argv[4]);
char *file = argv[5];
argv = &argv[5];

fcntl(COMM_PIPE_FD, F_SETFD, FD_CLOEXEC);

Expand Down