From cb335cffed71c7642922da5661424b374bc40a46 Mon Sep 17 00:00:00 2001 From: Bryan Biedenkapp Date: Sat, 8 Mar 2025 21:02:15 -0500 Subject: [PATCH] add some exception checking around some cases where vector types are used; --- src/common/lookups/AffiliationLookup.cpp | 4 +- src/common/lookups/ChannelLookup.cpp | 14 ++- src/common/lookups/ChannelLookup.h | 3 +- src/common/network/udp/Socket.cpp | 113 ++++++++++++----------- 4 files changed, 74 insertions(+), 60 deletions(-) diff --git a/src/common/lookups/AffiliationLookup.cpp b/src/common/lookups/AffiliationLookup.cpp index e4dde41c..e8b784df 100644 --- a/src/common/lookups/AffiliationLookup.cpp +++ b/src/common/lookups/AffiliationLookup.cpp @@ -305,7 +305,9 @@ bool AffiliationLookup::grantCh(uint32_t dstId, uint32_t srcId, uint32_t grantTi std::lock_guard lock(m_mutex); uint32_t chNo = m_chLookup->getFirstRFChannel(); - m_chLookup->removeRFCh(chNo); + if (!m_chLookup->removeRFCh(chNo)) { + return false; + } m_grantChTable[dstId] = chNo; m_grantSrcIdTable[dstId] = srcId; diff --git a/src/common/lookups/ChannelLookup.cpp b/src/common/lookups/ChannelLookup.cpp index b8a8b311..f803702a 100644 --- a/src/common/lookups/ChannelLookup.cpp +++ b/src/common/lookups/ChannelLookup.cpp @@ -71,12 +71,18 @@ bool ChannelLookup::addRFCh(uint32_t chNo, bool force) /* Helper to remove a RF channel. */ -void ChannelLookup::removeRFCh(uint32_t chNo) +bool ChannelLookup::removeRFCh(uint32_t chNo) { if (chNo == 0U) { - return; + return false; } - auto it = std::find(m_rfChTable.begin(), m_rfChTable.end(), chNo); - m_rfChTable.erase(it); + try { + auto it = std::find(m_rfChTable.begin(), m_rfChTable.end(), chNo); + m_rfChTable.erase(it); + } catch (...) { + return false; + } + + return true; } diff --git a/src/common/lookups/ChannelLookup.h b/src/common/lookups/ChannelLookup.h index afe3dcb7..7a979352 100644 --- a/src/common/lookups/ChannelLookup.h +++ b/src/common/lookups/ChannelLookup.h @@ -206,8 +206,9 @@ namespace lookups /** * @brief Helper to remove a RF channel. * @param chNo Channel Number. + * @returns bool True, if channel remove, otherwise false. */ - void removeRFCh(uint32_t chNo); + bool removeRFCh(uint32_t chNo); /** * @brief Helper to determine if there are any RF channels available.. * @returns bool True, if any RF channels are available for use, otherwise false. diff --git a/src/common/network/udp/Socket.cpp b/src/common/network/udp/Socket.cpp index d0971a2d..58b2a603 100644 --- a/src/common/network/udp/Socket.cpp +++ b/src/common/network/udp/Socket.cpp @@ -467,64 +467,69 @@ bool Socket::write(BufferVector& buffers, ssize_t* lenWritten) noexcept continue; } - // are we crypto wrapped? - if (m_isCryptoWrapped && m_presharedKey != nullptr) { - uint32_t cryptedLen = length * sizeof(uint8_t); - uint8_t* cryptoBuffer = buffers[i]->buffer; - - // do we need to pad the original buffer to be block aligned? - if (cryptedLen % crypto::AES::BLOCK_BYTES_LEN != 0) { - uint32_t alignment = crypto::AES::BLOCK_BYTES_LEN - (cryptedLen % crypto::AES::BLOCK_BYTES_LEN); - cryptedLen += alignment; - - // reallocate buffer and copy - cryptoBuffer = new uint8_t[cryptedLen]; - ::memset(cryptoBuffer, 0x00U, cryptedLen); - ::memcpy(cryptoBuffer, buffers.at(i)->buffer, length); - } - - // encrypt - uint8_t* crypted = m_aes->encryptECB(cryptoBuffer, cryptedLen, m_presharedKey); - delete[] cryptoBuffer; - - if (crypted == nullptr) { - --size; - continue; + try { + // are we crypto wrapped? + if (m_isCryptoWrapped && m_presharedKey != nullptr) { + uint32_t cryptedLen = length * sizeof(uint8_t); + uint8_t* cryptoBuffer = buffers[i]->buffer; + + // do we need to pad the original buffer to be block aligned? + if (cryptedLen % crypto::AES::BLOCK_BYTES_LEN != 0) { + uint32_t alignment = crypto::AES::BLOCK_BYTES_LEN - (cryptedLen % crypto::AES::BLOCK_BYTES_LEN); + cryptedLen += alignment; + + // reallocate buffer and copy + cryptoBuffer = new uint8_t[cryptedLen]; + ::memset(cryptoBuffer, 0x00U, cryptedLen); + ::memcpy(cryptoBuffer, buffers.at(i)->buffer, length); + } + + // encrypt + uint8_t* crypted = m_aes->encryptECB(cryptoBuffer, cryptedLen, m_presharedKey); + delete[] cryptoBuffer; + + if (crypted == nullptr) { + --size; + continue; + } + + // Utils::dump(1U, "Socket::write() crypted", crypted, cryptedLen); + + // finalize + UInt8Array __outBuf = std::make_unique(cryptedLen + 2U); + uint8_t* out = __outBuf.get(); + ::memcpy(out + 2U, crypted, cryptedLen); + __SET_UINT16B(AES_WRAPPED_PCKT_MAGIC, out, 0U); + + // cleanup buffers and replace with new + delete[] crypted; + //delete buffers[i]->buffer; + + // this should never happen... + if (buffers[i] == nullptr) { + --size; + continue; + } + + buffers[i]->buffer = new uint8_t[cryptedLen + 2U]; + ::memcpy(buffers[i]->buffer, out, cryptedLen + 2U); + buffers[i]->length = cryptedLen + 2U; } - // Utils::dump(1U, "Socket::write() crypted", crypted, cryptedLen); - - // finalize - UInt8Array __outBuf = std::make_unique(cryptedLen + 2U); - uint8_t* out = __outBuf.get(); - ::memcpy(out + 2U, crypted, cryptedLen); - __SET_UINT16B(AES_WRAPPED_PCKT_MAGIC, out, 0U); - - // cleanup buffers and replace with new - delete[] crypted; - //delete buffers[i]->buffer; - - // this should never happen... - if (buffers[i] == nullptr) { - --size; - continue; - } + chunks[i].iov_len = buffers.at(i)->length; + chunks[i].iov_base = buffers.at(i)->buffer; + sent += buffers.at(i)->length; - buffers[i]->buffer = new uint8_t[cryptedLen + 2U]; - ::memcpy(buffers[i]->buffer, out, cryptedLen + 2U); - buffers[i]->length = cryptedLen + 2U; + headers[i].msg_hdr.msg_name = (void*)&buffers.at(i)->address; + headers[i].msg_hdr.msg_namelen = buffers.at(i)->addrLen; + headers[i].msg_hdr.msg_iov = &chunks[i]; + headers[i].msg_hdr.msg_iovlen = 1; + headers[i].msg_hdr.msg_control = 0; + headers[i].msg_hdr.msg_controllen = 0; + } + catch (...) { + --size; } - - chunks[i].iov_len = buffers.at(i)->length; - chunks[i].iov_base = buffers.at(i)->buffer; - sent += buffers.at(i)->length; - - headers[i].msg_hdr.msg_name = (void*)&buffers.at(i)->address; - headers[i].msg_hdr.msg_namelen = buffers.at(i)->addrLen; - headers[i].msg_hdr.msg_iov = &chunks[i]; - headers[i].msg_hdr.msg_iovlen = 1; - headers[i].msg_hdr.msg_control = 0; - headers[i].msg_hdr.msg_controllen = 0; } bool skip = false;