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)
Maintainer antworten meist innerhalb von 1 Tag
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Anfängerfreundlichkeit
- 22/100
- Issue-Typ
- Bug
- Klarheit
- Größtenteils klar
- Aktivitätsstatus
- Aktiv
- Tech-Stack
- c
- Bereich
- networking
Rechercherichtung
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.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
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
- Vorherrschende Sprache
- C
- Sterne
- 41.8k
- Forks
- 8.6k
- Ø Merge
- 2 T. 15 Std.
- Gemergte PRs (30 T.)
- 125
Entwicklungsumgebung
- Kein Dockerfile und keine Docker-Compose-Datei
- Keine Pull-Request-Vorlage
- Beitragsleitfaden lesen
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus php/php-src
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
Maintainer antworten meist innerhalb von 1 Tag
-
ext/openssl: 8.2.31+/8.3.31+ fail to build against OpenSSL 1.0.2 (ASN1_STRING_get0_data in xp_ssl.c)OffenExtension: openssl Status: Needs Triage
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 79/100
php/php-src#25402 · 2 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag
-
Bug Status: Needs Triage
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 68/100
php/php-src#25397 · 3 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag
-
SNMP accepts malformed remote portsEvtl. vergeben @somethingwithproof hat das vor 21 Tagen übernommen. Offen
Schwierigkeit 2/5 Ein halber Tag Anfängerfreundlichkeit 62/100
php/php-src#25137 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag
-
Bug Status: Needs Triage
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 64/100
Maintainer antworten meist innerhalb von 1 Tag
Ähnliche Issues
-
interpreter-core type-bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 62/100
Maintainer antworten meist innerhalb von 1 Tag
-
bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 88/100
ImageMagick/ImageMagick6#490 ·
-
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 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)Evtl. vergeben Ein verknüpfter Pull Request ist offen oder bereits gemergt. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 74/100
AcademySoftwareFoundation/openexr#2884 ·
Maintainer antworten meist innerhalb von 1 Tag