Fix #79631: SSH disconnect segfault with SFTP (assertion failed)

If the SSH2 Session resource has already been closed, the libssh2
session has been as well, and the `LIBSSH2_SESSION*` has been set NULL.
If that is the case, we must not call `libssh2_sftp_shutdown()`.
This commit is contained in:
Christoph M. Becker
2021-02-09 14:48:43 +01:00
parent 93265d71bd
commit 9fb1cf84f0
2 changed files with 27 additions and 1 deletions

View File

@@ -36,7 +36,9 @@ void php_ssh2_sftp_dtor(zend_resource *rsrc)
return;
}
libssh2_sftp_shutdown(data->sftp);
if (data->session_rsrc->ptr != NULL) {
libssh2_sftp_shutdown(data->sftp);
}
zend_list_delete(data->session_rsrc);
@@ -873,6 +875,10 @@ PHP_FUNCTION(ssh2_sftp_realpath)
RETURN_FALSE;
}
if (data->session_rsrc->ptr == NULL) {
RETURN_FALSE;
}
if ((targ_len = libssh2_sftp_symlink_ex(data->sftp, link->val, link->len, targ, 8192, LIBSSH2_SFTP_REALPATH)) < 0) {
php_error_docref(NULL, E_WARNING, "Unable to resolve realpath for '%s'", link->val);
RETURN_FALSE;

20
tests/bug79631.phpt Normal file
View File

@@ -0,0 +1,20 @@
--TEST--
Bug 79631 (SSH disconnect segfault with SFTP (assertion failed))
--SKIPIF--
<?php
require('ssh2_skip.inc');
ssh2t_needs_auth();
ssh2t_writes_remote();
?>
--FILE--
<?php
require('ssh2_test.inc');
$ssh = ssh2_connect(TEST_SSH2_HOSTNAME, TEST_SSH2_PORT);
ssh2t_auth($ssh);
$sftp = ssh2_sftp($ssh);
ssh2_disconnect($ssh);
echo "done\n";
?>
--EXPECT--
done