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

NULL safe set_boxed_mut_ptr/set_arc_mut_ptr #402

Merged
merged 1 commit into from
Apr 2, 2024

Conversation

cpu
Copy link
Member

@cpu cpu commented Mar 29, 2024

Previously the set_boxed_mut_ptr() and set_arc_mut_ptr() helper fns used for assigning out parameters across the FFI boundary took *mut *mut C and *mut *const C for the destination argument dst. Using these safely required callers always verify that dst != NULL. In practice it's very easy to forget to do this and danger lurks!

We could modify these helpers to do NULL checking, but we tend to use them near the end of a function to assign a result in a success case and we would prefer NULL checking happen at the beginning of the function.

One proposed solution is to modify these setter functions to take &mut *mut C and &mut *const C. By using new helper macros to carefully construct a &mut from the input double pointer we can front-load the NULL check and the assignment in the set fns can proceed knowing there's no possibility for a NULL outer pointer.

This commit implements this strategy, updating the argument type of set_boxed_mut_ptr and set_arc_mut_ptr to take &mut (*const|*mut) C. New try_mut_from_ptr_ptr and try_ref_from_ptr_ptr macros allow converting from *mut *mut C and *mut *const C to the reference types, bailing early for NULL.

Resolves #380

Previously the `set_boxed_mut_ptr()` and `set_arc_mut_ptr()` helper fns
used for assigning out parameters across the FFI boundary took `*mut
*mut C` and `*mut *const C` for the destination argument `dst`. Using
these safely required callers always verify that `dst != NULL`. In
practice it's very easy to forget to do this and danger lurks!

We could modify these helpers to do the check itself, but we tend to use
these fns near the end of a function to assign a result in a success
case and we would prefer `NULL` checking happen at the beginning of the
function.

One proposed solution is to modify these setter functions to take `&mut
*mut C` and `&mut *const C`. By using new helper fns to carefully
construct a `&mut` from the input double pointer we can front-load
the `NULL` check and the assignment in the set fns can proceed knowing
there's no possibility for a `NULL` outer pointer.

This commit implements this strategy, updating the argument type of
`set_boxed_mut_ptr` and `set_arc_mut_ptr` to take `&mut (*const|*mut)
C`. New `try_mut_from_ptr_ptr` and `try_ref_from_ptr_ptr` macros allow
converting from `*mut *mut C` and `*mut *const C` to the reference
types, bailing early for `NULL`.
@cpu
Copy link
Member Author

cpu commented Mar 29, 2024

I think the fn names and rustdoc could use some bike shedding but I wanted to get this up for input before I log off. 💤

@cpu cpu requested a review from ctz March 29, 2024 22:05
@cpu cpu self-assigned this Mar 30, 2024
@cpu
Copy link
Member Author

cpu commented Apr 2, 2024

@ctz Did you want to give this a review pass since you were interested in #380 ?

Copy link
Member

@ctz ctz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mental note to pull these into openssl-compat

@cpu
Copy link
Member Author

cpu commented Apr 2, 2024

Thanks for taking a look.

Mental note to pull these into openssl-compat

#404 might be of interest in that area as well.

@cpu cpu merged commit f935f30 into rustls:main Apr 2, 2024
21 checks passed
@cpu cpu deleted the cpu-safer-out-params branch April 2, 2024 14:14
@cpu cpu mentioned this pull request Apr 2, 2024
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

Successfully merging this pull request may close these issues.

Missing pointer null checks
3 participants