Repository navigation
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
Open
Keith Horton (keith-horton) wants to merge 13 commits into
Keith Horton (keith-horton) wants to merge 13 commits into
Conversation
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
| } | ||
| constexpr DWORD resized_wchar_buffer_size_bytes(DWORD requestedBytes) WI_NOEXCEPT | ||
| { | ||
| return requestedBytes - (requestedBytes % sizeof(wchar_t)); |
Member
There was a problem hiding this comment.
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
RegGetValueWreports the size needed for an odd-sizedREG_SZvalue (for example, a 23-byte value reports 25 bytes).RegSetValueExWaccepts 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_typeadvertised the requested 25 bytes instead of the 24 actually allocated. If the value grew between the size query and the read,RegGetValueWcopied 25 bytes into the 24-byte buffer before returningERROR_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. Forstd::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_readinterceptsRegGetValueWto grow a 23-byteREG_SZto 25 bytes between WIL's size query and its read. It checks the exact byte count WIL advertises forunique_bstr,unique_cotaskmem_stringandstd::wstring.25 == 24), and the ASan build reportsheap-buffer-overflow.[registry]tests pass on the normal, noexcept, cpplatest, win7 and ASan builds.