Potential buffer overflow in demoplayer module of GoldSource Engine
The demo player's message-reading routine trusts a length value taken from a .dem file when copying data into fixed-size stack buffers, so a malformed demo could overflow them. The author states that current packet-size limits and buffer layout prevent exploitation today, but a future recompile could make it exploitable for code execution.
Originally disclosed on HackerOne
- Triaged in 5 days
- Fixed in 4 months
- Paid in 3 months
- Disclosed after 15 months
Summary by the program
Introduction
Hey. There's a potential vulnerability in the GoldSource Engine that allows to write data to stack of arbitrary size, thereby causing a buffer overflow and the ability to execute assembler code using .dem files.
Description
The problem is located in the DemoPlayer::ReadDemoMessage function, which lies in demoplayer module, which is called when the viewdemo console command is executed. Since this command is not included in the filter list of the ValidStuffText function, and the .dem extension is not included in the filter list of the IsSafeFileToDownload function, the server can upload the malformed dem-file to the client and run it. Also, it is possible for hackers to spread incorrect dem-files without using HLDS, sending a malformed dem to users on web-resources or using messengers.
The following opcodes are vulnerable: DemoCmd::PlaySound (8), DemoCmd::PayLoad (9). These opcodes contain a variable responsible for the amount of data that must be read by the BitBuffer::ReadBuf function. Since the variable where the read result fits is located in the stack memory, it is possible to overflow the buffer and overwrite the return address on its own code.
At the moment, the vulnerability does not pose a risk, since the limit of any network packet is 4010 (MAX_UDP_PACKET constant), which excludes the possibility of a buffer overflow in the DemoCmd::PayLoad command (which is 32767 bytes in size). At the same time, the buffer for DemoCmd::PlaySound is 256 bytes, but this buffer is located in front of the buffer for DemoCmd::PayLoad, which will not allow rewriting the return address on the stack. However, there is a potential danger that in future recompiling of the project, the buffers may be swapped, which will cause a vulnerability for the DemoCmd::PlaySound package.
Possible Solutions
Create conditions that don't allow to read too much data. For example:
size = BitBuffer::ReadLong(buf);
if (size > sizeof(buffer))
size = sizeof(buffer);
BitBuffer::ReadBuf(buf, size, buffer);
Impact
As said before, an attacker can remotely execute arbitrary assembly code on the clients or player can run malformed dem-file himself with viewdemo console command.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the length field read from the demo file is passed to the buffer-read routine without being checked against the destination buffer size.
Impact
The author states the flaw does not pose a risk at present because of the 4010-byte packet limit and the current buffer ordering, but warns it could allow remote code execution if the buffers are reordered in a future build.
What the attacker needed
Crafted .dem file; victim runs viewdemo or joins a server
The fix
The length read from the file should be clamped to the destination buffer size before the copy is performed.
How it was found
- Obtain or craft a malformed .dem file containing the vulnerable opcodes.
- Deliver it to the client, either by a server pushing it or by sending it through a website or messenger.
- Have the player run it with the viewdemo console command.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
A demo file is a recording of a game session that the game replays. The game reads a number from the file saying how much data to copy, and it does not check that number against the size of the space it copies into, so a crafted file could spill past that space and corrupt nearby memory. The author says today's limits stop this from being exploitable, but the code is still unsafe.
What is Memory corruption?
Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.