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)

未關閉
#25,656 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 1 天內回覆

還沒有人認領這個 Issue。

評估

難度
4/5
預估耗時
3-5 天
新手友好度
22/100
Issue 類型
缺陷
描述清晰度
基本清楚
活躍度
活躍
技術堆疊
c
領域
networking

研究方向

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.

由索引模型根據 Issue 內容生成。

描述

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

主要語言
C
星號
41.8k
分支
8.6k
平均合併
2 天 15 小時
30 天內合併 PR
125

環境準備

  • 沒有 Dockerfile 或 Docker Compose 檔案
  • 沒有 Pull Request 範本
  • 閱讀貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

php/php-src 的其他 Issue

查看 php/php-src 的全部 Issue

相似的 Issue

更多 C Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。