From 90016ad744285941435b1c4e070a3037d93d0d07 Mon Sep 17 00:00:00 2001 From: moneromooo-monero Date: Mon, 6 Jul 2020 15:47:57 +0000 Subject: [PATCH 1/4] blockchain: guard against exceptions in add_new_block/children Reporter requested credit to be given to Decred --- src/cryptonote_core/blockchain.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/cryptonote_core/blockchain.cpp b/src/cryptonote_core/blockchain.cpp index 7851b0f6a..21cbb48e9 100644 --- a/src/cryptonote_core/blockchain.cpp +++ b/src/cryptonote_core/blockchain.cpp @@ -4369,6 +4369,9 @@ bool Blockchain::update_next_cumulative_weight_limit(uint64_t *long_term_effecti //------------------------------------------------------------------ bool Blockchain::add_new_block(const block& bl, block_verification_context& bvc) { + try + { + LOG_PRINT_L3("Blockchain::" << __func__); crypto::hash id = get_block_hash(bl); CRITICAL_REGION_LOCAL(m_tx_pool);//to avoid deadlock lets lock tx_pool for whole add/reorganize process @@ -4396,6 +4399,14 @@ bool Blockchain::add_new_block(const block& bl, block_verification_context& bvc) rtxn_guard.stop(); return handle_block_to_main_chain(bl, id, bvc); + + } + catch (const std::exception &e) + { + LOG_ERROR("Exception at [add_new_block], what=" << e.what()); + bvc.m_verifivation_failed = true; + return false; + } } //------------------------------------------------------------------ //TODO: Refactor, consider returning a failure height and letting From 6dd95d5308548d62f9340d1f8bfc9ffa21e2eec8 Mon Sep 17 00:00:00 2001 From: moneromooo-monero Date: Mon, 6 Jul 2020 15:49:02 +0000 Subject: [PATCH 2/4] epee: guard against exceptions in RPC handlers --- .../include/net/http_server_handlers_map2.h | 40 ++++++++++++++----- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/contrib/epee/include/net/http_server_handlers_map2.h b/contrib/epee/include/net/http_server_handlers_map2.h index 0c0653cd6..ac22cd7a9 100644 --- a/contrib/epee/include/net/http_server_handlers_map2.h +++ b/contrib/epee/include/net/http_server_handlers_map2.h @@ -42,8 +42,17 @@ MINFO("HTTP [" << m_conn_context.m_remote_address.host_str() << "] " << query_info.m_http_method_str << " " << query_info.m_URI); \ response.m_response_code = 200; \ response.m_response_comment = "Ok"; \ - if(!handle_http_request_map(query_info, response, m_conn_context)) \ - {response.m_response_code = 404;response.m_response_comment = "Not found";} \ + try \ + { \ + if(!handle_http_request_map(query_info, response, m_conn_context)) \ + {response.m_response_code = 404;response.m_response_comment = "Not found";} \ + } \ + catch (const std::exception &e) \ + { \ + MERROR(m_conn_context << "Exception in handle_http_request_map: " << e.what()); \ + response.m_response_code = 500; \ + response.m_response_comment = "Internal Server Error"; \ + } \ return true; \ } @@ -69,9 +78,11 @@ uint64_t ticks1 = epee::misc_utils::get_tick_count(); \ boost::value_initialized resp;\ MINFO(m_conn_context << "calling " << s_pattern); \ - if(!callback_f(static_cast(req), static_cast(resp), &m_conn_context)) \ + bool res = false; \ + try { res = callback_f(static_cast(req), static_cast(resp), &m_conn_context); } \ + catch (const std::exception &e) { MERROR(m_conn_context << "Failed to " << #callback_f << "(): " << e.what()); } \ + if (!res) \ { \ - MERROR(m_conn_context << "Failed to " << #callback_f << "()"); \ response_info.m_response_code = 500; \ response_info.m_response_comment = "Internal Server Error"; \ return true; \ @@ -97,9 +108,11 @@ uint64_t ticks1 = misc_utils::get_tick_count(); \ boost::value_initialized resp;\ MINFO(m_conn_context << "calling " << s_pattern); \ - if(!callback_f(static_cast(req), static_cast(resp), &m_conn_context)) \ + bool res = false; \ + try { res = callback_f(static_cast(req), static_cast(resp), &m_conn_context); } \ + catch (const std::exception &e) { MERROR(m_conn_context << "Failed to " << #callback_f << "()"); } \ + if (!res) \ { \ - MERROR(m_conn_context << "Failed to " << #callback_f << "()"); \ response_info.m_response_code = 500; \ response_info.m_response_comment = "Internal Server Error"; \ return true; \ @@ -184,7 +197,10 @@ fail_resp.jsonrpc = "2.0"; \ fail_resp.id = req.id; \ MINFO(m_conn_context << "Calling RPC method " << method_name); \ - if(!callback_f(req.params, resp.result, fail_resp.error, &m_conn_context)) \ + bool res = false; \ + try { res = callback_f(req.params, resp.result, fail_resp.error, &m_conn_context); } \ + catch (const std::exception &e) { MERROR(m_conn_context << "Failed to " << #callback_f << "(): " << e.what()); } \ + if (!res) \ { \ epee::serialization::store_t_to_json(static_cast(fail_resp), response_info.m_body); \ return true; \ @@ -203,7 +219,10 @@ fail_resp.jsonrpc = "2.0"; \ fail_resp.id = req.id; \ MINFO(m_conn_context << "calling RPC method " << method_name); \ - if(!callback_f(req.params, resp.result, fail_resp.error, response_info, &m_conn_context)) \ + bool res = false; \ + try { res = callback_f(req.params, resp.result, fail_resp.error, response_info, &m_conn_context); } \ + catch (const std::exception &e) { MERROR(m_conn_context << "Failed to " << #callback_f << "(): " << e.what()); } \ + if (!res) \ { \ epee::serialization::store_t_to_json(static_cast(fail_resp), response_info.m_body); \ return true; \ @@ -217,7 +236,10 @@ { \ PREPARE_OBJECTS_FROM_JSON(command_type) \ MINFO(m_conn_context << "calling RPC method " << method_name); \ - if(!callback_f(req.params, resp.result, &m_conn_context)) \ + bool res = false; \ + try { res = callback_f(req.params, resp.result, &m_conn_context); } \ + catch (const std::exception &e) { MERROR(m_conn_context << "Failed to " << #callback_f << "(): " << e.what()); } \ + if (!res) \ { \ epee::json_rpc::error_response fail_resp = AUTO_VAL_INIT(fail_resp); \ fail_resp.jsonrpc = "2.0"; \ From 92e6b7df2c816dc4b0a075b0d2fafbc5dc018bbb Mon Sep 17 00:00:00 2001 From: moneromooo-monero Date: Wed, 29 Jul 2020 17:03:21 +0000 Subject: [PATCH 3/4] easylogging++: fix crash with reentrant logging --- external/easylogging++/easylogging++.cc | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/external/easylogging++/easylogging++.cc b/external/easylogging++/easylogging++.cc index 0d748c225..bf877c018 100644 --- a/external/easylogging++/easylogging++.cc +++ b/external/easylogging++/easylogging++.cc @@ -2968,6 +2968,16 @@ void Writer::initializeLogger(Logger *logger, bool needLock) { } void Writer::processDispatch() { + static std::atomic_flag in_dispatch; + if (in_dispatch.test_and_set()) + { + if (m_proceed && m_logger != NULL) + { + m_logger->stream().str(ELPP_LITERAL("")); + m_logger->releaseLock(); + } + return; + } #if ELPP_LOGGING_ENABLED if (ELPP->hasFlag(LoggingFlag::MultiLoggerSupport)) { bool firstDispatched = false; @@ -3006,6 +3016,7 @@ void Writer::processDispatch() { m_logger->releaseLock(); } #endif // ELPP_LOGGING_ENABLED + in_dispatch.clear(); } void Writer::triggerDispatch(void) { From 13eee1d6abb035232acee3d757137d154f0ebb94 Mon Sep 17 00:00:00 2001 From: moneromooo-monero Date: Wed, 29 Jul 2020 17:40:46 +0000 Subject: [PATCH 4/4] rpc: reject wrong sized txid Reporter requested credit to be given to Decred --- src/rpc/core_rpc_server.cpp | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/rpc/core_rpc_server.cpp b/src/rpc/core_rpc_server.cpp index bc561abc4..c1a70c570 100644 --- a/src/rpc/core_rpc_server.cpp +++ b/src/rpc/core_rpc_server.cpp @@ -2453,14 +2453,13 @@ namespace cryptonote { for (const auto &str: req.txids) { - cryptonote::blobdata txid_data; - if(!epee::string_tools::parse_hexstr_to_binbuff(str, txid_data)) + crypto::hash txid; + if(!epee::string_tools::hex_to_pod(str, txid)) { failed = true; } else { - crypto::hash txid = *reinterpret_cast(txid_data.data()); txids.push_back(txid); } } @@ -2805,15 +2804,14 @@ namespace cryptonote res.status = ""; for (const auto &str: req.txids) { - cryptonote::blobdata txid_data; - if(!epee::string_tools::parse_hexstr_to_binbuff(str, txid_data)) + crypto::hash txid; + if(!epee::string_tools::hex_to_pod(str, txid)) { if (!res.status.empty()) res.status += ", "; res.status += std::string("invalid transaction id: ") + str; failed = true; continue; } - crypto::hash txid = *reinterpret_cast(txid_data.data()); cryptonote::blobdata txblob; if (m_core.get_pool_transaction(txid, txblob, relay_category::legacy))