Skip to content
Business logic Write-up Advanced

Yield Protocol

A logic error in the strategy contract's burn function allowed an attacker to inflate the pool token balance by directly transferring pool tokens to the contract, then burning strategy shares to receive more LP tokens than deserved. This could drain base tokens from the pool, risking approximately $950k.

medium.com

Published on medium.com

Cybersecurity

Immunefi, on medium.com

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

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

Yield Protocol is a DeFi protocol that enables fixed-rate, fixed-term loans between borrowers and lenders. It does this through fyTokens (fixed yield tokens), an ERC-20 token that can be exchanged one-to-one for an underlying asset once a predetermined maturity date is reached.

The vulnerability described here sits in the protocol's strategy contract, which lets liquidity providers deposit combined liquidity into a YieldSpace Pool. By depositing funds into the strategy contract, a liquidity provider mints strategy tokens. The number of strategy tokens received is proportional to the deposit amount, and the holder can later burn them to redeem the LP tokens plus any gain in fees or interest.

The report was published by Immunefi on 25 July 2023 and covers a disclosure made to Yield Protocol on 28 April by the whitehat Paludo0x.

Setup

The proof of concept runs in Forge, the test framework from Foundry. The report gives three steps to reproduce it:

  1. Install the Foundry framework from https://github.com/foundry-rs/foundry
  2. Clone the Immunefi bugfix review repository:
Bash
git clone https://github.com/immunefi-team/bugfix-reviews-pocs.git
  1. Run the following command to run the PoC:
Bash
forge test -vvv — match-path ./test/YieldProtocol/AttackTest.t.sol

The report links a strategy contract at [redacted] on Arbiscan.

Finding it

The issue is in the burning shares functionality, burn(address to), of the strategy contract. That function burns the caller's strategy tokens and lets the user withdraw LP tokens. It works in two stages: first the contract's strategy tokens are burned, then the amount of liquidity pool tokens to be acquired is calculated from the LP tokens held by the strategy contract and transferred to the designated address.

The amount returned to the caller is computed as:

Text
poolTokensObtained = pool.balanceOf(address(this)) * burnt / totalSupply_;

The problem is the input to that calculation. pool.balanceOf(address(this)) is the strategy contract's current LP token balance, and anyone can increase that balance by transferring pool tokens to the contract directly. The attacker therefore controls a term in the payout formula. The report notes that the pool tokens used to inflate the calculation stay inside the strategy contract, so the attacker can mint share tokens afterwards and burn them to recover the pool tokens they used.

Exploitation

Step 1: Inflate the strategy contract's LP balance

The attacker transfers a specific amount of pool tokens directly to the strategy contract. This raises pool.balanceOf(address(this)) without going through the deposit path, so no strategy tokens are minted in return for it.

Step 2: Burn strategy shares to withdraw inflated LP tokens

The attacker calls burn(…). The contract burns the strategy tokens and computes the payout with the inflated balance:

Text
poolTokensObtained = pool.balanceOf(address(this)) * burnt / totalSupply_;

Because the numerator now includes the transferred tokens, the caller receives more LP tokens than their share of the strategy warrants.

Step 3: Recover the tokens used to inflate the balance

The pool tokens transferred in step 1 are still held by the strategy contract. The attacker calls mint(…) to obtain strategy share tokens, then calls burn(…) again to get back the LP tokens that were transferred. The report describes this as the method by which the attacker retrieves the tokens used to inflate the calculation.

Running the PoC

After cloning the repository and running the Forge command above, the test produces the following log output:

From the original on medium.com

The report presents this log as the output of the PoC run.

Root cause

The payout formula trusts a balance that outside parties can change. pool.balanceOf(address(this)) reflects every LP token the strategy contract holds, including tokens sent to it directly rather than deposited through the strategy. The formula reads the live balance, so a direct transfer changes the value used to price a burn.

Impact

The report states that the vulnerability could allow an attacker to drain the base tokens from a pool by manipulating the token balance of a contract. It characterises the issue as a critical logic error and estimates that the fix saved approximately $950k in funds across various pools on both the Arbitrum and Ethereum blockchains. No user funds were lost. The whitehat received a bounty of $95,000 USDC, equivalent to 10% of the funds at risk.

Fix

The project team modified the burn(…) function in the strategy contract to use poolCached_ instead of pool.balanceOf(address(this)) when determining how many LP tokens should be transferred to the caller. The new formula is:

Text
poolTokensObtained = poolCached_ * burnt / totalSupply_;

The patched formula uses poolCached_ instead of the live balance. The fix is in commit [redacted] in the yieldprotocol/strategy-v2 repository.

Variants to test

  • Share-price formulas that read live token balances. Look for any mint, burn or redeem path that multiplies by token.balanceOf(address(this)) or an equivalent live balance. A positive result is that sending tokens directly to the contract changes the amount a subsequent burn or redeem returns.
  • Donation attacks on vault-style contracts. Transfer the underlying or LP token to the contract outside the deposit function, then mint and burn in the smallest possible amounts. A positive result is that the attacker's round trip returns more than it cost.
  • Cached versus live accounting. Compare the value the contract stores at deposit time with the value it reads at withdrawal time. A positive result is a divergence between the two after a direct transfer.
  • Recovery of donated tokens. After inflating a balance, check whether a later mint and burn returns the donated tokens to the attacker. A positive result is that the inflation is free, which is the condition that turns a rounding quirk into a drain.
  • Cross-pool exposure. The report says the fix saved approximately $950k across pools on both Arbitrum and Ethereum. Check whether the same strategy code is deployed against several pools, and whether an inflation against one pool affects payouts in another.

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. 11 min read.

What happened, in plain words

Yield Protocol is a service, built on blockchains, that lets people borrow and lend money at a fixed interest rate for a fixed period. Instead of a bank setting the rate, computer programs called smart contracts hold the money and enforce the rules. A smart contract is a program that lives on a blockchain: once it is deployed, anyone can call it, and it runs exactly as written, with no human in the middle.

On 28 April, a security researcher who goes by the name Paludo0x found a flaw in one of Yield Protocol's smart contracts. A security researcher of this kind is often called a whitehat: someone who looks for weaknesses and reports them privately to the company instead of using them to steal. Paludo0x reported the flaw through Immunefi, a platform where such reports are made and bounties are paid. The flaw was a logic error, meaning the code did what it was written to do, but the arithmetic behind it could be bent by an outsider. In the worst case, an attacker could have drained the base tokens from a pool, that is, taken the main currency the pool held. The team fixed it quickly, and the report estimates that about $950k of funds across several pools on the Arbitrum and Ethereum blockchains were saved. No user funds were lost, and Paludo0x was paid a bounty of $95,000 USDC, which the report describes as 10% of the funds that were at risk. USDC is a digital currency designed to stay worth one US dollar.

From the original on medium.com

The product and the feature involved

Yield Protocol offers fixed-rate, fixed-term loans between borrowers and lenders. It does this with fyTokens, short for fixed yield tokens. An fyToken is a kind of digital token, an ERC-20 token, which is a standard format for tokens on Ethereum-style blockchains, a bit like a standard plug shape so that many devices can use the same socket. An fyToken can be exchanged one-to-one for an underlying asset once a set maturity date arrives, much like a savings certificate you can cash in on a fixed day.

The flaw sits in the protocol's strategy contract. A strategy contract is a helper contract that takes money from liquidity providers and puts it to work in a YieldSpace Pool. A liquidity provider is someone who deposits funds into a pool so that others can trade or borrow against them, and earns a share of the fees in return. A pool is simply a pot of tokens that a contract manages.

When someone deposits funds into the strategy contract, the contract gives them strategy tokens in return. The number of strategy tokens they receive is proportional to how much they deposited, the same way a shared savings club might give you one receipt per pound you put in. Later they can burn those strategy tokens, meaning destroy them, and redeem their LP tokens plus any gain in fees or interest. LP tokens, or liquidity pool tokens, are themselves receipts: they represent a share of the pool.

The flaw in the burn function

The problem is in the part of the strategy contract that burns shares, a function written as burn(address to). A function is a named block of code that someone can call, and address to is the parameter saying which account should receive the tokens. This function is responsible for burning the caller's strategy tokens and letting the user withdraw LP tokens.

It works in two stages. First, the contract's strategy tokens are burned. Then the amount of liquidity pool tokens to be handed over is calculated from the LP tokens the strategy contract holds, and transferred to the designated address.

The amount returned to the caller is calculated like this:

Text
poolTokensObtained = pool.balanceOf(address(this)) * burnt / totalSupply_;

Line by line, in plain words:

  • poolTokensObtained is the answer being worked out: how many pool tokens the caller will get back.
  • pool.balanceOf(address(this)) asks the pool contract how many LP tokens the strategy contract currently holds. address(this) means "this contract, the one making the call".
  • * burnt multiplies that balance by the number of strategy tokens being burned right now.
  • / totalSupply_ divides by the total number of strategy tokens that exist, so the caller gets a slice proportional to their share.

The trouble is where that first number comes from. pool.balanceOf(address(this)) is simply the strategy contract's current LP token balance, and anyone can increase that balance by sending pool tokens to the contract directly. Sending tokens to a contract is like pushing cash through a letterbox: the contract receives it whether or not it asked. The attacker therefore controls one term in the payout formula. The report notes that the pool tokens used to inflate the calculation stay inside the strategy contract, so the attacker can mint share tokens afterwards and burn them to recover the pool tokens they used.

How the attack works, step by step

Step 1: Inflate the strategy contract's LP balance

The attacker transfers a specific amount of pool tokens directly to the strategy contract. This raises pool.balanceOf(address(this)) without going through the normal deposit path, so no strategy tokens are minted in return for it. In everyday terms, the attacker tops up the till without getting a receipt, so the till looks fuller than the receipts say it should be.

Step 2: Burn strategy shares to withdraw inflated LP tokens

The attacker calls burn(…). The contract burns the strategy tokens and computes the payout with the inflated balance:

Text
poolTokensObtained = pool.balanceOf(address(this)) * burnt / totalSupply_;

Because the top of the fraction now includes the transferred tokens, the caller receives more LP tokens than their share of the strategy warrants. The attacker is paid as if the extra tokens were part of the shared pot, even though they were not deposited through the proper route.

Step 3: Recover the tokens used to inflate the balance

The pool tokens transferred in step 1 are still held by the strategy contract. The attacker calls mint(…) to obtain strategy share tokens, then calls burn(…) again to get back the LP tokens that were transferred. The report describes this as the method by which the attacker retrieves the tokens used to inflate the calculation. In other words, the money used to inflate the till is not lost: the attacker gets it back through the normal deposit and withdrawal route, while keeping the extra payout from step 2.

Trying it yourself

The proof of concept, or PoC, is a small test program that demonstrates the attack safely on a test setup. It runs in Forge, the testing tool that comes with a development framework called Foundry. The report gives three steps to reproduce it:

  1. Install the Foundry framework from https://github.com/foundry-rs/foundry
  2. Clone the Immunefi bugfix review repository:
Bash
git clone https://github.com/immunefi-team/bugfix-reviews-pocs.git
  1. Run the following command to run the PoC:
Bash
forge test -vvv — match-path ./test/YieldProtocol/AttackTest.t.sol

What each command does:

  • git clone https://github.com/immunefi-team/bugfix-reviews-pocs.git uses git, a tool for copying code repositories, to download a folder of example tests from GitHub, a website where code is shared. It is like downloading a zip file of sample exercises.
  • forge test runs the tests in that folder. -vvv asks for very detailed output, so you can see what happened inside the test. — match-path ./test/YieldProtocol/AttackTest.t.sol narrows the run to one specific test file, the one that contains the attack.

The report links a strategy contract at [redacted] on Arbiscan. Arbitrum is a blockchain network that runs alongside Ethereum, and Arbiscan is a website for looking up activity on it.

After cloning the repository and running the Forge command above, the test produces the following log output:

From the original on medium.com

The report presents this log as the output of the PoC run.

Why it worked

The payout formula trusts a balance that outside parties can change. pool.balanceOf(address(this)) reflects every LP token the strategy contract holds, including tokens sent to it directly rather than deposited through the strategy. The formula reads the live balance, so a direct transfer changes the value used to price a burn. A cached figure would be a number the contract writes down at deposit time and trusts afterwards, like a shopkeeper counting the till at opening and using that count all day instead of re-counting whenever someone walks past.

Impact

The report states that the vulnerability could allow an attacker to drain the base tokens from a pool by manipulating the token balance of a contract. It calls the issue a critical logic error and estimates that the fix saved approximately $950k in funds across various pools on both the Arbitrum and Ethereum blockchains. No user funds were lost. The whitehat received a bounty of $95,000 USDC, equivalent to 10% of the funds at risk.

The fix

The project team modified the burn(…) function in the strategy contract to use poolCached_ instead of pool.balanceOf(address(this)) when determining how many LP tokens should be transferred to the caller. The new formula is:

