emissions/keeper/GetIdsOfActiveTopics may always return empty array []
The function GetIdsOfActiveTopics uses a pagination method that starts from key 0, but if the active topics have IDs that do not start at 0, it returns an empty array. An attacker could create many topics that are never activated, causing the function to always return empty, so topic weights are not updated and rewards are not processed.
Kow Follow with LZ_security and imsrybr0
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2024-06-allora-judging/issues/91
Found by
Kow, LZ_security, imsrybr0
Summary
GetIdsOfActiveTopics may always return empty array, causing topic weights to not be updated.
Vulnerability Detail
IterateRaw is used in the GetIdsOfActiveTopics function to iterate over the map
startKey := make([]byte, binary.MaxVarintLen64)
binary.BigEndian.PutUint64(startKey, start)
nextKey := make([]byte, binary.MaxVarintLen64)
binary.BigEndian.PutUint64(nextKey, start+limit)
rng, err := k.activeTopics.IterateRaw(ctx, startKey, nextKey, collections.OrderAscending)
if err != nil {
return nil, nil, err
}
activeTopics, err := rng.Keys()
if err != nil {
return nil, nil, err
}
startKey starts from 0: topicPageKey := make([]byte, 0)
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
}
......
}
The problem is that if the element in activeTopics does not start at 0, IterateRaw will return less data than the number of pageLimit.
Suppose the elements in activeTopics are 20 to 100, startKey is 0, pageLimit is 10,
IterateRaw returns a null value.
If null is returned, GetIdsOfActiveTopics returns nextKey starting from 0,
SafeApplyFuncOnAllActiveEpochEndingTopics function of topicPageKey has not updated, so has been unable to get to the value.
// If there are no topics, we return the nil for next key
if activeTopics == nil {
nextKey = make([]byte, 0)
}
return activeTopics, &types.SimpleCursorPaginationResponse{
NextKey: nextKey,
}, nil
The following test code demonstrates a case where GetIdsOfActiveTopics can't get the value:
func (s *KeeperTestSuite) TestGetActiveTopics1() {
ctx := s.ctx
keeper := s.emissionsKeeper
for i := 20; i < 100; i++ {
topic1 := types.Topic{Id: uint64(i)}
_ = keeper.SetTopic(ctx, topic1.Id, topic1)
_ = keeper.ActivateTopic(ctx, topic1.Id)
}
topicPageKey := make([]byte, 0)
pagination := &types.SimpleCursorPaginationRequest{
Key: topicPageKey,
Limit: 10,
}
activeTopics, topicPageResponse, _ := keeper.GetIdsOfActiveTopics(ctx, pagination)
fmt.Println(topicPageResponse.NextKey)
fmt.Println(len(activeTopics))
}
Put the test code into the test file: allora-chain/x/emissions/keeper/keeper_test.go
cd allora-chain/x/emissions/keeper/ go test -v -run TestKeeperTestSuite/TestGetActiveTopics1
Print input as follows::
=== RUN TestKeeperTestSuite
=== RUN TestKeeperTestSuite/TestGetActiveTopics1
[]
0
--- PASS: TestKeeperTestSuite (0.00s)
--- PASS: TestKeeperTestSuite/TestGetActiveTopics1 (0.00s)
PASS
Let's look at the call flow: emissions/module/acbi/EndEndBlocker -> rewards.GetAndUpdateActiveTopicWeights -> SafeApplyFuncOnAllActiveEpochEndingTopics -> k.GetIdsOfActiveTopics
If GetIdsOfActiveTopics always returns empty [], topic weights will not be updated and topic Rewards will not be processed.
After a topic is created, not all topics will be activated. If an attacker(or just normal users who want to keep some topics) creates pageLimit number of topics while the network is online, and these topics never enter Activate state, it will happen that the GetIdsOfActiveTopics function will always return a null value.
Impact
GetIdsOfActiveTopics always return empty array, causing topic weights to not be updated.
Code Snippet
Tool used
Manual Review
Recommendation
Use other paging methods
Discussion
sherlock-admin2
The protocol team fixed this issue in the following PRs/commits: https://github.com/allora-network/allora-chain/pull/406
WangSecurity
This issue will be duplicated with #60 based on the discussion here https://github.com/sherlock-audit/2024-06-allora-judging/issues/115#issuecomment-2291343224
zhaojio
This issue will be duplicated with #60 based on the discussion here #115 (comment)
#91 is not duplicate with #60, #60 and other reports do not describe attack by attacker, their impact is different.
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 pagination logic uses a start key of 0 and a fixed limit, but the underlying iterator returns no results if the first key in the range is not present, causing an empty page and a next key that never advances.
Impact
Topic weights are not updated and topic rewards are not processed, potentially affecting all topics.
What the attacker needed
Ability to create topics; topics not activated
The fix
The protocol team fixed this issue in a pull request.
How it was found
- Create many topics (at least pageLimit) that are never activated.
- Call GetIdsOfActiveTopics with a start key of 0 and a limit.
- Observe that it returns an empty array and a next key of 0.
- The pagination loop in SafeApplyFuncOnAllActiveEpochEndingTopics never advances, so topic weights are not updated.
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-based rewards system. The system tries to list active topics in pages, but it starts looking from the very first possible ID (0). If no active topic has an ID near 0, the listing returns nothing, so the system thinks there are no active topics and never updates their weights or rewards. An attacker can make this happen by creating many topics that are never activated, blocking the reward updates for everyone.
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.