Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: CEI to fix reentrancy risk with reentrant tokens (eg ERC777) #704

Merged
merged 4 commits into from
Nov 2, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
22 changes: 15 additions & 7 deletions contracts/protocol/libraries/logic/SupplyLogic.sol
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,22 @@ library SupplyLogic {

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

bool isCollateral;
miguelmtzinf marked this conversation as resolved.
Show resolved Hide resolved

if ((isCollateral = userConfig.isUsingAsCollateral(reserve.id))) {
if (amountToWithdraw == userBalance) {
userConfig.setUsingAsCollateral(reserve.id, false);
emit ReserveUsedAsCollateralDisabled(params.asset, msg.sender);
miguelmtzinf marked this conversation as resolved.
Show resolved Hide resolved
}
}
IAToken(reserveCache.aTokenAddress).burn(
msg.sender,
params.to,
amountToWithdraw,
reserveCache.nextLiquidityIndex
);

if (userConfig.isUsingAsCollateral(reserve.id)) {
if (isCollateral) {
if (userConfig.isBorrowingAny()) {
ValidationLogic.validateHFAndLtv(
miguelmtzinf marked this conversation as resolved.
Show resolved Hide resolved
reservesData,
Expand All @@ -149,11 +157,6 @@ library SupplyLogic {
params.userEModeCategory
);
}

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

emit Withdraw(params.asset, msg.sender, params.to, amountToWithdraw);
Expand Down Expand Up @@ -264,7 +267,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