Skip to content

Zero the OVERLAPPED passed to LockFileEx in windows_named_sync - #286

Open
juanlofer-eprosima wants to merge 1 commit into
boostorg:developfrom
juanlofer-eprosima:fix-windows-named-sync-overlapped
Open

juanlofer-eprosima wants to merge 1 commit into
boostorg:developfrom
juanlofer-eprosima:fix-windows-named-sync-overlapped

Conversation

@juanlofer-eprosima

Copy link
Copy Markdown

windows_named_sync::open_or_create() and windows_named_sync::close() pass an uninitialized winapi::interprocess_overlapped to LockFileEx (and open_or_create() also to UnlockFileEx). LockFileEx takes the start of the range to lock from Offset/OffsetHigh, and its documentation requires hEvent to be zero or a valid handle. So the byte that gets locked is whatever was left on the stack. This has been the case since windows_named_sync was added (Boost 1.50), and it still is in the latest releases and develop branch.

Effect

The lock is meant to make processes take turns while they set up or read the file behind a named object. That file holds the id that names the native object, and the counts that named_semaphore and named_condition_any write in close() and read back in open_or_create(). With an arbitrary start, two processes can lock different bytes, and then they don't wait for each other. From reading the code (not reproduced): two processes creating the same object at the same time could both find the file empty, write different ids, and end up using different native objects for the same name.

How we found it

We hit this bug in one of our products testing suite, which still uses Boost 1.67 (the lock covered sizeof_file_info bytes instead of 1); Offset/OffsetHigh held 0xFFFFFFFFFFFFFFFF, so the range ran past 2^64, LockFileEx failed with ERROR_INVALID_LOCK_RANGE, and the named_mutex constructor threw. Zeroing the structure fixed it. Since 1.77 the range is 1 byte long and can't overflow (we measured that LockFileEx accepts a range ending exactly at 2^64), so that error no longer shows, but the locked byte is still arbitrary.

🤖 Generated with Claude Code

Signed-off-by: Juan Lopez Fernandez <juanlopez@eprosima.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.

1 participant