Fix undefined behavior in StartElevated stderr read loop (#1550)#1687
Merged
Conversation
) The read loop that captures stderr from the sudo child process used `vector<char> buffer(4096); buffer.clear();` followed by `read(fd, &buffer[0], buffer.capacity())`. This has two instances of undefined behavior: 1. `operator[](0)` on a vector with `size() == 0` violates the C++ standard precondition `n < size()`. libstdc++ built with `-D_GLIBCXX_ASSERTIONS` aborts the process with: stl_vector.h:1128: Assertion '__n < this->size()' failed. 2. `buffer.begin() + bytesRead` on the same empty vector constructs an iterator past `end()`, also UB. `-D_GLIBCXX_ASSERTIONS` is in the default build flags of Arch Linux, Fedora, and several other distributions. On those systems, the unprivileged helper process aborts as soon as sudo writes anything to stderr (a password prompt, a 'user is not in the sudoers file' error, etc.). The main process then sees EOF on the service output pipe, and throws `InsufficientData`, which renders to the user as 'Not enough data available'. A second mount attempt fails at `File::Write` because the helper is dead and the pipe is broken, producing the bare message 'VeraCrypt::File::Write:395'. Fix by replacing `buffer` with a plain `char[4096]` and using `reserve(4096)` on `errOutput` to preserve the original pre-allocation intent. No behavioral change on systems where the UB happened to work; aborts are eliminated on systems where the assertions fire. Reported-by: multiple users, see veracrypt#1550, veracrypt#1446, veracrypt#844
Member
|
Good catch! |
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.
The read loop that captures stderr from the sudo child process used
vector<char> buffer(4096); buffer.clear();followed byread(fd, &buffer[0], buffer.capacity()). This has two instances of undefined behavior:operator[](0)on a vector withsize() == 0violates the C++ standard preconditionn < size(). libstdc++ built with-D_GLIBCXX_ASSERTIONSaborts the process with:stl_vector.h:1128: Assertion '__n < this->size()' failed.
buffer.begin() + bytesReadon the same empty vector constructs an iterator pastend(), also UB.-D_GLIBCXX_ASSERTIONSis in the default build flags of Arch Linux, Fedora, and several other distributions. On those systems, the unprivileged helper process aborts as soon as sudo writes anything to stderr (a password prompt, a 'user is not in the sudoers file' error, etc.). The main process then sees EOF on the service output pipe, and throwsInsufficientData, which renders to the user as 'Not enough data available'. A second mount attempt fails atFile::Writebecause the helper is dead and the pipe is broken, producing the bare message 'VeraCrypt::File::Write:395'.Fix by replacing
bufferwith a plainchar[4096]and usingreserve(4096)onerrOutputto preserve the original pre-allocation intent. No behavioral change on systems where the UB happened to work; aborts are eliminated on systems where the assertions fire.Reported-by: multiple users
fixes #1550 #1446 #844