From 0cca99ea86584b118039aeca90f00ea4747077a8 Mon Sep 17 00:00:00 2001 From: Nicholas Hastings Date: Wed, 10 Jun 2026 20:22:40 -0400 Subject: [PATCH] Fix cases of possible buffer overrun --- core/PlayerManager.cpp | 15 ++++++++++++--- extensions/sdktools/CellRecipientFilter.h | 8 ++++++++ extensions/sdktools/vsound.cpp | 7 ++++--- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/core/PlayerManager.cpp b/core/PlayerManager.cpp index 1d541daa..2bcdd786 100644 --- a/core/PlayerManager.cpp +++ b/core/PlayerManager.cpp @@ -1650,6 +1650,14 @@ void PlayerManager::ProcessCommandTarget(cmd_target_info_t *info) strcpy(new_pattern, "STEAM_"); len = strlen(&info->pattern[7]); + /* Bound the copy so the "STEAM_" prefix, the copied bytes and the + * null terminator all fit within new_pattern. An over-long pattern + * is simply truncated; it will not match a real client's id. + */ + if (len > sizeof(new_pattern) - 7) + { + len = sizeof(new_pattern) - 7; + } for (p = 0; p < len; p++) { new_pattern[6 + p] = info->pattern[7 + p]; @@ -1664,12 +1672,13 @@ void PlayerManager::ProcessCommandTarget(cmd_target_info_t *info) { size_t p = 0; char c; - while ((c = info->pattern[p + 1]) != '\0') + /* Leave room for the null terminator; truncate over-long patterns. */ + while ((c = info->pattern[p + 1]) != '\0' && p < sizeof(new_pattern) - 1) { - new_pattern[p] = (c == '_') ? ':' : c; + new_pattern[p] = (c == '_') ? ':' : c; ++p; } - + new_pattern[p] = '\0'; } diff --git a/extensions/sdktools/CellRecipientFilter.h b/extensions/sdktools/CellRecipientFilter.h index 213443a5..215c5dcd 100644 --- a/extensions/sdktools/CellRecipientFilter.h +++ b/extensions/sdktools/CellRecipientFilter.h @@ -106,6 +106,14 @@ inline void CellRecipientFilter::SetToReliable(bool isreliable) inline void CellRecipientFilter::Initialize(cell_t *ptr, size_t count) { + /* Never copy more entries than m_Players can hold. Callers derive count + * from plugin-supplied data, which may contain duplicate client indices + * that pass validation while exceeding the array bounds. + */ + if (count > (sizeof(m_Players) / sizeof(m_Players[0]))) + { + count = sizeof(m_Players) / sizeof(m_Players[0]); + } memcpy(m_Players, ptr, count * sizeof(cell_t)); m_Size = count; } diff --git a/extensions/sdktools/vsound.cpp b/extensions/sdktools/vsound.cpp index 52a131da..29d50edc 100644 --- a/extensions/sdktools/vsound.cpp +++ b/extensions/sdktools/vsound.cpp @@ -31,6 +31,7 @@ #include "vsound.h" #include +#include "am-string.h" SH_DECL_HOOK8_void(IVEngineServer, EmitAmbientSound, SH_NOATTRIB, 0, int, const Vector &, const char *, float, soundlevel_t, int, int, float); @@ -228,7 +229,7 @@ void SoundHooks::OnEmitAmbientSound(int entindex, const Vector &pos, const char cell_t vec[3] = {sp_ftoc(pos.x), sp_ftoc(pos.y), sp_ftoc(pos.z)}; cell_t res = static_cast(Pl_Continue); char buffer[PLATFORM_MAX_PATH]; - strcpy(buffer, samp); + ke::SafeStrcpy(buffer, sizeof(buffer), samp); for (iter=m_AmbientFuncs.begin(); iter!=m_AmbientFuncs.end(); iter++) { @@ -343,7 +344,7 @@ void SoundHooks::OnEmitSound(IRecipientFilter &filter, int iEntIndex, int iChann IPluginFunction *pFunc; cell_t res = static_cast(Pl_Continue); char buffer[PLATFORM_MAX_PATH]; - strcpy(buffer, pSample); + ke::SafeStrcpy(buffer, sizeof(buffer), pSample); char soundEntry[PLATFORM_MAX_PATH] = ""; #if SOURCE_ENGINE >= SE_PORTAL2 @@ -507,7 +508,7 @@ void SoundHooks::OnEmitSound2(IRecipientFilter &filter, int iEntIndex, int iChan cell_t res = static_cast(Pl_Continue); cell_t sndlevel = static_cast(ATTN_TO_SNDLVL(flAttenuation)); char buffer[PLATFORM_MAX_PATH]; - strcpy(buffer, pSample); + ke::SafeStrcpy(buffer, sizeof(buffer), pSample); char soundEntry[PLATFORM_MAX_PATH] = ""; #if SOURCE_ENGINE >= SE_PORTAL2