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

Missing pointer null checks #380

Closed
marco-vassena opened this issue Jan 12, 2024 · 4 comments · Fixed by #402
Closed

Missing pointer null checks #380

marco-vassena opened this issue Jan 12, 2024 · 4 comments · Fixed by #402

Comments

@marco-vassena
Copy link

marco-vassena commented Jan 12, 2024

Functions set_boxed_mut_ptr and set_arc_mut_ptr assume that the caller pass non-null pointers. It'd be safer to explicitly check that the pointers are not null and return rustls_result.

@jsha
Copy link
Collaborator

jsha commented Jan 18, 2024

One thing that's tricky here: ideally we want all our NULL checks to be at the top of the function, so we can avoid doing any work or allocating anything before potentially bailing out. But set_boxed_mut_ptr / set_arc_mut_ptr are generally called at the end of the function.

One way to do this would be to keep the requirement that set_boxed_mut_ptr / set_arc_mut_ptr must not be called with NULL pointers, but additionally enforce that invariant with a panic. For functions that correctly check for NULL at the top, presumably the compiler would be smart enough to eliminate the second check. And for functions that forget to check for NULL, there would be a panic rather than undefined behavior.

Another possibility would be to take &mut *mut T instead of *mut *mut T, and rely on our existing try_ macros to produce the reference, which we would then know is non-NULL.

@jsha
Copy link
Collaborator

jsha commented Jan 18, 2024

Yet another way to do this would be to make inner functions that return T and then have a helper function that (a) checks the out pointer for NULL, (b) calls the inner function, and (c) assigns the result to the target of the out pointer.

@ctz
Copy link
Member

ctz commented Jan 18, 2024

Another possibility would be to take &mut *mut T instead of *mut *mut T, and rely on our existing try_ macros to produce the reference, which we would then know is non-NULL.

I like this option. We can produce the reference at the top of the function (with the NULL check and early return), and that will eliminate the late failures.

@cpu
Copy link
Member

cpu commented Apr 2, 2024

@marco-vassena Thanks for the report. We've implemented a fix in #402 that should make this a harder trap to fall into going forward.

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 a pull request may close this issue.

4 participants