Skip to content
Access control Write-up Intermediate

BeanStalk

The transferTokenFrom function in Beanstalk's Token Facet only checked internal balance allowances, not external token allowances. An attacker could call it with EXTERNAL mode to transfer tokens from any user who had approved the Beanstalk contract, stealing up to $3.1 million.

medium.com

Published on medium.com

Beanstalk

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

Beanstalk is a permissionless stablecoin protocol built on Ethereum. Its native fiat currency, the stablecoin called Bean, is intended to create a monetary basis for a rent-free economy on the Ethereum network. The protocol aims to incentivise independent market participants to sustainably cross the price of 1 Bean over its dollar peg, without collateral, while keeping decentralisation and competitive carrying costs.

The contract that matters here is the Beanstalk diamond proxy, a modular smart contract system that can be upgraded or extended after deployment without significant size constraints. It operates by using external functions provided by contracts known as facets. Facets are independent contracts that can access shared internal functions, libraries and state variables. The diamond proxy in question was using the Token Facet, which handles the logic of farming: querying internal balances of accounts, approving tokens and transferring tokens.

The vulnerable function was transferTokenFrom() in the Token Facet, which transfers tokens from a sender to a recipient. It takes transfer modes (fromMode, toMode) that can be either EXTERNAL or INTERNAL. INTERNAL mode updates the internal token balance of the account in the LibBalance facet, which contains the accounting logic of the contract. EXTERNAL mode transfers the amount of tokens directly from the sender to the recipient using the token.safeTransferFrom call.

Setup

The report describes an anonymous whitehat submitting the issue to the Beanstalk protocol through Immunefi on 15 November. The proof of concept was prepared by the Immunefi team and run with Foundry. The report does not give further details of the researcher's environment, accounts or tool versions.

The precondition for the bug is that a victim, either an externally owned account (EOA) or a contract, has authorised the Beanstalk contract to handle its tokens using the ERC20 approve() function. Only accounts that had granted that approval were affected.

Finding it

The whitehat reported the vulnerability in one of the facet libraries used by the Beanstalk diamond proxy. The Token Facet contract can be viewed at the following address:

Text
[redacted]

The facets used by the Beanstalk diamond proxy can be explored on Louper, an interface for inspecting Ethereum diamond proxy facets, which is how the TokenFacet contract can be located. The diamond proxy itself is at:

Text
[redacted]

The problem is in how transferTokenFrom() handles the two transfer modes. The function checks the allowance for the internal balance for msg.sender, but not for external transfers. When msg.sender calls the function with the EXTERNAL transfer type, the allowance is not checked and LibTransfer.transferToken(...) is involved, which calls token.safeTransferFrom(victim, attacker, amount). The attacker would then receive funds from the victim's account, provided that victim had already granted approval to the Beanstalk contract for the transfer of that token.

Exploitation

Step 1: Identify an approved victim

The attacker needs an account that has called ERC20 approve() on the token, authorising the Beanstalk contract to spend it. The report does not describe how such accounts were enumerated; the condition is simply that the approval exists.

Step 2: Call transferTokenFrom() with EXTERNAL mode

The attacker calls transferTokenFrom() on the Token Facet, setting the transfer mode so that the external path is taken. Because the allowance check only covers the internal balance of msg.sender, the call is not rejected. The external path reaches LibTransfer.transferToken(...), which performs:

Text
token.safeTransferFrom(victim, attacker, amount)

This moves the tokens from the approving victim to the attacker.

Step 3: Confirm with the PoC

The Immunefi team prepared a Foundry proof of concept to demonstrate the vulnerability. The report shows the output of running it:

Output of running Foundry PoC From the original on medium.com

The report shows the output of running the Foundry PoC. The report does not reproduce the PoC source itself.

Root cause

The mistake is an authorisation gap between the two transfer modes. transferTokenFrom() validates the allowance for the internal balance of msg.sender, but the EXTERNAL branch does not perform that check. The external branch delegates to LibTransfer.transferToken(...), which calls token.safeTransferFrom(victim, attacker, amount) and therefore relies on the victim's ERC20 approval of the Beanstalk contract rather than on any authorisation of the caller. The existing allowance check did not stop the attack because it only covered the internal path.

