User Collateral Cap Check Issue
The contract checks a user's collateral cap before adding the new deposit amount, so a deposit can push the total above the cap. An attacker could deposit more than the allowed cap, potentially exceeding risk limits.
Source: https://github.com/sherlock-audit/2024-05-elfi-protocol-judging/issues/262
Found by
0x486776, 0xAadi, 0xPwnd, 0xrex, KrisRenZo, KupiaSec, Salem, ZeroTrust, chaduke, dany.armstrong90, debugging3, jah, nikhil840096, pashap9990
Summary
User Collateral can exceeds the cap andd deposit will still be processed
Vulnerability Detail
The check accountProps.getTokenAmount(token) > tradeTokenConfig.collateralUserCap is designed to ensure that a user's collateral does not exceed their specific cap. However, this validation occurs before the new deposit amount is added, thereby only verifying the current balance. Consequently, this can result in the user's collateral exceeding the cap once the deposit is processed.
Impact
this can result in the user's collateral exceeding the cap once the deposit is processed.
Code Snippet
POC
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;
import "forge-std/Test.sol";
import "../src/AssetsProcess.sol";
import "../src/mocks/MockERC20.sol";
import "../src/mocks/MockVault.sol";
import "../src/mocks/MockAccount.sol";
import "../src/mocks/MockAppTradeTokenConfig.sol";
import "../src/mocks/MockCommonData.sol";
contract AssetsProcessTest is Test {
AssetsProcess assetsProcess;
MockERC20 token;
MockVault vault;
MockAccount account;
MockAppTradeTokenConfig appTradeTokenConfig;
MockCommonData commonData;
address user = address(0x123);
function setUp() public {
token = new MockERC20("Mock Token", "MTK", 18);
vault = new MockVault();
account = new MockAccount();
appTradeTokenConfig = new MockAppTradeTokenConfig();
commonData = new MockCommonData();
assetsProcess = new AssetsProcess();
// Set up initial balances and allowances
token.mint(user, 1000 ether);
token.approve(address(vault), 1000 ether);
token.approve(address(assetsProcess), 1000 ether);
// Set up mock configurations
appTradeTokenConfig.setTradeTokenConfig(address(token), true, 500 ether, 100 ether);
commonData.setTradeTokenCollateral(address(token), 0);
}
function testUserCollateralCapCheckIssue() public {
// Set initial user balance to 90 ether
account.setTokenAmount(user, address(token), 90 ether);
// Deposit 20 ether, which should exceed the user cap of 100 ether
AssetsProcess.DepositParams memory params = AssetsProcess.DepositParams({
account: user,
token: address(token),
amount: 20 ether,
from: AssetsProcess.DepositFrom.MANUAL,
isNativeToken: false
});
// Expect the deposit to succeed despite exceeding the user cap
vm.prank(user);
assetsProcess.deposit(params);
// Check the final balance to confirm the vulnerability
uint256 finalBalance = account.getTokenAmount(user, address(token));
assertEq(finalBalance, 110 ether, "User collateral cap exceeded");
}
}
Tool used
Manual Review
Recommendation
Please find the updated check for the new deposit amount below:
uint256 newUserCollateralAmount = accountProps.getTokenAmount(token) + params.amount;
require(newUserCollateralAmount <= tradeTokenConfig.collateralUserCap, "CollateralUserCapOverflow");
This modification ensures that the user's total collateral, including the new deposit, does not exceed the predefined user cap.
Discussion
salemthedeveloper
This issue has been labeled won't fix but https://github.com/sherlock-audit/2024-05-elfi-protocol-judging/issues/6 it was just confirmed here too
which is a duplicate of https://github.com/sherlock-audit/2024-05-elfi-protocol-judging/issues/6
olaoyesalem
@sherlock-admin2 @sherlock-admin3 @Shogoki @rcstanciu @rcstanciu
sherlock-admin2
The protocol team fixed this issue in the following PRs/commits: https://github.com/0xCedar/elfi-perp-contracts/pull/41
sherlock-admin2
The Lead Senior Watson signed off on the fix.
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 cap check uses the current balance instead of the balance after the new deposit is added.
Impact
A user's collateral can exceed the cap after a deposit, potentially allowing more collateral than intended.
What the attacker needed
A user account with collateral below the cap; ability to call the deposit function
The fix
The check should be updated to include the new deposit amount before comparing to the cap.
How it was found
- Set up a user with an initial collateral balance below the cap.
- Call the deposit function with an amount that would make the total exceed the cap.
- Observe that the deposit succeeds and the final balance exceeds the cap.
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 collateral cap is a limit on how much a user can deposit as security. The contract checked the old balance before adding the new deposit, so a user could go over the limit. This means the safety limit could be bypassed.
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.