Skip to content

Fix double free in ext/ftp data_accept() on accept failure - #24339

Closed
alepacheco wants to merge 2 commits into
php:PHP-8.4from
alepacheco:fix-ftp-data-accept-double-free
Closed

alepacheco wants to merge 2 commits into
php:PHP-8.4from
alepacheco:fix-ftp-data-accept-double-free

Conversation

@alepacheco

Copy link
Copy Markdown
Contributor

In active mode, when the server never opens the data connection, my_accept() fails and data_accept() frees the databuf_t. ftp_getdata() has already stored that buffer in ftp->data, so the caller's bail: path runs data_close(ftp) and frees it again. Every transfer and listing function that goes through data_accept() is affected (ftp_get(), ftp_put(), ftp_append(), ftp_nb_get(), ftp_nb_put(), ftp_nlist()/ftp_rawlist()/ftp_mlsd()).

The SSL failure paths in data_accept() already return without freeing and leave the cleanup to data_close(). This makes the accept failure path do the same, so data_close() is the only place that frees a published ftp->data.

The test adds a server.inc file name that answers RETR with 150 but never connects back, and checks that ftp_get() and ftp_nb_get() both fail cleanly after the timeout.

Targeting PHP-8.4 as the oldest branch with the bug in active support.

When the server never opens the active-mode data connection, my_accept()
fails and data_accept() frees the databuf_t. ftp_getdata() has already
stored it in ftp->data, so the caller's bail path then runs data_close(),
which frees it a second time. This affects ftp_get(), ftp_put(),
ftp_append(), ftp_nb_get(), ftp_nb_put() and the listing functions.

data_accept() does not own the buffer: leave it in ftp->data and let
data_close() release it, as the SSL failure paths in data_accept()
already do.
…out.phpt

musl and FreeBSD report "Operation timed out", glibc "Connection timed out".

@ndossche ndossche left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks valid, thanks

@alepacheco

Copy link
Copy Markdown
Contributor Author

Thanks for the review! The WINDOWS_X64_ZTS failure is the proc_open_null/proc_open_redirect pair that fails on other PHP-8.4 PRs too; nothing in ext/ftp.

@ndossche ndossche closed this in 0d78037 Oct 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants