- Notifications
You must be signed in to change notification settings - Fork 13.9k
cmse: lint on unions crossing the secure boundary #147697
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
Open
folkertdev wants to merge 2 commits into rust-lang:master Choose a base branch from folkertdev:cmse-lint-on-uninitialized
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
+473 −60
Open
Changes from all commits
Commits
Show all changes
2 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 |
---|---|---|
@@ -0,0 +1,188 @@ | ||
use rustc_abi::ExternAbi; | ||
use rustc_hir::{self as hir, Expr, ExprKind}; | ||
use rustc_middle::ty::layout::{LayoutCx, TyAndLayout}; | ||
use rustc_middle::ty::{self, TyCtxt, TypeVisitableExt}; | ||
use rustc_session::{declare_lint, declare_lint_pass}; | ||
| ||
use crate::{LateContext, LateLintPass, LintContext, lints}; | ||
| ||
declare_lint! { | ||
/// The `cmse_uninitialized_leak` lint detects values that may be (partially) uninitialized that | ||
/// cross the secure boundary. | ||
/// | ||
/// ### Example | ||
/// | ||
/// ```rust,ignore (ABI is only supported on thumbv8) | ||
/// extern "cmse-nonsecure-entry" fn foo() -> MaybeUninit<u64> { | ||
/// MaybeUninit::uninit() | ||
/// } | ||
/// ``` | ||
/// | ||
/// This will produce: | ||
/// | ||
/// ```text | ||
/// warning: passing a union across the security boundary may leak information | ||
/// --> lint_example.rs:2:5 | ||
/// | | ||
/// 2 | MaybeUninit::uninit() | ||
/// | ^^^^^^^^^^^^^^^^^^^^^ | ||
/// | | ||
/// = note: the bits not used by the current variant may contain stale secure data | ||
/// = note: `#[warn(cmse_uninitialized_leak)]` on by default | ||
/// ``` | ||
/// | ||
/// ### Explanation | ||
/// | ||
/// The cmse calling conventions normally take care of clearing registers to make sure that | ||
/// stale secure information is not observable from non-secure code. Uninitialized memory may | ||
/// still contain secret information, so the programmer must be careful when (partially) | ||
/// uninitialized values cross the secure boundary. This lint fires when a partially | ||
/// uninitialized value (e.g. a `union` value or a type with a niche) crosses the secure | ||
/// boundary, i.e.: | ||
/// | ||
/// - when returned from a `cmse-nonsecure-entry` function | ||
/// - when passed as an argument to a `cmse-nonsecure-call` function | ||
/// | ||
/// This lint is a best effort: not all cases of (partially) uninitialized data crossing the | ||
/// secure boundary are caught. | ||
pub CMSE_UNINITIALIZED_LEAK, | ||
Warn, | ||
"(partially) uninitialized value may leak secure information" | ||
} | ||
| ||
declare_lint_pass!(CmseUninitializedLeak => [CMSE_UNINITIALIZED_LEAK]); | ||
| ||
impl<'tcx> LateLintPass<'tcx> for CmseUninitializedLeak { | ||
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) { | ||
check_cmse_entry_return(cx, expr); | ||
check_cmse_call_call(cx, expr); | ||
} | ||
} | ||
| ||
fn check_cmse_call_call<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) { | ||
let ExprKind::Call(callee, arguments) = expr.kind else { | ||
return; | ||
}; | ||
| ||
// Determine the callee ABI. | ||
let callee_ty = cx.typeck_results().expr_ty(callee); | ||
let sig = match callee_ty.kind() { | ||
ty::FnPtr(poly_sig, header) if header.abi == ExternAbi::CmseNonSecureCall => { | ||
poly_sig.skip_binder() | ||
} | ||
_ => return, | ||
}; | ||
| ||
let fn_sig = cx.tcx.erase_and_anonymize_regions(sig); | ||
let typing_env = ty::TypingEnv::fully_monomorphized(); | ||
| ||
for (arg, ty) in arguments.iter().zip(fn_sig.inputs()) { | ||
// `impl Trait` is not allowed in the argument types. | ||
if ty.has_opaque_types() { | ||
continue; | ||
} | ||
| ||
let Ok(layout) = cx.tcx.layout_of(typing_env.as_query_input(*ty)) else { | ||
continue; | ||
}; | ||
| ||
if layout_contains_union(cx.tcx, &layout) { | ||
cx.emit_span_lint( | ||
CMSE_UNINITIALIZED_LEAK, | ||
arg.span, | ||
lints::CmseUnionMayLeakInformation, | ||
); | ||
} | ||
} | ||
} | ||
| ||
fn check_cmse_entry_return<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) { | ||
let owner = cx.tcx.hir_enclosing_body_owner(expr.hir_id); | ||
| ||
match cx.tcx.def_kind(owner) { | ||
hir::def::DefKind::Fn | hir::def::DefKind::AssocFn => {} | ||
_ => return, | ||
} | ||
| ||
// Only continue if the current expr is an (implicit) return. | ||
let body = cx.tcx.hir_body_owned_by(owner); | ||
let is_implicit_return = expr.hir_id == body.value.hir_id; | ||
if !(matches!(expr.kind, ExprKind::Ret(_)) || is_implicit_return) { | ||
return; | ||
} | ||
| ||
let sig = cx.tcx.fn_sig(owner).skip_binder(); | ||
if sig.abi() != ExternAbi::CmseNonSecureEntry { | ||
return; | ||
} | ||
| ||
let fn_sig = cx.tcx.instantiate_bound_regions_with_erased(sig); | ||
let fn_sig = cx.tcx.erase_and_anonymize_regions(fn_sig); | ||
let return_type = fn_sig.output(); | ||
| ||
// `impl Trait` is not allowed in the return type. | ||
if return_type.has_opaque_types() { | ||
return; | ||
} | ||
| ||
let typing_env = ty::TypingEnv::fully_monomorphized(); | ||
let Ok(ret_layout) = cx.tcx.layout_of(typing_env.as_query_input(return_type)) else { | ||
return; | ||
}; | ||
| ||
if layout_contains_union(cx.tcx, &ret_layout) { | ||
let return_expr_span = if is_implicit_return { | ||
match expr.kind { | ||
ExprKind::Block(block, _) => match block.expr { | ||
Some(tail) => tail.span, | ||
None => expr.span, | ||
}, | ||
_ => expr.span, | ||
} | ||
} else { | ||
expr.span | ||
}; | ||
| ||
cx.emit_span_lint( | ||
CMSE_UNINITIALIZED_LEAK, | ||
return_expr_span, | ||
lints::CmseUnionMayLeakInformation, | ||
); | ||
} | ||
} | ||
| ||
/// Check whether any part of the layout is a union, which may contain secure data still. | ||
fn layout_contains_union<'tcx>(tcx: TyCtxt<'tcx>, layout: &TyAndLayout<'tcx>) -> bool { | ||
if layout.ty.is_union() { | ||
return true; | ||
} | ||
| ||
let typing_env = ty::TypingEnv::fully_monomorphized(); | ||
let cx = LayoutCx::new(tcx, typing_env); | ||
| ||
match &layout.variants { | ||
rustc_abi::Variants::Single { .. } => { | ||
for i in 0..layout.fields.count() { | ||
if layout_contains_union(tcx, &layout.field(&cx, i)) { | ||
return true; | ||
} | ||
} | ||
} | ||
| ||
rustc_abi::Variants::Multiple { variants, .. } => { | ||
for (variant_idx, _vdata) in variants.iter_enumerated() { | ||
let variant_layout = layout.for_variant(&cx, variant_idx); | ||
| ||
for i in 0..variant_layout.fields.count() { | ||
if layout_contains_union(tcx, &variant_layout.field(&cx, i)) { | ||
return true; | ||
} | ||
} | ||
} | ||
} | ||
| ||
rustc_abi::Variants::Empty => {} | ||
} | ||
| ||
false | ||
} |
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
55 changes: 55 additions & 0 deletions 55 tests/ui/cmse-nonsecure/cmse-nonsecure-call/params-uninitialized.rs
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,55 @@ | ||
//@ add-core-stubs | ||
//@ compile-flags: --target thumbv8m.main-none-eabi --crate-type lib | ||
//@ needs-llvm-components: arm | ||
//@ check-pass | ||
#![feature(abi_cmse_nonsecure_call, no_core, lang_items)] | ||
#![no_core] | ||
#![allow(improper_ctypes_definitions)] | ||
| ||
extern crate minicore; | ||
use minicore::*; | ||
| ||
#[repr(Rust)] | ||
pub union ReprRustUnionU64 { | ||
_unused: u64, | ||
} | ||
| ||
#[repr(C)] | ||
pub union ReprCUnionU64 { | ||
_unused: u64, | ||
_unused1: u32, | ||
} | ||
| ||
#[repr(C)] | ||
pub struct ReprCAggregate { | ||
a: usize, | ||
b: ReprCUnionU64, | ||
} | ||
| ||
#[no_mangle] | ||
pub fn test_union( | ||
f1: extern "cmse-nonsecure-call" fn(ReprRustUnionU64), | ||
f2: extern "cmse-nonsecure-call" fn(ReprCUnionU64), | ||
f3: extern "cmse-nonsecure-call" fn(MaybeUninit<u32>), | ||
f4: extern "cmse-nonsecure-call" fn(MaybeUninit<u64>), | ||
f5: extern "cmse-nonsecure-call" fn((usize, MaybeUninit<u64>)), | ||
f6: extern "cmse-nonsecure-call" fn(ReprCAggregate), | ||
) { | ||
f1(ReprRustUnionU64 { _unused: 1 }); | ||
//~^ WARN passing a union across the security boundary may leak information | ||
| ||
f2(ReprCUnionU64 { _unused: 1 }); | ||
//~^ WARN passing a union across the security boundary may leak information | ||
| ||
f3(MaybeUninit::uninit()); | ||
//~^ WARN passing a union across the security boundary may leak information | ||
| ||
f4(MaybeUninit::uninit()); | ||
//~^ WARN passing a union across the security boundary may leak information | ||
| ||
f5((0, MaybeUninit::uninit())); | ||
//~^ WARN passing a union across the security boundary may leak information | ||
| ||
f6(ReprCAggregate { a: 0, b: ReprCUnionU64 { _unused: 1 } }); | ||
//~^ WARN passing a union across the security boundary may leak information | ||
} |
51 changes: 51 additions & 0 deletions 51 tests/ui/cmse-nonsecure/cmse-nonsecure-call/params-uninitialized.stderr
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,51 @@ | ||
warning: passing a union across the security boundary may leak information | ||
--> $DIR/params-uninitialized.rs:38:8 | ||
| | ||
LL | f1(ReprRustUnionU64 { _unused: 1 }); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: the bits not used by the current variant may contain stale secure data | ||
= note: `#[warn(cmse_uninitialized_leak)]` on by default | ||
| ||
warning: passing a union across the security boundary may leak information | ||
--> $DIR/params-uninitialized.rs:41:8 | ||
| | ||
LL | f2(ReprCUnionU64 { _unused: 1 }); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: the bits not used by the current variant may contain stale secure data | ||
| ||
warning: passing a union across the security boundary may leak information | ||
--> $DIR/params-uninitialized.rs:44:8 | ||
| | ||
LL | f3(MaybeUninit::uninit()); | ||
| ^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: the bits not used by the current variant may contain stale secure data | ||
| ||
warning: passing a union across the security boundary may leak information | ||
--> $DIR/params-uninitialized.rs:47:8 | ||
| | ||
LL | f4(MaybeUninit::uninit()); | ||
| ^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: the bits not used by the current variant may contain stale secure data | ||
| ||
warning: passing a union across the security boundary may leak information | ||
--> $DIR/params-uninitialized.rs:50:8 | ||
| | ||
LL | f5((0, MaybeUninit::uninit())); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: the bits not used by the current variant may contain stale secure data | ||
| ||
warning: passing a union across the security boundary may leak information | ||
--> $DIR/params-uninitialized.rs:53:8 | ||
| | ||
LL | f6(ReprCAggregate { a: 0, b: ReprCUnionU64 { _unused: 1 } }); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: the bits not used by the current variant may contain stale secure data | ||
| ||
warning: 6 warnings emitted | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add cases where the unions are contained within other types to these tests?