Skip to content

Commit

Permalink
replace most boost serialization with existing monero serialization
Browse files Browse the repository at this point in the history
This reduces the attack surface for data that can come from
malicious sources (exported output and key images, multisig
transactions...) since the monero serialization is already
exposed to the outside, and the boost lib we were using had
a few known crashers.

For interoperability, a new load-deprecated-formats wallet
setting is added (off by default). This allows loading boost
format data if there is no alternative. It will likely go
at some point, along with the ability to load those.

Notably, the peer lists file still uses the boost serialization
code, as the data it stores is define in epee, while the new
serialization code is in monero, and migrating it was fairly
hairy. Since this file is local and not obtained from anyone
else, the marginal risk is minimal, but it could be migrated
later if needed.

Some tests and tools also do, this will stay as is for now.
  • Loading branch information
moneromooo-monero committed Aug 17, 2020
1 parent 43a4fd9 commit 7175dcb
Show file tree
Hide file tree
Showing 34 changed files with 836 additions and 391 deletions.
2 changes: 0 additions & 2 deletions src/blockchain_utilities/blockchain_blackball.cpp
Expand Up @@ -28,8 +28,6 @@

#include <boost/range/adaptor/transformed.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/archive/portable_binary_iarchive.hpp>
#include <boost/archive/portable_binary_oarchive.hpp>
#include "common/unordered_containers_boost_serialization.h"
#include "common/command_line.h"
#include "common/varint.h"
Expand Down
2 changes: 1 addition & 1 deletion src/cryptonote_basic/cryptonote_basic.h
Expand Up @@ -37,7 +37,7 @@
#include <sstream>
#include <atomic>
#include "serialization/variant.h"
#include "serialization/vector.h"
#include "serialization/containers.h"
#include "serialization/binary_archive.h"
#include "serialization/json_archive.h"
#include "serialization/debug_archive.h"
Expand Down
1 change: 0 additions & 1 deletion src/cryptonote_basic/cryptonote_basic_impl.cpp
Expand Up @@ -34,7 +34,6 @@ using namespace epee;
#include "cryptonote_basic_impl.h"
#include "string_tools.h"
#include "serialization/binary_utils.h"
#include "serialization/container.h"
#include "cryptonote_format_utils.h"
#include "cryptonote_config.h"
#include "misc_language.h"
Expand Down
1 change: 0 additions & 1 deletion src/cryptonote_basic/cryptonote_boost_serialization.h
Expand Up @@ -36,7 +36,6 @@
#include <boost/serialization/set.hpp>
#include <boost/serialization/map.hpp>
#include <boost/serialization/is_bitwise_serializable.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/portable_binary_iarchive.hpp>
#include <boost/archive/portable_binary_oarchive.hpp>
#include "cryptonote_basic.h"
Expand Down
22 changes: 22 additions & 0 deletions src/p2p/p2p_protocol_defs.h
Expand Up @@ -39,6 +39,7 @@
#include "misc_language.h"
#include "string_tools.h"
#include "time_helper.h"
#include "serialization/serialization.h"
#include "cryptonote_config.h"

namespace nodetool
Expand Down Expand Up @@ -84,6 +85,15 @@ namespace nodetool
KV_SERIALIZE_OPT(rpc_port, (uint16_t)0)
KV_SERIALIZE_OPT(rpc_credits_per_hash, (uint32_t)0)
END_KV_SERIALIZE_MAP()

BEGIN_SERIALIZE()
FIELD(adr)
FIELD(id)
VARINT_FIELD(last_seen)
VARINT_FIELD(pruning_seed)
VARINT_FIELD(rpc_port)
VARINT_FIELD(rpc_credits_per_hash)
END_SERIALIZE()
};
typedef peerlist_entry_base<epee::net_utils::network_address> peerlist_entry;

Expand All @@ -99,6 +109,12 @@ namespace nodetool
KV_SERIALIZE(id)
KV_SERIALIZE(first_seen)
END_KV_SERIALIZE_MAP()

BEGIN_SERIALIZE()
FIELD(adr)
FIELD(id)
VARINT_FIELD(first_seen)
END_SERIALIZE()
};
typedef anchor_peerlist_entry_base<epee::net_utils::network_address> anchor_peerlist_entry;

Expand All @@ -114,6 +130,12 @@ namespace nodetool
KV_SERIALIZE(id)
KV_SERIALIZE(is_income)
END_KV_SERIALIZE_MAP()

