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

Fix UB due to missing 'static on task::waker #2206

Merged
merged 2 commits into from Sep 5, 2020

Conversation

Darksonn
Copy link
Contributor

@Darksonn Darksonn commented Sep 2, 2020

The futures::task::waker function currently allows you to transmute an Arc<T> into a Waker, even if T is not 'static. This can cause an use-after-free.

use std::sync::Arc;
use futures::task::{waker, ArcWake};

struct MyRef<'a> {
    a: &'a str,
}

impl<'a> ArcWake for MyRef<'a> {
    fn wake_by_ref(arc_self: &Arc<Self>) {
        println!("{}", arc_self.a);
    }
}

fn main() {
    let string = "Hello World!".to_string();
    let waker = waker(Arc::new(MyRef { a: &string }));
    drop(string);
    waker.wake(); // This causes an use-after-free of the string.
}

playground

@taiki-e taiki-e merged commit 4d54611 into rust-lang:master Sep 5, 2020
@taiki-e
Copy link
Member

taiki-e commented Sep 5, 2020

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-task Area: futures::task
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants