Skip to content

Commit c018d9f

Browse files
committed
Merge pull request #442 from open-source-parsers/JSONCPP_STRING
Secure allocator available for wiping memory. Resolves #437. Resolves #152.
2 parents 59e327f + ae56465 commit c018d9f

File tree

14 files changed

+187
-21
lines changed

14 files changed

+187
-21
lines changed

Diff for: .gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,8 @@ jsoncpp_lib_static.dir/
4545
/src/lib_json/Makefile
4646
/src/test_lib_json/Makefile
4747
/src/test_lib_json/jsoncpp_test
48+
49+
# eclipse project files
50+
.project
51+
.cproject
52+
/.settings/

Diff for: CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ jsoncpp_parse_version( ${JSONCPP_VERSION} JSONCPP_VERSION )
6969
#IF(NOT JSONCPP_VERSION_FOUND)
7070
# MESSAGE(FATAL_ERROR "Failed to parse version string properly. Expect X.Y.Z")
7171
#ENDIF(NOT JSONCPP_VERSION_FOUND)
72+
SET( JSONCPP_USE_SECURE_MEMORY "0" CACHE STRING "-D...=1 to use memory-wiping allocator for STL" )
7273

7374
MESSAGE(STATUS "JsonCpp Version: ${JSONCPP_VERSION_MAJOR}.${JSONCPP_VERSION_MINOR}.${JSONCPP_VERSION_PATCH}")
7475
# File version.h is only regenerated on CMake configure step

