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

some panic safety issues that cause double free in unwind branch #1

Open
cchanging opened this issue Apr 21, 2021 · 0 comments
Open

Comments

@cchanging
Copy link

Hi there, we detected a few potential panic safety issues in this library.
double free will appear mainly caused by panic happened between Box::from_raw and std::mem::forget.
example as follows:

pub unsafe extern "C" fn mesh_read(mesh: *mut Mesh, path: *const libc::c_char) -> bool {
let mut zms = Box::from_raw(mesh);
let path_str = CStr::from_ptr(path).to_str().unwrap_or_default();
let p = PathBuf::from(path_str);
let res = zms.read_from_path(&p).is_ok();
std::mem::forget(zms);
res
}

When panic occurs in let res = zms.read_from_path(&p).is_ok(); or the other codes between Box::from_raw and std::mem::forget , the zms will be dropped in unwinding and make the mesh an empty pointer, which will cause double-free bug in the upper function.
You can use mem:ManuallyDrop::new(Box::from_raw(mesh)) instead to avoid this problem.
This type of bug is difficult to detect with test cases, but it does pose a security risk.

@cchanging cchanging changed the title some panic safety issues that caused double free in unwind branch some panic safety issues that cause double free in unwind branch Apr 21, 2021
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