Skip to content

fix: bound RunThink to player slots - #1495

Merged
roflmuffin merged 1 commit into
roflmuffin:mainfrom
makadore:fix/run-think-loop-bound
Nov 15, 2026
Merged

roflmuffin merged 1 commit into
roflmuffin:mainfrom
makadore:fix/run-think-loop-bound

Conversation

@makadore

Copy link
Copy Markdown
Contributor

PlayerManager::RunThink loops with i <= MaxClients(). With maxClients = 64 in the test run, the extra iteration takes entity index 65, outside the player slots, casts it to CCSPlayerController and reads m_hPlayerPawn. GetPlayerBySlot(64) returns an existing array element, so !player does not stop that iteration.
AddressSanitizer reported a heap-buffer-overflow at player_manager.cpp:337 and :344 in all six runs with the original bound after the map loaded. One report is below.
With the bound changed to i < MaxClients(), this RunThink report was absent in three runs on the same Linux CS2 1.41.9.7 setup; the 54 Tests.Native cases of the five classes run passed each time.
No test is added: a test without ASan would not reliably detect this invalid read; the before and after ASan runs are the regression check.

AddressSanitizer report on main
==3553==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x51e0000e3444 at pc 0x7d56e990b010 bp 0x7ffe19340d70 sp 0x7ffe19340d60
READ of size 4 at 0x51e0000e3444 thread T0
    #0 0x7d56e990b00f in CEntityHandle::IsValid() const libraries/hl2sdk-cs2/public/entityhandle.h:97
    #1 0x7d56e990b00f in counterstrikesharp::PlayerManager::RunThink() const src/core/managers/player_manager.cpp:337
    #2 0x7d56f40e2a60  (libserver.so+0xee2a60)
    #3 0x7d56f4b833f0  (libserver.so+0x19833f0)
    #4 0x7d56e9400a75 in KHook::Virtual<ISource2Server, void, bool, bool, bool>::_KHook_MakeOriginalCall(bool, bool, bool) libraries/metamod-source/third_party/khook/include/khook.hpp:2078
0x51e0000e3444 is located 60 bytes before 2816-byte region [0x51e0000e3480,0x51e0000e3f80)
allocated by thread T0 here:
    #0 0x7d5745efe548 in operator new(unsigned long) ../../../../src/libsanitizer/asan/asan_new_delete.cpp:95
    #1 0x7d56f54633ef  (libserver.so+0x22633ef)
SUMMARY: AddressSanitizer: heap-buffer-overflow libraries/hl2sdk-cs2/public/entityhandle.h:97 in CEntityHandle::IsValid() const

RunThink included i == maxClients and treated entity index
maxClients + 1 as a player controller. ASan reported an out-of-bounds
read in all six runs with the original bound.
@makadore
makadore requested a review from roflmuffin as a code owner November 14, 2026 17:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants