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

Misaligned pointer exposure in <String as TensorType>::unpack #416

Open
shinmao opened this issue Sep 27, 2023 · 1 comment
Open

Misaligned pointer exposure in <String as TensorType>::unpack #416

shinmao opened this issue Sep 27, 2023 · 1 comment
Labels

Comments

@shinmao
Copy link

shinmao commented Sep 27, 2023

The source of unsoundness

Hi, we found an unsound implementation in unpack which could create a misaligned pointer:

rust/src/lib.rs

Lines 900 to 903 in 62a3019

fn unpack(data: &[u8], count: usize) -> Result<Vec<Self>> {
let tstrings =
unsafe { slice::from_raw_parts(data.as_ptr() as *const tf::TF_TString, count) };
let mut out = Vec::with_capacity(count);

At line 902, the u8 pointer would cast to TF_TString which had stronger alignment requirements. The misaligned pointer passed to slice::from_raw_parts would break the safety guarantee. Please check and happy to have a discussion:)

@adamcrume adamcrume added the bug label Oct 5, 2023
@adamcrume
Copy link
Contributor

It's not released yet, but as of #418 the TensorType trait is sealed. I'd like to follow up by marking that function as unsafe and hiding it from the docs. It's not intended to be called by client code, and in the code path that uses it internally it simply uses data from the C FFI, which is presumably aligned.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants