Platform: Code4rena
Start Date: 12/08/2022
Pot Size: $50,000 USDC
Total HM: 15
Participants: 120
Period: 5 days
Judge: Justin Goro
Total Solo HM: 6
Id: 153
League: ETH
Rank: 36/120
Findings: 2
Award: $70.40
🌟 Selected for report: 0
🚀 Solo Findings: 0
🌟 Selected for report: 0x1f8b
Also found by: 0x52, 0xA5DF, 0xDjango, 0xNazgul, 0xNineDec, 0xSmartContract, 0xmatt, 0xsolstars, Aymen0909, Bnke0x0, CertoraInc, Chom, CodingNameKiki, Deivitto, Dravee, ElKu, EthLedger, Funen, IllIllI, JC, Junnon, Lambda, LeoS, MiloTruck, Noah3o6, PaludoX0, ReyAdmirado, Rohan16, RoiEvenHaim, Rolezn, SaharAP, Sm4rty, SooYa, The_GUILD, TomJ, Waze, Yiko, _Adam, __141345__, a12jmx, ak1, asutorufos, auditor0517, ayeslick, ballx, beelzebufo, berndartmueller, bin2chen, brgltd, c3phas, cRat1st0s, cccz, cryptonue, cryptphi, d3e4, delfin454000, dipp, djxploit, durianSausage, dy, erictee, fatherOfBlocks, gogo, gzeon, hyh, ignacio, kyteg, ladboy233, medikko, mics, minhquanym, oyc_109, pfapostol, rbserver, reassor, ret2basic, robee, sach1r0, simon135, sryysryy, tabish, yac, yash90, zzzitron
49.1892 USDC - $49.19
Some tokens don't correctly implement the EIP20 standard and their approve function returns void instead of a success boolean. Calling these functions with the correct EIP20 function signatures will always revert. Tokens that don't correctly implement the latest EIP20 spec, like USDT, will be unusable in the mentioned contracts as they revert the transaction because of the missing return value. We recommend using OpenZeppelin’s SafeERC20 versions with the safeApprove function that handle the return value check as well as non-standard-compliant tokens. The list of occurrences in format (solidity file, line number, actual line)
FraxlendPairCore.sol, 1102, _assetContract.approve(_swapperAddress, _borrowAmount);
FraxlendPairCore.sol, 1183, _collateralContract.approve(_swapperAddress, _collateralToSwap);
The attacker can push unlimitedly to an array, that some function loop over this array. If increasing the array size enough, calling the function that does a loop over the array will always revert since there is a gas limit. This is an High Risk issue since those arrays are publicly allows to push items into them.
FraxlendPairDeployer.sol (L123): Unbounded loop on the array deployedPairsArray that can be publicly pushed by ['deploy', 'deployCustom']
To improve algorithm precision instead using division in comparison use multiplication in the following scenario:
Instead a < b / c use a * c < b.
In all of the big and trusted contracts this rule is maintained.
VaultAccount.sol, 42, if (roundUp && (amount * total.shares) / total.amount < shares) { VaultAccount.sol, 25, if (roundUp && (shares * total.amount) / total.shares < amount) {
Some fee parameters of functions are not checked for invalid values. Validate the parameters:
FraxlendPairDeployer.deployCustom (_liquidationFee) FraxlendPairDeployer._deployFirst (_liquidationFee) FraxlendPair.constructor (_liquidationFee) FraxlendPair.changeFee (_newFee) FraxlendPairDeployer._logDeploy (_liquidationFee) FraxlendPairCore.constructor (_liquidationFee)
You use safeApprove of openZeppelin although it's deprecated. (see https://github.com/OpenZeppelin/openzeppelin-contracts/blob/566a774222707e424896c0c390a84dc3c13bdcb2/contracts/token/ERC20/utils/SafeERC20.sol#L38) You should change it to increase/decrease Allowance as OpenZeppilin says.
Deprecated safeApprove in FraxlendPairCore.sol line 1102: _assetContract.approve(_swapperAddress, _borrowAmount); Deprecated safeApprove in FraxlendPairCore.sol line 1183: _collateralContract.approve(_swapperAddress, _collateralToSwap);
external / public functions parameters should be validated to make sure the address is not 0. Otherwise if not given the right input it can mistakenly lead to loss of user funds.
FraxlendPairCore.sol.repayAsset _borrower FraxlendPairCore.sol.repayAssetWithCollateral _swapperAddress FraxlendPair.sol.withdrawFees _recipient FraxlendPairCore.sol.mint _receiver FraxlendPairCore.sol.redeem _receiver
owner param should be validated to make sure the owner address is not address(0). Otherwise if not given the right input all only owner accessible functions will be unaccessible.
FraxlendPairCore.sol.withdraw _owner FraxlendPairCore.sol.redeem _owner
Users can mistakenly think that the return value is the named return, but it is actually the actualreturn statement that comes after. To know that the user needs to read the code and is confusing. Furthermore, removing either the actual return or the named return will save gas.
FraxlendPairCore.sol, _updateExchangeRate FraxlendPairDeployer.sol, _deployFirst FraxlendPairCore.sol, _addInterest VariableInterestRate.sol, getConstants FraxlendPairCore.sol, addInterest LinearInterestRate.sol, getConstants
The following contracts have a function that allows them an admin to change it to a different address. If the admin accidentally uses an invalid address for which they do not have the private key, then the system gets locked. It is important to have two steps admin change where the first is announcing a pending new admin and the new address should then claim its ownership. A similar issue was reported in a previous contest and was assigned a severity of medium: code-423n4/2021-06-realitycards-findings#105
IFraxlendWhitelist.sol IFraxlendPair.sol
The following functions are missing reentrancy modifier although some other pulbic/external functions does use reentrancy modifer. Even though I did not find a way to exploit it, it seems like those functions should have the nonReentrant modifier as the other functions have it as well..
FraxlendPairCore.sol, initialize is missing a reentrancy modifier
Those are functions and parameters pairs that the function doesn't use the parameter. In case those functions are external/public this is even worst since the user is required to put value that never used and can misslead him and waste its time.
VariableInterestRate.sol: function requireValidInitData parameter _initData isn't used. (requireValidInitData is external) VariableInterestRate.sol: function getNewRate parameter _initData isn't used. (getNewRate is external)
Transferring tokens to the zero address is usually prohibited to accidentally avoid "burning" tokens by sending them to an unrecoverable zero address.
https://github.com/code-423n4/2022-08-frax/tree/main/src/contracts/FraxlendPairCore.sol#L574 https://github.com/code-423n4/2022-08-frax/tree/main/src/contracts/FraxlendPairCore.sol#L728 https://github.com/code-423n4/2022-08-frax/tree/main/src/contracts/FraxlendPairCore.sol#L819 https://github.com/code-423n4/2022-08-frax/tree/main/src/contracts/FraxlendPairCore.sol#L777 https://github.com/code-423n4/2022-08-frax/tree/main/src/contracts/FraxlendPairCore.sol#L651
The following functions are missing commenting as describe below:
FraxlendPairDeployer.sol, _deployFirst (private), parameters _saltSeed, _immutables, _penaltyRate not commented FraxlendPairDeployer.sol, deployCustom (external), parameter _penaltyRate not commented VariableInterestRate.sol, requireValidInitData (external), parameter _initData not commented
To give more trust to users: functions that set key/critical variables should be put behind a timelock.
https://github.com/code-423n4/2022-08-frax/tree/main/src/contracts/FraxlendPair.sol#L204 https://github.com/code-423n4/2022-08-frax/tree/main/src/contracts/FraxlendPairDeployer.sol#L170 https://github.com/code-423n4/2022-08-frax/tree/main/src/contracts/FraxlendWhitelist.sol#L65 https://github.com/code-423n4/2022-08-frax/tree/main/src/contracts/FraxlendPair.sol#L288
🌟 Selected for report: IllIllI
Also found by: 0x1f8b, 0xA5DF, 0xDjango, 0xNazgul, 0xSmartContract, 0xackermann, 0xbepresent, 0xc0ffEE, 0xkatana, 2997ms, Amithuddar, Aymen0909, Bnke0x0, Chinmay, Chom, CodingNameKiki, Deivitto, Diraco, Dravee, ElKu, EthLedger, Fitraldys, Funen, IgnacioB, JC, Junnon, Lambda, LeoS, Metatron, MiloTruck, Noah3o6, NoamYakov, PaludoX0, Randyyy, ReyAdmirado, Rohan16, Rolezn, Ruhum, SaharAP, Sm4rty, SooYa, TomJ, Tomio, Waze, Yiko, _Adam, __141345__, a12jmx, ajtra, ak1, asutorufos, ballx, brgltd, c3phas, cRat1st0s, carlitox477, chrisdior4, d3e4, delfin454000, dharma09, djxploit, durianSausage, erictee, fatherOfBlocks, find_a_bug, flyx, francoHacker, gerdusx, gogo, gzeon, hakerbaya, ignacio, jag, kyteg, ladboy233, ltyu, m_Rassska, medikko, mics, mrpathfindr, newfork01, nxrblsrpr, oyc_109, pfapostol, rbserver, reassor, ret2basic, robee, sach1r0, saian, simon135, sryysryy, zeesaw
21.2057 USDC - $21.21
Boolean variables can be checked within conditionals directly without the use of equality operators to true/false.
FraxlendPair.sol, 290: // Do not set when _approval == false and _lender == msg.sender FraxlendPairCore.sol, 310: if (_borrowerAmount == 0) return true; FraxlendPair.sol, 309: // Do not set when _approval == false and _borrower == msg.sender FraxlendPairCore.sol, 312: if (_collateralAmount == 0) return false; FraxlendPairCore.sol, 308: if (maxLTV == 0) return true;
Unused state variables are gas consuming at deployment (since they are located in storage) and are a bad code practice. Removing those variables will decrease deployment gas cost and improve code quality. This is a full list of all the unused storage variables we found in your code base.
FraxlendPairConstants.sol, UTIL_PREC FraxlendPairConstants.sol, FRAXSWAP_ROUTER_ADDRESS FraxlendPairConstants.sol, DEFAULT_INT FraxlendPairConstants.sol, LTV_PRECISION FraxlendPairConstants.sol, LIQ_PRECISION FraxlendPairConstants.sol, MAX_PROTOCOL_FEE FraxlendPairConstants.sol, DEFAULT_PROTOCOL_FEE FraxlendPairConstants.sol, EXCHANGE_PRECISION FraxlendPairConstants.sol, FEE_PRECISION
Caching the array length is more gas efficient. This is because access to a local variable in solidity is more efficient than query storage / calldata / memory. We recommend to change from:
for (uint256 i=0; i<array.length; i++) { ... }
to:
uint len = array.length for (uint256 i=0; i<len; i++) { ... }
FraxlendWhitelist.sol, _addresses, 51 FraxlendPairCore.sol, _approvedLenders, 270 FraxlendWhitelist.sol, _addresses, 81 FraxlendWhitelist.sol, _addresses, 66 FraxlendPairCore.sol, _approvedBorrowers, 265 FraxlendPair.sol, _borrowers, 308 FraxlendPair.sol, _lenders, 289
Prefix increments are cheaper than postfix increments.
Further more, using unchecked {++x} is even more gas efficient, and the gas saving accumulates every iteration and can make a real change
There is no risk of overflow caused by increamenting the iteration index in for loops (the ++i
in for (uint256 i = 0; i < numIterations; ++i)
).
But increments perform overflow checks that are not necessary in this case.
These functions use not using prefix increments (++x
) or not using the unchecked keyword:
change to prefix increment and unchecked: FraxlendPair.sol, i, 289 just change to unchecked: FraxlendPairCore.sol, i, 265 change to prefix increment and unchecked: FraxlendPair.sol, i, 308 change to prefix increment and unchecked: FraxlendWhitelist.sol, i, 51 change to prefix increment and unchecked: FraxlendWhitelist.sol, i, 81 just change to unchecked: FraxlendPairCore.sol, i, 270 change to prefix increment and unchecked: FraxlendWhitelist.sol, i, 66
In for loops you initialize the index to start from 0, but it already initialized to 0 in default and this assignment cost gas. It is more clear and gas efficient to declare without assigning 0 and will have the same meaning:
FraxlendPairCore.sol, 265 FraxlendPair.sol, 308 FraxlendWhitelist.sol, 51 FraxlendWhitelist.sol, 81 FraxlendPairDeployer.sol, 402 FraxlendPair.sol, 289 FraxlendPairCore.sol, 270 FraxlendWhitelist.sol, 66
Reading a storage variable is gas costly (SLOAD). In cases of multiple read of a storage variable in the same scope, caching the first read (i.e saving as a local variable) can save gas and decrease the overall gas uses. The following is a list of functions and the storage variables that you read twice:
VariableInterestRate.sol: INT_HALF_LIFE is read twice in getNewRate
Unnecessary default assignments, you can just declare and it will save gas and have the same meaning.
LinearInterestRate.sol (L#33) : uint256 private constant MIN_INT = 0; FraxlendPairConstants.sol (L#47) : uint16 internal constant DEFAULT_PROTOCOL_FEE = 0;
You can change the order of the storage variables to decrease memory uses.
In FraxlendPairCore.sol,rearranging the storage fields can optimize to: 24 slots from: 25 slots. The new order of types (you choose the actual variables): 1. string 2. IERC20 3. IERC20 4. uint256 5. uint256 6. uint256 7. uint256 8. IRateCalculator 9. bytes 10. string 11. uint256 12. uint256 13. CurrentRateInfo 14. ExchangeRateInfo 15. VaultAccount 16. VaultAccount 17. uint256 18. address 19. bool 20. bool 21. address 22. address 23. address 24. address 25. address 26. address
Use bytes32 instead of string to save gas whenever possible. String is a dynamic data structure and therefore is more gas consuming then bytes32.
FraxlendPairCore.sol (L51), string public version = "1.0.0"; FraxlendPairDeployer.sol (L315), string memory _name = string( abi.encodePacked( "FraxlendV1 - ", IERC20(_collateral).safeName(), "/", IERC20(_asset).safeName(), " - ", IRateCalculator(_rateContract).name(), " - ", (deployedPairsArray.length + 1).toString() ) );
Using != 0 is slightly cheaper than > 0. (see https://github.com/code-423n4/2021-12-maple-findings/issues/75 for similar issue)
FraxlendPair.sol, 256: change 'balance > 0' to 'balance != 0' FraxlendPairCore.sol, 1094: change '_initialCollateralAmount > 0' to '_initialCollateralAmount != 0' FraxlendPairCore.sol, 754: change '_collateralAmount > 0' to '_collateralAmount != 0'
You can inline the following functions instead of writing a specific function to save gas. (see https://github.com/code-423n4/2021-11-nested-findings/issues/167 for a similar issue.)
FraxlendPairCore.sol, _totalAssetAvailable, { return _totalAsset.amount - _totalBorrow.amount; } FraxlendPairHelper.sol, _isPastMaturity, { return _maturityDate != 0 && _timestamp > _maturityDate; } FraxlendPairCore.sol, _isPastMaturity, { return maturityDate != 0 && block.timestamp > maturityDate; }
The following functions are used exactly once. Therefore you can inline them and save gas and improve code clearness.
FraxlendPairHelper.sol, _isPastMaturity SafeERC20.sol, safeTransferFrom SafeERC20.sol, safeTransfer
We recommend not to cache msg.sender since calling it is 2 gas while reading a variable is more.
https://github.com/code-423n4/2022-08-frax/tree/main/src/contracts/FraxlendPairCore.sol#L168