Skip to content

Commit 6f54ae2

Browse files
authored
p2p: add 0 port check in dialer (#21008)
* p2p: add low port check in dialer We already have a check like this for UDP ports, add a similar one in the dialer. This prevents dials to port zero and it's also an extra layer of protection against spamming HTTP servers. * p2p/discover: use errLowPort in v4 code * p2p: change port check * p2p: add comment * p2p/simulations/adapters: ensure assigned port is in all node records
1 parent 069a7e1 commit 6f54ae2

3 files changed

Lines changed: 16 additions & 7 deletions

File tree

‎p2p/dial.go‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ var (
7878
errAlreadyConnected = errors.New("already connected")
7979
errRecentlyDialed = errors.New("recently dialed")
8080
errNotWhitelisted = errors.New("not contained in netrestrict whitelist")
81+
errNoPort = errors.New("node does not provide TCP port")
8182
)
8283

8384
// dialer creates outbound connections and submits them into Server.
@@ -388,6 +389,12 @@ func (d *dialScheduler) checkDial(n *enode.Node) error {
388389
if n.ID() == d.self {
389390
return errSelf
390391
}
392+
if n.IP() != nil && n.TCP() == 0 {
393+
// This check can trigger if a non-TCP node is found
394+
// by discovery. If there is no IP, the node is a static
395+
// node and the actual endpoint will be resolved later in dialTask.
396+
return errNoPort
397+
}
391398
if _, ok := d.dialing[n.ID()]; ok {
392399
return errAlreadyDialing
393400
}
@@ -474,15 +481,13 @@ type dialError struct {
474481
}
475482

476483
func (t *dialTask) run(d *dialScheduler) {
477-
if t.dest.Incomplete() {
478-
if !t.resolve(d) {
479-
return
480-
}
484+
if t.needResolve() && !t.resolve(d) {
485+
return
481486
}
482487

483488
err := t.dial(d, t.dest)
484489
if err != nil {
485-
// Try resolving the ID of static nodes if dialing failed.
490+
// For static nodes, resolve one more time if dialing fails.
486491
if _, ok := err.(*dialError); ok && t.flags&staticDialedConn != 0 {
487492
if t.resolve(d) {
488493
t.dial(d, t.dest)
@@ -491,6 +496,10 @@ func (t *dialTask) run(d *dialScheduler) {
491496
}
492497
}
493498

499+
func (t *dialTask) needResolve() bool {
500+
return t.flags&staticDialedConn != 0 && t.dest.IP() == nil
501+
}
502+
494503
// resolve attempts to find the current endpoint for the destination
495504
// using discovery.
496505
//

‎p2p/discover/v4_udp.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ func makeEndpoint(addr *net.UDPAddr, tcpPort uint16) rpcEndpoint {
169169

170170
func (t *UDPv4) nodeFromRPC(sender *net.UDPAddr, rn rpcNode) (*node, error) {
171171
if rn.UDP <= 1024 {
172-
return nil, errors.New("low port")
172+
return nil, errLowPort
173173
}
174174
if err := netutil.CheckRelayIP(sender.IP, rn.IP); err != nil {
175175
return nil, err

‎p2p/simulations/adapters/types.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,5 +300,5 @@ func (n *NodeConfig) initEnode(ip net.IP, tcpport int, udpport int) error {
300300
}
301301

302302
func (n *NodeConfig) initDummyEnode() error {
303-
return n.initEnode(net.IPv4(127, 0, 0, 1), 0, 0)
303+
return n.initEnode(net.IPv4(127, 0, 0, 1), int(n.Port), 0)
304304
}

0 commit comments

Comments
 (0)