Skip to content
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

Kani does not detect UB when generating an invalid slice reference from an invalid slice pointer #3498

Closed
celinval opened this issue Sep 5, 2024 · 2 comments · Fixed by #3513
Assignees
Labels
[C] Bug This is a bug. Something isn't working. [F] Soundness Kani failed to detect an issue

Comments

@celinval
Copy link
Contributor

celinval commented Sep 5, 2024

I tried this code:

//! Converting a raw pointer to a reference should trigger UB if the metadata is not valid.
#![feature(set_ptr_value)]

// Generate invalid fat pointer by incrementing the pointer address without adjusting the metadata.
#[kani::proof]
fn check_invalid_read() {
    let data = "hello";
    let ptr = data as *const str;
    // This should trigger UB since the metadata does not get adjusted.
    let val = unsafe { &*ptr.byte_add(1) };
    assert_eq!(val.len(), data.len());
}

// Generate invalid fat pointer by combining the metadata.
#[kani::proof]
fn check_with_metadata() {
    let short = [0u32; 2];
    let long = [0u32; 10];
    let ptr = &short as *const [u32];
    // This should trigger UB since the slice is not valid for the new length.
    let fake_long = unsafe { &*ptr.with_metadata_of(&long) };
    assert_eq!(fake_long.len(), long.len());
}

using the following command line invocation:

kani invalid_fat_reference.rs

with Kani version: 0.54.0-dev

I expected to see this happen: Both harnesses should fail due to UB. Note that both cases fail in MIRI.

Instead, this happened: Verification succeeds

@celinval celinval added [C] Bug This is a bug. Something isn't working. [F] Soundness Kani failed to detect an issue labels Sep 5, 2024
@celinval
Copy link
Contributor Author

celinval commented Sep 5, 2024

I believe this is related to #2975, but we probably forgot to check for fat pointers.

@zhassan-aws zhassan-aws self-assigned this Sep 6, 2024
@celinval
Copy link
Contributor Author

celinval commented Sep 6, 2024

Actually, Kani does find an error with the version of check_with_metadata described in the issue. But if you replace the array by a string slice, verification succeeds. 😮

#[kani::proof]
fn check_with_metadata() {
    let short = "sh";
    let long = "longer";
    let ptr = short as *const str;
    // This should trigger UB since the slice is not valid for the new length.
    let fake_long = unsafe { &*ptr.with_metadata_of(long) };
    assert_eq!(fake_long.len(), long.len());
}

github-merge-queue bot pushed a commit that referenced this issue Sep 18, 2024
…and str's (#3513)

As pointed out in #3498, validity checks for pointer to reference casts
(added in #3221) were not instrumented in the case of fat pointers (e.g.
array and string slices). This PR extends the instrumentation of
validity checks to handle those cases.

Resolves #3498 

By submitting this pull request, I confirm that my contribution is made
under the terms of the Apache 2.0 and MIT licenses.

---------

Co-authored-by: Celina G. Val <celinval@amazon.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[C] Bug This is a bug. Something isn't working. [F] Soundness Kani failed to detect an issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants