Frontrunning of setPerTokenWalletCap edge case
The owner must manually check the maximum LP position before reducing the per-token wallet cap, but a user can add liquidity between the check and the update, leaving that user above the new cap. This can cause denial-of-service conditions for that user and break assumptions in other functions.
Originally disclosed on Code4rena
Submitted by sirhashalot
The setPerTokenWalletCap() function in WhitelistPeriodManager.sol contains a comment stating:
Special care must be taken when calling this function
There are no checks for _perTokenWalletCap (since it's onlyOwner), but it's essential that it should be >= max lp provided by an lp.
Checking this on chain will probably require implementing a bbst, which needs more bandwidth
Call the view function getMaxCommunityLpPositon() separately before changing this value
Even if the manual step of calling the getMaxCommunityLpPositon() function is properly performed, it is possible for a user to add liquidity to increase the maxLp value in between when the getMaxCommunityLpPositon() function is called and when the setPerTokenWalletCap() function is called. Because this process is manual, this doesn’t need to be bot frontrunning in the same block as when the setPerTokenWalletCap() function is called, but can be cause by poor timing of an innocent unknowing user adding liquidity to the protocol. If this condition occurs, the liquidity provider will have provided more liquidity than the perTokenWalletCap limit, breaking the assumptions for this variable and leading to some denial of service conditions.
This edge situation can impact the setTotalCap() function and the “perTokenTotalCap[_token]” state variable as well, but the “perTokenWalletCap[_token]” value would have to be reduced before the “perTokenTotalCap[_token]” value is reduced. The impact to setTotalCap() follows the same execution path but adds the additional step of calling the setTotalCap() function at the end of the process.
Proof of Concept
- Owner calls
getMaxCommunityLpPositon(_token)function to identify maxLp value to confirm new perTokenWalletCap value is below maxLp value - An innocent user adds liquidity to their position without the knowledge that the owner is going to reduce the “perTokenWalletCap[_token]” value soon
- Owner calls
setPerTokenWalletCap()functionto reduce “perTokenWalletCap[_token]” value - The innocent user has more liquidity than the new “perTokenWalletCap[_token]” value. This means that the user can be in a situation where if they remove x amount of liquidity and attempt to add x liquidity back to their position, the innocent user will be unable to do so. Other functions that rely on the assumption that the largest user deposit is below the “perTokenWalletCap[_token]” value may break due to incorrect assumptions
This edge situation can impact the setTotalCap() function and the “perTokenTotalCap[_token]” state variable as well, but the “perTokenWalletCap[_token]” value would have to be reduced before the “perTokenTotalCap[_token]” value is reduced. The impact to setTotalCap() follows the same execution path but adds the additional step of calling the setTotalCap() function at the end of the process.
Recommended Mitigation Steps
A programmatic solution is the only way to avoid these edge case scenarios, though it will increase gas consumption. To convert the manual calling of getMaxCommunityLpPositon(_token) to a programmatic solution, add the following require statement next to the existing require statement of the setPerTokenWalletCap() function:
require(_perTokenWalletCap <= getMaxCommunityLpPositon(_token), "ERR_PWC_GT_MCLP");
ankurdubey521 (Biconomy) acknowledged
The concern is valid but I do not think that there is any profit for the attacker, and the impact for the regular users is minimal because this value can be updated anytime again by the owner, so I am hesitating if this should be of medium severity or lower, but because the warden provided a nice and comprehensive description, I will leave this in favor of warden.
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 cap update relies on a manual, off-chain check of the current maximum LP position instead of enforcing the relationship on-chain, so the state can change between the check and the update.
Impact
The affected user may be unable to remove and re-add their liquidity, and other functions that assume the largest deposit is below the cap may break, causing denial-of-service conditions; the judge noted there is no attacker profit and the owner can update the value again.
What the attacker needed
Owner lowers the per-token wallet cap; a user adds liquidity before the update
The fix
The check should be enforced programmatically by requiring the new per-token wallet cap to be less than or equal to the current maximum community LP position.
How it was found
- The owner calls getMaxCommunityLpPositon to find the current maximum LP value.
- A user adds liquidity without knowing the owner plans to lower the per-token wallet cap.
- The owner calls setPerTokenWalletCap to reduce the per-token wallet cap.
- The user now holds more liquidity than the new cap, so they cannot remove and re-add that amount, and other functions relying on the cap assumption may break.
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 lets the owner lower a limit on how much liquidity each wallet can hold. The owner first checks the current largest wallet, then lowers the limit in a separate transaction. If someone adds liquidity in between, that person can end up above the new limit, which can stop them from adding liquidity again and may break other parts of the contract.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.