Skip to content

Secure allocator #412

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
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
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
*.tlog/
*.pdb

# eclipse project files
.project
.cproject
/.settings/

# CMake-generated files:
CMakeFiles/
CTestTestFile.cmake
Expand Down
92 changes: 92 additions & 0 deletions include/json/allocator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Copyright 2015 Baptiste Lepilleur
// Distributed under MIT license, or public domain if desired and
// recognized in your jurisdiction.
// See file LICENSE for detail or copy at https://raw.githubusercontent.com/open-source-parsers/jsoncpp/master/LICENSE
#ifndef CPPTL_JSON_ALLOCATOR_H_INCLUDED
#define CPPTL_JSON_ALLOCATOR_H_INCLUDED

#include <algorithm> //std::filln
#include <cstring> // std::memset
#include <cstddef> // std::size_t, std::ptrdiff_t
#include <utility> // std::forward

#if !defined(JSON_IS_AMALGAMATION)
#include "config.h"
#endif // if !defined(JSON_IS_AMALGAMATION)

namespace Json {

/**
* This class is used for de-allocating memory securely, allocation happens
* in the normal manner however it's important that de-allocation explicitly
* overwrites the memory with 0s to ensure the memory cannot be "sniffed"
*/
template<typename T>
class JSON_API SecureAllocator {
public:
// Type definitions
using value_type = T;
using pointer = T*;
using const_pointer = const T*;
using reference = T&;
using const_reference = const T&;
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;

/**
* Allocate memory for N items using the standard allocator.
*/
pointer allocate(size_type n) {
// allocate using "global operator new"
return static_cast<pointer>(::operator new(n * sizeof(T)));
}

/**
* Release memory which was allocated for N items at pointer P.
*
* The memory block is filled with zeroes before being released.
* The pointer argument is tagged as "volatile" to prevent the
* compiler optimizing out this critical step.
*/
void deallocate(volatile pointer p, size_type n) {
std::fill_n((volatile char*)p, n * sizeof(value_type), 0);
// free using "global operator delete"
::operator delete(p);
}

/**
* Construct an item in-place at pointer P.
*/
template<typename... Args>
void construct(pointer p, Args&&... args) {
// construct using "placement new" and "perfect forwarding"
::new (static_cast<void*>(p)) T(std::forward<Args>(args)...);
}

/**
* Destroy an item in-place at pointer P.
*/
void destroy(pointer p) {
// destroy using "explicit destructor"
p->~T();
}

// Boilerplate
SecureAllocator() {}
template<typename U> SecureAllocator(const SecureAllocator<U>&) {}
template<typename U> struct rebind { using other = SecureAllocator<U>; };
};

template<typename T, typename U>
bool operator==(const SecureAllocator<T>&, const SecureAllocator<U>&) {
return true;
}

template<typename T, typename U>
bool operator!=(const SecureAllocator<T>&, const SecureAllocator<U>&) {
return false;
}

} //end namespace Json

#endif // CPPTL_JSON_ALLOCATOR_H_INCLUDED
7 changes: 7 additions & 0 deletions include/json/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@
#define JSONCPP_DEPRECATED(message)
#endif // if !defined(JSONCPP_DEPRECATED)

//We may want to have different presets for the default allocator and
//string
//#define JSONCPP_DEFAULT_CHAR_TRAITS = std::char_traits<char>
//#define JSONCPP_DEFAULT_ALLOCATOR = std::allocator<char>
#define JSONCPP_DEFAULT_CHAR_TRAITS
#define JSONCPP_DEFAULT_ALLOCATOR

namespace Json {
typedef int Int;
typedef unsigned int UInt;
Expand Down
11 changes: 10 additions & 1 deletion include/json/forwards.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@
namespace Json {

// writer.h
template<typename _Traits, typename _Alloc>
class FastWriter;
template<typename _Traits, typename _Alloc>
class StyledWriter;

// reader.h
// reader_declaration.h
template<typename _Traits, typename _Alloc>
class Reader;

// features.h
Expand All @@ -25,11 +28,17 @@ class Features;
// value.h
typedef unsigned int ArrayIndex;
class StaticString;
template<typename _Traits, typename _Alloc>
class Path;
template<typename _Traits, typename _Alloc>
class PathArgument;
template<typename _Traits, typename _Alloc>
class Value;
template<typename _Traits, typename _Alloc>
class ValueIteratorBase;
template<typename _Traits, typename _Alloc>
class ValueIterator;
template<typename _Traits, typename _Alloc>
class ValueConstIterator;

} // namespace Json
Expand Down
1 change: 1 addition & 0 deletions include/json/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#ifndef JSON_JSON_H_INCLUDED
#define JSON_JSON_H_INCLUDED

#include "allocator.h"
#include "autolink.h"
#include "value.h"
#include "reader.h"
Expand Down
Loading