Conhost crash due to corrupted ConsoleWaitBlock::_WaitReplyMessage.msgHeader.ApiNumber #13447

Open
opened 2026-01-31 03:42:57 +00:00 by claunia · 0 comments
Owner

Originally created by @emmett-b on GitHub (Apr 15, 2021).

Windows Terminal version (or Windows build number)

Windows Terminal version 1.6.10571.0
Windows version 21343.1000

Other Software

#include <Windows.h>
#include <vector>

int main() {

    const auto hConOut = GetStdHandle(STD_OUTPUT_HANDLE);
    const auto hConIn = GetStdHandle(STD_INPUT_HANDLE);

    if (!SetConsoleMode(hConOut, 0)) return 1;
    if (!SetConsoleMode(hConIn , 0)) return 1;

    auto buf = std::vector<char>();

    for (;;) {
        buf.resize(1000000);
        DWORD nCharRead;
        if (!ReadConsoleA(hConIn, buf.data(), DWORD(buf.size()), &nCharRead, NULL)) return 1;
        buf.resize(nCharRead);

        DWORD nCharWritten;
        if (!WriteConsoleA(hConOut, buf.data(), DWORD(buf.size()), &nCharWritten, NULL)) return 1;
    }
}

Steps to reproduce

Run the above "messaround" program with a debugger:
image

Smash your keyboard quickly and randomly. If OpenConsole doesn't crash after ~20 keystrokes, stop the program and retry.
Usually, after less than 20 tries, I get a crash.

The crash comes from _WaitReplyMessage.msgHeader.ApiNumber being corrupted:
image

There's usually another thread doing some memory management when this break point is hit:
image

It looks like a use-after-free of the ConsoleWaitBlock in the ConsoleWaitQueue.

Expected Behavior

no crash

Actual Behavior

crash

Originally created by @emmett-b on GitHub (Apr 15, 2021). ### Windows Terminal version (or Windows build number) Windows Terminal version 1.6.10571.0 Windows version 21343.1000 ### Other Software #include <Windows.h> #include <vector> int main() { const auto hConOut = GetStdHandle(STD_OUTPUT_HANDLE); const auto hConIn = GetStdHandle(STD_INPUT_HANDLE); if (!SetConsoleMode(hConOut, 0)) return 1; if (!SetConsoleMode(hConIn , 0)) return 1; auto buf = std::vector<char>(); for (;;) { buf.resize(1000000); DWORD nCharRead; if (!ReadConsoleA(hConIn, buf.data(), DWORD(buf.size()), &nCharRead, NULL)) return 1; buf.resize(nCharRead); DWORD nCharWritten; if (!WriteConsoleA(hConOut, buf.data(), DWORD(buf.size()), &nCharWritten, NULL)) return 1; } } ### Steps to reproduce Run the above "messaround" program with a debugger: ![image](https://user-images.githubusercontent.com/28929018/114794942-c9694100-9d8d-11eb-8eff-dbeca8573554.png) Smash your keyboard quickly and randomly. If OpenConsole doesn't crash after ~20 keystrokes, stop the program and retry. Usually, after less than 20 tries, I get a crash. The crash comes from `_WaitReplyMessage.msgHeader.ApiNumber` being corrupted: ![image](https://user-images.githubusercontent.com/28929018/114795069-16e5ae00-9d8e-11eb-9862-b7630c4d0d82.png) There's usually another thread doing some memory management when this break point is hit: ![image](https://user-images.githubusercontent.com/28929018/114795199-4eecf100-9d8e-11eb-9d18-245a5c0c0fb2.png) It looks like a use-after-free of the `ConsoleWaitBlock` in the `ConsoleWaitQueue`. ### Expected Behavior no crash ### Actual Behavior crash
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#13447