Rust: Query for dereferencing an invalid pointer#19080
Conversation
|
QHelp previews: rust/ql/src/queries/security/CWE-825/AccessInvalidPointer.qhelpAccess of invalid pointerDereferencing an invalid or dangling pointer may cause undefined behavior. Memory may be corrupted causing the program to crash or behave incorrectly, in some cases exposing the program to potential attacks. RecommendationWhen dereferencing a pointer in ExampleIn the following example, unsafe {
std::ptr::drop_in_place(ptr); // executes the destructor of `*ptr`
}
// ...
unsafe {
do_something(&*ptr); // BAD: dereferences `ptr`
}In this case, undefined behavior can be avoided by rearranging the code so that the dereferencing comes before the call to unsafe {
do_something(&*ptr); // GOOD: dereferences `ptr` while it is still valid
}
// ...
{
std::ptr::drop_in_place(ptr); // executes the destructor of `*ptr`
}References
|
rust/ql/lib/codeql/rust/security/AccessInvalidPointerExtensions.qll
Outdated
Show resolved
Hide resolved
|
DCA:
|
mchammer01
left a comment
There was a problem hiding this comment.
@geoffw0 👋🏻 - approving on behalf of Docs.
Left a few minor suggestions. Feel free to ignore the ones you don't agree with 😅
rust/ql/src/queries/security/CWE-825/AccessInvalidPointer.qhelp
Outdated
Show resolved
Hide resolved
rust/ql/src/queries/security/CWE-825/AccessInvalidPointer.qhelp
Outdated
Show resolved
Hide resolved
rust/ql/src/queries/security/CWE-825/AccessInvalidPointer.qhelp
Outdated
Show resolved
Hide resolved
Co-authored-by: mc <42146119+mchammer01@users.noreply.github.com>
Co-authored-by: mc <42146119+mchammer01@users.noreply.github.com>
|
@mchammer01 suggestions accepted, thank you. I still need to decide what to do about the results inside |
|
Re: results inside I'll do another DCA run to confirm we no longer get false positive results. |
|
DCA
|
|
#19195 should hopefully remove the data flow inconsistencies. |
|
I've merged in the fix for the consistency check. I think this PR is ready for approval now. |
hvitved
left a comment
There was a problem hiding this comment.
The two empty DataFlowConsistency.expected files should be deleted.
|
Good point. Done. |
hvitved
left a comment
There was a problem hiding this comment.
I have started a final DCA run.
|
DCA LGTM. There are 2 results, in both cases the sources and sinks are good but the flow misses an |
New query
rust/access-invalid-pointerthat spots dereferences of pointers that are invalid to dereference. There are tests for two general cases, but this query is only intended to catch the first one:deallocfunction before dereferencing. Analogous tocpp/use-after-free.TODO: