Skip to content

Commit

Permalink
Fix: CEI to fix reentrancy risk with reentrant tokens (eg ERC777) (#704)
Browse files Browse the repository at this point in the history
* fix: liquidationCall reentrancy

* fix: reentrancy on withdraw

* fix: Join conditionals in withdraw function

Co-authored-by: miguelmtzinf <[email protected]>
  • Loading branch information
The-3D and miguelmtzinf authored Nov 2, 2022
1 parent f94aac9 commit 7fbdc6e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 28 deletions.
16 changes: 8 additions & 8 deletions contracts/protocol/libraries/logic/LiquidationLogic.sol
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,13 @@ library LiquidationLogic {
userConfig.setBorrowing(debtReserve.id, false);
}

// If the collateral being liquidated is equal to the user balance,
// we set the currency as not being used as collateral anymore
if (vars.actualCollateralToLiquidate == vars.userCollateralBalance) {
userConfig.setUsingAsCollateral(collateralReserve.id, false);
emit ReserveUsedAsCollateralDisabled(params.collateralAsset, params.user);
}

_burnDebtTokens(params, vars);

debtReserve.updateInterestRates(
Expand Down Expand Up @@ -197,14 +204,7 @@ library LiquidationLogic {
vars.liquidationProtocolFeeAmount
);
}

// If the collateral being liquidated is equal to the user balance,
// we set the currency as not being used as collateral anymore
if (vars.actualCollateralToLiquidate == vars.userCollateralBalance) {
userConfig.setUsingAsCollateral(collateralReserve.id, false);
emit ReserveUsedAsCollateralDisabled(params.collateralAsset, params.user);
}


// Transfers the debt asset being repaid to the aToken, where the liquidity is kept
IERC20(params.debtAsset).safeTransferFrom(
msg.sender,
Expand Down
45 changes: 25 additions & 20 deletions contracts/protocol/libraries/logic/SupplyLogic.sol
Original file line number Diff line number Diff line change
Expand Up @@ -128,32 +128,32 @@ library SupplyLogic {

reserve.updateInterestRates(reserveCache, params.asset, 0, amountToWithdraw);

bool isCollateral = userConfig.isUsingAsCollateral(reserve.id);

if (isCollateral && amountToWithdraw == userBalance) {
userConfig.setUsingAsCollateral(reserve.id, false);
emit ReserveUsedAsCollateralDisabled(params.asset, msg.sender);
}

IAToken(reserveCache.aTokenAddress).burn(
msg.sender,
params.to,
amountToWithdraw,
reserveCache.nextLiquidityIndex
);

if (userConfig.isUsingAsCollateral(reserve.id)) {
if (userConfig.isBorrowingAny()) {
ValidationLogic.validateHFAndLtv(
reservesData,
reservesList,
eModeCategories,
userConfig,
params.asset,
msg.sender,
params.reservesCount,
params.oracle,
params.userEModeCategory
);
}

if (amountToWithdraw == userBalance) {
userConfig.setUsingAsCollateral(reserve.id, false);
emit ReserveUsedAsCollateralDisabled(params.asset, msg.sender);
}
if (isCollateral && userConfig.isBorrowingAny()) {
ValidationLogic.validateHFAndLtv(
reservesData,
reservesList,
eModeCategories,
userConfig,
params.asset,
msg.sender,
params.reservesCount,
params.oracle,
params.userEModeCategory
);
}

emit Withdraw(params.asset, msg.sender, params.to, amountToWithdraw);
Expand Down Expand Up @@ -264,7 +264,12 @@ library SupplyLogic {

if (useAsCollateral) {
require(
ValidationLogic.validateUseAsCollateral(reservesData, reservesList, userConfig, reserveCache.reserveConfiguration),
ValidationLogic.validateUseAsCollateral(
reservesData,
reservesList,
userConfig,
reserveCache.reserveConfiguration
),
Errors.USER_IN_ISOLATION_MODE
);

Expand Down

0 comments on commit 7fbdc6e

Please sign in to comment.