-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Slices that cover the last byte of the address space are invalid #83996
Comments
rust/library/alloc/src/vec/mod.rs Lines 2429 to 2439 in 010c236
rust/library/core/src/array/iter.rs Lines 26 to 33 in 010c236
|
@rustbot label A-iterators C-bug |
@rustbot label I-unsound 💥 |
Error: Label 💥 can only be set by Rust team members Please let |
An allocator must ensure that the one-past-the-end address of an allocation does not overflow. In other words, |
Yes, that would solve the issue! I can't seem to find it in the documentation, however. Should I change the labeling of this issue to make it a doc-bug? |
Yeah this could certainly be documented better. |
I think this has always been part of the implicit invariant for slices, but it would definitely be good to mention it explicitly in things like the documentation for https://doc.rust-lang.org/std/slice/fn.from_raw_parts.html#safety (The last bullet there is coming from the expectation that |
This is not really specific to slices though, it is a property of all "allocated objects" in Rust and the allocator must comply by it. |
We assume in lots of places that rust/library/core/src/slice/mod.rs Lines 496 to 517 in d408fdd
The documentation for rust/library/core/src/ptr/const_ptr.rs Lines 195 to 196 in d408fdd
The implementation of rust/library/core/src/slice/index.rs Lines 166 to 170 in d408fdd
|
Should that part of the allocator API contracts? |
Removing the @rustbot label -I-prioritize |
Yes, this effectively already is part of the allocator API contract. |
…Denton slice::from_raw_parts: mention no-wrap-around condition Cc rust-lang#83996. This probably needs to be mentioned in more places, so I am not closing that issue, but this here should help at least.
…Denton slice::from_raw_parts: mention no-wrap-around condition Cc rust-lang#83996. This probably needs to be mentioned in more places, so I am not closing that issue, but this here should help at least.
…Denton slice::from_raw_parts: mention no-wrap-around condition Cc rust-lang#83996. This probably needs to be mentioned in more places, so I am not closing that issue, but this here should help at least.
slice::from_raw_parts: mention no-wrap-around condition Cc rust-lang/rust#83996. This probably needs to be mentioned in more places, so I am not closing that issue, but this here should help at least.
The current implementation uses
pointer::add
to compute the end pointer for the bounds check:rust/library/core/src/slice/iter.rs
Lines 88 to 102 in 69e1d22
The method requires that the calculation will not overflow a
usize
, however that is not always the case. For instance, an allocator might return the last available page (0xfffff000
on x86) and correctly return a slice ofu8
(with size 4096 on x86). If a program now iterates over the slice, the end pointer will overflow, wrapping around the address space and thus creating UB.This behaviour is extremely unlikely and only occurs with
no_std
as most kernels reserve the higher half of the address space anyway.Solutions
The text was updated successfully, but these errors were encountered: