Daniele
Daniele

Reputation: 604

Segmentation fault on fopen using sftp and ssh2

I have a php system running on AWS and a class that upload a xlsx file on external server using shh2 and sftp. This code worked fine until last upgrade of aws package openssh-clients-6.6.1p1-31.62 and openssh-server-6.6.1p1-31.62 by this time I have a segfault during fopen. Fopen create a file on external server. Here the code:

$stream = @fopen("ssh2.sftp://$this->sftp$remote_file", 'w');

Then I use $stream to write the content, but the code stop on fopen bacause a segfault. I don't find anything about this problem, I think the problem is the new upgrade of opnessh, because the php code isn't changed. Any idea?

Upvotes: 7

Views: 7416

Answers (5)

DesertEagle
DesertEagle

Reputation: 599

ssh2 caused a segfault in my case (php8) during shutdown, after the usage of the $dir = dir(...) function without calling $dir->close();. I assume this is the same behaviour if opendir() is used.

Upvotes: 0

Paul Melekhov
Paul Melekhov

Reputation: 151

You may experience "segmentation fault" issue even with intval($sftp) solution if you're closing connection with ssh2_disconnect(). The right solution is to close SFTP connection before closing SSH2-connection by unset($sftp) or $sftp = null. Here's my fully working example of reading remote file:

if (!$ssh2_connection = ssh2_connect($host, $port)) {
    die("Failed to connect\n");
}
if (!ssh2_auth_password($ssh2_connection, $username, $password)) {
    die("Failed to login\n");
}
if (!$sftp_connection = ssh2_sftp($ssh2_connection)) {
    die("Failed to open SFTP session\n");
}
if (!$fh = fopen("ssh2.sftp://".intval($sftp_connection).$path, 'r')) {
    die("Failed to open file\n");
}
while (($s = fgets($fh)) !== false) {
    echo $s;
}
fclose($fh);
unset($sftp_connection);
if (!ssh2_disconnect($ssh2_connection)) {
    die("Failed to disconnect\n");
}

P.S. Also instead of "segmentation fault" you may see something like:

php: channel.c:2570: _libssh2_channel_free: Assertion `session' failed.

Aborted

My solution solves it.

Upvotes: 8

Arian Acosta
Arian Acosta

Reputation: 6817

In our case, the intval() didn't solve the segmentation fault. However, changing the call format did work:

fopen("ssh2.sftp://{$username}:{$password}@{$host}:{$port}/{$absolutePath}/{$filename}", 'w');

Upvotes: 3

Steph M
Steph M

Reputation: 11

I had the same error, but I have found this PHP bug (see post from: [2016-12-15 09:47 UTC] by ovoelker at wavecon dot de)

  1. add php5-dev for compiling pecl module
  2. reinstall ssh2 pecl module.
  3. restart apache and work well

Upvotes: 1

Christian
Christian

Reputation: 918

Found the answer here on StackOverflow: https://stackoverflow.com/a/40972584/2520795

It seems since this PHP update, you have to surround your host part (result of ssh2_sftp()) with intval():

$handle = opendir("ssh2.sftp://".intval($sftp)."/path/to/directory");

In my case there was a fopen instead of an opendir, but the solution is the same.

Upvotes: 19

Related Questions