Skip to content
Business logic Write-up Intermediate

Free blockchain storage – Tale of a bug in Substrate’s FRAME runtime

A bug in Substrate's FRAME runtime fee calculation allowed attackers to include arbitrarily large payloads in transactions without paying extra fees. This could let anyone store unlimited data on the blockchain for free.

Mudit Gupta Follow

Published on mudit.blog

Bounty$250 View original

This is the story of a simple bug in the FRAME runtime of Parity’s Substrate blockchain framework. The bug allowed attackers to do infinitely large transactions without paying any extra fees.

Mudit Gupta, on mudit.blog

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

Context

Substrate is Parity Technologies' blockchain framework, and FRAME is its runtime module system. One of the things FRAME does is calculate the fee for every transaction (extrinsic) that is submitted to a chain built on it. The fee is meant to grow with the size of the transaction, so that stuffing large amounts of data into the chain costs proportionally more.

The bug described here lived in that fee calculation. It let an attacker submit transactions with arbitrarily large payloads without paying any extra fee for the bytes. The author's example is putting an entire manga collection into the payload of a simple transaction, which would have made it part of the blockchain forever: the cheapest decentralised blockchain storage available.

Setup

The report does not describe any special accounts, roles or tooling beyond a browser and the public Polkadot UI. The precondition is a chain where the fee multiplier targeted_fee_adjustment has dropped low enough that the size fee is not applied. Kusama is named as the place to try it, and the fix was deployed to Kusama, Polkadot and the Polymesh Aldebaran testnet.

Finding it

The transaction fee is supposed to increase with the size of the transaction. A constant, TransactionByteFee, is meant to be added to the total fee for every byte of data in the transaction. That alone would have stopped the large-payload trick, but a small mistake in the code meant the fee was not applied under certain circumstances.

The fee was calculated as:

Text
fee = base_fee + targeted_fee_adjustment * (len_fee + weight_fee);

The terms are:

  • base_fee: a static fee applied to all transactions, analogous to the 21k gas fee in Ethereum.
  • targeted_fee_adjustment: a fee multiplier based on recent activity on the chain. If recent blocks have been full, the multiplier increases; if recent blocks have been empty, it decreases.
  • len_fee: the extra fee charged for the size of a transaction. Bigger payloads give a bigger len_fee.
  • weight_fee: based on the compute requirements of a transaction. A more resource-intensive transaction has a higher weight_fee.

Two problems combine here. First, len_fee is multiplied by targeted_fee_adjustment, but according to the W3F documentation on setting transaction fees it should not be. Second, targeted_fee_adjustment was allowed to fall as low as zero. When the chain is not busy, the multiplier can fall as low as zero, and then neither len_fee nor weight_fee is charged. Anyone can then submit large transactions and inflate the size of the blockchain.

The author illustrates the effect with sample fees for a 2 megabyte payload before the bug fix:

From the original on mudit.blog

and after the bug fix:

From the original on mudit.blog

Exploitation

The vulnerability was very easy to exploit: send a huge payload alongside a valid transaction. The reproduction steps are:

Step 1: Open the Polkadot UI

Go to https://polkadot.js.org/apps.

Step 2: Open the Extrinsics tab

Click on the "Extrinsics" tab on the left.

Step 3: Select the remark extrinsic

Select the "remark" extrinsic from the "system" frame.

Step 4: Upload the payload

Click on "file upload" on the right and upload your payload, anything you want to put on the blockchain.

Step 5: Submit

Submit the transaction. With the multiplier low, the size of the payload does not add to the fee.

Root cause

The fee formula applied the targeted_fee_adjustment multiplier to len_fee as well as to weight_fee, contrary to the W3F research, and the multiplier itself was permitted to reach zero. The existing defence, the per-byte TransactionByteFee folded into len_fee, was therefore neutralised whenever the chain was quiet: multiplying the length component by zero removed it entirely.

Impact

An attacker could add arbitrary data, such as a manga collection, to the payload of a simple transaction without paying any extra fees, and that data would become part of the blockchain forever. The condition is a quiet chain, where targeted_fee_adjustment has fallen low enough to cancel the size fee. The effect is cheap, permanent inflation of the blockchain's size.

Fix

The fix was twofold but simple. First, len_fee was moved out of the multiplier's scope, giving the new formula:

Text
fee = base_fee + len_fee + targeted_fee_adjustment * weight_fee;

Second, targeted_fee_adjustment was adjusted so that its minimum is one instead of zero. This brings the implementation closer to what the W3F research suggested and makes the exploit impossible. The change is in paritytech/substrate PR #6334, and a runtime upgrade was deployed to Kusama, Polkadot and the Polymesh Aldebaran testnet.

The bug was reported on 17/06/2020, acknowledged the same day and fixed the same day. Parity offered a 250 USD bug bounty, which was donated to the Smile Foundation on the author's behalf. Fredrik Harrysson, CTO at Parity, commented that Kusama exists to test the underlying economic assumptions before they reach Polkadot, and encouraged continued bug hunting on Kusama.

