Skip to content
Business logic Audit finding Intermediate

Protocol doesn’t handle fee on transfer tokens

The protocol's sendToCosmos function credits the full amount sent by the user, even if the token charges a transfer fee, so the contract's balance increases by less than the credited amount. An attacker could exploit this to drain other users' funds by repeatedly depositing and withdrawing with a fee-on-transfer token.

wuwe1 Follow

Originally disclosed on Code4rena

Submitted by wuwe1, also found by cccz, defsec, dipp, Dravee, GermanKuber, GimelSec, jah, reassor, and WatchPug

Gravity.sol#L600

Since the _tokenContract can be any token, it is possible that loans will be created with tokens that support fee on transfer. If a fee on transfer asset token is chosen, other user’s funds might be drained.

Proof of Concept

  1. Assume transfer fee to be 5% and Gravity.sol has 200 token.
  2. Alice sendToCosmos 100 token. Now, Gravity.sol has 295 token.
  3. Alice calls the send-to-eth method to withdraw 100 token.
  4. Gravity.sol ends up having 195 token.

Recommended Mitigation Steps

Change to

	function sendToCosmos(
		address _tokenContract,
		bytes32 _destination,
		uint256 _amount
	) public nonReentrant  {
                uint256 oldBalance = IERC20(_tokenContract).balanceOf(address(this));
		IERC20(_tokenContract).safeTransferFrom(msg.sender, address(this), _amount);
                uint256 receivedAmout = IERC20(_tokenContract).balanceOf(address(this)) - oldBalance;
		state_lastEventNonce = state_lastEventNonce.add(1);
		emit SendToCosmosEvent(
			_tokenContract,
			msg.sender,
			_destination,
			receivedAmout,
			state_lastEventNonce
		);
	}

mlukanova (Cudos) acknowledged and commented:

Token transfers are restricted to the Cudos token which doesn’t support fee on transfer. Will be fixed with issue #58.


The original report as published on Code4rena, by wuwe1. 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 credits the requested transfer amount rather than measuring the actual balance increase, so fee-on-transfer tokens cause an accounting mismatch.

Impact

Other users' funds could be drained if a fee-on-transfer token is used, though the project says transfers are restricted to the Cudos token which does not charge such a fee.

What the attacker needed

A fee-on-transfer token is accepted; attacker can deposit and withdraw

The fix

The contract should measure the actual balance increase after the transfer and credit that amount instead of the requested amount; the project says it will be fixed.

How it was found

  1. Assume a 5% transfer fee and the contract holds 200 tokens.
  2. Alice sends 100 tokens to the contract; the contract now holds 295 tokens.
  3. Alice calls the send-to-eth method to withdraw 100 tokens.
  4. The contract ends up holding 195 tokens.

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.

Some tokens take a small fee when they are transferred, so the receiver gets less than the sender sent. This protocol assumed the full amount always arrives and recorded that full amount as the user's deposit. That mistake lets someone deposit a fee-charging token, get credited more than actually arrived, and then withdraw the difference from other people's money.

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 wuwe1

View all

3 more disclosed reports, best first.

More from Cudos contest

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.