bufio.CachedConn: data race on the cached buffer between Read/ReaderReplaceable and Close
まだ誰も着手していません。
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 55/100
- issue の種類
- バグ
- 明瞭さ
- おおむね明確
- 活発さ
- 活発
- 技術スタック
- go
- 領域
- networking
調査の方向性
Start in common/bufio/cache.go with CachedConn.Read, ReaderReplaceable, Close, and the corresponding CachedPacketConn methods. Reproduce the reported concurrent paths with the race detector, including the routing and DNS-over-TCP entry points named in the issue. Done means the cache has one consistent ownership path, without races or double release.
索引モデルが issue の本文から書いたものです。
説明
sing v0.8.10 (via sing-box v1.13.12), go 1.26, -race
CachedConn.buffer is read and cleared without synchronization in Read and ReaderReplaceable, while Close (usually called from another goroutine) reads and clears it too. Close guards itself with taken, but Read / ReaderReplaceable don't check or set taken:
// common/bufio/cache.go
func (c *CachedConn) Read(p []byte) (n int, err error) {
if c.buffer != nil { // read
...
c.buffer.DecRef()
c.buffer.Release()
c.buffer = nil // write
}
return c.Conn.Read(p)
}
func (c *CachedConn) ReaderReplaceable() bool {
return c.buffer == nil // read
}
func (c *CachedConn) Close() error {
if c.taken.CompareAndSwap(false, true) {
if buffer := c.buffer; buffer != nil { // read (races with Read)
c.buffer = nil
buffer.DecRef()
buffer.Release() // can release a buffer Read already released
}
}
return c.Conn.Close()
}
CachedPacketConn.ReadPacket / Close have the same pattern.
Besides the race report, the worst case is the pooled buffer being released twice, so it can come back from buf.New to another connection while still referenced.
Where it shows up in sing-box 1.13.12 (both found with -race):
- Routing a sniffed TCP connection:
route.(*Router).routeConnectionwraps it inbufio.NewCachedConn;ConnectionManager.connectionCopythen callsReaderReplaceable(viaN.UnwrapCountReaderinbufio.CopyWithCounters) on one goroutine, while the other copy direction callsClose(route/conn.go:262 vs :271). - Hijacked DNS over TCP:
protocol/dns.HandleStreamDNSRequestreads the next query from the conn (handle.go:24) while the exchange goroutine of the previous query callsconn.Close()on error (handle.go:46).
When the wrapper can be unwrapped before copying, ReadCached (which does use taken) avoids it, but callers such as a ConnectionTracker that wraps the conn force the unsynchronized Read path.
Suggested fix: make every consumer of the cache take it through the same atomic flag, e.g.
func (c *CachedConn) Read(p []byte) (n int, err error) {
if !c.taken.Load() {
c.mu.Lock() // or take the buffer once with CompareAndSwap and keep it reader-local
...
}
return c.Conn.Read(p)
}
or have Read take the buffer once (CompareAndSwap(false, true)), move it into a reader-owned field, and never touch it from Close after that. ReaderReplaceable would then read an atomic. Happy to send a PR if the approach sounds right.
- 主要言語
- Go
- スター
- 131
- フォーク
- 116
- 平均マージ
- 1時間 42分
- マージ済み PR(30日)
- 1
環境構築
このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
SagerNet/sing のほかの issue
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
-
socks握手时的bufio会吞掉客户端流水线发送的首段payload,进而导致UoT v2客户端的每条流都会失败対応中かも @Snowy117 が 15 日前に担当しました。 オープン
難易度 4/5 1〜2日 初心者へのやさしさ 48/100
-
難易度 3/5 1〜2日 初心者へのやさしさ 68/100
-
addr 相关的几个问题オープン
難易度 4/5 3〜5日 初心者へのやさしさ 42/100
-
難易度 3/5 1〜2日 初心者へのやさしさ 30/100
似ている issue
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 84/100
メンテナーはふだん 1 日以内に返信
-
Mend: dependency security vulnerability
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
kube-tarian/sigrun#202 ·
-
good first issue help wanted
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
eugenioenko/ttt#1085 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
fullsend-ai/fullsend#8688 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
question raised-by:verifier
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
medici-finance/assay#4013 ·
メンテナーはふだん 1 日以内に返信