Some Iterators are not closed in emissions module Keeper
Source: https://github.com/sherlock-audit/2024-06-allora-judging/issues/83
Found by
imsrybr0
Summary
Some Iterators are not closed in emissions module Keeper
Vulnerability Detail
GetStakeRemovalsForBlock, GetDelegateStakeRemovalsForBlock, GetInferenceScoresUntilBlock, GetForecastScoresUntilBlock do not Close the Iterator they create.
For reference : https://docs.cosmos.network/main/build/packages/collections#iterateaccounts
Impact
Unless Keys, Values, KeyValues or Walk are used, a collection Iterator must be explicitly closed and here they aren't.
Code Snippet
func (k *Keeper) GetStakeRemovalsForBlock(
ctx context.Context,
blockHeight BlockHeight,
) ([]types.StakeRemovalInfo, error) {
ret := make([]types.StakeRemovalInfo, 0)
rng := collections.NewPrefixedTripleRange[BlockHeight, TopicId, ActorId](blockHeight)
iter, err := k.stakeRemovalsByBlock.Iterate(ctx, rng) // <===== Audit
if err != nil {
return ret, err
}
for ; iter.Valid(); iter.Next() {
val, err := iter.Value()
if err != nil {
return ret, err
}
ret = append(ret, val)
}
return ret, nil
}
GetDelegateStakeRemovalsForBlock
func (k *Keeper) GetDelegateStakeRemovalsForBlock(
ctx context.Context,
blockHeight BlockHeight,
) ([]types.DelegateStakeRemovalInfo, error) {
ret := make([]types.DelegateStakeRemovalInfo, 0)
rng := NewSinglePrefixedQuadrupleRange[BlockHeight, TopicId, ActorId, ActorId](blockHeight)
iter, err := k.delegateStakeRemovalsByBlock.Iterate(ctx, rng) // <===== Audit
if err != nil {
return ret, err
}
for ; iter.Valid(); iter.Next() {
val, err := iter.Value()
if err != nil {
return ret, err
}
ret = append(ret, val)
}
return ret, nil
}
func (k *Keeper) GetInferenceScoresUntilBlock(ctx context.Context, topicId TopicId, blockHeight BlockHeight) ([]*types.Score, error) {
rng := collections.
NewPrefixedPairRange[TopicId, BlockHeight](topicId).
EndInclusive(blockHeight).
Descending()
scores := make([]*types.Score, 0)
iter, err := k.infererScoresByBlock.Iterate(ctx, rng) // <===== Audit
if err != nil {
return nil, err
}
// Get max number of time steps that should be retrieved
moduleParams, err := k.GetParams(ctx)
if err != nil {
return nil, err
}
maxNumTimeSteps := moduleParams.MaxSamplesToScaleScores
count := 0
for ; iter.Valid() && count < int(maxNumTimeSteps); iter.Next() {
existingScores, err := iter.KeyValue()
if err != nil {
return nil, err
}
for _, score := range existingScores.Value.Scores {
scores = append(scores, score)
count++
}
}
return scores, nil
}
func (k *Keeper) GetForecastScoresUntilBlock(ctx context.Context, topicId TopicId, blockHeight BlockHeight) ([]*types.Score, error) {
rng := collections.
NewPrefixedPairRange[TopicId, BlockHeight](topicId).
EndInclusive(blockHeight).
Descending()
scores := make([]*types.Score, 0)
iter, err := k.forecasterScoresByBlock.Iterate(ctx, rng) // <===== Audit
if err != nil {
return nil, err
}
// Get max number of time steps that should be retrieved
moduleParams, err := k.GetParams(ctx)
if err != nil {
return nil, err
}
maxNumTimeSteps := moduleParams.MaxSamplesToScaleScores
count := 0
for ; iter.Valid() && count < int(maxNumTimeSteps); iter.Next() {
existingScores, err := iter.KeyValue()
if err != nil {
return nil, err
}
for _, score := range existingScores.Value.Scores {
scores = append(scores, score)
count++
}
}
return scores, nil
}
Tool used
Manual Review
Recommendation
Explicitly Close the Iterators with "defer iter.Close()"
Discussion
sherlock-admin2
The protocol team fixed this issue in the following PRs/commits: https://github.com/allora-network/allora-chain/pull/362
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
What is known
A medium-severity doS bug in Allora. Sherlock made it public on 10 August 2024. Found in the 454 audit contest (finding M-17).
About DoS
A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
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.