Users can not to buy/sell minimum credit allowed due to exactAmountIn condition
The validation in buyCreditMarket and sellCreditMarket compared the user's cash amount directly against the minimum credit threshold, so users could not buy or sell the minimum allowed credit when using exactAmountIn. This caused valid transactions to revert with CREDIT_LOWER_THAN_MINIMUM_CREDIT.
iam_emptyset Follow with ether_sky, 0x04bytes and 20 others
Originally disclosed on Code4rena
Submitted by iam_emptyset, also found by ether_sky, 0x04bytes, samuraii77, Jorgect, radin100 (1, 2), DanielArmstrong, 0xStalin, 0xanmol, carlos__alegre, inzinko, dhank, Infect3d (1, 2), hyh, 0xarno, elhaj, zarkk01, trachev, KupiaSec, 0xRobocop, ilchovski, 0xJoyBoy03, BoltzmannBrain, pkqs90, and said
https://github.com/code-423n4/2024-06-size/blob/8850e25fb088898e9cf86f9be1c401ad155bea86/src/libraries/actions/BuyCreditMarket.sol#L91 https://github.com/code-423n4/2024-06-size/blob/8850e25fb088898e9cf86f9be1c401ad155bea86/src/libraries/actions/SellCreditMarket.sol#L93
Impact
The minimum credit user can buy or sell can be gotten from state.riskConfig.minimumCreditBorrowAToken which was set as 5e6. However, in buyCreditMarket and sellCreditMarket user can not buy or sell minimun credit allowed or small amount above it due to the exactAmountIn condition he chose.
In buyCreditMarket: when user set params.exactAmountIn = true, The params.amount value will be cash he want to use to buy credit. Since the params.amount value is cash it can be set less than state.riskConfig.minimumCreditBorrowAToken (5e6) as long as when the value get converted to credit it will reach the state.riskConfig.minimumCreditBorrowAToken value. But, unfortunately, in validateBuyCreditMarket() whenever params.amount is less than state.riskConfig.minimumCreditBorrowAToken the transaction will revert due to below code present in the function.
if (params.amount < state.riskConfig.minimumCreditBorrowAToken) {
revert Errors.CREDIT_LOWER_THAN_MINIMUM_CREDIT(params.amount, state.riskConfig.minimumCreditBorrowAToken);
}
For example, the cash user can use to buy minimum credit allowed (5e6) can be calculated as in the code below, And the value should be less than the minimum credit allowed (5e6).
uint256 minimumCash = Math.mulDivUp(5e6, PERCENT, PERCENT + ratePerTenor);
In sellCreditMarket: when user set params.exactAmountIn = false, The params.amount will be the exact cash he want to receive. But he will also not be able to set the params.amount value to be less than state.riskConfig.minimumCreditBorrowAToken value due to the below code present in validateSellCreditMarket().
if (params.amount < state.riskConfig.minimumCreditBorrowAToken) {
revert Errors.CREDIT_LOWER_THAN_MINIMUM_CREDIT(params.amount, state.riskConfig.minimumCreditBorrowAToken);
}
Proof of Concept
For BuyCreditMarket: Copy and paste below code in /test/local/actions/BuyCreditMarket.t.sol:
function testFail_UserCanNotBuyMinimumCredit() public {
// alice deposit weth
_deposit(alice, weth, 100e18);
// bob deposit usdc
_deposit(bob, usdc, 200e6);
// alice create a borrow offer
_sellCreditLimit(alice, 0.03e18, 365 days);
// calculate cash from minimum credit borrowAToken allowed = 5e6
uint256 minimumCash = Math.mulDivUp(5e6, PERCENT, PERCENT + 0.03e18);
// Expect revert with an error CREDIT_LOWER_THAN_MINIMUM_CREDIT
uint256 debtPositionId = _buyCreditMarket(bob, alice, minimumCash, 365 days, true);
}
Then run the test with below command, the test should pass because it will revert with an error Errors.CREDIT_LOWER_THAN_MINIMUM_CREDIT.
forge test --mt testFail_UserCanNotBuyMinimumCredit
For SellCreditMarket: Copy and paste below code in /test/local/actions/SellCreditMarket.t.sol:
function testFail_UserCanNotSellMinimumCredit() public {
// alice deposit usdc
_deposit(alice, usdc, 200e6);
// bob deposit weth
_deposit(bob, weth, 100e18);
// alice create a loan offer
_buyCreditLimit(alice, block.timestamp + 365 days, YieldCurveHelper.pointCurve(365 days, 0.03e18));
// calculate cash from minimum credit borrowAToken allowed = 5e6
uint256 minimumCreditInCash = Math.mulDivUp(5e6, PERCENT, PERCENT + 0.03e18);
// Expect revert with an error CREDIT_LOWER_THAN_MINIMUM_CREDIT
uint256 debtPositionId = _sellCreditMarket(bob, alice, RESERVED_ID, minimumCreditInCash, 365 days, false);
}
Then run the test with below command, the test should pass because it will revert with an error Errors.CREDIT_LOWER_THAN_MINIMUM_CREDIT.
forge test --mt testFail_UserCanNotSellMinimumCredit
Recommended Mitigation Steps
In validateBuyCreditMarket() and validateSellCreditMarket() for BuyCreditMarket and SellCreditMarket libraries respectively, there is a need of considering the condition of params.exactAmountIn value. The check should be implemented as shown below.
For BuyCreditMarket library: inside validateBuyCreditMarket() replace below code:
if (params.amount < state.riskConfig.minimumCreditBorrowAToken) { // @audit 5e6 USDC
revert Errors.CREDIT_LOWER_THAN_MINIMUM_CREDIT(params.amount, state.riskConfig.minimumCreditBorrowAToken);
}
With the below code:
uint256 ratePerTenor = borrowOffer.getRatePerTenor(VariablePoolBorrowRateParams({variablePoolBorrowRate: state.oracle.variablePoolBorrowRate, variablePoolBorrowRateUpdatedAt: state.oracle.variablePoolBorrowRateUpdatedAt, variablePoolBorrowRateStaleRateInterval: state.oracle.variablePoolBorrowRateStaleRateInterval}), tenor);
if (params.exactAmountIn && params.amount < Math.mulDivUp(state.riskConfig.minimumCreditBorrowAToken, PERCENT, PERCENT + ratePerTenor)) {
revert Errors.CREDIT_LOWER_THAN_MINIMUM_CREDIT(params.amount, state.riskConfig.minimumCreditBorrowAToken);
} else if (!params.exactAmountIn && params.amount < state.riskConfig.minimumCreditBorrowAToken) {
revert Errors.CREDIT_LOWER_THAN_MINIMUM_CREDIT(params.amount, state.riskConfig.minimumCreditBorrowAToken);
}
For SellCreditMarket library: inside validateSellCreditMarket() replace below code:
if (params.amount < state.riskConfig.minimumCreditBorrowAToken) { // @audit 5e6 USDC
revert Errors.CREDIT_LOWER_THAN_MINIMUM_CREDIT(params.amount, state.riskConfig.minimumCreditBorrowAToken);
}
With the below code:
uint256 ratePerTenor = loanOffer.getRatePerTenor(VariablePoolBorrowRateParams({variablePoolBorrowRate: state.oracle.variablePoolBorrowRate, variablePoolBorrowRateUpdatedAt: state.oracle.variablePoolBorrowRateUpdatedAt, variablePoolBorrowRateStaleRateInterval: state.oracle.variablePoolBorrowRateStaleRateInterval}), tenor);
if (params.exactAmountIn && params.amount < state.riskConfig.minimumCreditBorrowAToken) {
revert Errors.CREDIT_LOWER_THAN_MINIMUM_CREDIT(params.amount, state.riskConfig.minimumCreditBorrowAToken);
} else if (!params.exactAmountIn && params.amount < Math.mulDivUp(state.riskConfig.minimumCreditBorrowAToken, PERCENT, PERCENT + ratePerTenor)) {
revert Errors.CREDIT_LOWER_THAN_MINIMUM_CREDIT(params.amount, state.riskConfig.minimumCreditBorrowAToken);
}
Assessed type
Invalid Validation
aviggiano (Size) confirmed and commented:
Fixed in https://github.com/SizeCredit/size-solidity/pull/125
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 validation compared the cash amount directly to the minimum credit amount without converting between cash and credit when exactAmountIn was set.
Impact
Users could not buy or sell the minimum allowed credit or small amounts above it, causing valid transactions to revert.
What the attacker needed
A credit market with minimum credit set; user chooses exactAmountIn; sufficient funds deposited
The fix
The validation should account for exactAmountIn by converting the minimum credit to cash when needed; the report says it was fixed in a pull request.
How it was found
- Deposit collateral for the seller and cash for the buyer.
- Create a borrow or loan offer with a chosen rate and tenor.
- Calculate the cash equivalent of the minimum credit amount.
- Call buyCreditMarket or sellCreditMarket with that cash amount and the exactAmountIn flag.
- Observe the transaction reverts with CREDIT_LOWER_THAN_MINIMUM_CREDIT.
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.
This is a smart contract bug in a credit market. The code checked the wrong number against a minimum limit, so some valid trades were rejected even though they should have been allowed.
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.