Incomplete Topic Processing Due to Continuous Retry on Pagination Error
The function that applies a callback to all active topics with an epoch ending at the current block uses a continue statement when fetching a page of topic IDs fails, causing it to retry the same page indefinitely. An attacker could trigger a persistent error to cause an infinite loop, skip all topic processing, and waste resources.
Source: https://github.com/sherlock-audit/2024-06-allora-judging/issues/80
Found by
0x416, defsec
Summary
The SafeApplyFuncOnAllActiveEpochEndingTopics function continues to the next iteration when failing to get IDs of active topics, potentially causing an infinite loop or skipping all topics.
Vulnerability Detail
In the current implementation, when k.GetIdsOfActiveTopics() fails, the function logs a warning and continues to the next iteration of the main loop. This behavior can lead to repeated failures and potentially skip processing all topics.
Description: The problematic code section is:
topicsActive, topicPageResponse, err := k.GetIdsOfActiveTopics(ctx, topicPageRequest)
if err != nil {
Logger(ctx).Warn(fmt.Sprintf("Error getting ids of active topics: %s", err.Error()))
continue // This should be 'break'
}
This continue statement causes the function to retry getting the same page of topic IDs indefinitely if there's a persistent error, without moving to the next page or terminating the loop.
Impact
- Potential infinite loop: If the error persists, the function may never terminate.
- Skipped topic processing: All topics may be skipped if the first page consistently fails to load.
- Resource waste: Continuous retries of a failing operation waste computational resources.
- Misleading behavior: The function appears to complete successfully but may not have processed any topics.
Code Snippet
// Apply a function on all active topics that also have an epoch ending at this block
// Active topics have more than a globally-set minimum weight, a function of revenue and stake
// "Safe" because bounded by max number of pages and apply running, online operations.
func SafeApplyFuncOnAllActiveEpochEndingTopics(
ctx sdk.Context,
k keeper.Keeper,
block BlockHeight,
fn func(sdkCtx sdk.Context, topic *types.Topic) error,
topicPageLimit uint64,
maxTopicPages uint64,
) error {
topicPageKey := make([]byte, 0)
i := uint64(0)
for {
topicPageRequest := &types.SimpleCursorPaginationRequest{Limit: topicPageLimit, Key: topicPageKey}
topicsActive, topicPageResponse, err := k.GetIdsOfActiveTopics(ctx, topicPageRequest)
if err != nil {
Logger(ctx).Warn(fmt.Sprintf("Error getting ids of active topics: %s", err.Error()))
continue
}
for _, topicId := range topicsActive {
topic, err := k.GetTopic(ctx, topicId)
if err != nil {
Logger(ctx).Warn(fmt.Sprintf("Error getting topic: %s", err.Error()))
continue
}
if k.CheckCadence(block, topic) {
// All checks passed => Apply function on the topic
err = fn(ctx, &topic)
if err != nil {
Logger(ctx).Warn(fmt.Sprintf("Error applying function on topic: %s", err.Error()))
continue
}
}
}
// if pageResponse.NextKey is empty then we have reached the end of the list
if topicsActive == nil || i > maxTopicPages {
break
}
topicPageKey = topicPageResponse.NextKey
i++
}
return nil
}
Tool used
Manual Review
Recommendation
Change the continue statement to break when failing to get IDs of active topics:
topicsActive, topicPageResponse, err := k.GetIdsOfActiveTopics(ctx, topicPageRequest)
if err != nil {
Logger(ctx).Error(fmt.Sprintf("Error getting ids of active topics: %s", err.Error()))
break // Exit the loop instead of continuing
}
Discussion
sherlock-admin4
2 comment(s) were left on this issue during the judging contest.
0xmystery commented:
Function loop
continues even after error when it should likelybreak
0xmystery commented:
Errors in
RemoveStakes/RemoveDelegateStakesare silently handled inEndBlocker
sherlock-admin2
The protocol team fixed this issue in the following PRs/commits: https://github.com/allora-network/allora-chain/pull/437
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
Likely, the error handling uses continue instead of break, so a persistent error causes an infinite retry loop on the same page.
Impact
If the error persists, the function may never terminate, all topics may be skipped, and resources are wasted; the function appears to complete successfully but may not have processed any topics.
What the attacker needed
Persistent error in GetIdsOfActiveTopics
The fix
The continue statement should be changed to break when failing to get IDs of active topics, and the protocol team fixed this issue in a pull request.
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 is a bug in a blockchain function that processes topics. When it fails to get a list of topics, it keeps trying the same request forever instead of stopping, which can freeze the system and skip all work.
What is DoS?
A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.