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

Callback handling contains UB and may lead to use-after-free #9

Open
jasonyu1996 opened this issue Nov 27, 2024 · 0 comments
Open

Callback handling contains UB and may lead to use-after-free #9

jasonyu1996 opened this issue Nov 27, 2024 · 0 comments

Comments

@jasonyu1996
Copy link

ConfigBuilder::with_callback receives an owned cb the address of which is assigned to cfg.decode.context.

cfg.decode.context = &mut cb as *mut _ as *mut c_void;

The callback is later invoked in decode_callback.

let (res, bytes) = c(&(&*cfg).into(), pos);

However, cb only lives until the end of with_callback. This can lead to use-after-free, as demonstrated by this test case below:

#[test]
fn test_bad_callback() {
    let mut data = [18; 3];
    let c = {
        let counter = [1, 2, 3, 4, 5, 6, 7, 8];
        ConfigBuilder::with_callback(
            &mut data,
            move |c, p| {
            assert_eq!(counter, [1, 2, 3, 4, 5, 6, 7, 8]); // counter points to freed memory
            (Unknown::new(c.0.cpu.model + p[0]), 1) })
    }.unwrap().finish();
    unsafe {
        let mut ukn: pt_packet_unknown = std::mem::zeroed();
        let _ = c.0.decode.callback.unwrap()(&mut ukn,
                                         c.0.as_ref(), c.0.begin,
                                         c.0.decode.context);
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant