Open
Description
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 of u8
(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
- Use wrapping_add instead, which avoids the UB, but might disable some optimizations
- Update the requirements for allocators so they aren't allowed to return the last bit of memory
- …