Impact

The vulnerability allowed direct theft of assets from accounts that had approved the Beanstalk contract. The Beanstalk Immunefi Committee estimated that the vulnerability could have resulted in a loss of up to $3.1 million in funds, with $537k worth of BEAN tokens and $2.5 million in non-BEAN assets at risk. The total funds at risk was about $3,087,655, composed of multiple assets:

Source From the original on medium.com

The impact was limited to EOAs or contracts that had authorised the Beanstalk contract to handle their tokens using ERC20 approve(). No user funds were lost: the Beanstalk Community Multisig remediated the issue quickly after the report. The whitehat was awarded 181,850 BEAN tokens ($181,850 USD) through Beanstalk's bug bounty program on Immunefi.

Fix

An EBIP (Emergency Beanstalk Improvement Proposal) was submitted to remove the vulnerable transferTokenFrom(...) function until a suitable fix could be implemented. The Beanstalk Community Multisig removed the transferTokenFrom(...) functionality and introduced a new function, transferInternalTokenFrom(...), which always transfers with INTERNAL fromMode. The changes were made in accordance with the BIP, implemented in EBIP-6 and the Facet contract upgrade, as outlined in the fixed GitHub pull request.

Variants to test

  1. Other facets with mode arguments. Look for functions that accept a mode or flag selecting between an internal accounting path and an external token movement path. A positive result is an external branch that reaches safeTransferFrom without checking the caller's allowance.
  2. Allowance checks scoped to the wrong party. Search for allowance checks that validate msg.sender's internal balance but not the source account used in the external call. A positive result is a call where the source of the transfer differs from the account whose allowance was checked.
  3. Approved contracts as victims. Enumerate accounts and contracts that have granted ERC20 approval to the target contract, then attempt the external path against them. A positive result is a transfer that succeeds from an account that never authorised the caller.
  4. Diamond proxy upgrades. After a facet upgrade, re-test the replaced functions and any new wrappers such as transferInternalTokenFrom(...). A positive result is a new entry point that reintroduces the external path without the allowance check.

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

What happened, in plain words

Imagine you keep your money in a bank, and you sign a standing permission slip that lets a particular shop take money from your account when you buy something. That permission slip is useful, but it is also dangerous: if the shop's till has a flaw, someone else might be able to use your permission slip to empty your account without you buying anything at all.

That is roughly what happened here. Beanstalk is a piece of software on the Ethereum blockchain that holds people's tokens. To work, it needs permission to move tokens on behalf of its users, and users grant that permission with a standard blockchain instruction called approve(). A security researcher (called a "whitehat", meaning someone who finds bugs and reports them instead of abusing them) discovered that one function inside Beanstalk would move a user's tokens to whoever asked, without checking whether the asker had any right to them. The only thing needed was that the victim had once given Beanstalk permission to move their tokens.

On 15 November an anonymous whitehat reported this to Beanstalk through Immunefi, a platform that runs bug bounty programmes (rewards for people who find and report security bugs). The Beanstalk Immunefi Committee estimated the flaw could have led to a loss of up to $3.1 million: $537k worth of BEAN tokens and $2.5 million in other assets. The Beanstalk Community Multisig remediated the issue, no user funds were lost, and the whitehat was paid 181,850 BEAN tokens, worth $181,850 USD.

This article walks through the whole story from zero, explaining every term as it comes up.

What is Beanstalk?

Beanstalk is a permissionless stablecoin protocol built on Ethereum. Let us unpack that.

  • Ethereum is a public blockchain: a shared computer network that runs programs called smart contracts. Anyone can send instructions to those programs, and the programs run exactly as written.
  • A smart contract is a program that lives on the blockchain. It holds data (such as balances) and exposes functions (actions) that anyone can call. Think of it as a vending machine that is always on and never sleeps.
  • A stablecoin is a digital token designed to stay close to a fixed price, usually one US dollar. Ordinary cryptocurrencies swing up and down; a stablecoin tries not to.
  • Bean is Beanstalk's own stablecoin, intended to stay near one dollar.
  • Permissionless means nobody has to be approved to use it. There is no sign-up desk; anyone with an Ethereum account can interact.

