mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-08-29 00:00:11 +02:00
util: prevent read buffer from being swapped during a read_poll (#2993)
This commit is contained in:
@@ -58,7 +58,25 @@ where
|
|||||||
|
|
||||||
let n = {
|
let n = {
|
||||||
let mut buf = ReadBuf::uninit(buf.bytes_mut());
|
let mut buf = ReadBuf::uninit(buf.bytes_mut());
|
||||||
|
let before = buf.filled().as_ptr();
|
||||||
|
|
||||||
ready!(read.poll_read(cx, &mut buf)?);
|
ready!(read.poll_read(cx, &mut buf)?);
|
||||||
|
|
||||||
|
// This prevents a malicious read implementation from swapping out the
|
||||||
|
// buffer being read, which would allow `filled` to be advanced without
|
||||||
|
// actually initializing the provided buffer.
|
||||||
|
//
|
||||||
|
// We avoid this by asserting that the `ReadBuf` instance wraps the same
|
||||||
|
// memory address both before and after the poll. Which will panic in
|
||||||
|
// case its swapped.
|
||||||
|
//
|
||||||
|
// See https://github.com/tokio-rs/tokio/issues/2827 for more info.
|
||||||
|
assert! {
|
||||||
|
std::ptr::eq(before, buf.filled().as_ptr()),
|
||||||
|
"Read buffer must not be changed during a read poll. \
|
||||||
|
See https://github.com/tokio-rs/tokio/issues/2827 for more info."
|
||||||
|
};
|
||||||
|
|
||||||
buf.filled().len()
|
buf.filled().len()
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user