Skip to content
Business logic Audit finding Intermediate

Wrong shareChange() function ( vToken.sol )

The shareChange() function in vToken.sol calculates new shares incorrectly when the total supply equals the old shares, resulting in zero new shares. An attacker could potentially exploit this to receive fewer vTokens than deserved, causing a loss of funds.

TrungOre Follow

Originally disclosed on Code4rena

Submitted by TrungOre

vToken.sol#L160

Users can get the wrong amount of vToken => Make users lose their fund

Proof of Concept

Base on the code in function shareChange() in vToken.sol Assume that if oldShare = totalSupply > 0,

  • newShares

= (_amountInAsset * (_totalSupply - oldShares)) / (_assetBalance - availableAssets); = (_amountInAsset * (_totalSupply - _totalSupply)) / (_assetBalance - availableAssets); = 0

It make no sense, because if amountInAsset >> availableAssets, newShares should be bigger than oldShares, but in this case newShares = 0 < oldShares

Recommended Mitigation Steps

Modify the line from if (_totalSupply > 0) to if (_totalSupply - oldShares > 0).

olivermehr (Phuture Finance) disputed

jn-lp (Phuture Finance) commented:

Such a case is considered impossible due to the fact that it can only work with a 0xdead address.

moose-code (judge) commented:

Agree it’s not an issue as on initialization tokens are sent to the burn address making this unlikely.

However the orderer role could possibly burn the tokens held by the burn address causing this issue to happen.

JasoonS (judge) commented:

Agree with mitigation step:

Modify the line from if (*totalSupply > 0) to if (*totalSupply - oldShares > 0)

If it were impossible for tokens to be burned from the 0xdead address then this wouldn’t be a concern.

So although extremely unlikely, this is valid.


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

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 5 min read.

Context

Phuture is a protocol whose contracts include vToken.sol, a token contract that mints vTokens in exchange for an asset. The amount of vTokens a user receives is computed by a function called shareChange(). The report focuses on the branch of that function that decides how many new shares to issue when the total supply is non-zero.

The researcher's claim is that under a particular state of the contract, shareChange() returns the wrong number of shares, so a user receives the wrong amount of vTokens and, in the researcher's words, loses their funds.

Setup

The report does not describe any accounts, roles, tools or versions used to reproduce the issue. It is a code review finding against the linked source file, with the reasoning carried out by hand on the formula in shareChange(). The relevant code is at vToken.sol#L160 in the repository referenced by the report.

Two roles appear in the discussion: the orderer role, which the judge says could burn tokens held by the burn address, and the burn address itself, 0xdead, to which tokens are sent on initialisation.

Finding it

The researcher looked at shareChange() in vToken.sol and worked through the formula for newShares under the assumption that oldShare = totalSupply > 0. Substituting oldShares = _totalSupply into the expression gives:

  • newShares

= (_amountInAsset * (_totalSupply - oldShares)) / (_assetBalance - availableAssets); = (_amountInAsset * (_totalSupply - _totalSupply)) / (_assetBalance - availableAssets); = 0

The researcher argues this makes no sense: if amountInAsset >> availableAssets, then newShares should be bigger than oldShares, but in this case newShares = 0 < oldShares. In other words, a user depositing a large amount of the asset relative to the available assets would be credited with zero new shares instead of a larger number than the existing supply.

Exploitation

The report does not give a step-by-step exploitation procedure, requests or transactions. What it does give is the state that triggers the wrong result and the reasoning behind it.

Step 1: reach the state where oldShare equals totalSupply

The faulty branch is taken when oldShare = totalSupply > 0. In that state the subtraction _totalSupply - oldShares inside the numerator is zero, so the whole numerator is zero regardless of _amountInAsset.

Step 2: deposit an amount large relative to the available assets

With amountInAsset >> availableAssets, the expected result is newShares > oldShares. Instead the formula yields newShares = 0, which is less than oldShares. The user is credited with no new shares for their deposit.

Step 3: the user loses funds

Because the user receives the wrong amount of vTokens, the researcher's stated consequence is that users lose their funds.

Root cause

The guard on line 160 is if (_totalSupply > 0). That condition only checks that the total supply is non-zero; it does not check that there is anything left to mint against, that is, that _totalSupply - oldShares is greater than zero. When oldShares equals _totalSupply, the numerator collapses to zero and the function returns zero new shares.

