Skip to content

Race Condition which can lead to Undefined Behavior #1

@hildebrandmw

Description

@hildebrandmw

This is in reference this:

rcurs/src/rcu.rs

Lines 58 to 62 in c4728e8

pub fn get(&self) -> Guard<'_, T> {
let inner = self.ptr.load(Ordering::Relaxed).cast_const();
unsafe { (*inner).refs.take_ref() };
Guard { _marker: PhantomData, inner }
}

pub fn get(&self) -> Guard<'_, T> {                            // 1
    let inner = self.ptr.load(Ordering::Relaxed).cast_const(); // 2
    unsafe { (*inner).refs.take_ref() };                       // 3
    Guard { _marker: PhantomData, inner }                      // 4
}

The race condition exists between lines 2 and 3. Suppose thread A finishes line 2, acquiring a pointer to inner and is then pre-empted by the OS before it can increment the ref-count. While thread A is asleep, thread B invokes update, swaps the pointer, decrements the old ref-count, and deletes the old object.

When thread A resumes, inner is now dangling and line 3 triggers a segfault (if you are lucky) or writes randomly to memory (if you are not).

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions