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

*session_ptr and *pp_quote_config may be leaked if overwrites to #441

Open
labyrinth-ssr opened this issue Jul 28, 2023 · 0 comments
Open

Comments

@labyrinth-ssr
Copy link

labyrinth-ssr commented Jul 28, 2023

let ptr = Box::into_raw(Box::new(session_info));
*session_ptr = ptr as * mut _ as usize;

let p_ret_ql_config = Box::into_raw(Box::new(ql_config));
unsafe {
*pp_quote_config = p_ret_ql_config;
}

with Box::into_raw(), the pointee is on the heap. Multiple assignments will cause leak of the old value.

Probable fix is like:
If session_request_safe should only be called once, adding an Atomic to guarantee assigning only once.

const UNINITIALIZED: usize = 0;
const INITIALIZING: usize = 1;
const INITIALIZED: usize = 2;
static GLOBAL_INIT: AtomicUsize = AtomicUsize::new(UNINITIALIZED);
pub struct SetGlobalDefaultError {
    _no_construct: (),
}

// in `session_request_safe`
       if GLOBAL_INIT
                .compare_exchange(
                    UNINITIALIZED,
                    INITIALIZING,
                    Ordering::SeqCst,
                    Ordering::SeqCst,
                )
                .is_ok()
            {
                let ptr = Box::into_raw(Box::new(session_info));
                *session_ptr = ptr as * mut _ as usize;
            }

Otherwise add the else branch:

           else {
              drop(Box::from_raw(*session_ptr));
              let ptr = Box::into_raw(Box::new(session_info));
              *session_ptr = ptr as * mut _ as usize;
          }
@labyrinth-ssr labyrinth-ssr changed the title session_ptr and p_ret_ql_config may be leaked if overwrites to *session_ptr and *pp_quote_config may be leaked if overwrites to Jul 28, 2023
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