Skip to content

Fixes #81#94

Merged
rlerdorf merged 1 commit intomasterfrom
fix/sftp-stream-close-use-after-free
Apr 4, 2026
Merged

Fixes #81#94
rlerdorf merged 1 commit intomasterfrom
fix/sftp-stream-close-use-after-free

Conversation

@rlerdorf
Copy link
Copy Markdown
Member

@rlerdorf rlerdorf commented Apr 4, 2026

A use-after-free crash when ssh2_disconnect() is called while SFTP streams are still open. The session dtor already had a guard (session_rsrc->ptr != NULL check), but both php_ssh2_sftp_stream_close and php_ssh2_sftp_dirstream_close called libssh2_sftp_close() unconditionally. If the session was already destroyed, this dereferences freed memory.

Fixed by adding the same guard to both stream close functions. Skip libssh2_sftp_close() if the session has already been freed.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes a use-after-free crash in the PHP SSH2 extension when ssh2_disconnect() is called while SFTP streams are still open, by ensuring stream close handlers don’t call into libssh2 after the underlying session has been closed.

Changes:

  • Add a session-liveness guard in php_ssh2_sftp_stream_close() before calling libssh2_sftp_close().
  • Add the same guard in php_ssh2_sftp_dirstream_close() to avoid dereferencing freed session state.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@rlerdorf rlerdorf merged commit 9d78647 into master Apr 4, 2026
44 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants