From 60576f8bd1084e1da8862985bc36e87f01c75c8d Mon Sep 17 00:00:00 2001 From: basavaraj-sm05 Date: Mon, 10 Aug 2026 19:41:38 +0530 Subject: [PATCH] net/socks5: compare proxy auth credentials in constant time The SOCKS5 server checked the client-supplied username and password against the configured credentials with plain string equality, which returns on the first differing byte. In tsnet the password is a random 128-bit value that gates every dial out through the node, and the listener is on 127.0.0.1, so a local process can time the auth reject to recover it a byte at a time with unlimited attempts and no lockout. The LocalAPI sharing the same loopback listener already compares its credential with subtle.ConstantTimeCompare; do the same here for both fields, evaluating both so the username result does not gate whether the password is examined. Updates #20998 Signed-off-by: basavaraj-sm05 --- net/socks5/socks5.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/net/socks5/socks5.go b/net/socks5/socks5.go index f67dc1ecc..f501a000e 100644 --- a/net/socks5/socks5.go +++ b/net/socks5/socks5.go @@ -15,6 +15,7 @@ package socks5 import ( "bytes" "context" + "crypto/subtle" "encoding/binary" "errors" "fmt" @@ -173,7 +174,14 @@ func (c *Conn) Run() error { } user, pwd, err := parseClientAuth(c.clientConn) - if err != nil || user != c.srv.Username || pwd != c.srv.Password { + // Compare both credentials in constant time. The listener is reachable by + // any local process, so a data-dependent comparison would let one recover + // the username or password a byte at a time by timing the reject. Evaluate + // both halves unconditionally so the username result doesn't gate whether + // the password is examined. + userMatch := subtle.ConstantTimeCompare([]byte(user), []byte(c.srv.Username)) + pwdMatch := subtle.ConstantTimeCompare([]byte(pwd), []byte(c.srv.Password)) + if err != nil || userMatch != 1 || pwdMatch != 1 { c.clientConn.Write([]byte{1, 1}) // auth error return err }