Skip to content

Commit 6d99e1c

Browse files
authored
util: prevent read buffer from being swapped during a read_poll (#2993)
1 parent f73a2ad commit 6d99e1c

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

tokio-util/src/io/poll_read_buf.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,25 @@ where
5858

5959
let n = {
6060
let mut buf = ReadBuf::uninit(buf.bytes_mut());
61+
let before = buf.filled().as_ptr();
62+
6163
ready!(read.poll_read(cx, &mut buf)?);
64+
65+
// This prevents a malicious read implementation from swapping out the
66+
// buffer being read, which would allow `filled` to be advanced without
67+
// actually initializing the provided buffer.
68+
//
69+
// We avoid this by asserting that the `ReadBuf` instance wraps the same
70+
// memory address both before and after the poll. Which will panic in
71+
// case its swapped.
72+
//
73+
// See https://github.com/tokio-rs/tokio/issues/2827 for more info.
74+
assert! {
75+
std::ptr::eq(before, buf.filled().as_ptr()),
76+
"Read buffer must not be changed during a read poll. \
77+
See https://github.com/tokio-rs/tokio/issues/2827 for more info."
78+
};
79+
6280
buf.filled().len()
6381
};
6482

0 commit comments

Comments
 (0)