inout: safe-API aliasing UB on in-place buffers in InOutBufReserved::split_reserved and into_padded_blocks (0.2.x)
Description
Two related pointer-provenance bugs in inout 0.2.x make ordinary safe code on in-place buffers (InOutBufReserved::from_mut_slice) undefined behaviour under both Stacked Borrows and Tree Borrows:
split_reserved(all 0.2.x) derives the body'sin_ptrfrom aget_in()shared reborrow and then takes aget_out()unique reborrow of the same bytes, which invalidatesin_ptr.into_padded_blocks(0.2.3-0.2.5) storesin_ptr: blocks.as_ptr(), a read-only reborrow, so any in-place write-then-read throughget_blocks()is UB. This was introduced by the fix for #1556.
Details
inout-0.2.5/src/reserved.rs:109-110:
``rust let in_ptr = self.get_in().as_ptr(); let out_ptr = self.get_out().as_mut_ptr(); ``
For from_mut_slice, input and output alias. The get_out() unique retag kills in_ptr's tag (Stacked Borrows), or freezes it so the first output write disables it (Tree Borrows). The UB fires at inout_buf.rs:119 (get_in), inout.rs:28 and inout_buf.rs:276 (xor_in2out).
inout-0.2.5/src/reserved.rs:208:in_ptr: blocks.as_ptr(), whereblocks = P::pad_detached(self.get_in()). The value is consumed byget_blocks(reserved.rs:236→InOutBuf::from_raw). The UB fires atinout.rs:28andinout_buf.rs:119. 0.2.2 is clean for this pattern; it only had the separate #1556 bug.
Verified fix for both, with every PoC mode clean in SB and TB and inout's own tests passing under Miri:
- reuse the stored raw pointers:
let in_ptr = self.in_ptr; let out_ptr = self.out_ptr;; - in
into_padded_blocks, usein_ptr: self.in_ptr.cast()after the existing checks.
The code is unchanged on master (ddefdca).
Proof of concept
Cargo.toml:
[package]
name = "inout-aliasing-poc"
version = "0.1.0"
edition = "2021"
[dependencies]
inout = { version = "=0.2.5", features = ["block-padding"] }
hybrid-array = "=0.4.15"src/main.rs:
#![forbid(unsafe_code)]
use hybrid_array::sizes::U16;
use inout::{block_padding::Pkcs7, InOutBufReserved};
fn main() {
// (1) split_reserved: message||tag layout, in place
let mut buf = [0x11u8; 32];
let mut r = InOutBufReserved::from_mut_slice(&mut buf, 16).unwrap();
let (mut body, _tag) = r.split_reserved();
for i in 0..body.len() {
let x = body.get_in()[i]; // SB: UB on the first read; TB: UB on the read after a write
body.get_out()[i] = x ^ 0x5A;
}
// (2) into_padded_blocks: in-place write-then-read on a block
let mut buf2 = [0x11u8; 48];
let r2 = InOutBufReserved::from_mut_slice(&mut buf2, 32).unwrap();
let mut p = r2.into_padded_blocks::<Pkcs7, U16>().unwrap();
let mut blocks = p.get_blocks();
let mut b0 = blocks.get(0);
b0.get_out()[0] = 0xAA;
let _ = b0.get_in()[0]; // UB in SB and TB
}rustup toolchain install nightly --component miri
cargo +nightly miri run # Stacked Borrows
MIRIFLAGS=-Zmiri-tree-borrows cargo +nightly miri run # Tree BorrowsMiri output (0.2.5):
error: Undefined Behavior: trying to retag from <1555> for SharedReadOnly permission at alloc673[0x0],
but that tag does not exist in the borrow stack for this location
--> inout-0.2.5/src/inout_buf.rs:119:18
(tag created at split_reserved; later invalidated by a Unique retag)
error: Undefined Behavior: reborrow through <1506> at alloc673[0x0] is forbidden
(Tree Borrows: tag Disabled due to a foreign write access)
error: Undefined Behavior: trying to retag from <2518> for SharedReadOnly ... created at into_padded_blocks
--> inout-0.2.5/src/inout.rs:28:18Control: the same loops on InOutBuf::from(&mut [u8]) are clean in SB and TB. A realistic case (ChaCha20::apply_keystream_inout(body) on a split_reserved body, chacha20 0.10.2 / cipher 0.5.2) is also Stacked Borrows UB inside inout's own xor_in2out.
Impact
Undefined behaviour reachable from the safe public API whenever a caller works in place on a reserved buffer. The miscompilation risk is the usual one for aliasing UB: the optimizer may reorder or elide reads of the input after output writes.
- (1) hits the message||tag in-place layout
split_reservedexists for. - (2) is not triggered by RustCrypto's own block-mode backends, which read each block before writing it, but it is triggered by direct users and by third-party modes that do the natural write-then-read.
Same class as #1556 (fixed as a plain release); (2) is a regression from that fix.