Text
poolTokensObtained = poolCached_ * burnt / totalSupply_;

In plain words, the payout is now based on a stored figure, poolCached_, rather than on the live balance. The patched formula uses poolCached_ instead of the live balance. The fix is in commit [redacted] in the yieldprotocol/strategy-v2 repository. A commit is a saved change in a code repository, with a unique identifier so anyone can look it up.

Similar things to look for

These are patterns worth testing if you ever review a contract of this kind:

  • Share-price formulas that read live token balances. Look for any mint, burn or redeem path that multiplies by token.balanceOf(address(this)) or an equivalent live balance. A positive result is that sending tokens directly to the contract changes the amount a later burn or redeem returns.
  • Donation attacks on vault-style contracts. Transfer the underlying or LP token to the contract outside the deposit function, then mint and burn in the smallest possible amounts. A positive result is that the attacker's round trip returns more than it cost.
  • Cached versus live accounting. Compare the value the contract stores at deposit time with the value it reads at withdrawal time. A positive result is a divergence between the two after a direct transfer.
  • Recovery of donated tokens. After inflating a balance, check whether a later mint and burn returns the donated tokens to the attacker. A positive result is that the inflation is free, which is the condition that turns a rounding quirk into a drain.
  • Cross-pool exposure. The report says the fix saved approximately $950k across pools on both Arbitrum and Ethereum. Check whether the same strategy code is deployed against several pools, and whether an inflation against one pool affects payouts in another.

Glossary

  • Smart contract: a program stored on a blockchain that anyone can call and that runs exactly as written.
  • Blockchain: a shared, tamper-resistant record of transactions kept by many computers.
  • DeFi: short for decentralised finance, financial services run by smart contracts rather than banks.
  • Yield Protocol: a DeFi protocol offering fixed-rate, fixed-term loans between borrowers and lenders.
  • fyToken: a fixed yield token, an ERC-20 token exchangeable one-to-one for an underlying asset at a set maturity date.
  • ERC-20: a common standard format for tokens on Ethereum-style blockchains.
  • Underlying asset: the token that an fyToken can be exchanged for at maturity.
  • Maturity date: the fixed date on which an fyToken can be redeemed.
  • Strategy contract: a helper contract that takes liquidity providers' funds and deposits them into a pool.
  • Liquidity provider: someone who deposits funds into a pool so others can trade or borrow, earning fees in return.
  • YieldSpace Pool: the pool contract into which the strategy contract deposits combined liquidity.
  • Pool: a pot of tokens managed by a contract.
  • LP token: a liquidity pool token, a receipt representing a share of a pool.
  • Strategy token: a token minted by the strategy contract in proportion to a deposit, burnable later to redeem LP tokens and gains.
  • Mint: to create new tokens, here the strategy tokens issued for a deposit.
  • Burn: to destroy tokens, here strategy tokens destroyed to redeem the underlying share.
  • Function: a named block of code that can be called, optionally with parameters.
  • Parameter: an input value passed to a function, such as the address that should receive tokens.
  • Address: an identifier for an account or contract on a blockchain.
  • address(this): in a contract, a way of referring to the contract's own address.
  • balanceOf: a function that reports how many tokens a given address holds.
  • Logic error: a flaw where code runs as written but the logic or arithmetic allows unintended behaviour.
  • Whitehat: a security researcher who reports flaws privately instead of exploiting them.
  • Immunefi: a platform for reporting security flaws in web3 projects and paying bounties.
  • Bounty: a reward paid for a valid security report.
  • Proof of concept (PoC): a small program or test that demonstrates a vulnerability safely.
  • Foundry: a development framework for Ethereum-style smart contracts.
  • Forge: the testing tool included with Foundry.
  • git: a tool for copying and tracking versions of code repositories.
  • Repository: a folder of code, often hosted on a site such as GitHub.
  • Commit: a saved change in a repository, identified by a unique hash.
  • Arbitrum: a blockchain network that runs alongside Ethereum.
  • Ethereum: a blockchain network on which many DeFi contracts run.
  • Arbiscan: a website for looking up activity on the Arbitrum blockchain.
  • USDC: a digital currency designed to be worth one US dollar.
  • Donation attack: sending tokens directly to a contract to distort a calculation that reads its live balance.
  • Cached value: a figure stored by a contract at one point and reused later, instead of re-reading a live balance.
  • Drain: to remove all or most of the funds from a pool.
  • Base token: the main currency held by a pool.

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.