-
Notifications
You must be signed in to change notification settings - Fork 3.3k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Rust: read_scalar(_at) is unsound #5825
Comments
Thanks for finding this and for recommending solutions! Do you think that the way we use this in generated code is unsound? It seems that we use it correctly in generated code; as such, adopting your solution of marking these functions as unsafe is the way to go. I'll note that these functions are part of the public API but we don't expect anyone to use them manually, due to the code generation process flatc uses. |
Perhaps we could go all-in on zero-unsafe and interpret these values manually, without unsafe at all, like we do in Go: https://github.com/google/flatbuffers/blob/master/go/encode.go#L55 |
Looking at rustsec/advisory-db#281, it seems that these functions also allow doing unaligned memory reads. Looking at Regarding the generated code, looking at: flatbuffers/tests/include_test/sub/include_test2_generated.rs Lines 50 to 63 in 6da1cf7
Besides allowing to read invalid enum values (with read_scalar ), it also allows swapping the byte order of enums in big endian machines.
I would go for zero-unsafe code. Reading and writing integers can be done as I suggested, and enums can use I also think that it would great to get rid of |
#6393 claims to have fixed this issue. Is that correct? Did it fix this issue? |
It didn't. I left some comments there. |
iirc, the consensus was that we should mark it as unsafe due to the lack of bounds checking (which is done in the verifier). |
What's the intended (public API) usage of the 2 read_scalar and the emplace_scalar functions? Are they not something that should be private/internal? @eduardosm Could you explain how emplace_scalar allows unaligned writes? (I only see it writing to the &mut [u8] and afaik that has no alignment requirements). It does have problems for types that can only be represented as T in native endiannes, but that would be fixed by Casper's I see another problem with Example: fn main() {
flatbuffers::emplace_scalar(&mut [], 0u8);
} |
Its meant to be internal. Like most of the
Yep, I think that's the plan, though no one is working on that atm. |
With the [release of flatbuffers 2.0.0](https://github.com/google/flatbuffers/releases/tag/v2.0.0) google/flatbuffers#5825 has been fixed and released. This patch updates the advisory-db to indicate a patched version addressing the issue.
With the [release of flatbuffers 2.0.0](https://github.com/google/flatbuffers/releases/tag/v2.0.0) google/flatbuffers#5825 has been fixed and released. This patch updates the advisory-db to indicate a patched version addressing the issue.
The
read_scalar
andread_scalar_at
functions are unsound because the allow to do things such as:or
or even worse:
(this last one causes a segmentation fault)
read_scalar
is behaving similar totransmute
, leading to undefined behavior for types where not all bit patterns are valid (such asbool
orNonZero*
) or allowing to create dangling pointers.I suggest three breaking solutions:
read_scalar
andread_scalar_at
functions unsafe.EndianScalar
trait unsafe.EndianScalar
trait something like:which, for example, would be implemented for
u32
like this:This last solution, even though it is the most disrupting one, has the advantage of not requiring any
unsafe
at all.The text was updated successfully, but these errors were encountered: