inout 0.2.3: PaddedInOutBuf writes the padded tail block past a too-small output buffer (safe API)
Description
inout 0.2.3 (released 2026-10-14, #1558) dropped the output-buffer size check for the padded tail block in InOutBufReserved::into_padded_blocks. In 0.2.2 that function returned PadError when the reserved output space was smaller than the tail block. In 0.2.3 the check is gone, but PaddedInOutBuf::get_tail_block still writes through out_ptr.add(blocks_len) under a SAFETY comment that assumes the space exists. A caller whose output buffer is too small for the padded tail gets an out-of-bounds write through a safe API. cipher 0.5's encrypt_padded_b2b reaches exactly this path (InOutBufReserved::from_slices, then into_padded_blocks, then get_tail_block) and documents that it returns PadError if the output buffer is not sufficient.
Details
- 0.2.2
src/reserved.rs,into_padded_blocks:let reserve_len = out_len - blocks_byte_len; if reserve_len < tail_block.len() { return Err(PadError); } - 0.2.3
src/reserved.rs,into_padded_blocks: this check is removed;PaddedInOutBufnow storesout_ptr,blocks_lenandtail_block, and nothing compares againstout_len. - 0.2.3
get_tail_block:let tail_out_ptr = self.out_ptr.add(self.blocks_len); InOut::from_raw(in_block, tail_out_ptr). InOutBufReserved::from_slicesonly checksin.len() <= out.len(), so a buffer with no room for the padding passes it.
Suggested fix
Restore the reserve_len < tail_block.len() → PadError check in into_padded_blocks before building PaddedInOutBuf, and add a regression test with an output buffer one byte short of the padded length.
Proof of concept
Cargo.toml dependencies: inout = { version = "=0.2.3", features = ["block-padding"] }, block-padding = "=0.4.2", hybrid-array = "0.4".
use block_padding::Pkcs7;
use hybrid_array::sizes::U16;
// 15 message bytes pad to one 16-byte block. The output slice is the first 15
// bytes of a 32-byte buffer: one byte short. backing[15] lies outside the slice.
fn main() {
let msg = [0x11u8; 15];
let mut backing = [0u8; 32];
let reserved = inout::InOutBufReserved::from_slices(&msg, &mut backing[..15]).unwrap();
match reserved.into_padded_blocks::<Pkcs7, U16>() {
Err(_) => println!("PadError, refused"),
Ok(mut padded) => {
if let Some(mut tail) = padded.get_tail_block() {
*tail.get_out() = [0xAA; 16].into();
}
println!("Ok, tail written; backing[15] (outside the slice) = {:#04x}", backing[15]);
}
}
}Output (x86_64 Linux):
- inout 0.2.2:
PadError, refused - inout 0.2.3:
Ok, tail written; backing[15] (outside the slice) = 0xaa
Impact
users of inout 0.2.3 with the block-padding feature, which includes every user of cipher 0.5's padded block-mode helpers. BlockModeEncrypt::encrypt_padded (via InOutBufReserved::from_mut_slice) and encrypt_padded_b2b (via InOutBufReserved::from_slices) both call into_padded_blocks and then write the tail block through get_tail_block. Both constructors only check that the output buffer is at least as long as the message, not that it has room for the padding.