Skip to content

Do not inherit open FDs to SSH child process #2

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

Merged
merged 1 commit into from
Dec 6, 2018
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
25 changes: 21 additions & 4 deletions src/SshProcessConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,27 @@ public function connect($uri)
return \React\Promise\reject(new \InvalidArgumentException('Invalid target URI'));
}

$process = new Process($this->cmd . ' -W ' . \escapeshellarg($parts['host'] . ':' . $parts['port']));
$command = $this->cmd . ' -W ' . \escapeshellarg($parts['host'] . ':' . $parts['port']);

// try to get list of all open FDs (Linux only) or simply assume range 3-1024 (FD_SETSIZE)
$fds = @scandir('/proc/self/fd');

Choose a reason for hiding this comment

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

FYI working on a package to do this cross platform: https://github.com/WyriHaximus/php-file-descriptors

Copy link
Owner Author

Choose a reason for hiding this comment

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

@WyriHaximus Thank you, will definitely keep an eye on this! 👍

Choose a reason for hiding this comment

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

@clue currently onto the daunting task of figuring it out for Windows...

if ($fds === false) {
$fds = range(3, 1024); // @codeCoverageIgnore
}

// do not inherit open FDs by explicitly closing all of them
foreach ($fds as $fd) {
if ($fd > 2) {
$command .= ' ' . $fd . '>&-';
}
}

// default `sh` only accepts single-digit FDs, so run in bash if needed
if ($fds && max($fds) > 9) {
$command = 'exec bash -c ' . escapeshellarg($command);
}

$process = new Process($command);
$process->start($this->loop);

$deferred = new Deferred(function () use ($process, $uri) {
Expand Down Expand Up @@ -110,9 +130,6 @@ public function connect($uri)
}

$connection = new CompositeConnection($process->stdout, $process->stdin);
$connection->on('close', function() use ($process) {
//$process->terminate();
});
$deferred->resolve($connection);
});

Expand Down
28 changes: 28 additions & 0 deletions tests/FunctionalSshProcessConnectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,32 @@ public function testConnectValidTargetWillReturnPromiseWhichResolvesToConnection
$this->assertTrue($connection->isWritable());
$connection->close();
}

public function testConnectValidTargetWillNotInheritActiveFileDescriptors()
{
$server = stream_socket_server('tcp://127.0.0.1:0');
$address = stream_socket_get_name($server, false);

// ensure that we can not listen on the same address twice
$copy = @stream_socket_server('tcp://' . $address);
if ($copy !== false) {
fclose($server);
fclose($copy);

$this->markTestSkipped('Platform does not prevent binding to same address (Windows?)');
}

// wait for successful connection
$promise = $this->connector->connect('example.com:80');
$connection = \Clue\React\Block\await($promise, $this->loop, self::TIMEOUT);

// close server and ensure we can start a new server on the previous address
// the open SSH connection process should not inherit the existing server socket
fclose($server);
$server = stream_socket_server('tcp://' . $address);
$this->assertTrue(is_resource($server));

fclose($server);
$connection->close();
}
}