Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 29 additions & 13 deletions src/lib_json/json_value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,20 +133,20 @@ static inline char* duplicateStringValue(const char* value, size_t length) {
/* Record the length as a prefix.
*/
static inline char* duplicateAndPrefixStringValue(const char* value,
unsigned int length) {
size_t length) {
// Avoid an integer overflow in the call to malloc below by limiting length
// to a sane value.
// to a sane value. This also keeps it within the unsigned prefix.
JSON_ASSERT_MESSAGE(length <= static_cast<unsigned>(Value::maxInt) -
sizeof(unsigned) - 1U,
"in Json::Value::duplicateAndPrefixStringValue(): "
"length too big for prefixing");
size_t actualLength = sizeof(length) + length + 1;
size_t actualLength = sizeof(unsigned) + length + 1;
auto newString = static_cast<char*>(malloc(actualLength));
if (newString == nullptr) {
throwRuntimeError("in Json::Value::duplicateAndPrefixStringValue(): "
"Failed to allocate string value buffer");
}
*reinterpret_cast<unsigned*>(newString) = length;
*reinterpret_cast<unsigned*>(newString) = static_cast<unsigned>(length);
memcpy(newString + sizeof(unsigned), value, length);
newString[actualLength - 1U] =
0; // to avoid buffer over-run accidents by users later
Expand Down Expand Up @@ -264,6 +264,11 @@ JSONCPP_NORETURN void throwLogicError(String const& msg) {
// Notes: policy_ indicates if the string was allocated when
// a string is stored.

// A longer key doesn't fit in CZString::StringStorage::length_.
static inline bool isValidKeyLength(size_t length) {
return length < (1U << 30);
}

Value::CZString::CZString(ArrayIndex index) : cstr_(nullptr), index_(index) {}

Value::CZString::CZString(char const* str, unsigned length,
Expand Down Expand Up @@ -456,20 +461,18 @@ Value::Value(const char* value) {
initBasic(stringValue, true);
JSON_ASSERT_MESSAGE(value != nullptr,
"Null Value Passed to Value Constructor");
value_.string_ = duplicateAndPrefixStringValue(
value, static_cast<unsigned>(strlen(value)));
value_.string_ = duplicateAndPrefixStringValue(value, strlen(value));
}

Value::Value(const char* begin, const char* end) {
initBasic(stringValue, true);
value_.string_ =
duplicateAndPrefixStringValue(begin, static_cast<unsigned>(end - begin));
duplicateAndPrefixStringValue(begin, static_cast<size_t>(end - begin));
}

Value::Value(const String& value) {
initBasic(stringValue, true);
value_.string_ = duplicateAndPrefixStringValue(
value.data(), static_cast<unsigned>(value.length()));
value_.string_ = duplicateAndPrefixStringValue(value.data(), value.length());
}

Value::Value(const StaticString& value) {
Expand Down Expand Up @@ -1112,7 +1115,10 @@ Value& Value::resolveReference(const char* key) {
<< valueTypeToString(type()));
if (type() == nullValue)
*this = Value(objectValue);
CZString actualKey(key, static_cast<unsigned>(strlen(key)),
const size_t keyLength = strlen(key);
JSON_ASSERT_MESSAGE(isValidKeyLength(keyLength),
"in Json::Value::resolveReference(): key too long");
CZString actualKey(key, static_cast<unsigned>(keyLength),
CZString::noDuplication); // NOTE!
auto it = value_.map_->lower_bound(actualKey);
if (it != value_.map_->end() && (*it).first == actualKey)
Expand All @@ -1132,7 +1138,11 @@ Value& Value::resolveReference(char const* key, char const* end) {
<< valueTypeToString(type()));
if (type() == nullValue)
*this = Value(objectValue);
CZString actualKey(key, static_cast<unsigned>(end - key),
const size_t keyLength = static_cast<size_t>(end - key);
JSON_ASSERT_MESSAGE(
isValidKeyLength(keyLength),
"in Json::Value::resolveReference(key, end): key too long");
CZString actualKey(key, static_cast<unsigned>(keyLength),
CZString::duplicateOnCopy);
auto it = value_.map_->lower_bound(actualKey);
if (it != value_.map_->end() && (*it).first == actualKey)
Expand All @@ -1157,7 +1167,10 @@ Value const* Value::find(char const* begin, char const* end) const {
"objectValue or nullValue");
if (type() == nullValue)
return nullptr;
CZString actualKey(begin, static_cast<unsigned>(end - begin),
const size_t keyLength = static_cast<size_t>(end - begin);
if (!isValidKeyLength(keyLength))
return nullptr;
CZString actualKey(begin, static_cast<unsigned>(keyLength),
CZString::noDuplication);
ObjectValues::const_iterator it = value_.map_->find(actualKey);
if (it == value_.map_->end())
Expand Down Expand Up @@ -1282,7 +1295,10 @@ bool Value::removeMember(const char* begin, const char* end, Value* removed) {
if (type() != objectValue) {
return false;
}
CZString actualKey(begin, static_cast<unsigned>(end - begin),
const size_t keyLength = static_cast<size_t>(end - begin);
if (!isValidKeyLength(keyLength))
return false;
Comment on lines +1298 to +1300

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Wrong member gets deleted

The new check misses removeMember(const char*). That overload still builds a CZString directly from strlen(key). For a null-terminated key of length 2^30 + 1 starting with "x", CZString stores length 1, so root.removeMember(key) can delete root["x"].

Route that overload through the checked overload while keeping its existing type check.

CZString actualKey(begin, static_cast<unsigned>(keyLength),
CZString::noDuplication);
auto it = value_.map_->find(actualKey);
if (it == value_.map_->end())
Expand Down
33 changes: 33 additions & 0 deletions src/test_lib_json/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#include "jsontest.h"
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
Expand Down Expand Up @@ -2184,6 +2186,37 @@ JSONTEST_FIXTURE_LOCAL(ValueTest, removeMemberWithEmbeddedNull) {
}
}

JSONTEST_FIXTURE_LOCAL(ValueTest, lengthsTooLongToStoreAreNotTruncated) {
#if JSON_USE_EXCEPTION
// Lengths are checked before anything is read, so most of this buffer stays
// untouched and costs address space, not memory.
const std::uint64_t stringLength = (std::uint64_t{1} << 32) + 1;
const size_t keyLength = (size_t{1} << 30) + 1;
if (stringLength > std::numeric_limits<size_t>::max())
return;
std::unique_ptr<char, decltype(&std::free)> buffer(
static_cast<char*>(std::malloc(static_cast<size_t>(stringLength))),
Comment on lines +2195 to +2198

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Key tests silently skip

The early return for stringLength skips the key checks too. On a 32-bit build, the 4 GiB string cannot fit, but the 1 GiB key length can. If the larger allocation fails, the key checks also skip even when a smaller allocation could succeed.

Split the string and key checks so each can run independently.

&std::free);
if (!buffer)
return;
buffer.get()[0] = 'x';
const char* begin = buffer.get();

JSONTEST_ASSERT_THROWS(
Json::Value(begin, begin + static_cast<size_t>(stringLength)));

Json::Value root(Json::objectValue);
JSONTEST_ASSERT_THROWS(root.demand(begin, begin + keyLength));
JSONTEST_ASSERT(root.empty());

// Must not alias the key made of the low bits of the length.
root["x"] = "keep";
JSONTEST_ASSERT(root.find(begin, begin + keyLength) == nullptr);
JSONTEST_ASSERT(!root.removeMember(begin, begin + keyLength, nullptr));
JSONTEST_ASSERT_EQUAL(1U, root.size());
#endif
}

JSONTEST_FIXTURE_LOCAL(ValueTest, removeMemberStringOnNullAndEmptyKey) {
Json::Value root;
root.removeMember("missing");
Expand Down