Skip to content

Commit 56df206

Browse files
committed
limit stackDepth for old (deprecated) Json::Reader too
This is an improper solution. If multiple Readers exist, then the effect stackLimit is reduced because of side-effects. But our options are limited. We need to address the security hole without breaking binary-compatibility. However, this is not likely to cause any practical problems because: * Anyone using `operator>>(istream, Json::Value)` will be using the new code already * Multiple Readers are uncommon. * The stackLimit is quite high. * Deeply nested JSON probably would have hit the system limits anyway.
1 parent 4dca80d commit 56df206

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/lib_json/json_reader.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
#pragma warning(disable : 4996)
2929
#endif
3030

31+
static int const stackLimit_g = 1000;
32+
static int stackDepth_g = 0; // see readValue()
33+
3134
namespace Json {
3235

3336
#if __cplusplus >= 201103L
@@ -118,6 +121,7 @@ bool Reader::parse(const char* beginDoc,
118121
nodes_.pop();
119122
nodes_.push(&root);
120123

124+
stackDepth_g = 0; // Yes, this is bad coding, but options are limited.
121125
bool successful = readValue();
122126
Token token;
123127
skipCommentTokens(token);
@@ -140,6 +144,13 @@ bool Reader::parse(const char* beginDoc,
140144
}
141145

142146
bool Reader::readValue() {
147+
// This is a non-reentrant way to support a stackLimit. Terrible!
148+
// But this deprecated class has a security problem: Bad input can
149+
// cause a seg-fault. This seems like a fair, binary-compatible way
150+
// to prevent the problem.
151+
if (stackDepth_g >= stackLimit_g) throw std::runtime_error("Exceeded stackLimit in readValue().");
152+
++stackDepth_g;
153+
143154
Token token;
144155
skipCommentTokens(token);
145156
bool successful = true;
@@ -211,6 +222,7 @@ bool Reader::readValue() {
211222
lastValue_ = &currentValue();
212223
}
213224

225+
--stackDepth_g;
214226
return successful;
215227
}
216228

0 commit comments

Comments
 (0)