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 undefined behavior when transmuting slices. #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

gnzlbg
Copy link

@gnzlbg gnzlbg commented Aug 31, 2018

The representation of slices is unspecified meaning that a mem::transmute is not
guaranteed to do the right thing although it currently does in practice so AFAICT
this currently is not a security vulnerability.

The fix is "simple": instead of transmuting the slices, we get the pointer and
the length of a slice, and use from_raw_parts to construct the other by
casting the pointer to the other pointer type. For this to work correctly, both
types have to have the same size, since otherwise, the length of the first slice
would be incorrect. Also, either both types have the same alignment, or the
original pointer is suitably aligned for the second type. Since the types here
are fixed and internal to the library, we assert these conditions using
debug_assert!s only. As long as these transmutes are tested, any changes in
layout that would introduce undefined behavior will be detected.

@gnzlbg
Copy link
Author

gnzlbg commented Aug 31, 2018

@carllerche I think the failure is spurious but I am not sure.

@gnzlbg
Copy link
Author

gnzlbg commented Aug 31, 2018

Ah yes, it appears so, merging #17 should fix the issues here.

@carllerche
Copy link
Owner

I merged #17. Would you be able to merge master into this branch?

The representation of slices is unspecified meaning that a mem::transmute is not
guaranteed to do the right thing although it currently does in practice so AFAICT
this currently is not a security vulnerability.

The fix is "simple": instead of transmuting the slices, we get the pointer and
the length of a slice, and use `from_raw_parts` to construct the other by
casting the pointer to the other pointer type. For this to work correctly, both
types have to have the same size, since otherwise, the length of the first slice
would be incorrect. Also, either both types have the same alignment, or the
original pointer is suitably aligned for the second type. Since the types here
are fixed and internal to the library, we assert these conditions using
`debug_assert!`s only. As long as these transmutes are tested, any changes in
layout that would introduce undefined behavior will be detected.
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