Lack of flash loan protection leads to reward manipulation
Summary
InSophonFarming contract, users earn points based on their staking activity. These points are later used to determine the distribution of rewards, typically in the form of tokens. The key functions involved in this process include deposit, withdraw, increaseBoost, and the internal calculations for accumulating points and determining pending rewards. The contract does not include checks or mechanisms to mitigate the risk of flash loan attacks, where an attacker could borrow a large amount of tokens, manipulate pool states, and quickly repay the loan within the same transaction.
Vulnerability Detail
Flash Loan Execution: An attacker takes out a flash loan to borrow a large amount of tokens.
Temporary Balance Inflation: The attacker stakes the borrowed tokens by calling the deposit function. This action temporarily inflates their balance and consequently their share of the pool.
Points Accumulation: Because the attacker now has a significantly larger balance, they accumulate points at an accelerated rate. This is due to the function _deposit updating their amount and recalculating accPointsPerShare.
Immediate Reward Claim: The attacker may call withdraw or increaseBoost to realize the points and potentially boost their balance further, depending on the contract's exact reward realization mechanism.
Loan Repayment: Within the same transaction, the attacker withdraws the staked tokens and repays the flash loan.
Excessive Points Accumulation: Despite the attacker's balance being temporarily inflated, they keep the points earned during the attack. This results in an unfair distribution of points, giving the attacker more points than they would have legitimately earned.
Impact
Unfair Reward Distribution: The attacker ends up with more points than they deserve, which later translates into a larger share of the rewards. This reduces the reward pool available to legitimate stakers.
Economic Disincentive: Honest users receive fewer rewards than expected, leading to dissatisfaction and potentially driving them away from the protocol.
Protocol Integrity: Repeated exploitation of this vulnerability can drain the reward pool, undermining the protocol's integrity and sustainability.
Implement mechanisms to track and limit the impact of flash loans, such as time-weighted average balances (TWAB), flash loan guards or minimum staking periods, to ensure that rewards are distributed fairly based on long-term staking.
Time-Weighted Average Balances (TWAB) Implementation:
TWAB Data Structures
struct TWAB {
uint256 balance;
uint256 timestamp;
}
struct UserInfo {
uint256 amount; // Current amount of LP tokens the user has
uint256 boostAmount; // Boosted value purchased by the user
uint256 depositAmount; // Remaining deposits not applied to boost purchases
uint256 rewardSettled; // Reward settled
uint256 rewardDebt; // Reward debt
TWAB[] twabs; // Array of TWABs
}
Helper Functions
The _updateTWAB function updates the user's time-weighted average balance whenever they interact with the contract by depositing, withdrawing, or increasing their boost. This ensures that the balance history is accurately recorded.
The _getTWAB function calculates the average balance over a specified period. This can be used to determine the reward distribution based on the average balance rather than the instantaneous balance, mitigating the risk of flash loan attacks.
Modified deposit, withdraw, and increaseBoost Functions
The deposit, withdraw, and increaseBoost functions are modified to call _updateTWAB after every balance-changing operation to ensure the TWAB is always up to date.
dimi6oni
high
Lack of flash loan protection leads to reward manipulation
Summary
In
SophonFarming
contract, users earn points based on their staking activity. These points are later used to determine the distribution of rewards, typically in the form of tokens. The key functions involved in this process includedeposit
,withdraw
,increaseBoost
, and the internal calculations for accumulating points and determining pending rewards. The contract does not include checks or mechanisms to mitigate the risk of flash loan attacks, where an attacker could borrow a large amount of tokens, manipulate pool states, and quickly repay the loan within the same transaction.Vulnerability Detail
deposit
function. This action temporarily inflates their balance and consequently their share of the pool._deposit
updating theiramount
and recalculatingaccPointsPerShare
.withdraw
orincreaseBoost
to realize the points and potentially boost their balance further, depending on the contract's exact reward realization mechanism.Impact
Code Snippet
SophonFarming::deposit
https://github.com/sherlock-audit/2024-05-sophon/blob/05059e53755f24ae9e3a3bb2996de15df0289a6c/farming-contracts/contracts/farm/SophonFarming.sol#L443-L451
SophonFarming::increaseBoost
https://github.com/sherlock-audit/2024-05-sophon/blob/05059e53755f24ae9e3a3bb2996de15df0289a6c/farming-contracts/contracts/farm/SophonFarming.sol#L631-L681
SophonFarming::withdraw
https://github.com/sherlock-audit/2024-05-sophon/blob/05059e53755f24ae9e3a3bb2996de15df0289a6c/farming-contracts/contracts/farm/SophonFarming.sol#L699-L742
Tool used
Manual Review
Recommendation
Implement mechanisms to track and limit the impact of flash loans, such as time-weighted average balances (TWAB), flash loan guards or minimum staking periods, to ensure that rewards are distributed fairly based on long-term staking.
Time-Weighted Average Balances (TWAB) Implementation:
TWAB Data Structures
Helper Functions
The
_updateTWAB
function updates the user's time-weighted average balance whenever they interact with the contract by depositing, withdrawing, or increasing their boost. This ensures that the balance history is accurately recorded. The_getTWAB
function calculates the average balance over a specified period. This can be used to determine the reward distribution based on the average balance rather than the instantaneous balance, mitigating the risk of flash loan attacks.Modified
deposit
,withdraw
, andincreaseBoost
FunctionsThe
deposit
,withdraw
, andincreaseBoost
functions are modified to call_updateTWAB
after every balance-changing operation to ensure the TWAB is always up to date.