-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Validation: check raw wide pointer metadata #63880
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
92e75c0
factor wide ptr metadata checking into separate method
RalfJung 96baf1c
validate raw wide pointers
RalfJung 0c1cb32
test for too long slices
RalfJung ae78862
raw slices do not have to comply to the size limit
RalfJung 04580b6
adjust tests
RalfJung File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,15 +3,15 @@ use std::ops::RangeInclusive; | |
|
||
use syntax_pos::symbol::{sym, Symbol}; | ||
use rustc::hir; | ||
use rustc::ty::layout::{self, TyLayout, LayoutOf, VariantIdx}; | ||
use rustc::ty::layout::{self, Size, TyLayout, LayoutOf, VariantIdx}; | ||
use rustc::ty; | ||
use rustc_data_structures::fx::FxHashSet; | ||
|
||
use std::hash::Hash; | ||
|
||
use super::{ | ||
GlobalAlloc, InterpResult, | ||
OpTy, Machine, InterpCx, ValueVisitor, MPlaceTy, | ||
Scalar, OpTy, Machine, InterpCx, ValueVisitor, MPlaceTy, | ||
}; | ||
|
||
macro_rules! throw_validation_failure { | ||
|
@@ -250,6 +250,56 @@ impl<'rt, 'mir, 'tcx, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, 'tcx, M | |
self.path.truncate(path_len); | ||
Ok(()) | ||
} | ||
|
||
fn check_wide_ptr_meta( | ||
&mut self, | ||
meta: Option<Scalar<M::PointerTag>>, | ||
pointee: TyLayout<'tcx>, | ||
) -> InterpResult<'tcx> { | ||
let tail = self.ecx.tcx.struct_tail_erasing_lifetimes(pointee.ty, self.ecx.param_env); | ||
match tail.sty { | ||
ty::Dynamic(..) => { | ||
let vtable = meta.unwrap(); | ||
try_validation!( | ||
self.ecx.memory.check_ptr_access( | ||
vtable, | ||
3*self.ecx.tcx.data_layout.pointer_size, // drop, size, align | ||
self.ecx.tcx.data_layout.pointer_align.abi, | ||
), | ||
"dangling or unaligned vtable pointer in wide pointer or too small vtable", | ||
self.path | ||
); | ||
try_validation!(self.ecx.read_drop_type_from_vtable(vtable), | ||
"invalid drop fn in vtable", self.path); | ||
try_validation!(self.ecx.read_size_and_align_from_vtable(vtable), | ||
"invalid size or align in vtable", self.path); | ||
// FIXME: More checks for the vtable. | ||
} | ||
ty::Slice(..) | ty::Str => { | ||
let len = try_validation!(meta.unwrap().to_usize(self.ecx), | ||
"non-integer slice length in wide pointer", self.path); | ||
// check max slice length | ||
let elem_size = match tail.sty { | ||
ty::Str => Size::from_bytes(1), | ||
ty::Slice(ty) => self.ecx.layout_of(ty)?.size, | ||
_ => bug!("It cannot be another type"), | ||
}; | ||
if elem_size.checked_mul(len, &*self.ecx.tcx).is_none() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (offtopic) @oli-obk why doesn't |
||
throw_validation_failure!( | ||
"too large slice (longer than isize::MAX bytes)", | ||
self.path | ||
RalfJung marked this conversation as resolved.
Show resolved
Hide resolved
|
||
); | ||
} | ||
} | ||
ty::Foreign(..) => { | ||
// Unsized, but not wide. | ||
} | ||
_ => | ||
bug!("Unexpected unsized type tail: {:?}", tail), | ||
} | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
impl<'rt, 'mir, 'tcx, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M> | ||
|
@@ -341,56 +391,34 @@ impl<'rt, 'mir, 'tcx, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M> | |
} | ||
} | ||
ty::RawPtr(..) => { | ||
// Check pointer part. | ||
if self.ref_tracking_for_consts.is_some() { | ||
// Integers/floats in CTFE: For consistency with integers, we do not | ||
// accept undef. | ||
let _ptr = try_validation!(value.to_scalar_ptr(), | ||
"undefined address in raw pointer", self.path); | ||
let _meta = try_validation!(value.to_meta(), | ||
"uninitialized data in raw fat pointer metadata", self.path); | ||
} else { | ||
// Remain consistent with `usize`: Accept anything. | ||
} | ||
|
||
// Check metadata. | ||
let meta = try_validation!(value.to_meta(), | ||
"uninitialized data in wide pointer metadata", self.path); | ||
let layout = self.ecx.layout_of(value.layout.ty.builtin_deref(true).unwrap().ty)?; | ||
if layout.is_unsized() { | ||
self.check_wide_ptr_meta(meta, layout)?; | ||
} | ||
} | ||
_ if ty.is_box() || ty.is_region_ptr() => { | ||
// Handle fat pointers. | ||
// Handle wide pointers. | ||
// Check metadata early, for better diagnostics | ||
let ptr = try_validation!(value.to_scalar_ptr(), | ||
"undefined address in pointer", self.path); | ||
let meta = try_validation!(value.to_meta(), | ||
"uninitialized data in fat pointer metadata", self.path); | ||
"uninitialized data in wide pointer metadata", self.path); | ||
let layout = self.ecx.layout_of(value.layout.ty.builtin_deref(true).unwrap().ty)?; | ||
if layout.is_unsized() { | ||
let tail = self.ecx.tcx.struct_tail_erasing_lifetimes(layout.ty, | ||
self.ecx.param_env); | ||
match tail.sty { | ||
ty::Dynamic(..) => { | ||
let vtable = meta.unwrap(); | ||
try_validation!( | ||
self.ecx.memory.check_ptr_access( | ||
vtable, | ||
3*self.ecx.tcx.data_layout.pointer_size, // drop, size, align | ||
self.ecx.tcx.data_layout.pointer_align.abi, | ||
), | ||
"dangling or unaligned vtable pointer or too small vtable", | ||
self.path | ||
); | ||
try_validation!(self.ecx.read_drop_type_from_vtable(vtable), | ||
"invalid drop fn in vtable", self.path); | ||
try_validation!(self.ecx.read_size_and_align_from_vtable(vtable), | ||
"invalid size or align in vtable", self.path); | ||
// FIXME: More checks for the vtable. | ||
} | ||
ty::Slice(..) | ty::Str => { | ||
try_validation!(meta.unwrap().to_usize(self.ecx), | ||
"non-integer slice length in fat pointer", self.path); | ||
} | ||
ty::Foreign(..) => { | ||
// Unsized, but not fat. | ||
} | ||
_ => | ||
bug!("Unexpected unsized type tail: {:?}", tail), | ||
} | ||
self.check_wide_ptr_meta(meta, layout)?; | ||
} | ||
// Make sure this is dereferencable and all. | ||
let (size, align) = self.ecx.size_and_align_of(meta, layout)? | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.