Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unsoundness in msys_tty_on #183

Merged
merged 2 commits into from
Jan 9, 2024
Merged

Conversation

LingMan
Copy link
Contributor

@LingMan LingMan commented Aug 30, 2023

Two distinct problems here:

  1. Potential creation of an unaligned reference if a custom global allocator is used.
  2. Potential out of bounds read in some situations involving long filenames.

See the commit messages for details. Both fixes are ported from is-terminal.

The `name_info_bytes` Vec is only guaranteed to have an alignment of 1
(`std::mem::align_of::<u8>()`), however a raw pointer to it gets turned into a `&FILE_NAME_INFO`,
which has an alignment requirement of 4.
Creating unaligned references is immediate undefined behavior.

This bug was copied from `atty` and is tracked there as RUSTSEC-2021-0145 [1]. The fix in this
commit is taken from `atty`'s fork `is-terminal` [2], which was created since `atty` is
unmaintained.

Note that this code only gets compiled in on Windows and Windows' system allocator guarantees a
large enough alignment. Thus, in practice the pointer could only be unaligned if a custom global
allocator is used.

[1] https://rustsec.org/advisories/RUSTSEC-2021-0145.html
[2] sunfishcode/is-terminal@0c8d91d
In some situations involving long paths it is possible that `GetFileInformationByHandleEx` produces
a `FILE_NAME_INFO` with `FileNameLength` exceeding the provided buffer [1].
Return `false` from `msys_tty_on` if that happens, instead of reading beyond the end of the
allocation.

[1] sunfishcode/is-terminal#18
@mitsuhiko mitsuhiko merged commit ef2d442 into console-rs:master Jan 9, 2024
12 checks passed
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.

None yet

2 participants