MAX_TRUST_LIMIT might be too high
The smart contracts use an expensive sort function that could run out of gas if the list of stakers becomes too long. An attacker could add themselves as a staker to lengthen the list and cause a denial of service, though a limit of 100 stakers is in place.
Submitted by gpersoon
Both SumOfTrust.sol and CreditLimitByMedian.sol contain an expensive sort function. This is used by UserManager.sol via the functions getLockedAmount and getCreditLimit.
If the list of stakers would be very long then the sort would take up all the gas and revert. Attackers could make the list of stakers longer by voting in themselves (as soon as they have 3 accounts voted in), this would result in a griefing attack.
Luckily the number of stakers and borrowers is limited in the function updateTrust by applying a limit of MAX_TRUST_LIMIT.
However this limit is quite high (100), if that amount of stakers would be present then an out of gas error would probably occur with the sort. Note: there are also other for loops in the code that could have a similar problem, however sort is the most expensive.
Proof of Concept
- https://github.com/code-423n4/2021-10-union/blob/4176c366986e6d1a6b3f6ec0079ba547b040ac0f/contracts/user/SumOfTrust.sol#L98-L121
- https://github.com/code-423n4/2021-10-union/blob/4176c366986e6d1a6b3f6ec0079ba547b040ac0f/contracts/user/CreditLimitByMedian.sol#L107-L122
- https://github.com/code-423n4/2021-10-union/blob/4176c366986e6d1a6b3f6ec0079ba547b040ac0f/contracts/user/UserManager.sol#L594
- https://github.com/code-423n4/2021-10-union/blob/4176c366986e6d1a6b3f6ec0079ba547b040ac0f/contracts/user/UserManager.sol#L368
- https://github.com/code-423n4/2021-10-union/blob/4176c366986e6d1a6b3f6ec0079ba547b040ac0f/contracts/user/UserManager.sol#L50
- https://github.com/code-423n4/2021-10-union/blob/4176c366986e6d1a6b3f6ec0079ba547b040ac0f/contracts/user/UserManager.sol#L423-L427
Recommended Mitigation Steps
Do a test with a MAX_TRUST_LIMIT number of stakers and borrowers and check if the code still works.
Set the MAX_TRUST_LIMIT so that everything still works, probably include a margin for future changes in gas costs.
GeraldHost (Union Finance) acknowledged:
yes we have tested for this and landed on the 100 limit. We have plans to improve gas efficiency considerably in future.
GalloDaSballo (judge) commented:
The warden has indentify a griefing exploit that can be applied only under specific circumstances, I agree with the severity
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 sort algorithm has a gas cost that grows with the number of stakers, and the maximum allowed stakers (100) may still be too many for the sort to complete within a block's gas limit.
Impact
An attacker could cause a griefing attack by making the staker list longer, potentially causing out-of-gas errors in functions that use the sort, but the existing limit of 100 may mitigate this.
What the attacker needed
Attacker has 3 accounts voted in; attacker can vote in themselves as staker
The fix
The maximum number of stakers should be set to a value that keeps the sort within gas limits, with a margin for future gas cost changes, and tested with that many stakers.
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 smart contract has a function that sorts a list of people who have staked tokens. Sorting takes more gas (computational effort) as the list grows. If the list gets too long, the sorting uses up all the gas and the transaction fails, which could stop the contract from working 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.