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)
維護者通常 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).
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
- 主要語言
- C
- 星號
- 41.8k
- 分支
- 8.6k
- 平均合併
- 2 天 15 小時
- 30 天內合併 PR
- 125
環境準備
- 沒有 Dockerfile 或 Docker Compose 檔案
- 沒有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
php/php-src 的其他 Issue
-
難度 2/5 1-3 小時 新手友好度 72/100
維護者通常 1 天內回覆
-
ext/openssl: 8.2.31+/8.3.31+ fail to build against OpenSSL 1.0.2 (ASN1_STRING_get0_data in xp_ssl.c)未關閉Extension: openssl Status: Needs Triage
難度 2/5 1-3 小時 新手友好度 79/100
維護者通常 1 天內回覆
-
Bug Status: Needs Triage
難度 2/5 1-3 小時 新手友好度 68/100
維護者通常 1 天內回覆
-
SNMP accepts malformed remote ports可能已有人在做 @somethingwithproof 於 21 天前認領。 未關閉
難度 2/5 半天 新手友好度 62/100
維護者通常 1 天內回覆
-
Bug Status: Needs Triage
難度 2/5 1-3 小時 新手友好度 64/100
維護者通常 1 天內回覆
相似的 Issue
-
bug
難度 2/5 1-3 小時 新手友好度 72/100
維護者通常 1 天內回覆
-
ZIP/ZIPS decode dereferences NULL when the cached libdeflate decompressor can't be allocated (3.5 regression)可能已有人在做 關聯的 PR 仍在進行中或已合併。 未關閉
難度 2/5 1-3 小時 新手友好度 74/100
AcademySoftwareFoundation/openexr#2884 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 82/100
python-pillow/Pillow#10590 ·
維護者通常 1 天內回覆
-
status: needs-triage type: bug
難度 2/5 1-3 小時 新手友好度 62/100
-
bug
難度 2/5 1-3 小時 新手友好度 62/100
openwrt/packages#31407 · 1 則留言 ·
維護者通常 1 天內回覆