ext/ftp: every second upload over TLS 1.3 fails against ProFTPD with 425 (session reuse on the data connection, follow-up to GH-9348)
I maintainer di solito rispondono entro 1 giorno
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 22/100
- Tipo di issue
- Bug
- Chiarezza
- Abbastanza chiara
- Stato di attività
- Attiva
- Stack tecnologico
- c
- Ambito
- networking
Direzione di ricerca
The relevant code is ext/ftp/ftp.c: start with data_accept() where SSL_set_session() is called with ftp->last_ssl_session, and with ftp_ssl_new_session_cb() and ftp_ssl_shutdown(). Reproduce with the PHP snippet against a ProFTPD server using TLS 1.3 and check whether every second ftp_put() fails with 425. Done would mean repeated uploads over one control connection all succeed, shown by a test or script, without regressing listings and downloads.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Description
Over one ftp_ssl_connect() connection to a ProFTPD server that negotiates TLS 1.3, every second upload fails. The server answers with 425 Unable to build data connection: Operation not permitted, which is what ProFTPD's mod_tls sends when the data connection does not resume the TLS session it requires. The uploads in between succeed. Listings and downloads over the same connection never fail.
Plain FTP to the same server never fails, and neither does libcurl 8.23.0 with CURLUSESSL_ALL over one reused handle (its trace shows SSL reusing session for each data connection).
The following code:
<?php
$ftp = ftp_ssl_connect('HOST');
ftp_login($ftp, 'USER', 'PASSWORD');
ftp_pasv($ftp, true);
file_put_contents(__DIR__ . '/local.txt', str_repeat('x', 4096));
for ($i = 0; $i < 8; $i++) {
$ok = @ftp_put($ftp, "test-$i.txt", __DIR__ . '/local.txt', FTP_BINARY);
echo "put $i: ", $ok ? 'ok' : 'FAILED: ' . error_get_last()['message'], "\n";
}
ftp_close($ftp);
Resulted in this output:
put 0: ok
put 1: FAILED: ftp_put(): Unable to build data connection: Operation not permitted
put 2: ok
put 3: FAILED: ftp_put(): Unable to build data connection: Operation not permitted
put 4: ok
put 5: FAILED: ftp_put(): Unable to build data connection: Operation not permitted
put 6: ok
put 7: FAILED: ftp_put(): Unable to build data connection: Operation not permitted
But I expected this output instead:
put 0: ok
put 1: ok
put 2: ok
put 3: ok
put 4: ok
put 5: ok
put 6: ok
put 7: ok
What else I measured over a single control connection (TLSv1.3, TLS_AES_256_GCM_SHA384):
| Calls | Result |
|---|---|
8 × ftp_nlist() |
all ok |
8 × ftp_put() |
ok, fail, ok, fail, ok, fail, ok, fail |
6 × ftp_get() |
all ok |
8 × ftp_nb_put() + ftp_nb_continue() in a tight loop |
ok, fail, ok, fail, ok, fail, ok, fail |
8 × ftp_nb_put() + ftp_nb_continue() with usleep(10000) between calls |
ok, fail, fail, ok, fail, fail, fail, ok |
In the last variant most failures come with two warnings from ftp_nb_continue(): first SSL_shutdown failed, then Unable to build data connection: Operation not permitted.
FTP_USEPASVADDRESS, FTP_TIMEOUT_SEC and FTP_AUTOSEEK make no difference.
Analysis
This looks like a remaining part of GH-9348 (fixed by GH-12851). I have not debugged a build of PHP; what follows is from reading the sources of ext/ftp (PHP-8.5), OpenSSL 3.5 and ProFTPD (master).
ftp_login()setsSSL_SESS_CACHE_BOTH | SSL_SESS_CACHE_NO_INTERNAL, andftp_ssl_new_session_cb()keeps the most recent session inftp->last_ssl_session.data_accept()passes that very object toSSL_set_session().- On a client with
SSL_SESS_CACHE_CLIENT, OpenSSL callsSSL_CTX_remove_session(s->session_ctx, s->session)when a TLS 1.3 handshake finishes (tls_finish_handshake()inssl/statem/statem_lib.c: "We encourage applications to only use TLSv1.3 tickets once"), andremove_session_lock()setsnot_resumable = 1on the session whether or not it was in the internal cache. So once a data connection has resumed fromftp->last_ssl_session, that object can no longer be resumed. - For the next data connection
tls_construct_client_hello()sees!SSL_SESSION_is_resumable(sess)and silently starts a full handshake. mod_tls requires resumption for data connections (unlessTLSOptions NoSessionReuseRequired) and rejects the connection, hence the 425. - Everything therefore depends on a new session ticket arriving, and being processed, on every data connection before the next one is opened. That evidently happens for listings and downloads, where the client reads from the data connection, and does not happen reliably for uploads, where the client only writes and reads nothing until
ftp_ssl_shutdown(). I have not established why the read loop inftp_ssl_shutdown()does not pick the ticket up after an upload; theSSL_shutdown failedwarnings in the paused non-blocking variant suggest that the shutdown path is involved.
libcurl does not depend on a new ticket per connection: it keeps the session serialized and creates a fresh SSL_SESSION with d2i_SSL_SESSION() for each connection (lib/vtls/openssl.c), so the same ticket is offered again and the server accepts it.
A possible fix in the same spirit is to resume from a copy, so that the stored session is never the object OpenSSL marks as used:
/* and set it on the data connection */
SSL_set_app_data(data->ssl_handle, ftp); /* Needed for ftp_ssl_new_session_cb */
- res = SSL_set_session(data->ssl_handle, session);
+ session = SSL_SESSION_dup(session);
+ res = session != NULL && SSL_set_session(data->ssl_handle, session);
+ SSL_SESSION_free(session);
if (res == 0) {
A newer ticket would still replace the stored session through the callback; TLS 1.2 is unaffected. I have not tested this patch.
PHP Version
PHP 8.5.13 (cli) (built: Nov 18 2026 01:01:49) (ZTS Visual C++ 2022 x64), OpenSSL 3.5.9
Operating System
Windows 11 (client); the server is ProFTPD
- Lingua principale
- C
- Stelle
- 41.8k
- Fork
- 8.6k
- Merge medio
- 2g 15h
- PR unite (30g)
- 125
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Nessun modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di php/php-src
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
I maintainer di solito rispondono entro 1 giorno
-
ext/openssl: 8.2.31+/8.3.31+ fail to build against OpenSSL 1.0.2 (ASN1_STRING_get0_data in xp_ssl.c)ApertaExtension: openssl Status: Needs Triage
Difficoltà 2/5 1-3 ore Idoneità per principianti 79/100
php/php-src#25402 · 2 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
Bug Status: Needs Triage
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
php/php-src#25397 · 3 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
SNMP accepts malformed remote portsForse già presa @somethingwithproof l’ha presa 21 giorni fa. Aperta
Difficoltà 2/5 Mezza giornata Idoneità per principianti 62/100
php/php-src#25137 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
Bug Status: Needs Triage
Difficoltà 2/5 1-3 ore Idoneità per principianti 64/100
I maintainer di solito rispondono entro 1 giorno
Issue simili
-
interpreter-core type-bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100
I maintainer di solito rispondono entro 1 giorno
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 88/100
ImageMagick/ImageMagick6#490 ·
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 62/100
camera-2018/hdu-cs-wiki#647 ·
-
ZIP/ZIPS decode dereferences NULL when the cached libdeflate decompressor can't be allocated (3.5 regression)Forse già presa Una pull request collegata a questa issue è aperta o già unita. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 74/100
AcademySoftwareFoundation/openexr#2884 ·
I maintainer di solito rispondono entro 1 giorno