WantedPeerDials() branch will never be executed
The report identifies a logic flaw in the Prysm Ethereum client's peer discovery loop where the check for zero wanted peer dials is unreachable because a prior peer limit check always causes a continue when the peer count reaches the maximum. An attacker cannot exploit this; it is a code quality issue with no direct security impact.
Originally disclosed on Immunefi
- Disclosed after under an hour
#37148 [BC-Insight] `wantedPeerDials()` branch will never be executed
Submitted on Nov 26th 2024 at 19:59:03 UTC by @br0nz3p1ck4x3 for Attackathon | Ethereum Protocol
- Report ID: #37148
- Report Type: Blockchain/DLT
- Report severity: Insight
- Target: https://github.com/prysmaticlabs/prysm
- Impacts:
- (Specifications) A bug in specifications with no direct impact on client implementations
Description
Hey Triagers, Ethereum Foundation, Prysm!
We encountered the following during our research and we think it can add value to the Prysm team.
Description
Inside prysm/beacon-chain/p2p/discovery.go::listenForNodes() we can find the following:
func (s *Service) listenForNewNodes() {
//..
if s.isPeerAtLimit(false /* inbound */) {
// Pause the main loop for a period to stop looking
// for new peers.
log.Trace("Not looking for peers, at peer limit")
time.Sleep(pollingPeriod)
continue
}
wantedCount := s.wantedPeerDials()
if wantedCount == 0 {
log.Trace("Not looking for peers, at peer limit")
time.Sleep(pollingPeriod)
continue
}
//..
}
There is a problem here though in the logic.
To showcase this, we will first explain it with arbitrary values. After reading this, please run the Proof of Concept attached to the report.
Alright.
If we look at isPeerAtLimit(), we see the following:
func (s *Service) isPeerAtLimit(inbound bool) bool {
numOfConns := len(s.host.Network().Peers())
maxPeers := int(s.cfg.MaxPeers)
// If we are measuring the limit for inbound peers
// we apply the high watermark buffer.
if inbound {
maxPeers += highWatermarkBuffer
maxInbound := s.peers.InboundLimit() + highWatermarkBuffer
currInbound := len(s.peers.InboundConnected())
// Exit early if we are at the inbound limit.
if currInbound >= maxInbound {
return true
}
}
activePeers := len(s.Peers().Active())
return activePeers >= maxPeers || numOfConns >= maxPeers
}
Let's take the following arbitrary values that maximize the numbers such that the return statement will return true:
numOfConns = 9maxPeers = 10activePeers = 9
These values will evaluate to true in this return statement:
activePeers >= maxPeers || numOfConns >= maxPeers
Now, we go back to listenForNewNodes():
func (s *Service) listenForNewNodes() {
//..
if s.isPeerAtLimit(false /* inbound */) {
// Pause the main loop for a period to stop looking
// for new peers.
log.Trace("Not looking for peers, at peer limit")
time.Sleep(pollingPeriod)
continue
}
wantedCount := s.wantedPeerDials()
if wantedCount == 0 {
log.Trace("Not looking for peers, at peer limit")
time.Sleep(pollingPeriod)
continue
}
//..
}
Now, wantedPeerDials() is called. If we look at the implementation:
func (s *Service) wantedPeerDials() int {
maxPeers := int(s.cfg.MaxPeers)
activePeers := len(s.Peers().Active())
wantedCount := 0
if maxPeers > activePeers {
wantedCount = maxPeers - activePeers
}
return wantedCount
}
The problem here is the following if-statement:
if maxPeers > activePeers {
As we saw in isPeerAtLimit(), which was called before wantedPeerDials(), maxPeers will always be bigger than activePeers. If it was not bigger than activePeers, it would enter the if-statement and continue:
func (s *Service) listenForNewNodes() {
//..
if s.isPeerAtLimit(false /* inbound */) {
// Pause the main loop for a period to stop looking
// for new peers.
-> log.Trace("Not looking for peers, at peer limit")
-> time.Sleep(pollingPeriod)
-> continue
}
wantedCount := s.wantedPeerDials()
if wantedCount == 0 {
log.Trace("Not looking for peers, at peer limit")
time.Sleep(pollingPeriod)
continue
}
//..
}
Thus, wantedPeerDials() will always return a non-nil value and thus, this if-statement inside listenForNewNodes() will never be executed:
func (s *Service) listenForNewNodes() {
//..
if s.isPeerAtLimit(false /* inbound */) {
// Pause the main loop for a period to stop looking
// for new peers.
log.Trace("Not looking for peers, at peer limit")
time.Sleep(pollingPeriod)
continue
}
wantedCount := s.wantedPeerDials()
-> if wantedCount == 0 {
log.Trace("Not looking for peers, at peer limit")
time.Sleep(pollingPeriod)
continue
}
//..
}
Proof of Concept
Proof of Concept
cd prysm/beacon-chain/p2pvim discovery_test.go
Paste the following test in this file:
func TestPoc(t *testing.T) {
// - `numOfConns = 9`
// - `maxPeers = 10`
// - `activePeers = 9`
fakePeer := testp2p.NewTestP2P(t)
s := &Service{
cfg: &Config{MaxPeers: 10},
ipLimiter: leakybucket.NewCollector(ipLimit, ipBurst, 1*time.Second, false),
peers: peers.NewStatus(context.Background(), &peers.StatusConfig{
PeerLimit: 10,
ScorerParams: &scorers.Config{},
}),
host: fakePeer.BHost,
}
for i := 0; i < 9; i++ {
_ = addPeer(t, s.peers, peerdata.PeerConnectionState(ethpb.ConnectionState_CONNECTED), false)
}
// We will make two cases, one case where there are 9 peers with a limit of 10 maxPeers
fmt.Print("Current setup: 9 peers with a limit of 10 peers\n")
isAtLimit := s.isPeerAtLimit(false)
fmt.Print("Has the limit been hit? ", isAtLimit, "\n")
wantedPeers := s.wantedPeerDials()
fmt.Print("Wanted Peers:", wantedPeers, "\n")
// Add one more peer so we hit the limit
_ = addPeer(t, s.peers, peerdata.PeerConnectionState(ethpb.ConnectionState_CONNECTED), false)
fmt.Print("Current setup: 10 peers with a limit of 10 peers\n")
isAtLimit = s.isPeerAtLimit(false)
fmt.Print("Has the limit been hit? ", isAtLimit, "\n")
fmt.Print("This will never execute because the previous `isPeerAtLimit()` call will lead to `continue` and replaying the loop\n")
wantedPeers = s.wantedPeerDials()
fmt.Print("Wanted Peers:", wantedPeers, "\n")
}
- Run using:
go test -timeout 30s -run ^TestPoc
The output will be:
Current setup: 9 peers with a limit of 10 peers
Has the limit been hit? false
Wanted Peers:1
Current setup: 10 peers with a limit of 10 peers
Has the limit been hit? true
This will never execute because the previous `isPeerAtLimit()` call will lead to `continue` and replaying the loop
Wanted Peers:0
PASS
ok github.com/prysmaticlabs/prysm/v5/beacon-chain/p2p 0.627s
As we can see, the only way to get 0wanted peers is for activePeers == maxPeers, but as shown, this can never happen because the for-loop inside listenForNewNodes() will return early because of:
if s.isPeerAtLimit(false /* inbound */) {
// Pause the main loop for a period to stop looking
// for new peers.
log.Trace("Not looking for peers, at peer limit")
time.Sleep(pollingPeriod)
continue
}
Recommended Patch
Consider removing the wantedPeerDials() usage since it can not be invoked right now.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
The function `wantedPeerDials()` is called only after `isPeerAtLimit()` returns false, which guarantees that `maxPeers > activePeers`, so `wantedPeerDials()` always returns a positive number and the subsequent zero check is dead code.
Impact
This is a logic flaw with no direct security impact; it only results in dead code and does not affect peer discovery behaviour.
What the attacker needed
None
The fix
The code should be fixed by removing the unreachable `wantedPeerDials()` check or refactoring the logic to avoid redundancy.
How it was found
- Set up a test service with MaxPeers=10 and add 9 peers.
- Call isPeerAtLimit(false) and observe it returns false.
- Call wantedPeerDials() and observe it returns 1.
- Add one more peer to reach 10 peers.
- Call isPeerAtLimit(false) and observe it returns true.
- Call wantedPeerDials() and observe it returns 0, but note this branch is unreachable in the actual loop.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
This report is about a piece of code that checks how many new peers a node wants to connect to. The code has a redundant check that can never be true because an earlier check already handles the case. It is not a security vulnerability; it is just a logic error that makes some code dead.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.