Diff for: amalgamate.py

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ def amalgamate_source(source_top_dir=None,
6767
header.add_text("/// to prevent private header inclusion.")
6868
header.add_text("#define JSON_IS_AMALGAMATION")
6969
header.add_file("include/json/version.h")
70+
header.add_file("include/json/allocator.h")
7071
header.add_file("include/json/config.h")
7172
header.add_file("include/json/forwards.h")
7273
header.add_file("include/json/features.h")

Diff for: include/json/allocator.h

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// Copyright 2007-2010 Baptiste Lepilleur
2+
// Distributed under MIT license, or public domain if desired and
3+
// recognized in your jurisdiction.
4+
// See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
5+
6+
#ifndef CPPTL_JSON_ALLOCATOR_H_INCLUDED
7+
#define CPPTL_JSON_ALLOCATOR_H_INCLUDED
8+
9+
#include <cstring>
10+
#include <memory>
11+
12+
namespace Json {
13+
template<typename T>
14+
class SecureAllocator {
15+
public:
16+
// Type definitions
17+
using value_type = T;
18+
using pointer = T*;
19+
using const_pointer = const T*;
20+
using reference = T&;
21+
using const_reference = const T&;
22+
using size_type = std::size_t;
23+
using difference_type = std::ptrdiff_t;
24+
25+
/**
26+
* Allocate memory for N items using the standard allocator.
27+
*/
28+
pointer allocate(size_type n) {
29+
// allocate using "global operator new"
30+
return static_cast<pointer>(::operator new(n * sizeof(T)));
31+
}
32+
33+
/**
34+
* Release memory which was allocated for N items at pointer P.
35+
*
36+
* The memory block is filled with zeroes before being released.
37+
* The pointer argument is tagged as "volatile" to prevent the
38+
* compiler optimizing out this critical step.
39+
*/
40+
void deallocate(volatile pointer p, size_type n) {
41+
std::memset(p, 0, n * sizeof(T));
42+
// free using "global operator delete"
43+
::operator delete(p);
44+
}
45+
46+
/**
47+
* Construct an item in-place at pointer P.
48+
*/
49+
template<typename... Args>
50+
void construct(pointer p, Args&&... args) {
51+
// construct using "placement new" and "perfect forwarding"
52+
::new (static_cast<void*>(p)) T(std::forward<Args>(args)...);
53+
}
54+
55+
size_type max_size() const {
56+
return size_t(-1) / sizeof(T);
57+
}
58+
59+
pointer address( reference x ) const {
60+
return std::addressof(x);
61+
}
62+
63+
const_pointer address( const_reference x ) const {
64+
return std::addressof(x);
65+
}
66+
67+
/**
68+
* Destroy an item in-place at pointer P.
69+
*/
70+
void destroy(pointer p) {
71+
// destroy using "explicit destructor"
72+
p->~T();
73+
}
74+
75+
// Boilerplate
76+
SecureAllocator() {}
77+
template<typename U> SecureAllocator(const SecureAllocator<U>&) {}
78+
template<typename U> struct rebind { using other = SecureAllocator<U>; };
79+
};
80+
81+
82+
template<typename T, typename U>
83+
bool operator==(const SecureAllocator<T>&, const SecureAllocator<U>&) {
84+
return true;
85+
}
86+
87+
template<typename T, typename U>
88+
bool operator!=(const SecureAllocator<T>&, const SecureAllocator<U>&) {
89+
return false;
90+
}
91+
92+
} //namespace Json
93+
94+
#endif // CPPTL_JSON_ALLOCATOR_H_INCLUDED

Diff for: include/json/config.h

+14
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,12 @@
119119
# define JSON_USE_INT64_DOUBLE_CONVERSION 1
120120
#endif
121121

122+
#include "version.h"
123+
124+
#if JSONCPP_USING_SECURE_MEMORY
125+
#include "allocator.h" //typedef Allocator
126+
#endif
127+
122128
namespace Json {
123129
typedef int Int;
124130
typedef unsigned int UInt;
@@ -139,11 +145,19 @@ typedef Int64 LargestInt;
139145
typedef UInt64 LargestUInt;
140146
#define JSON_HAS_INT64
141147
#endif // if defined(JSON_NO_INT64)
148+
#if JSONCPP_USING_SECURE_MEMORY
149+
#define JSONCPP_STRING std::basic_string<char, std::char_traits<char>, Json::SecureAllocator<char> >
150+
#define JSONCPP_OSTRINGSTREAM std::basic_ostringstream<char, std::char_traits<char>, Json::SecureAllocator<char> >
151+
#define JSONCPP_OSTREAM std::basic_ostream<char, std::char_traits<char>>
152+
#define JSONCPP_ISTRINGSTREAM std::basic_istringstream<char, std::char_traits<char>, Json::SecureAllocator<char> >
153+
#define JSONCPP_ISTREAM std::istream
154+
#else
142155
#define JSONCPP_STRING std::string
143156
#define JSONCPP_OSTRINGSTREAM std::ostringstream
144157
#define JSONCPP_OSTREAM std::ostream
145158
#define JSONCPP_ISTRINGSTREAM std::istringstream
146159
#define JSONCPP_ISTREAM std::istream
160+
#endif // if JSONCPP_USING_SECURE_MEMORY
147161
} // end namespace Json
148162

149163
#endif // JSON_CONFIG_H_INCLUDED

Diff for: include/json/value.h

+3
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,9 @@ Json::Value obj_value(Json::objectValue); // {}
336336
int compare(const Value& other) const;
337337

338338
const char* asCString() const; ///< Embedded zeroes could cause you trouble!
339+
#if JSONCPP_USING_SECURE_MEMORY
340+
unsigned getCStringLength() const; //Allows you to understand the length of the CString
341+
#endif
339342
JSONCPP_STRING asString() const; ///< Embedded zeroes are possible.
340343
/** Get raw char* of string-value.
341344
* \return false if !string. (Seg-fault if str or end are NULL.)

Diff for: include/json/version.h

+7
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,11 @@
1010
# define JSONCPP_VERSION_QUALIFIER
1111
# define JSONCPP_VERSION_HEXA ((JSONCPP_VERSION_MAJOR << 24) | (JSONCPP_VERSION_MINOR << 16) | (JSONCPP_VERSION_PATCH << 8))
1212

13+
#ifdef JSONCPP_USING_SECURE_MEMORY
14+
#undef JSONCPP_USING_SECURE_MEMORY
15+
#endif
16+
#define JSONCPP_USING_SECURE_MEMORY 1
17+
// If non-zero, the library zeroes any memory that it has allocated before
18+
// it frees its memory.
19+
1320
#endif // JSON_VERSION_H_INCLUDED

Diff for: src/jsontestrunner/main.cpp

+1-7
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@ static JSONCPP_STRING normalizeFloatingPointStr(double value) {
3333
#endif
3434
buffer[sizeof(buffer) - 1] = 0;
3535
JSONCPP_STRING s(buffer);
36-
#if JSON_USE_SECURE_MEMORY
37-
memset(&buffer, 0, sizeof(buffer));
38-
#endif
3936
JSONCPP_STRING::size_type index = s.find_last_of("eE");
4037
if (index != JSONCPP_STRING::npos) {
4138
JSONCPP_STRING::size_type hasSign =
@@ -69,9 +66,6 @@ static JSONCPP_STRING readInputTestFile(const char* path) {
6966
if (fread(buffer, 1, usize, file) == usize)
7067
text = buffer;
7168
fclose(file);
72-
#if JSON_USE_SECURE_MEMORY
73-
memset(buffer, 0, static_cast<size_t>(size + 1));
74-
#endif
7569
delete[] buffer;
7670
return text;
7771
}
@@ -151,7 +145,7 @@ static int parseAndSaveValueTree(const JSONCPP_STRING& input,
151145
Json::Value* root)
152146
{
153147
Json::Reader reader(features);
154-
bool parsingSuccessful = reader.parse(input, *root);
148+
bool parsingSuccessful = reader.parse(input.data(), input.data() + input.size(), *root);
155149
if (!parsingSuccessful) {
156150
printf("Failed to parse %s file: \n%s\n",
157151
kind.c_str(),

Diff for: src/lib_json/json_reader.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ Reader::Reader(const Features& features)
9898

9999
bool
100100
Reader::parse(const std::string& document, Value& root, bool collectComments) {
101-
document_ = document;
101+
JSONCPP_STRING documentCopy(document.data(), document.data() + document.capacity());
102+
std::swap(documentCopy, document_);
102103
const char* begin = document_.c_str();
103104
const char* end = begin + document_.length();
104105
return parse(begin, end, root, collectComments);
@@ -114,7 +115,7 @@ bool Reader::parse(std::istream& sin, Value& root, bool collectComments) {
114115
// create an extra copy.
115116
JSONCPP_STRING doc;
116117
std::getline(sin, doc, (char)EOF);
117-
return parse(doc, root, collectComments);
118+
return parse(doc.data(), doc.data() + doc.size(), root, collectComments);
118119
}
119120

120121
bool Reader::parse(const char* beginDoc,

Diff for: src/lib_json/json_value.cpp

+47-10
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,29 @@ inline static void decodePrefixedString(
138138
}
139139
/** Free the string duplicated by duplicateStringValue()/duplicateAndPrefixStringValue().
140140
*/
141-
static inline void releaseStringValue(char* value) { free(value); }
141+
#if JSONCPP_USING_SECURE_MEMORY
142+
static inline void releasePrefixedStringValue(char* value) {
143+
unsigned length = 0;
144+
char const* valueDecoded;
145+
decodePrefixedString(true, value, &length, &valueDecoded);
146+
size_t const size = sizeof(unsigned) + length + 1U;
147+
memset(value, 0, size);
148+
free(value);
149+
}
150+
static inline void releaseStringValue(char* value, unsigned length) {
151+
// length==0 => we allocated the strings memory
152+
size_t size = (length==0) ? strlen(value) : length;
153+
memset(value, 0, size);
154+
free(value);
155+
}
156+
#else // !JSONCPP_USING_SECURE_MEMORY
157+
static inline void releasePrefixedStringValue(char* value) {
158+
free(value);
159+
}
160+
static inline void releaseStringValue(char* value, unsigned length) {
161+
free(value);
162+
}
163+
#endif // JSONCPP_USING_SECURE_MEMORY
142164

143165
} // namespace Json
144166

