diff options
| author | mtg0001 <106211915+mtg0001@users.noreply.github.com> | 2026-07-31 16:05:35 -0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-07-31 16:05:35 -0300 |
| commit | 59777ee34a5ab6297684b7ab8d5d93facd4ad6f3 (patch) | |
| tree | 2a471982436dd64eec993ecaad97c7baeae1286a /src/rpc.cpp | |
| parent | e2d5c1bbe01370dbf75e2118d697a159c4ee582b (diff) | |
net: bound what a peer can make the node allocate (#73)
Three independent bounds on what an untrusted peer can make this node allocate:
- serialize.h: reject a compact size above MAX_SIZE before reading a string or a
vector, instead of discovering it read by read. MAX_SIZE moves from main.h to
serialize.h, where the checks live; headers_core.h includes serialize.h first,
so every existing user still sees it.
- main.cpp: cap an orphan transaction at 10,000 inputs. Orphans are stored and
never validated, so mapOrphanTransactionsByPrev grew with one entry per input
of every orphan a peer cared to send.
- rpc.cpp: drop a Stratum line nested deeper than 20 before json::parse recurses
into it. The scan tracks string state, so a worker name containing { or [ is
no longer mistaken for nesting.
Verified: builds clean on UCRT64; the depth scanner passes a table of legitimate
Stratum lines and nesting attacks; a node built from this branch synced the chain
from genesis to 4462 with no deserialization failure and no rejected block.
Diffstat (limited to 'src/rpc.cpp')
| -rw-r--r-- | src/rpc.cpp | 27 |
1 files changed, 26 insertions, 1 deletions
diff --git a/src/rpc.cpp b/src/rpc.cpp index 285f11b..3db8b94 100644 --- a/src/rpc.cpp +++ b/src/rpc.cpp | |||
| @@ -681,7 +681,32 @@ static bool HandleLine(Miner* m, const std::string& rawLine, | |||
| 681 | int& blocksFoundThisSession) | 681 | int& blocksFoundThisSession) |
| 682 | { | 682 | { |
| 683 | json req; | 683 | json req; |
| 684 | try { req = json::parse(rawLine); } | 684 | try { |
| 685 | // Fast pre-parse depth check to prevent stack overflow in json::parse. | ||
| 686 | // Legitimate Stratum requests have a depth of at most 4. | ||
| 687 | int depth = 0; | ||
| 688 | bool in_string = false; | ||
| 689 | bool escape = false; | ||
| 690 | for (char c : rawLine) { | ||
| 691 | if (in_string) { | ||
| 692 | if (escape) escape = false; | ||
| 693 | else if (c == '\\') escape = true; | ||
| 694 | else if (c == '"') in_string = false; | ||
| 695 | } else { | ||
| 696 | if (c == '"') in_string = true; | ||
| 697 | else if (c == '{' || c == '[') { | ||
| 698 | if (++depth > 20) { | ||
| 699 | LogPrint("worker", "[worker] JSON too deeply nested from %s\n", | ||
| 700 | m->address.empty() ? "(unauth)" : m->address.c_str()); | ||
| 701 | return true; | ||
| 702 | } | ||
| 703 | } else if (c == '}' || c == ']') { | ||
| 704 | depth--; | ||
| 705 | } | ||
| 706 | } | ||
| 707 | } | ||
| 708 | req = json::parse(rawLine); | ||
| 709 | } | ||
| 685 | catch (...) { | 710 | catch (...) { |
| 686 | LogPrint("worker", "[worker] JSON parse error from %s: %.80s\n", | 711 | LogPrint("worker", "[worker] JSON parse error from %s: %.80s\n", |
| 687 | m->address.empty() ? "(unauth)" : m->address.c_str(), | 712 | m->address.empty() ? "(unauth)" : m->address.c_str(), |