Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

child_process: add safety checks on stdio accesses #8779

Closed
wants to merge 1 commit 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
76 changes: 45 additions & 31 deletions lib/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -741,14 +741,23 @@ exports.execFile = function(file /* args, options, callback */) {

function errorhandler(e) {
ex = e;
child.stdout.destroy();
child.stderr.destroy();

if (child.stdout)
child.stdout.destroy();

if (child.stderr)
child.stderr.destroy();

exithandler();
}

function kill() {
child.stdout.destroy();
child.stderr.destroy();

if (child.stdout)
child.stdout.destroy();

if (child.stderr)
child.stderr.destroy();

killed = true;
try {
Expand All @@ -766,37 +775,42 @@ exports.execFile = function(file /* args, options, callback */) {
}, options.timeout);
}

child.stdout.addListener('data', function(chunk) {
stdoutLen += chunk.length;
if (child.stdout) {
if (encoding)
child.stdout.setEncoding(encoding);

if (stdoutLen > options.maxBuffer) {
ex = new Error('stdout maxBuffer exceeded.');
kill();
} else {
if (!encoding)
_stdout.push(chunk);
else
_stdout += chunk;
}
});
child.stdout.addListener('data', function(chunk) {
stdoutLen += chunk.length;

child.stderr.addListener('data', function(chunk) {
stderrLen += chunk.length;
if (stdoutLen > options.maxBuffer) {
ex = new Error('stdout maxBuffer exceeded.');
kill();
} else {
if (!encoding)
_stdout.push(chunk);
else
_stdout += chunk;
}
});
}

if (stderrLen > options.maxBuffer) {
ex = new Error('stderr maxBuffer exceeded.');
kill();
} else {
if (!encoding)
_stderr.push(chunk);
else
_stderr += chunk;
}
});
if (child.stderr) {
if (encoding)
child.stderr.setEncoding(encoding);

child.stderr.addListener('data', function(chunk) {
stderrLen += chunk.length;

if (encoding) {
child.stderr.setEncoding(encoding);
child.stdout.setEncoding(encoding);
if (stderrLen > options.maxBuffer) {
ex = new Error('stderr maxBuffer exceeded.');
kill();
} else {
if (!encoding)
_stderr.push(chunk);
else
_stderr += chunk;
}
});
}

child.addListener('close', exithandler);
Expand Down