The recommended change is to test the difference rather than the raw total supply:

Modify the line from if (_totalSupply > 0) to if (_totalSupply - oldShares > 0).

The Phuture team disputed the finding. jn-lp (Phuture Finance) commented that such a case is considered impossible because it can only work with a 0xdead address. The judge, moose-code, initially agreed that it is not an issue, since on initialisation tokens are sent to the burn address, making the state unlikely. The judge then added that the orderer role could possibly burn the tokens held by the burn address, causing the issue to happen. A second judge, JasoonS, agreed with the mitigation step and concluded that if it were impossible for tokens to be burned from the 0xdead address then this would not be a concern, so although extremely unlikely, the issue is valid.

Impact

A user who deposits into the affected state receives the wrong amount of vTokens, specifically zero new shares where a larger amount than the existing supply would be expected, and per the researcher this means the user loses their funds. The report frames the impact in terms of users generally, not a specific attacker.

The reachability of the state is the limiting factor. The team considers it impossible in normal operation because it requires the 0xdead address, and the judge notes that on initialisation tokens are sent to the burn address. The judge's caveat is that the orderer role could burn the tokens held by the burn address, which would make the state reachable. The judges describe the scenario as extremely unlikely but valid.

Fix

The report recommends changing the condition on line 160 from if (_totalSupply > 0) to if (_totalSupply - oldShares > 0). Both judges agreed with this mitigation step.

Variants to test

  • Look for guards that test a total rather than the remaining quantity, such as totalSupply > 0 where the arithmetic actually depends on totalSupply - alreadyAccounted. A positive result is a branch that can be entered with the difference equal to zero, producing a zero or otherwise degenerate result.
  • Check mint and share calculations for a numerator that can become zero through subtraction of two values that are equal in some reachable state. A positive result is a deposit that yields zero shares while the deposit amount is large relative to available assets.
  • Trace who can move or burn tokens held by a burn address, such as an orderer role. A positive result is a privileged path that can empty the burn address and make the degenerate state reachable.

Hunting notes

This kind of bug hides in arithmetic guards in token and vault contracts, particularly where a condition checks totalSupply > 0 but the formula subtracts an already-accounted amount from the total supply. During review, the signal is a subtraction inside a numerator or denominator whose operands can become equal.

The report shows the value of writing out the formula with the triggering state substituted in, as the researcher did with oldShares = _totalSupply, because that makes the zero result obvious. It also shows that a finding can survive a dispute when the judges can identify a privileged path, here the orderer role burning tokens from 0xdead, that makes the supposedly impossible state reachable. The finding survived the dispute because the judges identified a privileged path, the orderer role burning tokens from 0xdead, that makes the state reachable; they called it extremely unlikely but valid.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 10 min read.

What happened, in plain words

Phuture is a set of programs that run on a blockchain (a shared, public ledger that many computers keep copies of). One of those programs is a token contract called vToken.sol. A token contract is a piece of code that keeps track of who owns how many units of something, much like a spreadsheet with one row per owner. In Phuture, users hand over an asset (some other token they own) and, in exchange, the contract gives them vTokens. The number of vTokens they get back is worked out by a small function called shareChange().

The researcher, TrungOre, read that function and found a situation where the arithmetic gives the wrong answer: a user who deposits a large amount of the asset is credited with zero new vTokens instead of a number larger than the existing supply. In the researcher's words, this makes users lose their funds.

The Phuture team disagreed, saying the situation cannot happen in practice. Two judges looked at it. One first agreed it was not an issue, then noted that a privileged role could create the situation. The other judge agreed with the suggested fix and concluded that, although extremely unlikely, the issue is valid.

Background ideas you need first

Before the details make sense, here are a few ideas from zero.

  • Blockchain: a public record of transactions that many computers keep in sync. Programs called smart contracts live on it and run automatically.
  • Smart contract: code stored on the blockchain. Once deployed, anyone can call its functions, and it executes exactly as written. There is no human in the middle to correct a mistake.
  • Token: a smart contract that tracks balances, like a digital voucher. Holding a token means the contract's records say you do.
  • Minting: creating new tokens out of nothing and assigning them to someone. The opposite, destroying tokens, is usually called burning.
  • Total supply: the number of tokens that currently exist according to the contract.
  • Share: a unit of ownership in a pool. When you deposit into a pool, you receive shares representing your slice of it. More shares means a bigger claim.
  • Function: a named block of code inside a contract that can be called to do something, for example to calculate a number or to transfer tokens.
  • Variable: a named storage slot holding a value, such as a number.
  • Role: a permission label in a contract. An address holding a role is allowed to call certain functions that ordinary users cannot.
  • Burn address: a blockchain address that nobody controls the private key for, so tokens sent there are effectively gone forever. The report names it as 0xdead.
  • >> (much greater than): shorthand used in the report to mean "is far larger than".
  • Code review finding: a problem spotted by reading code, rather than by attacking a live system.

