Skip to content

Potential Unsound Issue in SharedMutex #3

@CXWorks

Description

@CXWorks

Hi, thanks for your time to read this issue.

Our static analyzer find a potential unsound issue in SharedMutex, where the drop fuction needs to check if the pthread_mutex is unlocked and destroying a locked pthread_mutex is considered as undefined behaviors in doc.

fn drop(&mut self) {
if getpid() == self.owner_pid {
check_libc_err(unsafe { pthread_mutex_destroy(self.mutex.get_mut()) })
.expect("cannot destroy mutex");
}
}

A potentail PoC code is like:

#[deny(unsafe_code)]
use process_sync::SharedMutex;

fn main() {
    if let Ok(mut s) = SharedMutex::new(){
        if let Ok(_) = s.lock(){
            drop(s);
        }
    }
}

FYI, a good way to handle is like std library:

https://github.com/rust-lang/rust/blob/414482f6a0d4e7290f614300581a0b55442552a3/library/std/src/sys/sync/mutex/pthread.rs#L55-L64

Thanks again for your time.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions