Node.js crash on counterMap overflow
The counterMap in the NestedCounters class is never cleared, so an attacker can send many requests with unique second parameters to grow it indefinitely, eventually crashing Node.js. This can disable nodes and potentially shut down the network.
- Disclosed after under an hour
#36029 [BC-Insight] Node.js crash on counterMap overflow
Submitted on Oct 16th 2024 at 10:24:53 UTC by @dldLambda for Audit Comp | Shardeum: Core II
- Report ID: #36029
- Report Type: Blockchain/DLT
- Report severity: Insight
- Target: https://github.com/shardeum/shardus-core/tree/dev
- Impacts:
- Network not being able to confirm new transactions (total network shutdown)
- Shutdown of greater than or equal to 30% of network processing nodes without brute force actions, but does not shut down the network
Description
Brief/Intro
Sending a large number of requests will cause the "counterMap" overflow and Node.js crash.
Vulnerability Details
In "sendRequests" function, the "countEvent" function is called with the second parameter, which can controlled from the request.
If the "counterMap" does not yet have such a key, then a new key-value pair is created and added to "counterMap". «counterMap» is a global Map that is filled but not cleared.
It’s overflow will lead to Node.js crash.
https://github.com/shardeum/shardus-core/blob/dev/src/p2p/ServiceQueue.ts#L387 — link to "sendRequests" function.
Next, a gossip request is formed and the second parameter is a string "gossip send - ${add.hash}" , that includes the hash of the original request parameter (https://github.com/shardeum/shardus-core/blob/dev/src/p2p/ServiceQueue.ts#L397).
And this is how counterMap is created and "countEvent" is declared (https://github.com/shardeum/shardus-core/blob/dev/src/utils/nestedCounters.ts#L96):
``` class NestedCounters { constructor() { this.eventCounters = new Map() // <--- this.rareEventCounters = new Map() this.crypto = null this.infLoopDebug = false } //some lines... countEvent(category1: string, category2: string, count = 1): void { let counterMap: CounterMap = this.eventCounters // <---
let nextNode: CounterNode = null
if (counterMap.has(category1) === false) {
nextNode = { count: 0, subCounters: new Map() }
counterMap.set(category1, nextNode)
} else {
nextNode = counterMap.get(category1)
}
nextNode.count += count
counterMap = nextNode.subCounters
//unrolled loop to avoid memory alloc
category1 = category2
if (counterMap.has(category1) === false) {
nextNode = { count: 0, subCounters: new Map() }
counterMap.set(category1, nextNode)
} else {
nextNode = counterMap.get(category1)
}
nextNode.count += count
counterMap = nextNode.subCounters
} //some lines... } ``` It is noticeable that if, when checking the uniqueness of the received second parameter, this key is not found, then a new key-value pair is added, but counterMap is not cleared, which can lead to overflow.
Impact Details
An attacker will be able to send a huge number of requests, overflow the counterMap and therefore Node.js will fail.
Proof of Concept
Proof of Concept
- Let’s check that we can get there using a request. In your local copy of Shardeum, open the file ./src/index.ts and add the following function at line #1139 ( https://gist.github.com/dldLambda/335024ad4787abcced5f5550aed0da31 — in gistfile1.txt)
Is ./src/index.ts you need to add "import * as Comms from '@shardus/core/dist/p2p/Comms' " and "import { nodeListFromStates } from './Join' ".
Then rebuild the project using "npm run prepare".
Now, you can send a request with the parameters you selected and make sure that your data is received:
``` fetch(`http://193.108.115.45:9001/dl\\_func\\`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({"cycle":13,"hash":"1","priority":0,"sign":{"owner":"2","sig":"3"},"subQueueKey":"4","txData":{"nodeId":"5","publicKey":"6","sign":{"owner":"7","sig":"8"},"startTime":1729004605},"type":"nodeInitReward" })}).then(response => { if (!response.ok) { throw new Error('Network response was not ok'); } return response.json(); }).then(data => { console.log('Success:', data); }).catch(error => { console.error('Error:', error); })
``` The data in the request can be quite arbitrary. Then in the ./Shardeum do the following command "grep -r "hoho55" . "
You will see in the out.log after the line "hoho55" the body of your request.
- Example
First, set a memory limit using the command "ulimit -Sv 1000000". Next, run the following script with "npx tsc "insert filename" " (run "npm install -g npx" if you need).
https://gist.github.com/dldLambda/e60f585401f01e77b8d7338bb0431edd — link to example script.
Firstly, you will see that the size of the counterMap grows indefinitely, and secondly, the Node.js soon fail. Thus, an attacker can disable nodes.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the counterMap is a global Map that is populated with attacker-controlled keys but never cleared, leading to unbounded memory growth.
Impact
An attacker could send a huge number of requests to overflow the counterMap and crash Node.js, potentially disabling nodes and shutting down the network.
What the attacker needed
Ability to send requests to the node; local Shardeum instance for testing
How it was found
- Add a function to the local Shardeum index.ts file to expose a request handler.
- Rebuild the project using npm run prepare.
- Send a crafted POST request to the exposed endpoint with arbitrary data.
- Verify the request body is logged by searching for a marker string in the output log.
- Set a memory limit with ulimit -Sv 1000000 and run a script that sends many requests with unique parameters.
- Observe the counterMap growing indefinitely and Node.js eventually crashing.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
A counter map is a data structure that keeps track of how many times different events happen. In this code, it is never emptied, so if an attacker sends many requests with different values, the map keeps growing until the program runs out of memory and crashes. This means an attacker could take down parts of the network by flooding it with requests.
What is DoS?
A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.