Beanstalk's stated aim is to create a monetary basis for a rent-free economy on Ethereum, and to incentivise independent market participants to sustainably push the price of 1 Bean above its dollar peg. It does this without collateral (no assets locked up as a guarantee) while keeping decentralisation and competitive carrying costs.

The building blocks: diamond proxy and facets

The contract at the centre of this story is the Beanstalk diamond proxy. A proxy is a contract that sits in front of other contracts and forwards calls to them. A diamond proxy is a specific pattern (described in Ethereum standard EIP-2535) that lets a system be upgraded or extended after deployment without significant size constraints.

Why does that matter? A normal smart contract has a size limit, and once deployed it cannot be changed. A diamond proxy solves both problems: it is a single address that users talk to, but the actual code lives in separate contracts called facets. Each facet is an independent contract that provides a set of functions, and facets can share internal functions, libraries and state variables. Upgrading the system means swapping a facet, not redeploying everything.

An everyday comparison: think of a restaurant with one front door (the diamond proxy) and several kitchens behind it (the facets). Customers always walk through the same door, but the kitchen that prepares their dish depends on what they ordered. The restaurant can renovate one kitchen without closing the front door.

The facet involved here is the Token Facet, which handles the logic of farming: querying the internal balances of accounts, approving tokens and transferring tokens. "Farming" in this context is the set of operations around managing and moving tokens inside the protocol.

The vulnerable function

The problem was in a function called transferTokenFrom() in the Token Facet. Its job is simple to describe: it transfers tokens from a sender to a recipient.

What makes it interesting is that it takes two extra arguments, fromMode and toMode, which select a transfer mode. Each mode can be either EXTERNAL or INTERNAL:

  1. INTERNAL mode updates the internal token balance of the account in the LibBalance facet. LibBalance is the part of the system that keeps the accounting, like a ledger inside the protocol.
  2. EXTERNAL mode transfers the amount of tokens directly from the sender to the recipient using the token.safeTransferFrom call. This is a real movement of tokens on the blockchain, not just a number in an internal ledger.

So the same function can either shuffle numbers in an internal book or move real tokens. That dual behaviour is where the bug hides.

Setup: who reported it and how it was tested

An anonymous whitehat submitted the issue to the Beanstalk protocol through Immunefi on 15 November. The proof of concept (a small demonstration that proves the bug is real) was prepared by the Immunefi team and run with Foundry. Foundry is a toolkit for building and testing Ethereum smart contracts; you write a test in code, run it, and it simulates the blockchain locally so you can see what would happen.

The precondition for the bug is that a victim, either an externally owned account (EOA) or a contract, has authorised the Beanstalk contract to handle its tokens using the ERC20 approve() function. Let us explain those terms:

  • An EOA is a normal Ethereum account controlled by a person with a private key, as opposed to a contract account controlled by code.
  • ERC20 is the most common standard for tokens on Ethereum. It defines a fixed set of functions every token must have.
  • approve() is one of those functions. It says: "I, the token holder, allow this other address to spend up to this amount of my tokens." It is like a standing permission slip at a shop: you are not paying yet, but you have authorised the shop to take money when needed.

Only accounts that had granted that approval were affected. If you never gave Beanstalk permission to move your tokens, this bug could not touch you.

Finding it: where the code lives

The whitehat reported the vulnerability in one of the facet libraries used by the Beanstalk diamond proxy. The Token Facet contract can be viewed at this address:

Text
[redacted]

An Ethereum address is like a bank account number for a contract or a person: a long string of characters that identifies exactly one thing on the network. You can paste it into a block explorer (a website that shows everything on the blockchain) to read the code.