BEGIN_SERIALIZE()
FIELD(adr)
FIELD(id)
FIELD(is_income)
END_SERIALIZE()
};
typedef connection_entry_base<epee::net_utils::network_address> connection_entry;

Expand Down
29 changes: 28 additions & 1 deletion src/ringct/rctTypes.h
Expand Up @@ -49,7 +49,7 @@ extern "C" {
#include "hex.h"
#include "span.h"
#include "memwipe.h"
#include "serialization/vector.h"
#include "serialization/containers.h"
#include "serialization/debug_archive.h"
#include "serialization/binary_archive.h"
#include "serialization/json_archive.h"
Expand Down Expand Up @@ -239,6 +239,12 @@ namespace rct {
struct RCTConfig {
RangeProofType range_proof_type;
int bp_version;

BEGIN_SERIALIZE_OBJECT()
VERSION_FIELD(0)
VARINT_FIELD(range_proof_type)
VARINT_FIELD(bp_version)
END_SERIALIZE()
};
struct rctSigBase {
uint8_t type;
Expand Down Expand Up @@ -317,6 +323,16 @@ namespace rct {
ar.end_array();
return ar.stream().good();
}

BEGIN_SERIALIZE_OBJECT()
FIELD(type)
FIELD(message)
FIELD(mixRing)
FIELD(pseudoOuts)
FIELD(ecdhInfo)
FIELD(outPk)
VARINT_FIELD(txnFee)
END_SERIALIZE()
};
struct rctSigPrunable {
std::vector<rangeSig> rangeSigs;
Expand Down Expand Up @@ -436,6 +452,12 @@ namespace rct {
return ar.stream().good();
}

BEGIN_SERIALIZE_OBJECT()
FIELD(rangeSigs)
FIELD(bulletproofs)
FIELD(MGs)
FIELD(pseudoOuts)
END_SERIALIZE()
};
struct rctSig: public rctSigBase {
rctSigPrunable p;
Expand All @@ -449,6 +471,11 @@ namespace rct {
{
return type == RCTTypeBulletproof || type == RCTTypeBulletproof2 ? p.pseudoOuts : pseudoOuts;
}

BEGIN_SERIALIZE_OBJECT()
FIELDS((rctSigBase&)*this)
FIELD(p)
END_SERIALIZE()
};

//other basepoint H = toPoint(cn_fast_hash(G)), G the basepoint
Expand Down
29 changes: 21 additions & 8 deletions src/rpc/rpc_payment.cpp
Expand Up @@ -27,14 +27,12 @@
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include <boost/archive/portable_binary_iarchive.hpp>
#include <boost/archive/portable_binary_oarchive.hpp>
#include "cryptonote_config.h"
#include "include_base_utils.h"
#include "string_tools.h"
#include "file_io_utils.h"
#include "int-util.h"
#include "common/util.h"
#include "serialization/crypto.h"
#include "common/unordered_containers_boost_serialization.h"
#include "cryptonote_basic/cryptonote_boost_serialization.h"
#include "cryptonote_basic/cryptonote_format_utils.h"
Expand Down Expand Up @@ -296,14 +294,28 @@ namespace cryptonote
data.open(state_file_path, std::ios_base::binary | std::ios_base::in);
if (!data.fail())
{
bool loaded = false;
try
{
boost::archive::portable_binary_iarchive a(data);
a >> *this;
binary_archive<false> ar(data);
if (::serialization::serialize(ar, *this))
if (::serialization::check_stream_state(ar))
loaded = true;
}
catch (const std::exception &e)
catch (...) {}
if (!loaded)
{
MERROR("Failed to load RPC payments file: " << e.what());
try
{
boost::archive::portable_binary_iarchive a(data);
a >> *this;
loaded = true;
}
catch (...) {}
}
if (!loaded)
{
MERROR("Failed to load RPC payments file");
m_client_info.clear();
}
}
Expand Down Expand Up @@ -344,8 +356,9 @@ namespace cryptonote
MWARNING("Failed to save RPC payments to file " << state_file_path);
return false;
};
boost::archive::portable_binary_oarchive a(data);
a << *this;
binary_archive<true> ar(data);
if (!::serialization::serialize(ar, *const_cast<rpc_payment*>(this)))
return false;
return true;
CATCH_ENTRY_L0("rpc_payment::store", false);
}
Expand Down
57 changes: 50 additions & 7 deletions src/rpc/rpc_payment.h
Expand Up @@ -31,10 +31,17 @@
#include <string>
#include <unordered_set>
#include <unordered_map>
#include <map>
#include <boost/thread/mutex.hpp>
#include <boost/serialization/version.hpp>
#include "cryptonote_basic/blobdatatype.h"
#include "cryptonote_basic/cryptonote_basic.h"
#include <boost/serialization/list.hpp>
#include <boost/serialization/vector.hpp>
#include "serialization/crypto.h"
#include "serialization/string.h"
#include "serialization/pair.h"
#include "serialization/containers.h"

namespace cryptonote
{
Expand Down Expand Up @@ -96,6 +103,33 @@ namespace cryptonote
a & nonces_bad;
a & nonces_dupe;
}

BEGIN_SERIALIZE_OBJECT()
VERSION_FIELD(0)
FIELD(block)
FIELD(previous_block)
FIELD(hashing_blob)
FIELD(previous_hashing_blob)
VARINT_FIELD(seed_height)
VARINT_FIELD(previous_seed_height)
FIELD(seed_hash)
FIELD(previous_seed_hash)
VARINT_FIELD(cookie)
FIELD(top)
FIELD(previous_top)
VARINT_FIELD(credits)
FIELD(payments)
FIELD(previous_payments)
FIELD(update_time)
FIELD(last_request_timestamp)
FIELD(block_template_update_time)
VARINT_FIELD(credits_total)
VARINT_FIELD(credits_used)
VARINT_FIELD(nonces_good)
VARINT_FIELD(nonces_stale)
VARINT_FIELD(nonces_bad)
VARINT_FIELD(nonces_dupe)
END_SERIALIZE()
};