A useful everyday comparison: think of a savings account that issues you a receipt showing your share of the pot. If you deposit a large sum but the clerk's formula hands you a receipt for zero shares, your money went in but your claim on the pot did not grow. That is the shape of the bug here.

Where the code lives

The report points at one line in the source file:

vToken.sol#L160

The link goes to a specific commit of the repository, so the line number refers to that exact snapshot of the code. The report also links the file on the main branch:

vToken.sol

Setup

The report does not describe any accounts, roles, tools or versions used to reproduce the issue. It is a code review finding against the linked source file, with the reasoning carried out by hand on the formula in shareChange(). No test environment, no scripts and no transactions are given.

Two roles appear later in the discussion: the orderer role, which a judge says could burn tokens held by the burn address, and the burn address itself, 0xdead, to which tokens are sent on initialisation (the one-time setup when the contract starts).

Finding it

The researcher looked at shareChange() in vToken.sol and worked through the formula for newShares under the assumption that oldShare = totalSupply > 0. In plain terms: suppose the pool already has some tokens in circulation, and suppose the value the code calls oldShares is equal to the total supply.

Substituting oldShares = _totalSupply into the expression gives:

Text
- `newShares`

= `(_amountInAsset * (_totalSupply - oldShares)) / (_assetBalance - availableAssets);`
 = `(_amountInAsset * (_totalSupply - _totalSupply)) / (_assetBalance - availableAssets);`
 = `0`

Line by line, in plain words:

  1. newShares is the number of new shares the function is about to hand out.
  2. The formula multiplies the amount the user is depositing (_amountInAsset) by the difference between the total supply and oldShares.
  3. Because oldShares equals _totalSupply, that difference is zero.
  4. Anything multiplied by zero is zero, so the top of the fraction (the numerator) is zero.
  5. Zero divided by anything non-zero is zero, so newShares comes out as 0.

The researcher argues this makes no sense: if amountInAsset >> availableAssets (the deposit is far larger than the assets currently available), then newShares should be bigger than oldShares, but in this case newShares = 0 < oldShares. In other words, a user depositing a large amount of the asset relative to the available assets would be credited with zero new shares instead of a larger number than the existing supply.

Exploitation

The report does not give a step-by-step exploitation procedure, requests or transactions. What it does give is the state that triggers the wrong result and the reasoning behind it.

Step 1: reach the state where oldShare equals totalSupply

The faulty branch is taken when oldShare = totalSupply > 0. In that state the subtraction _totalSupply - oldShares inside the numerator is zero, so the whole numerator is zero regardless of _amountInAsset. The deposit amount no longer matters: the result is zero either way.

Step 2: deposit an amount large relative to the available assets

With amountInAsset >> availableAssets, the expected result is newShares > oldShares. Instead the formula yields newShares = 0, which is less than oldShares. The user is credited with no new shares for their deposit.

Step 3: the user loses funds

Because the user receives the wrong amount of vTokens, the researcher's stated consequence is that users lose their funds. The asset went in; the shares that should represent the claim on the pool did not come out.

Root cause

The guard on line 160 is if (_totalSupply > 0). A guard is simply a condition that decides whether a block of code runs. This one only checks that the total supply is non-zero; it does not check that there is anything left to mint against, that is, that _totalSupply - oldShares is greater than zero. When oldShares equals _totalSupply, the numerator collapses to zero and the function returns zero new shares.

The recommended change is to test the difference rather than the raw total supply:

Modify the line from if (_totalSupply > 0) to if (_totalSupply - oldShares > 0).

In plain words: instead of asking "are there any tokens at all?", ask "is there a positive amount left after subtracting what has already been accounted for?". If the answer is no, the code should not take the branch that divides using that difference.

