lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date:   Mon, 25 Sep 2023 11:51:21 -0700
From:   Boqun Feng <boqun.feng@...il.com>
To:     Benno Lossin <benno.lossin@...ton.me>
Cc:     Alice Ryhl <aliceryhl@...gle.com>,
        Wedson Almeida Filho <wedsonaf@...il.com>,
        rust-for-linux@...r.kernel.org, Miguel Ojeda <ojeda@...nel.org>,
        Alex Gaynor <alex.gaynor@...il.com>,
        Gary Guo <gary@...yguo.net>,
        Björn Roy Baron <bjorn3_gh@...tonmail.com>,
        Andreas Hindborg <a.hindborg@...sung.com>,
        linux-kernel@...r.kernel.org,
        Wedson Almeida Filho <walmeida@...rosoft.com>
Subject: Re: [PATCH v2 2/2] rust: arc: remove `ArcBorrow` in favour of
 `WithRef`

On Mon, Sep 25, 2023 at 05:00:45PM +0000, Benno Lossin wrote:
> On 25.09.23 18:16, Boqun Feng wrote:
> > On Mon, Sep 25, 2023 at 03:07:44PM +0000, Benno Lossin wrote:
> >> ```rust
> >> struct MutatingDrop {
> >>       value: i32,
> >> }
> >>
> >> impl Drop for MutatingDrop {
> >>       fn drop(&mut self) {
> >>           self.value = 0;
> >>       }
> >> }
> >>
> >> let arc = Arc::new(MutatingDrop { value: 42 });
> >> let wr = arc.as_with_ref(); // this creates a shared `&` reference to the MutatingDrop
> >> let arc2: Arc<MutatingDrop> = wr.into(); // increments the reference count to 2
> > 
> > More precisely, here we did a
> > 
> > 	&WithRef<_> -> NonNull<WithRef<_>>
> > 
> > conversion, and later on, we may use the `NonNull<WithRef<_>>` in
> > `drop` to get a `Box<WithRef<_>>`.
> 
> Indeed.
> 

Can we workaround this issue by (ab)using the `UnsafeCell` inside
`WithRef<T>`?

impl<T: ?Sized> From<&WithRef<T>> for Arc<T> {
    fn from(b: &WithRef<T>) -> Self {
        // SAFETY: The existence of the references proves that
	// `b.refcount.get()` is a valid pointer to `WithRef<T>`.
	let ptr = unsafe { NonNull::new_unchecked(b.refcount.get().cast::<WithRef<T>>()) };

	// SAFETY: see the SAFETY above `let ptr = ..` line.
        ManuallyDrop::new(unsafe { Arc::from_inner(ptr) })
            .deref()
            .clone()
    }
}

This way, the raw pointer in the new Arc no longer derives from the
reference of `WithRef<T>`.

Regards,
Boqun

> > 
> >> drop(arc); // this decrements the reference count to 1
> >> drop(arc2); // this decrements the reference count to 0, so it will drop it
> >> ```
> >> When dropping `arc2` it will run the destructor for `MutatingDrop`,
> >> which mutates `value`. This is a problem, because the mutable reference
> >> supplied was derived from a `&`, that is not allowed in Rust.
> >>
> > 
> > Is this an UB? I kinda wonder what's the real damage we can get, because
> > in this case, we just use a reference to carry a value of a pointer,
> > i.e.
> > 
> > 	ptr -> reference -> ptr
> > 
> > I cannot think of any real damage compiler can make, but I'm happy to be
> > surprised ;-)
> 
> This is UB, so anything can happen :)
> 
> -- 
> Cheers,
> Benno
> 
> 

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