public:
Expand All @@ -114,8 +148,8 @@ namespace cryptonote
template <class t_archive>
inline void serialize(t_archive &a, const unsigned int ver)
{
a & m_client_info;
a & m_hashrate;
a & m_client_info.parent();
a & m_hashrate.parent();
a & m_credits_total;
a & m_credits_used;
a & m_nonces_good;
Expand All @@ -124,16 +158,28 @@ namespace cryptonote
a & m_nonces_dupe;
}

BEGIN_SERIALIZE_OBJECT()
VERSION_FIELD(0)
FIELD(m_client_info)
FIELD(m_hashrate)
VARINT_FIELD(m_credits_total)
VARINT_FIELD(m_credits_used)
VARINT_FIELD(m_nonces_good)
VARINT_FIELD(m_nonces_stale)
VARINT_FIELD(m_nonces_bad)
VARINT_FIELD(m_nonces_dupe)
END_SERIALIZE()

bool load(std::string directory);
bool store(const std::string &directory = std::string()) const;

private:
cryptonote::account_public_address m_address;
uint64_t m_diff;
uint64_t m_credits_per_hash_found;
std::unordered_map<crypto::public_key, client_info> m_client_info;
serializable_unordered_map<crypto::public_key, client_info> m_client_info;
std::string m_directory;
std::map<uint64_t, uint64_t> m_hashrate;
serializable_map<uint64_t, uint64_t> m_hashrate;
uint64_t m_credits_total;
uint64_t m_credits_used;
uint64_t m_nonces_good;
Expand All @@ -143,6 +189,3 @@ namespace cryptonote
mutable boost::mutex mutex;
};
}

BOOST_CLASS_VERSION(cryptonote::rpc_payment, 0);
BOOST_CLASS_VERSION(cryptonote::rpc_payment::client_info, 0);
6 changes: 1 addition & 5 deletions src/serialization/container.h
Expand Up @@ -28,10 +28,6 @@
//
// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers

#pragma once

#include "serialization.h"

namespace serialization
{
namespace detail
Expand Down Expand Up @@ -103,7 +99,7 @@ bool do_serialize_container(Archive<true> &ar, C &v)
return false;
if (i != v.begin())
ar.delimit_array();
if(!::serialization::detail::serialize_container_element(ar, const_cast<typename C::value_type&>(*i)))
if(!::serialization::detail::serialize_container_element(ar, (typename C::value_type&)*i))
return false;
if (!ar.stream().good())
return false;
Expand Down

0 comments on commit 7175dcb

Please sign in to comment.