Variants to test

  • Look for fee or pricing formulas where a size-based component is multiplied by a dynamic adjustment factor. A positive result is a quiet period in which the size component collapses to zero or near zero while the payload still goes through.
  • Check whether dynamic multipliers have a lower bound of zero. A positive result is a multiplier that reaches zero under low load and removes a whole fee term.
  • Compare the implementation against the published specification for each fee term. A positive result is a term that the spec says should be independent but that the code scales by another factor.
  • On any chain with a remark-style extrinsic, submit a large payload during an idle period and compare the fee with the same payload during a busy period. A positive result is a fee that does not grow with payload size when the chain is quiet.

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

What happened, in plain words

A blockchain is a shared record book that many computers keep copies of. Once something is written into it, it stays there, and every computer that keeps a copy has to store it. Because storage is not free, blockchains charge a fee for each transaction (an instruction sent to the network), and that fee is meant to grow with the amount of data the transaction carries. If you send a lot of data, you pay more.

This is the story of a simple bug in the FRAME runtime of Parity's Substrate blockchain framework. Substrate is a toolkit that Parity Technologies provides for building blockchains, and FRAME is the part of Substrate that defines how a chain behaves, including how it works out the fee for each transaction. The bug allowed attackers to do infinitely large transactions without paying any extra fees. The vulnerability was the result of a buggy implementation of fee calculation in the FRAME runtime of Substrate. By exploiting this vulnerability, an attacker could have added their manga collection to the payload of a simple transaction without paying any extra fees. This would have resulted in the manga collection becoming a part of the blockchain, forever. Cheapest decentralised blockchain storage!

From the original on mudit.blog

To see the effect, here are sample fees for a 2 megabyte payload before the bug fix:

From the original on mudit.blog

And here are sample fees for a 2 megabyte payload after the bug fix:

From the original on mudit.blog

Please note that this bug is now fixed and a runtime upgrade has been deployed to Kusama, Polkadot and Polymesh Aldebaran testnet. A runtime upgrade is a change to the rules the chain follows, pushed out to the network so that every node applies the new rules.

Bug details

The transaction fee is supposed to increase as the size of the transaction increases. There's a constant TransactionByteFee that is supposed to be added to the total fee for every byte of data contained in the transaction. A byte is a single unit of data, roughly one character of text. This would have prevented the above exploit from working, but due to a very tiny bug in the code, this fee was not being applied under certain circumstances.

The fee was being calculated as:

Text
fee = base_fee + targeted_fee_adjustment * (len_fee + weight_fee);

Line by line, in plain words:

  • fee = means "the total amount the sender is charged is set to the following".
  • base_fee is a fixed starting amount that every transaction pays, whatever it contains.
  • + means this amount is added to the total.
  • targeted_fee_adjustment is a multiplier, a number the total is scaled by, which changes depending on how busy the chain has been.
  • * means multiply. Everything inside the brackets is multiplied by that adjustment number.
  • (len_fee + weight_fee) is a pair of charges added together first, and then scaled by the multiplier.
  • len_fee is the part of the charge that depends on how many bytes the transaction carries.
  • weight_fee is the part of the charge that depends on how much computing work the transaction asks the network to do.

Each term is described in the report as follows:

  • base_fee is a static fee applied to all transactions (analogous to the 21k gas fee in Ethereum). In Ethereum, another blockchain, every simple transfer costs a fixed minimum amount of "gas", the unit used to measure computing work; the base fee plays the same role here.
  • targeted_fee_adjustment is a fee multiplier that is based on recent activity on the chain. If the recent blocks have been full, this multiplier will increase and if the recent blocks have been empty, this multiplier will decrease. A block is a batch of transactions added to the chain at once.
  • len_fee is the extra fee charged for the size of a transaction. Transactions with bigger payloads will have a bigger len_fee. The payload is the data the transaction carries along with it.
  • weight_fee is based on the compute requirements of a transaction. A more resource-intensive transaction will have a higher weight_fee.

There are two problems here. Firstly, the len_fee is being affected by the targeted_fee_adjustment multiplier. However, as per the W3F docs, it should not be. The W3F, the Web3 Foundation, publishes research describing how these fees are meant to work, so the code can be compared against a written specification. The second problem is that the targeted_fee_adjustment multiplier was allowed to go as low as zero. The combined result of these two problems is that when the chain is not busy, no len_fee, and weight_fee are charged and anyone can submit large transactions to inflate the blockchain size.

Think of it like a taxi meter that is supposed to charge for distance and for waiting time. If the waiting-time rate is set by how busy the streets are, and that rate can drop to zero, then on a quiet night the meter stops counting the distance too, and you ride across the city for the price of simply getting in.

Reproduction steps

