Skip to content
Business logic Audit finding Advanced

Transferout exclusive feeOnTransfer tokens will run out of well

The Well contract does not account for fee-on-transfer tokens that deduct fees from the sender, so each transfer out reduces the Well's actual token balance below its stored reserves. An attacker could repeatedly swap and skim to drain the Well's funds.

kutugu Follow

Originally disclosed on Code4rena

Submitted by kutugu

https://github.com/code-423n4/2023-07-basin/blob/9403cf973e95ef7219622dbbe2a08396af90b64c/src/Well.sol#L610 https://github.com/code-423n4/2023-07-basin/blob/9403cf973e95ef7219622dbbe2a08396af90b64c/src/Well.sol#L304 https://github.com/code-423n4/2023-07-basin/blob/9403cf973e95ef7219622dbbe2a08396af90b64c/src/Well.sol#L370

Impact

The well does not check the actual transferout amount and the k value, which for exclusive feeOnTransfer tokens causes the well to have a portion of the token fee cut out of air every time the token is transferred, which is not included in the transfer amount. Attackers can take advantage of this to run out of well.

Proof of Concept

The most common attack flow is through the skim function:

  1. Attacker swaps to increase the price of exclusive feeOnTransfer token
  2. Attacker transfers token to well and skim. This will cut some of the well funds
  3. Repeat the above process to reduce the number of well tokens to 1
  4. Attacker calls sync and swap to pair token

Recommended Mitigation Steps

Check the correct amount every time you transfer out.

alcueca (Judge) commented:

I think this is correct. Most fee-on-transfer tokens will take the fee on the recipient, but there isn’t a rule or even a standard that forbids the fee-on-transfer token to take the fees from the sender, or from both. My bank has all of these options for transfers that include fees.

The Well in scope is intended to support fee-on-transfer tokens, but nowhere it is specified that it is intended to support only recipient-fee-on-transfer tokens, and therefore is vulnerable. This could be a critical except that recipient-fee-on-transfer tokens are exceedingly rare at the time, and only Wells deployed for those exceedingly rare tokens would be drained.

publiuss (Basin) disputed and commented:

Skim will only transfer tokens out of the Well if the balances of tokens in the well are greater than the reserves of the Well. This only happens if someone sends tokens to the Well without calling sync or shift.

There is no way to abuse this mechanism as the Well’s token balances can never drop below reserve balances as a result of skim.

alcueca (Judge) commented:

@publiuss, I can reduce the severity to QA on account of the low quality and invalid Proof of Concept, but I think that the attack vector is valid.

Correct me if I’m wrong, but swapFromFeeOnTransfer only considers fees in the incoming token. For the outgoing token it assumes that fees will be deducted from the amount received, and calls _swapFrom as for non-fee tokens.

Normally I wouldn’t worry too much about fee-on-transfer tokens, but you explicitly intend to support them, and there is no standard defining how fees should be collected, so they could be deducted from the sender’s balance after each successful transfer.

During a swapFromFeeOnTransfer, inside the _swapFrom call, we will calculate what the reserves should be, and from there the amountOut. Then the Well transfers out the amountOut and sets the reserves to the calculated amounts.

Only that if a fee is collected from the sender after the transfer, the stored reserves will be higher than the actual well balances, and this discrepancy will grow with each swap.

Would you please check again if my reasoning is right?

I don’t know if I would fix this in the code, I would most likely just state in the docs that only fee-on-transfer tokens where the fee is collected from the receiver are supported, and those where the fee is collected from the sender are not, but that is up to you.

publiuss (Basin) commented:

Hello @alcueca, your reasoning is correct and I agree with your thoughts in regards to the solution.


The original report as published on Code4rena, by kutugu. 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

Likely the contract calculates reserves and output amounts without verifying the actual token balance after a transfer, so sender-side fees cause the stored reserves to exceed the real balance.

Impact

The Well could be drained of its tokens, but only for the exceedingly rare fee-on-transfer tokens that take the fee from the sender; the judge noted the proof of concept was invalid and reduced severity to QA.

What the attacker needed

Well supports a sender-fee-on-transfer token; Attacker has some of that token

The fix

The contract should check the actual amount received or sent on every transfer, or the documentation should state that only recipient-fee-on-transfer tokens are supported.

How it was found

  1. Attacker swaps to increase the price of the exclusive fee-on-transfer token.
  2. Attacker transfers the token to the Well and calls skim, which cuts some of the Well's funds.
  3. Repeat the above process to reduce the Well's token count to 1.
  4. Attacker calls sync and swap to the pair token.

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 fee-on-transfer token takes a small cut every time it is moved. The Well contract assumed the cut always comes from the receiver, but some tokens take it from the sender instead. Because the contract did not check the real balance after sending tokens out, its records slowly became wrong and an attacker could exploit that to empty it.

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.

More by kutugu

View all

8 more disclosed reports, best first.

More from Basin Invitational

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.