What the team and judges said

The Phuture team disputed the finding. jn-lp (Phuture Finance) commented that such a case is considered impossible because it can only work with a 0xdead address.

The judge, moose-code, initially agreed that it is not an issue, since on initialisation tokens are sent to the burn address, making the state unlikely. The judge then added that the orderer role could possibly burn the tokens held by the burn address, causing the issue to happen.

A second judge, JasoonS, agreed with the mitigation step and concluded that if it were impossible for tokens to be burned from the 0xdead address then this would not be a concern, so although extremely unlikely, the issue is valid.

Impact

A user who deposits into the affected state receives the wrong amount of vTokens, specifically zero new shares where a larger amount than the existing supply would be expected, and per the researcher this means the user loses their funds. The report frames the impact in terms of users generally, not a specific attacker.

The reachability of the state is the limiting factor. The team considers it impossible in normal operation because it requires the 0xdead address, and the judge notes that on initialisation tokens are sent to the burn address. The judge's caveat is that the orderer role could burn the tokens held by the burn address, which would make the state reachable. The judges describe the scenario as extremely unlikely but valid.

Fix

The report recommends changing the condition on line 160 from if (_totalSupply > 0) to if (_totalSupply - oldShares > 0). Both judges agreed with this mitigation step.

Variants to test

Here are three directions for looking for similar bugs.

  1. Look for guards that test a total rather than the remaining quantity, such as totalSupply > 0 where the arithmetic actually depends on totalSupply - alreadyAccounted. A positive result is a branch that can be entered with the difference equal to zero, producing a zero or otherwise degenerate result.
  2. Check mint and share calculations for a numerator that can become zero through subtraction of two values that are equal in some reachable state. A positive result is a deposit that yields zero shares while the deposit amount is large relative to available assets.
  3. Trace who can move or burn tokens held by a burn address, such as an orderer role. A positive result is a privileged path that can empty the burn address and make the degenerate state reachable.

Hunting notes

This kind of bug hides in arithmetic guards in token and vault contracts, particularly where a condition checks totalSupply > 0 but the formula subtracts an already-accounted amount from the total supply. During review, the signal is a subtraction inside a numerator or denominator whose operands can become equal.

The report shows the value of writing out the formula with the triggering state substituted in, as the researcher did with oldShares = _totalSupply, because that makes the zero result obvious. It also shows that a finding can survive a dispute when the judges can identify a privileged path, here the orderer role burning tokens from 0xdead, that makes the supposedly impossible state reachable. The finding survived the dispute because the judges identified a privileged path, the orderer role burning tokens from 0xdead, that makes the state reachable; they called it extremely unlikely but valid.

Glossary

  • Blockchain: a public, shared record of transactions kept in sync by many computers.
  • Smart contract: code stored on a blockchain that runs automatically when called.
  • Token: a smart contract that tracks balances of a digital unit.
  • vToken: the token issued by Phuture's vToken.sol contract in exchange for a deposited asset.
  • Minting: creating new tokens and assigning them to an address.
  • Burning: destroying tokens so they no longer exist.
  • Share: a unit of ownership in a pool, representing a slice of the deposited assets.
  • Function: a named block of code in a contract that can be called to perform an action or calculation.
  • Variable: a named storage slot holding a value.
  • Guard: a condition that decides whether a block of code runs.
  • Branch: one path through an if statement, taken when its condition is true.
  • Numerator: the top part of a fraction, the number being divided.
  • Denominator: the bottom part of a fraction, the number doing the dividing.
  • Role: a permission label in a contract that allows certain addresses to call restricted functions.
  • orderer: the role named in the report that a judge says could burn tokens held by the burn address.
  • Burn address: an address nobody controls, so tokens sent there are effectively gone; the report names 0xdead.
  • Initialisation: the one-time setup that runs when a contract starts.
  • >>: shorthand in the report meaning "is far larger than".
  • Code review finding: a problem found by reading code rather than by attacking a live system.
  • Mitigation: a change that removes or reduces the problem.
  • Severity: a rating of how serious a finding is.
  • Reachability: whether a state or code path can actually be arrived at in practice.
  • Degenerate result: an output that is technically valid arithmetic but meaningless, such as zero shares for a real deposit.

More by TrungOre

View all

7 more disclosed reports, best first.

More from Phuture

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.