VaultRouter cannot be used for deposits when it reaches the maximum deposit limit
The VaultRouter contract, when depositing to a vault, is treated as the depositor and thus subject to the vault's maximum deposit limit. An attacker can deposit through the router with the router as recipient and then withdraw in the same transaction, causing the router to reach its limit and preventing all future deposits via the router.
Submitted by 0xlemon
https://github.com/code-423n4/2024-12-bakerfi/blob/main/contracts/core/VaultBase.sol#L251
https://github.com/code-423n4/2024-12-bakerfi/blob/main/contracts/core/VaultRouter.sol#L116
Summary
VaultRouter cannot be used for deposits when it reaches the maximum deposit limit because this contract is the msg.sender to the vault and it is treated as a depositor who has a limit.
Vulnerability Details
When doing deposits to a vault from the VaultRouter the router does an external call to the vault meaning that in Vault’s case msg.sender will be the router itself. The protocol, however, enforces a max deposit limit for depositors. This means that after the VaultRouter reaches the vault’s getMaxDeposit() no one will be able to deposit to the vault using the router.
Since the vault looks at balanceOf(msg.sender) for the deposit limit, an attacker can use the router to deposit to the vault specifying the recipient to be the router itself and then immediately withdrawing in the same transaction so that his tokens won’t be stolen. He can do that to reach VaultRouter deposit limit and now no one will be able to deposit through the router.
function _depositInternal(uint256 assets, address receiver) private returns (uint256 shares) {
//...
// Check if deposit exceeds the maximum allowed per wallet
uint256 maxDepositLocal = getMaxDeposit();
if (maxDepositLocal > 0) {
@-> uint256 depositInAssets = (balanceOf(msg.sender) * _ONE) / tokenPerAsset();
uint256 newBalance = assets + depositInAssets;
if (newBalance > maxDepositLocal) revert MaxDepositReached();
}
//...
}
Impact
DoS of the router’s deposit functionality
Recommended mitigation steps
You can try to enforce the same deposit limit on the router level and give the router unlimited deposit limit
Same root cause different impact. S-2 mitigation would work.
While the finding here is interesting, according to the Supreme Court decisions, this should be a duplicate.
@Dravee - the recommended mitigation in S-2 is absolutely wrong. As I said previously it is a
depositlimit so taking thebalanceOf(receiver) does absolutely nothing but limit the amount a single user can “hold”.The correct way to fix S-2 would be to implement some kind of a mapping to account for any deposit and limit the amount
msg.sendercan deposit. This, however, doesn’t solve this finding. The root cause is not the same because here it is shown the interactions between VaultRouter and the Vault and it blocks the VaultRouter from depositing.
0xlemon, I agree. Additionally, making S-15 as primary instead of S-2 due to the wrong mitigation.
The current implementation of the deposit limit in the system is incorrect. This report is based on the issue that the sponsor implemented an erroneous mitigation measure. Therefore, this issue should be categorized under implementation problems related to the deposit limit. Relevant historical judgments:https://github.com/code-423n4/2024-08-chakra-findings/issues/33
In report s-2, it was mentioned:
“Additionally, considering the whitelist mechanism, if you want to limit the deposit amount for whitelisted addresses, it is recommended to create a mapping to store the deposit amount for each address, rather than checking the
balanceOf.”This point was not elaborated further because managing the deposit limit mapping is challenging. For example, if an address has a deposit limit of 1000, and it deposits 1000, increasing
depositLimit[addr1]by 1000. If this address then transfers shares to another addressaddr2, andaddr2withdraws, withdepositLimit[addr2] = 0, how should the deposit limit be deducted? If the deposit limit is not reduced, under the condition that the whitelist is not increased, the total assets in the system will only decrease over time.
shaflow2 - the problem in this issue is that the VaultRouter is the
msg.senderand since we are limitingmsg.senderit will get blocked after it reaches the limit. The problem in S-15 is thatbalanceOfwas used to account for the deposit limit. Do you see the difference? In my problem it doesn’t matter how this deposit limit is implemented when it limitsmsg.senderas it can be seen.For the mapping part first of all it would be highly unlikely for a user to just give (transfer) his tokens away to someone but even if they do why do we need to deduct the limit when transfering? As I said it doesn’t matter how much a single user holds, we only need to make sure he doesn’t deposit more than
maxDepositLocal
MrPotatoMagic (warden) commented:
@Dravee, I agree with klau5 and shaflow2. This issue should not be considered as separate.
According to the SC ruling here, if fixing the root cause (in a reasonable manner) resolves the issue, they’re dups. It is important to focus on the point of fixing the issue in reasonable manner here.
How can the issue actually be mitigated?
- If msg.sender is the router, take in another parameter
routerMsgSenderto thedeposit()/_depositInternal()function that stores themsg.senderof the router contract.- Implement an if check in
deposit()/_depositInternal()that whenmsg.sender == routeris true, we utilize the parameterrouterMsgSenderto check the limit as per the method suggested by S-2. I.e., “it is recommended to create a mapping to store the deposit amount for each address”.Both the issues are two sides of the same coin. A universal mitigation as mentioned above resolves both of them.
I’m agreeing with 0xlemon on this one, as reasonably fixing the other issue (by strictly fixing the other issue) would leave this current issue unfixed (unless already aware of this finding, which would be unreasonable).
chefkenji (BakerFi) disputed and commented:
Duplicate issue
Status: Mitigation confirmed. Full details in the report from 0xlemon.
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 vault enforces its deposit limit based on the caller (msg.sender), and when deposits go through VaultRouter, the router itself becomes the caller, so its balance counts toward the limit.
Impact
The router's deposit functionality is blocked, causing a denial of service for all users who rely on the router to deposit.
What the attacker needed
Access to the VaultRouter deposit function; ability to withdraw in the same transaction
The fix
The deposit limit should be enforced at the router level, giving the router an unlimited deposit limit, or the vault should track deposits per actual user rather than per caller.
How it was found
- Call the router to deposit into the vault, setting the recipient as the router itself.
- Immediately withdraw the deposited tokens in the same transaction to avoid loss.
- Repeat until the router's balance reaches the vault's maximum deposit limit.
- Now no one can deposit through the router because it has reached the limit.
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 vault contract limits how much each depositor can put in. When people deposit through a helper contract called VaultRouter, the vault sees the router as the depositor. An attacker can fill up the router's limit and then take their money back, leaving the router unable to deposit for anyone else.
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.