The vulnerability was very easy to exploit. All you had to do was to send a huge payload alongside a valid transaction. Sample reproduction steps are as follows:

  1. Open the Polkadot UI at https://polkadot.js.org/apps. This is a website that lets you build and send transactions to a Substrate-based chain from an ordinary browser, without installing anything special.
  2. Click on the "Extrinsics" tab on the left. An extrinsic is simply a transaction, an instruction coming from outside the chain's own code.
  3. Select the "remark" extrinsic from the "system" frame. The "system" frame is the built-in set of basic instructions every Substrate chain has, and "remark" is the one that stores an arbitrary piece of data on the chain, with no other purpose.
  4. Click on "file upload" on the right and upload your payload (anything you want to put on the blockchain). This is where the manga collection goes: the file becomes the data carried by the transaction.
  5. Submit the transaction. Because the multiplier was low, the size of the payload added nothing to the fee.

Each step exists for a reason. Opening the UI gives you a way to talk to the chain without writing code. The Extrinsics tab is where the UI lets you assemble a transaction by hand. Uploading the file is how the payload gets attached. Submitting sends it to the network, where the fee is calculated by the buggy formula.

Resolution

The fix was twofold but simple. Firstly, the len_fee was moved out so that it was not affected by the targeted_fee_adjustment multiplier. The new fee formula is

Text
fee = base_fee + len_fee + targeted_fee_adjustment * weight_fee;

Reading it line by line:

  • fee = sets the total charge.
  • base_fee is still the fixed amount every transaction pays.
  • + len_fee adds the size-based charge directly, outside any multiplier, so it can no longer be scaled down to nothing.
  • + targeted_fee_adjustment * weight_fee keeps the busy-ness multiplier, but now it only scales the computing-work charge.

In addition, the targeted_fee_adjustment was adjusted so that the minimum multiplier is one instead of zero. This brings the implementation closer to what the W3F research suggested. It makes exploits like the one mentioned in this post impossible. With a floor of one, the multiplier can still rise when the chain is busy, but it can never cancel a charge out entirely.

Timeline

  • Reported: 17/06/2020
  • Acknowledged: 17/06/2020
  • Fixed: 17/06/2020

Addendum

The author thanks Parity Technologies for being responsive and fixing the bug within hours of the report. Kusama and Polkadot were upgraded soon after the bug fix. Kusama is a network created to test the underlying economic assumptions before they reach Polkadot. Parity offered a 250 USD bug bounty for this disclosure, which they kindly donated to the Smile Foundation on the author's behalf. A bug bounty is a reward a company pays to someone who reports a security problem to them.

Everyone is welcome to try their hands on Kusama and report any bugs to Parity. Below is a direct quote from Fredrik, CTO at Parity. CTO stands for chief technology officer, the senior technical role at a company.

We are thankful to Mudit for reporting this to us. We created Kusama for this specific purpose of testing the underlying economic assumptions before they reach Polkadot and are happy that it worked in this case. We encourage everyone to continue hunting bugs on Kusama and reporting them to us!

Fredrik Harrysson, CTO at Parity

Glossary

  • Blockchain: a shared record book kept in copies by many computers, where entries stay permanently once written.
  • Block: a batch of transactions added to the chain at one time.
  • Transaction: an instruction sent to a blockchain network, for example to store data or move funds.
  • Extrinsic: the term Substrate uses for a transaction, an instruction coming from outside the chain's own code.
  • Substrate: Parity Technologies' framework for building blockchains.
  • FRAME: the runtime module system of Substrate, the part that defines how a chain behaves, including fee calculation.
  • Runtime: the set of rules a chain follows, which can be upgraded across the network.
  • Runtime upgrade: a change to those rules, deployed so that every node applies the new version.
  • Fee: the amount charged to send a transaction.
  • base_fee: a fixed fee every transaction pays, whatever it contains.
  • len_fee: the part of the fee that grows with the size of the transaction's data.
  • weight_fee: the part of the fee that grows with the computing work a transaction requires.
  • targeted_fee_adjustment: a multiplier applied to fees that rises when recent blocks are full and falls when they are empty.
  • TransactionByteFee: a constant meant to add a charge for every byte of data in a transaction.
  • Byte: a single unit of data, roughly one character of text.
  • Payload: the data a transaction carries along with it.
  • Remark: a built-in Substrate instruction that stores arbitrary data on the chain.
  • System frame: the built-in set of basic instructions present on every Substrate chain.
  • Polkadot UI: a website for building and sending transactions to a Substrate-based chain from a browser.
  • Kusama: a live test network used to try changes before they reach Polkadot.
  • Polkadot: the main network in this ecosystem.
  • Polymesh Aldebaran testnet: a test network named in the report as one of the places the fix was deployed.
  • W3F: the Web3 Foundation, which publishes research describing how these fees are meant to work.
  • Gas: Ethereum's unit for measuring computing work, used in the report as a comparison for base_fee.
  • Vulnerability: a flaw in software that lets someone make it behave in a way its authors did not intend.
  • Exploit: using such a flaw to get an unintended result.
  • Bug bounty: a reward a company pays to someone who reports a security problem to them.
  • CTO: chief technology officer, the senior technical role at a company.
  • Testnet: a network used for trying things out rather than for real use.

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.