From: Martin Quinson Date: Sat, 12 Feb 2022 08:41:39 +0000 (+0100) Subject: Fix the security hotspots of sonar X-Git-Tag: v3.31~433 X-Git-Url: http://bilbo.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/ba5430814ffccfb457bdd0e99b4f31c42200adf1 Fix the security hotspots of sonar --- diff --git a/src/kernel/actor/SimcallObserver.cpp b/src/kernel/actor/SimcallObserver.cpp index 8a9287cc5d..03ce516bce 100644 --- a/src/kernel/actor/SimcallObserver.cpp +++ b/src/kernel/actor/SimcallObserver.cpp @@ -34,7 +34,9 @@ void RandomSimcall::serialize(Simcall& type, char* buffer) std::stringstream stream; stream << min_ << ' ' << max_; - strcpy(buffer, stream.str().c_str()); + xbt_assert(stream.str().size() < SIMCALL_SERIALIZATION_BUFFER_SIZE, + "The serialized simcall is too large for the buffer. Please fix the code."); + strncpy(buffer, stream.str().c_str(), SIMCALL_SERIALIZATION_BUFFER_SIZE); } bool MutexSimcall::depends(SimcallObserver* other) @@ -237,10 +239,12 @@ void ActivityWaitSimcall::serialize(Simcall& type, char* buffer) stream << ' ' << (comm->dst_actor_ != nullptr ? comm->dst_actor_->get_pid() : -1); stream << ' ' << comm->get_mailbox_id(); stream << ' ' << (void*)comm->src_buff_ << ' ' << (void*)comm->dst_buff_ << ' ' << comm->src_buff_size_; - strcpy(buffer, stream.str().c_str()); + xbt_assert(stream.str().size() < SIMCALL_SERIALIZATION_BUFFER_SIZE, + "The serialized simcall is too large for the buffer. Please fix the code."); + strncpy(buffer, stream.str().c_str(), SIMCALL_SERIALIZATION_BUFFER_SIZE); } else { type = Simcall::UNKNOWN; - strcpy(buffer, stream.str().c_str()); + buffer[0] = '\0'; } } @@ -356,7 +360,9 @@ void CommIsendSimcall::serialize(Simcall& type, char* buffer) type = Simcall::ISEND; std::stringstream stream; stream << mbox_->get_id() << ' ' << (void*)src_buff_ << ' ' << src_buff_size_; - strcpy(buffer, stream.str().c_str()); + xbt_assert(stream.str().size() < SIMCALL_SERIALIZATION_BUFFER_SIZE, + "The serialized simcall is too large for the buffer. Please fix the code."); + strncpy(buffer, stream.str().c_str(), SIMCALL_SERIALIZATION_BUFFER_SIZE); XBT_DEBUG("SendObserver mbox:%u buff:%p size:%zu", mbox_->get_id(), src_buff_, src_buff_size_); } @@ -365,7 +371,9 @@ void CommIrecvSimcall::serialize(Simcall& type, char* buffer) type = Simcall::IRECV; std::stringstream stream; stream << mbox_->get_id() << dst_buff_; - strcpy(buffer, stream.str().c_str()); + xbt_assert(stream.str().size() < SIMCALL_SERIALIZATION_BUFFER_SIZE, + "The serialized simcall is too large for the buffer. Please fix the code."); + strncpy(buffer, stream.str().c_str(), SIMCALL_SERIALIZATION_BUFFER_SIZE); } diff --git a/src/kernel/actor/SimcallObserver.hpp b/src/kernel/actor/SimcallObserver.hpp index 95f6e25036..2666b9ca74 100644 --- a/src/kernel/actor/SimcallObserver.hpp +++ b/src/kernel/actor/SimcallObserver.hpp @@ -12,6 +12,8 @@ #include +#define SIMCALL_SERIALIZATION_BUFFER_SIZE 2048 + namespace simgrid { namespace kernel { namespace actor { diff --git a/src/mc/remote/mc_protocol.h b/src/mc/remote/mc_protocol.h index e6a798a850..1a1fed354e 100644 --- a/src/mc/remote/mc_protocol.h +++ b/src/mc/remote/mc_protocol.h @@ -104,7 +104,7 @@ struct s_mc_message_simcall_execute_t { struct s_mc_message_simcall_execute_answer_t { simgrid::mc::MessageType type; simgrid::kernel::actor::SimcallObserver::Simcall simcall; - char buffer[2048]; + char buffer[SIMCALL_SERIALIZATION_BUFFER_SIZE]; }; struct s_mc_message_restore_t {