Skip to content

Fix one-byte overflow reading odd-sized REG_SZ values into string types - #677

Open
Keith Horton (keith-horton) wants to merge 13 commits into
microsoft:masterfrom
keith-horton:registry-odd-size-string-overflow
Open

Keith Horton (keith-horton) wants to merge 13 commits into
microsoft:masterfrom
keith-horton:registry-odd-size-string-overflow

Conversation

@keith-horton

Copy link
Copy Markdown
Member

Problem

RegGetValueW reports the size needed for an odd-sized REG_SZ value (for example, a 23-byte value reports 25 bytes). RegSetValueExW accepts odd byte counts, so these values can exist. WIL's string types can only allocate whole WCHARs (24 bytes here). After resizing, get_value_with_type advertised the requested 25 bytes instead of the 24 actually allocated. If the value grew between the size query and the read, RegGetValueW copied 25 bytes into the 24-byte buffer before returning ERROR_MORE_DATA. That is a one-byte heap overflow, confirmed with AddressSanitizer.

Affected: BSTR, unique_bstr, shared_bstr, unique_cotaskmem_string, shared_cotaskmem_string, unique_process_heap_string. For std::wstring, the extra byte landed in the string's null-terminator slot: still inside the allocation, but undefined behavior.

Fix

After a resize, advertise only the bytes actually allocated. A new resized_buffer_size_bytes<T> trait rounds down to whole WCHARs for the string types and is a pass-through for byte buffers. Odd byte counts are not rounded up, because the extra byte is not a full UTF-16 character.

Test

BasicRegistryTests::odd_size_string_growing_during_read intercepts RegGetValueW to grow a 23-byte REG_SZ to 25 bytes between WIL's size query and its read. It checks the exact byte count WIL advertises for unique_bstr, unique_cotaskmem_string and std::wstring.

  • Without the fix: every section fails (25 == 24), and the ASan build reports heap-buffer-overflow.
  • With the fix: all [registry] tests pass on the normal, noexcept, cpplatest, win7 and ASan builds.

RegGetValueW reports the size needed for an odd-sized REG_SZ value
(for example 23 bytes reports 25), but WIL's string types can only
allocate whole WCHARs (24 bytes). After resizing, get_value_with_type
advertised the requested byte count instead of the allocated count. If
the value grew between calls, RegGetValueW could write one byte past
the buffer before returning ERROR_MORE_DATA.

Advertise only the bytes actually allocated for BSTR, unique_bstr,
shared_bstr, std::wstring, unique_cotaskmem_string,
shared_cotaskmem_string and unique_process_heap_string. Add a regression
test that grows an odd-sized REG_SZ between WIL's size query and its
read, and verifies the exact advertised byte count.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 29d5903e-abc5-4c41-92ee-389b67299477
Exercise get_value_expanded_string_nothrow (unique_bstr, unique_cotaskmem_string) and the std::wstring expanded read path against values stored as both REG_SZ and REG_EXPAND_SZ. The expanded readers accept REG_SZ, which reports odd sizes and is vulnerable without the fix; REG_EXPAND_SZ is reported at its expanded (even) size.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 29d5903e-abc5-4c41-92ee-389b67299477
Grow an odd-sized REG_MULTI_SZ during the read and verify the byte buffers used by the multi-string readers advertise exactly what they allocate, plus end-to-end get_value_multistring and get_value_multistring_nothrow parsing (validated under ASan).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 29d5903e-abc5-4c41-92ee-389b67299477
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 29d5903e-abc5-4c41-92ee-389b67299477
Comment thread include/wil/registry_helpers.h Outdated
}
constexpr DWORD resized_wchar_buffer_size_bytes(DWORD requestedBytes) WI_NOEXCEPT
{
return requestedBytes - (requestedBytes % sizeof(wchar_t));

@dunhor Duncan Horn (dunhor) Nov 13, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's say some byte size is odd. What's the behavior then? Seems like we'd never end up reading the full buffer and would spin infinitely in the loop. E.g. it returns size 5, we allocate 4, try again, it says that the size is 5, we allocate 4, etc. Or is there something to terminate the loop? IMO it might be better to resize with (bytes + 1) / sizeof(wchar_t). Or equivalently if you want to be more general (bytes + sizeof(wchar_t) - 1) / sizeof(wchar_t)

Signed-off-by: Keith Horton <khorton@microsoft.com>
…om conditions

Signed-off-by: Keith Horton <khorton@microsoft.com>
Port of the OS test updates: BSTR comparisons now check SysStringByteLen and
compare counted UTF-16 (including embedded NULs) instead of wcscmp. Adds
orphan-byte, odd embedded-null, and multiple trailing-null three-parser
scenarios, and a validation read in the ExampleUsage test.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…ze queries

- Hook sized operator delete as well as unsized: ASAN's sized delete does not forward to the unsized overload.
- Write even-sized unterminated string payloads from a padded copy so RegSetValueExW does not append a NUL based on uninitialized memory past cbData.
- Make the three_parser_vector_parse_failures fake return ERROR_SUCCESS for null-buffer size queries, matching the real RegGetValueW.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…ailing byte

Stores { 'h', 0, 'i', 0, 0, 0, '!' } as REG_SZ/REG_EXPAND_SZ and verifies RegGetValueW returns all 7 raw bytes (an odd count) for 7- to 10-byte buffers, rejects 6, and that WIL string readers return "hi".

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
… test flake

Add the embedded-null-aligned-null-trailing-byte string scenario
(L"abc\0def\0A", 17 bytes) so the three-parser payload, raw and fixed
routes cover a value with an interior NUL whose last whole WCHAR is NUL
followed by an extra odd byte.

Fix an intermittent failure in allocation_shared_bstr_resize_oom
(payloadFrees 2 == 1). Reproduced under ASAN with cdb: the second
counted free was RaiseRoOriginateOnWilExceptions releasing a
GetErrorDetails description BSTR, left by a stale restricted error from
an earlier test, that the heap had placed at the payload's just-freed
address. Call RoClearError() before arming so the count stays exact and
genuine double frees are still detected. Verified with 325 consecutive
full ASAN [registry] runs under cdb without a failure.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Restores registry.h/registry_helpers.h to the REG_SZ-only fix and removes the OOM tests and helpers, adapting the remaining tests to the original walk_multistring and get_cotaskmemstring_array_from_multistring_nothrow APIs.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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