Skip to content

dnsext-do53: 途中で切れたメッセージはメッセージではない - #297

Open
kazu-yamamoto wants to merge 2 commits into
mainfrom
fix-short-vc-read
Open

kazu-yamamoto wants to merge 2 commits into
mainfrom
fix-short-vc-read

Conversation

@kazu-yamamoto

@kazu-yamamoto kazu-yamamoto commented Sep 22, 2026 •

Copy link
Copy Markdown
Collaborator

途中までしか届かなかったメッセージを、そのまま 1 つのメッセージとして受け取っていました。#195 に向けた一歩です。

どこがどう間違っていたか

dnsext-do53/DNS/Do53/IO.hs の recvVCwith。2 オクテットの長さ接頭辞を読み、その数だけ読んで、空でない限りそのまま返します。

    bs <- recvN len
    if BS.null bs
        then E.throwIO $ DecodeError "message length is not enough"
        else return bs
  • recvN は届いた分を返します。長さを送ってから去った相手はメッセージの断片を残し、その断片が全体であるかのように返っていました。
  • 受け取った側が DNS.decode に掛けて DecodeError "incomplete input: BufferOverrun" で落ちます。どこから来た何が悪いのかを言えず、ループで読む接続では、読み位置を見失ってそれ以降のすべてのメッセージで同じことを言い続けます。bowline DecodeError loop #195 が報告している毎秒 2 万行はこれです。
  • 長さ接頭辞自体が届かなかった場合も同じ入口に入ります。decodeVCLength は読めなかった接頭辞を 0 オクテットと呼ぶためです。

どう直したか

読めた長さが長さ接頭辞と一致するかを見ます。

-    if BS.null bs
-        then E.throwIO $ DecodeError "message length is not enough"
-        else return bs
+    whole len bs

whole は、空なら従来どおりのエラー、長さが足りなければ「何オクテットのはずが何オクテット届いたか」を言うエラー、一致すれば受け取り、です。

どの層・どのパッケージ

dnsext-do53 の recvVCwith 1 か所。TCP・DoT・DoH など、仮想回線からメッセージを読むすべての経路が通ります。スタブ、DoX、clove でも使われる関数なので、振る舞いの変更は「不完全なものを渡さない」ことに限っています。

どう確かめたか

dnsext-do53 に RecvVCSpec を追加。6 例のうち 1 例が修正前は赤です。

refuses a message which stopped short
  expected: True
   but got: False

残りの 5 例は、これまでの振る舞いを固定するものです(丸ごと届いた、分割して届いた、始まらなかった、何も言わずに切れた、上限より長い)。cabal test all 9 スイートすべて 0 failures。

🤖 Generated with Claude Code

@kazu-yamamoto kazu-yamamoto changed the title dnsext-do53: half a message off a virtual circuit is not a message dnsext-do53: 途中で切れたメッセージはメッセージではない Oct 9, 2026
kazu-yamamoto and others added 2 commits November 18, 2026 11:10
recvVC says of itself that it "returns exactly-necessary-length data.
If necessary-length data is not received, an exception is thrown."  It
throws where nothing at all arrives.  Where some of the message arrives
and the rest does not, it hands over what it has:

  refuses a message which stopped short [x]
       expected: True
        but got: False

The other five say what it already does and should go on doing: a
message which arrived whole, one which arrived in pieces, one which
never started, a circuit which said nothing at all, and one longer than
the limit.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
recvVC reads the two-octet length and then that many octets, and takes
whatever comes back so long as it is not empty.  A read gives back what
has arrived: a peer which sent the length and then went away leaves part
of the message behind, and that part was handed on as if it were the
whole of it.

Whoever decodes it then fails with

    DecodeError "incomplete input: BufferOverrun"

which says nothing about where the message came from or what was wrong
with it, and on a connection which is read in a loop says it again for
every message after this one, the reader having lost where it was.

So the length is checked against what arrived.  Nothing at all keeps the
message it had, which covers a length prefix that did not arrive either:
decodeVCLength calls a prefix it could not read zero octets long.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant