Skip to content

Fix stack buffer overflow in SaveBacktraceSummary and improve crash h… - #5560

Open
Almost8bl wants to merge 3 commits into
multitheftauto:masterfrom
Almost8bl:patch-1
Open

Almost8bl wants to merge 3 commits into
multitheftauto:masterfrom
Almost8bl:patch-1

Conversation

@Almost8bl

@Almost8bl Almost8bl commented Nov 17, 2026 •

Copy link
Copy Markdown

…andler safety

Description

This PR improves the reliability and safety of the server crash handler (Server/core/CCrashHandler.cpp) across both POSIX and Windows platforms:

  1. Fix stack buffer overflow in SaveBacktraceSummary() (Linux/macOS): backtrace() expects the buffer capacity as an element count (NUMELMS(buffer)), not the byte size (sizeof buffer). On 64-bit systems, passing sizeof buffer passed 800 for a 100-element array, risking a stack buffer overflow inside the crash handler during deep-stack crashes (e.g., infinite recursion).
  2. Use thread-safe localtime_r on POSIX: Replaced localtime() with localtime_r() to avoid mutating shared static state during crash handling (matching localtime_s already used in the Windows path).
  3. Avoid heap allocation in HandleExceptionGlobal (Windows): Allocated CExceptionInformation_Impl on the stack instead of using new (std::nothrow) so the crash handler does not touch a potentially corrupted process heap before writing the crash log. Also removed the now-unused <new> header.
  4. Clean up empty/corrupt .dmp files on failure (Windows): If TryWriteDump fails after CreateFile has already created the file on disk, delete the leftover 0-byte/incomplete dump file via DeleteFileA.

Summary

Motivation

Test plan

Checklist

  • Your code should follow the coding guidelines.
  • Smaller pull requests are easier to review. If your pull request is beefy, your pull request should be reviewable commit-by-commit.

Almost8bl and others added 3 commits November 17, 2026 16:44
…andler safety

### Description
This PR improves the reliability and safety of the server crash handler (`Server/core/CCrashHandler.cpp`) across both POSIX and Windows platforms:

1. **Fix stack buffer overflow in `SaveBacktraceSummary()` (Linux/macOS):**
   `backtrace()` expects the buffer capacity as an element count (`NUMELMS(buffer)`), not the byte size (`sizeof buffer`). On 64-bit systems, passing `sizeof buffer` passed `800` for a 100-element array, risking a stack buffer overflow inside the crash handler during deep-stack crashes (e.g., infinite recursion).
2. **Use thread-safe `localtime_r` on POSIX:**
   Replaced `localtime()` with `localtime_r()` to avoid mutating shared static state during crash handling (matching `localtime_s` already used in the Windows path).
3. **Avoid heap allocation in `HandleExceptionGlobal` (Windows):**
   Allocated `CExceptionInformation_Impl` on the stack instead of using `new (std::nothrow)` so the crash handler does not touch a potentially corrupted process heap before writing the crash log. Also removed the now-unused `<new>` header.
4. **Clean up empty/corrupt `.dmp` files on failure (Windows):**
   If `TryWriteDump` fails after `CreateFile` has already created the file on disk, delete the leftover 0-byte/incomplete dump file via `DeleteFileA`.
### Summary of Changes
- **Fixed POSIX Stack Buffer Overflow:** Corrected `backtrace()` call in `SaveBacktraceSummary()` to pass element count (`NUMELMS(buffer)`) instead of byte size (`sizeof buffer`), preventing out-of-bounds writes on 64-bit Linux/macOS systems during deep-stack crashes.
- **Improved Windows Crash Handler Safety:** Allocated `CExceptionInformation_Impl` directly on the stack instead of using heap allocation (`new`), avoiding deadlocks or faults if the process heap is corrupted at the time of the exception.
- **Thread-Safe Time Formatting:** Replaced non-thread-safe `localtime()` with `localtime_r()` on POSIX platforms.
- **Dump Cleanup:** Added automatic deletion (`DeleteFileA`) of incomplete/0-byte `.dmp` files on Windows if `TryWriteDump` fails.
- **Code Formatting:** Adjusted spacing and alignment to fully comply with project `clang-format` standards.
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