uclient-fetch hangs forever when a single HTTP header line exceeds ~4 KB

オープン
#21 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
58/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
c
領域
networking

調査の方向性

Start with __uclient_notify_read() in uclient-http.c and the read-buffer limits in libubox/ustream.c; trace how an incomplete header line is handled when the buffer fills. Use the issue’s Python server reproducer to compare the 4085- and 4086-byte cases. Done means long header lines no longer leave uclient-fetch hung; test both the reproducer and ordinary downloads.

索引モデルが issue の本文から書いたものです。

説明

Environment

  • OpenWrt 25.12.5 (r33051-f5dae5ece4), mediatek/filogic, aarch64_cortex-a53, kernel 6.12.94
  • uclient-fetch 2026.06.07~daad21fa-r1 (/usr/bin/wget -> /bin/uclient-fetch)
  • libuclient20201210 2026.06.07~daad21fa-r1
  • libubox20260213 2026.06.19~7dd12784-r1
  • libustream-mbedtls20201210 2026.03.01~99f1c0db-r1 (mbedTLS 3.6.7)
  • Reproduced identically on two other OpenWrt routers running different releases.

curl 8.19.0 on the same router downloads every affected URL without problems, and GNU wget on a Linux desktop does too, so the network path, DNS, MTU and any DPI/proxying in between are not involved.

Summary

uclient-fetch deadlocks on a response that contains a single HTTP header line longer than the 4096-byte ustream read buffer.

The TCP connection and the TLS handshake complete, the client sends the request, the server sends the full response, the kernel receives and ACKs it — and then uclient-fetch simply stops reading the socket. The receive queue fills up, the advertised window drops to zero, the process sleeps in epoll_wait() and nothing ever wakes it up. With -T N it eventually exits with

Connecting to 140.82.121.3:443
Connection error: Connection timed out

which is misleading (the connection was fine; the response was never read). Without -T it hangs forever.

Real-world trigger

github.com now returns a ~4.2–4.4 KB single-line Content-Security-Policy header on its dynamic responses. Measured with curl --http1.1 -D - on the router:

URL longest header line uclient-fetch
https://github.com/ 4394 hangs
https://github.com/manifest.json 4174 hangs
https://github.com/<org>/<repo>/releases/download/<tag>/<asset> (302) 4174 hangs
https://github.com/humans.txt (301 -> /about, which also has CSP) 2328 then 4174 hangs
https://github.com/robots.txt 414 OK
https://github.com/favicon.ico 408 OK
https://github.com/404 (93 KB body) 410 OK
https://github.com/apple-touch-icon.png 410 OK

So it is deterministic per URL (3/3 runs each) and is not related to response size — a 93 KB static file works, a 1.4 KB JSON manifest does not.

This breaks wget-based installers, e.g.

wget https://github.com/snarknn/openwrt-smaller-tailscale/releases/download/v1.104.5/tailscale_1.104.5_arm64.tar.gz

The 302 response from github.com already carries the long CSP header, so the client hangs before it can even follow the redirect to release-assets.githubusercontent.com.

Minimal reproducer

A server that returns a single header line of an arbitrary length (plain HTTP, no TLS needed — the bug is in the HTTP/ustream layer, not in the TLS backend):

from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer

class H(BaseHTTPRequestHandler):
    protocol_version = "HTTP/1.1"
    def do_GET(self):
        n = int(self.path.strip("/"))
        body = b"ok"
        self.send_response(200)
        self.send_header("Content-Type", "text/plain")
        self.send_header("Content-Length", str(len(body)))
        self.send_header("X-Long", "a" * n)   # one long header line
        self.end_headers()
        self.wfile.write(body)

ThreadingHTTPServer(("0.0.0.0", 8088), H).serve_forever()

Then on the router:

uclient-fetch -O /dev/null http://127.0.0.1:8088/4085   # completes
uclient-fetch -O /dev/null http://127.0.0.1:8088/4086   # hangs