The facets used by the Beanstalk diamond proxy can be explored on Louper, an interface for inspecting Ethereum diamond proxy facets. Louper is useful because a diamond proxy hides its facets: the front door is one address, but the code is spread across several. Louper shows you which facet provides which function, which is how the TokenFacet contract can be located. The diamond proxy itself is at:

Text
[redacted]

The problem is in how transferTokenFrom() handles the two transfer modes. The function checks the allowance for the internal balance for msg.sender, but not for external transfers. Two more terms:

  • msg.sender is the address that called the function. In plain words, "who is asking".
  • An allowance check is the code that asks: "has this caller been given permission to move this many tokens?" If the answer is no, the call should fail.

When msg.sender calls the function with the EXTERNAL transfer type, the allowance is not checked, and LibTransfer.transferToken(...) is involved. That library function calls:

Text
token.safeTransferFrom(victim, attacker, amount)

Line by line:

  • token is the token contract being moved.
  • safeTransferFrom is the ERC20 function that moves tokens from one address to another, provided the owner has approved the caller.
  • victim is the account the tokens are taken from.
  • attacker is the account the tokens go to.
  • amount is how many tokens are moved.

The attacker would then receive funds from the victim's account, provided that victim had already granted approval to the Beanstalk contract for the transfer of that token. In other words, the victim's permission slip was addressed to Beanstalk, but the flawed function let anyone use it.

Exploitation: how the theft would work

Step 1: Identify an approved victim

The attacker needs an account that has called ERC20 approve() on the token, authorising the Beanstalk contract to spend it. The report does not describe how such accounts were enumerated; the condition is simply that the approval exists.

Why this step comes first: without an approval, there is nothing to abuse. The bug does not create permission out of thin air; it misuses permission that already exists.

Step 2: Call transferTokenFrom() with EXTERNAL mode

The attacker calls transferTokenFrom() on the Token Facet, setting the transfer mode so that the external path is taken. Because the allowance check only covers the internal balance of msg.sender, the call is not rejected. The external path reaches LibTransfer.transferToken(...), which performs the transfer shown above.

Why this works: the check that should have stopped the call was written for the internal path only. The external path skipped it entirely, so the function happily moved real tokens from the victim to the attacker.

Step 3: Confirm with the PoC

The Immunefi team prepared a Foundry proof of concept to demonstrate the vulnerability. The report shows the output of running it:

Output of running Foundry PoC From the original on medium.com

The report shows the output of running the Foundry PoC. The report does not reproduce the PoC source itself.

Root cause

The mistake is an authorisation gap between the two transfer modes. transferTokenFrom() validates the allowance for the internal balance of msg.sender, but the EXTERNAL branch does not perform that check. The external branch delegates to LibTransfer.transferToken(...), which calls token.safeTransferFrom(victim, attacker, amount) and therefore relies on the victim's ERC20 approval of the Beanstalk contract rather than on any authorisation of the caller. The existing allowance check did not stop the attack because it only covered the internal path.

An everyday comparison: a building has two doors, and the guard only checks badges at the first door. Anyone who walks to the second door gets in without being asked for a badge, even though the building was supposed to be protected.

Impact

The vulnerability allowed direct theft of assets from accounts that had approved the Beanstalk contract. The Beanstalk Immunefi Committee estimated that the vulnerability could have resulted in a loss of up to $3.1 million in funds, with $537k worth of BEAN tokens and $2.5 million in non-BEAN assets at risk. The total funds at risk was about $3,087,655, composed of multiple assets:

Source From the original on medium.com

The impact was limited to EOAs or contracts that had authorised the Beanstalk contract to handle their tokens using ERC20 approve(). No user funds were lost: the Beanstalk Community Multisig remediated the issue quickly after the report. The whitehat was awarded 181,850 BEAN tokens ($181,850 USD) through Beanstalk's bug bounty program on Immunefi.

Fix

An EBIP (Emergency Beanstalk Improvement Proposal) was submitted to remove the vulnerable transferTokenFrom(...) function until a suitable fix could be implemented. An improvement proposal is a formal change request for the protocol.