@@ -193,12 +215,12 @@ Value::CommentInfo::CommentInfo() : comment_(0)
193215

194216
Value::CommentInfo::~CommentInfo() {
195217
if (comment_)
196-
releaseStringValue(comment_);
218+
releaseStringValue(comment_, 0u);
197219
}
198220

199221
void Value::CommentInfo::setComment(const char* text, size_t len) {
200222
if (comment_) {
201-
releaseStringValue(comment_);
223+
releaseStringValue(comment_, 0u);
202224
comment_ = 0;
203225
}
204226
JSON_ASSERT(text != 0);
@@ -229,10 +251,10 @@ Value::CZString::CZString(char const* str, unsigned ulength, DuplicationPolicy a
229251
storage_.length_ = ulength & 0x3FFFFFFF;
230252
}
231253

232-
Value::CZString::CZString(const CZString& other)
233-
: cstr_(other.storage_.policy_ != noDuplication && other.cstr_ != 0
234-
? duplicateStringValue(other.cstr_, other.storage_.length_)
235-
: other.cstr_) {
254+
Value::CZString::CZString(const CZString& other) {
255+
cstr_ = (other.storage_.policy_ != noDuplication && other.cstr_ != 0
256+
? duplicateStringValue(other.cstr_, other.storage_.length_)
257+
: other.cstr_);
236258
storage_.policy_ = static_cast<unsigned>(other.cstr_
237259
? (static_cast<DuplicationPolicy>(other.storage_.policy_) == noDuplication
238260
? noDuplication : duplicate)
@@ -248,8 +270,9 @@ Value::CZString::CZString(CZString&& other)
248270
#endif
249271

250272
Value::CZString::~CZString() {
251-
if (cstr_ && storage_.policy_ == duplicate)
252-
releaseStringValue(const_cast<char*>(cstr_));
273+
if (cstr_ && storage_.policy_ == duplicate) {
274+
releaseStringValue(const_cast<char*>(cstr_), storage_.length_ + 1u); //+1 for null terminating character for sake of completeness but not actually necessary
275+
}
253276
}
254277

255278
void Value::CZString::swap(CZString& other) {
@@ -455,7 +478,7 @@ Value::~Value() {
455478
break;
456479
case stringValue:
457480
if (allocated_)
458-
releaseStringValue(value_.string_);
481+
releasePrefixedStringValue(value_.string_);
459482
break;
460483
case arrayValue:
461484
case objectValue:
@@ -467,6 +490,8 @@ Value::~Value() {
467490

468491
if (comments_)
469492
delete[] comments_;
493+
494+
value_.uint_ = 0;
470495
}
471496

472497
Value& Value::operator=(Value other) {
@@ -611,6 +636,18 @@ const char* Value::asCString() const {
611636
return this_str;
612637
}
613638

639+
#if JSONCPP_USING_SECURE_MEMORY
640+
unsigned Value::getCStringLength() const {
641+
JSON_ASSERT_MESSAGE(type_ == stringValue,
642+
"in Json::Value::asCString(): requires stringValue");
643+
if (value_.string_ == 0) return 0;
644+
unsigned this_len;
645+
char const* this_str;
646+
decodePrefixedString(this->allocated_, this->value_.string_, &this_len, &this_str);
647+
return this_len;
648+
}
649+
#endif
650+
614651
bool Value::getString(char const** str, char const** cend) const {
615652
if (type_ != stringValue) return false;
616653
if (value_.string_ == 0) return false;

Diff for: src/lib_json/version.h.in

+7
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,11 @@
1010
# define JSONCPP_VERSION_QUALIFIER
1111
# define JSONCPP_VERSION_HEXA ((JSONCPP_VERSION_MAJOR << 24) | (JSONCPP_VERSION_MINOR << 16) | (JSONCPP_VERSION_PATCH << 8))
1212

13+
#ifdef JSONCPP_USING_SECURE_MEMORY
14+
#undef JSONCPP_USING_SECURE_MEMORY
15+
#endif
16+
#define JSONCPP_USING_SECURE_MEMORY @JSONCPP_USE_SECURE_MEMORY@
17+
// If non-zero, the library zeroes any memory that it has allocated before
18+
// it frees its memory.
19+
1320
#endif // JSON_VERSION_H_INCLUDED

Diff for: src/test_lib_json/jsontest.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ JSONCPP_STRING ToJsonString(JSONCPP_STRING in) {
434434
return in;
435435
}
436436

437-
#if JSON_USE_SECURE_MEMORY
437+
#if JSONCPP_USING_SECURE_MEMORY
438438
JSONCPP_STRING ToJsonString(std::string in) {
439439
return JSONCPP_STRING(in.data(), in.data() + in.length());
440440
}

Diff for: src/test_lib_json/jsontest.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ TestResult& checkEqual(TestResult& result,
193193

194194
JSONCPP_STRING ToJsonString(const char* toConvert);
195195
JSONCPP_STRING ToJsonString(JSONCPP_STRING in);
196-
#if JSON_USE_SECURE_MEMORY
196+
#if JSONCPP_USING_SECURE_MEMORY
197197
JSONCPP_STRING ToJsonString(std::string in);
198198
#endif
199199

Diff for: travis.sh

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ env | sort
1919

2020
cmake -DJSONCPP_WITH_CMAKE_PACKAGE=$CMAKE_PKG -DBUILD_SHARED_LIBS=$SHARED_LIB -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DCMAKE_VERBOSE_MAKEFILE=$VERBOSE_MAKE .
2121
make
22+
cmake -DJSONCPP_WITH_CMAKE_PACKAGE=$CMAKE_PKG -DBUILD_SHARED_LIBS=$SHARED_LIB -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DCMAKE_VERBOSE_MAKEFILE=$VERBOSE_MAKE -DJSONCPP_USE_SECURE_MEMORY=1 .
23+
make
2224

2325
# Python is not available in Travis for osx.
2426
# https://github.com/travis-ci/travis-ci/issues/2320

0 commit comments

Comments
 (0)