Measured on the router, 3 runs each:

X-Long value length result
100, 1000, 3000, 3900, 4050, 4070, 4080, 4085 Download completed
4086, 4087, 4088, 4089, 4090, 4200, 6000 Connection error: Connection timed out

The boundary is exactly the 4096-byte read buffer ("X-Long: " + value + CRLF).

It is a per line limit, not a total header size limit — 60 separate headers of 200 bytes each (~12.5 KB of headers in total) download fine.

Evidence collected during the hang

  • tcpdump on the WAN interface: SYN/SYN-ACK/ACK, TLS 1.3 handshake, client request, full server response; the router's kernel ACKs the data.
  • ss -tni while hanging: ESTABLISHED, Recv-Q 76573, Send-Q 0, window eventually 0.
  • ps: process in state S (sleeping in epoll_wait), RSS 2356 kB, 1 thread.
  • Only after -T expires: Connection error: Connection timed out.

Root cause

  1. libubox/ustream.c, ustream_init_defaults() sets up the read buffer list as

    DEFAULT_SET(s->r.min_buffers, 1);
    DEFAULT_SET(s->r.max_buffers, 1);
    DEFAULT_SET(s->r.buffer_len, 4096);
    

    and uclient does not override these values. So the read side has exactly one 4096-byte buffer and cannot grow.

  2. uclient-http.c, __uclient_notify_read() parses headers line by line and only consumes complete lines:

    do {
        sep = strchr(data, '\n');
        if (!sep)
            break;                 /* no complete line: consume nothing */
        ...
        *sep = 0;
        cur_len = next - data;
        uclient_parse_http_line(uh, data);
        ustream_consume(uh->us, cur_len);
        ...
    } while (data && uh->state == HTTP_STATE_RECV_HEADERS);
    
  3. When the buffer is full and there is still no \n, ustream_reserve() -> ustream_prepare_buf() cannot allocate a second buffer (buffers == max_buffers), so it does

    __ustream_set_read_blocked(s, s->read_blocked | READ_BLOCKED_FULL);
    *maxlen = 0;
    return NULL;
    
  4. ustream-ssl.c, __ustream_ssl_poll() then stops reading the socket:

    buf = ustream_reserve(s, 1, &len);
    if (!len)
        break;
    

Since the stuck line can never be completed, nothing ever calls ustream_consume() for it, READ_BLOCKED_FULL is never cleared, and the read side stays blocked forever. There is no timeout that applies to "waiting for more data while the read side is blocked by ourselves", so the only escape is -T.

A response header field longer than 4 KB is perfectly legal (RFC 9110 does not limit field-value length), and real servers do send them, so this is a hard deadlock on valid input rather than a malformed-response case.

Suggested fix

Any of these would resolve it:

  • Make header parsing independent of the 4 KB read buffer: accumulate the incomplete line (e.g. into uh->meta / a growing blob) and consume it as it arrives, instead of requiring the whole line to be present in the contiguous ustream buffer.
  • Or enlarge the read buffer for the client stream (r.buffer_len, e.g. 16 KB) — this only raises the threshold, it does not remove the class of bug.
  • Or, at the very least, fail fast instead of hanging: when the buffer is full and no CRLF was found, abort the request with a clear error (e.g. "HTTP response header too long" / 431) rather than blocking the read side indefinitely.

Impact

Any script or installer that uses wget on OpenWrt to download from github.com is now broken, including release-asset downloads, because the 302 redirect response itself carries the oversized header. Workarounds for users: use curl (present on many images, or installable), or fetch the release-assets.githubusercontent.com URL directly and skip the redirect.

主要言語
C
スター
8
フォーク
16
PR マージ指標
30日以内にマージされた PR はありません

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

openwrt/uclient のほかの issue

openwrt/uclient の issue をすべて見る

似ている issue

C の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。