The Beanstalk Community Multisig removed the transferTokenFrom(...) functionality and introduced a new function, transferInternalTokenFrom(...), which always transfers with INTERNAL fromMode. In plain words: the vulnerable function was removed and replaced with one that always uses the INTERNAL fromMode.

The changes were made in accordance with the BIP, implemented in EBIP-6 and the Facet contract upgrade, as outlined in the fixed GitHub pull request.

Variants to test

If you want to look for similar bugs elsewhere, these are the patterns worth checking:

  1. Other facets with mode arguments. Look for functions that accept a mode or flag selecting between an internal accounting path and an external token movement path. A positive result is an external branch that reaches safeTransferFrom without checking the caller's allowance.
  2. Allowance checks scoped to the wrong party. Search for allowance checks that validate msg.sender's internal balance but not the source account used in the external call. A positive result is a call where the source of the transfer differs from the account whose allowance was checked.
  3. Approved contracts as victims. Enumerate accounts and contracts that have granted ERC20 approval to the target contract, then attempt the external path against them. A positive result is a transfer that succeeds from an account that never authorised the caller.
  4. Diamond proxy upgrades. After a facet upgrade, re-test the replaced functions and any new wrappers such as transferInternalTokenFrom(...). A positive result is a new entry point that reintroduces the external path without the allowance check.

Glossary

  • Allowance: the amount of a token that one address is permitted to spend on behalf of another, granted with approve().
  • Bean: Beanstalk's native stablecoin, intended to stay near one US dollar.
  • Beanstalk: a permissionless stablecoin protocol built on Ethereum.
  • Beanstalk Community Multisig: a group of keyholders who can act quickly to change the Beanstalk protocol.
  • Beanstalk Immunefi Committee: the group that estimated the impact of this vulnerability.
  • BIP: Beanstalk Improvement Proposal, a formal change request for the protocol.
  • Bug bounty programme: a scheme that pays people for finding and reporting security bugs responsibly.
  • Collateral: assets locked up as a guarantee, which Beanstalk does not require.
  • Diamond proxy: a smart contract pattern (EIP-2535) that forwards calls to separate contracts called facets, allowing upgrades without size limits.
  • EBIP: Emergency Beanstalk Improvement Proposal, a fast-tracked change request.
  • EOA (externally owned account): an Ethereum account controlled by a person with a private key, rather than by code.
  • ERC20: the standard interface for tokens on Ethereum, defining functions such as approve() and safeTransferFrom.
  • Ethereum: a public blockchain network that runs smart contracts.
  • EXTERNAL mode: a transfer mode that moves real tokens on the blockchain.
  • Facet: an independent contract that provides functions to a diamond proxy.
  • Foundry: a toolkit for building and testing Ethereum smart contracts.
  • INTERNAL mode: a transfer mode that updates an internal ledger balance rather than moving real tokens.
  • LibBalance: the part of Beanstalk that keeps the internal accounting.
  • LibTransfer: a library used by the Token Facet to perform token transfers.
  • Louper: an interface for inspecting the facets of an Ethereum diamond proxy.
  • msg.sender: the address that called a function.
  • Permissionless: open to anyone, with no approval needed to use it.
  • Proof of concept (PoC): a small demonstration that proves a vulnerability is real.
  • Proxy: a contract that forwards calls to other contracts.
  • safeTransferFrom: an ERC20 function that moves tokens from one address to another, relying on an existing approval.
  • Smart contract: a program that lives on a blockchain and exposes functions anyone can call.
  • Stablecoin: a digital token designed to stay close to a fixed price.
  • Token Facet: the Beanstalk facet that handles farming logic, including querying balances, approving tokens and transferring tokens.
  • Whitehat: a security researcher who finds bugs and reports them instead of abusing them.

Similar reports

View all

Same bug class and techniques, clearest first.

Broken access control

The lesson behind this report.

A user can do something their role should not allow: reach an admin function, act on a feature they have not paid for, or skip a permission check. IDOR is the special case where the object belongs to someone else.