-
-
Notifications
You must be signed in to change notification settings - Fork 15.2k
cmse: clear padding when crossing the secure boundary #157397
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 all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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
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 |
|---|---|---|
| @@ -1,6 +1,9 @@ | ||
| use std::cmp; | ||
| use std::ops::Range; | ||
|
|
||
| use rustc_abi::{Align, BackendRepr, ExternAbi, HasDataLayout, Reg, Size, WrappingRange}; | ||
| use rustc_abi::{ | ||
| Align, ArmCall, BackendRepr, CanonAbi, ExternAbi, HasDataLayout, Reg, Size, WrappingRange, | ||
| }; | ||
| use rustc_ast as ast; | ||
| use rustc_ast::{InlineAsmOptions, InlineAsmTemplatePiece}; | ||
| use rustc_data_structures::packed::Pu128; | ||
|
|
@@ -597,6 +600,20 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { | |
| } | ||
| ZeroSized => bug!("ZST return value shouldn't be in PassMode::Cast"), | ||
| }; | ||
|
|
||
| if self.fn_abi.conv == CanonAbi::Arm(ArmCall::CCmseNonSecureEntry) { | ||
| // The return value of an `extern "cmse-nonsecure-entry"` function crosses the | ||
| // secure boundary. Zero padding bytes so information does not leak. | ||
| // | ||
| // This only zeroes "guaranteed" padding. There may be more bytes that are | ||
| // padding for some but not all variants of this type; those are not zeroed. | ||
| // | ||
| // Returning a value with value-dependent padding will instead trigger a lint. | ||
| let ret_layout = self.fn_abi.ret.layout; | ||
| let uninit_ranges = ret_layout.padding_ranges(bx.cx()); | ||
| self.zero_byte_ranges(bx, llslot, ret_layout.size, &uninit_ranges); | ||
| } | ||
|
|
||
| load_cast(bx, cast_ty, llslot, self.fn_abi.ret.layout.align.abi) | ||
| } | ||
| }; | ||
|
|
@@ -1341,6 +1358,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { | |
|
|
||
| self.codegen_argument( | ||
| bx, | ||
| fn_abi.conv, | ||
| op, | ||
| by_move, | ||
| &mut llargs, | ||
|
|
@@ -1351,6 +1369,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { | |
| let num_untupled = untuple.map(|tup| { | ||
| self.codegen_arguments_untupled( | ||
| bx, | ||
| fn_abi.conv, | ||
| &tup.node, | ||
| &mut llargs, | ||
| &fn_abi.args[first_args.len()..], | ||
|
|
@@ -1380,6 +1399,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { | |
| let last_arg = fn_abi.args.last().unwrap(); | ||
| self.codegen_argument( | ||
| bx, | ||
| fn_abi.conv, | ||
| location, | ||
| /* by_move */ false, | ||
| &mut llargs, | ||
|
|
@@ -1696,9 +1716,31 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { | |
| } | ||
| } | ||
|
|
||
| fn zero_byte_ranges( | ||
| &mut self, | ||
| bx: &mut Bx, | ||
| ptr: Bx::Value, | ||
| limit: Size, | ||
| ranges: &[Range<Size>], | ||
| ) { | ||
| let zero = bx.const_u8(0); | ||
|
|
||
| for range in ranges { | ||
| let end = cmp::min(range.end, limit); | ||
| if range.start >= end { | ||
| continue; | ||
| } | ||
| let offset = bx.const_usize(range.start.bytes()); | ||
| let len = bx.const_usize((end - range.start).bytes()); | ||
| let ptr = bx.inbounds_ptradd(ptr, offset); | ||
| bx.memset(ptr, zero, len, Align::ONE, MemFlags::empty()); | ||
| } | ||
| } | ||
|
|
||
| fn codegen_argument( | ||
| &mut self, | ||
| bx: &mut Bx, | ||
| conv: CanonAbi, | ||
| op: OperandRef<'tcx, Bx::Value>, | ||
| by_move: bool, | ||
| llargs: &mut Vec<Bx::Value>, | ||
|
|
@@ -1822,6 +1864,23 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { | |
| MemFlags::empty(), | ||
| None, | ||
| ); | ||
|
|
||
| // The arguments of an `extern "cmse-nonsecure-call"` function cross the secure | ||
| // boundary. Zero padding bytes so information does not leak. | ||
|
Member
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. This should at least acknowledge that value-dependent padding is ignored, and reference somewhere that explains why that is okay. |
||
| // | ||
| // This only zeroes "guaranteed" padding. There may be more bytes that are | ||
| // padding for some but not all variants of this type; those are not zeroed. | ||
| // | ||
| // Passing an argument with value-dependent padding will instead trigger a lint. | ||
| if conv == CanonAbi::Arm(ArmCall::CCmseNonSecureCall) { | ||
| self.zero_byte_ranges( | ||
| bx, | ||
| llscratch, | ||
| Size::from_bytes(copy_bytes), | ||
| &arg.layout.padding_ranges(bx.cx()), | ||
| ); | ||
| } | ||
|
|
||
| // ...and then load it with the ABI type. | ||
| llval = load_cast(bx, cast, llscratch, scratch_align); | ||
| bx.lifetime_end(llscratch, scratch_size); | ||
|
|
@@ -1848,6 +1907,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { | |
| fn codegen_arguments_untupled( | ||
| &mut self, | ||
| bx: &mut Bx, | ||
| conv: CanonAbi, | ||
| operand: &mir::Operand<'tcx>, | ||
| llargs: &mut Vec<Bx::Value>, | ||
| args: &[ArgAbi<'tcx, Ty<'tcx>>], | ||
|
|
@@ -1867,6 +1927,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { | |
| let field = bx.load_operand(field_ptr); | ||
| self.codegen_argument( | ||
| bx, | ||
| conv, | ||
| field, | ||
| by_move, | ||
| llargs, | ||
|
|
@@ -1878,7 +1939,15 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { | |
| // If the tuple is immediate, the elements are as well. | ||
| for i in 0..tuple.layout.fields.count() { | ||
| let op = tuple.extract_field(self, bx, i); | ||
| self.codegen_argument(bx, op, by_move, llargs, &args[i], lifetime_ends_after_call); | ||
| self.codegen_argument( | ||
| bx, | ||
| conv, | ||
| op, | ||
| by_move, | ||
| llargs, | ||
| &args[i], | ||
| lifetime_ends_after_call, | ||
| ); | ||
| } | ||
| } | ||
| tuple.layout.fields.count() | ||
|
|
||
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
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
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 |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| /// Represents a set of `Size` values as a sorted list of ranges. | ||
| /// | ||
| /// These are (offset, length) pairs, and they are sorted and mutually disjoint, | ||
| /// and never adjacent (i.e. there's always a gap between two of them). | ||
| #[derive(Debug, Clone)] | ||
| pub struct RangeSet<T>(pub Vec<(T, T)>); | ||
|
|
||
| impl<T> RangeSet<T> | ||
| where | ||
| T: Copy + Ord + Default, | ||
| T: core::ops::Add<Output = T>, | ||
| T: core::ops::Sub<Output = T>, | ||
| { | ||
| pub fn new() -> Self { | ||
| Self(Vec::new()) | ||
| } | ||
|
|
||
| pub fn add_range(&mut self, offset: T, size: T) { | ||
| if size == T::default() { | ||
| // No need to track empty ranges. | ||
| return; | ||
| } | ||
| let v = &mut self.0; | ||
| // We scan for a partition point where the left partition is all the elements that end | ||
| // strictly before we start. Those are elements that are too "low" to merge with us. | ||
| let idx = | ||
| v.partition_point(|&(other_offset, other_size)| other_offset + other_size < offset); | ||
| // Now we want to either merge with the first element of the second partition, or insert ourselves before that. | ||
| if let Some(&(other_offset, other_size)) = v.get(idx) | ||
| && offset + size >= other_offset | ||
| { | ||
| // Their end is >= our start (otherwise it would not be in the 2nd partition) and | ||
| // our end is >= their start. This means we can merge the ranges. | ||
| let new_start = other_offset.min(offset); | ||
| let mut new_end = (other_offset + other_size).max(offset + size); | ||
| // We grew to the right, so merge with overlapping/adjacent elements. | ||
| // (We also may have grown to the left, but that can never make us adjacent with | ||
| // anything there since we selected the first such candidate via `partition_point`.) | ||
| let mut scan_right = 1; | ||
| while let Some(&(next_offset, next_size)) = v.get(idx + scan_right) | ||
| && new_end >= next_offset | ||
| { | ||
| // Increase our size to absorb the next element. | ||
| new_end = new_end.max(next_offset + next_size); | ||
| // Look at the next element. | ||
| scan_right += 1; | ||
| } | ||
| // Update the element we grew. | ||
| v[idx] = (new_start, new_end - new_start); | ||
| // Remove the elements we absorbed (if any). | ||
| if scan_right > 1 { | ||
| drop(v.drain((idx + 1)..(idx + scan_right))); | ||
| } | ||
| } else { | ||
| // Insert new element. | ||
| v.insert(idx, (offset, size)); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
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.