7

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?

2
  • This is a big problem. Surely no one but us has not faced it ...
    – BigTon
    Commented Dec 14, 2016 at 12:29
  • I'm having the same effect (segfault) on a Debian 8 machine. It started after I installed updates: Start-Date: 2016-12-14 12:53:53 Commandline: apt-get upgrade Upgrade: apt:amd64 libapache2-mod-php5:amd64 php5-mysql:amd64 python-urllib3:amd64 php5-common:amd64 php5-curl:amd64 apt-utils:amd64 php5-readline:amd64 libapt-inst1.5:amd64 php5:amd64 libapt-pkg4.12:amd64 php5-cli:amd64 python-requests:amd64 php-pear:amd64 php5-intl:amd64
    – Christian
    Commented Dec 15, 2016 at 10:42

5 Answers 5

19

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.

0
8

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.

0
3

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');
1

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
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.