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)

Aperta
#25,656 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

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).

  1. ftp_login() sets SSL_SESS_CACHE_BOTH | SSL_SESS_CACHE_NO_INTERNAL, and ftp_ssl_new_session_cb() keeps the most recent session in ftp->last_ssl_session. data_accept() passes that very object to SSL_set_session().
  2. On a client with SSL_SESS_CACHE_CLIENT, OpenSSL calls SSL_CTX_remove_session(s->session_ctx, s->session) when a TLS 1.3 handshake finishes (tls_finish_handshake() in ssl/statem/statem_lib.c: "We encourage applications to only use TLSv1.3 tickets once"), and remove_session_lock() sets not_resumable = 1 on the session whether or not it was in the internal cache. So once a data connection has resumed from ftp->last_ssl_session, that object can no longer be resumed.
  3. 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 (unless TLSOptions NoSessionReuseRequired) and rejects the connection, hence the 425.
  4. 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 in ftp_ssl_shutdown() does not pick the ticket up after an upload; the SSL_shutdown failed warnings 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

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di php/php-src

Tutte le issue di php/php-src

Issue simili

Altre issue su C

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.