go-ethereum/p2p/nat/stun.go
ozpool 45698e9cb9
p2p/nat: bump pion/stun to v3 to pull in fixed pion/dtls (#34980)
### Summary

Closes #34621.

`github.com/pion/dtls/v2` is affected by
[CVE-2026-26014](https://nvd.nist.gov/vuln/detail/CVE-2026-26014); the
fix lives in `github.com/pion/dtls/v3`. In this tree, dtls/v2 is pulled
in indirectly via `github.com/pion/stun/v2 v2.0.0` (declared at
`go.mod:53`), which is the only direct consumer — `p2p/nat/stun.go` is
the sole call site.

`github.com/pion/stun/v3` already uses dtls/v3, so bumping `stun`
upgrades the vulnerable dependency without touching `pion/dtls`
directly.

### API check

The v3 surface used by `p2p/nat/stun.go` is byte-identical in shape to
v2:

| Symbol | v2 | v3 |
|---|---|---|
| `Dial` | `func Dial(network, address string) (*Client, error)` | same
|
| `Build` | `func Build(setters ...Setter) (*Message, error)` | same |
| `TransactionID` | `var TransactionID Setter` | same |
| `BindingRequest` | `var BindingRequest = NewType(MethodBinding,
ClassRequest)` | same |
| `Event` | `type Event struct` | same |
| `XORMappedAddress` | `type XORMappedAddress struct { …
GetFrom(*Message) error }` | same |
| `DefaultPort` | `const DefaultPort = 3478` | same |

So the code change is just the import rename plus an alias rename to
keep the local label honest (`stunV2` → `stunV3`).

### Change

`go.mod` / `go.sum`:

- Replace direct `github.com/pion/stun/v2 v2.0.0` with
`github.com/pion/stun/v3 v3.0.1`.
- `go mod tidy` drops every `pion/dtls/v2` and `pion/stun/v2` entry from
`go.sum` and pulls `pion/dtls/v3 v3.0.7`, `pion/stun/v3 v3.0.1`,
`pion/transport/v3 v3.0.8` as the new indirect set.

`p2p/nat/stun.go`:

- Update the import path and rename the alias from `stunV2` to `stunV3`.

### Verification

- `go build ./p2p/nat/` clean.
- `go test ./p2p/nat/ -count=1` passes (26s).
- `grep 'pion/dtls/v2\|pion/stun/v2' go.sum` returns zero matches.

### Notes

- `pion/dtls` is not imported directly anywhere in the tree, so no other
code needs touching.
- `pion/transport/v3` was already in the dependency graph (the `stun/v3`
upgrade just bumps the patch from v3.0.1 → v3.0.8); the v2 transport
drops out cleanly.
2026-05-27 13:38:18 +02:00

144 lines
3.4 KiB
Go

// Copyright 2025 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package nat
import (
_ "embed"
"errors"
"fmt"
"math/rand"
"net"
"strings"
"time"
"github.com/ethereum/go-ethereum/log"
stunV3 "github.com/pion/stun/v3"
)
//go:embed stun-list.txt
var stunDefaultServers string
const requestLimit = 3
var errSTUNFailed = errors.New("STUN requests failed")
type stun struct {
serverList []string
}
func newSTUN(serverAddr string) (Interface, error) {
s := new(stun)
if serverAddr == "" {
s.serverList = strings.Split(stunDefaultServers, "\n")
} else {
_, err := net.ResolveUDPAddr("udp4", serverAddr)
if err != nil {
return nil, err
}
s.serverList = []string{serverAddr}
}
return s, nil
}
func (s stun) String() string {
if len(s.serverList) == 1 {
return fmt.Sprintf("stun:%s", s.serverList[0])
}
return "stun"
}
func (s stun) MarshalText() ([]byte, error) {
return []byte(s.String()), nil
}
func (stun) SupportsMapping() bool {
return false
}
func (stun) AddMapping(protocol string, extport, intport int, name string, lifetime time.Duration) (uint16, error) {
return uint16(extport), nil
}
func (stun) DeleteMapping(string, int, int) error {
return nil
}
func (s *stun) ExternalIP() (net.IP, error) {
for _, server := range s.randomServers(requestLimit) {
ip, err := s.externalIP(server)
if err != nil {
log.Debug("STUN request failed", "server", server, "err", err)
continue
}
return ip, nil
}
return nil, errSTUNFailed
}
func (s *stun) randomServers(n int) []string {
n = min(n, len(s.serverList))
m := make(map[int]struct{}, n)
list := make([]string, 0, n)
for i := 0; i < len(s.serverList)*2 && len(list) < n; i++ {
index := rand.Intn(len(s.serverList))
if _, alreadyHit := m[index]; alreadyHit {
continue
}
list = append(list, s.serverList[index])
m[index] = struct{}{}
}
return list
}
func (s *stun) externalIP(server string) (net.IP, error) {
_, _, err := net.SplitHostPort(server)
if err != nil {
server += fmt.Sprintf(":%d", stunV3.DefaultPort)
}
log.Trace("Attempting STUN binding request", "server", server)
conn, err := stunV3.Dial("udp4", server)
if err != nil {
return nil, err
}
defer conn.Close()
message, err := stunV3.Build(stunV3.TransactionID, stunV3.BindingRequest)
if err != nil {
return nil, err
}
var responseError error
var mappedAddr stunV3.XORMappedAddress
err = conn.Do(message, func(event stunV3.Event) {
if event.Error != nil {
responseError = event.Error
return
}
if err := mappedAddr.GetFrom(event.Message); err != nil {
responseError = err
}
})
if err != nil {
return nil, err
}
if responseError != nil {
return nil, responseError
}
log.Trace("STUN returned IP", "server", server, "ip", mappedAddr.IP)
return mappedAddr.IP, nil
}