Potential race conditions due to usage of ````sdk.Context```` in concurrent goroutines
The TopicsHandler methods use sdk.Context in concurrent goroutines, which is not thread-safe, potentially causing race conditions and data corruption. An attacker could exploit this to cause unpredictable behaviour or crashes.
0xsi Follow with KingNFT and Minato7namikazi
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2024-06-allora-judging/issues/87
Found by
0xsi, KingNFT, Minato7namikazi
Summary
The implementation of the TopicsHandler methods uses sdk.Context in concurrent goroutines, which can lead to race conditions and unexpected results.
Vulnerability Detail
In the TopicsHandler struct, both requestTopicWorkers and requestTopicReputers methods use the sdk.Context object within goroutines. sdk.Context is not thread-safe, and concurrent access to it can lead to race conditions and data corruption.
Here's an example of the problematic code:
func (th *TopicsHandler) requestTopicWorkers(ctx sdk.Context, topic emissionstypes.Topic) {
...
go generateInferencesRequest(ctx, topic.InferenceLogic, topic.InferenceMethod, topic.DefaultArg, topic.Id, topic.AllowNegative, *nonceCopy)
}
func (th *TopicsHandler) requestTopicReputers(ctx sdk.Context, topic emissionstypes.Topic) {
...
go generateLossesRequest(ctx, reputerValueBundle, topic.LossLogic, topic.LossMethod, topic.Id, topic.AllowNegative, *nonceCopy.ReputerNonce, *nonceCopy.WorkerNonce, previousBlockApproxTime)
}
In the PrepareProposalHandler method, these functions are called within goroutines, passing the ctx object:
func (th *TopicsHandler) PrepareProposalHandler() sdk.PrepareProposalHandler {
return func(ctx sdk.Context, req *abci.RequestPrepareProposal) (*abci.ResponsePrepareProposal, error) {
...
for _, churnableTopicId := range churnableTopics {
wg.Add(1)
go func(topicId TopicId) {
defer wg.Done()
topic, err := th.emissionsKeeper.GetTopic(ctx, topicId)
if err != nil {
Logger(ctx).Error("Error getting topic: " + err.Error())
return
}
th.requestTopicWorkers(ctx, topic)
th.requestTopicReputers(ctx, topic)
}(churnableTopicId)
}
wg.Wait()
return &abci.ResponsePrepareProposal{Txs: req.Txs}, nil
}
}
Using ctx in multiple goroutines without synchronization can cause race conditions.
Impact
The use of sdk.Context in concurrent goroutines can lead to several issues: (1) Race Conditions: Concurrent access to ctx can result in inconsistent or corrupted state, leading to unpredictable behavior and potential application crashes. (2) Data Corruption: Shared state within ctx may be modified by multiple goroutines simultaneously, causing data corruption and unexpected results.
Code Snippet
Tool used
Manual Review
Recommendation
To avoid race conditions and ensure thread safety, create a copy of sdk.Context for each goroutine. The WithContext method can be used to create a new context from an existing one.
Discussion
sherlock-admin2
The protocol team fixed this issue in the following PRs/commits: https://github.com/allora-network/allora-chain/pull/494
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 code passes a single sdk.Context instance to multiple goroutines without synchronisation, but sdk.Context is not thread-safe.
Impact
Concurrent access to ctx could cause inconsistent or corrupted state, leading to unpredictable behaviour and potential application crashes.
What the attacker needed
None
The fix
The protocol team fixed this issue by creating a copy of sdk.Context for each goroutine, as stated in the discussion.
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.
A race condition happens when two parts of a program try to use the same data at the same time, and the result depends on timing. Here, the code starts several background tasks that all share the same context object, which is not designed for that, so they can interfere with each other and corrupt data.
What is Race condition?
Parallel requests reach a state the code assumed could not happen: a coupon used twice, a limit overrun, a check skipped.