[web3] Stacks DoS Bugfix Review. This is a postmortem of a vulnerability… | by Immunefi | Feb, 2024 | Medium
A flaw in the Clarity virtual machine caused an unhandled exception when a contract was absent, which crashed Stacks client nodes. An attacker could send a transaction containing a malicious smart contract to disrupt the virtual machine.
medium.com
Published on medium.com
Immunefi Editor
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.
Context
Stacks is a layer 2 for Bitcoin that enables smart contracts and decentralised applications using Bitcoin's secure base layer. Central to the Stacks ecosystem is the smart-contract programming language Clarity, which uses a LISP-like syntax. Unlike compiled languages, Clarity relies on interpretation for execution, which the report frames as adding a layer of security to smart contracts.
Stacks also employs Proof of Transfer (PoX) mining, anchoring its security to the Bitcoin blockchain. Leader elections within the Stacks network occur on the Bitcoin blockchain, with Stacks miners subsequently generating new blocks on a separate Stacks blockchain.
Within the Stacks network, nodes execute smart contracts and process user transactions. The vulnerability was in the Clarity virtual machine, which runs contract code on those nodes.

Setup
On 17 November 2023, an independent researcher who goes by the pseudonym Catchme reported a Critical vulnerability to Immunefi. The target was Stacks. A bounty of $76,011 was paid for the finding.
The report does not describe any accounts, tooling, node versions or other preconditions used by the researcher. The only precondition it states is on the attacker's side: to disrupt the virtual machine, an attacker would need to send a transaction containing a malicious smart contract to the network.

Finding it
The vulnerability Catchme identified stemmed from a flaw in the Clarity virtual machine, leading to an unhandled exception. That unhandled exception in turn triggered the client node of Stacks to crash.
In other words, the observable symptom was a node crash caused by contract code that the virtual machine failed to handle gracefully. The report does not describe how the researcher arrived at the flaw, what he was testing when he found it, or any intermediate observations.

Exploitation
Step 1: submit a malicious contract
The attack path is a single step as described: send a transaction containing a malicious smart contract to the network. The contract is crafted to trigger the flaw in the Clarity virtual machine.
Step 2: the node crashes
When the unhandled exception is raised, the Stacks client node crashes. Because nodes are the components that execute smart contracts and process user transactions, crashing them disrupts that processing.
The report does not include the contract source, the transaction, any request or response, or the specific function involved. It also does not state how many nodes are affected by a single transaction or whether the crash is persistent.
Root cause
The flaw sits in the Clarity virtual machine and manifests as an unhandled exception. The fix description points at the mechanism: a function relied on .expect() in a case where a contract is absent, and the correct behaviour is to return a handled error instead.
Impact
The vulnerability was rated Critical. An attacker able to submit a transaction containing a malicious smart contract could crash a Stacks client node, disrupting the virtual machine and the processing of user transactions. The report does not quantify how many nodes could be affected, whether the crash is repeatable, or what recovery requires.
Fix
To mitigate the issue, the function must be modified so that it returns a handled error in cases where a contract is absent, without relying on .expect(). The report states this as the required change and credits the Stacks team with addressing and patching the reported issue.

