Skip to content

Commit f7c6e0e

Browse files
committed
Do additional bounds checks
1 parent ab05967 commit f7c6e0e

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

src/helpers.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -356,12 +356,16 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
356356
let len = bytes.len();
357357
// If `size` is smaller or equal than `bytes.len()`, writing `bytes` plus the required null
358358
// terminator to memory using the `ptr` pointer would cause an overflow.
359-
if size <= bytes.len() as u64 {
359+
if size <= len as u64 {
360360
throw_unsup_format!("OsString of length {} is too large for destination buffer of size {}", len, size)
361361
}
362-
362+
let actual_len = (len as u64)
363+
.checked_add(1)
364+
.map(Size::from_bytes)
365+
.ok_or_else(|| err_unsup_format!("OsString of length {} is too large", len))?;
363366
let this = self.eval_context_mut();
364-
let buffer = this.memory.get_mut(ptr.alloc_id)?.get_bytes_mut(&*this.tcx, ptr, Size::from_bytes(len as u64 + 1))?;
367+
this.memory.check_ptr_access(ptr.into(), actual_len, Align::from_bytes(1).unwrap())?;
368+
let buffer = this.memory.get_mut(ptr.alloc_id)?.get_bytes_mut(&*this.tcx, ptr, actual_len)?;
365369
buffer[..len].copy_from_slice(bytes);
366370
// This is ok because the buffer was strictly larger than `bytes`, so after adding the
367371
// null terminator, the buffer size is larger or equal to `bytes.len()`, meaning that

0 commit comments

Comments
 (0)