Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Rust: Exclude self parameter accesses from rust/access-after-lifetime-ended #21155

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
geoffw0 wants to merge 5 commits into github:main
base: main
Choose a base branch
Loading
from geoffw0:lifetimeself
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ module AccessAfterLifetime {
// parameter
exists(Callable c |
var.getParameter().getEnclosingCallable() = c and
scope.getParentNode() = c
scope.getParentNode() = c and
not var.getParameter() instanceof SelfParam
)
}

Expand Down
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
category: minorAnalysis
---
* Fixed false positives from the `rust/access-after-lifetime-ended` query, involving pointers to fields of `self`.
47 changes: 47 additions & 0 deletions rust/ql/test/query-tests/security/CWE-825/lifetime.rs
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -857,3 +857,50 @@ pub fn test_generic() {
let result = generic_caller::<MyProcessor>();
println!(" result = {result}");
}

// --- self ---

struct MyObjectWithGetters {
value: i64
}

impl MyObjectWithGetters {
pub fn new(_value: i64) -> Self {
Self {
value: _value
}
}

pub unsafe fn get_ptr1(&self) -> *const i64 {
&raw const self.value // $ MISSING: Source[rust/access-after-lifetime-ended]=self_value
// (the returned pointer is valid as long as the containing object is)
}

pub unsafe fn get_ptr2(self) -> *const i64 {
&raw const self.value // $ MISSING: Source[rust/access-after-lifetime-ended]=self_value
// (the returned pointer is valid as long as the containing object is)
}
}

pub fn test_get_self() {
let ptr1: *const i64;
let ptr2: *const i64;

unsafe {
let obj = MyObjectWithGetters::new(1111);
ptr1 = obj.get_ptr1();
ptr2 = obj.get_ptr2();

let v1 = *ptr1; // GOOD
let v2 = *ptr2; // GOOD
Copy link
Contributor

@paldepind paldepind Jan 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is in fact bad.

I tried inserting use_the_stack(); above these two lines and can then observe this pointer access resulting in gibberish.

get_ptr2 receives a copy of MyObjectWithGetters which will sit on its stack. The &raw const foo.value then takes the address of the value field on the stack, and that becomes invalid after return.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's interesting ... then I think the first step to fixing this PR is going to be correcting and expanding the test cases.

paldepind reacted with thumbs up emoji
println!(" v1 = {}", v1);
println!(" v2 = {}", v2);
}

use_the_stack();

let v3 = unsafe { *ptr1 }; // $ MISSING: Alert[rust/access-after-lifetime-ended]=self_value
let v4 = unsafe { *ptr2 }; // $ MISSING: Alert[rust/access-after-lifetime-ended]=self_value
println!(" v3 = {} (!)", v3);
println!(" v4 = {} (!)", v4); // corrupt in practice
}
3 changes: 3 additions & 0 deletions rust/ql/test/query-tests/security/CWE-825/main.rs
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,7 @@ fn main() {

println!("test_generic:");
test_generic();

println!("test_get_self:");
test_get_self();
}

AltStyle によって変換されたページ (->オリジナル) /