If a user approves junior vault tokens to WithdrawPeriphery, anyone can withdraw/redeem his/her token
The WithdrawPeriphery contract lets a caller specify any 'from' address when withdrawing or redeeming junior vault tokens, relying only on that address having approved the periphery. An attacker could therefore withdraw or redeem another user's tokens if that user had approved the periphery, including via a front-running attack.
Source: https://github.com/sherlock-audit/2022-10-rage-trade-judging/issues/79
Found by
Nyx, simon135, GimelSec, cccz, clems4ever
Summary
If users want to withdraw/redeem tokens by WithdrawPeriphery, they should approve token approval to WithdrawPeriphery, then call withdrawToken() or redeemToken().
But if users approve dnGmxJuniorVault to WithdrawPeriphery, anyone can withdraw/redeem his/her token.
Vulnerability Detail
Users should approve dnGmxJuniorVault before calling withdrawToken() or redeemToken():
function withdrawToken(
address from,
address token,
address receiver,
uint256 sGlpAmount
) external returns (uint256 amountOut) {
// user has approved periphery to use junior vault shares
dnGmxJuniorVault.withdraw(sGlpAmount, address(this), from);
...
function redeemToken(
address from,
address token,
address receiver,
uint256 sharesAmount
) external returns (uint256 amountOut) {
// user has approved periphery to use junior vault shares
dnGmxJuniorVault.redeem(sharesAmount, address(this), from);
...
For better user experience, we always use approve(WithdrawPeriphery, type(uint256).max). It means that if Alice approves the max amount, anyone can withdraw/redeem her tokens anytime.
Another scenario is that if Alice approves 30 amounts, she wants to call withdrawToken to withdraw 30 tokens. But in this case Alice should send two transactions separately, then an attacker can frontrun withdrawToken transaction and withdraw Alice’s token.
Impact
Attackers can frontrun withdraw/redeem transactions and steal tokens. And some UI always approves max amount, which means that anyone can withdraw users tokens.
Code Snippet
https://github.com/sherlock-audit/2022-10-rage-trade/blob/main/dn-gmx-vaults/contracts/periphery/WithdrawPeriphery.sol#L119-L120 https://github.com/sherlock-audit/2022-10-rage-trade/blob/main/dn-gmx-vaults/contracts/periphery/WithdrawPeriphery.sol#L139-L140
Tool used
Manual Review
Recommendation
Replace from parameter by msg.sender.
// user has approved periphery to use junior vault shares
dnGmxJuniorVault.withdraw(sGlpAmount, address(this), msg.sender);
// user has approved periphery to use junior vault shares
dnGmxJuniorVault.redeem(sharesAmount, address(this), msg.sender);
Discussion
0xDosa
Fix PR: https://github.com/RageTrade/delta-neutral-gmx-vaults/pull/45
0x00052
Fix looks good
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
The withdrawToken and redeemToken functions take a 'from' address from the caller and pass it to the vault without verifying that the caller is that address, so any approved user's tokens can be moved by anyone.
Impact
Attackers could front-run withdraw or redeem transactions and steal tokens, and because some interfaces approve the maximum amount, anyone could withdraw a user's tokens at any time.
What the attacker needed
Victim has approved the periphery contract; attacker can front-run or call directly
The fix
The fix replaces the caller-supplied 'from' parameter with msg.sender so the periphery only acts on behalf of the actual caller; the text states a fix PR was opened and reviewed as looking good.
How it was found
- A user approves the WithdrawPeriphery contract to spend their junior vault tokens.
- The user calls withdrawToken or redeemToken, or an attacker observes the pending transaction.
- The attacker calls the same function with the victim's address as the 'from' parameter.
- The vault transfers the victim's tokens to the attacker-chosen receiver.
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 can be given permission to move someone else's tokens, called an approval. Here the helper contract trusted a caller-supplied address instead of checking who was actually making the request, so anyone could act on behalf of a user who had approved it.
What is Access control?
A user can do something their role should not allow: reach an admin function, act on a feature they have not paid for, or skip a permission check. IDOR is the special case where the object belongs to someone else.