valpackett / systemstat

Rust library for getting system information | also on https://codeberg.org/valpackett/systemstat
https://crates.io/crates/systemstat
The Unlicense
610 stars 71 forks source link

Potential unsound implementation in `parse_addr` #120

Closed shinmao closed 10 months ago

shinmao commented 10 months ago

Hi, we are the researchers from SunLab, we found that parse_addr might have unsound implementation with our bug detector. https://github.com/valpackett/systemstat/blob/cbd9c1638b792d1819479f0c2baa5840f65af727/src/platform/unix.rs#L53-L55 aptr is aligned to 2 bytes while sockaddr_in6 is aligned to 4 bytes. Type conversion with transmute could create a misaligned pointer. In following line, the misaligned pointer dereference happens with *addr6.

Maybe that's also what you mean by horrible? (:

valpackett commented 10 months ago

I meant that usage of transmute was required in general :)

There is no other way to implement this conversion (directly, not considering going through string representations which would be slow); there is no actual safety problem here.

This parsing is only ever done on data returned from libc, so we know that addr.sa_family == AF_INET6 guarantees that the *const sockaddr was actually a cast from *const sockaddr_in6. There's no compile time guarantee because, well, stupid unix history reasons.