Skip to content

Clone for UnionN is unsound with malicious P: ErasablePtr + Clone #89

@RustyYato

Description

@RustyYato

Currently ErasablePtr doesn't require that Clone preserves that the pointer is aligned. So it's possible to construct a type like this

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=292dca0467b3b0e1e8514b98087dc0c7

struct FancyBox {
    ptr: *mut u32,
}

impl FancyBox {
    fn new(value: u32) -> Self {
        FancyBox {
            ptr: Box::into_raw(Box::new(value))
        } 
    }
}

impl Clone for FancyBox {
    fn clone(&self) -> Self {
        let mut x = FancyBox::new(**self);
        
        x.ptr = x.ptr.map_addr(|ptr| ptr | 1);
        
        x
    }
}

impl core::ops::Deref for FancyBox {
    type Target = u32;

    fn deref(&self) -> &u32 {
        let ptr = self.ptr.map_addr(|ptr| ptr & !3usize);
        unsafe { &*ptr }
    }
}


fn main() {
    let x = FancyBox::new(10);
    
    assert_eq!(*x, 10);

    let y = x.clone();
    
    assert_eq!(*y, 10);
}

In particular in affects impl Clone for UnionN

let builder = unsafe { <$Builder<$($A,)*>>::new_unchecked() };

since here it's assumed that Clone will always return an aligned pointer.

So either ErasablePtr needs to be updated to include this, or UnionN needs to check that the cloned pointers are aligned.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions