Skip to content

Commit 6e5fdee

Browse files
committed
Add a new lint for catching unbound lifetimes in return values
This lint is based on some unsoundness found in Libra during security audits. Some function signatures can look sound, but based on the concrete types that end up being used, lifetimes may be unbound instead of anchored to the references of the arguments or the fields of a struct. When combined with unsafe code, this can cause transmuted lifetimes to be not what was intended.
1 parent b245fbd commit 6e5fdee

12 files changed

+245
-25
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1226,6 +1226,7 @@ Released 2018-09-13
12261226
[`try_err`]: https://rust-lang.github.io/rust-clippy/master/index.html#try_err
12271227
[`type_complexity`]: https://rust-lang.github.io/rust-clippy/master/index.html#type_complexity
12281228
[`type_repetition_in_bounds`]: https://rust-lang.github.io/rust-clippy/master/index.html#type_repetition_in_bounds
1229+
[`unbound_return_lifetimes`]: https://rust-lang.github.io/rust-clippy/master/index.html#unbound_return_lifetimes
12291230
[`unicode_not_nfc`]: https://rust-lang.github.io/rust-clippy/master/index.html#unicode_not_nfc
12301231
[`unimplemented`]: https://rust-lang.github.io/rust-clippy/master/index.html#unimplemented
12311232
[`uninit_assumed_init`]: https://rust-lang.github.io/rust-clippy/master/index.html#uninit_assumed_init

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
A collection of lints to catch common mistakes and improve your [Rust](https://github.com/rust-lang/rust) code.
88

9-
[There are 340 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
9+
[There are 341 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
1010

1111
We have a bunch of lint categories to allow you to choose how much Clippy is supposed to ~~annoy~~ help you:
1212

clippy_lints/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ pub mod transmuting_null;
290290
pub mod trivially_copy_pass_by_ref;
291291
pub mod try_err;
292292
pub mod types;
293+
pub mod unbound_return_lifetimes;
293294
pub mod unicode;
294295
pub mod unsafe_removed_from_name;
295296
pub mod unused_io_amount;
@@ -770,6 +771,7 @@ pub fn register_plugins(store: &mut lint::LintStore, sess: &Session, conf: &Conf
770771
&types::UNIT_CMP,
771772
&types::UNNECESSARY_CAST,
772773
&types::VEC_BOX,
774+
&unbound_return_lifetimes::UNBOUND_RETURN_LIFETIMES,
773775
&unicode::NON_ASCII_LITERAL,
774776
&unicode::UNICODE_NOT_NFC,
775777
&unicode::ZERO_WIDTH_SPACE,
@@ -937,6 +939,7 @@ pub fn register_plugins(store: &mut lint::LintStore, sess: &Session, conf: &Conf
937939
store.register_late_pass(|| box trait_bounds::TraitBounds);
938940
store.register_late_pass(|| box comparison_chain::ComparisonChain);
939941
store.register_late_pass(|| box mul_add::MulAddCheck);
942+
store.register_late_pass(|| box unbound_return_lifetimes::UnboundReturnLifetimes);
940943
store.register_early_pass(|| box reference::DerefAddrOf);
941944
store.register_early_pass(|| box reference::RefInDeref);
942945
store.register_early_pass(|| box double_parens::DoubleParens);
@@ -1300,6 +1303,7 @@ pub fn register_plugins(store: &mut lint::LintStore, sess: &Session, conf: &Conf
13001303
LintId::of(&types::UNIT_CMP),
13011304
LintId::of(&types::UNNECESSARY_CAST),
13021305
LintId::of(&types::VEC_BOX),
1306+
LintId::of(&unbound_return_lifetimes::UNBOUND_RETURN_LIFETIMES),
13031307
LintId::of(&unicode::ZERO_WIDTH_SPACE),
13041308
LintId::of(&unsafe_removed_from_name::UNSAFE_REMOVED_FROM_NAME),
13051309
LintId::of(&unused_io_amount::UNUSED_IO_AMOUNT),
@@ -1547,6 +1551,7 @@ pub fn register_plugins(store: &mut lint::LintStore, sess: &Session, conf: &Conf
15471551
LintId::of(&types::CAST_PTR_ALIGNMENT),
15481552
LintId::of(&types::CAST_REF_TO_MUT),
15491553
LintId::of(&types::UNIT_CMP),
1554+
LintId::of(&unbound_return_lifetimes::UNBOUND_RETURN_LIFETIMES),
15501555
LintId::of(&unicode::ZERO_WIDTH_SPACE),
15511556
LintId::of(&unused_io_amount::UNUSED_IO_AMOUNT),
15521557
LintId::of(&unwrap::PANICKING_UNWRAP),
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
use if_chain::if_chain;
2+
use rustc::declare_lint_pass;
3+
use rustc::hir::*;
4+
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
5+
use rustc_session::declare_tool_lint;
6+
7+
use crate::utils::span_lint;
8+
9+
declare_clippy_lint! {
10+
/// **What it does:** Checks for lifetime annotations on return values
11+
/// which might not always get bound.
12+
///
13+
/// **Why is this bad?** If the function contains unsafe code which
14+
/// transmutes to the lifetime, the resulting lifetime may live longer
15+
/// than was intended.
16+
///
17+
/// **Known problems*: None.
18+
///
19+
/// **Example:**
20+
/// ```rust
21+
/// struct WrappedStr(str);
22+
///
23+
/// // Bad: unbound return lifetime causing unsoundnes
24+
/// fn foo<'a>(x: impl AsRef<str> + 'a) -> &'a WrappedStr {
25+
/// let s = x.as_ref();
26+
/// unsafe { &*(s as *const str as *const WrappedStr) }
27+
/// }
28+
///
29+
/// // Good: bound return lifetime is sound
30+
/// fn foo<'a>(x: &'a str) -> &'a WrappedStr {
31+
/// unsafe { &*(x as *const str as *const WrappedStr) }
32+
/// }
33+
/// ```
34+
pub UNBOUND_RETURN_LIFETIMES,
35+
correctness,
36+
"unbound lifetimes in function return values"
37+
}
38+
39+
declare_lint_pass!(UnboundReturnLifetimes => [UNBOUND_RETURN_LIFETIMES]);
40+
41+
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnboundReturnLifetimes {
42+
fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) {
43+
if let ItemKind::Fn(ref sig, ref generics, _id) = item.kind {
44+
check_fn_inner(cx, &sig.decl, generics, None);
45+
}
46+
}
47+
48+
fn check_impl_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx ImplItem) {
49+
if let ImplItemKind::Method(ref sig, _) = item.kind {
50+
let parent_generics = impl_generics(cx, item.hir_id);
51+
check_fn_inner(cx, &sig.decl, &item.generics, parent_generics);
52+
}
53+
}
54+
}
55+
56+
fn check_fn_inner<'a, 'tcx>(
57+
cx: &LateContext<'a, 'tcx>,
58+
decl: &'tcx FnDecl,
59+
generics: &'tcx Generics,
60+
parent_generics: Option<(&'tcx Generics, Option<&'tcx Generics>, Option<&'tcx Generics>)>,
61+
) {
62+
if let FunctionRetTy::Return(ref typ) = decl.output {
63+
if let TyKind::Rptr(ref lifetime, _) = typ.kind {
64+
if let LifetimeName::Param(_) = lifetime.name {
65+
let target_lifetime = lifetime;
66+
67+
// check function generics
68+
// the target lifetime parameter must appear on the left of some outlives relation
69+
if let Some(param) = generics.get_named(target_lifetime.name.ident().name) {
70+
if param
71+
.bounds
72+
.iter()
73+
.any(|b| if let GenericBound::Outlives(_) = b { true } else { false })
74+
{
75+
return;
76+
}
77+
}
78+
79+
// check parent generics
80+
// the target lifetime parameter must appear on the left of some outlives relation
81+
if let Some((ref parent_generics, _, _)) = parent_generics {
82+
if let Some(param) = parent_generics.get_named(target_lifetime.name.ident().name) {
83+
if param
84+
.bounds
85+
.iter()
86+
.any(|b| if let GenericBound::Outlives(_) = b { true } else { false })
87+
{
88+
return;
89+
}
90+
}
91+
}
92+
93+
// check type generics
94+
// the target lifetime parameter must be included in the struct
95+
if let Some((_, _, Some(ref typ_generics))) = parent_generics {
96+
if typ_generics.get_named(target_lifetime.name.ident().name).is_some() {
97+
return;
98+
}
99+
}
100+
101+
// check arguments
102+
// the target lifetime parameter must be included as a lifetime of a reference
103+
for input in decl.inputs.iter() {
104+
if let TyKind::Rptr(ref lifetime, _) = input.kind {
105+
if lifetime.name == target_lifetime.name {
106+
return;
107+
}
108+
}
109+
}
110+
111+
span_lint(
112+
cx,
113+
UNBOUND_RETURN_LIFETIMES,
114+
target_lifetime.span,
115+
"lifetime is unconstrained",
116+
);
117+
}
118+
}
119+
}
120+
}
121+
122+
fn impl_generics<'tcx>(
123+
cx: &LateContext<'_, 'tcx>,
124+
hir_id: HirId,
125+
) -> Option<(&'tcx Generics, Option<&'tcx Generics>, Option<&'tcx Generics>)> {
126+
let parent_impl = cx.tcx.hir().get_parent_item(hir_id);
127+
if_chain! {
128+
if parent_impl != CRATE_HIR_ID;
129+
if let Node::Item(item) = cx.tcx.hir().get(parent_impl);
130+
if let ItemKind::Impl(_, _, _, ref parent_generics, ref _trait_ref, ref ty, _) = item.kind;
131+
then {
132+
if let TyKind::Path(ref qpath) = ty.kind {
133+
if let Some(typ_def_id) = cx.tables.qpath_res(qpath, ty.hir_id).opt_def_id() {
134+
let typ_generics = cx.tcx.hir().get_generics(typ_def_id);
135+
return Some((parent_generics, None, typ_generics))
136+
}
137+
}
138+
}
139+
}
140+
None
141+
}

src/lintlist/mod.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pub use lint::Lint;
66
pub use lint::LINT_LEVELS;
77

88
// begin lint list, do not remove this comment, it’s used in `update_lints`
9-
pub const ALL_LINTS: [Lint; 340] = [
9+
pub const ALL_LINTS: [Lint; 341] = [
1010
Lint {
1111
name: "absurd_extreme_comparisons",
1212
group: "correctness",
@@ -2037,6 +2037,13 @@ pub const ALL_LINTS: [Lint; 340] = [
20372037
deprecation: None,
20382038
module: "trait_bounds",
20392039
},
2040+
Lint {
2041+
name: "unbound_return_lifetimes",
2042+
group: "correctness",
2043+
desc: "unbound lifetimes in function return values",
2044+
deprecation: None,
2045+
module: "unbound_return_lifetimes",
2046+
},
20402047
Lint {
20412048
name: "unicode_not_nfc",
20422049
group: "pedantic",

tests/ui/extra_unused_lifetimes.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
dead_code,
44
clippy::needless_lifetimes,
55
clippy::needless_pass_by_value,
6-
clippy::trivially_copy_pass_by_ref
6+
clippy::trivially_copy_pass_by_ref,
7+
clippy::unbound_return_lifetimes
78
)]
89
#![warn(clippy::extra_unused_lifetimes)]
910

tests/ui/extra_unused_lifetimes.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
error: this lifetime isn't used in the function definition
2-
--> $DIR/extra_unused_lifetimes.rs:14:14
2+
--> $DIR/extra_unused_lifetimes.rs:15:14
33
|
44
LL | fn unused_lt<'a>(x: u8) {}
55
| ^^
66
|
77
= note: `-D clippy::extra-unused-lifetimes` implied by `-D warnings`
88

99
error: this lifetime isn't used in the function definition
10-
--> $DIR/extra_unused_lifetimes.rs:16:25
10+
--> $DIR/extra_unused_lifetimes.rs:17:25
1111
|
1212
LL | fn unused_lt_transitive<'a, 'b: 'a>(x: &'b u8) {
1313
| ^^
1414

1515
error: this lifetime isn't used in the function definition
16-
--> $DIR/extra_unused_lifetimes.rs:41:10
16+
--> $DIR/extra_unused_lifetimes.rs:42:10
1717
|
1818
LL | fn x<'a>(&self) {}
1919
| ^^
2020

2121
error: this lifetime isn't used in the function definition
22-
--> $DIR/extra_unused_lifetimes.rs:67:22
22+
--> $DIR/extra_unused_lifetimes.rs:68:22
2323
|
2424
LL | fn unused_lt<'a>(x: u8) {}
2525
| ^^

tests/ui/needless_lifetimes.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
#![warn(clippy::needless_lifetimes)]
2-
#![allow(dead_code, clippy::needless_pass_by_value, clippy::trivially_copy_pass_by_ref)]
2+
#![allow(
3+
dead_code,
4+
clippy::needless_pass_by_value,
5+
clippy::trivially_copy_pass_by_ref,
6+
clippy::unbound_return_lifetimes
7+
)]
38

49
fn distinct_lifetimes<'a, 'b>(_x: &'a u8, _y: &'b u8, _z: u8) {}
510

0 commit comments

Comments
 (0)