Variants to test
- Absent-contract lookups in other interpreters. Look for contract or module resolution paths where a missing entry is unwrapped rather than returned as an error. A positive result is a node or process crash, or a panic in logs, triggered by a transaction that references a contract which does not exist.
.expect()andunwrap()on user-influenced values. Grep the client for these calls on values derived from transactions or contract code. A positive result is a panic reachable from a submitted transaction rather than from local configuration.- Malformed contract submissions. Submit contracts that are syntactically valid but reference missing or inconsistent definitions. A positive result is an unhandled exception rather than a structured error response.
- Error-path coverage in the VM. For each error the interpreter can produce, check whether it is converted into a handled result before reaching the node's transaction processing. A positive result is any error that escapes as a process-level failure.
What happened, in plain words
A security researcher found a way to crash part of a cryptocurrency network called Stacks by sending it a specially crafted piece of code. The network runs small programs called smart contracts, and the component that runs those programs had a bug: when it hit a certain situation, instead of reporting a normal error it blew up in a way that took the whole program down. A bounty of $76,011 was paid for the finding.
This article walks through what Stacks is, what the bug was, how it could be triggered, and how it was fixed, explaining each technical idea from scratch along the way.
Context
What Stacks is
Bitcoin is the original cryptocurrency network. It is very good at one thing: keeping a secure, tamper-proof record of who owns what. It was not designed to run complex programs.
Stacks is what people call a layer 2 for Bitcoin. Think of Bitcoin as a very secure but slow bank vault, and Stacks as a busy office built on top of it that uses the vault for safety. Stacks lets people build smart contracts and decentralised applications (apps that run on a network of computers rather than on one company's server) while still leaning on Bitcoin's security.
A smart contract is a small program stored on a blockchain that runs automatically when certain conditions are met. A simple everyday comparison: a vending machine. You put in money, press a button, and it dispenses a snack according to fixed rules. Nobody has to be standing there to make it work. A smart contract is the digital equivalent: code that executes agreed rules without a middleman.
Clarity, the language
Central to the Stacks ecosystem is the smart-contract programming language Clarity, which uses a LISP-like syntax. LISP is one of the oldest programming language families; its hallmark is that everything is written inside parentheses, like (do-something (with-this value)). You do not need to read Clarity to follow this article, but it helps to know it looks unusual compared with languages like Python or JavaScript.
Unlike compiled languages, Clarity relies on interpretation for execution. Here is the difference:
- A compiled language is like translating a whole book into another language before anyone reads it. A program called a compiler turns the entire source code into machine instructions up front.
- An interpreted language is like a live interpreter at a meeting: the code is read and acted on line by line, as it runs.
The report frames this interpretation as adding a layer of security to smart contracts. The idea is that the code is checked and executed step by step rather than being turned into a blob of machine instructions first.

Proof of Transfer mining
Stacks also employs Proof of Transfer (PoX) mining, which anchors its security to the Bitcoin blockchain. To unpack that:
- Mining is the process by which new blocks (batches of transactions) are added to a blockchain. In Bitcoin, miners compete to solve a mathematical puzzle.
- Anchoring means tying one system's records to another, more established system so that the second one vouches for the first. Think of a notary stamping a document: the document stands on its own, but the stamp makes it much harder to tamper with.
In Stacks, leader elections within the network occur on the Bitcoin blockchain, and Stacks miners subsequently generate new blocks on a separate Stacks blockchain. In plain terms: the choice of who gets to produce the next batch of Stacks blocks is recorded on Bitcoin, and then that chosen miner writes the actual Stacks blocks on the Stacks chain. This borrows Bitcoin's security without putting all of Stacks' activity directly on Bitcoin.
Nodes and the virtual machine
Within the Stacks network, nodes execute smart contracts and process user transactions. A node is simply a computer running the Stacks software; together, many nodes form the network. If you have ever used a peer-to-peer file-sharing program, you have seen the same idea: many independent computers each holding and processing the same data.
The vulnerability was in the Clarity virtual machine, which runs contract code on those nodes. A virtual machine here is not a whole simulated computer; it is a controlled environment inside the node software where contract code is executed. The comparison that usually helps: a fenced play area. The code runs inside the fence, and the fence is supposed to keep anything going wrong inside from affecting the rest of the building.

Setup
On 17 November 2023, an independent researcher who goes by the pseudonym Catchme reported a Critical vulnerability to Immunefi. Immunefi is a bug bounty platform: a website where companies offer rewards to security researchers who find and privately report flaws in their software. A bug bounty is the arrangement itself: find a bug, report it responsibly, get paid.
A vulnerability is a weakness in software that someone could abuse to make it do something it should not. Critical is the severity label the platform uses for the most serious kind: flaws that could cause major damage or take a system down.
The target was Stacks. Paying a reward is the incentive that makes researchers report a flaw privately rather than sell it or exploit it.
The report does not describe any accounts, tooling, node versions or other preconditions used by the researcher. The only precondition it states is on the attacker's side: to disrupt the virtual machine, an attacker would need to send a transaction containing a malicious smart contract to the network.
A transaction is a message submitted to a blockchain asking it to do something, such as transfer funds or, in this case, deploy or call a contract. A malicious smart contract is simply a contract written with the intent to cause harm, in this case to trip the bug.

Finding it
The vulnerability Catchme identified stemmed from a flaw in the Clarity virtual machine, leading to an unhandled exception. That unhandled exception in turn triggered the client node of Stacks to crash.
Two terms need unpacking here:
- An exception is a program's way of saying "something went wrong here". Well-written software catches exceptions and turns them into a sensible error message, the way a shop assistant says "sorry, we're out of stock" instead of screaming and closing the shop.
- An unhandled exception is one that nobody catches. It travels up through the program until it reaches the top, and the program stops. That is the crash.
So the observable symptom was a node crash caused by contract code that the virtual machine failed to handle gracefully. The report does not describe how the researcher arrived at the flaw, what he was testing when he found it, or any intermediate observations.

Exploitation
Exploitation means actually using a vulnerability to cause the effect it allows. Here the effect is a crash, which is a form of denial of service (DoS): making a service unavailable to the people who need it, without necessarily stealing anything.
Step 1: submit a malicious contract
The attack path is a single step as described: send a transaction containing a malicious smart contract to the network. The contract is crafted to trigger the flaw in the Clarity virtual machine.
Step 2: the node crashes
When the unhandled exception is raised, the Stacks client node crashes. Because nodes are the components that execute smart contracts and process user transactions, crashing them disrupts that processing.
Think of a receptionist who is the only person able to accept deliveries. If a particular kind of parcel makes the receptionist walk out and lock the door, then nobody's parcels get through, not just the attacker's.
The report does not include the contract source, the transaction, any request or response, or the specific function involved. It also does not state how many nodes are affected by a single transaction or whether the crash is persistent.
Root cause
A root cause is the underlying reason a bug exists, as opposed to the symptom it produces. The symptom here is a crash; the root cause is a specific mistake in the code.
The flaw sits in the Clarity virtual machine and manifests as an unhandled exception. The fix description points at the mechanism: a function relied on .expect() in a case where a contract is absent, and the correct behaviour is to return a handled error instead.
To understand that, you need a little about the Rust programming language:
- In Rust, many operations return a value that is either a success or a failure. The failure case is not thrown automatically; the programmer has to decide what to do with it.
.expect()is a shortcut that says "this should always succeed; if it does not, stop the program immediately and print a message". When the wrapped value is an error orNone(Rust's way of saying "no value here"),.expect()panics.- A panic is Rust's version of an unrecoverable error. It unwinds and terminates the running program unless something catches it.
The everyday comparison: a courier with a rule that says "if the recipient is not home, abandon the entire delivery round and go home". Most of the time the recipient is home, so the rule never bites. But on the day a contract is absent, the courier follows the rule literally and the whole round stops. The correct rule would be "if the recipient is not home, leave a note and carry on".
In other words, the fence around the play area had a gap, and the problem escaped through it.
Impact
Impact is what an attacker could actually achieve by using the flaw. The vulnerability was rated Critical. An attacker able to submit a transaction containing a malicious smart contract could crash a Stacks client node, disrupting the virtual machine and the processing of user transactions.
The report does not quantify how many nodes could be affected, whether the crash is repeatable, or what recovery requires.
Fix
A fix or patch is the change to the software that removes the vulnerability. To mitigate the issue, the function must be modified so that it returns a handled error in cases where a contract is absent, without relying on .expect(). In plain words: instead of the courier abandoning the round when nobody is home, the code should notice the missing contract, report it as an ordinary error, and let the node carry on running.
The report states this as the required change and credits the Stacks team with addressing and patching the reported issue.

Variants to test
A variant is the same class of mistake appearing somewhere else in the code. Once you know one place where a missing value was handled badly, it is worth looking for siblings.
- Absent-contract lookups in other interpreters. Look for contract or module resolution paths where a missing entry is unwrapped rather than returned as an error. A positive result is a node or process crash, or a panic in logs, triggered by a transaction that references a contract which does not exist.
.expect()andunwrap()on user-influenced values. Grep the client for these calls on values derived from transactions or contract code. A positive result is a panic reachable from a submitted transaction rather than from local configuration. (unwrap()is a sibling of.expect(): it also stops the program on failure, just without a custom message.)- Malformed contract submissions. Submit contracts that are syntactically valid but reference missing or inconsistent definitions. A positive result is an unhandled exception rather than a structured error response.
- Error-path coverage in the VM. For each error the interpreter can produce, check whether it is converted into a handled result before reaching the node's transaction processing. A positive result is any error that escapes as a process-level failure.
The common thread: whenever software assumes something will always be present, ask what happens when it is not, and whether the answer is a tidy error or a crash.
Glossary
- Layer 2: a system built on top of a base blockchain (here Bitcoin) that adds features while relying on the base chain for security.
- Smart contract: a small program stored on a blockchain that runs automatically according to fixed rules.
- Decentralised application: an application that runs across a network of computers rather than on a single company's server.
- Clarity: the smart-contract programming language used by Stacks, with a LISP-like syntax.
- LISP-like syntax: a style of programming where expressions are written inside parentheses, in the tradition of the LISP language family.
- Compiled language: a language whose entire source code is translated into machine instructions before it runs.
- Interpreted language: a language whose code is read and acted on step by step while it runs.
- Proof of Transfer (PoX): Stacks' mining method, which records leader elections on the Bitcoin blockchain and then produces Stacks blocks on the Stacks chain.
- Mining: the process by which new blocks of transactions are added to a blockchain.
- Anchoring: tying one system's records to another, more established system so the second vouches for the first.
- Node: a computer running the network's software and taking part in processing its data.
- Virtual machine: a controlled environment inside a program where other code is executed, kept separate from the rest of the software.
- Immunefi: a bug bounty platform where companies reward researchers for privately reporting flaws.
- Bug bounty: an arrangement in which a company pays a reward for responsibly disclosed vulnerabilities.
- Vulnerability: a weakness in software that could be abused to make it behave in unintended ways.
- Critical: the most serious severity rating, used for flaws that could cause major damage or take a system down.
- Transaction: a message submitted to a blockchain asking it to perform an action, such as transferring funds or deploying a contract.
- Malicious smart contract: a contract written with the intent to cause harm, such as triggering a bug.
- Exception: a program's signal that something went wrong during execution.
- Unhandled exception: an exception that no part of the program catches, causing the program to stop.
- Crash: the sudden stop of a running program.
- Exploitation: actually using a vulnerability to produce the effect it allows.
- Denial of service (DoS): making a service unavailable to its legitimate users.
- Root cause: the underlying reason a bug exists, as opposed to the symptom it produces.
- Rust: a programming language whose
.expect()shortcut stops the program on failure. .expect(): a Rust shortcut that stops the program with a message if the value it is applied to is an error or absent.unwrap(): a Rust shortcut similar to.expect()that stops the program on failure, without a custom message.None: Rust's way of representing the absence of a value.- Panic: Rust's unrecoverable error, which terminates the running program unless caught.
- Impact: what an attacker could actually achieve by using a flaw.
- Fix / patch: the change to software that removes a vulnerability.
- Variant: the same class of mistake occurring elsewhere in the code.
- Grep: a command-line tool for searching text, commonly used to find every occurrence of a pattern in source code.
- Error path: the route code takes when something goes wrong, as opposed to the normal success route.